[Bf-blender-cvs] [24ae9c52d8b] temp-ui-cpp: Cleanup: Move search menu template to C++

Hans Goudey noreply at git.blender.org
Sat Nov 20 05:51:53 CET 2021


Commit: 24ae9c52d8b679a4c479caf3d38dd7541fbb6af7
Author: Hans Goudey
Date:   Fri Nov 19 23:51:48 2021 -0500
Branches: temp-ui-cpp
https://developer.blender.org/rB24ae9c52d8b679a4c479caf3d38dd7541fbb6af7

Cleanup: Move search menu template to C++

This will allow using improved data structures, and
make other refactors to the search button API cleaner.

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

M	source/blender/editors/interface/CMakeLists.txt
R082	source/blender/editors/interface/interface_template_search_menu.c	source/blender/editors/interface/interface_template_search_menu.cc

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

diff --git a/source/blender/editors/interface/CMakeLists.txt b/source/blender/editors/interface/CMakeLists.txt
index 2a8f40b2631..bc3075f9de8 100644
--- a/source/blender/editors/interface/CMakeLists.txt
+++ b/source/blender/editors/interface/CMakeLists.txt
@@ -73,7 +73,7 @@ set(SRC
   interface_template_asset_view.cc
   interface_template_list.cc
   interface_template_attribute_search.cc
-  interface_template_search_menu.c
+  interface_template_search_menu.cc
   interface_template_search_operator.c
   interface_templates.c
   interface_undo.c
diff --git a/source/blender/editors/interface/interface_template_search_menu.c b/source/blender/editors/interface/interface_template_search_menu.cc
similarity index 82%
rename from source/blender/editors/interface/interface_template_search_menu.c
rename to source/blender/editors/interface/interface_template_search_menu.cc
index 26250e105eb..b5266e6ffb9 100644
--- a/source/blender/editors/interface/interface_template_search_menu.c
+++ b/source/blender/editors/interface/interface_template_search_menu.cc
@@ -21,8 +21,8 @@
  * Accessed via the #WM_OT_search_menu operator.
  */
 
-#include <stdio.h>
-#include <string.h>
+#include <cstdio>
+#include <cstring>
 
 #include "MEM_guardedalloc.h"
 
@@ -85,16 +85,16 @@ struct MenuSearch_Context {
 };
 
 struct MenuSearch_Parent {
-  struct MenuSearch_Parent *parent;
+  MenuSearch_Parent *parent;
   MenuType *parent_mt;
   const char *drawstr;
 
   /** Set while writing menu items only. */
-  struct MenuSearch_Parent *temp_child;
+  MenuSearch_Parent *temp_child;
 };
 
 struct MenuSearch_Item {
-  struct MenuSearch_Item *next, *prev;
+  MenuSearch_Item *next, *prev;
   const char *drawstr;
   const char *drawwstr_full;
   /** Support a single level sub-menu nesting (for operator buttons that expand). */
@@ -102,12 +102,12 @@ struct MenuSearch_Item {
   int icon;
   int state;
 
-  struct MenuSearch_Parent *menu_parent;
+  MenuSearch_Parent *menu_parent;
   MenuType *mt;
 
-  enum {
-    MENU_SEARCH_TYPE_OP = 1,
-    MENU_SEARCH_TYPE_RNA = 2,
+  enum Type {
+    Operator = 1,
+    RNA = 2,
   } type;
 
   union {
@@ -129,8 +129,8 @@ struct MenuSearch_Item {
     } rna;
   };
 
-  /** Set when we need each menu item to be able to set its own context. may be NULL. */
-  struct MenuSearch_Context *wm_context;
+  /** Set when we need each menu item to be able to set its own context. may be nullptr. */
+  MenuSearch_Context *wm_context;
 };
 
 struct MenuSearch_Data {
@@ -148,15 +148,15 @@ struct MenuSearch_Data {
 
 static int menu_item_sort_by_drawstr_full(const void *menu_item_a_v, const void *menu_item_b_v)
 {
-  const struct MenuSearch_Item *menu_item_a = menu_item_a_v;
-  const struct MenuSearch_Item *menu_item_b = menu_item_b_v;
+  const MenuSearch_Item *menu_item_a = (MenuSearch_Item *)menu_item_a_v;
+  const MenuSearch_Item *menu_item_b = (MenuSearch_Item *)menu_item_b_v;
   return strcmp(menu_item_a->drawwstr_full, menu_item_b->drawwstr_full);
 }
 
 static const char *strdup_memarena(MemArena *memarena, const char *str)
 {
   const uint str_size = strlen(str) + 1;
-  char *str_dst = BLI_memarena_alloc(memarena, str_size);
+  char *str_dst = (char *)BLI_memarena_alloc(memarena, str_size);
   memcpy(str_dst, str, str_size);
   return str_dst;
 }
@@ -164,50 +164,53 @@ static const char *strdup_memarena(MemArena *memarena, const char *str)
 static const char *strdup_memarena_from_dynstr(MemArena *memarena, DynStr *dyn_str)
 {
   const uint str_size = BLI_dynstr_get_len(dyn_str) + 1;
-  char *str_dst = BLI_memarena_alloc(memarena, str_size);
+  char *str_dst = (char *)BLI_memarena_alloc(memarena, str_size);
   BLI_dynstr_get_cstring_ex(dyn_str, str_dst);
   return str_dst;
 }
 
-static bool menu_items_from_ui_create_item_from_button(struct MenuSearch_Data *data,
+static bool menu_items_from_ui_create_item_from_button(MenuSearch_Data *data,
                                                        MemArena *memarena,
                                                        struct MenuType *mt,
                                                        const char *drawstr_submenu,
                                                        uiBut *but,
-                                                       struct MenuSearch_Context *wm_context)
+                                                       MenuSearch_Context *wm_context)
 {
-  struct MenuSearch_Item *item = NULL;
+  MenuSearch_Item *item = nullptr;
 
   /* Use override if the name is empty, this can happen with popovers. */
-  const char *drawstr_override = NULL;
+  const char *drawstr_override = nullptr;
   const char *drawstr_sep = (but->flag & UI_BUT_HAS_SEP_CHAR) ?
                                 strrchr(but->drawstr, UI_SEP_CHAR) :
-                                NULL;
+                                nullptr;
   const bool drawstr_is_empty = (drawstr_sep == but->drawstr) || (but->drawstr[0] == '\0');
 
-  if (but->optype != NULL) {
+  if (but->optype != nullptr) {
     if (drawstr_is_empty) {
       drawstr_override = WM_operatortype_name(but->optype, but->opptr);
     }
 
-    item = BLI_memarena_calloc(memarena, sizeof(*item));
-    item->type = MENU_SEARCH_TYPE_OP;
+    item = (MenuSearch_Item *)BLI_memarena_calloc(memarena, sizeof(*item));
+    item->type = MenuSearch_Item::Type::Operator;
 
     item->op.type = but->optype;
     item->op.opcontext = but->opcontext;
     item->op.context = but->context;
     item->op.opptr = but->opptr;
-    but->opptr = NULL;
+    but->opptr = nullptr;
   }
-  else if (but->rnaprop != NULL) {
+  else if (but->rnaprop != nullptr) {
     const int prop_type = RNA_property_type(but->rnaprop);
 
     if (drawstr_is_empty) {
       if (prop_type == PROP_ENUM) {
         const int value_enum = (int)but->hardmax;
         EnumPropertyItem enum_item;
-        if (RNA_property_enum_item_from_value_gettexted(
-                but->block->evil_C, &but->rnapoin, but->rnaprop, value_enum, &enum_item)) {
+        if (RNA_property_enum_item_from_value_gettexted((bContext *)but->block->evil_C,
+                                                        &but->rnapoin,
+                                                        but->rnaprop,
+                                                        value_enum,
+                                                        &enum_item)) {
           drawstr_override = enum_item.name;
         }
         else {
@@ -229,8 +232,8 @@ static bool menu_items_from_ui_create_item_from_button(struct MenuSearch_Data *d
              prop_type);
     }
     else {
-      item = BLI_memarena_calloc(memarena, sizeof(*item));
-      item->type = MENU_SEARCH_TYPE_RNA;
+      item = (MenuSearch_Item *)BLI_memarena_calloc(memarena, sizeof(*item));
+      item->type = MenuSearch_Item::Type::RNA;
 
       item->rna.ptr = but->rnapoin;
       item->rna.prop = but->rnaprop;
@@ -242,13 +245,12 @@ static bool menu_items_from_ui_create_item_from_button(struct MenuSearch_Data *d
     }
   }
 
-  if (item != NULL) {
+  if (item != nullptr) {
     /* Handle shared settings. */
-    if (drawstr_override != NULL) {
+    if (drawstr_override != nullptr) {
       const char *drawstr_suffix = drawstr_sep ? drawstr_sep : "";
-      char *drawstr_alloc = BLI_string_joinN("(", drawstr_override, ")", drawstr_suffix);
-      item->drawstr = strdup_memarena(memarena, drawstr_alloc);
-      MEM_freeN(drawstr_alloc);
+      std::string drawstr = std::string("(") + drawstr_override + ")" + drawstr_suffix;
+      item->drawstr = strdup_memarena(memarena, drawstr.c_str());
     }
     else {
       item->drawstr = strdup_memarena(memarena, but->drawstr);
@@ -258,7 +260,7 @@ static bool menu_items_from_ui_create_item_from_button(struct MenuSearch_Data *d
     item->state = (but->flag &
                    (UI_BUT_DISABLED | UI_BUT_INACTIVE | UI_BUT_REDALERT | UI_BUT_HAS_SEP_CHAR));
     item->mt = mt;
-    item->drawstr_submenu = drawstr_submenu ? strdup_memarena(memarena, drawstr_submenu) : NULL;
+    item->drawstr_submenu = drawstr_submenu ? strdup_memarena(memarena, drawstr_submenu) : nullptr;
 
     item->wm_context = wm_context;
 
@@ -272,11 +274,11 @@ static bool menu_items_from_ui_create_item_from_button(struct MenuSearch_Data *d
 /**
  * Populate a fake button from a menu item (use for context menu).
  */
-static bool menu_items_to_ui_button(struct MenuSearch_Item *item, uiBut *but)
+static bool menu_items_to_ui_button(MenuSearch_Item *item, uiBut *but)
 {
   bool changed = false;
   switch (item->type) {
-    case MENU_SEARCH_TYPE_OP: {
+    case MenuSearch_Item::Type::Operator: {
       but->optype = item->op.type;
       but->opcontext = item->op.opcontext;
       but->context = item->op.context;
@@ -284,7 +286,7 @@ static bool menu_items_to_ui_button(struct MenuSearch_Item *item, uiBut *but)
       changed = true;
       break;
     }
-    case MENU_SEARCH_TYPE_RNA: {
+    case MenuSearch_Item::Type::RNA: {
       const int prop_type = RNA_property_type(item->rna.prop);
 
       but->rnapoin = item->rna.ptr;
@@ -302,12 +304,12 @@ static bool menu_items_to_ui_button(struct MenuSearch_Item *item, uiBut *but)
   if (changed) {
     STRNCPY(but->drawstr, item->drawstr);
     char *drawstr_sep = (item->state & UI_BUT_HAS_SEP_CHAR) ? strrchr(but->drawstr, UI_SEP_CHAR) :
-                                                              NULL;
+                                                              nullptr;
     if (drawstr_sep) {
       *drawstr_sep = '\0';
     }
 
-    but->icon = item->icon;
+    but->icon = (BIFIconID)item->icon;
     but->str = but->strdata;
   }
 
@@ -327,13 +329,13 @@ static void menu_types_add_from_keymap_items(bContext *C,
 {
   wmWindowManager *wm = CTX_wm_manager(C);
   ListBase *handlers[] = {
-      region ? &region->handlers : NULL,
-      area ? &area->handlers : NULL,
+      region ? &region->handlers : nullptr,
+      area ? &area->handlers : nullptr,
       &win->handlers,
   };
 
   for (int handler_index = 0; handler_index < ARRAY_SIZE(handlers); handler_index++) {
-    if (handlers[handler_index] == NULL) {
+    if (handlers[handler_index] == nullptr) {
       continue;
     }
     LISTBASE_FOREACH (wmEventHandler *, handler_base, handlers[handler_index]) 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list