Commit: e61bbc00b759b958c729ac3b014944416edd5d36
Author: Campbell Barton
Date:   Sun Jun 24 10:01:13 2018 +0200
Branches: master
https://developer.blender.org/rBe61bbc00b759b958c729ac3b014944416edd5d36

Cleanup: move UI context menus into own file

===================================================================

M       source/blender/editors/interface/CMakeLists.txt
A       source/blender/editors/interface/interface_context_menu.c
M       source/blender/editors/interface/interface_handlers.c
M       source/blender/editors/interface/interface_intern.h
M       source/blender/editors/interface/interface_panel.c

===================================================================

diff --git a/source/blender/editors/interface/CMakeLists.txt 
b/source/blender/editors/interface/CMakeLists.txt
index 0adc315fd69..3a05e60bd35 100644
--- a/source/blender/editors/interface/CMakeLists.txt
+++ b/source/blender/editors/interface/CMakeLists.txt
@@ -42,6 +42,7 @@ set(SRC
        interface.c
        interface_align.c
        interface_anim.c
+       interface_context_menu.c
        interface_draw.c
        interface_eyedropper.c
        interface_eyedropper_color.c
@@ -54,13 +55,13 @@ set(SRC
        interface_layout.c
        interface_ops.c
        interface_panel.c
-       interface_regions.c
        interface_region_color_picker.c
        interface_region_menu_pie.c
        interface_region_menu_popup.c
        interface_region_popup.c
        interface_region_search.c
        interface_region_tooltip.c
+       interface_regions.c
        interface_style.c
        interface_templates.c
        interface_utils.c
diff --git a/source/blender/editors/interface/interface_context_menu.c 
b/source/blender/editors/interface/interface_context_menu.c
new file mode 100644
index 00000000000..0306139bac0
--- /dev/null
+++ b/source/blender/editors/interface/interface_context_menu.c
@@ -0,0 +1,635 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/editors/interface/interface_context_menu.c
+ *  \ingroup edinterface
+ *
+ * Generic context popup menus.
+ */
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+
+#include "BLI_path_util.h"
+#include "BLI_string.h"
+#include "BLI_utildefines.h"
+
+#include "BLT_translation.h"
+
+#include "BKE_addon.h"
+#include "BKE_context.h"
+#include "BKE_idprop.h"
+#include "BKE_screen.h"
+
+#include "ED_screen.h"
+#include "ED_keyframing.h"
+
+#include "UI_interface.h"
+
+#include "interface_intern.h"
+
+#include "RNA_access.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+/* This hack is needed because we don't have a good way to re-reference keymap 
items once added: T42944 */
+#define USE_KEYMAP_ADD_HACK
+
+/* -------------------------------------------------------------------- */
+/** \name Button Context Menu
+ * \{ */
+
+static void but_shortcut_name_func(bContext *C, void *arg1, int UNUSED(event))
+{
+       uiBut *but = (uiBut *)arg1;
+
+       if (but->optype) {
+               char shortcut_str[128];
+
+               IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
+
+               /* complex code to change name of button */
+               if (WM_key_event_operator_string(
+                       C, but->optype->idname, but->opcontext, prop, true,
+                       shortcut_str, sizeof(shortcut_str)))
+               {
+                       ui_but_add_shortcut(but, shortcut_str, true);
+               }
+               else {
+                       /* simply strip the shortcut */
+                       ui_but_add_shortcut(but, NULL, true);
+               }
+       }
+}
+
+static uiBlock *menu_change_shortcut(bContext *C, ARegion *ar, void *arg)
+{
+       wmWindowManager *wm = CTX_wm_manager(C);
+       uiBlock *block;
+       uiBut *but = (uiBut *)arg;
+       wmKeyMap *km;
+       wmKeyMapItem *kmi;
+       PointerRNA ptr;
+       uiLayout *layout;
+       uiStyle *style = UI_style_get_dpi();
+       IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
+
+       kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, 
prop, true, &km);
+       BLI_assert(kmi != NULL);
+
+       RNA_pointer_create(&wm->id, &RNA_KeyMapItem, kmi, &ptr);
+
+       block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
+       UI_block_func_handle_set(block, but_shortcut_name_func, but);
+       UI_block_flag_enable(block, UI_BLOCK_MOVEMOUSE_QUIT);
+       UI_block_direction_set(block, UI_DIR_CENTER_Y);
+
+       layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 
0, 200, 20, 0, style);
+
+       uiItemR(layout, &ptr, "type", UI_ITEM_R_FULL_EVENT | 
UI_ITEM_R_IMMEDIATE, "", ICON_NONE);
+
+       UI_block_bounds_set_popup(block, 6, -50, 26);
+
+       return block;
+}
+
+#ifdef USE_KEYMAP_ADD_HACK
+static int g_kmi_id_hack;
+#endif
+
+static uiBlock *menu_add_shortcut(bContext *C, ARegion *ar, void *arg)
+{
+       wmWindowManager *wm = CTX_wm_manager(C);
+       uiBlock *block;
+       uiBut *but = (uiBut *)arg;
+       wmKeyMap *km;
+       wmKeyMapItem *kmi;
+       PointerRNA ptr;
+       uiLayout *layout;
+       uiStyle *style = UI_style_get_dpi();
+       IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
+       int kmi_id;
+
+       /* XXX this guess_opname can potentially return a different keymap than 
being found on adding later... */
+       km = WM_keymap_guess_opname(C, but->optype->idname);
+       kmi = WM_keymap_add_item(km, but->optype->idname, AKEY, KM_PRESS, 0, 0);
+       kmi_id = kmi->id;
+
+       /* copy properties, prop can be NULL for reset */
+       if (prop)
+               prop = IDP_CopyProperty(prop);
+       WM_keymap_properties_reset(kmi, prop);
+
+       /* update and get pointers again */
+       WM_keyconfig_update(wm);
+
+       km = WM_keymap_guess_opname(C, but->optype->idname);
+       kmi = WM_keymap_item_find_id(km, kmi_id);
+
+       RNA_pointer_create(&wm->id, &RNA_KeyMapItem, kmi, &ptr);
+
+       block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
+       UI_block_func_handle_set(block, but_shortcut_name_func, but);
+       UI_block_direction_set(block, UI_DIR_CENTER_Y);
+
+       layout = UI_block_layout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 
0, 200, 20, 0, style);
+
+       uiItemR(layout, &ptr, "type", UI_ITEM_R_FULL_EVENT | 
UI_ITEM_R_IMMEDIATE, "", ICON_NONE);
+
+       UI_block_bounds_set_popup(block, 6, -50, 26);
+
+#ifdef USE_KEYMAP_ADD_HACK
+       g_kmi_id_hack = kmi_id;
+#endif
+       return block;
+}
+
+static void menu_add_shortcut_cancel(struct bContext *C, void *arg1)
+{
+       uiBut *but = (uiBut *)arg1;
+       wmKeyMap *km;
+       wmKeyMapItem *kmi;
+#ifndef USE_KEYMAP_ADD_HACK
+       IDProperty *prop;
+#endif
+       int kmi_id;
+
+#ifdef USE_KEYMAP_ADD_HACK
+       km = WM_keymap_guess_opname(C, but->optype->idname);
+       kmi_id = g_kmi_id_hack;
+       UNUSED_VARS(but);
+#else
+       prop  = (but->opptr) ? but->opptr->data : NULL;
+       kmi_id = WM_key_event_operator_id(C, but->optype->idname, 
but->opcontext, prop, true, &km);
+#endif
+
+       kmi = WM_keymap_item_find_id(km, kmi_id);
+       WM_keymap_remove_item(km, kmi);
+}
+
+static void popup_change_shortcut_func(bContext *C, void *arg1, void 
*UNUSED(arg2))
+{
+       uiBut *but = (uiBut *)arg1;
+       UI_popup_block_invoke(C, menu_change_shortcut, but);
+}
+
+static void remove_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2))
+{
+       uiBut *but = (uiBut *)arg1;
+       wmKeyMap *km;
+       wmKeyMapItem *kmi;
+       IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
+
+       kmi = WM_key_event_operator(C, but->optype->idname, but->opcontext, 
prop, true, &km);
+       BLI_assert(kmi != NULL);
+
+       WM_keymap_remove_item(km, kmi);
+
+       but_shortcut_name_func(C, but, 0);
+}
+
+static void popup_add_shortcut_func(bContext *C, void *arg1, void 
*UNUSED(arg2))
+{
+       uiBut *but = (uiBut *)arg1;
+       UI_popup_block_ex(C, menu_add_shortcut, NULL, menu_add_shortcut_cancel, 
but, NULL);
+}
+
+static void ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, 
PropertyRNA *prop)
+{
+       const PropertySubType subtype = RNA_property_subtype(prop);
+       wmOperatorType *ot = WM_operatortype_find("WM_OT_path_open", true);
+       char filepath[FILE_MAX];
+       char dir[FILE_MAXDIR];
+       char file[FILE_MAXFILE];
+       PointerRNA props_ptr;
+
+       BLI_assert(ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH));
+       UNUSED_VARS_NDEBUG(subtype);
+
+       RNA_property_string_get(ptr, prop, filepath);
+       BLI_split_dirfile(filepath, dir, file, sizeof(dir), sizeof(file));
+
+       if (file[0]) {
+               BLI_assert(subtype == PROP_FILEPATH);
+               uiItemFullO_ptr(
+                       layout, ot, 
CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open File Externally"),
+                       ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &props_ptr);
+               RNA_string_set(&props_ptr, "filepath", filepath);
+       }
+
+       uiItemFullO_ptr(
+               layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open 
Location Externally"),
+               ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &props_ptr);
+       RNA_string_set(&props_ptr, "filepath", dir);
+}
+
+bool ui_popup_context_menu_for_button(bContext *C, uiBut *but)
+{
+       /* having this menu for some buttons makes no sense */
+       if (but->type == UI_BTYPE_IMAGE) {
+               return false;
+       }
+
+       uiPopupMenu *pup;
+       uiLayout *layout;
+
+       {
+               uiStringInfo label = {BUT_GET_LABEL, NULL};
+
+               /* highly unlikely getting the label ever fails */
+               UI_but_string_info_get(C, but, &label, NULL);
+
+               pup = UI_popup_menu_begin(C, label.strinfo ? label.strinfo : 
"", ICON_NONE);
+               layout = UI_popup_menu_layout(pup);
+               if (label.strinfo) {
+                       MEM_freeN(label.strinfo);
+               }
+               uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
+       }
+
+       if (but->rnapoin.data && but->rnaprop) {
+               PointerRNA *ptr = &but->rnapoin;
+               PropertyRNA *prop = but->rnaprop;
+               const PropertyType type = RNA_property_type(prop);
+               const PropertySubType subtype = RNA_property_subtype(prop);
+               bool is_anim = RNA_property_animateable(ptr, prop);
+               bool is_editable = RNA_property_editable(ptr, prop);
+               /*bool is_idprop = RNA_property_is_idprop(prop);*/ /* XXX does 
not work as expected, not strictly needed */
+               bool is_set = RNA_property_is_set(ptr, prop);
+
+               /* second slower test, saved people finding keyframe items in 
menus when its not possible */
+               if (is_anim)
+                       is_anim = 
RNA_property_path_from_ID_check(&but->rnapoin, but->rnaprop);
+
+               /* determine if we can key a single component of an array */
+               const bool is_array = RNA_property_array_length(&but->rnapoin, 
but->rnaprop) != 0;
+               const bool is_array_component = (is_array && but->rnaindex != 
-1);
+
+               /* Keyframes */
+               if (but->flag & UI_BUT_ANIMATED_KEY) {
+                       /* Set the (button_pointer, button_prop) and pointer 
data for Python access to the hovered ui element. */
+                       uiLayoutSetContextFromBut(layout, but);
+
+                       /* replace/delete keyfraemes */
+                       if (is_array_component)

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to