Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abstract_view_item.cc « views « interface « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f73183d07e9937cd9ac2c296eb42f6faf0573e42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup edinterface
 */

#include "BKE_context.h"

#include "BLI_listbase.h"
#include "BLI_string.h"

#include "WM_api.h"

#include "UI_interface.h"
#include "interface_intern.h"

#include "UI_abstract_view.hh"

namespace blender::ui {

/* ---------------------------------------------------------------------- */
/** \name View Reconstruction
 * \{ */

void AbstractViewItem::update_from_old(const AbstractViewItem &old)
{
  is_active_ = old.is_active_;
  is_renaming_ = old.is_renaming_;
}

/** \} */

/* ---------------------------------------------------------------------- */
/** \name Renaming
 * \{ */

bool AbstractViewItem::supports_renaming() const
{
  /* No renaming by default. */
  return false;
}
bool AbstractViewItem::rename(StringRefNull /*new_name*/)
{
  /* No renaming by default. */
  return false;
}

StringRef AbstractViewItem::get_rename_string() const
{
  /* No rename string by default. */
  return {};
}

bool AbstractViewItem::is_renaming() const
{
  return is_renaming_;
}

void AbstractViewItem::begin_renaming()
{
  AbstractView &view = get_view();
  if (view.is_renaming() || !supports_renaming()) {
    return;
  }

  if (view.begin_renaming()) {
    is_renaming_ = true;
  }

  StringRef initial_str = get_rename_string();
  std::copy(std::begin(initial_str), std::end(initial_str), std::begin(view.get_rename_buffer()));
}

void AbstractViewItem::rename_apply()
{
  const AbstractView &view = get_view();
  rename(view.get_rename_buffer().data());
  end_renaming();
}

void AbstractViewItem::end_renaming()
{
  if (!is_renaming()) {
    return;
  }

  is_renaming_ = false;

  AbstractView &view = get_view();
  view.end_renaming();
}

static AbstractViewItem *find_item_from_rename_button(const uiBut &rename_but)
{
  /* A minimal sanity check, can't do much more here. */
  BLI_assert(rename_but.type == UI_BTYPE_TEXT && rename_but.poin);

  LISTBASE_FOREACH (uiBut *, but, &rename_but.block->buttons) {
    if (but->type != UI_BTYPE_VIEW_ITEM) {
      continue;
    }

    uiButViewItem *view_item_but = (uiButViewItem *)but;
    AbstractViewItem *item = reinterpret_cast<AbstractViewItem *>(view_item_but->view_item);
    const AbstractView &view = item->get_view();

    if (item->is_renaming() && (view.get_rename_buffer().data() == rename_but.poin)) {
      return item;
    }
  }

  return nullptr;
}

static void rename_button_fn(bContext *UNUSED(C), void *arg, char *UNUSED(origstr))
{
  const uiBut *rename_but = static_cast<uiBut *>(arg);
  AbstractViewItem *item = find_item_from_rename_button(*rename_but);
  BLI_assert(item);
  item->rename_apply();
}

void AbstractViewItem::add_rename_button(uiBlock &block)
{
  AbstractView &view = get_view();
  uiBut *rename_but = uiDefBut(&block,
                               UI_BTYPE_TEXT,
                               1,
                               "",
                               0,
                               0,
                               UI_UNIT_X * 10,
                               UI_UNIT_Y,
                               view.get_rename_buffer().data(),
                               1.0f,
                               view.get_rename_buffer().size(),
                               0,
                               0,
                               "");

  /* Gotta be careful with what's passed to the `arg1` here. Any view data will be freed once the
   * callback is executed. */
  UI_but_func_rename_set(rename_but, rename_button_fn, rename_but);
  UI_but_flag_disable(rename_but, UI_BUT_UNDO);

  const bContext *evil_C = reinterpret_cast<bContext *>(block.evil_C);
  ARegion *region = CTX_wm_region(evil_C);
  /* Returns false if the button was removed. */
  if (UI_but_active_only(evil_C, region, &block, rename_but) == false) {
    end_renaming();
  }
}

/** \} */

/* ---------------------------------------------------------------------- */
/** \name Context Menu
 * \{ */

void AbstractViewItem::build_context_menu(bContext & /*C*/, uiLayout & /*column*/) const
{
  /* No context menu by default. */
}

/** \} */

/* ---------------------------------------------------------------------- */
/** \name Drag 'n Drop
 * \{ */

std::unique_ptr<AbstractViewItemDragController> AbstractViewItem::create_drag_controller() const
{
  /* There's no drag controller (and hence no drag support) by default. */
  return nullptr;
}

std::unique_ptr<AbstractViewItemDropController> AbstractViewItem::create_drop_controller() const
{
  /* There's no drop controller (and hence no drop support) by default. */
  return nullptr;
}

AbstractViewItemDragController::AbstractViewItemDragController(AbstractView &view) : view_(view)
{
}

void AbstractViewItemDragController::on_drag_start()
{
  /* Do nothing by default. */
}

AbstractViewItemDropController::AbstractViewItemDropController(AbstractView &view) : view_(view)
{
}

/** \} */

/* ---------------------------------------------------------------------- */
/** \name General Getters & Setters
 * \{ */

AbstractView &AbstractViewItem::get_view() const
{
  if (UNLIKELY(!view_)) {
    throw std::runtime_error(
        "Invalid state, item must be registered through AbstractView::register_item()");
  }
  return *view_;
}

bool AbstractViewItem::is_active() const
{
  BLI_assert_msg(get_view().is_reconstructed(),
                 "State can't be queried until reconstruction is completed");
  return is_active_;
}

/** \} */

}  // namespace blender::ui

