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 int colbuts = len / (2 * cols);
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(uiLayout *layout, uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, const char *uiname, int h, int icon_only)
488 EnumPropertyItem *item;
490 int a, totitem, itemw, icon, value, free;
492 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item, &totitem, &free);
494 /* we dont want nested rows, cols in menus */
495 if (layout->root->type != UI_LAYOUT_MENU) {
496 uiBlockSetCurLayout(block, ui_item_local_sublayout(layout, layout, 1));
499 uiBlockSetCurLayout(block, layout);
502 for (a = 0; a < totitem; a++) {
503 if (!item[a].identifier[0])
506 name = (!uiname || uiname[0]) ? item[a].name : "";
508 value = item[a].value;
509 itemw = ui_text_icon_width(block->curlayout, name, icon, 0);
511 if (icon && name[0] && !icon_only)
512 but = uiDefIconTextButR_prop(block, ROW, 0, icon, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
514 but = uiDefIconButR_prop(block, ROW, 0, icon, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
516 but = uiDefButR_prop(block, ROW, 0, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
518 if (ui_layout_local_dir(layout) != UI_LAYOUT_HORIZONTAL)
519 but->flag |= UI_TEXT_LEFT;
521 uiBlockSetCurLayout(block, layout);
528 /* callback for keymap item change button */
529 static void ui_keymap_but_cb(bContext *UNUSED(C), void *but_v, void *UNUSED(key_v))
533 RNA_boolean_set(&but->rnapoin, "shift", (but->modifier_key & KM_SHIFT) != 0);
534 RNA_boolean_set(&but->rnapoin, "ctrl", (but->modifier_key & KM_CTRL) != 0);
535 RNA_boolean_set(&but->rnapoin, "alt", (but->modifier_key & KM_ALT) != 0);
536 RNA_boolean_set(&but->rnapoin, "oskey", (but->modifier_key & KM_OSKEY) != 0);
539 /* create label + button for RNA property */
540 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)
545 PropertySubType subtype;
548 sub = uiLayoutRow(layout, FALSE);
549 uiBlockSetCurLayout(block, sub);
552 /* XXX UI_GetStringWidth is not accurate */
554 labelw = UI_GetStringWidth(name);
555 CLAMP(labelw, w / 4, 3 * w / 4);
558 uiDefBut(block, LABEL, 0, name, x, y, labelw, h, NULL, 0.0, 0.0, 0, 0, "");
562 type = RNA_property_type(prop);
563 subtype = RNA_property_subtype(prop);
565 if (subtype == PROP_FILEPATH || subtype == PROP_DIRPATH) {
566 uiBlockSetCurLayout(block, uiLayoutRow(sub, TRUE));
567 uiDefAutoButR(block, ptr, prop, index, "", icon, x, y, w - UI_UNIT_X, h);
569 /* BUTTONS_OT_file_browse calls uiFileBrowseContextProperty */
570 but = uiDefIconButO(block, BUT, subtype == PROP_DIRPATH ?
571 "BUTTONS_OT_directory_browse" :
572 "BUTTONS_OT_file_browse",
573 WM_OP_INVOKE_DEFAULT, ICON_FILESEL, x, y, UI_UNIT_X, h, NULL);
575 else if (flag & UI_ITEM_R_EVENT) {
576 uiDefButR_prop(block, KEYEVT, 0, name, x, y, w, h, ptr, prop, index, 0, 0, -1, -1, NULL);
578 else if (flag & UI_ITEM_R_FULL_EVENT) {
579 if (RNA_struct_is_a(ptr->type, &RNA_KeyMapItem)) {
582 WM_keymap_item_to_string(ptr->data, buf, sizeof(buf));
584 but = uiDefButR_prop(block, HOTKEYEVT, 0, buf, x, y, w, h, ptr, prop, 0, 0, 0, -1, -1, NULL);
585 uiButSetFunc(but, ui_keymap_but_cb, but, NULL);
586 if (flag & UI_ITEM_R_IMMEDIATE)
587 uiButSetFlag(but, UI_BUT_IMMEDIATE);
591 but = uiDefAutoButR(block, ptr, prop, index, (type == PROP_ENUM && !(flag & UI_ITEM_R_ICON_ONLY)) ? NULL : "", icon, x, y, w, h);
593 uiBlockSetCurLayout(block, layout);
597 void uiFileBrowseContextProperty(const bContext *C, PointerRNA *ptr, PropertyRNA **prop)
599 ARegion *ar = CTX_wm_region(C);
601 uiBut *but, *prevbut;
603 memset(ptr, 0, sizeof(*ptr));
609 for (block = ar->uiblocks.first; block; block = block->next) {
610 for (but = block->buttons.first; but; but = but->next) {
613 /* find the button before the active one */
614 if ((but->flag & UI_BUT_LAST_ACTIVE) && prevbut && prevbut->rnapoin.data) {
615 if (RNA_property_type(prevbut->rnaprop) == PROP_STRING) {
616 *ptr = prevbut->rnapoin;
617 *prop = prevbut->rnaprop;
625 /********************* Button Items *************************/
628 * Update a buttons tip with an enum's description if possible.
630 static void ui_but_tip_from_enum_item(uiBut *but, EnumPropertyItem *item)
632 if (but->tip == NULL || but->tip[0] == '\0') {
633 if (item->description && item->description[0]) {
634 but->tip = item->description;
640 static void ui_item_disabled(uiLayout *layout, const char *name)
642 uiBlock *block = layout->root->block;
646 uiBlockSetCurLayout(block, layout);
651 w = ui_text_icon_width(layout, name, 0, 0);
653 but = uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
654 but->flag |= UI_BUT_DISABLED;
660 PointerRNA uiItemFullO_ptr(uiLayout *layout, wmOperatorType *ot, const char *name, int icon, IDProperty *properties, int context, int flag)
662 uiBlock *block = layout->root->block;
668 name = RNA_struct_ui_name(ot->srna);
673 if (layout->root->type == UI_LAYOUT_MENU && !icon)
677 uiBlockSetCurLayout(block, layout);
679 w = ui_text_icon_width(layout, name, icon, 0);
681 if (flag & UI_ITEM_R_NO_BG)
682 uiBlockSetEmboss(block, UI_EMBOSSN);
684 /* create the button */
687 but = uiDefIconTextButO_ptr(block, BUT, ot, context, icon, name, 0, 0, w, UI_UNIT_Y, NULL);
690 but = uiDefIconButO_ptr(block, BUT, ot, context, icon, 0, 0, w, UI_UNIT_Y, NULL);
694 but = uiDefButO_ptr(block, BUT, ot, context, name, 0, 0, w, UI_UNIT_Y, NULL);
697 assert(but->optype != NULL);
699 /* text alignment for toolbar buttons */
700 if ((layout->root->type == UI_LAYOUT_TOOLBAR) && !icon)
701 but->flag |= UI_TEXT_LEFT;
703 if (flag & UI_ITEM_R_NO_BG)
704 uiBlockSetEmboss(block, UI_EMBOSS);
706 if (layout->redalert)
707 uiButSetFlag(but, UI_BUT_REDALERT);
709 /* assign properties */
710 if (properties || (flag & UI_ITEM_O_RETURN_PROPS)) {
711 PointerRNA *opptr = uiButGetOperatorPtrRNA(but);
714 opptr->data = properties;
717 IDPropertyTemplate val = {0};
718 opptr->data = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
724 return PointerRNA_NULL;
727 PointerRNA uiItemFullO(uiLayout *layout, const char *opname, const char *name, int icon, IDProperty *properties, int context, int flag)
729 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
731 UI_OPERATOR_ERROR_RET(ot, opname, return PointerRNA_NULL);
733 return uiItemFullO_ptr(layout, ot, name, icon, properties, context, flag);
736 static const char *ui_menu_enumpropname(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int retval)
738 EnumPropertyItem *item;
742 RNA_property_enum_items(layout->root->block->evil_C, ptr, prop, &item, &totitem, &free);
743 if (RNA_enum_name(item, retval, &name)) {
744 name = CTX_IFACE_(RNA_property_translation_context(prop), name);
757 /* same as below but 'prop' is already known */
758 static void uiItemEnumO_ptr__internal(uiLayout *layout, wmOperatorType *ot, const char *name, int icon, PropertyRNA *prop, int value)
761 WM_operator_properties_create_ptr(&ptr, ot);
762 RNA_property_enum_set(&ptr, prop, value);
765 name = ui_menu_enumpropname(layout, &ptr, prop, value);
767 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
769 void uiItemEnumO_ptr(uiLayout *layout, wmOperatorType *ot, const char *name, int icon, const char *propname, int value)
774 WM_operator_properties_create_ptr(&ptr, ot);
776 if ((prop = RNA_struct_find_property(&ptr, propname))) {
780 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
784 RNA_property_enum_set(&ptr, prop, value);
787 name = ui_menu_enumpropname(layout, &ptr, prop, value);
789 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
791 void uiItemEnumO(uiLayout *layout, const char *opname, const char *name, int icon, const char *propname, int value)
793 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
796 uiItemEnumO_ptr(layout, ot, name, icon, propname, value);
799 ui_item_disabled(layout, opname);
800 RNA_warning("unknown operator '%s'", opname);
805 void uiItemsFullEnumO(uiLayout *layout, const char *opname, const char *propname, IDProperty *properties, int context, int flag)
807 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
812 uiBlock *block = layout->root->block;
814 if (!ot || !ot->srna) {
815 ui_item_disabled(layout, opname);
816 RNA_warning("%s '%s'", ot ? "unknown operator" : "operator missing srna", opname);
820 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
821 prop = RNA_struct_find_property(&ptr, propname);
823 /* don't let bad properties slip through */
824 BLI_assert((prop == NULL) || (RNA_property_type(prop) == PROP_ENUM));
826 if (prop && RNA_property_type(prop) == PROP_ENUM) {
827 EnumPropertyItem *item;
828 int totitem, i, free;
829 uiLayout *split = uiLayoutSplit(layout, 0.0f, FALSE);
830 uiLayout *column = uiLayoutColumn(split, FALSE);
832 RNA_property_enum_items_gettexted(block->evil_C, &ptr, prop, &item, &totitem, &free);
834 for (i = 0; i < totitem; i++) {
835 if (item[i].identifier[0]) {
839 WM_operator_properties_create_ptr(&tptr, ot);
841 IDP_FreeProperty(tptr.data);
842 MEM_freeN(tptr.data);
844 tptr.data = IDP_CopyProperty(properties);
845 RNA_property_enum_set(&tptr, prop, item[i].value);
847 uiItemFullO_ptr(column, ot, item[i].name, item[i].icon, tptr.data, context, flag);
850 uiItemEnumO_ptr__internal(column, ot, item[i].name, item[i].icon, prop, item[i].value);
852 ui_but_tip_from_enum_item(block->buttons.last, &item[i]);
857 column = uiLayoutColumn(split, FALSE);
858 /* inconsistent, but menus with labels do not look good flipped */
859 block->flag |= UI_BLOCK_NO_FLIP;
862 uiItemL(column, item[i].name, ICON_NONE);
863 bt = block->buttons.last;
864 bt->flag = UI_TEXT_LEFT;
866 ui_but_tip_from_enum_item(bt, &item[i]);
868 else { /* XXX bug here, colums draw bottom item badly */
878 else if (prop && RNA_property_type(prop) != PROP_ENUM) {
879 RNA_warning("%s.%s, not an enum type", RNA_struct_identifier(ptr.type), propname);
883 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
888 void uiItemsEnumO(uiLayout *layout, const char *opname, const char *propname)
890 uiItemsFullEnumO(layout, opname, propname, NULL, layout->root->opcontext, 0);
893 /* for use in cases where we have */
894 void uiItemEnumO_value(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
896 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
900 UI_OPERATOR_ERROR_RET(ot, opname, return );
902 WM_operator_properties_create_ptr(&ptr, ot);
905 if ((prop = RNA_struct_find_property(&ptr, propname))) {
909 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
913 RNA_property_enum_set(&ptr, prop, value);
915 /* same as uiItemEnumO */
917 name = ui_menu_enumpropname(layout, &ptr, prop, value);
919 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
922 void uiItemEnumO_string(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, const char *value_str)
924 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
928 EnumPropertyItem *item;
931 UI_OPERATOR_ERROR_RET(ot, opname, return );
933 WM_operator_properties_create_ptr(&ptr, ot);
936 if ((prop = RNA_struct_find_property(&ptr, propname))) {
937 /* no need for translations here */
938 RNA_property_enum_items(layout->root->block->evil_C, &ptr, prop, &item, NULL, &free);
939 if (item == NULL || RNA_enum_value_from_id(item, value_str, &value) == 0) {
943 RNA_warning("%s.%s, enum %s not found", RNA_struct_identifier(ptr.type), propname, value_str);
952 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
956 RNA_property_enum_set(&ptr, prop, value);
958 /* same as uiItemEnumO */
960 name = ui_menu_enumpropname(layout, &ptr, prop, value);
962 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
965 void uiItemBooleanO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
967 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
970 UI_OPERATOR_ERROR_RET(ot, opname, return );
972 WM_operator_properties_create_ptr(&ptr, ot);
973 RNA_boolean_set(&ptr, propname, value);
975 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
978 void uiItemIntO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
980 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
983 UI_OPERATOR_ERROR_RET(ot, opname, return );
985 WM_operator_properties_create_ptr(&ptr, ot);
986 RNA_int_set(&ptr, propname, value);
988 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
991 void uiItemFloatO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, float value)
993 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
996 UI_OPERATOR_ERROR_RET(ot, opname, return );
998 WM_operator_properties_create_ptr(&ptr, ot);
999 RNA_float_set(&ptr, propname, value);
1001 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1004 void uiItemStringO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, const char *value)
1006 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1009 UI_OPERATOR_ERROR_RET(ot, opname, return );
1011 WM_operator_properties_create_ptr(&ptr, ot);
1012 RNA_string_set(&ptr, propname, value);
1014 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1017 void uiItemO(uiLayout *layout, const char *name, int icon, const char *opname)
1019 uiItemFullO(layout, opname, name, icon, NULL, layout->root->opcontext, 0);
1022 /* RNA property items */
1024 static void ui_item_rna_size(uiLayout *layout, const char *name, int icon, PointerRNA *ptr, PropertyRNA *prop, int index, int icon_only, int *r_w, int *r_h)
1027 PropertySubType subtype;
1030 /* arbitrary extended width by type */
1031 type = RNA_property_type(prop);
1032 subtype = RNA_property_subtype(prop);
1033 len = RNA_property_array_length(ptr, prop);
1035 if (ELEM3(type, PROP_STRING, PROP_POINTER, PROP_ENUM) && !name[0] && !icon_only)
1036 name = "non-empty text";
1037 else if (type == PROP_BOOLEAN && !name[0] && !icon_only)
1040 w = ui_text_icon_width(layout, name, icon, 0);
1043 /* increase height for arrays */
1044 if (index == RNA_NO_INDEX && len > 0) {
1045 if (!name[0] && icon == ICON_NONE)
1048 if (ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER))
1050 else if (subtype == PROP_MATRIX)
1051 h += ceil(sqrt(len)) * UI_UNIT_Y;
1053 h += len * UI_UNIT_Y;
1055 else if (ui_layout_vary_direction(layout) == UI_ITEM_VARY_X) {
1056 if (type == PROP_BOOLEAN && name[0])
1058 else if (type == PROP_ENUM)
1060 else if (type == PROP_FLOAT || type == PROP_INT)
1068 void uiItemFullR(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int index, int value, int flag, const char *name, int icon)
1070 uiBlock *block = layout->root->block;
1073 char namestr[UI_MAX_NAME_STR];
1074 int len, is_array, w, h, slider, toggle, expand, icon_only, no_bg;
1076 uiBlockSetCurLayout(block, layout);
1079 type = RNA_property_type(prop);
1080 is_array = RNA_property_array_check(prop);
1081 len = (is_array) ? RNA_property_array_length(ptr, prop) : 0;
1083 /* set name and icon */
1085 name = RNA_property_ui_name(prop);
1086 if (icon == ICON_NONE)
1087 icon = RNA_property_ui_icon(prop);
1089 if (ELEM4(type, PROP_INT, PROP_FLOAT, PROP_STRING, PROP_POINTER))
1090 name = ui_item_name_add_colon(name, namestr);
1091 else if (type == PROP_BOOLEAN && is_array && index == RNA_NO_INDEX)
1092 name = ui_item_name_add_colon(name, namestr);
1093 else if (type == PROP_ENUM && index != RNA_ENUM_VALUE)
1094 name = ui_item_name_add_colon(name, namestr);
1096 if (layout->root->type == UI_LAYOUT_MENU) {
1097 if (type == PROP_BOOLEAN && ((is_array == FALSE) || (index != RNA_NO_INDEX))) {
1098 if (is_array) icon = (RNA_property_boolean_get_index(ptr, prop, index)) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1099 else icon = (RNA_property_boolean_get(ptr, prop)) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1101 else if (type == PROP_ENUM && index == RNA_ENUM_VALUE) {
1102 int enum_value = RNA_property_enum_get(ptr, prop);
1103 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1104 icon = (enum_value & value) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1107 icon = (enum_value == value) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1112 slider = (flag & UI_ITEM_R_SLIDER);
1113 toggle = (flag & UI_ITEM_R_TOGGLE);
1114 expand = (flag & UI_ITEM_R_EXPAND);
1115 icon_only = (flag & UI_ITEM_R_ICON_ONLY);
1116 no_bg = (flag & UI_ITEM_R_NO_BG);
1119 ui_item_rna_size(layout, name, icon, ptr, prop, index, icon_only, &w, &h);
1122 uiBlockSetEmboss(block, UI_EMBOSSN);
1124 /* array property */
1125 if (index == RNA_NO_INDEX && is_array)
1126 ui_item_array(layout, block, name, icon, ptr, prop, len, 0, 0, w, h, expand, slider, toggle, icon_only);
1128 else if (type == PROP_ENUM && index == RNA_ENUM_VALUE) {
1129 if (icon && name[0] && !icon_only)
1130 uiDefIconTextButR_prop(block, ROW, 0, icon, name, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1132 uiDefIconButR_prop(block, ROW, 0, icon, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1134 uiDefButR_prop(block, ROW, 0, name, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1137 else if (type == PROP_ENUM && (expand || RNA_property_flag(prop) & PROP_ENUM_FLAG))
1138 ui_item_enum_expand(layout, block, ptr, prop, name, h, icon_only);
1139 /* property with separate label */
1140 else if (type == PROP_ENUM || type == PROP_STRING || type == PROP_POINTER) {
1141 but = ui_item_with_label(layout, block, name, icon, ptr, prop, index, 0, 0, w, h, flag);
1142 ui_but_add_search(but, ptr, prop, NULL, NULL);
1144 if (layout->redalert)
1145 uiButSetFlag(but, UI_BUT_REDALERT);
1149 but = uiDefAutoButR(block, ptr, prop, index, name, icon, 0, 0, w, h);
1151 if (slider && but->type == NUM)
1154 if (toggle && but->type == OPTION)
1157 if (layout->redalert)
1158 uiButSetFlag(but, UI_BUT_REDALERT);
1162 uiBlockSetEmboss(block, UI_EMBOSS);
1165 void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
1167 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1170 ui_item_disabled(layout, propname);
1171 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1175 uiItemFullR(layout, ptr, prop, RNA_NO_INDEX, 0, flag, name, icon);
1178 void uiItemEnumR(uiLayout *layout, const char *name, int icon, struct PointerRNA *ptr, const char *propname, int value)
1180 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1182 if (!prop || RNA_property_type(prop) != PROP_ENUM) {
1183 ui_item_disabled(layout, propname);
1184 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1188 uiItemFullR(layout, ptr, prop, RNA_ENUM_VALUE, value, 0, name, icon);
1191 void uiItemEnumR_string(uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *value, const char *name, int icon)
1193 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1194 EnumPropertyItem *item;
1195 int ivalue, a, free;
1197 if (!prop || RNA_property_type(prop) != PROP_ENUM) {
1198 ui_item_disabled(layout, propname);
1199 RNA_warning("enum property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1203 RNA_property_enum_items(layout->root->block->evil_C, ptr, prop, &item, NULL, &free);
1205 if (!RNA_enum_value_from_id(item, value, &ivalue)) {
1209 ui_item_disabled(layout, propname);
1210 RNA_warning("enum property value not found: %s", value);
1214 for (a = 0; item[a].identifier; a++) {
1215 if (item[a].value == ivalue) {
1216 const char *item_name = CTX_IFACE_(RNA_property_translation_context(prop), item[a].name);
1218 uiItemFullR(layout, ptr, prop, RNA_ENUM_VALUE, ivalue, 0, item_name ? item_name : name, icon ? icon : item[a].icon);
1228 void uiItemsEnumR(uiLayout *layout, struct PointerRNA *ptr, const char *propname)
1231 uiBlock *block = layout->root->block;
1234 prop = RNA_struct_find_property(ptr, propname);
1237 ui_item_disabled(layout, propname);
1238 RNA_warning("enum property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1242 if (RNA_property_type(prop) != PROP_ENUM) {
1243 RNA_warning("not an enum property: %s.%s", RNA_struct_identifier(ptr->type), propname);
1247 EnumPropertyItem *item;
1248 int totitem, i, free;
1249 uiLayout *split = uiLayoutSplit(layout, 0.0f, FALSE);
1250 uiLayout *column = uiLayoutColumn(split, FALSE);
1252 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item, &totitem, &free);
1254 for (i = 0; i < totitem; i++) {
1255 if (item[i].identifier[0]) {
1256 uiItemEnumR(column, item[i].name, ICON_NONE, ptr, propname, item[i].value);
1257 ui_but_tip_from_enum_item(block->buttons.last, &item[i]);
1262 column = uiLayoutColumn(split, FALSE);
1263 /* inconsistent, but menus with labels do not look good flipped */
1264 block->flag |= UI_BLOCK_NO_FLIP;
1267 uiItemL(column, item[i].name, ICON_NONE);
1268 bt = block->buttons.last;
1269 bt->flag = UI_TEXT_LEFT;
1271 ui_but_tip_from_enum_item(bt, &item[i]);
1284 /* Pointer RNA button with search */
1286 typedef struct CollItemSearch {
1287 struct CollItemSearch *next, *prev;
1293 static int sort_search_items_list(void *a, void *b)
1295 CollItemSearch *cis1 = (CollItemSearch *)a;
1296 CollItemSearch *cis2 = (CollItemSearch *)b;
1298 if (BLI_strcasecmp(cis1->name, cis2->name) > 0)
1304 static void rna_search_cb(const struct bContext *C, void *arg_but, const char *str, uiSearchItems *items)
1306 uiBut *but = arg_but;
1308 int i = 0, iconid = 0, flag = RNA_property_flag(but->rnaprop);
1309 ListBase *items_list = MEM_callocN(sizeof(ListBase), "items_list");
1310 CollItemSearch *cis;
1311 const int skip_filter = !but->changed;
1313 /* build a temporary list of relevant items first */
1314 RNA_PROP_BEGIN (&but->rnasearchpoin, itemptr, but->rnasearchprop)
1316 if (flag & PROP_ID_SELF_CHECK)
1317 if (itemptr.data == but->rnapoin.id.data)
1321 if (RNA_property_type(but->rnaprop) == PROP_POINTER) {
1322 if (RNA_property_pointer_poll(&but->rnapoin, but->rnaprop, &itemptr) == 0)
1326 if (itemptr.type && RNA_struct_is_ID(itemptr.type)) {
1327 ID *id = itemptr.data;
1328 char name_ui[MAX_ID_NAME];
1330 #if 0 /* this name is used for a string comparison and can't be modified, TODO */
1331 name_uiprefix_id(name_ui, id);
1333 BLI_strncpy(name_ui, id->name + 2, sizeof(name_ui));
1335 name = BLI_strdup(name_ui);
1336 iconid = ui_id_icon_get((bContext *)C, id, false);
1339 name = RNA_struct_name_get_alloc(&itemptr, NULL, 0, NULL); /* could use the string length here */
1344 if (skip_filter || BLI_strcasestr(name, str)) {
1345 cis = MEM_callocN(sizeof(CollItemSearch), "CollectionItemSearch");
1346 cis->name = MEM_dupallocN(name);
1348 cis->iconid = iconid;
1349 BLI_addtail(items_list, cis);
1358 BLI_sortlist(items_list, sort_search_items_list);
1360 /* add search items from temporary list */
1361 for (cis = items_list->first; cis; cis = cis->next) {
1362 if (false == uiSearchItemAdd(items, cis->name, SET_INT_IN_POINTER(cis->index), cis->iconid)) {
1367 for (cis = items_list->first; cis; cis = cis->next) {
1368 MEM_freeN(cis->name);
1370 BLI_freelistN(items_list);
1371 MEM_freeN(items_list);
1374 static void search_id_collection(StructRNA *ptype, PointerRNA *ptr, PropertyRNA **prop)
1378 /* look for collection property in Main */
1379 RNA_main_pointer_create(G.main, ptr);
1383 RNA_STRUCT_BEGIN (ptr, iprop)
1385 /* if it's a collection and has same pointer type, we've got it */
1386 if (RNA_property_type(iprop) == PROP_COLLECTION) {
1387 srna = RNA_property_pointer_type(ptr, iprop);
1389 if (ptype == srna) {
1398 void ui_but_add_search(uiBut *but, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop)
1403 /* for ID's we do automatic lookup */
1405 if (RNA_property_type(prop) == PROP_POINTER) {
1406 ptype = RNA_property_pointer_type(ptr, prop);
1407 search_id_collection(ptype, &sptr, &searchprop);
1412 /* turn button into search button */
1414 but->type = RNA_property_is_unlink(prop) ? SEARCH_MENU_UNLINK : SEARCH_MENU;
1415 but->hardmax = MAX2(but->hardmax, 256.0f);
1416 but->rnasearchpoin = *searchptr;
1417 but->rnasearchprop = searchprop;
1418 but->flag |= UI_ICON_LEFT | UI_TEXT_LEFT;
1420 if (RNA_property_type(prop) == PROP_ENUM) {
1421 /* XXX, this will have a menu string,
1422 * but in this case we just want the text */
1426 uiButSetSearchFunc(but, rna_search_cb, but, NULL, NULL);
1430 void uiItemPointerR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, struct PointerRNA *searchptr, const char *searchpropname, const char *name, int icon)
1432 PropertyRNA *prop, *searchprop;
1436 StructRNA *icontype;
1439 /* validate arguments */
1440 prop = RNA_struct_find_property(ptr, propname);
1443 RNA_warning("property not found: %s.%s",
1444 RNA_struct_identifier(ptr->type), propname);
1448 type = RNA_property_type(prop);
1449 if (!ELEM3(type, PROP_POINTER, PROP_STRING, PROP_ENUM)) {
1450 RNA_warning("Property %s must be a pointer, string or enum", propname);
1454 searchprop = RNA_struct_find_property(searchptr, searchpropname);
1458 RNA_warning("search collection property not found: %s.%s",
1459 RNA_struct_identifier(searchptr->type), searchpropname);
1462 else if (RNA_property_type(searchprop) != PROP_COLLECTION) {
1463 RNA_warning("search collection property is not a collection type: %s.%s",
1464 RNA_struct_identifier(searchptr->type), searchpropname);
1468 /* get icon & name */
1469 if (icon == ICON_NONE) {
1470 if (type == PROP_POINTER)
1471 icontype = RNA_property_pointer_type(ptr, prop);
1473 icontype = RNA_property_pointer_type(searchptr, searchprop);
1475 icon = RNA_struct_ui_icon(icontype);
1478 name = RNA_property_ui_name(prop);
1481 block = uiLayoutGetBlock(layout);
1483 ui_item_rna_size(layout, name, icon, ptr, prop, 0, 0, &w, &h);
1484 w += UI_UNIT_X; /* X icon needs more space */
1485 but = ui_item_with_label(layout, block, name, icon, ptr, prop, 0, 0, 0, w, h, 0);
1487 ui_but_add_search(but, ptr, prop, searchptr, searchprop);
1491 static void ui_item_menutype_func(bContext *C, uiLayout *layout, void *arg_mt)
1493 MenuType *mt = (MenuType *)arg_mt;
1497 menu.layout = layout;
1499 if (G.debug & G_DEBUG_WM) {
1500 printf("%s: opening menu \"%s\"\n", __func__, mt->idname);
1503 if (layout->context)
1504 CTX_store_set(C, layout->context);
1508 if (layout->context)
1509 CTX_store_set(C, NULL);
1512 static void ui_item_menu(uiLayout *layout, const char *name, int icon, uiMenuCreateFunc func, void *arg, void *argN, const char *tip)
1514 uiBlock *block = layout->root->block;
1518 uiBlockSetCurLayout(block, layout);
1520 if (layout->root->type == UI_LAYOUT_HEADER)
1521 uiBlockSetEmboss(block, UI_EMBOSS);
1525 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1528 w = ui_text_icon_width(layout, name, icon, 1);
1531 if (layout->root->type == UI_LAYOUT_HEADER) /* ugly .. */
1534 if (name[0] && icon)
1535 but = uiDefIconTextMenuBut(block, func, arg, icon, name, 0, 0, w, h, tip);
1537 but = uiDefIconMenuBut(block, func, arg, icon, 0, 0, w, h, tip);
1539 but = uiDefMenuBut(block, func, arg, name, 0, 0, w, h, tip);
1541 if (argN) { /* ugly .. */
1542 but->poin = (char *)but;
1543 but->func_argN = argN;
1546 if (layout->root->type == UI_LAYOUT_HEADER)
1547 uiBlockSetEmboss(block, UI_EMBOSS);
1548 else if (ELEM(layout->root->type, UI_LAYOUT_PANEL, UI_LAYOUT_TOOLBAR)) {
1550 but->flag |= UI_TEXT_LEFT;
1554 void uiItemM(uiLayout *layout, bContext *UNUSED(C), const char *menuname, const char *name, int icon)
1558 mt = WM_menutype_find(menuname, FALSE);
1561 RNA_warning("not found %s", menuname);
1566 name = CTX_IFACE_(mt->translation_context, mt->label);
1569 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1572 ui_item_menu(layout, name, icon, ui_item_menutype_func, mt, NULL, TIP_(mt->description));
1576 static uiBut *uiItemL_(uiLayout *layout, const char *name, int icon)
1578 uiBlock *block = layout->root->block;
1582 uiBlockSetCurLayout(block, layout);
1586 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1589 w = ui_text_icon_width(layout, name, icon, 0);
1591 if (icon && name[0])
1592 but = uiDefIconTextBut(block, LABEL, 0, icon, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1594 but = uiDefIconBut(block, LABEL, 0, icon, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1596 but = uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1598 /* to compensate for string size padding in ui_text_icon_width,
1599 * make text aligned right if the layout is aligned right.
1601 if (uiLayoutGetAlignment(layout) == UI_LAYOUT_ALIGN_RIGHT) {
1602 but->flag &= ~UI_TEXT_LEFT; /* default, needs to be unset */
1603 but->flag |= UI_TEXT_RIGHT;
1609 void uiItemL(uiLayout *layout, const char *name, int icon)
1611 uiItemL_(layout, name, icon);
1614 void uiItemLDrag(uiLayout *layout, PointerRNA *ptr, const char *name, int icon)
1616 uiBut *but = uiItemL_(layout, name, icon);
1618 if (ptr && ptr->type)
1619 if (RNA_struct_is_ID(ptr->type))
1620 uiButSetDragID(but, ptr->id.data);
1625 void uiItemV(uiLayout *layout, const char *name, int icon, int argval)
1628 uiBlock *block = layout->root->block;
1629 float *retvalue = (block->handle) ? &block->handle->retvalue : NULL;
1632 uiBlockSetCurLayout(block, layout);
1636 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1639 w = ui_text_icon_width(layout, name, icon, 0);
1641 if (icon && name[0])
1642 uiDefIconTextButF(block, BUT, argval, icon, name, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1644 uiDefIconButF(block, BUT, argval, icon, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1646 uiDefButF(block, BUT, argval, name, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1649 /* separator item */
1650 void uiItemS(uiLayout *layout)
1652 uiBlock *block = layout->root->block;
1654 uiBlockSetCurLayout(block, layout);
1655 uiDefBut(block, SEPR, 0, "", 0, 0, 0.3f * UI_UNIT_X, 0.3f * UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1659 void uiItemMenuF(uiLayout *layout, const char *name, int icon, uiMenuCreateFunc func, void *arg)
1664 ui_item_menu(layout, name, icon, func, arg, NULL, "");
1667 typedef struct MenuItemLevel {
1669 /* don't use pointers to the strings because python can dynamically
1670 * allocate strings and free before the menu draws, see [#27304] */
1671 char opname[OP_MAX_TYPENAME];
1672 char propname[MAX_IDPROP_NAME];
1676 static void menu_item_enum_opname_menu(bContext *UNUSED(C), uiLayout *layout, void *arg)
1678 MenuItemLevel *lvl = (MenuItemLevel *)(((uiBut *)arg)->func_argN);
1680 uiLayoutSetOperatorContext(layout, lvl->opcontext);
1681 uiItemsEnumO(layout, lvl->opname, lvl->propname);
1684 void uiItemMenuEnumO(uiLayout *layout, bContext *C, const char *opname, const char *propname, const char *name, int icon)
1686 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1688 char namestr[UI_MAX_NAME_STR], keybuf[128];
1690 UI_OPERATOR_ERROR_RET(ot, opname, return );
1693 ui_item_disabled(layout, opname);
1694 RNA_warning("operator missing srna '%s'", opname);
1699 BLI_strncpy(namestr, name, sizeof(namestr));
1701 BLI_strncpy(namestr, RNA_struct_ui_name(ot->srna), sizeof(namestr));
1703 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1706 lvl = MEM_callocN(sizeof(MenuItemLevel), "MenuItemLevel");
1707 BLI_strncpy(lvl->opname, opname, sizeof(lvl->opname));
1708 BLI_strncpy(lvl->propname, propname, sizeof(lvl->propname));
1709 lvl->opcontext = layout->root->opcontext;
1711 /* add hotkey here, lower UI code can't detect it */
1712 if (layout->root->block->flag & UI_BLOCK_LOOP) {
1713 if (ot->prop && WM_key_event_operator_string(C, ot->idname,
1714 layout->root->opcontext, NULL, false, keybuf, sizeof(keybuf)))
1716 strncat(namestr, "|", sizeof(namestr) - 1);
1717 strncat(namestr, keybuf, sizeof(namestr) - 1);
1721 ui_item_menu(layout, namestr, icon, menu_item_enum_opname_menu, NULL, lvl, RNA_struct_ui_description(ot->srna));
1724 static void menu_item_enum_rna_menu(bContext *UNUSED(C), uiLayout *layout, void *arg)
1726 MenuItemLevel *lvl = (MenuItemLevel *)(((uiBut *)arg)->func_argN);
1728 uiLayoutSetOperatorContext(layout, lvl->opcontext);
1729 uiItemsEnumR(layout, &lvl->rnapoin, lvl->propname);
1732 void uiItemMenuEnumR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *name, int icon)
1737 prop = RNA_struct_find_property(ptr, propname);
1739 ui_item_disabled(layout, propname);
1740 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1745 name = RNA_property_ui_name(prop);
1746 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1749 lvl = MEM_callocN(sizeof(MenuItemLevel), "MenuItemLevel");
1750 lvl->rnapoin = *ptr;
1751 BLI_strncpy(lvl->propname, propname, sizeof(lvl->propname));
1752 lvl->opcontext = layout->root->opcontext;
1754 ui_item_menu(layout, name, icon, menu_item_enum_rna_menu, NULL, lvl, RNA_property_description(prop));
1757 /**************************** Layout Items ***************************/
1759 /* single-row layout */
1760 static void ui_litem_estimate_row(uiLayout *litem)
1768 for (item = litem->items.first; item; item = item->next) {
1769 ui_item_size(item, &itemw, &itemh);
1772 litem->h = MAX2(itemh, litem->h);
1775 litem->w += litem->space;
1779 static int ui_litem_min_width(int itemw)
1781 return MIN2(2 * UI_UNIT_X, itemw);
1784 static void ui_litem_layout_row(uiLayout *litem)
1787 int x, y, w, tot, totw, neww, itemw, minw, itemh, offset;
1788 int fixedw, freew, fixedx, freex, flag = 0, lastw = 0;
1790 /* x = litem->x; */ /* UNUSED */
1796 for (item = litem->items.first; item; item = item->next) {
1797 ui_item_size(item, &itemw, &itemh);
1806 w -= (tot - 1) * litem->space;
1809 /* keep clamping items to fixed minimum size until all are done */
1815 for (item = litem->items.first; item; item = item->next) {
1819 ui_item_size(item, &itemw, &itemh);
1820 minw = ui_litem_min_width(itemw);
1823 neww = ui_item_fit(itemw, x, totw, w - lastw, !item->next, litem->alignment, NULL);
1825 neww = 0; /* no space left, all will need clamping to minimum size */
1829 if ((neww < minw || itemw == minw) && w != 0) {
1837 /* keep free size */
1850 for (item = litem->items.first; item; item = item->next) {
1851 ui_item_size(item, &itemw, &itemh);
1852 minw = ui_litem_min_width(itemw);
1855 /* fixed minimum size items */
1856 itemw = ui_item_fit(minw, fixedx, fixedw, min_ii(w, fixedw), !item->next, litem->alignment, NULL);
1860 /* free size item */
1861 itemw = ui_item_fit(itemw, freex, freew, w - fixedw, !item->next, litem->alignment, NULL);
1865 /* align right/center */
1867 if (litem->alignment == UI_LAYOUT_ALIGN_RIGHT) {
1868 if (freew + fixedw > 0 && freew + fixedw < w)
1869 offset = w - (fixedw + freew);
1871 else if (litem->alignment == UI_LAYOUT_ALIGN_CENTER) {
1872 if (freew + fixedw > 0 && freew + fixedw < w)
1873 offset = (w - (fixedw + freew)) / 2;
1877 ui_item_position(item, x + offset, y - itemh, itemw, itemh);
1884 litem->w = x - litem->x;
1885 litem->h = litem->y - y;
1890 /* single-column layout */
1891 static void ui_litem_estimate_column(uiLayout *litem)
1899 for (item = litem->items.first; item; item = item->next) {
1900 ui_item_size(item, &itemw, &itemh);
1902 litem->w = MAX2(litem->w, itemw);
1906 litem->h += litem->space;
1910 static void ui_litem_layout_column(uiLayout *litem)
1918 for (item = litem->items.first; item; item = item->next) {
1919 ui_item_size(item, NULL, &itemh);
1922 ui_item_position(item, x, y, litem->w, itemh);
1928 litem->h = litem->y - y;
1934 static void ui_litem_estimate_root(uiLayout *UNUSED(litem))
1939 static void ui_litem_layout_root(uiLayout *litem)
1941 if (litem->root->type == UI_LAYOUT_HEADER)
1942 ui_litem_layout_row(litem);
1944 ui_litem_layout_column(litem);
1948 static void ui_litem_estimate_box(uiLayout *litem)
1950 uiStyle *style = litem->root->style;
1952 ui_litem_estimate_column(litem);
1953 litem->w += 2 * style->boxspace;
1954 litem->h += style->boxspace;
1957 static void ui_litem_layout_box(uiLayout *litem)
1959 uiLayoutItemBx *box = (uiLayoutItemBx *)litem;
1960 uiStyle *style = litem->root->style;
1967 litem->x += style->boxspace;
1969 if (w != 0) litem->w -= 2 * style->boxspace;
1970 if (h != 0) litem->h -= 2 * style->boxspace;
1972 ui_litem_layout_column(litem);
1974 litem->x -= style->boxspace;
1975 litem->y -= style->boxspace;
1977 if (w != 0) litem->w += 2 * style->boxspace;
1978 if (h != 0) litem->h += style->boxspace;
1980 /* roundbox around the sublayout */
1981 but = box->roundbox;
1982 but->rect.xmin = litem->x;
1983 but->rect.ymin = litem->y;
1984 but->rect.xmax = litem->x + litem->w;
1985 but->rect.ymax = litem->y + litem->h;
1988 /* multi-column layout, automatically flowing to the next */
1989 static void ui_litem_estimate_column_flow(uiLayout *litem)
1991 uiStyle *style = litem->root->style;
1992 uiLayoutItemFlow *flow = (uiLayoutItemFlow *)litem;
1994 int col, x, y, emh, emy, miny, itemw, itemh, maxw = 0;
1997 /* compute max needed width and total height */
2000 for (item = litem->items.first; item; item = item->next) {
2001 ui_item_size(item, &itemw, &itemh);
2002 maxw = MAX2(maxw, itemw);
2007 if (flow->number <= 0) {
2008 /* auto compute number of columns, not very good */
2014 flow->totcol = max_ii(litem->root->emw / maxw, 1);
2015 flow->totcol = min_ii(flow->totcol, totitem);
2018 flow->totcol = flow->number;
2027 emh = toth / flow->totcol;
2029 /* create column per column */
2031 for (item = litem->items.first; item; item = item->next) {
2032 ui_item_size(item, &itemw, &itemh);
2034 y -= itemh + style->buttonspacey;
2035 miny = min_ii(miny, y);
2037 maxw = max_ii(itemw, maxw);
2039 /* decide to go to next one */
2040 if (col < flow->totcol - 1 && emy <= -emh) {
2041 x += maxw + litem->space;
2044 emy = 0; /* need to reset height again for next column */
2050 litem->h = litem->y - miny;
2053 static void ui_litem_layout_column_flow(uiLayout *litem)
2055 uiStyle *style = litem->root->style;
2056 uiLayoutItemFlow *flow = (uiLayoutItemFlow *)litem;
2058 int col, x, y, w, emh, emy, miny, itemw, itemh;
2059 int toth, totitem, offset;
2061 /* compute max needed width and total height */
2064 for (item = litem->items.first; item; item = item->next) {
2065 ui_item_size(item, &itemw, &itemh);
2076 w = litem->w - (flow->totcol - 1) * style->columnspace;
2077 emh = toth / flow->totcol;
2079 /* create column per column */
2081 for (item = litem->items.first; item; item = item->next) {
2082 ui_item_size(item, NULL, &itemh);
2083 itemw = ui_item_fit(1, x - litem->x, flow->totcol, w, col == flow->totcol - 1, litem->alignment, &offset);
2087 ui_item_position(item, x + offset, y, itemw, itemh);
2088 y -= style->buttonspacey;
2089 miny = min_ii(miny, y);
2091 /* decide to go to next one */
2092 if (col < flow->totcol - 1 && emy <= -emh) {
2093 x += itemw + style->columnspace;
2095 emy = 0; /* need to reset height again for next column */
2100 litem->h = litem->y - miny;
2106 static void ui_litem_estimate_absolute(uiLayout *litem)
2109 int itemx, itemy, itemw, itemh, minx, miny;
2116 for (item = litem->items.first; item; item = item->next) {
2117 ui_item_offset(item, &itemx, &itemy);
2118 ui_item_size(item, &itemw, &itemh);
2120 minx = min_ii(minx, itemx);
2121 miny = min_ii(miny, itemy);
2123 litem->w = MAX2(litem->w, itemx + itemw);
2124 litem->h = MAX2(litem->h, itemy + itemh);
2131 static void ui_litem_layout_absolute(uiLayout *litem)
2134 float scalex = 1.0f, scaley = 1.0f;
2135 int x, y, newx, newy, itemx, itemy, itemh, itemw, minx, miny, totw, toth;
2142 for (item = litem->items.first; item; item = item->next) {
2143 ui_item_offset(item, &itemx, &itemy);
2144 ui_item_size(item, &itemw, &itemh);
2146 minx = min_ii(minx, itemx);
2147 miny = min_ii(miny, itemy);
2149 totw = max_ii(totw, itemx + itemw);
2150 toth = max_ii(toth, itemy + itemh);
2156 if (litem->w && totw > 0)
2157 scalex = (float)litem->w / (float)totw;
2158 if (litem->h && toth > 0)
2159 scaley = (float)litem->h / (float)toth;
2162 y = litem->y - scaley * toth;
2164 for (item = litem->items.first; item; item = item->next) {
2165 ui_item_offset(item, &itemx, &itemy);
2166 ui_item_size(item, &itemw, &itemh);
2168 if (scalex != 1.0f) {
2169 newx = (itemx - minx) * scalex;
2170 itemw = (itemx - minx + itemw) * scalex - newx;
2171 itemx = minx + newx;
2174 if (scaley != 1.0f) {
2175 newy = (itemy - miny) * scaley;
2176 itemh = (itemy - miny + itemh) * scaley - newy;
2177 itemy = miny + newy;
2180 ui_item_position(item, x + itemx - minx, y + itemy - miny, itemw, itemh);
2183 litem->w = scalex * totw;
2184 litem->h = litem->y - y;
2185 litem->x = x + litem->w;
2190 static void ui_litem_estimate_split(uiLayout *litem)
2192 ui_litem_estimate_row(litem);
2195 static void ui_litem_layout_split(uiLayout *litem)
2197 uiLayoutItemSplit *split = (uiLayoutItemSplit *)litem;
2200 const int tot = BLI_countlist(&litem->items);
2201 int itemh, x, y, w, colw = 0;
2209 percentage = (split->percentage == 0.0f) ? 1.0f / (float)tot : split->percentage;
2211 w = (litem->w - (tot - 1) * litem->space);
2212 colw = w * percentage;
2213 colw = MAX2(colw, 0);
2215 for (item = litem->items.first; item; item = item->next) {
2216 ui_item_size(item, NULL, &itemh);
2218 ui_item_position(item, x, y - itemh, colw, itemh);
2222 colw = (w - (int)(w * percentage)) / (tot - 1);
2223 colw = MAX2(colw, 0);
2229 litem->w = x - litem->x;
2230 litem->h = litem->y - y;
2235 /* overlap layout */
2236 static void ui_litem_estimate_overlap(uiLayout *litem)
2244 for (item = litem->items.first; item; item = item->next) {
2245 ui_item_size(item, &itemw, &itemh);
2247 litem->w = MAX2(itemw, litem->w);
2248 litem->h = MAX2(itemh, litem->h);
2252 static void ui_litem_layout_overlap(uiLayout *litem)
2255 int itemw, itemh, x, y;
2260 for (item = litem->items.first; item; item = item->next) {
2261 ui_item_size(item, &itemw, &itemh);
2262 ui_item_position(item, x, y - itemh, litem->w, itemh);
2264 litem->h = MAX2(litem->h, itemh);
2268 litem->y = y - litem->h;
2271 /* layout create functions */
2272 uiLayout *uiLayoutRow(uiLayout *layout, int align)
2276 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutRow");
2277 litem->item.type = ITEM_LAYOUT_ROW;
2278 litem->root = layout->root;
2279 litem->align = align;
2280 litem->active = true;
2281 litem->enabled = true;
2282 litem->context = layout->context;
2283 litem->space = (align) ? 0 : layout->root->style->buttonspacex;
2284 litem->redalert = layout->redalert;
2285 litem->w = layout->w;
2286 BLI_addtail(&layout->items, litem);
2288 uiBlockSetCurLayout(layout->root->block, litem);
2293 uiLayout *uiLayoutColumn(uiLayout *layout, int align)
2297 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutColumn");
2298 litem->item.type = ITEM_LAYOUT_COLUMN;
2299 litem->root = layout->root;
2300 litem->align = align;
2301 litem->active = true;
2302 litem->enabled = true;
2303 litem->context = layout->context;
2304 litem->space = (litem->align) ? 0 : layout->root->style->buttonspacey;
2305 litem->redalert = layout->redalert;
2306 litem->w = layout->w;
2307 BLI_addtail(&layout->items, litem);
2309 uiBlockSetCurLayout(layout->root->block, litem);
2314 uiLayout *uiLayoutColumnFlow(uiLayout *layout, int number, int align)
2316 uiLayoutItemFlow *flow;
2318 flow = MEM_callocN(sizeof(uiLayoutItemFlow), "uiLayoutItemFlow");
2319 flow->litem.item.type = ITEM_LAYOUT_COLUMN_FLOW;
2320 flow->litem.root = layout->root;
2321 flow->litem.align = align;
2322 flow->litem.active = true;
2323 flow->litem.enabled = true;
2324 flow->litem.context = layout->context;
2325 flow->litem.space = (flow->litem.align) ? 0 : layout->root->style->columnspace;
2326 flow->litem.redalert = layout->redalert;
2327 flow->litem.w = layout->w;
2328 flow->number = number;
2329 BLI_addtail(&layout->items, flow);
2331 uiBlockSetCurLayout(layout->root->block, &flow->litem);
2333 return &flow->litem;
2336 static uiLayoutItemBx *ui_layout_box(uiLayout *layout, int type)
2338 uiLayoutItemBx *box;
2340 box = MEM_callocN(sizeof(uiLayoutItemBx), "uiLayoutItemBx");
2341 box->litem.item.type = ITEM_LAYOUT_BOX;
2342 box->litem.root = layout->root;
2343 box->litem.active = 1;
2344 box->litem.enabled = 1;
2345 box->litem.context = layout->context;
2346 box->litem.space = layout->root->style->columnspace;
2347 box->litem.redalert = layout->redalert;
2348 box->litem.w = layout->w;
2349 BLI_addtail(&layout->items, box);
2351 uiBlockSetCurLayout(layout->root->block, &box->litem);
2353 box->roundbox = uiDefBut(layout->root->block, type, 0, "", 0, 0, 0, 0, NULL, 0.0, 0.0, 0, 0, "");
2358 uiLayout *uiLayoutBox(uiLayout *layout)
2360 return (uiLayout *)ui_layout_box(layout, ROUNDBOX);
2363 uiLayout *uiLayoutListBox(uiLayout *layout, uiList *ui_list, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *actptr,
2364 PropertyRNA *actprop)
2366 uiLayoutItemBx *box = ui_layout_box(layout, LISTBOX);
2367 uiBut *but = box->roundbox;
2369 but->custom_data = ui_list;
2371 but->rnasearchpoin = *ptr;
2372 but->rnasearchprop = prop;
2373 but->rnapoin = *actptr;
2374 but->rnaprop = actprop;
2376 return (uiLayout *)box;
2379 uiLayout *uiLayoutAbsolute(uiLayout *layout, int align)
2383 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutAbsolute");
2384 litem->item.type = ITEM_LAYOUT_ABSOLUTE;
2385 litem->root = layout->root;
2386 litem->align = align;
2389 litem->context = layout->context;
2390 litem->redalert = layout->redalert;
2391 BLI_addtail(&layout->items, litem);
2393 uiBlockSetCurLayout(layout->root->block, litem);
2398 uiBlock *uiLayoutAbsoluteBlock(uiLayout *layout)
2402 block = uiLayoutGetBlock(layout);
2403 uiLayoutAbsolute(layout, FALSE);
2408 uiLayout *uiLayoutOverlap(uiLayout *layout)
2412 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutOverlap");
2413 litem->item.type = ITEM_LAYOUT_OVERLAP;
2414 litem->root = layout->root;
2415 litem->active = true;
2416 litem->enabled = true;
2417 litem->context = layout->context;
2418 litem->redalert = layout->redalert;
2419 BLI_addtail(&layout->items, litem);
2421 uiBlockSetCurLayout(layout->root->block, litem);
2426 uiLayout *uiLayoutSplit(uiLayout *layout, float percentage, int align)
2428 uiLayoutItemSplit *split;
2430 split = MEM_callocN(sizeof(uiLayoutItemSplit), "uiLayoutItemSplit");
2431 split->litem.item.type = ITEM_LAYOUT_SPLIT;
2432 split->litem.root = layout->root;
2433 split->litem.align = align;
2434 split->litem.active = true;
2435 split->litem.enabled = true;
2436 split->litem.context = layout->context;
2437 split->litem.space = layout->root->style->columnspace;
2438 split->litem.redalert = layout->redalert;
2439 split->litem.w = layout->w;
2440 split->percentage = percentage;
2441 BLI_addtail(&layout->items, split);
2443 uiBlockSetCurLayout(layout->root->block, &split->litem);
2445 return &split->litem;
2448 void uiLayoutSetActive(uiLayout *layout, bool active)
2450 layout->active = active;
2453 void uiLayoutSetEnabled(uiLayout *layout, bool enabled)
2455 layout->enabled = enabled;
2458 void uiLayoutSetRedAlert(uiLayout *layout, bool redalert)
2460 layout->redalert = redalert;
2463 void uiLayoutSetKeepAspect(uiLayout *layout, bool keepaspect)
2465 layout->keepaspect = keepaspect;
2468 void uiLayoutSetAlignment(uiLayout *layout, char alignment)
2470 layout->alignment = alignment;
2473 void uiLayoutSetScaleX(uiLayout *layout, float scale)
2475 layout->scale[0] = scale;
2478 void uiLayoutSetScaleY(uiLayout *layout, float scale)
2480 layout->scale[1] = scale;
2483 bool uiLayoutGetActive(uiLayout *layout)
2485 return layout->active;
2488 bool uiLayoutGetEnabled(uiLayout *layout)
2490 return layout->enabled;
2493 bool uiLayoutGetRedAlert(uiLayout *layout)
2495 return layout->redalert;
2498 bool uiLayoutGetKeepAspect(uiLayout *layout)
2500 return layout->keepaspect;
2503 int uiLayoutGetAlignment(uiLayout *layout)
2505 return layout->alignment;
2508 int uiLayoutGetWidth(uiLayout *layout)
2513 float uiLayoutGetScaleX(uiLayout *layout)
2515 return layout->scale[0];
2518 float uiLayoutGetScaleY(uiLayout *layout)
2520 return layout->scale[1];
2523 /********************** Layout *******************/
2525 static void ui_item_scale(uiLayout *litem, const float scale[2])
2530 for (item = litem->items.last; item; item = item->prev) {
2531 ui_item_size(item, &w, &h);
2532 ui_item_offset(item, &x, &y);
2534 if (scale[0] != 0.0f) {
2539 if (scale[1] != 0.0f) {
2544 ui_item_position(item, x, y, w, h);
2548 static void ui_item_estimate(uiItem *item)
2552 if (item->type != ITEM_BUTTON) {
2553 uiLayout *litem = (uiLayout *)item;
2555 for (subitem = litem->items.first; subitem; subitem = subitem->next)
2556 ui_item_estimate(subitem);
2558 if (litem->items.first == NULL)
2561 if (litem->scale[0] != 0.0f || litem->scale[1] != 0.0f)
2562 ui_item_scale(litem, litem->scale);
2564 switch (litem->item.type) {
2565 case ITEM_LAYOUT_COLUMN:
2566 ui_litem_estimate_column(litem);
2568 case ITEM_LAYOUT_COLUMN_FLOW:
2569 ui_litem_estimate_column_flow(litem);
2571 case ITEM_LAYOUT_ROW:
2572 ui_litem_estimate_row(litem);
2574 case ITEM_LAYOUT_BOX:
2575 ui_litem_estimate_box(litem);
2577 case ITEM_LAYOUT_ROOT:
2578 ui_litem_estimate_root(litem);
2580 case ITEM_LAYOUT_ABSOLUTE:
2581 ui_litem_estimate_absolute(litem);
2583 case ITEM_LAYOUT_SPLIT:
2584 ui_litem_estimate_split(litem);
2586 case ITEM_LAYOUT_OVERLAP:
2587 ui_litem_estimate_overlap(litem);
2595 static void ui_item_align(uiLayout *litem, short nr)
2598 uiButtonItem *bitem;
2599 uiLayoutItemBx *box;
2601 for (item = litem->items.last; item; item = item->prev) {
2602 if (item->type == ITEM_BUTTON) {
2603 bitem = (uiButtonItem *)item;
2604 if (ui_but_can_align(bitem->but))
2605 if (!bitem->but->alignnr)
2606 bitem->but->alignnr = nr;
2608 else if (item->type == ITEM_LAYOUT_ABSOLUTE) {
2611 else if (item->type == ITEM_LAYOUT_OVERLAP) {
2614 else if (item->type == ITEM_LAYOUT_BOX) {
2615 box = (uiLayoutItemBx *)item;
2616 box->roundbox->alignnr = nr;
2617 BLI_remlink(&litem->root->block->buttons, box->roundbox);
2618 BLI_addhead(&litem->root->block->buttons, box->roundbox);
2621 ui_item_align((uiLayout *)item, nr);
2625 static void ui_item_flag(uiLayout *litem, int flag)
2628 uiButtonItem *bitem;
2630 for (item = litem->items.last; item; item = item->prev) {
2631 if (item->type == ITEM_BUTTON) {
2632 bitem = (uiButtonItem *)item;
2633 bitem->but->flag |= flag;
2636 ui_item_flag((uiLayout *)item, flag);
2640 static void ui_item_layout(uiItem *item)
2644 if (item->type != ITEM_BUTTON) {
2645 uiLayout *litem = (uiLayout *)item;
2647 if (litem->items.first == NULL)
2651 ui_item_align(litem, ++litem->root->block->alignnr);
2653 ui_item_flag(litem, UI_BUT_INACTIVE);
2654 if (!litem->enabled)
2655 ui_item_flag(litem, UI_BUT_DISABLED);
2657 switch (litem->item.type) {
2658 case ITEM_LAYOUT_COLUMN:
2659 ui_litem_layout_column(litem);
2661 case ITEM_LAYOUT_COLUMN_FLOW:
2662 ui_litem_layout_column_flow(litem);
2664 case ITEM_LAYOUT_ROW:
2665 ui_litem_layout_row(litem);
2667 case ITEM_LAYOUT_BOX:
2668 ui_litem_layout_box(litem);
2670 case ITEM_LAYOUT_ROOT:
2671 ui_litem_layout_root(litem);
2673 case ITEM_LAYOUT_ABSOLUTE:
2674 ui_litem_layout_absolute(litem);
2676 case ITEM_LAYOUT_SPLIT:
2677 ui_litem_layout_split(litem);
2679 case ITEM_LAYOUT_OVERLAP:
2680 ui_litem_layout_overlap(litem);
2686 for (subitem = litem->items.first; subitem; subitem = subitem->next)
2687 ui_item_layout(subitem);
2691 static void ui_layout_end(uiBlock *block, uiLayout *layout, int *x, int *y)
2693 if (layout->root->handlefunc)
2694 uiBlockSetHandleFunc(block, layout->root->handlefunc, layout->root->argv);
2696 ui_item_estimate(&layout->item);
2697 ui_item_layout(&layout->item);
2699 if (x) *x = layout->x;
2700 if (y) *y = layout->y;
2703 static void ui_layout_free(uiLayout *layout)
2705 uiItem *item, *next;
2707 for (item = layout->items.first; item; item = next) {
2710 if (item->type == ITEM_BUTTON)
2713 ui_layout_free((uiLayout *)item);
2719 uiLayout *uiBlockLayout(uiBlock *block, int dir, int type, int x, int y, int size, int em, uiStyle *style)
2724 root = MEM_callocN(sizeof(uiLayoutRoot), "uiLayoutRoot");
2726 root->style = style;
2727 root->block = block;
2728 root->opcontext = WM_OP_INVOKE_REGION_WIN;
2730 layout = MEM_callocN(sizeof(uiLayout), "uiLayout");
2731 layout->item.type = ITEM_LAYOUT_ROOT;
2735 layout->root = root;
2736 layout->space = style->templatespace;
2738 layout->enabled = 1;
2739 layout->context = NULL;
2741 if (type == UI_LAYOUT_MENU)
2744 if (dir == UI_LAYOUT_HORIZONTAL) {
2746 layout->root->emh = em * UI_UNIT_Y;
2750 layout->root->emw = em * UI_UNIT_X;
2753 block->curlayout = layout;
2754 root->layout = layout;
2755 BLI_addtail(&block->layouts, root);
2760 uiBlock *uiLayoutGetBlock(uiLayout *layout)
2762 return layout->root->block;
2765 int uiLayoutGetOperatorContext(uiLayout *layout)
2767 return layout->root->opcontext;
2771 void uiBlockSetCurLayout(uiBlock *block, uiLayout *layout)
2773 block->curlayout = layout;
2776 void ui_layout_add_but(uiLayout *layout, uiBut *but)
2778 uiButtonItem *bitem;
2780 bitem = MEM_callocN(sizeof(uiButtonItem), "uiButtonItem");
2781 bitem->item.type = ITEM_BUTTON;
2783 BLI_addtail(&layout->items, bitem);
2785 if (layout->context) {
2786 but->context = layout->context;
2787 but->context->used = TRUE;
2791 void uiLayoutSetOperatorContext(uiLayout *layout, int opcontext)
2793 layout->root->opcontext = opcontext;
2796 void uiLayoutSetFunc(uiLayout *layout, uiMenuHandleFunc handlefunc, void *argv)
2798 layout->root->handlefunc = handlefunc;
2799 layout->root->argv = argv;
2802 void uiBlockLayoutResolve(uiBlock *block, int *x, int *y)
2809 block->curlayout = NULL;
2811 for (root = block->layouts.first; root; root = root->next) {
2812 /* NULL in advance so we don't interfere when adding button */
2813 ui_layout_end(block, root->layout, x, y);
2814 ui_layout_free(root->layout);
2817 BLI_freelistN(&block->layouts);
2819 /* XXX silly trick, interface_templates.c doesn't get linked
2820 * because it's not used by other files in this module? */
2822 UI_template_fix_linking();
2826 void uiLayoutSetContextPointer(uiLayout *layout, const char *name, PointerRNA *ptr)
2828 uiBlock *block = layout->root->block;
2829 layout->context = CTX_store_add(&block->contexts, name, ptr);
2832 void uiLayoutContextCopy(uiLayout *layout, bContextStore *context)
2834 uiBlock *block = layout->root->block;
2835 layout->context = CTX_store_add_all(&block->contexts, context);
2839 /* introspect funcs */
2840 #include "BLI_dynstr.h"
2842 static void ui_intro_button(DynStr *ds, uiButtonItem *bitem)
2844 uiBut *but = bitem->but;
2845 BLI_dynstr_appendf(ds, "'type':%d, ", but->type); /* see ~ UI_interface.h:200 */
2846 BLI_dynstr_appendf(ds, "'draw_string':'''%s''', ", but->drawstr);
2847 BLI_dynstr_appendf(ds, "'tip':'''%s''', ", but->tip ? but->tip : ""); /* not exactly needed, rna has this */
2850 char *opstr = WM_operator_pystring(but->block->evil_C, but->optype, but->opptr, 0);
2851 BLI_dynstr_appendf(ds, "'operator':'''%s''', ", opstr ? opstr : "");
2856 BLI_dynstr_appendf(ds, "'rna':'%s.%s[%d]', ", RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop), but->rnaindex);
2861 static void ui_intro_items(DynStr *ds, ListBase *lb)
2865 BLI_dynstr_append(ds, "[");
2867 for (item = lb->first; item; item = item->next) {
2869 BLI_dynstr_append(ds, "{");
2871 /* could also use the INT but this is nicer*/
2872 switch (item->type) {
2873 case ITEM_BUTTON: BLI_dynstr_append(ds, "'type':'BUTTON', "); break;
2874 case ITEM_LAYOUT_ROW: BLI_dynstr_append(ds, "'type':'ROW', "); break;
2875 case ITEM_LAYOUT_COLUMN: BLI_dynstr_append(ds, "'type':'COLUMN', "); break;
2876 case ITEM_LAYOUT_COLUMN_FLOW: BLI_dynstr_append(ds, "'type':'COLUMN_FLOW', "); break;
2877 case ITEM_LAYOUT_ROW_FLOW: BLI_dynstr_append(ds, "'type':'ROW_FLOW', "); break;
2878 case ITEM_LAYOUT_BOX: BLI_dynstr_append(ds, "'type':'BOX', "); break;
2879 case ITEM_LAYOUT_ABSOLUTE: BLI_dynstr_append(ds, "'type':'ABSOLUTE', "); break;
2880 case ITEM_LAYOUT_SPLIT: BLI_dynstr_append(ds, "'type':'SPLIT', "); break;
2881 case ITEM_LAYOUT_OVERLAP: BLI_dynstr_append(ds, "'type':'OVERLAP', "); break;
2882 case ITEM_LAYOUT_ROOT: BLI_dynstr_append(ds, "'type':'ROOT', "); break;
2883 default: BLI_dynstr_append(ds, "'type':'UNKNOWN', "); break;
2886 switch (item->type) {
2888 ui_intro_button(ds, (uiButtonItem *)item);
2891 BLI_dynstr_append(ds, "'items':");
2892 ui_intro_items(ds, &((uiLayout *)item)->items);
2896 BLI_dynstr_append(ds, "}");
2898 if (item != lb->last)
2899 BLI_dynstr_append(ds, ", ");
2901 BLI_dynstr_append(ds, "], ");
2904 static void ui_intro_uiLayout(DynStr *ds, uiLayout *layout)
2906 ui_intro_items(ds, &layout->items);
2909 static char *str = NULL; /* XXX, constant re-freeing, far from ideal. */
2910 const char *uiLayoutIntrospect(uiLayout *layout)
2912 DynStr *ds = BLI_dynstr_new();
2918 ui_intro_uiLayout(ds, layout);
2920 str = BLI_dynstr_get_cstring(ds);
2921 BLI_dynstr_free(ds);
2926 #ifdef USE_OP_RESET_BUT
2927 static void ui_layout_operator_buts__reset_cb(bContext *UNUSED(C), void *op_pt, void *UNUSED(arg_dummy2))
2929 WM_operator_properties_reset((wmOperator *)op_pt);
2933 /* this function does not initialize the layout, functions can be called on the layout before and after */
2934 void uiLayoutOperatorButs(const bContext *C, uiLayout *layout, wmOperator *op,
2935 bool (*check_prop)(struct PointerRNA *, struct PropertyRNA *),
2936 const char label_align, const short flag)
2938 if (!op->properties) {
2939 IDPropertyTemplate val = {0};
2940 op->properties = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
2943 if (flag & UI_LAYOUT_OP_SHOW_TITLE) {
2944 uiItemL(layout, RNA_struct_ui_name(op->type->srna), ICON_NONE);
2947 /* poll() on this operator may still fail, at the moment there is no nice feedback when this happens
2948 * just fails silently */
2949 if (!WM_operator_repeat_check(C, op)) {
2950 uiBlockSetButLock(uiLayoutGetBlock(layout), true, "Operator can't' redo");
2952 /* XXX, could give some nicer feedback or not show redo panel at all? */
2953 uiItemL(layout, IFACE_("* Redo Unsupported *"), ICON_NONE);
2957 if (op->type->flag & OPTYPE_PRESET) {
2958 /* XXX, no simple way to get WM_MT_operator_presets.bl_label from python! Label remains the same always! */
2962 uiLayoutGetBlock(layout)->ui_operator = op;
2964 row = uiLayoutRow(layout, TRUE);
2965 uiItemM(row, (bContext *)C, "WM_MT_operator_presets", NULL, ICON_NONE);
2967 WM_operator_properties_create(&op_ptr, "WM_OT_operator_preset_add");
2968 RNA_string_set(&op_ptr, "operator", op->type->idname);
2969 uiItemFullO(row, "WM_OT_operator_preset_add", "", ICON_ZOOMIN, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
2971 WM_operator_properties_create(&op_ptr, "WM_OT_operator_preset_add");
2972 RNA_string_set(&op_ptr, "operator", op->type->idname);
2973 RNA_boolean_set(&op_ptr, "remove_active", TRUE);
2974 uiItemFullO(row, "WM_OT_operator_preset_add", "", ICON_ZOOMOUT, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
2978 op->layout = layout;
2979 op->type->ui((bContext *)C, op);
2982 /* UI_LAYOUT_OP_SHOW_EMPTY ignored */
2985 wmWindowManager *wm = CTX_wm_manager(C);
2989 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
2991 /* main draw call */
2992 empty = uiDefAutoButsRNA(layout, &ptr, check_prop, label_align) == 0;
2994 if (empty && (flag & UI_LAYOUT_OP_SHOW_EMPTY)) {
2995 uiItemL(layout, IFACE_("No Properties"), ICON_NONE);
2999 #ifdef USE_OP_RESET_BUT
3000 /* its possible that reset can do nothing if all have PROP_SKIP_SAVE enabled
3001 * but this is not so important if this button is drawn in those cases
3002 * (which isn't all that likely anyway) - campbell */
3003 if (op->properties->len) {
3006 uiLayout *col; /* needed to avoid alignment errors with previous buttons */
3008 col = uiLayoutColumn(layout, FALSE);
3009 block = uiLayoutGetBlock(col);
3010 but = uiDefIconTextBut(block, BUT, 0, ICON_FILE_REFRESH, IFACE_("Reset"), 0, 0, UI_UNIT_X, UI_UNIT_Y,
3011 NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Reset operator defaults"));
3012 uiButSetFunc(but, ui_layout_operator_buts__reset_cb, op, NULL);
3016 /* set various special settings for buttons */
3020 for (but = uiLayoutGetBlock(layout)->buttons.first; but; but = but->next) {
3021 /* no undo for buttons for operator redo panels */
3022 uiButClearFlag(but, UI_BUT_UNDO);
3024 /* if button is operator's default property, and a text-field, enable focus for it
3025 * - this is used for allowing operators with popups to rename stuff with fewer clicks
3027 if ((but->rnaprop == op->type->prop) && (but->type == TEX)) {
3028 uiButSetFocusOnEnter(CTX_wm_window(C), but);
3034 /* this is a bit of a hack but best keep it in one place at least */
3035 MenuType *uiButGetMenuType(uiBut *but)
3037 if (but->menu_create_func == ui_item_menutype_func) {
3038 return (MenuType *)but->poin;