Blender V5.3
abstract_view_item.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10
11#include "BLI_fnmatch.hh"
12#include "BLI_listbase.hh"
13
14#include "WM_api.hh"
15
17#include "interface_intern.hh"
18
19#include "UI_abstract_view.hh"
20
21#include <stdexcept>
22
23namespace blender::ui {
24
25/* ---------------------------------------------------------------------- */
28
39
41
42/* ---------------------------------------------------------------------- */
45
47{
48 /* Do nothing by default. */
49}
50
51std::optional<bool> AbstractViewItem::should_be_active() const
52{
53 return std::nullopt;
54}
55
57{
58 BLI_assert_msg(get_view().is_reconstructed(),
59 "Item activation cannot be done until reconstruction is completed");
60
61 if (!is_activatable_) {
62 return false;
63 }
64 if (is_active()) {
65 return false;
66 }
67
68 /* Deactivate other items in the view. */
69 this->get_view().foreach_view_item([](auto &item) { item.deactivate(); });
70
71 is_active_ = true;
72 return true;
73}
74
76{
79 }
80
81 /* Make sure active item is selected. */
82 if (is_active()) {
83 set_selected(true);
84 }
85}
86
88{
90 this->activate(C);
91 }
92 else {
93 this->set_state_active();
94 }
95}
96
98{
99 if (is_active_) {
100 /* Deselect only active item, otherwise selection state before active item is cleared, see:
101 * !150891 */
102 is_selected_ = false;
103 }
104 is_active_ = false;
105}
106
107std::optional<bool> AbstractViewItem::should_be_selected() const
108{
109 return std::nullopt;
110}
111
116
118
119/* ---------------------------------------------------------------------- */
122
124{
125 if (const std::optional<bool> should_be_active = this->should_be_active()) {
126 if (*should_be_active) {
127 /* Don't call #activate() here, since this reflects an external state change and therefore
128 * shouldn't call #on_activate(). */
130 }
131 else if (is_active_) {
132 is_active_ = false;
133 is_selected_ = false;
134 }
135 }
136 if (std::optional<bool> is_selected = should_be_selected()) {
137 set_selected(is_selected.value_or(false));
138 }
139}
140
142
143/* ---------------------------------------------------------------------- */
146
148{
149 /* No renaming by default. */
150 return false;
151}
152bool AbstractViewItem::rename(const bContext & /*C*/, StringRefNull /*new_name*/)
153{
154 /* No renaming by default. */
155 return false;
156}
157
159{
160 /* No rename string by default. */
161 return {};
162}
163
165{
166 return is_renaming_;
167}
168
170{
171 AbstractView &view = this->get_view();
172 if (view.is_renaming() || !supports_renaming()) {
173 return;
174 }
175
176 if (view.begin_renaming()) {
177 is_renaming_ = true;
178 }
179
180 StringRef initial_str = this->get_rename_string();
181 std::copy(std::begin(initial_str), std::end(initial_str), std::begin(view.get_rename_buffer()));
182}
183
185{
186 const AbstractView &view = this->get_view();
187 rename(C, view.get_rename_buffer().data());
188 end_renaming();
189}
190
192{
193 if (!is_renaming()) {
194 return;
195 }
196
197 is_renaming_ = false;
198
199 AbstractView &view = this->get_view();
200 view.end_renaming();
201}
202
204{
205 AbstractView &view = this->get_view();
206 Button *rename_but = uiDefBut(&block,
208 "",
209 0,
210 0,
211 UI_UNIT_X * 10,
212 UI_UNIT_Y,
213 view.get_rename_buffer().data(),
214 1.0f,
215 view.get_rename_buffer().size(),
216 "");
217
218 /* Gotta be careful with what's passed to the `arg1` here. Any view data will be freed once the
219 * callback is executed. */
221 rename_but,
222 [item = this](bContext &C, StringRefNull /*oldname*/) -> void { item->rename_apply(C); });
223 button_flag_disable(rename_but, BUT_UNDO);
224
225 const bContext *evil_C = reinterpret_cast<bContext *>(block.evil_C);
226 ARegion *region = CTX_wm_region_popup(evil_C) ? CTX_wm_region_popup(evil_C) :
227 CTX_wm_region(evil_C);
228 /* Returns false if the button was removed. */
229 if (button_active_only(evil_C, region, &block, rename_but) == false) {
230 end_renaming();
231 }
232}
233
235{
236 /* No deletion by default. Needs type specific implementation. */
237}
238
240{
241 /* No action by default. Needs type specific implementation. */
242}
243
245
246/* ---------------------------------------------------------------------- */
249
250void AbstractViewItem::build_context_menu(bContext & /*C*/, Layout & /*column*/) const
251{
252 /* No context menu by default. */
253}
254
256
257/* ---------------------------------------------------------------------- */
260
262{
263 const StringRef name = this->get_rename_string();
264 return fnmatch(filter_string.c_str(), name.data(), FNM_CASEFOLD) == 0;
265}
266
268{
269 BLI_assert(get_view().needs_filtering_ == false);
271}
272
274
275/* ---------------------------------------------------------------------- */
278
279std::unique_ptr<AbstractViewItemDragController> AbstractViewItem::create_drag_controller() const
280{
281 /* There's no drag controller (and hence no drag support) by default. */
282 return nullptr;
283}
284
285std::unique_ptr<DropTargetInterface> AbstractViewItem::create_item_drop_target()
286{
287 /* There's no drop target (and hence no drop support) by default. */
288 return nullptr;
289}
290
291std::optional<std::string> AbstractViewItem::debug_name() const
292{
293 return {};
294}
295
297
299{
300 /* Do nothing by default. */
301}
302
304
305/* ---------------------------------------------------------------------- */
308
310{
311 if (!view_) [[unlikely]] {
312 throw std::runtime_error(
313 "Invalid state, item must be registered through AbstractView::register_item()");
314 }
315 return *view_;
316}
317
322
327
332
337
342
347
352
354{
355 return is_interactive_;
356}
357
359{
360 BLI_assert_msg(this->get_view().is_reconstructed(),
361 "State cannot be queried until reconstruction is completed");
362 return is_hovered_;
363}
364
366{
367 BLI_assert_msg(this->get_view().is_reconstructed(),
368 "State cannot be queried until reconstruction is completed");
369 return is_active_;
370}
371
373{
374 BLI_assert_msg(this->get_view().is_reconstructed(),
375 "State can't be queried until reconstruction is completed");
376 return is_selected_;
377}
378
383
385
386/* ---------------------------------------------------------------------- */
389
395 public:
396 static bool matches(const AbstractViewItem &a, const AbstractViewItem &b)
397 {
398 if (typeid(a) != typeid(b)) {
399 return false;
400 }
401 /* TODO should match the view as well. */
402 return a.matches(b);
403 }
404
406 {
407 std::swap(a.view_item_but_, b.view_item_but_);
408 }
409};
410
412{
414}
415
420
422{
423 const AbstractView &view = item.get_view();
424 return !view.is_renaming() && item.supports_renaming();
425}
426
431
433{
434 return item.create_drag_controller() != nullptr;
435}
436
438{
439 return item.get_view().get_popup_keep_open();
440}
441
443{
444 const std::unique_ptr<AbstractViewItemDragController> drag_controller =
446 if (!drag_controller) {
447 return false;
448 }
449
450 if (const std::optional<eWM_DragDataType> drag_type = drag_controller->get_drag_type()) {
452 &C, ICON_NONE, *drag_type, drag_controller->create_drag_data(), WM_DRAG_FREE_DATA);
453 }
454 drag_controller->on_drag_start(C, item);
455
456 return true;
457}
458
460
461} // namespace blender::ui
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define BLI_assert_msg(a, msg)
constexpr const char * c_str() const
constexpr const char * data() const
virtual void on_drag_start(bContext &C, ui::AbstractViewItem &item)
virtual void delete_item(bContext *C)
virtual void set_selected(const bool select)
virtual void update_from_old(const AbstractViewItem &old)
virtual bool should_be_filtered_visible(StringRefNull filter_string) const
virtual void on_activate(bContext &C)
virtual std::unique_ptr< DropTargetInterface > create_item_drop_target()
virtual bool supports_renaming() const
virtual std::optional< bool > should_be_active() const
virtual bool rename(const bContext &C, StringRefNull new_name)
virtual bool matches(const AbstractViewItem &other) const =0
ui::ButtonViewItem * view_item_button() const
ui::ButtonViewItem * view_item_but_
virtual std::optional< bool > should_be_selected() const
virtual StringRef get_rename_string() const
void rename_apply(const bContext &C)
void activate_for_context_menu(bContext &C)
virtual std::unique_ptr< AbstractViewItemDragController > create_drag_controller() const
virtual std::optional< std::string > debug_name() const
virtual void build_context_menu(bContext &C, Layout &column) const
virtual void foreach_view_item(FunctionRef< void(AbstractViewItem &)> iter_fn) const =0
Span< char > get_rename_buffer() const
static bool matches(const AbstractViewItem &a, const AbstractViewItem &b)
static void swap_button_pointers(AbstractViewItem &a, AbstractViewItem &b)
bool view_item_can_rename(const AbstractViewItem &item)
void text_button_func_rename_set(Button *but, std::function< void(bContext &C, StringRefNull oldname)> rename_func)
bool view_item_supports_drag(const AbstractViewItem &item)
void view_item_begin_rename(AbstractViewItem &item)
bool view_item_matches(const AbstractViewItem &a, const AbstractViewItem &b)
void view_item_swap_button_pointers(AbstractViewItem &a, AbstractViewItem &b)
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
bool button_active_only(const bContext *C, ARegion *region, Block *block, Button *but)
void button_flag_disable(Button *but, int64_t flag)
bool view_item_popup_keep_open(const AbstractViewItem &item)
bool view_item_drag_start(bContext &C, AbstractViewItem &item)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_event_start_drag(bContext *C, int icon, eWM_DragDataType type, void *poin, uint flags)
@ WM_DRAG_FREE_DATA
Definition WM_types.hh:1324
ARegion * CTX_wm_region_popup(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)