Merge branch 'master' into blender2.8
[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 void popup_user_menu_add_or_replace_func(bContext *C, void *arg1, void *arg2)
220 {
221         uiBut *but = arg1;
222         bUserMenuItem *umi = arg2;
223         if (umi) {
224                 ED_screen_user_menu_remove(umi);
225         }
226         char drawstr[sizeof(but->drawstr)];
227         STRNCPY(drawstr, but->drawstr);
228         if (but->flag & UI_BUT_HAS_SEP_CHAR) {
229                 char *sep = strrchr(drawstr, UI_SEP_CHAR);
230                 if (sep) {
231                         *sep = '\0';
232                 }
233         }
234         ED_screen_user_menu_add(C, drawstr, but->optype, but->opptr ? but->opptr->data : NULL, but->opcontext);
235 }
236
237 static void popup_user_menu_remove_func(bContext *UNUSED(C), void *UNUSED(arg1), void *arg2)
238 {
239         bUserMenuItem *umi = arg2;
240         ED_screen_user_menu_remove(umi);
241 }
242
243 static void ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop)
244 {
245         const PropertySubType subtype = RNA_property_subtype(prop);
246         wmOperatorType *ot = WM_operatortype_find("WM_OT_path_open", true);
247         char filepath[FILE_MAX];
248         char dir[FILE_MAXDIR];
249         char file[FILE_MAXFILE];
250         PointerRNA props_ptr;
251
252         BLI_assert(ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH));
253         UNUSED_VARS_NDEBUG(subtype);
254
255         RNA_property_string_get(ptr, prop, filepath);
256         BLI_split_dirfile(filepath, dir, file, sizeof(dir), sizeof(file));
257
258         if (file[0]) {
259                 BLI_assert(subtype == PROP_FILEPATH);
260                 uiItemFullO_ptr(
261                         layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open File Externally"),
262                         ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &props_ptr);
263                 RNA_string_set(&props_ptr, "filepath", filepath);
264         }
265
266         uiItemFullO_ptr(
267                 layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Location Externally"),
268                 ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &props_ptr);
269         RNA_string_set(&props_ptr, "filepath", dir);
270 }
271
272 bool ui_popup_context_menu_for_button(bContext *C, uiBut *but)
273 {
274         /* having this menu for some buttons makes no sense */
275         if (but->type == UI_BTYPE_IMAGE) {
276                 return false;
277         }
278
279         uiPopupMenu *pup;
280         uiLayout *layout;
281
282         {
283                 uiStringInfo label = {BUT_GET_LABEL, NULL};
284
285                 /* highly unlikely getting the label ever fails */
286                 UI_but_string_info_get(C, but, &label, NULL);
287
288                 pup = UI_popup_menu_begin(C, label.strinfo ? label.strinfo : "", ICON_NONE);
289                 layout = UI_popup_menu_layout(pup);
290                 if (label.strinfo) {
291                         MEM_freeN(label.strinfo);
292                 }
293                 uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
294         }
295
296         if (but->rnapoin.data && but->rnaprop) {
297                 PointerRNA *ptr = &but->rnapoin;
298                 PropertyRNA *prop = but->rnaprop;
299                 const PropertyType type = RNA_property_type(prop);
300                 const PropertySubType subtype = RNA_property_subtype(prop);
301                 bool is_anim = RNA_property_animateable(ptr, prop);
302                 bool is_editable = RNA_property_editable(ptr, prop);
303                 /*bool is_idprop = RNA_property_is_idprop(prop);*/ /* XXX does not work as expected, not strictly needed */
304                 bool is_set = RNA_property_is_set(ptr, prop);
305
306                 /* second slower test, saved people finding keyframe items in menus when its not possible */
307                 if (is_anim)
308                         is_anim = RNA_property_path_from_ID_check(&but->rnapoin, but->rnaprop);
309
310                 /* determine if we can key a single component of an array */
311                 const bool is_array = RNA_property_array_length(&but->rnapoin, but->rnaprop) != 0;
312                 const bool is_array_component = (is_array && but->rnaindex != -1);
313
314                 const int override_status = RNA_property_static_override_status(ptr, prop, -1);
315                 const bool is_overridable = (override_status & RNA_OVERRIDE_STATUS_OVERRIDABLE) != 0;
316
317                 /* Keyframes */
318                 if (but->flag & UI_BUT_ANIMATED_KEY) {
319                         /* Set the (button_pointer, button_prop) and pointer data for Python access to the hovered ui element. */
320                         uiLayoutSetContextFromBut(layout, but);
321
322                         /* replace/delete keyfraemes */
323                         if (is_array_component) {
324                                 uiItemBooleanO(
325                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Keyframes"),
326                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
327                                 uiItemBooleanO(
328                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Single Keyframe"),
329                                         ICON_NONE, "ANIM_OT_keyframe_insert_button", "all", 0);
330                                 uiItemBooleanO(
331                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Keyframes"),
332                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 1);
333                                 uiItemBooleanO(
334                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Single Keyframe"),
335                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 0);
336                         }
337                         else {
338                                 uiItemBooleanO(
339                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Replace Keyframe"),
340                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
341                                 uiItemBooleanO(
342                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Keyframe"),
343                                         ICON_NONE, "ANIM_OT_keyframe_delete_button", "all", 1);
344                         }
345
346                         /* keyframe settings */
347                         uiItemS(layout);
348
349
350                 }
351                 else if (but->flag & UI_BUT_DRIVEN) {
352                         /* pass */
353                 }
354                 else if (is_anim) {
355                         if (is_array_component) {
356                                 uiItemBooleanO(
357                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Keyframes"),
358                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
359                                 uiItemBooleanO(
360                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Single Keyframe"),
361                                         ICON_NONE, "ANIM_OT_keyframe_insert_button", "all", 0);
362                         }
363                         else {
364                                 uiItemBooleanO(
365                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Insert Keyframe"),
366                                         ICON_KEY_HLT, "ANIM_OT_keyframe_insert_button", "all", 1);
367                         }
368                 }
369
370                 if ((but->flag & UI_BUT_ANIMATED) && (but->rnapoin.type != &RNA_NlaStrip)) {
371                         if (is_array_component) {
372                                 uiItemBooleanO(
373                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Keyframes"),
374                                         ICON_KEY_DEHLT, "ANIM_OT_keyframe_clear_button", "all", 1);
375                                 uiItemBooleanO(
376                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Single Keyframes"),
377                                         ICON_NONE, "ANIM_OT_keyframe_clear_button", "all", 0);
378                         }
379                         else {
380                                 uiItemBooleanO(
381                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Keyframes"),
382                                         ICON_KEY_DEHLT, "ANIM_OT_keyframe_clear_button", "all", 1);
383                         }
384                 }
385
386                 /* Drivers */
387                 if (but->flag & UI_BUT_DRIVEN) {
388                         uiItemS(layout);
389
390                         if (is_array_component) {
391                                 uiItemBooleanO(
392                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Drivers"),
393                                         ICON_X, "ANIM_OT_driver_button_remove", "all", 1);
394                                 uiItemBooleanO(
395                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Single Driver"),
396                                         ICON_NONE, "ANIM_OT_driver_button_remove", "all", 0);
397                         }
398                         else {
399                                 uiItemBooleanO(
400                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Delete Driver"),
401                                         ICON_X, "ANIM_OT_driver_button_remove", "all", 1);
402                         }
403
404                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Driver"),
405                                 ICON_NONE, "ANIM_OT_copy_driver_button");
406                         if (ANIM_driver_can_paste()) {
407                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Paste Driver"),
408                                         ICON_NONE, "ANIM_OT_paste_driver_button");
409                         }
410
411                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Edit Driver"),
412                                 ICON_DRIVER, "ANIM_OT_driver_button_edit");
413
414                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Drivers Editor"),
415                                 ICON_NONE, "SCREEN_OT_drivers_editor_show");
416                 }
417                 else if (but->flag & (UI_BUT_ANIMATED_KEY | UI_BUT_ANIMATED)) {
418                         /* pass */
419                 }
420                 else if (is_anim) {
421                         uiItemS(layout);
422
423                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Driver"),
424                                 ICON_DRIVER, "ANIM_OT_driver_button_add");
425
426                         if (ANIM_driver_can_paste()) {
427                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Paste Driver"),
428                                         ICON_NONE, "ANIM_OT_paste_driver_button");
429                         }
430
431                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Drivers Editor"),
432                                 ICON_NONE, "SCREEN_OT_drivers_editor_show");
433                 }
434
435                 /* Keying Sets */
436                 /* TODO: check on modifyability of Keying Set when doing this */
437                 if (is_anim) {
438                         uiItemS(layout);
439
440                         if (is_array_component) {
441                                 uiItemBooleanO(
442                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add All to Keying Set"),
443                                         ICON_KEYINGSET, "ANIM_OT_keyingset_button_add", "all", 1);
444                                 uiItemBooleanO(
445                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Single to Keying Set"),
446                                         ICON_NONE, "ANIM_OT_keyingset_button_add", "all", 0);
447                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove from Keying Set"),
448                                         ICON_NONE, "ANIM_OT_keyingset_button_remove");
449                         }
450                         else {
451                                 uiItemBooleanO(
452                                         layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add to Keying Set"),
453                                         ICON_KEYINGSET, "ANIM_OT_keyingset_button_add", "all", 1);
454                                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove from Keying Set"),
455                                         ICON_NONE, "ANIM_OT_keyingset_button_remove");
456                         }
457                 }
458
459                 if (is_overridable) {
460                         wmOperatorType *ot;
461                         PointerRNA op_ptr;
462                         /* Override Operators */
463                         uiItemS(layout);
464
465                         if (but->flag & UI_BUT_OVERRIDEN) {
466                                 if (is_array_component) {
467 #if 0  /* Disabled for now. */
468                                         ot = WM_operatortype_find("UI_OT_override_type_set_button", false);
469                                         uiItemFullO_ptr(
470                                                 layout, ot, "Overrides Type", ICON_NONE,
471                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
472                                         RNA_boolean_set(&op_ptr, "all", true);
473                                         uiItemFullO_ptr(
474                                                 layout, ot, "Single Override Type", ICON_NONE,
475                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
476                                         RNA_boolean_set(&op_ptr, "all", false);
477 #endif
478                                         uiItemBooleanO(
479                                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Overrides"),
480                                                 ICON_X, "UI_OT_override_remove_button", "all", true);
481                                         uiItemBooleanO(
482                                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Single Override"),
483                                                 ICON_X, "UI_OT_override_remove_button", "all", false);
484                                 }
485                                 else {
486 #if 0  /* Disabled for now. */
487                                         uiItemFullO(
488                                                 layout, "UI_OT_override_type_set_button", "Override Type", ICON_NONE,
489                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
490                                         RNA_boolean_set(&op_ptr, "all", false);
491 #endif
492                                         uiItemBooleanO(
493                                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Override"),
494                                                 ICON_X, "UI_OT_override_remove_button", "all", true);
495                                 }
496                         }
497                         else {
498                                 if (is_array_component) {
499                                         ot = WM_operatortype_find("UI_OT_override_type_set_button", false);
500                                         uiItemFullO_ptr(
501                                                 layout, ot, "Define Overrides", ICON_NONE,
502                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
503                                         RNA_boolean_set(&op_ptr, "all", true);
504                                         uiItemFullO_ptr(
505                                                 layout, ot, "Define Single Override", ICON_NONE,
506                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
507                                         RNA_boolean_set(&op_ptr, "all", false);
508                                 }
509                                 else {
510                                         uiItemFullO(
511                                                 layout, "UI_OT_override_type_set_button", "Define Override", ICON_NONE,
512                                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
513                                         RNA_boolean_set(&op_ptr, "all", false);
514                                 }
515                         }
516                 }
517
518                 uiItemS(layout);
519
520                 /* Property Operators */
521
522                 /* Copy Property Value
523                  * Paste Property Value */
524
525                 if (is_array_component) {
526                         uiItemBooleanO(
527                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset All to Default Values"),
528                                 ICON_LOOP_BACK, "UI_OT_reset_default_button", "all", 1);
529                         uiItemBooleanO(
530                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset Single to Default Value"),
531                                 ICON_NONE, "UI_OT_reset_default_button", "all", 0);
532                 }
533                 else {
534                         uiItemBooleanO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Reset to Default Value"),
535                                 ICON_LOOP_BACK, "UI_OT_reset_default_button", "all", 1);
536                 }
537                 if (is_editable /*&& is_idprop*/ && is_set) {
538                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Unset"),
539                                 ICON_NONE, "UI_OT_unset_property_button");
540                 }
541
542                 if (is_array_component) {
543                         uiItemBooleanO(
544                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy All To Selected"),
545                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", true);
546                         uiItemBooleanO(
547                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Single To Selected"),
548                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", false);
549                 }
550                 else {
551                         uiItemBooleanO(
552                                 layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy To Selected"),
553                                 ICON_NONE, "UI_OT_copy_to_selected_button", "all", true);
554                 }
555
556                 uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy Data Path"),
557                         ICON_NONE, "UI_OT_copy_data_path_button");
558
559                 uiItemS(layout);
560
561                 if (type == PROP_STRING && ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH)) {
562                         ui_but_menu_add_path_operators(layout, ptr, prop);
563                         uiItemS(layout);
564                 }
565         }
566
567         /* Operator buttons */
568         if (but->optype) {
569                 uiBlock *block = uiLayoutGetBlock(layout);
570                 uiBut *but2;
571                 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
572                 int w = uiLayoutGetWidth(layout);
573                 wmKeyMap *km;
574                 /* We want to know if this op has a shortcut, be it hotkey or not. */
575                 wmKeyMapItem *kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, prop, false, &km);
576
577                 /* We do have a shortcut, but only keyboard ones are editbale that way... */
578                 if (kmi) {
579                         if (ISKEYBOARD(kmi->type)) {
580 #if 0                   /* would rather use a block but, but gets weirdly positioned... */
581                                 uiDefBlockBut(
582                                         block, menu_change_shortcut, but, "Change Shortcut",
583                                         0, 0, uiLayoutGetWidth(layout), UI_UNIT_Y, "");
584 #endif
585
586                                 but2 = uiDefIconTextBut(
587                                         block, UI_BTYPE_BUT, 0, ICON_HAND,
588                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Change Shortcut"),
589                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
590                                 UI_but_func_set(but2, popup_change_shortcut_func, but, NULL);
591
592                                 but2 = uiDefIconTextBut(
593                                         block, UI_BTYPE_BUT, 0, ICON_NONE,
594                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove Shortcut"),
595                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
596                                 UI_but_func_set(but2, remove_shortcut_func, but, NULL);
597                         }
598                         else {
599                                 but2 = uiDefIconTextBut(
600                                         block, UI_BTYPE_BUT, 0, ICON_HAND, IFACE_("Non-Keyboard Shortcut"),
601                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0,
602                                         TIP_("Only keyboard shortcuts can be edited that way, "
603                                              "please use User Preferences otherwise"));
604                                 UI_but_flag_enable(but2, UI_BUT_DISABLED);
605                         }
606                 }
607                 /* only show 'add' if there's a suitable key map for it to go in */
608                 else if (WM_keymap_guess_opname(C, but->optype->idname)) {
609                         but2 = uiDefIconTextBut(
610                                 block, UI_BTYPE_BUT, 0, ICON_HAND,
611                                 CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add Shortcut"),
612                                 0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
613                         UI_but_func_set(but2, popup_add_shortcut_func, but, NULL);
614                 }
615
616                 uiItemS(layout);
617
618                 {
619                         bUserMenuItem *umi = ED_screen_user_menu_find(
620                                 C, but->optype, but->opptr ? but->opptr->data : NULL, but->opcontext);
621
622                         but2 = uiDefIconTextBut(
623                                 block, UI_BTYPE_BUT, 0, ICON_MENU_PANEL,
624                                 CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add to Favorites Menu"),
625                                 0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0,
626                                 "Add to a user defined context menu (stored in the user preferences)");
627                         UI_but_func_set(but2, popup_user_menu_add_or_replace_func, but, umi);
628                         if (umi) {
629                                 but2 = uiDefIconTextBut(
630                                         block, UI_BTYPE_BUT, 0, ICON_CANCEL,
631                                         CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Remove from Favorites Menu"),
632                                         0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
633                                 UI_but_func_set(but2, popup_user_menu_remove_func, NULL, umi);
634                         }
635                 }
636
637                 /* Set the operator pointer for python access */
638                 uiLayoutSetContextFromBut(layout, but);
639
640                 uiItemS(layout);
641         }
642
643         /* Show header tools for header buttons. */
644         if (ui_block_is_menu(but->block) == false) {
645                 ARegion *ar = CTX_wm_region(C);
646                 if (ar && (ar->regiontype == RGN_TYPE_HEADER)) {
647                         uiItemMenuF(layout, IFACE_("Header"), ICON_NONE, ED_screens_header_tools_menu_create, NULL);
648                         uiItemS(layout);
649                 }
650         }
651
652         {   /* Docs */
653                 char buf[512];
654
655                 if (UI_but_online_manual_id(but, buf, sizeof(buf))) {
656                         PointerRNA ptr_props;
657                         uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Manual"),
658                                 ICON_URL, "WM_OT_doc_view_manual_ui_context");
659
660                         uiItemFullO(
661                                 layout, "WM_OT_doc_view",
662                                 CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Python Reference"),
663                                 ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &ptr_props);
664                         RNA_string_set(&ptr_props, "doc_id", buf);
665
666                         /* XXX inactive option, not for public! */
667 #if 0
668                         uiItemFullO(
669                                 layout, "WM_OT_doc_edit", "Submit Description", ICON_NONE,
670                                 NULL, WM_OP_INVOKE_DEFAULT, 0, &ptr_props);
671                         RNA_string_set(&ptr_props, "doc_id", buf);
672                         RNA_string_set(&ptr_props, "doc_new", RNA_property_description(but->rnaprop));
673 #endif
674                 }
675         }
676
677         if (but->optype) {
678                 uiItemO(layout, NULL,
679                         ICON_NONE, "UI_OT_copy_python_command_button");
680         }
681
682         /* perhaps we should move this into (G.debug & G_DEBUG) - campbell */
683         if (U.flag & USER_DEVELOPER_UI) {
684                 if (ui_block_is_menu(but->block) == false) {
685                         uiItemFullO(layout, "UI_OT_editsource", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, NULL);
686                 }
687         }
688
689         if (BKE_addon_find(&U.addons, "ui_translate")) {
690                 uiItemFullO(layout, "UI_OT_edittranslation_init", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, NULL);
691         }
692
693         MenuType *mt = WM_menutype_find("WM_MT_button_context", true);
694         if (mt) {
695                 UI_menutype_draw(C, mt, uiLayoutColumn(layout, false));
696         }
697
698         return UI_popup_menu_end_or_cancel(C, pup);
699 }
700
701 /** \} */
702
703 /* -------------------------------------------------------------------- */
704 /** \name Panel Context Menu
705  * \{ */
706
707 /**
708  * menu to show when right clicking on the panel header
709  */
710 void ui_popup_context_menu_for_panel(bContext *C, ARegion *ar, Panel *pa)
711 {
712         bScreen *sc = CTX_wm_screen(C);
713         const bool has_panel_category = UI_panel_category_is_visible(ar);
714         const bool any_item_visible = has_panel_category;
715         PointerRNA ptr;
716         uiPopupMenu *pup;
717         uiLayout *layout;
718
719         if (!any_item_visible) {
720                 return;
721         }
722
723         RNA_pointer_create(&sc->id, &RNA_Panel, pa, &ptr);
724
725         pup = UI_popup_menu_begin(C, IFACE_("Panel"), ICON_NONE);
726         layout = UI_popup_menu_layout(pup);
727
728         if (has_panel_category) {
729                 char tmpstr[80];
730                 BLI_snprintf(tmpstr, sizeof(tmpstr), "%s" UI_SEP_CHAR_S "%s", IFACE_("Pin"), IFACE_("Shift+Left Mouse"));
731                 uiItemR(layout, &ptr, "use_pin", 0, tmpstr, ICON_NONE);
732
733                 /* evil, force shortcut flag */
734                 {
735                         uiBlock *block = uiLayoutGetBlock(layout);
736                         uiBut *but = block->buttons.last;
737                         but->flag |= UI_BUT_HAS_SEP_CHAR;
738                 }
739         }
740         UI_popup_menu_end(C, pup);
741 }
742
743 /** \} */