2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/interface/interface_region_menu_pie.c
27 * \ingroup edinterface
37 #include "MEM_guardedalloc.h"
39 #include "DNA_userdef_types.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
52 #include "RNA_access.h"
54 #include "UI_interface.h"
56 #include "BLT_translation.h"
58 #include "ED_screen.h"
60 #include "interface_intern.h"
61 #include "interface_regions_intern.h"
63 /* -------------------------------------------------------------------- */
68 uiBlock *block_radial; /* radial block of the pie menu (more could be added later) */
73 static uiBlock *ui_block_func_PIE(bContext *UNUSED(C), uiPopupBlockHandle *handle, void *arg_pie)
76 uiPieMenu *pie = arg_pie;
77 int minwidth, width, height;
80 block = pie->block_radial;
82 /* in some cases we create the block before the region,
83 * so we set it delayed here if necessary */
84 if (BLI_findindex(&handle->region->uiblocks, block) == -1)
85 UI_block_region_set(block, handle->region);
87 UI_block_layout_resolve(block, &width, &height);
89 UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_NUMSELECT);
91 block->minbounds = minwidth;
95 block->bounds_type = UI_BLOCK_BOUNDS_PIE_CENTER;
97 block->pie_data.pie_center_spawned[0] = pie->mx;
98 block->pie_data.pie_center_spawned[1] = pie->my;
100 return pie->block_radial;
103 static float ui_pie_menu_title_width(const char *name, int icon)
105 const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
106 return (UI_fontstyle_string_width(fstyle, name) +
107 (UI_UNIT_X * (1.50f + (icon ? 0.25f : 0.0f))));
110 uiPieMenu *UI_pie_menu_begin(struct bContext *C, const char *title, int icon, const wmEvent *event)
116 wmWindow *win = CTX_wm_window(C);
118 style = UI_style_get_dpi();
119 pie = MEM_callocN(sizeof(*pie), "pie menu");
121 pie->block_radial = UI_block_begin(C, NULL, __func__, UI_EMBOSS);
122 /* may be useful later to allow spawning pies
123 * from old positions */
124 /* pie->block_radial->flag |= UI_BLOCK_POPUP_MEMORY; */
125 pie->block_radial->puphash = ui_popup_menu_hash(title);
126 pie->block_radial->flag |= UI_BLOCK_RADIAL;
128 /* if pie is spawned by a left click, release or click event, it is always assumed to be click style */
129 if (event->type == LEFTMOUSE || ELEM(event->val, KM_RELEASE, KM_CLICK)) {
130 pie->block_radial->pie_data.flags |= UI_PIE_CLICK_STYLE;
131 pie->block_radial->pie_data.event = EVENT_NONE;
132 win->lock_pie_event = EVENT_NONE;
135 if (win->last_pie_event != EVENT_NONE) {
136 /* original pie key has been released, so don't propagate the event */
137 if (win->lock_pie_event == EVENT_NONE) {
138 event_type = EVENT_NONE;
139 pie->block_radial->pie_data.flags |= UI_PIE_CLICK_STYLE;
142 event_type = win->last_pie_event;
145 event_type = event->type;
148 pie->block_radial->pie_data.event = event_type;
149 win->lock_pie_event = event_type;
152 pie->layout = UI_block_layout(pie->block_radial, UI_LAYOUT_VERTICAL, UI_LAYOUT_PIEMENU, 0, 0, 200, 0, 0, style);
154 /* Open from where we started dragging. */
155 if (event->val == KM_CLICK_DRAG) {
156 pie->mx = event->prevclickx;
157 pie->my = event->prevclicky;
164 /* create title button */
170 BLI_snprintf(titlestr, sizeof(titlestr), " %s", title);
171 w = ui_pie_menu_title_width(titlestr, icon);
172 but = uiDefIconTextBut(
173 pie->block_radial, UI_BTYPE_LABEL, 0, icon, titlestr, 0, 0, w, UI_UNIT_Y,
174 NULL, 0.0, 0.0, 0, 0, "");
177 w = ui_pie_menu_title_width(title, 0);
179 pie->block_radial, UI_BTYPE_LABEL, 0, title, 0, 0, w, UI_UNIT_Y,
180 NULL, 0.0, 0.0, 0, 0, "");
182 /* do not align left */
183 but->drawflag &= ~UI_BUT_TEXT_LEFT;
184 pie->block_radial->pie_data.title = but->str;
185 pie->block_radial->pie_data.icon = icon;
191 void UI_pie_menu_end(bContext *C, uiPieMenu *pie)
193 wmWindow *window = CTX_wm_window(C);
194 uiPopupBlockHandle *menu;
196 menu = ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_PIE, pie);
198 menu->towardstime = PIL_check_seconds_timer();
200 UI_popup_handlers_add(
201 C, &window->modalhandlers,
202 menu, WM_HANDLER_ACCEPT_DBL_CLICK);
203 WM_event_add_mousemove(C);
205 menu->can_refresh = false;
209 uiLayout *UI_pie_menu_layout(uiPieMenu *pie)
214 int UI_pie_menu_invoke(struct bContext *C, const char *idname, const wmEvent *event)
218 MenuType *mt = WM_menutype_find(idname, true);
221 printf("%s: named menu \"%s\" not found\n", __func__, idname);
222 return OPERATOR_CANCELLED;
225 if (WM_menutype_poll(C, mt) == false) {
226 /* cancel but allow event to pass through, just like operators do */
227 return (OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH);
230 pie = UI_pie_menu_begin(C, IFACE_(mt->label), ICON_NONE, event);
231 layout = UI_pie_menu_layout(pie);
233 UI_menutype_draw(C, mt, layout);
235 UI_pie_menu_end(C, pie);
237 return OPERATOR_INTERFACE;
240 int UI_pie_menu_invoke_from_operator_enum(
241 struct bContext *C, const char *title, const char *opname,
242 const char *propname, const wmEvent *event)
247 pie = UI_pie_menu_begin(C, IFACE_(title), ICON_NONE, event);
248 layout = UI_pie_menu_layout(pie);
250 layout = uiLayoutRadial(layout);
251 uiItemsEnumO(layout, opname, propname);
253 UI_pie_menu_end(C, pie);
255 return OPERATOR_INTERFACE;
258 int UI_pie_menu_invoke_from_rna_enum(
259 struct bContext *C, const char *title, const char *path,
260 const wmEvent *event)
268 RNA_pointer_create(NULL, &RNA_Context, C, &ctx_ptr);
270 if (!RNA_path_resolve(&ctx_ptr, path, &r_ptr, &r_prop)) {
271 return OPERATOR_CANCELLED;
274 /* invalid property, only accept enums */
275 if (RNA_property_type(r_prop) != PROP_ENUM) {
277 return OPERATOR_CANCELLED;
280 pie = UI_pie_menu_begin(C, IFACE_(title), ICON_NONE, event);
282 layout = UI_pie_menu_layout(pie);
284 layout = uiLayoutRadial(layout);
285 uiItemFullR(layout, &r_ptr, r_prop, RNA_NO_INDEX, 0, UI_ITEM_R_EXPAND, NULL, 0);
287 UI_pie_menu_end(C, pie);
289 return OPERATOR_INTERFACE;
294 /* -------------------------------------------------------------------- */
296 * \name Pie Menu Levels
298 * Pie menus can't contain more than 8 items (yet). When using #uiItemsFullEnumO, a "More" button is created that calls
299 * a new pie menu if the enum has too many items. We call this a new "level".
300 * Indirect recursion is used, so that a theoretically unlimited number of items is supported.
302 * This is a implementation specifically for operator enums, needed since the object mode pie now has more than 8
303 * items. Ideally we'd have some way of handling this for all kinds of pie items, but that's tricky.
305 * - Julian (Feb 2016)
309 typedef struct PieMenuLevelData {
310 char title[UI_MAX_NAME_STR]; /* parent pie title, copied for level */
311 int icon; /* parent pie icon, copied for level */
312 int totitem; /* total count of *remaining* items */
314 /* needed for calling uiItemsFullEnumO_array again for new level */
316 const char *propname;
317 IDProperty *properties;
322 * Invokes a new pie menu for a new level.
324 static void ui_pie_menu_level_invoke(bContext *C, void *argN, void *arg2)
326 EnumPropertyItem *item_array = (EnumPropertyItem *)argN;
327 PieMenuLevelData *lvl = (PieMenuLevelData *)arg2;
328 wmWindow *win = CTX_wm_window(C);
330 uiPieMenu *pie = UI_pie_menu_begin(C, IFACE_(lvl->title), lvl->icon, win->eventstate);
331 uiLayout *layout = UI_pie_menu_layout(pie);
333 layout = uiLayoutRadial(layout);
337 WM_operator_properties_create_ptr(&ptr, lvl->ot);
338 /* so the context is passed to itemf functions (some need it) */
339 WM_operator_properties_sanitize(&ptr, false);
340 PropertyRNA *prop = RNA_struct_find_property(&ptr, lvl->propname);
343 uiItemsFullEnumO_items(
344 layout, lvl->ot, ptr, prop, lvl->properties, lvl->context, lvl->flag,
345 item_array, lvl->totitem);
348 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), lvl->propname);
351 UI_pie_menu_end(C, pie);
355 * Set up data for defining a new pie menu level and add button that invokes it.
357 void ui_pie_menu_level_create(
358 uiBlock *block, wmOperatorType *ot, const char *propname, IDProperty *properties,
359 const EnumPropertyItem *items, int totitem, int context, int flag)
361 const int totitem_parent = PIE_MAX_ITEMS - 1;
362 const int totitem_remain = totitem - totitem_parent;
363 size_t array_size = sizeof(EnumPropertyItem) * totitem_remain;
365 /* used as but->func_argN so freeing is handled elsewhere */
366 EnumPropertyItem *remaining = MEM_mallocN(array_size + sizeof(EnumPropertyItem), "pie_level_item_array");
367 memcpy(remaining, items + totitem_parent, array_size);
368 /* a NULL terminating sentinal element is required */
369 memset(&remaining[totitem_remain], 0, sizeof(EnumPropertyItem));
372 /* yuk, static... issue is we can't reliably free this without doing dangerous changes */
373 static PieMenuLevelData lvl;
374 BLI_strncpy(lvl.title, block->pie_data.title, UI_MAX_NAME_STR);
375 lvl.totitem = totitem_remain;
377 lvl.propname = propname;
378 lvl.properties = properties;
379 lvl.context = context;
382 /* add a 'more' menu entry */
383 uiBut *but = uiDefIconTextBut(block, UI_BTYPE_BUT, 0, ICON_PLUS, "More", 0, 0, UI_UNIT_X * 3, UI_UNIT_Y, NULL,
384 0.0f, 0.0f, 0.0f, 0.0f, "Show more items of this menu");
385 UI_but_funcN_set(but, ui_pie_menu_level_invoke, remaining, &lvl);
388 /** \} */ /* Pie Menu Levels */