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