2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Blender Foundation 2009.
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/editors/interface/interface_layout.c
24 * \ingroup edinterface
34 #include "MEM_guardedalloc.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_armature_types.h"
38 #include "DNA_userdef_types.h"
40 #include "BLI_listbase.h"
41 #include "BLI_string.h"
43 #include "BLI_utildefines.h"
46 #include "BLF_translation.h"
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_idprop.h"
51 #include "BKE_screen.h"
53 #include "RNA_access.h"
55 #include "UI_interface.h"
61 #include "interface_intern.h"
63 /************************ Structs and Defines *************************/
65 #define RNA_NO_INDEX -1
66 #define RNA_ENUM_VALUE -2
69 // #define USE_OP_RESET_BUT // we may want to make this optional, disable for now.
71 #define UI_OPERATOR_ERROR_RET(_ot, _opname, return_statement) \
73 ui_item_disabled(layout, _opname); \
74 RNA_warning("'%s' unknown operator", _opname); \
81 typedef struct uiLayoutRoot {
82 struct uiLayoutRoot *next, *prev;
89 uiMenuHandleFunc handlefunc;
99 typedef enum uiItemType {
104 ITEM_LAYOUT_COLUMN_FLOW,
105 ITEM_LAYOUT_ROW_FLOW,
107 ITEM_LAYOUT_ABSOLUTE,
113 TEMPLATE_COLUMN_FLOW,
122 typedef struct uiItem {
128 typedef struct uiButtonItem {
137 bContextStore *context;
151 typedef struct uiLayoutItemFlow {
157 typedef struct uiLayoutItemBx {
162 typedef struct uiLayoutItemSplit {
167 typedef struct uiLayoutItemRoot {
171 /************************** Item ***************************/
173 static const char *ui_item_name_add_colon(const char *name, char namestr[UI_MAX_NAME_STR])
175 int len = strlen(name);
177 if (len != 0 && len + 1 < UI_MAX_NAME_STR) {
178 BLI_strncpy(namestr, name, UI_MAX_NAME_STR);
180 namestr[len + 1] = '\0';
187 static int ui_item_fit(int item, int pos, int all, int available, int last, int alignment, int *offset)
192 /* available == 0 is unlimited */
196 if (all > available) {
197 /* contents is bigger than available space */
199 return available - pos;
201 return (item * available) / all;
204 /* contents is smaller or equal to available space */
205 if (alignment == UI_LAYOUT_ALIGN_EXPAND) {
207 return available - pos;
209 return (item * available) / all;
216 /* variable button size in which direction? */
217 #define UI_ITEM_VARY_X 1
218 #define UI_ITEM_VARY_Y 2
220 static int ui_layout_vary_direction(uiLayout *layout)
222 return (layout->root->type == UI_LAYOUT_HEADER || layout->alignment != UI_LAYOUT_ALIGN_EXPAND) ? UI_ITEM_VARY_X : UI_ITEM_VARY_Y;
225 /* estimated size of text + icon */
226 static int ui_text_icon_width(uiLayout *layout, const char *name, int icon, int compact)
228 float f5 = 0.25f * UI_UNIT_X;
229 float f10 = 0.5f * UI_UNIT_X;
230 int variable = ui_layout_vary_direction(layout) == UI_ITEM_VARY_X;
232 if (icon && !name[0])
233 return UI_UNIT_X; /* icon only */
235 return (variable) ? UI_GetStringWidth(name) + (compact ? f5 : f10) + UI_UNIT_X : 10 * UI_UNIT_X; /* icon + text */
237 return (variable) ? UI_GetStringWidth(name) + (compact ? f5 : f10) + UI_UNIT_X : 10 * UI_UNIT_X; /* text only */
240 static void ui_item_size(uiItem *item, int *r_w, int *r_h)
242 if (item->type == ITEM_BUTTON) {
243 uiButtonItem *bitem = (uiButtonItem *)item;
245 if (r_w) *r_w = BLI_rctf_size_x(&bitem->but->rect);
246 if (r_h) *r_h = BLI_rctf_size_y(&bitem->but->rect);
249 uiLayout *litem = (uiLayout *)item;
251 if (r_w) *r_w = litem->w;
252 if (r_h) *r_h = litem->h;
256 static void ui_item_offset(uiItem *item, int *r_x, int *r_y)
258 if (item->type == ITEM_BUTTON) {
259 uiButtonItem *bitem = (uiButtonItem *)item;
261 if (r_x) *r_x = bitem->but->rect.xmin;
262 if (r_y) *r_y = bitem->but->rect.ymin;
270 static void ui_item_position(uiItem *item, int x, int y, int w, int h)
272 if (item->type == ITEM_BUTTON) {
273 uiButtonItem *bitem = (uiButtonItem *)item;
275 bitem->but->rect.xmin = x;
276 bitem->but->rect.ymin = y;
277 bitem->but->rect.xmax = x + w;
278 bitem->but->rect.ymax = y + h;
280 ui_check_but(bitem->but); /* for strlen */
283 uiLayout *litem = (uiLayout *)item;
292 /******************** Special RNA Items *********************/
294 static int ui_layout_local_dir(uiLayout *layout)
296 switch (layout->item.type) {
297 case ITEM_LAYOUT_ROW:
298 case ITEM_LAYOUT_ROOT:
299 case ITEM_LAYOUT_OVERLAP:
300 return UI_LAYOUT_HORIZONTAL;
301 case ITEM_LAYOUT_COLUMN:
302 case ITEM_LAYOUT_COLUMN_FLOW:
303 case ITEM_LAYOUT_SPLIT:
304 case ITEM_LAYOUT_ABSOLUTE:
305 case ITEM_LAYOUT_BOX:
307 return UI_LAYOUT_VERTICAL;
311 static uiLayout *ui_item_local_sublayout(uiLayout *test, uiLayout *layout, int align)
315 if (ui_layout_local_dir(test) == UI_LAYOUT_HORIZONTAL)
316 sub = uiLayoutRow(layout, align);
318 sub = uiLayoutColumn(layout, align);
324 static void ui_layer_but_cb(bContext *C, void *arg_but, void *arg_index)
326 wmWindow *win = CTX_wm_window(C);
327 uiBut *but = arg_but, *cbut;
328 PointerRNA *ptr = &but->rnapoin;
329 PropertyRNA *prop = but->rnaprop;
330 int i, index = GET_INT_FROM_POINTER(arg_index);
331 int shift = win->eventstate->shift;
332 int len = RNA_property_array_length(ptr, prop);
335 RNA_property_boolean_set_index(ptr, prop, index, TRUE);
337 for (i = 0; i < len; i++)
339 RNA_property_boolean_set_index(ptr, prop, i, 0);
341 RNA_property_update(C, ptr, prop);
343 for (cbut = but->block->buttons.first; cbut; cbut = cbut->next)
348 /* create buttons for an item with an RNA array */
349 static void ui_item_array(uiLayout *layout, uiBlock *block, const char *name, int icon,
350 PointerRNA *ptr, PropertyRNA *prop, int len, int x, int y, int w, int UNUSED(h),
351 int expand, int slider, int toggle, int icon_only)
353 uiStyle *style = layout->root->style;
356 PropertySubType subtype;
360 /* retrieve type and subtype */
361 type = RNA_property_type(prop);
362 subtype = RNA_property_subtype(prop);
364 sub = ui_item_local_sublayout(layout, layout, 1);
365 uiBlockSetCurLayout(block, sub);
369 uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
372 if (type == PROP_BOOLEAN && ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER)) {
373 /* special check for layer layout */
374 int butw, buth, unit;
375 int cols = (len >= 20) ? 2 : 1;
376 const unsigned int colbuts = len / (2 * cols);
377 unsigned int layer_used = 0;
379 uiBlockSetCurLayout(block, uiLayoutAbsolute(layout, FALSE));
381 unit = UI_UNIT_X * 0.75;
385 if (ptr->type == &RNA_Armature) {
386 bArmature *arm = (bArmature *)ptr->data;
387 layer_used = arm->layer_used;
390 for (b = 0; b < cols; b++) {
391 uiBlockBeginAlign(block);
393 for (a = 0; a < colbuts; a++) {
394 if (layer_used & (1 << (a + b * colbuts))) icon = ICON_LAYER_USED;
395 else icon = ICON_BLANK1;
397 but = uiDefAutoButR(block, ptr, prop, a + b * colbuts, "", icon, x + butw * a, y + buth, butw, buth);
398 if (subtype == PROP_LAYER_MEMBER)
399 uiButSetFunc(but, ui_layer_but_cb, but, SET_INT_IN_POINTER(a + b * colbuts));
401 for (a = 0; a < colbuts; a++) {
402 if (layer_used & (1 << (a + len / 2 + b * colbuts))) icon = ICON_LAYER_USED;
403 else icon = ICON_BLANK1;
405 but = uiDefAutoButR(block, ptr, prop, a + len / 2 + b * colbuts, "", icon, x + butw * a, y, butw, buth);
406 if (subtype == PROP_LAYER_MEMBER)
407 uiButSetFunc(but, ui_layer_but_cb, but, SET_INT_IN_POINTER(a + len / 2 + b * colbuts));
409 uiBlockEndAlign(block);
411 x += colbuts * butw + style->buttonspacex;
414 else if (subtype == PROP_MATRIX) {
415 int totdim, dim_size[3]; /* 3 == RNA_MAX_ARRAY_DIMENSION */
418 uiBlockSetCurLayout(block, uiLayoutAbsolute(layout, TRUE));
420 totdim = RNA_property_array_dimension(ptr, prop, dim_size);
421 if (totdim != 2) return; /* only 2D matrices supported in UI so far */
424 /* h /= dim_size[1]; */ /* UNUSED */
426 for (a = 0; a < len; a++) {
427 col = a % dim_size[0];
428 row = a / dim_size[0];
430 but = uiDefAutoButR(block, ptr, prop, a, "", ICON_NONE, x + w * col, y + (dim_size[1] * UI_UNIT_Y) - (row * UI_UNIT_Y), w, UI_UNIT_Y);
431 if (slider && but->type == NUM)
435 else if (subtype == PROP_DIRECTION && !expand) {
436 uiDefButR_prop(block, BUT_NORMAL, 0, name, x, y, UI_UNIT_X * 3, UI_UNIT_Y * 3, ptr, prop, 0, 0, 0, -1, -1, NULL);
439 /* note, this block of code is a bit arbitrary and has just been made
440 * to work with common cases, but may need to be re-worked */
442 /* special case, boolean array in a menu, this could be used in a more generic way too */
443 if (ELEM(subtype, PROP_COLOR, PROP_COLOR_GAMMA) && !expand) {
444 uiDefAutoButR(block, ptr, prop, -1, "", ICON_NONE, 0, 0, w, UI_UNIT_Y);
449 /* even if 'expand' is fale, expanding anyway */
451 /* layout for known array subtypes */
452 char str[3] = {'\0'};
455 if (type != PROP_BOOLEAN) {
460 /* show checkboxes for rna on a non-emboss block (menu for eg) */
461 if (type == PROP_BOOLEAN && ELEM(layout->root->block->dt, UI_EMBOSSN, UI_EMBOSSP)) {
462 boolarr = MEM_callocN(sizeof(int) * len, "ui_item_array");
463 RNA_property_boolean_get_array(ptr, prop, boolarr);
466 for (a = 0; a < len; a++) {
467 if (!icon_only) str[0] = RNA_property_array_item_char(prop, a);
468 if (boolarr) icon = boolarr[a] ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
469 but = uiDefAutoButR(block, ptr, prop, a, str, icon, 0, 0, w, UI_UNIT_Y);
470 if (slider && but->type == NUM)
472 if (toggle && but->type == OPTION)
482 uiBlockSetCurLayout(block, layout);
485 static void ui_item_enum_expand_handle(bContext *C, void *arg1, void *arg2)
487 wmWindow *win = CTX_wm_window(C);
489 if (!win->eventstate->shift) {
490 uiBut *but = (uiBut *)arg1;
491 int enum_value = GET_INT_FROM_POINTER(arg2);
493 int current_value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
494 if (!(current_value & enum_value)) {
495 current_value = enum_value;
498 current_value &= enum_value;
500 RNA_property_enum_set(&but->rnapoin, but->rnaprop, current_value);
503 static void ui_item_enum_expand(uiLayout *layout, uiBlock *block, PointerRNA *ptr, PropertyRNA *prop,
504 const char *uiname, int h, int icon_only)
507 EnumPropertyItem *item, *item_array;
509 int itemw, icon, value, free;
511 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item_array, NULL, &free);
513 /* we dont want nested rows, cols in menus */
514 if (layout->root->type != UI_LAYOUT_MENU) {
515 uiBlockSetCurLayout(block, ui_item_local_sublayout(layout, layout, 1));
518 uiBlockSetCurLayout(block, layout);
521 for (item = item_array; item->identifier; item++) {
522 if (!item->identifier[0])
525 name = (!uiname || uiname[0]) ? item->name : "";
528 itemw = ui_text_icon_width(block->curlayout, icon_only ? "" : name, icon, 0);
530 if (icon && name[0] && !icon_only)
531 but = uiDefIconTextButR_prop(block, ROW, 0, icon, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
533 but = uiDefIconButR_prop(block, ROW, 0, icon, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
535 but = uiDefButR_prop(block, ROW, 0, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
537 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
538 uiButSetFunc(but, ui_item_enum_expand_handle, but, SET_INT_IN_POINTER(value));
541 if (ui_layout_local_dir(layout) != UI_LAYOUT_HORIZONTAL)
542 but->flag |= UI_TEXT_LEFT;
544 uiBlockSetCurLayout(block, layout);
547 MEM_freeN(item_array);
551 /* callback for keymap item change button */
552 static void ui_keymap_but_cb(bContext *UNUSED(C), void *but_v, void *UNUSED(key_v))
556 RNA_boolean_set(&but->rnapoin, "shift", (but->modifier_key & KM_SHIFT) != 0);
557 RNA_boolean_set(&but->rnapoin, "ctrl", (but->modifier_key & KM_CTRL) != 0);
558 RNA_boolean_set(&but->rnapoin, "alt", (but->modifier_key & KM_ALT) != 0);
559 RNA_boolean_set(&but->rnapoin, "oskey", (but->modifier_key & KM_OSKEY) != 0);
562 /* create label + button for RNA property */
563 static uiBut *ui_item_with_label(uiLayout *layout, uiBlock *block, const char *name, int icon, PointerRNA *ptr, PropertyRNA *prop, int index, int x, int y, int w, int h, int flag)
568 PropertySubType subtype;
571 sub = uiLayoutRow(layout, layout->align);
572 uiBlockSetCurLayout(block, sub);
575 /* XXX UI_GetStringWidth is not accurate */
577 labelw = UI_GetStringWidth(name);
578 CLAMP(labelw, w / 4, 3 * w / 4);
581 uiDefBut(block, LABEL, 0, name, x, y, labelw, h, NULL, 0.0, 0.0, 0, 0, "");
585 type = RNA_property_type(prop);
586 subtype = RNA_property_subtype(prop);
588 if (subtype == PROP_FILEPATH || subtype == PROP_DIRPATH) {
589 uiBlockSetCurLayout(block, uiLayoutRow(sub, TRUE));
590 uiDefAutoButR(block, ptr, prop, index, "", icon, x, y, w - UI_UNIT_X, h);
592 /* BUTTONS_OT_file_browse calls uiFileBrowseContextProperty */
593 but = uiDefIconButO(block, BUT, subtype == PROP_DIRPATH ?
594 "BUTTONS_OT_directory_browse" :
595 "BUTTONS_OT_file_browse",
596 WM_OP_INVOKE_DEFAULT, ICON_FILESEL, x, y, UI_UNIT_X, h, NULL);
598 else if (flag & UI_ITEM_R_EVENT) {
599 uiDefButR_prop(block, KEYEVT, 0, name, x, y, w, h, ptr, prop, index, 0, 0, -1, -1, NULL);
601 else if (flag & UI_ITEM_R_FULL_EVENT) {
602 if (RNA_struct_is_a(ptr->type, &RNA_KeyMapItem)) {
605 WM_keymap_item_to_string(ptr->data, buf, sizeof(buf));
607 but = uiDefButR_prop(block, HOTKEYEVT, 0, buf, x, y, w, h, ptr, prop, 0, 0, 0, -1, -1, NULL);
608 uiButSetFunc(but, ui_keymap_but_cb, but, NULL);
609 if (flag & UI_ITEM_R_IMMEDIATE)
610 uiButSetFlag(but, UI_BUT_IMMEDIATE);
614 but = uiDefAutoButR(block, ptr, prop, index, (type == PROP_ENUM && !(flag & UI_ITEM_R_ICON_ONLY)) ? NULL : "", icon, x, y, w, h);
616 uiBlockSetCurLayout(block, layout);
620 void uiFileBrowseContextProperty(const bContext *C, PointerRNA *ptr, PropertyRNA **prop)
622 ARegion *ar = CTX_wm_region(C);
624 uiBut *but, *prevbut;
626 memset(ptr, 0, sizeof(*ptr));
632 for (block = ar->uiblocks.first; block; block = block->next) {
633 for (but = block->buttons.first; but; but = but->next) {
636 /* find the button before the active one */
637 if ((but->flag & UI_BUT_LAST_ACTIVE) && prevbut && prevbut->rnapoin.data) {
638 if (RNA_property_type(prevbut->rnaprop) == PROP_STRING) {
639 *ptr = prevbut->rnapoin;
640 *prop = prevbut->rnaprop;
648 /********************* Button Items *************************/
651 * Update a buttons tip with an enum's description if possible.
653 static void ui_but_tip_from_enum_item(uiBut *but, EnumPropertyItem *item)
655 if (but->tip == NULL || but->tip[0] == '\0') {
656 if (item->description && item->description[0]) {
657 but->tip = item->description;
663 static void ui_item_disabled(uiLayout *layout, const char *name)
665 uiBlock *block = layout->root->block;
669 uiBlockSetCurLayout(block, layout);
674 w = ui_text_icon_width(layout, name, 0, 0);
676 but = uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
677 but->flag |= UI_BUT_DISABLED;
683 PointerRNA uiItemFullO_ptr(uiLayout *layout, wmOperatorType *ot, const char *name, int icon, IDProperty *properties, int context, int flag)
685 uiBlock *block = layout->root->block;
691 name = RNA_struct_ui_name(ot->srna);
696 if (layout->root->type == UI_LAYOUT_MENU && !icon)
700 uiBlockSetCurLayout(block, layout);
702 w = ui_text_icon_width(layout, name, icon, 0);
704 if (flag & UI_ITEM_R_NO_BG)
705 uiBlockSetEmboss(block, UI_EMBOSSN);
707 /* create the button */
710 but = uiDefIconTextButO_ptr(block, BUT, ot, context, icon, name, 0, 0, w, UI_UNIT_Y, NULL);
713 but = uiDefIconButO_ptr(block, BUT, ot, context, icon, 0, 0, w, UI_UNIT_Y, NULL);
717 but = uiDefButO_ptr(block, BUT, ot, context, name, 0, 0, w, UI_UNIT_Y, NULL);
720 assert(but->optype != NULL);
722 /* text alignment for toolbar buttons */
723 if ((layout->root->type == UI_LAYOUT_TOOLBAR) && !icon)
724 but->flag |= UI_TEXT_LEFT;
726 if (flag & UI_ITEM_R_NO_BG)
727 uiBlockSetEmboss(block, UI_EMBOSS);
729 if (layout->redalert)
730 uiButSetFlag(but, UI_BUT_REDALERT);
732 /* assign properties */
733 if (properties || (flag & UI_ITEM_O_RETURN_PROPS)) {
734 PointerRNA *opptr = uiButGetOperatorPtrRNA(but);
737 opptr->data = properties;
740 IDPropertyTemplate val = {0};
741 opptr->data = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
747 return PointerRNA_NULL;
750 PointerRNA uiItemFullO(uiLayout *layout, const char *opname, const char *name, int icon, IDProperty *properties, int context, int flag)
752 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
754 UI_OPERATOR_ERROR_RET(ot, opname, return PointerRNA_NULL);
756 return uiItemFullO_ptr(layout, ot, name, icon, properties, context, flag);
759 static const char *ui_menu_enumpropname(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int retval)
761 EnumPropertyItem *item;
765 RNA_property_enum_items(layout->root->block->evil_C, ptr, prop, &item, NULL, &free);
766 if (RNA_enum_name(item, retval, &name)) {
767 name = CTX_IFACE_(RNA_property_translation_context(prop), name);
780 void uiItemEnumO_ptr(uiLayout *layout, wmOperatorType *ot, const char *name, int icon, const char *propname, int value)
785 WM_operator_properties_create_ptr(&ptr, ot);
787 if ((prop = RNA_struct_find_property(&ptr, propname))) {
791 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
795 RNA_property_enum_set(&ptr, prop, value);
798 name = ui_menu_enumpropname(layout, &ptr, prop, value);
800 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
802 void uiItemEnumO(uiLayout *layout, const char *opname, const char *name, int icon, const char *propname, int value)
804 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
807 uiItemEnumO_ptr(layout, ot, name, icon, propname, value);
810 ui_item_disabled(layout, opname);
811 RNA_warning("unknown operator '%s'", opname);
816 void uiItemsFullEnumO(uiLayout *layout, const char *opname, const char *propname, IDProperty *properties,
817 int context, int flag)
819 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
823 uiBlock *block = layout->root->block;
825 if (!ot || !ot->srna) {
826 ui_item_disabled(layout, opname);
827 RNA_warning("%s '%s'", ot ? "unknown operator" : "operator missing srna", opname);
831 WM_operator_properties_create_ptr(&ptr, ot);
832 /* so the context is passed to itemf functions (some need it) */
833 WM_operator_properties_sanitize(&ptr, false);
834 prop = RNA_struct_find_property(&ptr, propname);
836 /* don't let bad properties slip through */
837 BLI_assert((prop == NULL) || (RNA_property_type(prop) == PROP_ENUM));
839 if (prop && RNA_property_type(prop) == PROP_ENUM) {
840 EnumPropertyItem *item, *item_array = NULL;
842 uiLayout *split = uiLayoutSplit(layout, 0.0f, FALSE);
843 uiLayout *column = uiLayoutColumn(split, FALSE);
845 RNA_property_enum_items_gettexted(block->evil_C, &ptr, prop, &item_array, NULL, &free);
846 for (item = item_array; item->identifier; item++) {
847 if (item->identifier[0]) {
850 WM_operator_properties_create_ptr(&tptr, ot);
853 IDP_FreeProperty(tptr.data);
854 MEM_freeN(tptr.data);
856 tptr.data = IDP_CopyProperty(properties);
858 RNA_property_enum_set(&tptr, prop, item->value);
860 uiItemFullO_ptr(column, ot, item->name, item->icon, tptr.data, context, flag);
861 ui_but_tip_from_enum_item(block->buttons.last, item);
866 if (item != item_array) {
867 column = uiLayoutColumn(split, FALSE);
868 /* inconsistent, but menus with labels do not look good flipped */
869 block->flag |= UI_BLOCK_NO_FLIP;
872 uiItemL(column, item->name, ICON_NONE);
873 but = block->buttons.last;
874 but->flag = UI_TEXT_LEFT;
875 ui_but_tip_from_enum_item(but, item);
877 else { /* XXX bug here, colums draw bottom item badly */
883 MEM_freeN(item_array);
886 else if (prop && RNA_property_type(prop) != PROP_ENUM) {
887 RNA_warning("%s.%s, not an enum type", RNA_struct_identifier(ptr.type), propname);
891 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
896 void uiItemsEnumO(uiLayout *layout, const char *opname, const char *propname)
898 uiItemsFullEnumO(layout, opname, propname, NULL, layout->root->opcontext, 0);
901 /* for use in cases where we have */
902 void uiItemEnumO_value(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
904 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
908 UI_OPERATOR_ERROR_RET(ot, opname, return );
910 WM_operator_properties_create_ptr(&ptr, ot);
913 if ((prop = RNA_struct_find_property(&ptr, propname))) {
917 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
921 RNA_property_enum_set(&ptr, prop, value);
923 /* same as uiItemEnumO */
925 name = ui_menu_enumpropname(layout, &ptr, prop, value);
927 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
930 void uiItemEnumO_string(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, const char *value_str)
932 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
936 EnumPropertyItem *item;
939 UI_OPERATOR_ERROR_RET(ot, opname, return );
941 WM_operator_properties_create_ptr(&ptr, ot);
944 if ((prop = RNA_struct_find_property(&ptr, propname))) {
945 /* no need for translations here */
946 RNA_property_enum_items(layout->root->block->evil_C, &ptr, prop, &item, NULL, &free);
947 if (item == NULL || RNA_enum_value_from_id(item, value_str, &value) == 0) {
951 RNA_warning("%s.%s, enum %s not found", RNA_struct_identifier(ptr.type), propname, value_str);
960 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
964 RNA_property_enum_set(&ptr, prop, value);
966 /* same as uiItemEnumO */
968 name = ui_menu_enumpropname(layout, &ptr, prop, value);
970 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
973 void uiItemBooleanO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
975 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
978 UI_OPERATOR_ERROR_RET(ot, opname, return );
980 WM_operator_properties_create_ptr(&ptr, ot);
981 RNA_boolean_set(&ptr, propname, value);
983 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
986 void uiItemIntO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
988 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
991 UI_OPERATOR_ERROR_RET(ot, opname, return );
993 WM_operator_properties_create_ptr(&ptr, ot);
994 RNA_int_set(&ptr, propname, value);
996 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
999 void uiItemFloatO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, float value)
1001 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1004 UI_OPERATOR_ERROR_RET(ot, opname, return );
1006 WM_operator_properties_create_ptr(&ptr, ot);
1007 RNA_float_set(&ptr, propname, value);
1009 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1012 void uiItemStringO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, const char *value)
1014 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1017 UI_OPERATOR_ERROR_RET(ot, opname, return );
1019 WM_operator_properties_create_ptr(&ptr, ot);
1020 RNA_string_set(&ptr, propname, value);
1022 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1025 void uiItemO(uiLayout *layout, const char *name, int icon, const char *opname)
1027 uiItemFullO(layout, opname, name, icon, NULL, layout->root->opcontext, 0);
1030 /* RNA property items */
1032 static void ui_item_rna_size(uiLayout *layout, const char *name, int icon, PointerRNA *ptr, PropertyRNA *prop,
1033 int index, int icon_only, int *r_w, int *r_h)
1036 PropertySubType subtype;
1039 /* arbitrary extended width by type */
1040 type = RNA_property_type(prop);
1041 subtype = RNA_property_subtype(prop);
1042 len = RNA_property_array_length(ptr, prop);
1044 if (!name[0] && !icon_only) {
1045 if (ELEM(type, PROP_STRING, PROP_POINTER)) {
1046 name = "non-empty text";
1048 else if (type == PROP_BOOLEAN) {
1051 else if (type == PROP_ENUM) {
1052 /* Find the longest enum item name, instead of using a dummy text! */
1053 EnumPropertyItem *item, *item_array;
1056 RNA_property_enum_items_gettexted(layout->root->block->evil_C, ptr, prop, &item_array, NULL, &free);
1057 for (item = item_array; item->identifier; item++) {
1058 if (item->identifier[0]) {
1059 w = max_ii(w, ui_text_icon_width(layout, item->name, icon, 0));
1063 MEM_freeN(item_array);
1069 w = ui_text_icon_width(layout, name, icon, 0);
1072 /* increase height for arrays */
1073 if (index == RNA_NO_INDEX && len > 0) {
1074 if (!name[0] && icon == ICON_NONE)
1077 if (ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER))
1079 else if (subtype == PROP_MATRIX)
1080 h += ceil(sqrt(len)) * UI_UNIT_Y;
1082 h += len * UI_UNIT_Y;
1084 else if (ui_layout_vary_direction(layout) == UI_ITEM_VARY_X) {
1085 if (type == PROP_BOOLEAN && name[0])
1087 else if (type == PROP_ENUM)
1089 else if (type == PROP_FLOAT || type == PROP_INT)
1097 void uiItemFullR(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int index, int value, int flag, const char *name, int icon)
1099 uiBlock *block = layout->root->block;
1102 char namestr[UI_MAX_NAME_STR];
1103 int len, is_array, w, h, slider, toggle, expand, icon_only, no_bg;
1105 uiBlockSetCurLayout(block, layout);
1108 type = RNA_property_type(prop);
1109 is_array = RNA_property_array_check(prop);
1110 len = (is_array) ? RNA_property_array_length(ptr, prop) : 0;
1112 /* set name and icon */
1114 name = RNA_property_ui_name(prop);
1115 if (icon == ICON_NONE)
1116 icon = RNA_property_ui_icon(prop);
1118 if (ELEM4(type, PROP_INT, PROP_FLOAT, PROP_STRING, PROP_POINTER))
1119 name = ui_item_name_add_colon(name, namestr);
1120 else if (type == PROP_BOOLEAN && is_array && index == RNA_NO_INDEX)
1121 name = ui_item_name_add_colon(name, namestr);
1122 else if (type == PROP_ENUM && index != RNA_ENUM_VALUE)
1123 name = ui_item_name_add_colon(name, namestr);
1125 if (layout->root->type == UI_LAYOUT_MENU) {
1126 if (type == PROP_BOOLEAN && ((is_array == FALSE) || (index != RNA_NO_INDEX))) {
1127 if (is_array) icon = (RNA_property_boolean_get_index(ptr, prop, index)) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1128 else icon = (RNA_property_boolean_get(ptr, prop)) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1130 else if (type == PROP_ENUM && index == RNA_ENUM_VALUE) {
1131 int enum_value = RNA_property_enum_get(ptr, prop);
1132 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1133 icon = (enum_value & value) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1136 icon = (enum_value == value) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1141 slider = (flag & UI_ITEM_R_SLIDER);
1142 toggle = (flag & UI_ITEM_R_TOGGLE);
1143 expand = (flag & UI_ITEM_R_EXPAND);
1144 icon_only = (flag & UI_ITEM_R_ICON_ONLY);
1145 no_bg = (flag & UI_ITEM_R_NO_BG);
1148 ui_item_rna_size(layout, name, icon, ptr, prop, index, icon_only, &w, &h);
1151 uiBlockSetEmboss(block, UI_EMBOSSN);
1153 /* array property */
1154 if (index == RNA_NO_INDEX && is_array)
1155 ui_item_array(layout, block, name, icon, ptr, prop, len, 0, 0, w, h, expand, slider, toggle, icon_only);
1157 else if (type == PROP_ENUM && index == RNA_ENUM_VALUE) {
1158 if (icon && name[0] && !icon_only)
1159 uiDefIconTextButR_prop(block, ROW, 0, icon, name, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1161 uiDefIconButR_prop(block, ROW, 0, icon, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1163 uiDefButR_prop(block, ROW, 0, name, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1166 else if (type == PROP_ENUM && (expand || RNA_property_flag(prop) & PROP_ENUM_FLAG))
1167 ui_item_enum_expand(layout, block, ptr, prop, name, h, icon_only);
1168 /* property with separate label */
1169 else if (type == PROP_ENUM || type == PROP_STRING || type == PROP_POINTER) {
1170 but = ui_item_with_label(layout, block, name, icon, ptr, prop, index, 0, 0, w, h, flag);
1171 ui_but_add_search(but, ptr, prop, NULL, NULL);
1173 if (layout->redalert)
1174 uiButSetFlag(but, UI_BUT_REDALERT);
1178 but = uiDefAutoButR(block, ptr, prop, index, name, icon, 0, 0, w, h);
1180 if (slider && but->type == NUM)
1183 if (toggle && but->type == OPTION)
1186 if (layout->redalert)
1187 uiButSetFlag(but, UI_BUT_REDALERT);
1191 uiBlockSetEmboss(block, UI_EMBOSS);
1194 void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
1196 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1199 ui_item_disabled(layout, propname);
1200 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1204 uiItemFullR(layout, ptr, prop, RNA_NO_INDEX, 0, flag, name, icon);
1207 void uiItemEnumR(uiLayout *layout, const char *name, int icon, struct PointerRNA *ptr, const char *propname, int value)
1209 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1211 if (!prop || RNA_property_type(prop) != PROP_ENUM) {
1212 ui_item_disabled(layout, propname);
1213 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1217 uiItemFullR(layout, ptr, prop, RNA_ENUM_VALUE, value, 0, name, icon);
1220 void uiItemEnumR_string(uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *value, const char *name, int icon)
1222 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1223 EnumPropertyItem *item;
1224 int ivalue, a, free;
1226 if (!prop || RNA_property_type(prop) != PROP_ENUM) {
1227 ui_item_disabled(layout, propname);
1228 RNA_warning("enum property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1232 RNA_property_enum_items(layout->root->block->evil_C, ptr, prop, &item, NULL, &free);
1234 if (!RNA_enum_value_from_id(item, value, &ivalue)) {
1238 ui_item_disabled(layout, propname);
1239 RNA_warning("enum property value not found: %s", value);
1243 for (a = 0; item[a].identifier; a++) {
1244 if (item[a].value == ivalue) {
1245 const char *item_name = CTX_IFACE_(RNA_property_translation_context(prop), item[a].name);
1247 uiItemFullR(layout, ptr, prop, RNA_ENUM_VALUE, ivalue, 0, item_name ? item_name : name, icon ? icon : item[a].icon);
1257 void uiItemsEnumR(uiLayout *layout, struct PointerRNA *ptr, const char *propname)
1260 uiBlock *block = layout->root->block;
1263 prop = RNA_struct_find_property(ptr, propname);
1266 ui_item_disabled(layout, propname);
1267 RNA_warning("enum property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1271 if (RNA_property_type(prop) != PROP_ENUM) {
1272 RNA_warning("not an enum property: %s.%s", RNA_struct_identifier(ptr->type), propname);
1276 EnumPropertyItem *item;
1277 int totitem, i, free;
1278 uiLayout *split = uiLayoutSplit(layout, 0.0f, FALSE);
1279 uiLayout *column = uiLayoutColumn(split, FALSE);
1281 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item, &totitem, &free);
1283 for (i = 0; i < totitem; i++) {
1284 if (item[i].identifier[0]) {
1285 uiItemEnumR(column, item[i].name, ICON_NONE, ptr, propname, item[i].value);
1286 ui_but_tip_from_enum_item(block->buttons.last, &item[i]);
1291 column = uiLayoutColumn(split, FALSE);
1292 /* inconsistent, but menus with labels do not look good flipped */
1293 block->flag |= UI_BLOCK_NO_FLIP;
1296 uiItemL(column, item[i].name, ICON_NONE);
1297 bt = block->buttons.last;
1298 bt->flag = UI_TEXT_LEFT;
1300 ui_but_tip_from_enum_item(bt, &item[i]);
1313 /* Pointer RNA button with search */
1315 typedef struct CollItemSearch {
1316 struct CollItemSearch *next, *prev;
1322 static int sort_search_items_list(void *a, void *b)
1324 CollItemSearch *cis1 = (CollItemSearch *)a;
1325 CollItemSearch *cis2 = (CollItemSearch *)b;
1327 if (BLI_strcasecmp(cis1->name, cis2->name) > 0)
1333 static void rna_search_cb(const struct bContext *C, void *arg_but, const char *str, uiSearchItems *items)
1335 uiBut *but = arg_but;
1337 int i = 0, iconid = 0, flag = RNA_property_flag(but->rnaprop);
1338 ListBase *items_list = MEM_callocN(sizeof(ListBase), "items_list");
1339 CollItemSearch *cis;
1340 const int skip_filter = !but->changed;
1342 /* build a temporary list of relevant items first */
1343 RNA_PROP_BEGIN (&but->rnasearchpoin, itemptr, but->rnasearchprop)
1345 if (flag & PROP_ID_SELF_CHECK)
1346 if (itemptr.data == but->rnapoin.id.data)
1350 if (RNA_property_type(but->rnaprop) == PROP_POINTER) {
1351 if (RNA_property_pointer_poll(&but->rnapoin, but->rnaprop, &itemptr) == 0)
1355 if (itemptr.type && RNA_struct_is_ID(itemptr.type)) {
1356 ID *id = itemptr.data;
1357 char name_ui[MAX_ID_NAME];
1359 #if 0 /* this name is used for a string comparison and can't be modified, TODO */
1360 /* if ever enabled, make name_ui be MAX_ID_NAME+1 */
1361 name_uiprefix_id(name_ui, id);
1363 BLI_strncpy(name_ui, id->name + 2, sizeof(name_ui));
1365 name = BLI_strdup(name_ui);
1366 iconid = ui_id_icon_get((bContext *)C, id, false);
1369 name = RNA_struct_name_get_alloc(&itemptr, NULL, 0, NULL); /* could use the string length here */
1374 if (skip_filter || BLI_strcasestr(name, str)) {
1375 cis = MEM_callocN(sizeof(CollItemSearch), "CollectionItemSearch");
1376 cis->name = MEM_dupallocN(name);
1378 cis->iconid = iconid;
1379 BLI_addtail(items_list, cis);
1388 BLI_sortlist(items_list, sort_search_items_list);
1390 /* add search items from temporary list */
1391 for (cis = items_list->first; cis; cis = cis->next) {
1392 if (false == uiSearchItemAdd(items, cis->name, SET_INT_IN_POINTER(cis->index), cis->iconid)) {
1397 for (cis = items_list->first; cis; cis = cis->next) {
1398 MEM_freeN(cis->name);
1400 BLI_freelistN(items_list);
1401 MEM_freeN(items_list);
1404 static void search_id_collection(StructRNA *ptype, PointerRNA *ptr, PropertyRNA **prop)
1408 /* look for collection property in Main */
1409 RNA_main_pointer_create(G.main, ptr);
1413 RNA_STRUCT_BEGIN (ptr, iprop)
1415 /* if it's a collection and has same pointer type, we've got it */
1416 if (RNA_property_type(iprop) == PROP_COLLECTION) {
1417 srna = RNA_property_pointer_type(ptr, iprop);
1419 if (ptype == srna) {
1428 void ui_but_add_search(uiBut *but, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop)
1433 /* for ID's we do automatic lookup */
1435 if (RNA_property_type(prop) == PROP_POINTER) {
1436 ptype = RNA_property_pointer_type(ptr, prop);
1437 search_id_collection(ptype, &sptr, &searchprop);
1442 /* turn button into search button */
1444 but->type = RNA_property_is_unlink(prop) ? SEARCH_MENU_UNLINK : SEARCH_MENU;
1445 but->hardmax = MAX2(but->hardmax, 256.0f);
1446 but->rnasearchpoin = *searchptr;
1447 but->rnasearchprop = searchprop;
1448 but->flag |= UI_ICON_LEFT | UI_TEXT_LEFT;
1450 if (RNA_property_type(prop) == PROP_ENUM) {
1451 /* XXX, this will have a menu string,
1452 * but in this case we just want the text */
1456 uiButSetSearchFunc(but, rna_search_cb, but, NULL, NULL);
1460 void uiItemPointerR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, struct PointerRNA *searchptr, const char *searchpropname, const char *name, int icon)
1462 PropertyRNA *prop, *searchprop;
1466 StructRNA *icontype;
1468 char namestr[UI_MAX_NAME_STR];
1470 /* validate arguments */
1471 prop = RNA_struct_find_property(ptr, propname);
1474 RNA_warning("property not found: %s.%s",
1475 RNA_struct_identifier(ptr->type), propname);
1479 type = RNA_property_type(prop);
1480 if (!ELEM3(type, PROP_POINTER, PROP_STRING, PROP_ENUM)) {
1481 RNA_warning("Property %s must be a pointer, string or enum", propname);
1485 searchprop = RNA_struct_find_property(searchptr, searchpropname);
1489 RNA_warning("search collection property not found: %s.%s",
1490 RNA_struct_identifier(searchptr->type), searchpropname);
1493 else if (RNA_property_type(searchprop) != PROP_COLLECTION) {
1494 RNA_warning("search collection property is not a collection type: %s.%s",
1495 RNA_struct_identifier(searchptr->type), searchpropname);
1499 /* get icon & name */
1500 if (icon == ICON_NONE) {
1501 if (type == PROP_POINTER)
1502 icontype = RNA_property_pointer_type(ptr, prop);
1504 icontype = RNA_property_pointer_type(searchptr, searchprop);
1506 icon = RNA_struct_ui_icon(icontype);
1509 name = RNA_property_ui_name(prop);
1511 name = ui_item_name_add_colon(name, namestr);
1514 block = uiLayoutGetBlock(layout);
1516 ui_item_rna_size(layout, name, icon, ptr, prop, 0, 0, &w, &h);
1517 w += UI_UNIT_X; /* X icon needs more space */
1518 but = ui_item_with_label(layout, block, name, icon, ptr, prop, 0, 0, 0, w, h, 0);
1520 ui_but_add_search(but, ptr, prop, searchptr, searchprop);
1524 static void ui_item_menutype_func(bContext *C, uiLayout *layout, void *arg_mt)
1526 MenuType *mt = (MenuType *)arg_mt;
1530 menu.layout = layout;
1532 if (G.debug & G_DEBUG_WM) {
1533 printf("%s: opening menu \"%s\"\n", __func__, mt->idname);
1536 if (layout->context)
1537 CTX_store_set(C, layout->context);
1541 if (layout->context)
1542 CTX_store_set(C, NULL);
1545 static void ui_item_menu(uiLayout *layout, const char *name, int icon, uiMenuCreateFunc func, void *arg, void *argN,
1546 const char *tip, bool force_menu)
1548 uiBlock *block = layout->root->block;
1552 uiBlockSetCurLayout(block, layout);
1554 if (layout->root->type == UI_LAYOUT_HEADER)
1555 uiBlockSetEmboss(block, UI_EMBOSS);
1559 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1562 w = ui_text_icon_width(layout, name, icon, 1);
1565 if (layout->root->type == UI_LAYOUT_HEADER) { /* ugly .. */
1572 if (name[0] && icon)
1573 but = uiDefIconTextMenuBut(block, func, arg, icon, name, 0, 0, w, h, tip);
1575 but = uiDefIconMenuBut(block, func, arg, icon, 0, 0, w, h, tip);
1577 but = uiDefMenuBut(block, func, arg, name, 0, 0, w, h, tip);
1579 if (argN) { /* ugly .. */
1580 but->poin = (char *)but;
1581 but->func_argN = argN;
1584 if (layout->root->type == UI_LAYOUT_HEADER) {
1585 uiBlockSetEmboss(block, UI_EMBOSS);
1587 if (ELEM(layout->root->type, UI_LAYOUT_PANEL, UI_LAYOUT_TOOLBAR) ||
1588 (force_menu && layout->root->type != UI_LAYOUT_MENU)) /* We never want a dropdown in menu! */
1591 but->flag |= UI_TEXT_LEFT;
1595 void uiItemM(uiLayout *layout, bContext *UNUSED(C), const char *menuname, const char *name, int icon)
1599 mt = WM_menutype_find(menuname, FALSE);
1602 RNA_warning("not found %s", menuname);
1607 name = CTX_IFACE_(mt->translation_context, mt->label);
1610 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1613 ui_item_menu(layout, name, icon, ui_item_menutype_func, mt, NULL, TIP_(mt->description), false);
1617 static uiBut *uiItemL_(uiLayout *layout, const char *name, int icon)
1619 uiBlock *block = layout->root->block;
1623 uiBlockSetCurLayout(block, layout);
1627 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1630 w = ui_text_icon_width(layout, name, icon, 0);
1632 if (icon && name[0])
1633 but = uiDefIconTextBut(block, LABEL, 0, icon, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1635 but = uiDefIconBut(block, LABEL, 0, icon, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1637 but = uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1639 /* to compensate for string size padding in ui_text_icon_width,
1640 * make text aligned right if the layout is aligned right.
1642 if (uiLayoutGetAlignment(layout) == UI_LAYOUT_ALIGN_RIGHT) {
1643 but->flag &= ~UI_TEXT_LEFT; /* default, needs to be unset */
1644 but->flag |= UI_TEXT_RIGHT;
1647 /* Mark as a label inside a listbox. */
1648 if (block->flag & UI_BLOCK_LIST_ITEM) {
1649 but->type = LISTLABEL;
1655 void uiItemL(uiLayout *layout, const char *name, int icon)
1657 uiItemL_(layout, name, icon);
1660 void uiItemLDrag(uiLayout *layout, PointerRNA *ptr, const char *name, int icon)
1662 uiBut *but = uiItemL_(layout, name, icon);
1664 if (ptr && ptr->type)
1665 if (RNA_struct_is_ID(ptr->type))
1666 uiButSetDragID(but, ptr->id.data);
1671 void uiItemV(uiLayout *layout, const char *name, int icon, int argval)
1674 uiBlock *block = layout->root->block;
1675 float *retvalue = (block->handle) ? &block->handle->retvalue : NULL;
1678 uiBlockSetCurLayout(block, layout);
1682 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1685 w = ui_text_icon_width(layout, name, icon, 0);
1687 if (icon && name[0])
1688 uiDefIconTextButF(block, BUT, argval, icon, name, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1690 uiDefIconButF(block, BUT, argval, icon, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1692 uiDefButF(block, BUT, argval, name, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1695 /* separator item */
1696 void uiItemS(uiLayout *layout)
1698 uiBlock *block = layout->root->block;
1700 uiBlockSetCurLayout(block, layout);
1701 uiDefBut(block, SEPR, 0, "", 0, 0, 0.3f * UI_UNIT_X, 0.3f * UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1705 void uiItemMenuF(uiLayout *layout, const char *name, int icon, uiMenuCreateFunc func, void *arg)
1710 ui_item_menu(layout, name, icon, func, arg, NULL, "", false);
1713 typedef struct MenuItemLevel {
1715 /* don't use pointers to the strings because python can dynamically
1716 * allocate strings and free before the menu draws, see [#27304] */
1717 char opname[OP_MAX_TYPENAME];
1718 char propname[MAX_IDPROP_NAME];
1722 static void menu_item_enum_opname_menu(bContext *UNUSED(C), uiLayout *layout, void *arg)
1724 MenuItemLevel *lvl = (MenuItemLevel *)(((uiBut *)arg)->func_argN);
1726 uiLayoutSetOperatorContext(layout, lvl->opcontext);
1727 uiItemsEnumO(layout, lvl->opname, lvl->propname);
1730 void uiItemMenuEnumO(uiLayout *layout, bContext *C, const char *opname, const char *propname, const char *name, int icon)
1732 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1734 char namestr_buf[UI_MAX_NAME_STR], keybuf[128];
1735 char *namestr = namestr_buf;
1737 UI_OPERATOR_ERROR_RET(ot, opname, return );
1740 ui_item_disabled(layout, opname);
1741 RNA_warning("operator missing srna '%s'", opname);
1746 namestr += BLI_strncpy_rlen(namestr, name, sizeof(namestr_buf));
1748 namestr += BLI_strncpy_rlen(namestr, RNA_struct_ui_name(ot->srna), sizeof(namestr_buf));
1750 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1753 lvl = MEM_callocN(sizeof(MenuItemLevel), "MenuItemLevel");
1754 BLI_strncpy(lvl->opname, opname, sizeof(lvl->opname));
1755 BLI_strncpy(lvl->propname, propname, sizeof(lvl->propname));
1756 lvl->opcontext = layout->root->opcontext;
1758 /* add hotkey here, lower UI code can't detect it */
1759 if (layout->root->block->flag & UI_BLOCK_LOOP) {
1760 if (ot->prop && ot->invoke &&
1761 WM_key_event_operator_string(C, ot->idname, layout->root->opcontext, NULL, false, keybuf, sizeof(keybuf)))
1763 namestr += BLI_snprintf(namestr, sizeof(namestr_buf) - (namestr - namestr_buf), "|%s", keybuf);
1767 ui_item_menu(layout, namestr_buf, icon, menu_item_enum_opname_menu, NULL, lvl, RNA_struct_ui_description(ot->srna),
1771 static void menu_item_enum_rna_menu(bContext *UNUSED(C), uiLayout *layout, void *arg)
1773 MenuItemLevel *lvl = (MenuItemLevel *)(((uiBut *)arg)->func_argN);
1775 uiLayoutSetOperatorContext(layout, lvl->opcontext);
1776 uiItemsEnumR(layout, &lvl->rnapoin, lvl->propname);
1779 void uiItemMenuEnumR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *name, int icon)
1784 prop = RNA_struct_find_property(ptr, propname);
1786 ui_item_disabled(layout, propname);
1787 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1792 name = RNA_property_ui_name(prop);
1793 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1796 lvl = MEM_callocN(sizeof(MenuItemLevel), "MenuItemLevel");
1797 lvl->rnapoin = *ptr;
1798 BLI_strncpy(lvl->propname, propname, sizeof(lvl->propname));
1799 lvl->opcontext = layout->root->opcontext;
1801 ui_item_menu(layout, name, icon, menu_item_enum_rna_menu, NULL, lvl, RNA_property_description(prop), false);
1804 /**************************** Layout Items ***************************/
1806 /* single-row layout */
1807 static void ui_litem_estimate_row(uiLayout *litem)
1815 for (item = litem->items.first; item; item = item->next) {
1816 ui_item_size(item, &itemw, &itemh);
1819 litem->h = MAX2(itemh, litem->h);
1822 litem->w += litem->space;
1826 static int ui_litem_min_width(int itemw)
1828 return MIN2(2 * UI_UNIT_X, itemw);
1831 static void ui_litem_layout_row(uiLayout *litem)
1834 int x, y, w, tot, totw, neww, itemw, minw, itemh, offset;
1835 int fixedw, freew, fixedx, freex, flag = 0, lastw = 0;
1837 /* x = litem->x; */ /* UNUSED */
1843 for (item = litem->items.first; item; item = item->next) {
1844 ui_item_size(item, &itemw, &itemh);
1853 w -= (tot - 1) * litem->space;
1856 /* keep clamping items to fixed minimum size until all are done */
1862 for (item = litem->items.first; item; item = item->next) {
1866 ui_item_size(item, &itemw, &itemh);
1867 minw = ui_litem_min_width(itemw);
1870 neww = ui_item_fit(itemw, x, totw, w - lastw, !item->next, litem->alignment, NULL);
1872 neww = 0; /* no space left, all will need clamping to minimum size */
1876 if ((neww < minw || itemw == minw) && w != 0) {
1884 /* keep free size */
1897 for (item = litem->items.first; item; item = item->next) {
1898 ui_item_size(item, &itemw, &itemh);
1899 minw = ui_litem_min_width(itemw);
1902 /* fixed minimum size items */
1903 itemw = ui_item_fit(minw, fixedx, fixedw, min_ii(w, fixedw), !item->next, litem->alignment, NULL);
1907 /* free size item */
1908 itemw = ui_item_fit(itemw, freex, freew, w - fixedw, !item->next, litem->alignment, NULL);
1912 /* align right/center */
1914 if (litem->alignment == UI_LAYOUT_ALIGN_RIGHT) {
1915 if (freew + fixedw > 0 && freew + fixedw < w)
1916 offset = w - (fixedw + freew);
1918 else if (litem->alignment == UI_LAYOUT_ALIGN_CENTER) {
1919 if (freew + fixedw > 0 && freew + fixedw < w)
1920 offset = (w - (fixedw + freew)) / 2;
1924 ui_item_position(item, x + offset, y - itemh, itemw, itemh);
1931 litem->w = x - litem->x;
1932 litem->h = litem->y - y;
1937 /* single-column layout */
1938 static void ui_litem_estimate_column(uiLayout *litem)
1946 for (item = litem->items.first; item; item = item->next) {
1947 ui_item_size(item, &itemw, &itemh);
1949 litem->w = MAX2(litem->w, itemw);
1953 litem->h += litem->space;
1957 static void ui_litem_layout_column(uiLayout *litem)
1965 for (item = litem->items.first; item; item = item->next) {
1966 ui_item_size(item, NULL, &itemh);
1969 ui_item_position(item, x, y, litem->w, itemh);
1975 litem->h = litem->y - y;
1981 static void ui_litem_estimate_root(uiLayout *UNUSED(litem))
1986 static void ui_litem_layout_root(uiLayout *litem)
1988 if (litem->root->type == UI_LAYOUT_HEADER)
1989 ui_litem_layout_row(litem);
1991 ui_litem_layout_column(litem);
1995 static void ui_litem_estimate_box(uiLayout *litem)
1997 uiStyle *style = litem->root->style;
1999 ui_litem_estimate_column(litem);
2000 litem->w += 2 * style->boxspace;
2001 litem->h += style->boxspace;
2004 static void ui_litem_layout_box(uiLayout *litem)
2006 uiLayoutItemBx *box = (uiLayoutItemBx *)litem;
2007 uiStyle *style = litem->root->style;
2014 litem->x += style->boxspace;
2016 if (w != 0) litem->w -= 2 * style->boxspace;
2017 if (h != 0) litem->h -= 2 * style->boxspace;
2019 ui_litem_layout_column(litem);
2021 litem->x -= style->boxspace;
2022 litem->y -= style->boxspace;
2024 if (w != 0) litem->w += 2 * style->boxspace;
2025 if (h != 0) litem->h += style->boxspace;
2027 /* roundbox around the sublayout */
2028 but = box->roundbox;
2029 but->rect.xmin = litem->x;
2030 but->rect.ymin = litem->y;
2031 but->rect.xmax = litem->x + litem->w;
2032 but->rect.ymax = litem->y + litem->h;
2035 /* multi-column layout, automatically flowing to the next */
2036 static void ui_litem_estimate_column_flow(uiLayout *litem)
2038 uiStyle *style = litem->root->style;
2039 uiLayoutItemFlow *flow = (uiLayoutItemFlow *)litem;
2041 int col, x, y, emh, emy, miny, itemw, itemh, maxw = 0;
2044 /* compute max needed width and total height */
2047 for (item = litem->items.first; item; item = item->next) {
2048 ui_item_size(item, &itemw, &itemh);
2049 maxw = MAX2(maxw, itemw);
2054 if (flow->number <= 0) {
2055 /* auto compute number of columns, not very good */
2061 flow->totcol = max_ii(litem->root->emw / maxw, 1);
2062 flow->totcol = min_ii(flow->totcol, totitem);
2065 flow->totcol = flow->number;
2074 emh = toth / flow->totcol;
2076 /* create column per column */
2078 for (item = litem->items.first; item; item = item->next) {
2079 ui_item_size(item, &itemw, &itemh);
2081 y -= itemh + style->buttonspacey;
2082 miny = min_ii(miny, y);
2084 maxw = max_ii(itemw, maxw);
2086 /* decide to go to next one */
2087 if (col < flow->totcol - 1 && emy <= -emh) {
2088 x += maxw + litem->space;
2091 emy = 0; /* need to reset height again for next column */
2097 litem->h = litem->y - miny;
2100 static void ui_litem_layout_column_flow(uiLayout *litem)
2102 uiStyle *style = litem->root->style;
2103 uiLayoutItemFlow *flow = (uiLayoutItemFlow *)litem;
2105 int col, x, y, w, emh, emy, miny, itemw, itemh;
2106 int toth, totitem, offset;
2108 /* compute max needed width and total height */
2111 for (item = litem->items.first; item; item = item->next) {
2112 ui_item_size(item, &itemw, &itemh);
2123 w = litem->w - (flow->totcol - 1) * style->columnspace;
2124 emh = toth / flow->totcol;
2126 /* create column per column */
2128 for (item = litem->items.first; item; item = item->next) {
2129 ui_item_size(item, NULL, &itemh);
2130 itemw = ui_item_fit(1, x - litem->x, flow->totcol, w, col == flow->totcol - 1, litem->alignment, &offset);
2134 ui_item_position(item, x + offset, y, itemw, itemh);
2135 y -= style->buttonspacey;
2136 miny = min_ii(miny, y);
2138 /* decide to go to next one */
2139 if (col < flow->totcol - 1 && emy <= -emh) {
2140 x += itemw + style->columnspace;
2142 emy = 0; /* need to reset height again for next column */
2147 litem->h = litem->y - miny;
2153 static void ui_litem_estimate_absolute(uiLayout *litem)
2156 int itemx, itemy, itemw, itemh, minx, miny;
2163 for (item = litem->items.first; item; item = item->next) {
2164 ui_item_offset(item, &itemx, &itemy);
2165 ui_item_size(item, &itemw, &itemh);
2167 minx = min_ii(minx, itemx);
2168 miny = min_ii(miny, itemy);
2170 litem->w = MAX2(litem->w, itemx + itemw);
2171 litem->h = MAX2(litem->h, itemy + itemh);
2178 static void ui_litem_layout_absolute(uiLayout *litem)
2181 float scalex = 1.0f, scaley = 1.0f;
2182 int x, y, newx, newy, itemx, itemy, itemh, itemw, minx, miny, totw, toth;
2189 for (item = litem->items.first; item; item = item->next) {
2190 ui_item_offset(item, &itemx, &itemy);
2191 ui_item_size(item, &itemw, &itemh);
2193 minx = min_ii(minx, itemx);
2194 miny = min_ii(miny, itemy);
2196 totw = max_ii(totw, itemx + itemw);
2197 toth = max_ii(toth, itemy + itemh);
2203 if (litem->w && totw > 0)
2204 scalex = (float)litem->w / (float)totw;
2205 if (litem->h && toth > 0)
2206 scaley = (float)litem->h / (float)toth;
2209 y = litem->y - scaley * toth;
2211 for (item = litem->items.first; item; item = item->next) {
2212 ui_item_offset(item, &itemx, &itemy);
2213 ui_item_size(item, &itemw, &itemh);
2215 if (scalex != 1.0f) {
2216 newx = (itemx - minx) * scalex;
2217 itemw = (itemx - minx + itemw) * scalex - newx;
2218 itemx = minx + newx;
2221 if (scaley != 1.0f) {
2222 newy = (itemy - miny) * scaley;
2223 itemh = (itemy - miny + itemh) * scaley - newy;
2224 itemy = miny + newy;
2227 ui_item_position(item, x + itemx - minx, y + itemy - miny, itemw, itemh);
2230 litem->w = scalex * totw;
2231 litem->h = litem->y - y;
2232 litem->x = x + litem->w;
2237 static void ui_litem_estimate_split(uiLayout *litem)
2239 ui_litem_estimate_row(litem);
2242 static void ui_litem_layout_split(uiLayout *litem)
2244 uiLayoutItemSplit *split = (uiLayoutItemSplit *)litem;
2247 const int tot = BLI_countlist(&litem->items);
2248 int itemh, x, y, w, colw = 0;
2256 percentage = (split->percentage == 0.0f) ? 1.0f / (float)tot : split->percentage;
2258 w = (litem->w - (tot - 1) * litem->space);
2259 colw = w * percentage;
2260 colw = MAX2(colw, 0);
2262 for (item = litem->items.first; item; item = item->next) {
2263 ui_item_size(item, NULL, &itemh);
2265 ui_item_position(item, x, y - itemh, colw, itemh);
2269 colw = (w - (int)(w * percentage)) / (tot - 1);
2270 colw = MAX2(colw, 0);
2276 litem->w = x - litem->x;
2277 litem->h = litem->y - y;
2282 /* overlap layout */
2283 static void ui_litem_estimate_overlap(uiLayout *litem)
2291 for (item = litem->items.first; item; item = item->next) {
2292 ui_item_size(item, &itemw, &itemh);
2294 litem->w = MAX2(itemw, litem->w);
2295 litem->h = MAX2(itemh, litem->h);
2299 static void ui_litem_layout_overlap(uiLayout *litem)
2302 int itemw, itemh, x, y;
2307 for (item = litem->items.first; item; item = item->next) {
2308 ui_item_size(item, &itemw, &itemh);
2309 ui_item_position(item, x, y - itemh, litem->w, itemh);
2311 litem->h = MAX2(litem->h, itemh);
2315 litem->y = y - litem->h;
2318 /* layout create functions */
2319 uiLayout *uiLayoutRow(uiLayout *layout, int align)
2323 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutRow");
2324 litem->item.type = ITEM_LAYOUT_ROW;
2325 litem->root = layout->root;
2326 litem->align = align;
2327 litem->active = true;
2328 litem->enabled = true;
2329 litem->context = layout->context;
2330 litem->space = (align) ? 0 : layout->root->style->buttonspacex;
2331 litem->redalert = layout->redalert;
2332 litem->w = layout->w;
2333 BLI_addtail(&layout->items, litem);
2335 uiBlockSetCurLayout(layout->root->block, litem);
2340 uiLayout *uiLayoutColumn(uiLayout *layout, int align)
2344 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutColumn");
2345 litem->item.type = ITEM_LAYOUT_COLUMN;
2346 litem->root = layout->root;
2347 litem->align = align;
2348 litem->active = true;
2349 litem->enabled = true;
2350 litem->context = layout->context;
2351 litem->space = (litem->align) ? 0 : layout->root->style->buttonspacey;
2352 litem->redalert = layout->redalert;
2353 litem->w = layout->w;
2354 BLI_addtail(&layout->items, litem);
2356 uiBlockSetCurLayout(layout->root->block, litem);
2361 uiLayout *uiLayoutColumnFlow(uiLayout *layout, int number, int align)
2363 uiLayoutItemFlow *flow;
2365 flow = MEM_callocN(sizeof(uiLayoutItemFlow), "uiLayoutItemFlow");
2366 flow->litem.item.type = ITEM_LAYOUT_COLUMN_FLOW;
2367 flow->litem.root = layout->root;
2368 flow->litem.align = align;
2369 flow->litem.active = true;
2370 flow->litem.enabled = true;
2371 flow->litem.context = layout->context;
2372 flow->litem.space = (flow->litem.align) ? 0 : layout->root->style->columnspace;
2373 flow->litem.redalert = layout->redalert;
2374 flow->litem.w = layout->w;
2375 flow->number = number;
2376 BLI_addtail(&layout->items, flow);
2378 uiBlockSetCurLayout(layout->root->block, &flow->litem);
2380 return &flow->litem;
2383 static uiLayoutItemBx *ui_layout_box(uiLayout *layout, int type)
2385 uiLayoutItemBx *box;
2387 box = MEM_callocN(sizeof(uiLayoutItemBx), "uiLayoutItemBx");
2388 box->litem.item.type = ITEM_LAYOUT_BOX;
2389 box->litem.root = layout->root;
2390 box->litem.active = 1;
2391 box->litem.enabled = 1;
2392 box->litem.context = layout->context;
2393 box->litem.space = layout->root->style->columnspace;
2394 box->litem.redalert = layout->redalert;
2395 box->litem.w = layout->w;
2396 BLI_addtail(&layout->items, box);
2398 uiBlockSetCurLayout(layout->root->block, &box->litem);
2400 box->roundbox = uiDefBut(layout->root->block, type, 0, "", 0, 0, 0, 0, NULL, 0.0, 0.0, 0, 0, "");
2405 uiLayout *uiLayoutBox(uiLayout *layout)
2407 return (uiLayout *)ui_layout_box(layout, ROUNDBOX);
2410 /* Check all buttons defined in this layout, and set labels as active/selected.
2411 * Needed to handle correctly text colors of list items. */
2412 void ui_layout_list_set_labels_active(uiLayout *layout)
2414 uiButtonItem *bitem;
2415 for (bitem = layout->items.first; bitem; bitem = bitem->item.next) {
2416 if (bitem->item.type == ITEM_BUTTON && bitem->but->type == LISTLABEL) {
2417 uiButSetFlag(bitem->but, UI_SELECT);
2422 uiLayout *uiLayoutListBox(uiLayout *layout, uiList *ui_list, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *actptr,
2423 PropertyRNA *actprop)
2425 uiLayoutItemBx *box = ui_layout_box(layout, LISTBOX);
2426 uiBut *but = box->roundbox;
2428 but->custom_data = ui_list;
2430 but->rnasearchpoin = *ptr;
2431 but->rnasearchprop = prop;
2432 but->rnapoin = *actptr;
2433 but->rnaprop = actprop;
2435 /* Resizing data. */
2436 /* Note: we can't use usual "num button" value handling, as it only tries rnapoin when it is non-NULL... :/
2437 * So just setting but->poin, not but->pointype.
2439 but->poin = (void *)&ui_list->list_grip;
2440 but->hardmin = but->softmin = 0.0f;
2441 but->hardmax = but->softmax = 1000.0f; /* Should be more than enough! */
2444 /* only for the undo string */
2445 if (but->flag & UI_BUT_UNDO) {
2446 but->tip = RNA_property_description(actprop);
2449 return (uiLayout *)box;
2452 uiLayout *uiLayoutAbsolute(uiLayout *layout, int align)
2456 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutAbsolute");
2457 litem->item.type = ITEM_LAYOUT_ABSOLUTE;
2458 litem->root = layout->root;
2459 litem->align = align;
2462 litem->context = layout->context;
2463 litem->redalert = layout->redalert;
2464 BLI_addtail(&layout->items, litem);
2466 uiBlockSetCurLayout(layout->root->block, litem);
2471 uiBlock *uiLayoutAbsoluteBlock(uiLayout *layout)
2475 block = uiLayoutGetBlock(layout);
2476 uiLayoutAbsolute(layout, FALSE);
2481 uiLayout *uiLayoutOverlap(uiLayout *layout)
2485 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutOverlap");
2486 litem->item.type = ITEM_LAYOUT_OVERLAP;
2487 litem->root = layout->root;
2488 litem->active = true;
2489 litem->enabled = true;
2490 litem->context = layout->context;
2491 litem->redalert = layout->redalert;
2492 BLI_addtail(&layout->items, litem);
2494 uiBlockSetCurLayout(layout->root->block, litem);
2499 uiLayout *uiLayoutSplit(uiLayout *layout, float percentage, int align)
2501 uiLayoutItemSplit *split;
2503 split = MEM_callocN(sizeof(uiLayoutItemSplit), "uiLayoutItemSplit");
2504 split->litem.item.type = ITEM_LAYOUT_SPLIT;
2505 split->litem.root = layout->root;
2506 split->litem.align = align;
2507 split->litem.active = true;
2508 split->litem.enabled = true;
2509 split->litem.context = layout->context;
2510 split->litem.space = layout->root->style->columnspace;
2511 split->litem.redalert = layout->redalert;
2512 split->litem.w = layout->w;
2513 split->percentage = percentage;
2514 BLI_addtail(&layout->items, split);
2516 uiBlockSetCurLayout(layout->root->block, &split->litem);
2518 return &split->litem;
2521 void uiLayoutSetActive(uiLayout *layout, bool active)
2523 layout->active = active;
2526 void uiLayoutSetEnabled(uiLayout *layout, bool enabled)
2528 layout->enabled = enabled;
2531 void uiLayoutSetRedAlert(uiLayout *layout, bool redalert)
2533 layout->redalert = redalert;
2536 void uiLayoutSetKeepAspect(uiLayout *layout, bool keepaspect)
2538 layout->keepaspect = keepaspect;
2541 void uiLayoutSetAlignment(uiLayout *layout, char alignment)
2543 layout->alignment = alignment;
2546 void uiLayoutSetScaleX(uiLayout *layout, float scale)
2548 layout->scale[0] = scale;
2551 void uiLayoutSetScaleY(uiLayout *layout, float scale)
2553 layout->scale[1] = scale;
2556 bool uiLayoutGetActive(uiLayout *layout)
2558 return layout->active;
2561 bool uiLayoutGetEnabled(uiLayout *layout)
2563 return layout->enabled;
2566 bool uiLayoutGetRedAlert(uiLayout *layout)
2568 return layout->redalert;
2571 bool uiLayoutGetKeepAspect(uiLayout *layout)
2573 return layout->keepaspect;
2576 int uiLayoutGetAlignment(uiLayout *layout)
2578 return layout->alignment;
2581 int uiLayoutGetWidth(uiLayout *layout)
2586 float uiLayoutGetScaleX(uiLayout *layout)
2588 return layout->scale[0];
2591 float uiLayoutGetScaleY(uiLayout *layout)
2593 return layout->scale[1];
2596 /********************** Layout *******************/
2598 static void ui_item_scale(uiLayout *litem, const float scale[2])
2603 for (item = litem->items.last; item; item = item->prev) {
2604 ui_item_size(item, &w, &h);
2605 ui_item_offset(item, &x, &y);
2607 if (scale[0] != 0.0f) {
2612 if (scale[1] != 0.0f) {
2617 ui_item_position(item, x, y, w, h);
2621 static void ui_item_estimate(uiItem *item)
2625 if (item->type != ITEM_BUTTON) {
2626 uiLayout *litem = (uiLayout *)item;
2628 for (subitem = litem->items.first; subitem; subitem = subitem->next)
2629 ui_item_estimate(subitem);
2631 if (litem->items.first == NULL)
2634 if (litem->scale[0] != 0.0f || litem->scale[1] != 0.0f)
2635 ui_item_scale(litem, litem->scale);
2637 switch (litem->item.type) {
2638 case ITEM_LAYOUT_COLUMN:
2639 ui_litem_estimate_column(litem);
2641 case ITEM_LAYOUT_COLUMN_FLOW:
2642 ui_litem_estimate_column_flow(litem);
2644 case ITEM_LAYOUT_ROW:
2645 ui_litem_estimate_row(litem);
2647 case ITEM_LAYOUT_BOX:
2648 ui_litem_estimate_box(litem);
2650 case ITEM_LAYOUT_ROOT:
2651 ui_litem_estimate_root(litem);
2653 case ITEM_LAYOUT_ABSOLUTE:
2654 ui_litem_estimate_absolute(litem);
2656 case ITEM_LAYOUT_SPLIT:
2657 ui_litem_estimate_split(litem);
2659 case ITEM_LAYOUT_OVERLAP:
2660 ui_litem_estimate_overlap(litem);
2668 static void ui_item_align(uiLayout *litem, short nr)
2671 uiButtonItem *bitem;
2672 uiLayoutItemBx *box;
2674 for (item = litem->items.last; item; item = item->prev) {
2675 if (item->type == ITEM_BUTTON) {
2676 bitem = (uiButtonItem *)item;
2677 if (ui_but_can_align(bitem->but))
2678 if (!bitem->but->alignnr)
2679 bitem->but->alignnr = nr;
2681 else if (item->type == ITEM_LAYOUT_ABSOLUTE) {
2684 else if (item->type == ITEM_LAYOUT_OVERLAP) {
2687 else if (item->type == ITEM_LAYOUT_BOX) {
2688 box = (uiLayoutItemBx *)item;
2689 box->roundbox->alignnr = nr;
2690 BLI_remlink(&litem->root->block->buttons, box->roundbox);
2691 BLI_addhead(&litem->root->block->buttons, box->roundbox);
2693 else if (((uiLayout *)item)->align) {
2694 ui_item_align((uiLayout *)item, nr);
2699 static void ui_item_flag(uiLayout *litem, int flag)
2702 uiButtonItem *bitem;
2704 for (item = litem->items.last; item; item = item->prev) {
2705 if (item->type == ITEM_BUTTON) {
2706 bitem = (uiButtonItem *)item;
2707 bitem->but->flag |= flag;
2710 ui_item_flag((uiLayout *)item, flag);
2714 static void ui_item_layout(uiItem *item)
2718 if (item->type != ITEM_BUTTON) {
2719 uiLayout *litem = (uiLayout *)item;
2721 if (litem->items.first == NULL)
2725 ui_item_align(litem, ++litem->root->block->alignnr);
2727 ui_item_flag(litem, UI_BUT_INACTIVE);
2728 if (!litem->enabled)
2729 ui_item_flag(litem, UI_BUT_DISABLED);
2731 switch (litem->item.type) {
2732 case ITEM_LAYOUT_COLUMN:
2733 ui_litem_layout_column(litem);
2735 case ITEM_LAYOUT_COLUMN_FLOW:
2736 ui_litem_layout_column_flow(litem);
2738 case ITEM_LAYOUT_ROW:
2739 ui_litem_layout_row(litem);
2741 case ITEM_LAYOUT_BOX:
2742 ui_litem_layout_box(litem);
2744 case ITEM_LAYOUT_ROOT:
2745 ui_litem_layout_root(litem);
2747 case ITEM_LAYOUT_ABSOLUTE:
2748 ui_litem_layout_absolute(litem);
2750 case ITEM_LAYOUT_SPLIT:
2751 ui_litem_layout_split(litem);
2753 case ITEM_LAYOUT_OVERLAP:
2754 ui_litem_layout_overlap(litem);
2760 for (subitem = litem->items.first; subitem; subitem = subitem->next)
2761 ui_item_layout(subitem);
2765 static void ui_layout_end(uiBlock *block, uiLayout *layout, int *x, int *y)
2767 if (layout->root->handlefunc)
2768 uiBlockSetHandleFunc(block, layout->root->handlefunc, layout->root->argv);
2770 ui_item_estimate(&layout->item);
2771 ui_item_layout(&layout->item);
2773 if (x) *x = layout->x;
2774 if (y) *y = layout->y;
2777 static void ui_layout_free(uiLayout *layout)
2779 uiItem *item, *next;
2781 for (item = layout->items.first; item; item = next) {
2784 if (item->type == ITEM_BUTTON)
2787 ui_layout_free((uiLayout *)item);
2793 uiLayout *uiBlockLayout(uiBlock *block, int dir, int type, int x, int y, int size, int em, uiStyle *style)
2798 root = MEM_callocN(sizeof(uiLayoutRoot), "uiLayoutRoot");
2800 root->style = style;
2801 root->block = block;
2802 root->opcontext = WM_OP_INVOKE_REGION_WIN;
2804 layout = MEM_callocN(sizeof(uiLayout), "uiLayout");
2805 layout->item.type = ITEM_LAYOUT_ROOT;
2809 layout->root = root;
2810 layout->space = style->templatespace;
2812 layout->enabled = 1;
2813 layout->context = NULL;
2815 if (type == UI_LAYOUT_MENU)
2818 if (dir == UI_LAYOUT_HORIZONTAL) {
2820 layout->root->emh = em * UI_UNIT_Y;
2824 layout->root->emw = em * UI_UNIT_X;
2827 block->curlayout = layout;
2828 root->layout = layout;
2829 BLI_addtail(&block->layouts, root);
2834 uiBlock *uiLayoutGetBlock(uiLayout *layout)
2836 return layout->root->block;
2839 int uiLayoutGetOperatorContext(uiLayout *layout)
2841 return layout->root->opcontext;
2845 void uiBlockSetCurLayout(uiBlock *block, uiLayout *layout)
2847 block->curlayout = layout;
2850 void ui_layout_add_but(uiLayout *layout, uiBut *but)
2852 uiButtonItem *bitem;
2854 bitem = MEM_callocN(sizeof(uiButtonItem), "uiButtonItem");
2855 bitem->item.type = ITEM_BUTTON;
2857 BLI_addtail(&layout->items, bitem);
2859 if (layout->context) {
2860 but->context = layout->context;
2861 but->context->used = TRUE;
2865 void uiLayoutSetOperatorContext(uiLayout *layout, int opcontext)
2867 layout->root->opcontext = opcontext;
2870 void uiLayoutSetFunc(uiLayout *layout, uiMenuHandleFunc handlefunc, void *argv)
2872 layout->root->handlefunc = handlefunc;
2873 layout->root->argv = argv;
2876 void uiBlockLayoutResolve(uiBlock *block, int *x, int *y)
2883 block->curlayout = NULL;
2885 for (root = block->layouts.first; root; root = root->next) {
2886 /* NULL in advance so we don't interfere when adding button */
2887 ui_layout_end(block, root->layout, x, y);
2888 ui_layout_free(root->layout);
2891 BLI_freelistN(&block->layouts);
2893 /* XXX silly trick, interface_templates.c doesn't get linked
2894 * because it's not used by other files in this module? */
2896 UI_template_fix_linking();
2900 void uiLayoutSetContextPointer(uiLayout *layout, const char *name, PointerRNA *ptr)
2902 uiBlock *block = layout->root->block;
2903 layout->context = CTX_store_add(&block->contexts, name, ptr);
2906 void uiLayoutContextCopy(uiLayout *layout, bContextStore *context)
2908 uiBlock *block = layout->root->block;
2909 layout->context = CTX_store_add_all(&block->contexts, context);
2913 /* introspect funcs */
2914 #include "BLI_dynstr.h"
2916 static void ui_intro_button(DynStr *ds, uiButtonItem *bitem)
2918 uiBut *but = bitem->but;
2919 BLI_dynstr_appendf(ds, "'type':%d, ", but->type); /* see ~ UI_interface.h:200 */
2920 BLI_dynstr_appendf(ds, "'draw_string':'''%s''', ", but->drawstr);
2921 BLI_dynstr_appendf(ds, "'tip':'''%s''', ", but->tip ? but->tip : ""); /* not exactly needed, rna has this */
2924 char *opstr = WM_operator_pystring(but->block->evil_C, but->optype, but->opptr, 0);
2925 BLI_dynstr_appendf(ds, "'operator':'''%s''', ", opstr ? opstr : "");
2930 BLI_dynstr_appendf(ds, "'rna':'%s.%s[%d]', ", RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop), but->rnaindex);
2935 static void ui_intro_items(DynStr *ds, ListBase *lb)
2939 BLI_dynstr_append(ds, "[");
2941 for (item = lb->first; item; item = item->next) {
2943 BLI_dynstr_append(ds, "{");
2945 /* could also use the INT but this is nicer*/
2946 switch (item->type) {
2947 case ITEM_BUTTON: BLI_dynstr_append(ds, "'type':'BUTTON', "); break;
2948 case ITEM_LAYOUT_ROW: BLI_dynstr_append(ds, "'type':'ROW', "); break;
2949 case ITEM_LAYOUT_COLUMN: BLI_dynstr_append(ds, "'type':'COLUMN', "); break;
2950 case ITEM_LAYOUT_COLUMN_FLOW: BLI_dynstr_append(ds, "'type':'COLUMN_FLOW', "); break;
2951 case ITEM_LAYOUT_ROW_FLOW: BLI_dynstr_append(ds, "'type':'ROW_FLOW', "); break;
2952 case ITEM_LAYOUT_BOX: BLI_dynstr_append(ds, "'type':'BOX', "); break;
2953 case ITEM_LAYOUT_ABSOLUTE: BLI_dynstr_append(ds, "'type':'ABSOLUTE', "); break;
2954 case ITEM_LAYOUT_SPLIT: BLI_dynstr_append(ds, "'type':'SPLIT', "); break;
2955 case ITEM_LAYOUT_OVERLAP: BLI_dynstr_append(ds, "'type':'OVERLAP', "); break;
2956 case ITEM_LAYOUT_ROOT: BLI_dynstr_append(ds, "'type':'ROOT', "); break;
2957 default: BLI_dynstr_append(ds, "'type':'UNKNOWN', "); break;
2960 switch (item->type) {
2962 ui_intro_button(ds, (uiButtonItem *)item);
2965 BLI_dynstr_append(ds, "'items':");
2966 ui_intro_items(ds, &((uiLayout *)item)->items);
2970 BLI_dynstr_append(ds, "}");
2972 if (item != lb->last)
2973 BLI_dynstr_append(ds, ", ");
2975 BLI_dynstr_append(ds, "], ");
2978 static void ui_intro_uiLayout(DynStr *ds, uiLayout *layout)
2980 ui_intro_items(ds, &layout->items);
2983 static char *str = NULL; /* XXX, constant re-freeing, far from ideal. */
2984 const char *uiLayoutIntrospect(uiLayout *layout)
2986 DynStr *ds = BLI_dynstr_new();
2992 ui_intro_uiLayout(ds, layout);
2994 str = BLI_dynstr_get_cstring(ds);
2995 BLI_dynstr_free(ds);
3000 #ifdef USE_OP_RESET_BUT
3001 static void ui_layout_operator_buts__reset_cb(bContext *UNUSED(C), void *op_pt, void *UNUSED(arg_dummy2))
3003 WM_operator_properties_reset((wmOperator *)op_pt);
3007 /* this function does not initialize the layout, functions can be called on the layout before and after */
3008 void uiLayoutOperatorButs(const bContext *C, uiLayout *layout, wmOperator *op,
3009 bool (*check_prop)(struct PointerRNA *, struct PropertyRNA *),
3010 const char label_align, const short flag)
3012 if (!op->properties) {
3013 IDPropertyTemplate val = {0};
3014 op->properties = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
3017 if (flag & UI_LAYOUT_OP_SHOW_TITLE) {
3018 uiItemL(layout, RNA_struct_ui_name(op->type->srna), ICON_NONE);
3021 /* poll() on this operator may still fail, at the moment there is no nice feedback when this happens
3022 * just fails silently */
3023 if (!WM_operator_repeat_check(C, op)) {
3024 uiBlockSetButLock(uiLayoutGetBlock(layout), true, "Operator can't' redo");
3026 /* XXX, could give some nicer feedback or not show redo panel at all? */
3027 uiItemL(layout, IFACE_("* Redo Unsupported *"), ICON_NONE);
3031 if (op->type->flag & OPTYPE_PRESET) {
3032 /* XXX, no simple way to get WM_MT_operator_presets.bl_label from python! Label remains the same always! */
3036 uiLayoutGetBlock(layout)->ui_operator = op;
3038 row = uiLayoutRow(layout, TRUE);
3039 uiItemM(row, (bContext *)C, "WM_MT_operator_presets", NULL, ICON_NONE);
3041 WM_operator_properties_create(&op_ptr, "WM_OT_operator_preset_add");
3042 RNA_string_set(&op_ptr, "operator", op->type->idname);
3043 uiItemFullO(row, "WM_OT_operator_preset_add", "", ICON_ZOOMIN, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
3045 WM_operator_properties_create(&op_ptr, "WM_OT_operator_preset_add");
3046 RNA_string_set(&op_ptr, "operator", op->type->idname);
3047 RNA_boolean_set(&op_ptr, "remove_active", TRUE);
3048 uiItemFullO(row, "WM_OT_operator_preset_add", "", ICON_ZOOMOUT, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
3052 op->layout = layout;
3053 op->type->ui((bContext *)C, op);
3056 /* UI_LAYOUT_OP_SHOW_EMPTY ignored */
3059 wmWindowManager *wm = CTX_wm_manager(C);
3063 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
3065 /* main draw call */
3066 empty = uiDefAutoButsRNA(layout, &ptr, check_prop, label_align) == 0;
3068 if (empty && (flag & UI_LAYOUT_OP_SHOW_EMPTY)) {
3069 uiItemL(layout, IFACE_("No Properties"), ICON_NONE);
3073 #ifdef USE_OP_RESET_BUT
3074 /* its possible that reset can do nothing if all have PROP_SKIP_SAVE enabled
3075 * but this is not so important if this button is drawn in those cases
3076 * (which isn't all that likely anyway) - campbell */
3077 if (op->properties->len) {
3080 uiLayout *col; /* needed to avoid alignment errors with previous buttons */
3082 col = uiLayoutColumn(layout, FALSE);
3083 block = uiLayoutGetBlock(col);
3084 but = uiDefIconTextBut(block, BUT, 0, ICON_FILE_REFRESH, IFACE_("Reset"), 0, 0, UI_UNIT_X, UI_UNIT_Y,
3085 NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Reset operator defaults"));
3086 uiButSetFunc(but, ui_layout_operator_buts__reset_cb, op, NULL);
3090 /* set various special settings for buttons */
3094 for (but = uiLayoutGetBlock(layout)->buttons.first; but; but = but->next) {
3095 /* no undo for buttons for operator redo panels */
3096 uiButClearFlag(but, UI_BUT_UNDO);
3098 #if 0 /* broken, causes freedback loop, see [#36109] */
3099 /* if button is operator's default property, and a text-field, enable focus for it
3100 * - this is used for allowing operators with popups to rename stuff with fewer clicks
3102 if ((but->rnaprop == op->type->prop) && (but->type == TEX)) {
3103 uiButSetFocusOnEnter(CTX_wm_window(C), but);
3110 /* this is a bit of a hack but best keep it in one place at least */
3111 MenuType *uiButGetMenuType(uiBut *but)
3113 if (but->menu_create_func == ui_item_menutype_func) {
3114 return (MenuType *)but->poin;