Blender  V3.3
UI_abstract_view.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
18 #pragma once
19 
20 #include <array>
21 #include <memory>
22 
23 #include "DNA_defs.h"
24 
25 #include "BLI_span.hh"
26 #include "BLI_string_ref.hh"
27 
28 struct bContext;
29 struct uiBlock;
30 struct uiBut;
31 struct uiLayout;
32 struct uiViewItemHandle;
33 struct wmDrag;
34 struct wmNotifier;
35 
36 namespace blender::ui {
37 
38 class AbstractViewItem;
41 
42 class AbstractView {
43  friend class AbstractViewItem;
44 
45  bool is_reconstructed_ = false;
53  std::unique_ptr<std::array<char, MAX_NAME>> rename_buffer_;
54 
55  public:
56  virtual ~AbstractView() = default;
57 
59  virtual bool listen(const wmNotifier &) const;
60 
65  void register_item(AbstractViewItem &item);
66 
68  bool is_renaming() const;
70  bool begin_renaming();
71  void end_renaming();
74 
75  protected:
76  AbstractView() = default;
77 
78  virtual void update_children_from_old(const AbstractView &old_view) = 0;
79 
86  void update_from_old(uiBlock &new_block);
91  bool is_reconstructed() const;
92 };
93 
95  friend class AbstractView;
96  friend class ViewItemAPIWrapper;
97 
98  protected:
103  AbstractView *view_ = nullptr;
104  bool is_active_ = false;
105  bool is_renaming_ = false;
106 
107  public:
108  virtual ~AbstractViewItem() = default;
109 
110  virtual void build_context_menu(bContext &C, uiLayout &column) const;
111 
116  virtual bool supports_renaming() const;
124  virtual bool rename(StringRefNull new_name);
129  virtual StringRef get_rename_string() const;
130 
135  virtual std::unique_ptr<AbstractViewItemDragController> create_drag_controller() const;
143  virtual std::unique_ptr<AbstractViewItemDropController> create_drop_controller() const;
144 
146  AbstractView &get_view() const;
147 
152  bool is_active() const;
153 
154  bool is_renaming() const;
155  void begin_renaming();
156  void end_renaming();
157  void rename_apply();
158 
159  template<typename ToType = AbstractViewItem>
160  static ToType *from_item_handle(uiViewItemHandle *handle);
161 
162  protected:
163  AbstractViewItem() = default;
164 
172  virtual bool matches(const AbstractViewItem &other) const = 0;
173 
181  virtual void update_from_old(const AbstractViewItem &old);
182 
188  void add_rename_button(uiBlock &block);
189 };
190 
191 template<typename ToType> ToType *AbstractViewItem::from_item_handle(uiViewItemHandle *handle)
192 {
193  static_assert(std::is_base_of<AbstractViewItem, ToType>::value,
194  "Type must derive from and implement the AbstractViewItem interface");
195 
196  return dynamic_cast<ToType *>(reinterpret_cast<AbstractViewItem *>(handle));
197 }
198 
199 /* ---------------------------------------------------------------------- */
208  protected:
210 
211  public:
213  virtual ~AbstractViewItemDragController() = default;
214 
215  virtual int get_drag_type() const = 0;
216  virtual void *create_drag_data() const = 0;
217  virtual void on_drag_start();
218 
221  template<class ViewType> inline ViewType &get_view() const;
222 };
223 
230  protected:
232 
233  public:
235  virtual ~AbstractViewItemDropController() = default;
236 
245  virtual bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const = 0;
252  virtual std::string drop_tooltip(const wmDrag &drag) const = 0;
257  virtual bool on_drop(struct bContext *C, const wmDrag &drag) = 0;
258 
261  template<class ViewType> inline ViewType &get_view() const;
262 };
263 
264 template<class ViewType> ViewType &AbstractViewItemDragController::get_view() const
265 {
266  static_assert(std::is_base_of<AbstractView, ViewType>::value,
267  "Type must derive from and implement the ui::AbstractView interface");
268  return dynamic_cast<ViewType &>(view_);
269 }
270 
271 template<class ViewType> ViewType &AbstractViewItemDropController::get_view() const
272 {
273  static_assert(std::is_base_of<AbstractView, ViewType>::value,
274  "Type must derive from and implement the ui::AbstractView interface");
275  return dynamic_cast<ViewType &>(view_);
276 }
277 
280 } // namespace blender::ui
static AppView * view
#define C
Definition: RandGen.cpp:25
struct uiViewItemHandle uiViewItemHandle
Definition: UI_interface.h:78
virtual void * create_drag_data() const =0
virtual std::string drop_tooltip(const wmDrag &drag) const =0
virtual bool on_drop(struct bContext *C, const wmDrag &drag)=0
virtual bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const =0
static ToType * from_item_handle(uiViewItemHandle *handle)
virtual void update_from_old(const AbstractViewItem &old)
void add_rename_button(uiBlock &block)
AbstractView & get_view() const
virtual bool supports_renaming() const
virtual ~AbstractViewItem()=default
virtual bool rename(StringRefNull new_name)
virtual bool matches(const AbstractViewItem &other) const =0
virtual void build_context_menu(bContext &C, uiLayout &column) const
virtual StringRef get_rename_string() const
virtual std::unique_ptr< AbstractViewItemDropController > create_drop_controller() const
virtual std::unique_ptr< AbstractViewItemDragController > create_drag_controller() const
virtual bool listen(const wmNotifier &) const
void update_from_old(uiBlock &new_block)
virtual void update_children_from_old(const AbstractView &old_view)=0
void register_item(AbstractViewItem &item)
virtual ~AbstractView()=default
Span< char > get_rename_buffer() const