Blender V5.3
interface_region_menu_pie.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdarg>
12#include <cstdlib>
13#include <cstring>
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_userdef_types.h"
18
19#include "BLI_listbase.hh"
20#include "BLI_string_utf8.hh"
21#include "BLI_time.hh"
22#include "BLI_utildefines.hh"
23
24#include "BKE_context.hh"
25#include "BKE_screen.hh"
26
27#include "WM_api.hh"
28#include "WM_types.hh"
29
30#include "RNA_access.hh"
31#include "RNA_path.hh"
32#include "RNA_prototypes.hh"
33
35
36#include "BLT_translation.hh"
37
38#include "interface_intern.hh"
40
41namespace blender::ui {
42
43/* -------------------------------------------------------------------- */
46
47struct PieMenu {
48 Block *pie_block; /* radial block of the pie menu (more could be added later) */
50 int mx, my;
51};
52
53static Block *block_func_PIE(bContext * /*C*/, PopupBlockHandle *handle, void *arg_pie)
54{
55 Block *block;
56 PieMenu *pie = static_cast<PieMenu *>(arg_pie);
57 int minwidth;
58
59 minwidth = UI_MENU_WIDTH_MIN;
60 block = pie->pie_block;
61
62 /* in some cases we create the block before the region,
63 * so we set it delayed here if necessary */
64 if (BLI_findindex(&handle->region->runtime->uiblocks, block) == -1) {
65 block_region_set(block, handle->region);
66 }
67
69
72
73 block->minbounds = minwidth;
74 block->bounds = 1;
75 block->bounds_offset[0] = 0;
76 block->bounds_offset[1] = 0;
78
79 block->pie_data->pie_center_spawned[0] = pie->mx;
80 block->pie_data->pie_center_spawned[1] = pie->my;
81
82 return pie->pie_block;
83}
84
85static float pie_menu_title_width(const char *name, int icon)
86{
87 const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
88 return (fontstyle_string_width(fstyle, name) + (UI_UNIT_X * (1.50f + (icon ? 0.25f : 0.0f))));
89}
90
91PieMenu *pie_menu_begin(bContext *C, const char *title, int icon, const wmEvent *event)
92{
93 const uiStyle *style = style_get_dpi();
94 short event_type;
95
96 wmWindow *win = CTX_wm_window(C);
97
98 PieMenu *pie = MEM_new_zeroed<PieMenu>(__func__);
99
100 pie->pie_block = block_begin(C, nullptr, __func__, EmbossType::Emboss);
101 /* may be useful later to allow spawning pies
102 * from old positions */
103 // pie->pie_block->flag |= BLOCK_POPUP_MEMORY;
104 pie->pie_block->puphash = popup_menu_hash(title);
106 pie->pie_block->pie_data = std::make_unique<PieMenuData>();
107
108 /* if pie is spawned by a left click, release or click event,
109 * it is always assumed to be click style */
110 if (event->type == LEFTMOUSE || ELEM(event->val, KM_RELEASE, KM_CLICK)) {
111 pie->pie_block->pie_data->flags |= PIE_CLICK_STYLE;
112 pie->pie_block->pie_data->event_type = EVENT_NONE;
114 }
115 else {
116 if (win->pie_event_type_last != EVENT_NONE) {
117 /* original pie key has been released, so don't propagate the event */
118 if (win->pie_event_type_lock == EVENT_NONE) {
119 event_type = EVENT_NONE;
120 pie->pie_block->pie_data->flags |= PIE_CLICK_STYLE;
121 }
122 else {
123 event_type = win->pie_event_type_last;
124 }
125 }
126 else {
127 event_type = event->type;
128 }
129
130 pie->pie_block->pie_data->event_type = event_type;
131 win->pie_event_type_lock = event_type;
132 }
133
134 pie->layout = &block_layout(
135 pie->pie_block, LayoutDirection::Vertical, LayoutType::PieMenu, 0, 0, 200, 0, 0, style);
136
137 /* NOTE: #wmEvent.xy is where we started dragging in case of #KM_PRESS_DRAG. */
138 pie->mx = event->xy[0];
139 pie->my = event->xy[1];
140
141 /* create title button */
142 if (title[0]) {
143 Button *but;
144 char titlestr[256];
145 int w;
146 if (icon) {
147 SNPRINTF_UTF8(titlestr, " %s", title);
148 w = pie_menu_title_width(titlestr, icon);
149 but = uiDefIconTextBut(
150 pie->pie_block, ButtonType::Label, icon, titlestr, 0, 0, w, UI_UNIT_Y, nullptr, "");
151 }
152 else {
153 w = pie_menu_title_width(title, 0);
154 but = uiDefBut(
155 pie->pie_block, ButtonType::Label, title, 0, 0, w, UI_UNIT_Y, nullptr, 0.0, 0.0, "");
156 }
157 /* do not align left */
158 but->drawflag &= ~BUT_TEXT_LEFT;
159 pie->pie_block->pie_data->title = but->str.c_str();
160 pie->pie_block->pie_data->icon = icon;
161 }
162
163 return pie;
164}
165
167{
168 wmWindow *window = CTX_wm_window(C);
169
171 C, nullptr, nullptr, nullptr, block_func_PIE, pie, nullptr, false);
172 menu->popup = true;
174
177
178 MEM_delete(pie);
179}
180
182{
183 return pie->layout;
184}
185
186wmOperatorStatus pie_menu_invoke(bContext *C, const char *idname, const wmEvent *event)
187{
188 MenuType *mt = WM_menutype_find(idname, true);
189
190 if (mt == nullptr) {
191 printf("%s: named menu \"%s\" not found\n", __func__, idname);
192 return OPERATOR_CANCELLED;
193 }
194
195 if (WM_menutype_poll(C, mt) == false) {
196 /* cancel but allow event to pass through, just like operators do */
198 }
199
200 PieMenu *pie = pie_menu_begin(
201 C, CTX_IFACE_(mt->translation_context, mt->label), ICON_NONE, event);
202 Layout *layout = pie_menu_layout(pie);
203
204 menutype_draw(C, mt, layout);
205
206 pie_menu_end(C, pie);
207
208 return OPERATOR_INTERFACE;
209}
210
212
213/* -------------------------------------------------------------------- */
227
229 char title[UI_MAX_NAME_STR]; /* parent pie title, copied for level */
230 int icon; /* parent pie icon, copied for level */
231 int totitem; /* total count of *remaining* items */
232
233 /* needed for calling #Layout::operator_enum_items again for new level */
239};
240
244static void pie_menu_level_invoke(bContext *C, void *argN, void *arg2)
245{
246 EnumPropertyItem *item_array = static_cast<EnumPropertyItem *>(argN);
247 PieMenuLevelData *lvl = static_cast<PieMenuLevelData *>(arg2);
248 wmWindow *win = CTX_wm_window(C);
249
250 PieMenu *pie = pie_menu_begin(C, IFACE_(lvl->title), lvl->icon, win->runtime->eventstate);
251 Layout &layout = pie_menu_layout(pie)->menu_pie();
252
254 /* So the context is passed to `itemf` functions (some need it). */
257
258 if (prop) {
259 layout.op_enum_items(
260 lvl->ot, ptr, prop, lvl->properties, lvl->context, lvl->flag, item_array, lvl->totitem);
261 }
262 else {
263 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), lvl->propname.c_str());
264 }
265
266 pie_menu_end(C, pie);
267}
268
270 wmOperatorType *ot,
271 const StringRefNull propname,
272 IDProperty *properties,
273 const EnumPropertyItem *items,
274 int totitem,
275 const wm::OpCallContext context,
276 const eUI_Item_Flag flag)
277{
278 const int totitem_parent = PIE_MAX_ITEMS - 1;
279 const int totitem_remain = totitem - totitem_parent;
280 const size_t array_size = sizeof(EnumPropertyItem) * totitem_remain;
281
282 /* used as but->func_argN so freeing is handled elsewhere */
283 EnumPropertyItem *remaining = static_cast<EnumPropertyItem *>(
284 MEM_new_uninitialized(array_size + sizeof(EnumPropertyItem), "pie_level_item_array"));
285 memcpy(remaining, items + totitem_parent, array_size);
286 /* A null terminating sentinel element is required. */
287 memset(&remaining[totitem_remain], 0, sizeof(EnumPropertyItem));
288
289 /* yuk, static... issue is we can't reliably free this without doing dangerous changes */
290 static PieMenuLevelData lvl;
291 STRNCPY_UTF8(lvl.title, block->pie_data->title);
292 lvl.totitem = totitem_remain;
293 lvl.ot = ot;
294 lvl.propname = propname;
295 lvl.properties = properties;
296 lvl.context = context;
297 lvl.flag = flag;
298
299 /* add a 'more' menu entry */
300 Button *but = uiDefIconTextBut(block,
302 ICON_PLUS,
303 "More",
304 0,
305 0,
306 UI_UNIT_X * 3,
307 UI_UNIT_Y,
308 nullptr,
309 "Show more items of this menu");
310 button_funcN_set(but, pie_menu_level_invoke, remaining, &lvl);
311}
312 /* Pie Menu Levels */
314
315} // namespace blender::ui
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
Platform independent time functions.
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
Read Blender memory allocation and freeing.
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_FSTYLE_WIDGET
#define UI_UNIT_X
#define UI_MAX_NAME_STR
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr const char * c_str() const
#define PIE_MAX_ITEMS
#define UI_MENU_WIDTH_MIN
Button * uiDefIconTextBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, int icon, StringRef str, int x, int y, short width, short height, void *poin, std::optional< StringRef > tip)
PieMenu * pie_menu_begin(bContext *C, const char *title, int icon, const wmEvent *event) ATTR_NONNULL()
Layout * pie_menu_layout(PieMenu *pie)
static Block * block_func_PIE(bContext *, PopupBlockHandle *handle, void *arg_pie)
int fontstyle_string_width(const uiFontStyle *fs, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void block_flag_enable(Block *block, int flag)
static void pie_menu_level_invoke(bContext *C, void *argN, void *arg2)
const uiStyle * style_get_dpi()
void popup_handlers_add(bContext *C, ListBaseT< wmEventHandler > *handlers, PopupBlockHandle *popup, char flag)
Block * block_begin(const bContext *C, ARegion *region, std::string name, EmbossType emboss)
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)
void block_theme_style_set(Block *block, char theme_style)
static float pie_menu_title_width(const char *name, int icon)
void button_funcN_set(Button *but, ButtonHandleNFunc funcN, void *argN, void *arg2, ButtonArgNFree func_argN_free_fn=MEM_delete_void, ButtonArgNCopy func_argN_copy_fn=MEM_dupalloc_void)
wmOperatorStatus pie_menu_invoke(bContext *C, const char *idname, const wmEvent *event)
PopupBlockHandle * popup_block_create(bContext *C, ARegion *butregion, Button *but, BlockCreateFunc create_func, BlockHandleCreateFunc handle_create_func, void *arg, FreeArgFunc arg_free, bool can_refresh)
int2 block_layout_resolve(Block *block)
void menutype_draw(bContext *C, MenuType *mt, Layout *layout)
uint popup_menu_hash(const StringRef str)
void block_region_set(Block *block, ARegion *region)
Layout & block_layout(Block *block, LayoutDirection direction, LayoutType type, int x, int y, int size, int em, int padding, const uiStyle *style)
void pie_menu_end(bContext *C, PieMenu *pie)
void pie_menu_level_create(Block *block, wmOperatorType *ot, StringRefNull propname, IDProperty *properties, const EnumPropertyItem *items, int totitem, wm::OpCallContext context, eUI_Item_Flag flag)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void WM_operator_properties_sanitize(PointerRNA *ptr, const bool no_context)
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
wmWindow * CTX_wm_window(const bContext *C)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
@ WM_HANDLER_ACCEPT_DBL_CLICK
Definition WM_api.hh:711
bool WM_menutype_poll(bContext *C, MenuType *mt)
const char * RNA_struct_identifier(const StructRNA *type)
@ KM_RELEASE
Definition WM_types.hh:325
double BLI_time_now_seconds()
Definition time.cc:30
void WM_event_add_mousemove(wmWindow *win)
bke::ARegionRuntime * runtime
char translation_context[BKE_ST_MAXNAME]
char label[BKE_ST_MAXNAME]
StructRNA * type
Definition RNA_types.hh:63
ListBaseT< ui::Block > uiblocks
ListBaseT< wmEventHandler > modalhandlers
BlockBoundsCalc bounds_type
std::unique_ptr< PieMenuData > pie_data
void op_enum_items(wmOperatorType *ot, const PointerRNA &ptr, PropertyRNA *prop, IDProperty *properties, wm::OpCallContext context, eUI_Item_Flag flag, const EnumPropertyItem *item_array, int totitem, int active=-1)
wmEventType type
Definition WM_types.hh:817
bke::WindowRuntime * runtime