UI: allow passing "" for icon only enum buttons and still get an icon
authorCampbell Barton <ideasman42@gmail.com>
Wed, 12 Mar 2014 08:24:47 +0000 (19:24 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 12 Mar 2014 11:56:10 +0000 (22:56 +1100)
Enum icon-only buttons were getting their strings set,
then truncated with blenders string shortening methods, then not drawn
because there was no room (since buttons are icon width).

Modify UI code so icon-only buttons don't get names and passing "" to a
button won't have its text set later on.

source/blender/editors/include/UI_interface.h
source/blender/editors/interface/interface.c
source/blender/editors/interface/interface_layout.c
source/blender/editors/interface/interface_templates.c
source/blender/editors/interface/interface_utils.c
source/blender/editors/interface/interface_widgets.c
source/blender/editors/screen/area.c

index d235441d53e5b347aafab4c5b6d10eaa3c7f9a8b..e486ccc814eada337de5e7e2383b8991c8c6b441 100644 (file)
@@ -185,8 +185,6 @@ enum {
  *       (except for the 'align' ones)!
  */
 enum {
-       /* draw enum-like up/down arrows for button */
-       UI_BUT_DRAW_ENUM_ARROWS  = (1 << 0),
        /* Text and icon alignment (by default, they are centered). */
        UI_BUT_TEXT_LEFT         = (1 << 1),
        UI_BUT_ICON_LEFT         = (1 << 2),
index 765a73b7a3b743f339bc97957d292427671632e4..b8e00022a6132e22be5172ae18fcbef20b2efd7a 100644 (file)
@@ -3164,33 +3164,36 @@ static uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, const char *s
        }
 
        /* use rna values if parameters are not specified */
-       if (!str) {
-               if (ELEM3(type, MENU, ROW, LISTROW) && proptype == PROP_ENUM) {
-                       /* MENU is handled a little differently here */
-                       EnumPropertyItem *item;
-                       int value;
-                       bool free;
-                       int i;
+       if ((proptype == PROP_ENUM) && ELEM3(type, MENU, ROW, LISTROW)) {
+               /* MENU is handled a little differently here */
+               EnumPropertyItem *item;
+               int value;
+               bool free;
+               int i;
 
-                       RNA_property_enum_items(block->evil_C, ptr, prop, &item, NULL, &free);
+               RNA_property_enum_items(block->evil_C, ptr, prop, &item, NULL, &free);
 
-                       if (type == MENU) {
-                               value = RNA_property_enum_get(ptr, prop);
-                       }
-                       else {
-                               value = (int)max;
-                       }
+               if (type == MENU) {
+                       value = RNA_property_enum_get(ptr, prop);
+               }
+               else {
+                       value = (int)max;
+               }
 
-                       i = RNA_enum_from_value(item, value);
-                       if (i != -1) {
-                               str = item[i].name;
-                               icon = item[i].icon;
+               i = RNA_enum_from_value(item, value);
+               if (i != -1) {
 
+                       if (!str) {
+                               str = item[i].name;
 #ifdef WITH_INTERNATIONAL
                                str = CTX_IFACE_(RNA_property_translation_context(prop), str);
 #endif
                        }
-                       else {
+
+                       icon = item[i].icon;
+               }
+               else {
+                       if (!str) {
                                if (type == MENU) {
                                        str = "";
                                }
@@ -3198,19 +3201,21 @@ static uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, const char *s
                                        str = RNA_property_ui_name(prop);
                                }
                        }
+               }
 
-                       if (type == MENU) {
-                               func = ui_def_but_rna__menu;
-                       }
+               if (type == MENU) {
+                       func = ui_def_but_rna__menu;
+               }
 
-                       if (free) {
-                               MEM_freeN(item);
-                       }
+               if (free) {
+                       MEM_freeN(item);
                }
-               else {
+       }
+       else {
+               if (!str) {
                        str = RNA_property_ui_name(prop);
-                       icon = RNA_property_ui_icon(prop);
                }
+               icon = RNA_property_ui_icon(prop);
        }
 
        if (!tip && proptype != PROP_ENUM)
index b5255eb051604862e6b0d6a396a96b977533272c..0ba47eb534cfde9ef459cd12031cae0c6e251ef6 100644 (file)
@@ -1178,12 +1178,18 @@ void uiItemFullR(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int index
        if (icon == ICON_NONE)
                icon = RNA_property_ui_icon(prop);
        
-       if (ELEM4(type, PROP_INT, PROP_FLOAT, PROP_STRING, PROP_POINTER))
+       if (flag & UI_ITEM_R_ICON_ONLY) {
+               /* pass */
+       }
+       else if (ELEM4(type, PROP_INT, PROP_FLOAT, PROP_STRING, PROP_POINTER)) {
                name = ui_item_name_add_colon(name, namestr);
-       else if (type == PROP_BOOLEAN && is_array && index == RNA_NO_INDEX)
+       }
+       else if (type == PROP_BOOLEAN && is_array && index == RNA_NO_INDEX) {
                name = ui_item_name_add_colon(name, namestr);
-       else if (type == PROP_ENUM && index != RNA_ENUM_VALUE)
+       }
+       else if (type == PROP_ENUM && index != RNA_ENUM_VALUE) {
                name = ui_item_name_add_colon(name, namestr);
+       }
 
        if (layout->root->type == UI_LAYOUT_MENU) {
                if (type == PROP_BOOLEAN && ((is_array == false) || (index != RNA_NO_INDEX))) {
@@ -1257,6 +1263,12 @@ void uiItemFullR(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int index
 
        if (no_bg)
                uiBlockSetEmboss(block, UI_EMBOSS);
+
+       /* ensure text isn't added to icon_only buttons */
+       if (but && icon_only) {
+               BLI_assert(but->str[0] == '\0');
+       }
+
 }
 
 void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
index cdaecc62f0ff6820bb5e91931cc7d30082283df2..8ce2a290446695b94b97545e78a767e571938eb2 100644 (file)
@@ -435,8 +435,6 @@ static void template_ID(bContext *C, uiLayout *layout, TemplateID *template, Str
                but = uiDefBlockButN(block, id_search_menu, MEM_dupallocN(template), "", 0, 0, UI_UNIT_X * 1.6, UI_UNIT_Y,
                                     TIP_(template_id_browse_tip(type)));
 
-               uiButSetDrawFlag(but, UI_BUT_DRAW_ENUM_ARROWS);
-
                if (type) {
                        but->icon = RNA_struct_ui_icon(type);
                        /* default dragging of icon for id browse buttons */
index fc87801d6a37458e9bccd270aab3d4911d08b54d..261c666c33f3e6e6bcfdd30da0dd3dc5e289ca7c 100644 (file)
@@ -99,7 +99,7 @@ uiBut *uiDefAutoButR(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int ind
                        else if (icon)
                                but = uiDefIconTextButR_prop(block, MENU, 0, icon, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
                        else
-                               but = uiDefButR_prop(block, MENU, 0, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
+                               but = uiDefButR_prop(block, MENU, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
                        break;
                case PROP_STRING:
                        if (icon && name && name[0] == '\0')
index b4aeef7b01b3112cfad515f0a71777901449263e..91ee357ea4f706575244b09476cc77faa5ff58cf 100644 (file)
@@ -3432,20 +3432,26 @@ void ui_draw_but(const bContext *C, ARegion *ar, uiStyle *style, uiBut *but, rct
                                
                        case MENU:
                        case BLOCK:
-                               /* new node-link button, not active yet XXX */
-                               if (but->flag & UI_BUT_NODE_LINK)
+                               if (but->flag & UI_BUT_NODE_LINK) {
+                                       /* new node-link button, not active yet XXX */
                                        wt = widget_type(UI_WTYPE_MENU_NODE_LINK);
-
-                               /* no text, with icon */
-                               else if (!but->str[0] && but->icon) {
-                                       if (but->drawflag & UI_BUT_DRAW_ENUM_ARROWS)
-                                               wt = widget_type(UI_WTYPE_MENU_RADIO);  /* with arrows */
-                                       else
-                                               wt = widget_type(UI_WTYPE_MENU_ICON_RADIO);  /* no arrows */
                                }
-                               /* with menu arrows */
-                               else
-                                       wt = widget_type(UI_WTYPE_MENU_RADIO);
+                               else {
+                                       /* with menu arrows */
+
+                                       /* we could use a flag for this, but for now just check size,
+                                        * add updown arrows if there is room. */
+                                       if ((!but->str[0] && but->icon && (BLI_rcti_size_x(rect) < BLI_rcti_size_y(rect) + 2)) ||
+                                           /* disable for brushes also */
+                                           (but->flag & UI_ICON_PREVIEW))
+                                       {
+                                               /* no arrows */
+                                               wt = widget_type(UI_WTYPE_MENU_ICON_RADIO);
+                                       }
+                                       else {
+                                               wt = widget_type(UI_WTYPE_MENU_RADIO);
+                                       }
+                               }
                                break;
                                
                        case PULLDOWN:
index 111e2f97f62c312a58def17ed82d944227735496..70378046b02e47ebb37c5e8e39476ba5ee473514 100644 (file)
@@ -1535,7 +1535,7 @@ int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco)
 
        RNA_pointer_create(&(scr->id), &RNA_Area, sa, &areaptr);
 
-       uiDefButR(block, MENU, 0, NULL, xco, yco, 1.5 * U.widget_unit, U.widget_unit,
+       uiDefButR(block, MENU, 0, "", xco, yco, 1.5 * U.widget_unit, U.widget_unit,
                  &areaptr, "type", 0, 0.0f, 0.0f, 0.0f, 0.0f, "");
 
        return xco + 1.7 * U.widget_unit;