Cleanup: move UI context menus into own file
[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 "MEM_guardedalloc.h"
28
29 #include "DNA_scene_types.h"
30 #include "DNA_screen_types.h"
31
32 #include "BLI_path_util.h"
33 #include "BLI_string.h"
34 #include "BLI_utildefines.h"
35
36 #include "BLT_translation.h"
37
38 #include "BKE_addon.h"
39 #include "BKE_context.h"
40 #include "BKE_idprop.h"
41 #include "BKE_screen.h"
42
43 #include "ED_screen.h"
44 #include "ED_keyframing.h"
45
46 #include "UI_interface.h"
47
48 #include "interface_intern.h"
49
50 #include "RNA_access.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 /* This hack is needed because we don't have a good way to re-reference keymap items once added: T42944 */
56 #define USE_KEYMAP_ADD_HACK
57
58 /* -------------------------------------------------------------------- */
59 /** \name Button Context Menu
60  * \{ */
61
62 static void but_shortcut_name_func(bContext *C, void *arg1, int UNUSED(event))
63 {
64         uiBut *but = (uiBut *)arg1;
65
66         if (but->optype) {
67                 char shortcut_str[128];
68
69                 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
70
71                 /* complex code to change name of button */
72                 if (WM_key_event_operator_string(
73                         C, but->optype->idname, but->opcontext, prop, true,
74                         shortcut_str, sizeof(shortcut_str)))
75                 {
76                         ui_but_add_shortcut(but, shortcut_str, true);
77                 }
78                 else {
79                         /* simply strip the shortcut */
80                         ui_but_add_shortcut(but, NULL, true);
81                 }
82         }
83 }
84
85 static uiBlock *menu_change_shortcut(bContext *C, ARegion *ar, void *arg)
86 {
87         wmWindowManager *wm = CTX_wm_manager(C);
88         uiBlock *block;
89         uiBut *but = (uiBut *)arg;
90         wmKeyMap *km;
91         wmKeyMapItem *kmi;
92         PointerRNA ptr;
93         uiLayout *layout;
94         uiStyle *style = UI_style_get_dpi();
95         IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
96
97         kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, prop, true, &km);
98         BLI_assert(kmi != NULL);
99
100         RNA_pointer_create(&wm->id, &RNA_KeyMapItem, kmi, &ptr);
101
102         block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
103         UI_block_func_handle_set(block, but_shortcut_name_func, but);
104         UI_block_flag_enable(block, UI_BLOCK_MOVEMOUSE_QUIT);
105         UI_block_direction_set(block, UI_DIR_CENTER_Y);
106
107         layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 200, 20, 0, style);
108
109         uiItemR(layout, &ptr, "type", UI_ITEM_R_FULL_EVENT | UI_ITEM_R_IMMEDIATE, "", ICON_NONE);
110
111         UI_block_bounds_set_popup(block, 6, -50, 26);
112
113         return block;
114 }
115
116 #ifdef USE_KEYMAP_ADD_HACK
117 static int g_kmi_id_hack;
118 #endif
119
120 static uiBlock *menu_add_shortcut(bContext *C, ARegion *ar, void *arg)
121 {
122         wmWindowManager *wm = CTX_wm_manager(C);
123         uiBlock *block;
124         uiBut *but = (uiBut *)arg;
125         wmKeyMap *km;
126         wmKeyMapItem *kmi;
127         PointerRNA ptr;
128         uiLayout *layout;
129         uiStyle *style = UI_style_get_dpi();
130         IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
131         int kmi_id;
132
133         /* XXX this guess_opname can potentially return a different keymap than being found on adding later... */
134         km = WM_keymap_guess_opname(C, but->optype->idname);
135         kmi = WM_keymap_add_item(km, but->optype->idname, AKEY, KM_PRESS, 0, 0);
136         kmi_id = kmi->id;
137
138         /* copy properties, prop can be NULL for reset */
139         if (prop)
140                 prop = IDP_CopyProperty(prop);
141         WM_keymap_properties_reset(kmi, prop);
142
143         /* update and get pointers again */
144         WM_keyconfig_update(wm);
145
146         km = WM_keymap_guess_opname(C, but->optype->idname);
147         kmi = WM_keymap_item_find_id(km, kmi_id);
148
149         RNA_pointer_create(&wm->id, &RNA_KeyMapItem, kmi, &ptr);
150
151         block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
152         UI_block_func_handle_set(block, but_shortcut_name_func, but);
153         UI_block_direction_set(block, UI_DIR_CENTER_Y);
154
155         layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 200, 20, 0, style);
156
157         uiItemR(layout, &ptr, "type", UI_ITEM_R_FULL_EVENT | UI_ITEM_R_IMMEDIATE, "", ICON_NONE);
158
159         UI_block_bounds_set_popup(block, 6, -50, 26);
160
161 #ifdef USE_KEYMAP_ADD_HACK
162         g_kmi_id_hack = kmi_id;
163 #endif
164         return block;
165 }
166
167 static void menu_add_shortcut_cancel(struct bContext *C, void *arg1)
168 {
169         uiBut *but = (uiBut *)arg1;
170         wmKeyMap *km;
171         wmKeyMapItem *kmi;
172 #ifndef USE_KEYMAP_ADD_HACK
173         IDProperty *prop;
174 #endif
175         int kmi_id;
176
177 #ifdef USE_KEYMAP_ADD_HACK
178         km = WM_keymap_guess_opname(C, but->optype->idname);
179         kmi_id = g_kmi_id_hack;
180         UNUSED_VARS(but);
181 #else
182         prop  = (but->opptr) ? but->opptr->data : NULL;
183         kmi_id = WM_key_event_operator_id(C, but->optype->idname, but->opcontext, prop, true, &km);
184 #endif
185
186         kmi = WM_keymap_item_find_id(km, kmi_id);
187         WM_keymap_remove_item(km, kmi);
188 }
189
190 static void popup_change_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2))
191 {
192         uiBut *but = (uiBut *)arg1;
193         UI_popup_block_invoke(C, menu_change_shortcut, but);
194 }
195
196 static void remove_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2))
197 {
198         uiBut *but = (uiBut *)arg1;
199         wmKeyMap *km;
200         wmKeyMapItem *kmi;
201         IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
202
203         kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, prop, true, &km);
204         BLI_assert(kmi != NULL);
205
206         WM_keymap_remove_item(km, kmi);
207
208         but_shortcut_name_func(C, but, 0);
209 }
210
211 static void popup_add_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2))
212 {
213         uiBut *but = (uiBut *)arg1;
214         UI_popup_block_ex(C, menu_add_shortcut, NULL, menu_add_shortcut_cancel, but, NULL);
215 }
216
217 static void ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop)
218 {
219         const PropertySubType subtype = RNA_property_subtype(prop);
220         wmOperatorType *ot = WM_operatortype_find("WM_OT_path_open", true);
221         char filepath[FILE_MAX];
222         char dir[FILE_MAXDIR];
223         char file[FILE_MAXFILE];
224         PointerRNA props_ptr;
225
226         BLI_assert(ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH));
227         UNUSED_VARS_NDEBUG(subtype);
228
229         RNA_property_string_get(ptr, prop, filepath);
230         BLI_split_dirfile(filepath, dir, file, sizeof(dir), sizeof(file));
231
232         if (file[0]) {
233                 BLI_assert(subtype == PROP_FILEPATH);
234                 uiItemFullO_ptr(
235                         layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open File Externally"),
236                         ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &props_ptr);
237                 RNA_string_set(&props_ptr, "filepath", filepath);
238         }
239
240         uiItemFullO_ptr(
241                 layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Location Externally"),
242                 ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &props_ptr);
243         RNA_string_set(&props_ptr, "filepath", dir);
244 }
245
246 bool ui_popup_context_menu_for_button(bContext *C, uiBut *but)
247 {
248         /* having this menu for some buttons makes no sense */
249         if (but->type == UI_BTYPE_IMAGE) {
250                 return false;
251         }
252
253         uiPopupMenu *pup;
254         uiLayout *layout;
255
256         {
257                 uiStringInfo label = {BUT_GET_LABEL, NULL};
258
259                 /* highly unlikely getting the label ever fails */
260                 UI_but_string_info_get(C, but, &label, NULL);
261
262                 pup = UI_popup_menu_begin(C, label.strinfo ? label.strinfo : "", ICON_NONE);
263                 layout = UI_popup_menu_layout(pup);
264                 if (label.strinfo) {
265                         MEM_freeN(label.strinfo);
266                 }
267                 uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
268         }
269
270         if (but->rnapoin.data && but->rnaprop) {
271                 PointerRNA *ptr = &but->rnapoin;
272                 PropertyRNA *prop = but->rnaprop;
273                 const PropertyType type = RNA_property_type(prop);
274                 const PropertySubType subtype = RNA_property_subtype(prop);
275                 bool is_anim = RNA_property_animateable(ptr, prop);
276                 bool is_editable = RNA_property_editable(ptr, prop);
277                 /*bool is_idprop = RNA_property_is_idprop(prop);*/ /* XXX does not work as expected, not strictly needed */
278                 bool is_set = RNA_property_is_set(ptr, prop);
279
280                 /* second slower test, saved people finding keyframe items in menus when its not possible */
281                 if (is_anim)
282                         is_anim = RNA_property_path_from_ID_check(&but->rnapoin, but->rnaprop);
283
284                 /* determine if we can key a single component of an array */
285                 const bool is_array = RNA_property_array_length(&but->rnapoin, but->rnaprop) != 0;
286                 const bool is_array_component = (is_array && but->rnaindex != -1);
287
288                 /* Keyframes */
289                 if (but->flag & UI_BUT_ANIMATED_KEY) {
290                         /* Set the (button_pointer, button_prop) and pointer data for Python access to the hovered ui element. */
291                         uiLayoutSetContextFromBut(layout, but);
292
293                         /* replace/delete keyfraemes */
294                         if (is_array_component) {
295                                 uiItemBooleanO(
296                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Keyframes"),
297                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
298                                 uiItemBooleanO(
299                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Single Keyframe"),
300                                         ICON_NONE, "ANIM_OT_keyframe_insert_button", "all", 0);
301                                 uiItemBooleanO(
302                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Keyframes"),
303                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 1);
304                                 uiItemBooleanO(
305                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Single Keyframe"),
306                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 0);
307                         }
308                         else {
309                                 uiItemBooleanO(
310                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Keyframe"),
311                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
312                                 uiItemBooleanO(
313                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Keyframe"),
314                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 1);
315                         }
316
317                         /* keyframe settings */
318                         uiItemS(layout);
319
320
321                 }
322                 else if (but->flag & UI_BUT_DRIVEN) {
323                         /* pass */
324                 }
325                 else if (is_anim) {
326                         if (is_array_component) {
327                                 uiItemBooleanO(
328                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Keyframes"),
329                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
330                                 uiItemBooleanO(
331                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Single Keyframe"),
332                                         ICON_NONE, "ANIM_OT_keyframe_insert_button", "all", 0);
333                         }
334                         else {
335                                 uiItemBooleanO(
336                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Keyframe"),
337                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
338                         }
339                 }
340
341                 if ((but->flag & UI_BUT_ANIMATED) && (but->rnapoin.type != &RNA_NlaStrip)) {
342                         if (is_array_component) {
343                                 uiItemBooleanO(
344                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Keyframes"),
345                                         ICON_KEY_DEHLT, "ANIM_OT_keyframe_clear_button", "all", 1);
346                                 uiItemBooleanO(
347                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Single Keyframes"),
348                                         ICON_NONE, "ANIM_OT_keyframe_clear_button", "all", 0);
349                         }
350                         else {
351                                 uiItemBooleanO(
352                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Keyframes"),
353                                         ICON_KEY_DEHLT, "ANIM_OT_keyframe_clear_button", "all", 1);
354                         }
355                 }
356
357                 /* Drivers */
358                 if (but->flag & UI_BUT_DRIVEN) {
359                         uiItemS(layout);
360
361                         if (is_array_component) {
362                                 uiItemBooleanO(
363                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Drivers"),
364                                         ICON_X, "ANIM_OT_driver_button_remove", "all", 1);
365                                 uiItemBooleanO(
366                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Single Driver"),
367                                         ICON_NONE, "ANIM_OT_driver_button_remove", "all", 0);
368                         }
369                         else {
370                                 uiItemBooleanO(
371                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Driver"),
372                                         ICON_X, "ANIM_OT_driver_button_remove", "all", 1);
373                         }
374
375                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Driver"),
376                                 ICON_NONE, "ANIM_OT_copy_driver_button");
377                         if (ANIM_driver_can_paste()) {
378                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Paste Driver"),
379                                         ICON_NONE, "ANIM_OT_paste_driver_button");
380                         }
381                 }
382                 else if (but->flag & (UI_BUT_ANIMATED_KEY | UI_BUT_ANIMATED)) {
383                         /* pass */
384                 }
385                 else if (is_anim) {
386                         uiItemS(layout);
387
388                         if (is_array_component) {
389                                 uiItemMenuEnumO(
390                                         layout, C, "ANIM_OT_driver_button_add", "mapping_type",
391                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Drivers"),
392                                         ICON_DRIVER);
393                         }
394                         else {
395                                 uiItemMenuEnumO(
396                                         layout, C, "ANIM_OT_driver_button_add", "mapping_type",
397                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Driver"),
398                                         ICON_DRIVER);
399                         }
400
401                         if (ANIM_driver_can_paste()) {
402                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Paste Driver"),
403                                         ICON_NONE, "ANIM_OT_paste_driver_button");
404                         }
405                 }
406
407                 /* Keying Sets */
408                 /* TODO: check on modifyability of Keying Set when doing this */
409                 if (is_anim) {
410                         uiItemS(layout);
411
412                         if (is_array_component) {
413                                 uiItemBooleanO(
414                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add All to Keying Set"),
415                                         ICON_KEYINGSET, "ANIM_OT_keyingset_button_add", "all", 1);
416                                 uiItemBooleanO(
417                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Single to Keying Set"),
418                                         ICON_NONE, "ANIM_OT_keyingset_button_add", "all", 0);
419                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove from Keying Set"),
420                                         ICON_NONE, "ANIM_OT_keyingset_button_remove");
421                         }
422                         else {
423                                 uiItemBooleanO(
424                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add to Keying Set"),
425                                         ICON_KEYINGSET, "ANIM_OT_keyingset_button_add", "all", 1);
426                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove from Keying Set"),
427                                         ICON_NONE, "ANIM_OT_keyingset_button_remove");
428                         }
429                 }
430
431                 uiItemS(layout);
432
433                 /* Property Operators */
434
435                 /* Copy Property Value
436                  * Paste Property Value */
437
438                 if (is_array_component) {
439                         uiItemBooleanO(
440                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset All to Default Values"),
441                                 ICON_LOOP_BACK, "UI_OT_reset_default_button", "all", 1);
442                         uiItemBooleanO(
443                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset Single to Default Value"),
444                                 ICON_NONE, "UI_OT_reset_default_button", "all", 0);
445                 }
446                 else {
447                         uiItemBooleanO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset to Default Value"),
448                                 ICON_LOOP_BACK, "UI_OT_reset_default_button", "all", 1);
449                 }
450                 if (is_editable /*&& is_idprop*/ && is_set) {
451                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Unset"),
452                                 ICON_NONE, "UI_OT_unset_property_button");
453                 }
454
455                 if (is_array_component) {
456                         uiItemBooleanO(
457                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy All To Selected"),
458                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", true);
459                         uiItemBooleanO(
460                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Single To Selected"),
461                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", false);
462                 }
463                 else {
464                         uiItemBooleanO(
465                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy To Selected"),
466                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", true);
467                 }
468
469                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Data Path"),
470                         ICON_NONE, "UI_OT_copy_data_path_button");
471
472                 uiItemS(layout);
473
474                 if (type == PROP_STRING && ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH)) {
475                         ui_but_menu_add_path_operators(layout, ptr, prop);
476                         uiItemS(layout);
477                 }
478         }
479
480         /* Operator buttons */
481         if (but->optype) {
482                 uiBlock *block = uiLayoutGetBlock(layout);
483                 uiBut *but2;
484                 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
485                 int w = uiLayoutGetWidth(layout);
486                 wmKeyMap *km;
487                 /* We want to know if this op has a shortcut, be it hotkey or not. */
488                 wmKeyMapItem *kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, prop, false, &km);
489
490                 /* We do have a shortcut, but only keyboard ones are editbale that way... */
491                 if (kmi) {
492                         if (ISKEYBOARD(kmi->type)) {
493 #if 0                   /* would rather use a block but, but gets weirdly positioned... */
494                                 uiDefBlockBut(
495                                         block, menu_change_shortcut, but, "Change Shortcut",
496                                         0, 0, uiLayoutGetWidth(layout), UI_UNIT_Y, "");
497 #endif
498
499                                 but2 = uiDefIconTextBut(
500                                         block, UI_BTYPE_BUT, 0, ICON_HAND,
501                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Change Shortcut"),
502                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
503                                 UI_but_func_set(but2, popup_change_shortcut_func, but, NULL);
504
505                                 but2 = uiDefIconTextBut(
506                                         block, UI_BTYPE_BUT, 0, ICON_NONE,
507                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Shortcut"),
508                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
509                                 UI_but_func_set(but2, remove_shortcut_func, but, NULL);
510                         }
511                         else {
512                                 but2 = uiDefIconTextBut(
513                                         block, UI_BTYPE_BUT, 0, ICON_HAND, IFACE_("Non-Keyboard Shortcut"),
514                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0,
515                                         TIP_("Only keyboard shortcuts can be edited that way, "
516                                              "please use User Preferences otherwise"));
517                                 UI_but_flag_enable(but2, UI_BUT_DISABLED);
518                         }
519                 }
520                 /* only show 'add' if there's a suitable key map for it to go in */
521                 else if (WM_keymap_guess_opname(C, but->optype->idname)) {
522                         but2 = uiDefIconTextBut(
523                                 block, UI_BTYPE_BUT, 0, ICON_HAND,
524                                 CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Shortcut"),
525                                 0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
526                         UI_but_func_set(but2, popup_add_shortcut_func, but, NULL);
527                 }
528
529                 /* Set the operator pointer for python access */
530                 uiLayoutSetContextFromBut(layout, but);
531
532                 uiItemS(layout);
533         }
534
535         /* Show header tools for header buttons. */
536         if (ui_block_is_menu(but->block) == false) {
537                 ARegion *ar = CTX_wm_region(C);
538                 if (ar && (ar->regiontype == RGN_TYPE_HEADER)) {
539                         uiItemMenuF(layout, IFACE_("Header"), ICON_NONE, ED_screens_header_tools_menu_create, NULL);
540                         uiItemS(layout);
541                 }
542         }
543
544         {   /* Docs */
545                 char buf[512];
546
547                 if (UI_but_online_manual_id(but, buf, sizeof(buf))) {
548                         PointerRNA ptr_props;
549                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Manual"),
550                                 ICON_URL, "WM_OT_doc_view_manual_ui_context");
551
552                         uiItemFullO(
553                                 layout, "WM_OT_doc_view",
554                                 CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Python Reference"),
555                                 ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &ptr_props);
556                         RNA_string_set(&ptr_props, "doc_id", buf);
557
558                         /* XXX inactive option, not for public! */
559 #if 0
560                         uiItemFullO(
561                                 layout, "WM_OT_doc_edit", "Submit Description", ICON_NONE,
562                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &ptr_props);
563                         RNA_string_set(&ptr_props, "doc_id", buf);
564                         RNA_string_set(&ptr_props, "doc_new", RNA_property_description(but->rnaprop));
565 #endif
566                 }
567         }
568
569         if (but->optype) {
570                 uiItemO(layout, NULL,
571                         ICON_NONE, "UI_OT_copy_python_command_button");
572         }
573
574         /* perhaps we should move this into (G.debug & G_DEBUG) - campbell */
575         if (U.flag & USER_DEVELOPER_UI) {
576                 if (ui_block_is_menu(but->block) == false) {
577                         uiItemFullO(layout, "UI_OT_editsource", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, NULL);
578                 }
579         }
580
581         if (BKE_addon_find(&U.addons, "ui_translate")) {
582                 uiItemFullO(layout, "UI_OT_edittranslation_init", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, NULL);
583         }
584
585         MenuType *mt = WM_menutype_find("WM_MT_button_context", true);
586         if (mt) {
587                 UI_menutype_draw(C, mt, uiLayoutColumn(layout, false));
588         }
589
590         return UI_popup_menu_end_or_cancel(C, pup);
591 }
592
593 /** \} */
594
595 /* -------------------------------------------------------------------- */
596 /** \name Panel Context Menu
597  * \{ */
598
599 /**
600  * menu to show when right clicking on the panel header
601  */
602 void ui_popup_context_menu_for_panel(bContext *C, ARegion *ar, Panel *pa)
603 {
604         bScreen *sc = CTX_wm_screen(C);
605         const bool has_panel_category = UI_panel_category_is_visible(ar);
606         const bool any_item_visible = has_panel_category;
607         PointerRNA ptr;
608         uiPopupMenu *pup;
609         uiLayout *layout;
610
611         if (!any_item_visible) {
612                 return;
613         }
614
615         RNA_pointer_create(&sc->id, &RNA_Panel, pa, &ptr);
616
617         pup = UI_popup_menu_begin(C, IFACE_("Panel"), ICON_NONE);
618         layout = UI_popup_menu_layout(pup);
619
620         if (has_panel_category) {
621                 char tmpstr[80];
622                 BLI_snprintf(tmpstr, sizeof(tmpstr), "%s" UI_SEP_CHAR_S "%s", IFACE_("Pin"), IFACE_("Shift+Left Mouse"));
623                 uiItemR(layout, &ptr, "use_pin", 0, tmpstr, ICON_NONE);
624
625                 /* evil, force shortcut flag */
626                 {
627                         uiBlock *block = uiLayoutGetBlock(layout);
628                         uiBut *but = block->buttons.last;
629                         but->flag |= UI_BUT_HAS_SEP_CHAR;
630                 }
631         }
632         UI_popup_menu_end(C, pup);
633 }
634
635 /** \} */