bbc0f66605c12ed71b11a09e9c0f23ad4c3c61b7
[blender.git] / source / blender / editors / interface / interface_context_menu.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/interface/interface_context_menu.c
22  *  \ingroup edinterface
23  *
24  * Generic context popup menus.
25  */
26
27 #include <string.h>
28
29 #include "MEM_guardedalloc.h"
30
31 #include "DNA_scene_types.h"
32 #include "DNA_screen_types.h"
33
34 #include "BLI_path_util.h"
35 #include "BLI_string.h"
36 #include "BLI_utildefines.h"
37
38 #include "BLT_translation.h"
39
40 #include "BKE_addon.h"
41 #include "BKE_context.h"
42 #include "BKE_idprop.h"
43 #include "BKE_screen.h"
44
45 #include "ED_screen.h"
46 #include "ED_keyframing.h"
47
48 #include "UI_interface.h"
49
50 #include "interface_intern.h"
51
52 #include "RNA_access.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 /* This hack is needed because we don't have a good way to re-reference keymap items once added: T42944 */
58 #define USE_KEYMAP_ADD_HACK
59
60 /* -------------------------------------------------------------------- */
61 /** \name Button Context Menu
62  * \{ */
63
64 static void but_shortcut_name_func(bContext *C, void *arg1, int UNUSED(event))
65 {
66         uiBut *but = (uiBut *)arg1;
67
68         if (but->optype) {
69                 char shortcut_str[128];
70
71                 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
72
73                 /* complex code to change name of button */
74                 if (WM_key_event_operator_string(
75                         C, but->optype->idname, but->opcontext, prop, true,
76                         shortcut_str, sizeof(shortcut_str)))
77                 {
78                         ui_but_add_shortcut(but, shortcut_str, true);
79                 }
80                 else {
81                         /* simply strip the shortcut */
82                         ui_but_add_shortcut(but, NULL, true);
83                 }
84         }
85 }
86
87 static uiBlock *menu_change_shortcut(bContext *C, ARegion *ar, void *arg)
88 {
89         wmWindowManager *wm = CTX_wm_manager(C);
90         uiBlock *block;
91         uiBut *but = (uiBut *)arg;
92         wmKeyMap *km;
93         wmKeyMapItem *kmi;
94         PointerRNA ptr;
95         uiLayout *layout;
96         uiStyle *style = UI_style_get_dpi();
97         IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
98
99         kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, prop, true, &km);
100         BLI_assert(kmi != NULL);
101
102         RNA_pointer_create(&wm->id, &RNA_KeyMapItem, kmi, &ptr);
103
104         block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
105         UI_block_func_handle_set(block, but_shortcut_name_func, but);
106         UI_block_flag_enable(block, UI_BLOCK_MOVEMOUSE_QUIT);
107         UI_block_direction_set(block, UI_DIR_CENTER_Y);
108
109         layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 200, 20, 0, style);
110
111         uiItemR(layout, &ptr, "type", UI_ITEM_R_FULL_EVENT | UI_ITEM_R_IMMEDIATE, "", ICON_NONE);
112
113         UI_block_bounds_set_popup(block, 6, -50, 26);
114
115         return block;
116 }
117
118 #ifdef USE_KEYMAP_ADD_HACK
119 static int g_kmi_id_hack;
120 #endif
121
122 static uiBlock *menu_add_shortcut(bContext *C, ARegion *ar, void *arg)
123 {
124         wmWindowManager *wm = CTX_wm_manager(C);
125         uiBlock *block;
126         uiBut *but = (uiBut *)arg;
127         wmKeyMap *km;
128         wmKeyMapItem *kmi;
129         PointerRNA ptr;
130         uiLayout *layout;
131         uiStyle *style = UI_style_get_dpi();
132         IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
133         int kmi_id;
134
135         /* XXX this guess_opname can potentially return a different keymap than being found on adding later... */
136         km = WM_keymap_guess_opname(C, but->optype->idname);
137         kmi = WM_keymap_add_item(km, but->optype->idname, AKEY, KM_PRESS, 0, 0);
138         kmi_id = kmi->id;
139
140         /* copy properties, prop can be NULL for reset */
141         if (prop)
142                 prop = IDP_CopyProperty(prop);
143         WM_keymap_properties_reset(kmi, prop);
144
145         /* update and get pointers again */
146         WM_keyconfig_update(wm);
147
148         km = WM_keymap_guess_opname(C, but->optype->idname);
149         kmi = WM_keymap_item_find_id(km, kmi_id);
150
151         RNA_pointer_create(&wm->id, &RNA_KeyMapItem, kmi, &ptr);
152
153         block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
154         UI_block_func_handle_set(block, but_shortcut_name_func, but);
155         UI_block_direction_set(block, UI_DIR_CENTER_Y);
156
157         layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 200, 20, 0, style);
158
159         uiItemR(layout, &ptr, "type", UI_ITEM_R_FULL_EVENT | UI_ITEM_R_IMMEDIATE, "", ICON_NONE);
160
161         UI_block_bounds_set_popup(block, 6, -50, 26);
162
163 #ifdef USE_KEYMAP_ADD_HACK
164         g_kmi_id_hack = kmi_id;
165 #endif
166         return block;
167 }
168
169 static void menu_add_shortcut_cancel(struct bContext *C, void *arg1)
170 {
171         uiBut *but = (uiBut *)arg1;
172         wmKeyMap *km;
173         wmKeyMapItem *kmi;
174 #ifndef USE_KEYMAP_ADD_HACK
175         IDProperty *prop;
176 #endif
177         int kmi_id;
178
179 #ifdef USE_KEYMAP_ADD_HACK
180         km = WM_keymap_guess_opname(C, but->optype->idname);
181         kmi_id = g_kmi_id_hack;
182         UNUSED_VARS(but);
183 #else
184         prop  = (but->opptr) ? but->opptr->data : NULL;
185         kmi_id = WM_key_event_operator_id(C, but->optype->idname, but->opcontext, prop, true, &km);
186 #endif
187
188         kmi = WM_keymap_item_find_id(km, kmi_id);
189         WM_keymap_remove_item(km, kmi);
190 }
191
192 static void popup_change_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2))
193 {
194         uiBut *but = (uiBut *)arg1;
195         UI_popup_block_invoke(C, menu_change_shortcut, but);
196 }
197
198 static void remove_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2))
199 {
200         uiBut *but = (uiBut *)arg1;
201         wmKeyMap *km;
202         wmKeyMapItem *kmi;
203         IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
204
205         kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, prop, true, &km);
206         BLI_assert(kmi != NULL);
207
208         WM_keymap_remove_item(km, kmi);
209
210         but_shortcut_name_func(C, but, 0);
211 }
212
213 static void popup_add_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2))
214 {
215         uiBut *but = (uiBut *)arg1;
216         UI_popup_block_ex(C, menu_add_shortcut, NULL, menu_add_shortcut_cancel, but, NULL);
217 }
218
219 static bool ui_but_is_user_menu_compatible(bContext *C, uiBut *but)
220 {
221         return (but->optype ||
222                 (but->rnaprop &&
223                  (RNA_property_type(but->rnaprop) == PROP_BOOLEAN) &&
224                  (WM_context_member_from_ptr(C, &but->rnapoin) != NULL)) ||
225                 UI_but_menutype_get(but));
226 }
227
228 static bUserMenuItem *ui_but_user_menu_find(bContext *C, uiBut *but, bUserMenu *um)
229 {
230         MenuType *mt = NULL;
231         if (but->optype) {
232                 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
233                 return (bUserMenuItem *)ED_screen_user_menu_item_find_operator(
234                         &um->items, but->optype, prop, but->opcontext);
235         }
236         else if (but->rnaprop) {
237                 const char *member_id = WM_context_member_from_ptr(C, &but->rnapoin);
238                 const char *data_path = RNA_path_from_ID_to_struct(&but->rnapoin);
239                 const char *member_id_data_path = member_id;
240                 if (data_path) {
241                         member_id_data_path = BLI_sprintfN("%s.%s", member_id, data_path);
242                 }
243                 const char *prop_id = RNA_property_identifier(but->rnaprop);
244                 bUserMenuItem *umi = (bUserMenuItem *)ED_screen_user_menu_item_find_prop(
245                         &um->items, member_id_data_path, prop_id, but->rnaindex);
246                 if (data_path) {
247                         MEM_freeN((void *)data_path);
248                 }
249                 if (member_id != member_id_data_path) {
250                         MEM_freeN((void *)member_id_data_path);
251                 }
252                 return umi;
253         }
254         else if ((mt = UI_but_menutype_get(but))) {
255                 return (bUserMenuItem *)ED_screen_user_menu_item_find_menu(
256                         &um->items, mt);
257         }
258         else {
259                 return NULL;
260         }
261 }
262
263 static void ui_but_user_menu_add(bContext *C, uiBut *but, bUserMenu *um)
264 {
265         BLI_assert(ui_but_is_user_menu_compatible(C, but));
266
267         char drawstr[sizeof(but->drawstr)];
268         STRNCPY(drawstr, but->drawstr);
269         if (but->flag & UI_BUT_HAS_SEP_CHAR) {
270                 char *sep = strrchr(drawstr, UI_SEP_CHAR);
271                 if (sep) {
272                         *sep = '\0';
273                 }
274         }
275
276         MenuType *mt = NULL;
277         if (but->optype) {
278                 if (drawstr[0] == '\0') {
279                         /* Hard code overrides for generic operators. */
280                         if (UI_but_is_tool(but)) {
281                                 RNA_string_get(but->opptr, "name", drawstr);
282                         }
283                 }
284                 ED_screen_user_menu_item_add_operator(
285                         &um->items, drawstr,
286                         but->optype, but->opptr ? but->opptr->data : NULL, but->opcontext);
287         }
288         else if (but->rnaprop) {
289                 /* Note: 'member_id' may be a path. */
290                 const char *member_id = WM_context_member_from_ptr(C, &but->rnapoin);
291                 const char *data_path = RNA_path_from_ID_to_struct(&but->rnapoin);
292                 const char *member_id_data_path = member_id;
293                 if (data_path) {
294                         member_id_data_path = BLI_sprintfN("%s.%s", member_id, data_path);
295                 }
296                 const char *prop_id = RNA_property_identifier(but->rnaprop);
297                 /* Note, ignore 'drawstr', use property idname always. */
298                 ED_screen_user_menu_item_add_prop(
299                         &um->items, "",
300                         member_id_data_path, prop_id, but->rnaindex);
301                 if (data_path) {
302                         MEM_freeN((void *)data_path);
303                 }
304                 if (member_id != member_id_data_path) {
305                         MEM_freeN((void *)member_id_data_path);
306                 }
307         }
308         else if ((mt = UI_but_menutype_get(but))) {
309                 ED_screen_user_menu_item_add_menu(
310                         &um->items, drawstr,
311                         mt);
312         }
313 }
314
315 static void popup_user_menu_add_or_replace_func(bContext *C, void *arg1, void *UNUSED(arg2))
316 {
317         uiBut *but = arg1;
318         bUserMenu *um = ED_screen_user_menu_ensure(C);
319         ui_but_user_menu_add(C, but, um);
320 }
321
322 static void popup_user_menu_remove_func(bContext *UNUSED(C), void *arg1, void *arg2)
323 {
324         bUserMenu *um = arg1;
325         bUserMenuItem *umi = arg2;
326         ED_screen_user_menu_item_remove(&um->items, umi);
327 }
328
329 static void ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop)
330 {
331         const PropertySubType subtype = RNA_property_subtype(prop);
332         wmOperatorType *ot = WM_operatortype_find("WM_OT_path_open", true);
333         char filepath[FILE_MAX];
334         char dir[FILE_MAXDIR];
335         char file[FILE_MAXFILE];
336         PointerRNA props_ptr;
337
338         BLI_assert(ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH));
339         UNUSED_VARS_NDEBUG(subtype);
340
341         RNA_property_string_get(ptr, prop, filepath);
342         BLI_split_dirfile(filepath, dir, file, sizeof(dir), sizeof(file));
343
344         if (file[0]) {
345                 BLI_assert(subtype == PROP_FILEPATH);
346                 uiItemFullO_ptr(
347                         layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open File Externally"),
348                         ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &props_ptr);
349                 RNA_string_set(&props_ptr, "filepath", filepath);
350         }
351
352         uiItemFullO_ptr(
353                 layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Location Externally"),
354                 ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &props_ptr);
355         RNA_string_set(&props_ptr, "filepath", dir);
356 }
357
358 bool ui_popup_context_menu_for_button(bContext *C, uiBut *but)
359 {
360         /* having this menu for some buttons makes no sense */
361         if (but->type == UI_BTYPE_IMAGE) {
362                 return false;
363         }
364
365         uiPopupMenu *pup;
366         uiLayout *layout;
367
368         {
369                 uiStringInfo label = {BUT_GET_LABEL, NULL};
370
371                 /* highly unlikely getting the label ever fails */
372                 UI_but_string_info_get(C, but, &label, NULL);
373
374                 pup = UI_popup_menu_begin(C, label.strinfo ? label.strinfo : "", ICON_NONE);
375                 layout = UI_popup_menu_layout(pup);
376                 if (label.strinfo) {
377                         MEM_freeN(label.strinfo);
378                 }
379                 uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
380         }
381
382         const bool is_disabled = but->flag & UI_BUT_DISABLED;
383
384         if (is_disabled) {
385                 /* Suppress editing commands. */
386         }
387         else if (but->type == UI_BTYPE_TAB) {
388                 uiButTab *tab = (uiButTab *)but;
389                 if (tab->menu) {
390                         UI_menutype_draw(C, tab->menu, layout);
391                         uiItemS(layout);
392                 }
393         }
394         else if (but->rnapoin.data && but->rnaprop) {
395                 PointerRNA *ptr = &but->rnapoin;
396                 PropertyRNA *prop = but->rnaprop;
397                 const PropertyType type = RNA_property_type(prop);
398                 const PropertySubType subtype = RNA_property_subtype(prop);
399                 bool is_anim = RNA_property_animateable(ptr, prop);
400                 bool is_editable = RNA_property_editable(ptr, prop);
401                 /*bool is_idprop = RNA_property_is_idprop(prop);*/ /* XXX does not work as expected, not strictly needed */
402                 bool is_set = RNA_property_is_set(ptr, prop);
403
404                 /* second slower test, saved people finding keyframe items in menus when its not possible */
405                 if (is_anim)
406                         is_anim = RNA_property_path_from_ID_check(&but->rnapoin, but->rnaprop);
407
408                 /* determine if we can key a single component of an array */
409                 const bool is_array = RNA_property_array_length(&but->rnapoin, but->rnaprop) != 0;
410                 const bool is_array_component = (is_array && but->rnaindex != -1);
411
412                 const int override_status = RNA_property_static_override_status(ptr, prop, -1);
413                 const bool is_overridable = (override_status & RNA_OVERRIDE_STATUS_OVERRIDABLE) != 0;
414
415                 /* Set the (button_pointer, button_prop) and pointer data for Python access to the hovered ui element. */
416                 uiLayoutSetContextFromBut(layout, but);
417
418                 /* Keyframes */
419                 if (but->flag & UI_BUT_ANIMATED_KEY) {
420                         /* replace/delete keyfraemes */
421                         if (is_array_component) {
422                                 uiItemBooleanO(
423                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Keyframes"),
424                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
425                                 uiItemBooleanO(
426                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Single Keyframe"),
427                                         ICON_NONE, "ANIM_OT_keyframe_insert_button", "all", 0);
428                                 uiItemBooleanO(
429                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Keyframes"),
430                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 1);
431                                 uiItemBooleanO(
432                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Single Keyframe"),
433                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 0);
434                         }
435                         else {
436                                 uiItemBooleanO(
437                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Keyframe"),
438                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
439                                 uiItemBooleanO(
440                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Keyframe"),
441                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 1);
442                         }
443
444                         /* keyframe settings */
445                         uiItemS(layout);
446
447
448                 }
449                 else if (but->flag & UI_BUT_DRIVEN) {
450                         /* pass */
451                 }
452                 else if (is_anim) {
453                         if (is_array_component) {
454                                 uiItemBooleanO(
455                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Keyframes"),
456                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
457                                 uiItemBooleanO(
458                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Single Keyframe"),
459                                         ICON_NONE, "ANIM_OT_keyframe_insert_button", "all", 0);
460                         }
461                         else {
462                                 uiItemBooleanO(
463                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Keyframe"),
464                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
465                         }
466                 }
467
468                 if ((but->flag & UI_BUT_ANIMATED) && (but->rnapoin.type != &RNA_NlaStrip)) {
469                         if (is_array_component) {
470                                 uiItemBooleanO(
471                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Keyframes"),
472                                         ICON_KEY_DEHLT, "ANIM_OT_keyframe_clear_button", "all", 1);
473                                 uiItemBooleanO(
474                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Single Keyframes"),
475                                         ICON_NONE, "ANIM_OT_keyframe_clear_button", "all", 0);
476                         }
477                         else {
478                                 uiItemBooleanO(
479                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Keyframes"),
480                                         ICON_KEY_DEHLT, "ANIM_OT_keyframe_clear_button", "all", 1);
481                         }
482                 }
483
484                 /* Drivers */
485                 if (but->flag & UI_BUT_DRIVEN) {
486                         uiItemS(layout);
487
488                         if (is_array_component) {
489                                 uiItemBooleanO(
490                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Drivers"),
491                                         ICON_X, "ANIM_OT_driver_button_remove", "all", 1);
492                                 uiItemBooleanO(
493                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Single Driver"),
494                                         ICON_NONE, "ANIM_OT_driver_button_remove", "all", 0);
495                         }
496                         else {
497                                 uiItemBooleanO(
498                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Driver"),
499                                         ICON_X, "ANIM_OT_driver_button_remove", "all", 1);
500                         }
501
502                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Driver"),
503                                 ICON_NONE, "ANIM_OT_copy_driver_button");
504                         if (ANIM_driver_can_paste()) {
505                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Paste Driver"),
506                                         ICON_NONE, "ANIM_OT_paste_driver_button");
507                         }
508
509                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Edit Driver"),
510                                 ICON_DRIVER, "ANIM_OT_driver_button_edit");
511
512                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Drivers Editor"),
513                                 ICON_NONE, "SCREEN_OT_drivers_editor_show");
514                 }
515                 else if (but->flag & (UI_BUT_ANIMATED_KEY | UI_BUT_ANIMATED)) {
516                         /* pass */
517                 }
518                 else if (is_anim) {
519                         uiItemS(layout);
520
521                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Driver"),
522                                 ICON_DRIVER, "ANIM_OT_driver_button_add");
523
524                         if (ANIM_driver_can_paste()) {
525                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Paste Driver"),
526                                         ICON_NONE, "ANIM_OT_paste_driver_button");
527                         }
528
529                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Drivers Editor"),
530                                 ICON_NONE, "SCREEN_OT_drivers_editor_show");
531                 }
532
533                 /* Keying Sets */
534                 /* TODO: check on modifyability of Keying Set when doing this */
535                 if (is_anim) {
536                         uiItemS(layout);
537
538                         if (is_array_component) {
539                                 uiItemBooleanO(
540                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add All to Keying Set"),
541                                         ICON_KEYINGSET, "ANIM_OT_keyingset_button_add", "all", 1);
542                                 uiItemBooleanO(
543                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Single to Keying Set"),
544                                         ICON_NONE, "ANIM_OT_keyingset_button_add", "all", 0);
545                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove from Keying Set"),
546                                         ICON_NONE, "ANIM_OT_keyingset_button_remove");
547                         }
548                         else {
549                                 uiItemBooleanO(
550                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add to Keying Set"),
551                                         ICON_KEYINGSET, "ANIM_OT_keyingset_button_add", "all", 1);
552                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove from Keying Set"),
553                                         ICON_NONE, "ANIM_OT_keyingset_button_remove");
554                         }
555                 }
556
557                 if (is_overridable) {
558                         wmOperatorType *ot;
559                         PointerRNA op_ptr;
560                         /* Override Operators */
561                         uiItemS(layout);
562
563                         if (but->flag & UI_BUT_OVERRIDEN) {
564                                 if (is_array_component) {
565 #if 0  /* Disabled for now. */
566                                         ot = WM_operatortype_find("UI_OT_override_type_set_button", false);
567                                         uiItemFullO_ptr(
568                                                 layout, ot, "Overrides Type", ICON_NONE,
569                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
570                                         RNA_boolean_set(&op_ptr, "all", true);
571                                         uiItemFullO_ptr(
572                                                 layout, ot, "Single Override Type", ICON_NONE,
573                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
574                                         RNA_boolean_set(&op_ptr, "all", false);
575 #endif
576                                         uiItemBooleanO(
577                                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Overrides"),
578                                                 ICON_X, "UI_OT_override_remove_button", "all", true);
579                                         uiItemBooleanO(
580                                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Single Override"),
581                                                 ICON_X, "UI_OT_override_remove_button", "all", false);
582                                 }
583                                 else {
584 #if 0  /* Disabled for now. */
585                                         uiItemFullO(
586                                                 layout, "UI_OT_override_type_set_button", "Override Type", ICON_NONE,
587                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
588                                         RNA_boolean_set(&op_ptr, "all", false);
589 #endif
590                                         uiItemBooleanO(
591                                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Override"),
592                                                 ICON_X, "UI_OT_override_remove_button", "all", true);
593                                 }
594                         }
595                         else {
596                                 if (is_array_component) {
597                                         ot = WM_operatortype_find("UI_OT_override_type_set_button", false);
598                                         uiItemFullO_ptr(
599                                                 layout, ot, "Define Overrides", ICON_NONE,
600                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
601                                         RNA_boolean_set(&op_ptr, "all", true);
602                                         uiItemFullO_ptr(
603                                                 layout, ot, "Define Single Override", ICON_NONE,
604                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
605                                         RNA_boolean_set(&op_ptr, "all", false);
606                                 }
607                                 else {
608                                         uiItemFullO(
609                                                 layout, "UI_OT_override_type_set_button", "Define Override", ICON_NONE,
610                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
611                                         RNA_boolean_set(&op_ptr, "all", false);
612                                 }
613                         }
614                 }
615
616                 uiItemS(layout);
617
618                 /* Property Operators */
619
620                 /* Copy Property Value
621                  * Paste Property Value */
622
623                 if (is_array_component) {
624                         uiItemBooleanO(
625                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset All to Default Values"),
626                                 ICON_LOOP_BACK, "UI_OT_reset_default_button", "all", 1);
627                         uiItemBooleanO(
628                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset Single to Default Value"),
629                                 ICON_NONE, "UI_OT_reset_default_button", "all", 0);
630                 }
631                 else {
632                         uiItemBooleanO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset to Default Value"),
633                                 ICON_LOOP_BACK, "UI_OT_reset_default_button", "all", 1);
634                 }
635                 if (is_editable /*&& is_idprop*/ && is_set) {
636                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Unset"),
637                                 ICON_NONE, "UI_OT_unset_property_button");
638                 }
639
640                 if (is_array_component) {
641                         uiItemBooleanO(
642                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy All To Selected"),
643                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", true);
644                         uiItemBooleanO(
645                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Single To Selected"),
646                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", false);
647                 }
648                 else {
649                         uiItemBooleanO(
650                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy To Selected"),
651                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", true);
652                 }
653
654                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Data Path"),
655                         ICON_NONE, "UI_OT_copy_data_path_button");
656
657                 uiItemS(layout);
658
659                 if (type == PROP_STRING && ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH)) {
660                         ui_but_menu_add_path_operators(layout, ptr, prop);
661                         uiItemS(layout);
662                 }
663         }
664
665         /* Pointer properties and string properties with prop_search support jumping to target object/bone. */
666         if (but->rnapoin.data && but->rnaprop) {
667                 const PropertyType type = RNA_property_type(but->rnaprop);
668
669                 if ((type == PROP_POINTER) || (type == PROP_STRING && but->type == UI_BTYPE_SEARCH_MENU && but->search_func == ui_rna_collection_search_cb)) {
670                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Jump To Target"),
671                                 ICON_NONE, "UI_OT_jump_to_target_button");
672                         uiItemS(layout);
673                 }
674         }
675
676         /* Favorites Menu */
677         if (ui_but_is_user_menu_compatible(C, but)) {
678                 uiBlock *block = uiLayoutGetBlock(layout);
679                 const int w = uiLayoutGetWidth(layout);
680                 uiBut *but2;
681
682                 but2 = uiDefIconTextBut(
683                         block, UI_BTYPE_BUT, 0, ICON_MENU_PANEL,
684                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add to Quick Favorites"),
685                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0,
686                         "Add to a user defined context menu (stored in the user preferences)");
687                 UI_but_func_set(but2, popup_user_menu_add_or_replace_func, but, NULL);
688
689                 bUserMenu *um = ED_screen_user_menu_find(C);
690                 if (um) {
691                         bUserMenuItem *umi = ui_but_user_menu_find(C, but, um);
692                         if (umi != NULL) {
693                                 but2 = uiDefIconTextBut(
694                                         block, UI_BTYPE_BUT, 0, ICON_BLANK1,
695                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove from Quick Favorites"),
696                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
697                                 UI_but_func_set(but2, popup_user_menu_remove_func, um, umi);
698                         }
699                 }
700                 uiItemS(layout);
701         }
702
703         /* Operator buttons */
704         if (but->optype) {
705                 uiBlock *block = uiLayoutGetBlock(layout);
706                 uiBut *but2;
707                 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
708                 int w = uiLayoutGetWidth(layout);
709                 wmKeyMap *km;
710                 /* We want to know if this op has a shortcut, be it hotkey or not. */
711                 wmKeyMapItem *kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, prop, false, &km);
712
713                 /* We do have a shortcut, but only keyboard ones are editable that way... */
714                 if (kmi) {
715                         if (ISKEYBOARD(kmi->type)) {
716 #if 0                   /* would rather use a block but, but gets weirdly positioned... */
717                                 uiDefBlockBut(
718                                         block, menu_change_shortcut, but, "Change Shortcut",
719                                         0, 0, uiLayoutGetWidth(layout), UI_UNIT_Y, "");
720 #endif
721
722                                 but2 = uiDefIconTextBut(
723                                         block, UI_BTYPE_BUT, 0, ICON_HAND,
724                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Change Shortcut"),
725                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
726                                 UI_but_func_set(but2, popup_change_shortcut_func, but, NULL);
727
728                                 but2 = uiDefIconTextBut(
729                                         block, UI_BTYPE_BUT, 0, ICON_BLANK1,
730                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Shortcut"),
731                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
732                                 UI_but_func_set(but2, remove_shortcut_func, but, NULL);
733                         }
734                         else {
735                                 but2 = uiDefIconTextBut(
736                                         block, UI_BTYPE_BUT, 0, ICON_HAND, IFACE_("Non-Keyboard Shortcut"),
737                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0,
738                                         TIP_("Only keyboard shortcuts can be edited that way, "
739                                              "please use User Preferences otherwise"));
740                                 UI_but_flag_enable(but2, UI_BUT_DISABLED);
741                         }
742                 }
743                 /* only show 'assign' if there's a suitable key map for it to go in */
744                 else if (WM_keymap_guess_opname(C, but->optype->idname)) {
745                         but2 = uiDefIconTextBut(
746                                 block, UI_BTYPE_BUT, 0, ICON_HAND,
747                                 CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Assign Shortcut"),
748                                 0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
749                         UI_but_func_set(but2, popup_add_shortcut_func, but, NULL);
750                 }
751
752                 /* Set the operator pointer for python access */
753                 uiLayoutSetContextFromBut(layout, but);
754
755                 uiItemS(layout);
756         }
757
758         {   /* Docs */
759                 char buf[512];
760
761                 if (UI_but_online_manual_id(but, buf, sizeof(buf))) {
762                         PointerRNA ptr_props;
763                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Manual"),
764                                 ICON_URL, "WM_OT_doc_view_manual_ui_context");
765
766                         if (U.flag & USER_DEVELOPER_UI) {
767                                 uiItemFullO(
768                                         layout, "WM_OT_doc_view",
769                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Python Reference"),
770                                         ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &ptr_props);
771                                 RNA_string_set(&ptr_props, "doc_id", buf);
772                         }
773
774                         /* XXX inactive option, not for public! */
775 #if 0
776                         uiItemFullO(
777                                 layout, "WM_OT_doc_edit", "Submit Description", ICON_NONE,
778                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &ptr_props);
779                         RNA_string_set(&ptr_props, "doc_id", buf);
780                         RNA_string_set(&ptr_props, "doc_new", RNA_property_description(but->rnaprop));
781 #endif
782                 }
783         }
784
785         if (but->optype && U.flag & USER_DEVELOPER_UI) {
786                 uiItemO(layout, NULL,
787                         ICON_NONE, "UI_OT_copy_python_command_button");
788         }
789
790         /* perhaps we should move this into (G.debug & G_DEBUG) - campbell */
791         if (U.flag & USER_DEVELOPER_UI) {
792                 if (ui_block_is_menu(but->block) == false) {
793                         uiItemFullO(layout, "UI_OT_editsource", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, NULL);
794                 }
795         }
796
797         if (BKE_addon_find(&U.addons, "ui_translate")) {
798                 uiItemFullO(layout, "UI_OT_edittranslation_init", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, NULL);
799         }
800
801         /* Show header tools for header buttons. */
802         if (ui_block_is_popup_any(but->block) == false) {
803                 const ARegion *ar = CTX_wm_region(C);
804
805                 if (!ar) {
806                         /* skip */
807                 }
808                 else if (ar->regiontype == RGN_TYPE_HEADER) {
809                         uiItemMenuF(layout, IFACE_("Header"), ICON_NONE, ED_screens_header_tools_menu_create, NULL);
810                 }
811                 else if (ar->regiontype == RGN_TYPE_NAV_BAR) {
812                         uiItemMenuF(layout, IFACE_("Navigation Bar"), ICON_NONE, ED_screens_navigation_bar_tools_menu_create, NULL);
813                 }
814         }
815
816         MenuType *mt = WM_menutype_find("WM_MT_button_context", true);
817         if (mt) {
818                 UI_menutype_draw(C, mt, uiLayoutColumn(layout, false));
819         }
820
821         return UI_popup_menu_end_or_cancel(C, pup);
822 }
823
824 /** \} */
825
826 /* -------------------------------------------------------------------- */
827 /** \name Panel Context Menu
828  * \{ */
829
830 /**
831  * menu to show when right clicking on the panel header
832  */
833 void ui_popup_context_menu_for_panel(bContext *C, ARegion *ar, Panel *pa)
834 {
835         bScreen *sc = CTX_wm_screen(C);
836         const bool has_panel_category = UI_panel_category_is_visible(ar);
837         const bool any_item_visible = has_panel_category;
838         PointerRNA ptr;
839         uiPopupMenu *pup;
840         uiLayout *layout;
841
842         if (!any_item_visible) {
843                 return;
844         }
845
846         RNA_pointer_create(&sc->id, &RNA_Panel, pa, &ptr);
847
848         pup = UI_popup_menu_begin(C, IFACE_("Panel"), ICON_NONE);
849         layout = UI_popup_menu_layout(pup);
850
851         if (has_panel_category) {
852                 char tmpstr[80];
853                 BLI_snprintf(tmpstr, sizeof(tmpstr), "%s" UI_SEP_CHAR_S "%s", IFACE_("Pin"), IFACE_("Shift+Left Mouse"));
854                 uiItemR(layout, &ptr, "use_pin", 0, tmpstr, ICON_NONE);
855
856                 /* evil, force shortcut flag */
857                 {
858                         uiBlock *block = uiLayoutGetBlock(layout);
859                         uiBut *but = block->buttons.last;
860                         but->flag |= UI_BUT_HAS_SEP_CHAR;
861                 }
862         }
863         UI_popup_menu_end(C, pup);
864 }
865
866 /** \} */