/* ---------------------------------------------------------------------- */
/** \name C-API
 * \{ */

namespace blender::ui {

/**
 * Helper class to provide a higher level public (C-)API. Has access to private/protected view item
 * members and ensures some invariants that way.
 */
class ViewItemAPIWrapper {
 public:
  static bool matches(const AbstractViewItem &a, const AbstractViewItem &b)
  {
    if (typeid(a) != typeid(b)) {
      return false;
    }
    /* TODO should match the view as well. */
    return a.matches(b);
  }

  static bool can_rename(const AbstractViewItem &item)
  {
    const AbstractView &view = item.get_view();
    return !view.is_renaming() && item.supports_renaming();
  }

  static bool drag_start(bContext &C, const AbstractViewItem &item)
  {
    const std::unique_ptr<AbstractViewItemDragController> drag_controller =
        item.create_drag_controller();
    if (!drag_controller) {
      return false;
    }

    WM_event_start_drag(&C,
                        ICON_NONE,
                        drag_controller->get_drag_type(),
                        drag_controller->create_drag_data(),
                        0,
                        WM_DRAG_FREE_DATA);
    drag_controller->on_drag_start();

    return true;
  }

  static bool can_drop(const AbstractViewItem &item,
                       const wmDrag &drag,
                       const char **r_disabled_hint)
  {
    const std::unique_ptr<AbstractViewItemDropController> drop_controller =
        item.create_drop_controller();
    if (!drop_controller) {
      return false;
    }

    return drop_controller->can_drop(drag, r_disabled_hint);
  }

  static std::string drop_tooltip(const AbstractViewItem &item, const wmDrag &drag)
  {
    const std::unique_ptr<AbstractViewItemDropController> drop_controller =
        item.create_drop_controller();
    if (!drop_controller) {
      return {};
    }

    return drop_controller->drop_tooltip(drag);
  }

  static bool drop_handle(bContext &C, const AbstractViewItem &item, const ListBase &drags)
  {
    std::unique_ptr<AbstractViewItemDropController> drop_controller =
        item.create_drop_controller();

    const char *disabled_hint_dummy = nullptr;
    LISTBASE_FOREACH (const wmDrag *, drag, &drags) {
      if (drop_controller->can_drop(*drag, &disabled_hint_dummy)) {
        return drop_controller->on_drop(&C, *drag);
      }
    }

    return false;
  }
};

}  // namespace blender::ui

using namespace blender::ui;

bool UI_view_item_is_active(const uiViewItemHandle *item_handle)
{
  const AbstractViewItem &item = reinterpret_cast<const AbstractViewItem &>(*item_handle);
  return item.is_active();
}

bool UI_view_item_matches(const uiViewItemHandle *a_handle, const uiViewItemHandle *b_handle)
{
  const AbstractViewItem &a = reinterpret_cast<const AbstractViewItem &>(*a_handle);
  const AbstractViewItem &b = reinterpret_cast<const AbstractViewItem &>(*b_handle);
  return ViewItemAPIWrapper::matches(a, b);
}

bool UI_view_item_can_rename(const uiViewItemHandle *item_handle)
{
  const AbstractViewItem &item = reinterpret_cast<const AbstractViewItem &>(*item_handle);
  return ViewItemAPIWrapper::can_rename(item);
}

void UI_view_item_begin_rename(uiViewItemHandle *item_handle)
{
  AbstractViewItem &item = reinterpret_cast<AbstractViewItem &>(*item_handle);
  item.begin_renaming();
}

void UI_view_item_context_menu_build(bContext *C,
                                     const uiViewItemHandle *item_handle,
                                     uiLayout *column)
{
  const AbstractViewItem &item = reinterpret_cast<const AbstractViewItem &>(*item_handle);
  item.build_context_menu(*C, *column);
}

bool UI_view_item_drag_start(bContext *C, const uiViewItemHandle *item_)
{
  const AbstractViewItem &item = reinterpret_cast<const AbstractViewItem &>(*item_);
  return ViewItemAPIWrapper::drag_start(*C, item);
}

bool UI_view_item_can_drop(const uiViewItemHandle *item_,
                           const wmDrag *drag,
                           const char **r_disabled_hint)
{
  const AbstractViewItem &item = reinterpret_cast<const AbstractViewItem &>(*item_);
  return ViewItemAPIWrapper::can_drop(item, *drag, r_disabled_hint);
}

char *UI_view_item_drop_tooltip(const uiViewItemHandle *item_, const wmDrag *drag)
{
  const AbstractViewItem &item = reinterpret_cast<const AbstractViewItem &>(*item_);

  const std::string tooltip = ViewItemAPIWrapper::drop_tooltip(item, *drag);
  return tooltip.empty() ? nullptr : BLI_strdup(tooltip.c_str());
}

bool UI_view_item_drop_handle(bContext *C, const uiViewItemHandle *item_, const ListBase *drags)
{
  const AbstractViewItem &item = reinterpret_cast<const AbstractViewItem &>(*item_);
  return ViewItemAPIWrapper::drop_handle(*C, item, *drags);
}

/** \} */