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"
57 #include "ED_armature.h"
63 #include "interface_intern.h"
65 /************************ Structs and Defines *************************/
67 #define RNA_NO_INDEX -1
68 #define RNA_ENUM_VALUE -2
71 // #define USE_OP_RESET_BUT // we may want to make this optional, disable for now.
73 #define UI_OPERATOR_ERROR_RET(_ot, _opname, return_statement) \
75 ui_item_disabled(layout, _opname); \
76 RNA_warning("'%s' unknown operator", _opname); \
83 typedef struct uiLayoutRoot {
84 struct uiLayoutRoot *next, *prev;
91 uiMenuHandleFunc handlefunc;
101 typedef enum uiItemType {
106 ITEM_LAYOUT_COLUMN_FLOW,
107 ITEM_LAYOUT_ROW_FLOW,
109 ITEM_LAYOUT_ABSOLUTE,
115 TEMPLATE_COLUMN_FLOW,
124 typedef struct uiItem {
130 typedef struct uiButtonItem {
139 bContextStore *context;
153 typedef struct uiLayoutItemFlow {
159 typedef struct uiLayoutItemBx {
164 typedef struct uiLayoutItemSplit {
169 typedef struct uiLayoutItemRoot {
173 /************************** Item ***************************/
175 static const char *ui_item_name_add_colon(const char *name, char namestr[UI_MAX_NAME_STR])
177 int len = strlen(name);
179 if (len != 0 && len + 1 < UI_MAX_NAME_STR) {
180 BLI_strncpy(namestr, name, UI_MAX_NAME_STR);
182 namestr[len + 1] = '\0';
189 static int ui_item_fit(int item, int pos, int all, int available, int last, int alignment, int *offset)
194 /* available == 0 is unlimited */
198 if (all > available) {
199 /* contents is bigger than available space */
201 return available - pos;
203 return (item * available) / all;
206 /* contents is smaller or equal to available space */
207 if (alignment == UI_LAYOUT_ALIGN_EXPAND) {
209 return available - pos;
211 return (item * available) / all;
218 /* variable button size in which direction? */
219 #define UI_ITEM_VARY_X 1
220 #define UI_ITEM_VARY_Y 2
222 static int ui_layout_vary_direction(uiLayout *layout)
224 return (layout->root->type == UI_LAYOUT_HEADER || layout->alignment != UI_LAYOUT_ALIGN_EXPAND) ? UI_ITEM_VARY_X : UI_ITEM_VARY_Y;
227 /* estimated size of text + icon */
228 static int ui_text_icon_width(uiLayout *layout, const char *name, int icon, int compact)
230 float f5 = 0.25f * UI_UNIT_X;
231 float f10 = 0.5f * UI_UNIT_X;
232 int variable = ui_layout_vary_direction(layout) == UI_ITEM_VARY_X;
234 if (icon && !name[0])
235 return UI_UNIT_X; /* icon only */
237 return (variable) ? UI_GetStringWidth(name) + (compact ? f5 : f10) + UI_UNIT_X : 10 * UI_UNIT_X; /* icon + text */
239 return (variable) ? UI_GetStringWidth(name) + (compact ? f5 : f10) + UI_UNIT_X : 10 * UI_UNIT_X; /* text only */
242 static void ui_item_size(uiItem *item, int *r_w, int *r_h)
244 if (item->type == ITEM_BUTTON) {
245 uiButtonItem *bitem = (uiButtonItem *)item;
247 if (r_w) *r_w = BLI_rctf_size_x(&bitem->but->rect);
248 if (r_h) *r_h = BLI_rctf_size_y(&bitem->but->rect);
251 uiLayout *litem = (uiLayout *)item;
253 if (r_w) *r_w = litem->w;
254 if (r_h) *r_h = litem->h;
258 static void ui_item_offset(uiItem *item, int *r_x, int *r_y)
260 if (item->type == ITEM_BUTTON) {
261 uiButtonItem *bitem = (uiButtonItem *)item;
263 if (r_x) *r_x = bitem->but->rect.xmin;
264 if (r_y) *r_y = bitem->but->rect.ymin;
272 static void ui_item_position(uiItem *item, int x, int y, int w, int h)
274 if (item->type == ITEM_BUTTON) {
275 uiButtonItem *bitem = (uiButtonItem *)item;
277 bitem->but->rect.xmin = x;
278 bitem->but->rect.ymin = y;
279 bitem->but->rect.xmax = x + w;
280 bitem->but->rect.ymax = y + h;
282 ui_check_but(bitem->but); /* for strlen */
285 uiLayout *litem = (uiLayout *)item;
294 /******************** Special RNA Items *********************/
296 static int ui_layout_local_dir(uiLayout *layout)
298 switch (layout->item.type) {
299 case ITEM_LAYOUT_ROW:
300 case ITEM_LAYOUT_ROOT:
301 case ITEM_LAYOUT_OVERLAP:
302 return UI_LAYOUT_HORIZONTAL;
303 case ITEM_LAYOUT_COLUMN:
304 case ITEM_LAYOUT_COLUMN_FLOW:
305 case ITEM_LAYOUT_SPLIT:
306 case ITEM_LAYOUT_ABSOLUTE:
307 case ITEM_LAYOUT_BOX:
309 return UI_LAYOUT_VERTICAL;
313 static uiLayout *ui_item_local_sublayout(uiLayout *test, uiLayout *layout, int align)
317 if (ui_layout_local_dir(test) == UI_LAYOUT_HORIZONTAL)
318 sub = uiLayoutRow(layout, align);
320 sub = uiLayoutColumn(layout, align);
326 static void ui_layer_but_cb(bContext *C, void *arg_but, void *arg_index)
328 wmWindow *win = CTX_wm_window(C);
329 uiBut *but = arg_but, *cbut;
330 PointerRNA *ptr = &but->rnapoin;
331 PropertyRNA *prop = but->rnaprop;
332 int i, index = GET_INT_FROM_POINTER(arg_index);
333 int shift = win->eventstate->shift;
334 int len = RNA_property_array_length(ptr, prop);
337 RNA_property_boolean_set_index(ptr, prop, index, TRUE);
339 for (i = 0; i < len; i++)
341 RNA_property_boolean_set_index(ptr, prop, i, 0);
343 RNA_property_update(C, ptr, prop);
345 for (cbut = but->block->buttons.first; cbut; cbut = cbut->next)
350 /* create buttons for an item with an RNA array */
351 static void ui_item_array(uiLayout *layout, uiBlock *block, const char *name, int icon,
352 PointerRNA *ptr, PropertyRNA *prop, int len, int x, int y, int w, int UNUSED(h),
353 int expand, int slider, int toggle, int icon_only)
355 uiStyle *style = layout->root->style;
358 PropertySubType subtype;
362 /* retrieve type and subtype */
363 type = RNA_property_type(prop);
364 subtype = RNA_property_subtype(prop);
366 sub = ui_item_local_sublayout(layout, layout, 1);
367 uiBlockSetCurLayout(block, sub);
371 uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
374 if (type == PROP_BOOLEAN && ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER)) {
375 /* special check for layer layout */
376 int butw, buth, unit;
377 int cols = (len >= 20) ? 2 : 1;
378 const unsigned int colbuts = len / (2 * cols);
379 unsigned int layer_used = 0;
380 unsigned int layer_active = 0;
382 uiBlockSetCurLayout(block, uiLayoutAbsolute(layout, FALSE));
384 unit = UI_UNIT_X * 0.75;
388 if (ptr->type == &RNA_Armature) {
389 bArmature *arm = (bArmature *)ptr->data;
391 layer_used = arm->layer_used;
394 if (arm->act_edbone) {
395 layer_active |= arm->act_edbone->layer;
400 layer_active |= arm->act_bone->layer;
405 for (b = 0; b < cols; b++) {
406 uiBlockBeginAlign(block);
408 for (a = 0; a < colbuts; a++) {
409 int layer_num = a + b * colbuts;
410 int layer_flag = 1 << layer_num;
412 if (layer_used & layer_flag) {
413 if (layer_active & layer_flag)
414 icon = ICON_LAYER_ACTIVE;
416 icon = ICON_LAYER_USED;
422 but = uiDefAutoButR(block, ptr, prop, layer_num, "", icon, x + butw * a, y + buth, butw, buth);
423 if (subtype == PROP_LAYER_MEMBER)
424 uiButSetFunc(but, ui_layer_but_cb, but, SET_INT_IN_POINTER(layer_num));
426 for (a = 0; a < colbuts; a++) {
427 int layer_num = a + len / 2 + b * colbuts;
428 int layer_flag = 1 << layer_num;
430 if (layer_used & layer_flag) {
431 if (layer_active & layer_flag)
432 icon = ICON_LAYER_ACTIVE;
434 icon = ICON_LAYER_USED;
440 but = uiDefAutoButR(block, ptr, prop, layer_num, "", icon, x + butw * a, y, butw, buth);
441 if (subtype == PROP_LAYER_MEMBER)
442 uiButSetFunc(but, ui_layer_but_cb, but, SET_INT_IN_POINTER(layer_num));
444 uiBlockEndAlign(block);
446 x += colbuts * butw + style->buttonspacex;
449 else if (subtype == PROP_MATRIX) {
450 int totdim, dim_size[3]; /* 3 == RNA_MAX_ARRAY_DIMENSION */
453 uiBlockSetCurLayout(block, uiLayoutAbsolute(layout, TRUE));
455 totdim = RNA_property_array_dimension(ptr, prop, dim_size);
456 if (totdim != 2) return; /* only 2D matrices supported in UI so far */
459 /* h /= dim_size[1]; */ /* UNUSED */
461 for (a = 0; a < len; a++) {
462 col = a % dim_size[0];
463 row = a / dim_size[0];
465 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);
466 if (slider && but->type == NUM)
470 else if (subtype == PROP_DIRECTION && !expand) {
471 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);
474 /* note, this block of code is a bit arbitrary and has just been made
475 * to work with common cases, but may need to be re-worked */
477 /* special case, boolean array in a menu, this could be used in a more generic way too */
478 if (ELEM(subtype, PROP_COLOR, PROP_COLOR_GAMMA) && !expand) {
479 uiDefAutoButR(block, ptr, prop, -1, "", ICON_NONE, 0, 0, w, UI_UNIT_Y);
484 /* even if 'expand' is fale, expanding anyway */
486 /* layout for known array subtypes */
487 char str[3] = {'\0'};
490 if (type != PROP_BOOLEAN) {
495 /* show checkboxes for rna on a non-emboss block (menu for eg) */
496 if (type == PROP_BOOLEAN && ELEM(layout->root->block->dt, UI_EMBOSSN, UI_EMBOSSP)) {
497 boolarr = MEM_callocN(sizeof(int) * len, "ui_item_array");
498 RNA_property_boolean_get_array(ptr, prop, boolarr);
501 for (a = 0; a < len; a++) {
502 if (!icon_only) str[0] = RNA_property_array_item_char(prop, a);
503 if (boolarr) icon = boolarr[a] ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
504 but = uiDefAutoButR(block, ptr, prop, a, str, icon, 0, 0, w, UI_UNIT_Y);
505 if (slider && but->type == NUM)
507 if (toggle && but->type == OPTION)
509 if ((a == 0) && (subtype == PROP_AXISANGLE))
510 uiButSetUnitType(but, PROP_UNIT_ROTATION);
519 uiBlockSetCurLayout(block, layout);
522 static void ui_item_enum_expand_handle(bContext *C, void *arg1, void *arg2)
524 wmWindow *win = CTX_wm_window(C);
526 if (!win->eventstate->shift) {
527 uiBut *but = (uiBut *)arg1;
528 int enum_value = GET_INT_FROM_POINTER(arg2);
530 int current_value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
531 if (!(current_value & enum_value)) {
532 current_value = enum_value;
535 current_value &= enum_value;
537 RNA_property_enum_set(&but->rnapoin, but->rnaprop, current_value);
540 static void ui_item_enum_expand(uiLayout *layout, uiBlock *block, PointerRNA *ptr, PropertyRNA *prop,
541 const char *uiname, int h, int icon_only)
543 /* XXX The way this function currently handles uiname parameter is insane and inconsistent with general UI API:
544 * * uiname is the *enum property* label.
545 * * when it is NULL or empty, we do not draw *enum items* labels, this doubles the icon_only parameter.
546 * * we *never* draw (i.e. really use) the enum label uiname, it is just used as a mere flag!
547 * Unfortunately, fixing this implies an API "soft break", so better to defer it for later... :/
552 EnumPropertyItem *item, *item_array;
554 int itemw, icon, value, free;
556 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item_array, NULL, &free);
558 /* we dont want nested rows, cols in menus */
559 if (layout->root->type != UI_LAYOUT_MENU) {
560 uiBlockSetCurLayout(block, ui_item_local_sublayout(layout, layout, 1));
563 uiBlockSetCurLayout(block, layout);
566 for (item = item_array; item->identifier; item++) {
567 if (!item->identifier[0])
570 name = (!uiname || uiname[0]) ? item->name : "";
573 itemw = ui_text_icon_width(block->curlayout, icon_only ? "" : name, icon, 0);
575 if (icon && name[0] && !icon_only)
576 but = uiDefIconTextButR_prop(block, ROW, 0, icon, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
578 but = uiDefIconButR_prop(block, ROW, 0, icon, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
580 but = uiDefButR_prop(block, ROW, 0, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
582 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
583 uiButSetFunc(but, ui_item_enum_expand_handle, but, SET_INT_IN_POINTER(value));
586 if (ui_layout_local_dir(layout) != UI_LAYOUT_HORIZONTAL)
587 but->drawflag |= UI_BUT_TEXT_LEFT;
589 uiBlockSetCurLayout(block, layout);
592 MEM_freeN(item_array);
596 /* callback for keymap item change button */
597 static void ui_keymap_but_cb(bContext *UNUSED(C), void *but_v, void *UNUSED(key_v))
601 RNA_boolean_set(&but->rnapoin, "shift", (but->modifier_key & KM_SHIFT) != 0);
602 RNA_boolean_set(&but->rnapoin, "ctrl", (but->modifier_key & KM_CTRL) != 0);
603 RNA_boolean_set(&but->rnapoin, "alt", (but->modifier_key & KM_ALT) != 0);
604 RNA_boolean_set(&but->rnapoin, "oskey", (but->modifier_key & KM_OSKEY) != 0);
607 /* create label + button for RNA property */
608 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)
613 PropertySubType subtype;
616 sub = uiLayoutRow(layout, layout->align);
617 uiBlockSetCurLayout(block, sub);
620 /* XXX UI_GetStringWidth is not accurate */
622 labelw = UI_GetStringWidth(name);
623 CLAMP(labelw, w / 4, 3 * w / 4);
626 uiDefBut(block, LABEL, 0, name, x, y, labelw, h, NULL, 0.0, 0.0, 0, 0, "");
630 type = RNA_property_type(prop);
631 subtype = RNA_property_subtype(prop);
633 if (subtype == PROP_FILEPATH || subtype == PROP_DIRPATH) {
634 uiBlockSetCurLayout(block, uiLayoutRow(sub, TRUE));
635 uiDefAutoButR(block, ptr, prop, index, "", icon, x, y, w - UI_UNIT_X, h);
637 /* BUTTONS_OT_file_browse calls uiFileBrowseContextProperty */
638 but = uiDefIconButO(block, BUT, subtype == PROP_DIRPATH ?
639 "BUTTONS_OT_directory_browse" :
640 "BUTTONS_OT_file_browse",
641 WM_OP_INVOKE_DEFAULT, ICON_FILESEL, x, y, UI_UNIT_X, h, NULL);
643 else if (flag & UI_ITEM_R_EVENT) {
644 uiDefButR_prop(block, KEYEVT, 0, name, x, y, w, h, ptr, prop, index, 0, 0, -1, -1, NULL);
646 else if (flag & UI_ITEM_R_FULL_EVENT) {
647 if (RNA_struct_is_a(ptr->type, &RNA_KeyMapItem)) {
650 WM_keymap_item_to_string(ptr->data, buf, sizeof(buf));
652 but = uiDefButR_prop(block, HOTKEYEVT, 0, buf, x, y, w, h, ptr, prop, 0, 0, 0, -1, -1, NULL);
653 uiButSetFunc(but, ui_keymap_but_cb, but, NULL);
654 if (flag & UI_ITEM_R_IMMEDIATE)
655 uiButSetFlag(but, UI_BUT_IMMEDIATE);
659 but = uiDefAutoButR(block, ptr, prop, index, (type == PROP_ENUM && !(flag & UI_ITEM_R_ICON_ONLY)) ? NULL : "", icon, x, y, w, h);
661 uiBlockSetCurLayout(block, layout);
665 void uiFileBrowseContextProperty(const bContext *C, PointerRNA *ptr, PropertyRNA **prop)
667 ARegion *ar = CTX_wm_region(C);
669 uiBut *but, *prevbut;
671 memset(ptr, 0, sizeof(*ptr));
677 for (block = ar->uiblocks.first; block; block = block->next) {
678 for (but = block->buttons.first; but; but = but->next) {
681 /* find the button before the active one */
682 if ((but->flag & UI_BUT_LAST_ACTIVE) && prevbut && prevbut->rnapoin.data) {
683 if (RNA_property_type(prevbut->rnaprop) == PROP_STRING) {
684 *ptr = prevbut->rnapoin;
685 *prop = prevbut->rnaprop;
693 /********************* Button Items *************************/
696 * Update a buttons tip with an enum's description if possible.
698 static void ui_but_tip_from_enum_item(uiBut *but, EnumPropertyItem *item)
700 if (but->tip == NULL || but->tip[0] == '\0') {
701 if (item->description && item->description[0]) {
702 but->tip = item->description;
708 static void ui_item_disabled(uiLayout *layout, const char *name)
710 uiBlock *block = layout->root->block;
714 uiBlockSetCurLayout(block, layout);
719 w = ui_text_icon_width(layout, name, 0, 0);
721 but = uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
722 but->flag |= UI_BUT_DISABLED;
728 PointerRNA uiItemFullO_ptr(uiLayout *layout, wmOperatorType *ot, const char *name, int icon, IDProperty *properties, int context, int flag)
730 uiBlock *block = layout->root->block;
736 name = RNA_struct_ui_name(ot->srna);
741 if (layout->root->type == UI_LAYOUT_MENU && !icon)
745 uiBlockSetCurLayout(block, layout);
747 w = ui_text_icon_width(layout, name, icon, 0);
749 if (flag & UI_ITEM_R_NO_BG)
750 uiBlockSetEmboss(block, UI_EMBOSSN);
752 /* create the button */
755 but = uiDefIconTextButO_ptr(block, BUT, ot, context, icon, name, 0, 0, w, UI_UNIT_Y, NULL);
758 but = uiDefIconButO_ptr(block, BUT, ot, context, icon, 0, 0, w, UI_UNIT_Y, NULL);
762 but = uiDefButO_ptr(block, BUT, ot, context, name, 0, 0, w, UI_UNIT_Y, NULL);
765 assert(but->optype != NULL);
767 /* text alignment for toolbar buttons */
768 if ((layout->root->type == UI_LAYOUT_TOOLBAR) && !icon)
769 but->drawflag |= UI_BUT_TEXT_LEFT;
771 if (flag & UI_ITEM_R_NO_BG)
772 uiBlockSetEmboss(block, UI_EMBOSS);
774 if (layout->redalert)
775 uiButSetFlag(but, UI_BUT_REDALERT);
777 /* assign properties */
778 if (properties || (flag & UI_ITEM_O_RETURN_PROPS)) {
779 PointerRNA *opptr = uiButGetOperatorPtrRNA(but);
782 opptr->data = properties;
785 IDPropertyTemplate val = {0};
786 opptr->data = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
792 return PointerRNA_NULL;
795 PointerRNA uiItemFullO(uiLayout *layout, const char *opname, const char *name, int icon, IDProperty *properties, int context, int flag)
797 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
799 UI_OPERATOR_ERROR_RET(ot, opname, return PointerRNA_NULL);
801 return uiItemFullO_ptr(layout, ot, name, icon, properties, context, flag);
804 static const char *ui_menu_enumpropname(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int retval)
806 EnumPropertyItem *item;
810 RNA_property_enum_items(layout->root->block->evil_C, ptr, prop, &item, NULL, &free);
811 if (RNA_enum_name(item, retval, &name)) {
812 name = CTX_IFACE_(RNA_property_translation_context(prop), name);
825 void uiItemEnumO_ptr(uiLayout *layout, wmOperatorType *ot, const char *name, int icon, const char *propname, int value)
830 WM_operator_properties_create_ptr(&ptr, ot);
832 if ((prop = RNA_struct_find_property(&ptr, propname))) {
836 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
840 RNA_property_enum_set(&ptr, prop, value);
843 name = ui_menu_enumpropname(layout, &ptr, prop, value);
845 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
847 void uiItemEnumO(uiLayout *layout, const char *opname, const char *name, int icon, const char *propname, int value)
849 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
852 uiItemEnumO_ptr(layout, ot, name, icon, propname, value);
855 ui_item_disabled(layout, opname);
856 RNA_warning("unknown operator '%s'", opname);
861 void uiItemsFullEnumO(uiLayout *layout, const char *opname, const char *propname, IDProperty *properties,
862 int context, int flag)
864 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
868 uiBlock *block = layout->root->block;
870 if (!ot || !ot->srna) {
871 ui_item_disabled(layout, opname);
872 RNA_warning("%s '%s'", ot ? "unknown operator" : "operator missing srna", opname);
876 WM_operator_properties_create_ptr(&ptr, ot);
877 /* so the context is passed to itemf functions (some need it) */
878 WM_operator_properties_sanitize(&ptr, false);
879 prop = RNA_struct_find_property(&ptr, propname);
881 /* don't let bad properties slip through */
882 BLI_assert((prop == NULL) || (RNA_property_type(prop) == PROP_ENUM));
884 if (prop && RNA_property_type(prop) == PROP_ENUM) {
885 EnumPropertyItem *item, *item_array = NULL;
887 uiLayout *split = uiLayoutSplit(layout, 0.0f, FALSE);
888 uiLayout *column = uiLayoutColumn(split, FALSE);
890 RNA_property_enum_items_gettexted(block->evil_C, &ptr, prop, &item_array, NULL, &free);
891 for (item = item_array; item->identifier; item++) {
892 if (item->identifier[0]) {
895 WM_operator_properties_create_ptr(&tptr, ot);
898 IDP_FreeProperty(tptr.data);
899 MEM_freeN(tptr.data);
901 tptr.data = IDP_CopyProperty(properties);
903 RNA_property_enum_set(&tptr, prop, item->value);
905 uiItemFullO_ptr(column, ot, item->name, item->icon, tptr.data, context, flag);
906 ui_but_tip_from_enum_item(block->buttons.last, item);
911 if (item != item_array) {
912 column = uiLayoutColumn(split, FALSE);
913 /* inconsistent, but menus with labels do not look good flipped */
914 block->flag |= UI_BLOCK_NO_FLIP;
917 uiItemL(column, item->name, ICON_NONE);
918 but = block->buttons.last;
919 but->drawflag = UI_BUT_TEXT_LEFT;
920 ui_but_tip_from_enum_item(but, item);
922 else { /* XXX bug here, colums draw bottom item badly */
928 MEM_freeN(item_array);
931 else if (prop && RNA_property_type(prop) != PROP_ENUM) {
932 RNA_warning("%s.%s, not an enum type", RNA_struct_identifier(ptr.type), propname);
936 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
941 void uiItemsEnumO(uiLayout *layout, const char *opname, const char *propname)
943 uiItemsFullEnumO(layout, opname, propname, NULL, layout->root->opcontext, 0);
946 /* for use in cases where we have */
947 void uiItemEnumO_value(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
949 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
953 UI_OPERATOR_ERROR_RET(ot, opname, return );
955 WM_operator_properties_create_ptr(&ptr, ot);
958 if ((prop = RNA_struct_find_property(&ptr, propname))) {
962 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
966 RNA_property_enum_set(&ptr, prop, value);
968 /* same as uiItemEnumO */
970 name = ui_menu_enumpropname(layout, &ptr, prop, value);
972 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
975 void uiItemEnumO_string(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, const char *value_str)
977 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
981 EnumPropertyItem *item;
984 UI_OPERATOR_ERROR_RET(ot, opname, return );
986 WM_operator_properties_create_ptr(&ptr, ot);
989 if ((prop = RNA_struct_find_property(&ptr, propname))) {
990 /* no need for translations here */
991 RNA_property_enum_items(layout->root->block->evil_C, &ptr, prop, &item, NULL, &free);
992 if (item == NULL || RNA_enum_value_from_id(item, value_str, &value) == 0) {
996 RNA_warning("%s.%s, enum %s not found", RNA_struct_identifier(ptr.type), propname, value_str);
1005 RNA_warning("%s.%s not found", RNA_struct_identifier(ptr.type), propname);
1009 RNA_property_enum_set(&ptr, prop, value);
1011 /* same as uiItemEnumO */
1013 name = ui_menu_enumpropname(layout, &ptr, prop, value);
1015 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1018 void uiItemBooleanO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
1020 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1023 UI_OPERATOR_ERROR_RET(ot, opname, return );
1025 WM_operator_properties_create_ptr(&ptr, ot);
1026 RNA_boolean_set(&ptr, propname, value);
1028 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1031 void uiItemIntO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, int value)
1033 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1036 UI_OPERATOR_ERROR_RET(ot, opname, return );
1038 WM_operator_properties_create_ptr(&ptr, ot);
1039 RNA_int_set(&ptr, propname, value);
1041 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1044 void uiItemFloatO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, float value)
1046 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1049 UI_OPERATOR_ERROR_RET(ot, opname, return );
1051 WM_operator_properties_create_ptr(&ptr, ot);
1052 RNA_float_set(&ptr, propname, value);
1054 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1057 void uiItemStringO(uiLayout *layout, const char *name, int icon, const char *opname, const char *propname, const char *value)
1059 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1062 UI_OPERATOR_ERROR_RET(ot, opname, return );
1064 WM_operator_properties_create_ptr(&ptr, ot);
1065 RNA_string_set(&ptr, propname, value);
1067 uiItemFullO_ptr(layout, ot, name, icon, ptr.data, layout->root->opcontext, 0);
1070 void uiItemO(uiLayout *layout, const char *name, int icon, const char *opname)
1072 uiItemFullO(layout, opname, name, icon, NULL, layout->root->opcontext, 0);
1075 /* RNA property items */
1077 static void ui_item_rna_size(uiLayout *layout, const char *name, int icon, PointerRNA *ptr, PropertyRNA *prop,
1078 int index, int icon_only, int *r_w, int *r_h)
1081 PropertySubType subtype;
1084 /* arbitrary extended width by type */
1085 type = RNA_property_type(prop);
1086 subtype = RNA_property_subtype(prop);
1087 len = RNA_property_array_length(ptr, prop);
1089 if (!name[0] && !icon_only) {
1090 if (ELEM(type, PROP_STRING, PROP_POINTER)) {
1091 name = "non-empty text";
1093 else if (type == PROP_BOOLEAN) {
1096 else if (type == PROP_ENUM) {
1097 /* Find the longest enum item name, instead of using a dummy text! */
1098 EnumPropertyItem *item, *item_array;
1101 RNA_property_enum_items_gettexted(layout->root->block->evil_C, ptr, prop, &item_array, NULL, &free);
1102 for (item = item_array; item->identifier; item++) {
1103 if (item->identifier[0]) {
1104 w = max_ii(w, ui_text_icon_width(layout, item->name, icon, 0));
1108 MEM_freeN(item_array);
1114 w = ui_text_icon_width(layout, name, icon, 0);
1117 /* increase height for arrays */
1118 if (index == RNA_NO_INDEX && len > 0) {
1119 if (!name[0] && icon == ICON_NONE)
1122 if (ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER))
1124 else if (subtype == PROP_MATRIX)
1125 h += ceil(sqrt(len)) * UI_UNIT_Y;
1127 h += len * UI_UNIT_Y;
1129 else if (ui_layout_vary_direction(layout) == UI_ITEM_VARY_X) {
1130 if (type == PROP_BOOLEAN && name[0])
1132 else if (type == PROP_ENUM)
1134 else if (type == PROP_FLOAT || type == PROP_INT)
1142 void uiItemFullR(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int index, int value, int flag, const char *name, int icon)
1144 uiBlock *block = layout->root->block;
1147 char namestr[UI_MAX_NAME_STR];
1148 int len, is_array, w, h, slider, toggle, expand, icon_only, no_bg;
1150 uiBlockSetCurLayout(block, layout);
1153 type = RNA_property_type(prop);
1154 is_array = RNA_property_array_check(prop);
1155 len = (is_array) ? RNA_property_array_length(ptr, prop) : 0;
1157 /* set name and icon */
1159 name = RNA_property_ui_name(prop);
1160 if (icon == ICON_NONE)
1161 icon = RNA_property_ui_icon(prop);
1163 if (ELEM4(type, PROP_INT, PROP_FLOAT, PROP_STRING, PROP_POINTER))
1164 name = ui_item_name_add_colon(name, namestr);
1165 else if (type == PROP_BOOLEAN && is_array && index == RNA_NO_INDEX)
1166 name = ui_item_name_add_colon(name, namestr);
1167 else if (type == PROP_ENUM && index != RNA_ENUM_VALUE)
1168 name = ui_item_name_add_colon(name, namestr);
1170 if (layout->root->type == UI_LAYOUT_MENU) {
1171 if (type == PROP_BOOLEAN && ((is_array == FALSE) || (index != RNA_NO_INDEX))) {
1172 if (is_array) icon = (RNA_property_boolean_get_index(ptr, prop, index)) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1173 else icon = (RNA_property_boolean_get(ptr, prop)) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1175 else if (type == PROP_ENUM && index == RNA_ENUM_VALUE) {
1176 int enum_value = RNA_property_enum_get(ptr, prop);
1177 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1178 icon = (enum_value & value) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1181 icon = (enum_value == value) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
1186 slider = (flag & UI_ITEM_R_SLIDER);
1187 toggle = (flag & UI_ITEM_R_TOGGLE);
1188 expand = (flag & UI_ITEM_R_EXPAND);
1189 icon_only = (flag & UI_ITEM_R_ICON_ONLY);
1190 no_bg = (flag & UI_ITEM_R_NO_BG);
1193 ui_item_rna_size(layout, name, icon, ptr, prop, index, icon_only, &w, &h);
1196 uiBlockSetEmboss(block, UI_EMBOSSN);
1198 /* array property */
1199 if (index == RNA_NO_INDEX && is_array)
1200 ui_item_array(layout, block, name, icon, ptr, prop, len, 0, 0, w, h, expand, slider, toggle, icon_only);
1202 else if (type == PROP_ENUM && index == RNA_ENUM_VALUE) {
1203 if (icon && name[0] && !icon_only)
1204 uiDefIconTextButR_prop(block, ROW, 0, icon, name, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1206 uiDefIconButR_prop(block, ROW, 0, icon, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1208 uiDefButR_prop(block, ROW, 0, name, 0, 0, w, h, ptr, prop, -1, 0, value, -1, -1, NULL);
1211 else if (type == PROP_ENUM && (expand || RNA_property_flag(prop) & PROP_ENUM_FLAG))
1212 ui_item_enum_expand(layout, block, ptr, prop, name, h, icon_only);
1213 /* property with separate label */
1214 else if (type == PROP_ENUM || type == PROP_STRING || type == PROP_POINTER) {
1215 but = ui_item_with_label(layout, block, name, icon, ptr, prop, index, 0, 0, w, h, flag);
1216 ui_but_add_search(but, ptr, prop, NULL, NULL);
1218 if (layout->redalert)
1219 uiButSetFlag(but, UI_BUT_REDALERT);
1223 but = uiDefAutoButR(block, ptr, prop, index, name, icon, 0, 0, w, h);
1225 if (slider && but->type == NUM)
1228 if (toggle && but->type == OPTION)
1231 if (layout->redalert)
1232 uiButSetFlag(but, UI_BUT_REDALERT);
1236 uiBlockSetEmboss(block, UI_EMBOSS);
1239 void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
1241 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1244 ui_item_disabled(layout, propname);
1245 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1249 uiItemFullR(layout, ptr, prop, RNA_NO_INDEX, 0, flag, name, icon);
1252 void uiItemEnumR(uiLayout *layout, const char *name, int icon, struct PointerRNA *ptr, const char *propname, int value)
1254 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1256 if (!prop || RNA_property_type(prop) != PROP_ENUM) {
1257 ui_item_disabled(layout, propname);
1258 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1262 uiItemFullR(layout, ptr, prop, RNA_ENUM_VALUE, value, 0, name, icon);
1265 void uiItemEnumR_string(uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *value, const char *name, int icon)
1267 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1268 EnumPropertyItem *item;
1269 int ivalue, a, free;
1271 if (!prop || RNA_property_type(prop) != PROP_ENUM) {
1272 ui_item_disabled(layout, propname);
1273 RNA_warning("enum property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1277 RNA_property_enum_items(layout->root->block->evil_C, ptr, prop, &item, NULL, &free);
1279 if (!RNA_enum_value_from_id(item, value, &ivalue)) {
1283 ui_item_disabled(layout, propname);
1284 RNA_warning("enum property value not found: %s", value);
1288 for (a = 0; item[a].identifier; a++) {
1289 if (item[a].value == ivalue) {
1290 const char *item_name = CTX_IFACE_(RNA_property_translation_context(prop), item[a].name);
1292 uiItemFullR(layout, ptr, prop, RNA_ENUM_VALUE, ivalue, 0, item_name ? item_name : name, icon ? icon : item[a].icon);
1302 void uiItemsEnumR(uiLayout *layout, struct PointerRNA *ptr, const char *propname)
1305 uiBlock *block = layout->root->block;
1308 prop = RNA_struct_find_property(ptr, propname);
1311 ui_item_disabled(layout, propname);
1312 RNA_warning("enum property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1316 if (RNA_property_type(prop) != PROP_ENUM) {
1317 RNA_warning("not an enum property: %s.%s", RNA_struct_identifier(ptr->type), propname);
1321 EnumPropertyItem *item;
1322 int totitem, i, free;
1323 uiLayout *split = uiLayoutSplit(layout, 0.0f, FALSE);
1324 uiLayout *column = uiLayoutColumn(split, FALSE);
1326 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item, &totitem, &free);
1328 for (i = 0; i < totitem; i++) {
1329 if (item[i].identifier[0]) {
1330 uiItemEnumR(column, item[i].name, ICON_NONE, ptr, propname, item[i].value);
1331 ui_but_tip_from_enum_item(block->buttons.last, &item[i]);
1336 column = uiLayoutColumn(split, FALSE);
1337 /* inconsistent, but menus with labels do not look good flipped */
1338 block->flag |= UI_BLOCK_NO_FLIP;
1341 uiItemL(column, item[i].name, ICON_NONE);
1342 bt = block->buttons.last;
1343 bt->drawflag = UI_BUT_TEXT_LEFT;
1345 ui_but_tip_from_enum_item(bt, &item[i]);
1358 /* Pointer RNA button with search */
1360 typedef struct CollItemSearch {
1361 struct CollItemSearch *next, *prev;
1367 static int sort_search_items_list(void *a, void *b)
1369 CollItemSearch *cis1 = (CollItemSearch *)a;
1370 CollItemSearch *cis2 = (CollItemSearch *)b;
1372 if (BLI_strcasecmp(cis1->name, cis2->name) > 0)
1378 static void rna_search_cb(const struct bContext *C, void *arg_but, const char *str, uiSearchItems *items)
1380 uiBut *but = arg_but;
1382 int i = 0, iconid = 0, flag = RNA_property_flag(but->rnaprop);
1383 ListBase *items_list = MEM_callocN(sizeof(ListBase), "items_list");
1384 CollItemSearch *cis;
1385 const int skip_filter = !but->changed;
1387 /* build a temporary list of relevant items first */
1388 RNA_PROP_BEGIN (&but->rnasearchpoin, itemptr, but->rnasearchprop)
1390 if (flag & PROP_ID_SELF_CHECK)
1391 if (itemptr.data == but->rnapoin.id.data)
1395 if (RNA_property_type(but->rnaprop) == PROP_POINTER) {
1396 if (RNA_property_pointer_poll(&but->rnapoin, but->rnaprop, &itemptr) == 0)
1400 if (itemptr.type && RNA_struct_is_ID(itemptr.type)) {
1401 ID *id = itemptr.data;
1402 char name_ui[MAX_ID_NAME];
1404 #if 0 /* this name is used for a string comparison and can't be modified, TODO */
1405 /* if ever enabled, make name_ui be MAX_ID_NAME+1 */
1406 name_uiprefix_id(name_ui, id);
1408 BLI_strncpy(name_ui, id->name + 2, sizeof(name_ui));
1410 name = BLI_strdup(name_ui);
1411 iconid = ui_id_icon_get((bContext *)C, id, false);
1414 name = RNA_struct_name_get_alloc(&itemptr, NULL, 0, NULL); /* could use the string length here */
1419 if (skip_filter || BLI_strcasestr(name, str)) {
1420 cis = MEM_callocN(sizeof(CollItemSearch), "CollectionItemSearch");
1421 cis->name = MEM_dupallocN(name);
1423 cis->iconid = iconid;
1424 BLI_addtail(items_list, cis);
1433 BLI_sortlist(items_list, sort_search_items_list);
1435 /* add search items from temporary list */
1436 for (cis = items_list->first; cis; cis = cis->next) {
1437 if (false == uiSearchItemAdd(items, cis->name, SET_INT_IN_POINTER(cis->index), cis->iconid)) {
1442 for (cis = items_list->first; cis; cis = cis->next) {
1443 MEM_freeN(cis->name);
1445 BLI_freelistN(items_list);
1446 MEM_freeN(items_list);
1449 static void search_id_collection(StructRNA *ptype, PointerRNA *ptr, PropertyRNA **prop)
1453 /* look for collection property in Main */
1454 RNA_main_pointer_create(G.main, ptr);
1458 RNA_STRUCT_BEGIN (ptr, iprop)
1460 /* if it's a collection and has same pointer type, we've got it */
1461 if (RNA_property_type(iprop) == PROP_COLLECTION) {
1462 srna = RNA_property_pointer_type(ptr, iprop);
1464 if (ptype == srna) {
1473 void ui_but_add_search(uiBut *but, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop)
1478 /* for ID's we do automatic lookup */
1480 if (RNA_property_type(prop) == PROP_POINTER) {
1481 ptype = RNA_property_pointer_type(ptr, prop);
1482 search_id_collection(ptype, &sptr, &searchprop);
1487 /* turn button into search button */
1489 but->type = RNA_property_is_unlink(prop) ? SEARCH_MENU_UNLINK : SEARCH_MENU;
1490 but->hardmax = MAX2(but->hardmax, 256.0f);
1491 but->rnasearchpoin = *searchptr;
1492 but->rnasearchprop = searchprop;
1493 but->drawflag |= UI_BUT_ICON_LEFT | UI_BUT_TEXT_LEFT;
1495 if (RNA_property_type(prop) == PROP_ENUM) {
1496 /* XXX, this will have a menu string,
1497 * but in this case we just want the text */
1501 uiButSetSearchFunc(but, rna_search_cb, but, NULL, NULL);
1505 void uiItemPointerR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, struct PointerRNA *searchptr, const char *searchpropname, const char *name, int icon)
1507 PropertyRNA *prop, *searchprop;
1511 StructRNA *icontype;
1513 char namestr[UI_MAX_NAME_STR];
1515 /* validate arguments */
1516 prop = RNA_struct_find_property(ptr, propname);
1519 RNA_warning("property not found: %s.%s",
1520 RNA_struct_identifier(ptr->type), propname);
1524 type = RNA_property_type(prop);
1525 if (!ELEM3(type, PROP_POINTER, PROP_STRING, PROP_ENUM)) {
1526 RNA_warning("Property %s must be a pointer, string or enum", propname);
1530 searchprop = RNA_struct_find_property(searchptr, searchpropname);
1534 RNA_warning("search collection property not found: %s.%s",
1535 RNA_struct_identifier(searchptr->type), searchpropname);
1538 else if (RNA_property_type(searchprop) != PROP_COLLECTION) {
1539 RNA_warning("search collection property is not a collection type: %s.%s",
1540 RNA_struct_identifier(searchptr->type), searchpropname);
1544 /* get icon & name */
1545 if (icon == ICON_NONE) {
1546 if (type == PROP_POINTER)
1547 icontype = RNA_property_pointer_type(ptr, prop);
1549 icontype = RNA_property_pointer_type(searchptr, searchprop);
1551 icon = RNA_struct_ui_icon(icontype);
1554 name = RNA_property_ui_name(prop);
1556 name = ui_item_name_add_colon(name, namestr);
1559 block = uiLayoutGetBlock(layout);
1561 ui_item_rna_size(layout, name, icon, ptr, prop, 0, 0, &w, &h);
1562 w += UI_UNIT_X; /* X icon needs more space */
1563 but = ui_item_with_label(layout, block, name, icon, ptr, prop, 0, 0, 0, w, h, 0);
1565 ui_but_add_search(but, ptr, prop, searchptr, searchprop);
1569 static void ui_item_menutype_func(bContext *C, uiLayout *layout, void *arg_mt)
1571 MenuType *mt = (MenuType *)arg_mt;
1575 menu.layout = layout;
1577 if (G.debug & G_DEBUG_WM) {
1578 printf("%s: opening menu \"%s\"\n", __func__, mt->idname);
1581 if (layout->context)
1582 CTX_store_set(C, layout->context);
1586 if (layout->context)
1587 CTX_store_set(C, NULL);
1590 static void ui_item_menu(uiLayout *layout, const char *name, int icon, uiMenuCreateFunc func, void *arg, void *argN,
1591 const char *tip, bool force_menu)
1593 uiBlock *block = layout->root->block;
1597 uiBlockSetCurLayout(block, layout);
1599 if (layout->root->type == UI_LAYOUT_HEADER)
1600 uiBlockSetEmboss(block, UI_EMBOSS);
1604 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1607 w = ui_text_icon_width(layout, name, icon, 1);
1610 if (layout->root->type == UI_LAYOUT_HEADER) { /* ugly .. */
1617 if (name[0] && icon)
1618 but = uiDefIconTextMenuBut(block, func, arg, icon, name, 0, 0, w, h, tip);
1620 but = uiDefIconMenuBut(block, func, arg, icon, 0, 0, w, h, tip);
1622 but = uiDefMenuBut(block, func, arg, name, 0, 0, w, h, tip);
1624 if (argN) { /* ugly .. */
1625 but->poin = (char *)but;
1626 but->func_argN = argN;
1629 if (layout->root->type == UI_LAYOUT_HEADER) {
1630 uiBlockSetEmboss(block, UI_EMBOSS);
1632 if (ELEM(layout->root->type, UI_LAYOUT_PANEL, UI_LAYOUT_TOOLBAR) ||
1633 (force_menu && layout->root->type != UI_LAYOUT_MENU)) /* We never want a dropdown in menu! */
1636 but->drawflag |= UI_BUT_TEXT_LEFT;
1640 void uiItemM(uiLayout *layout, bContext *UNUSED(C), const char *menuname, const char *name, int icon)
1644 mt = WM_menutype_find(menuname, FALSE);
1647 RNA_warning("not found %s", menuname);
1652 name = CTX_IFACE_(mt->translation_context, mt->label);
1655 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1658 ui_item_menu(layout, name, icon, ui_item_menutype_func, mt, NULL, TIP_(mt->description), false);
1662 static uiBut *uiItemL_(uiLayout *layout, const char *name, int icon)
1664 uiBlock *block = layout->root->block;
1668 uiBlockSetCurLayout(block, layout);
1672 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1675 w = ui_text_icon_width(layout, name, icon, 0);
1677 if (icon && name[0])
1678 but = uiDefIconTextBut(block, LABEL, 0, icon, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1680 but = uiDefIconBut(block, LABEL, 0, icon, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1682 but = uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1684 /* to compensate for string size padding in ui_text_icon_width,
1685 * make text aligned right if the layout is aligned right.
1687 if (uiLayoutGetAlignment(layout) == UI_LAYOUT_ALIGN_RIGHT) {
1688 but->drawflag &= ~UI_BUT_TEXT_LEFT; /* default, needs to be unset */
1689 but->drawflag |= UI_BUT_TEXT_RIGHT;
1692 /* Mark as a label inside a listbox. */
1693 if (block->flag & UI_BLOCK_LIST_ITEM) {
1694 but->flag |= UI_BUT_LIST_ITEM;
1700 void uiItemL(uiLayout *layout, const char *name, int icon)
1702 uiItemL_(layout, name, icon);
1705 void uiItemLDrag(uiLayout *layout, PointerRNA *ptr, const char *name, int icon)
1707 uiBut *but = uiItemL_(layout, name, icon);
1709 if (ptr && ptr->type)
1710 if (RNA_struct_is_ID(ptr->type))
1711 uiButSetDragID(but, ptr->id.data);
1716 void uiItemV(uiLayout *layout, const char *name, int icon, int argval)
1719 uiBlock *block = layout->root->block;
1720 float *retvalue = (block->handle) ? &block->handle->retvalue : NULL;
1723 uiBlockSetCurLayout(block, layout);
1727 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1730 w = ui_text_icon_width(layout, name, icon, 0);
1732 if (icon && name[0])
1733 uiDefIconTextButF(block, BUT, argval, icon, name, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1735 uiDefIconButF(block, BUT, argval, icon, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1737 uiDefButF(block, BUT, argval, name, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, -1, "");
1740 /* separator item */
1741 void uiItemS(uiLayout *layout)
1743 uiBlock *block = layout->root->block;
1745 uiBlockSetCurLayout(block, layout);
1746 uiDefBut(block, SEPR, 0, "", 0, 0, 0.3f * UI_UNIT_X, 0.3f * UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1750 void uiItemMenuF(uiLayout *layout, const char *name, int icon, uiMenuCreateFunc func, void *arg)
1755 ui_item_menu(layout, name, icon, func, arg, NULL, "", false);
1758 typedef struct MenuItemLevel {
1760 /* don't use pointers to the strings because python can dynamically
1761 * allocate strings and free before the menu draws, see [#27304] */
1762 char opname[OP_MAX_TYPENAME];
1763 char propname[MAX_IDPROP_NAME];
1767 static void menu_item_enum_opname_menu(bContext *UNUSED(C), uiLayout *layout, void *arg)
1769 MenuItemLevel *lvl = (MenuItemLevel *)(((uiBut *)arg)->func_argN);
1771 uiLayoutSetOperatorContext(layout, lvl->opcontext);
1772 uiItemsEnumO(layout, lvl->opname, lvl->propname);
1775 void uiItemMenuEnumO(uiLayout *layout, bContext *C, const char *opname, const char *propname, const char *name, int icon)
1777 wmOperatorType *ot = WM_operatortype_find(opname, 0); /* print error next */
1779 char namestr_buf[UI_MAX_NAME_STR], keybuf[128];
1780 char *namestr = namestr_buf;
1782 UI_OPERATOR_ERROR_RET(ot, opname, return );
1785 ui_item_disabled(layout, opname);
1786 RNA_warning("operator missing srna '%s'", opname);
1791 namestr += BLI_strncpy_rlen(namestr, name, sizeof(namestr_buf));
1793 namestr += BLI_strncpy_rlen(namestr, RNA_struct_ui_name(ot->srna), sizeof(namestr_buf));
1795 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1798 lvl = MEM_callocN(sizeof(MenuItemLevel), "MenuItemLevel");
1799 BLI_strncpy(lvl->opname, opname, sizeof(lvl->opname));
1800 BLI_strncpy(lvl->propname, propname, sizeof(lvl->propname));
1801 lvl->opcontext = layout->root->opcontext;
1803 /* add hotkey here, lower UI code can't detect it */
1804 if (layout->root->block->flag & UI_BLOCK_LOOP) {
1805 if (ot->prop && ot->invoke &&
1806 WM_key_event_operator_string(C, ot->idname, layout->root->opcontext, NULL, false, keybuf, sizeof(keybuf)))
1808 namestr += BLI_snprintf(namestr, sizeof(namestr_buf) - (namestr - namestr_buf), "|%s", keybuf);
1812 ui_item_menu(layout, namestr_buf, icon, menu_item_enum_opname_menu, NULL, lvl, RNA_struct_ui_description(ot->srna),
1816 static void menu_item_enum_rna_menu(bContext *UNUSED(C), uiLayout *layout, void *arg)
1818 MenuItemLevel *lvl = (MenuItemLevel *)(((uiBut *)arg)->func_argN);
1820 uiLayoutSetOperatorContext(layout, lvl->opcontext);
1821 uiItemsEnumR(layout, &lvl->rnapoin, lvl->propname);
1824 void uiItemMenuEnumR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *name, int icon)
1829 prop = RNA_struct_find_property(ptr, propname);
1831 ui_item_disabled(layout, propname);
1832 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
1837 name = RNA_property_ui_name(prop);
1838 if (layout->root->type == UI_LAYOUT_MENU && !icon)
1841 lvl = MEM_callocN(sizeof(MenuItemLevel), "MenuItemLevel");
1842 lvl->rnapoin = *ptr;
1843 BLI_strncpy(lvl->propname, propname, sizeof(lvl->propname));
1844 lvl->opcontext = layout->root->opcontext;
1846 ui_item_menu(layout, name, icon, menu_item_enum_rna_menu, NULL, lvl, RNA_property_description(prop), false);
1849 /**************************** Layout Items ***************************/
1851 /* single-row layout */
1852 static void ui_litem_estimate_row(uiLayout *litem)
1860 for (item = litem->items.first; item; item = item->next) {
1861 ui_item_size(item, &itemw, &itemh);
1864 litem->h = MAX2(itemh, litem->h);
1867 litem->w += litem->space;
1871 static int ui_litem_min_width(int itemw)
1873 return MIN2(2 * UI_UNIT_X, itemw);
1876 static void ui_litem_layout_row(uiLayout *litem)
1879 int x, y, w, tot, totw, neww, itemw, minw, itemh, offset;
1880 int fixedw, freew, fixedx, freex, flag = 0, lastw = 0;
1882 /* x = litem->x; */ /* UNUSED */
1888 for (item = litem->items.first; item; item = item->next) {
1889 ui_item_size(item, &itemw, &itemh);
1898 w -= (tot - 1) * litem->space;
1901 /* keep clamping items to fixed minimum size until all are done */
1907 for (item = litem->items.first; item; item = item->next) {
1911 ui_item_size(item, &itemw, &itemh);
1912 minw = ui_litem_min_width(itemw);
1915 neww = ui_item_fit(itemw, x, totw, w - lastw, !item->next, litem->alignment, NULL);
1917 neww = 0; /* no space left, all will need clamping to minimum size */
1921 if ((neww < minw || itemw == minw) && w != 0) {
1929 /* keep free size */
1942 for (item = litem->items.first; item; item = item->next) {
1943 ui_item_size(item, &itemw, &itemh);
1944 minw = ui_litem_min_width(itemw);
1947 /* fixed minimum size items */
1948 itemw = ui_item_fit(minw, fixedx, fixedw, min_ii(w, fixedw), !item->next, litem->alignment, NULL);
1952 /* free size item */
1953 itemw = ui_item_fit(itemw, freex, freew, w - fixedw, !item->next, litem->alignment, NULL);
1957 /* align right/center */
1959 if (litem->alignment == UI_LAYOUT_ALIGN_RIGHT) {
1960 if (freew + fixedw > 0 && freew + fixedw < w)
1961 offset = w - (fixedw + freew);
1963 else if (litem->alignment == UI_LAYOUT_ALIGN_CENTER) {
1964 if (freew + fixedw > 0 && freew + fixedw < w)
1965 offset = (w - (fixedw + freew)) / 2;
1969 ui_item_position(item, x + offset, y - itemh, itemw, itemh);
1976 litem->w = x - litem->x;
1977 litem->h = litem->y - y;
1982 /* single-column layout */
1983 static void ui_litem_estimate_column(uiLayout *litem)
1991 for (item = litem->items.first; item; item = item->next) {
1992 ui_item_size(item, &itemw, &itemh);
1994 litem->w = MAX2(litem->w, itemw);
1998 litem->h += litem->space;
2002 static void ui_litem_layout_column(uiLayout *litem)
2010 for (item = litem->items.first; item; item = item->next) {
2011 ui_item_size(item, NULL, &itemh);
2014 ui_item_position(item, x, y, litem->w, itemh);
2020 litem->h = litem->y - y;
2026 static void ui_litem_estimate_root(uiLayout *UNUSED(litem))
2031 static void ui_litem_layout_root(uiLayout *litem)
2033 if (litem->root->type == UI_LAYOUT_HEADER)
2034 ui_litem_layout_row(litem);
2036 ui_litem_layout_column(litem);
2040 static void ui_litem_estimate_box(uiLayout *litem)
2042 uiStyle *style = litem->root->style;
2044 ui_litem_estimate_column(litem);
2045 litem->w += 2 * style->boxspace;
2046 litem->h += style->boxspace;
2049 static void ui_litem_layout_box(uiLayout *litem)
2051 uiLayoutItemBx *box = (uiLayoutItemBx *)litem;
2052 uiStyle *style = litem->root->style;
2059 litem->x += style->boxspace;
2061 if (w != 0) litem->w -= 2 * style->boxspace;
2062 if (h != 0) litem->h -= 2 * style->boxspace;
2064 ui_litem_layout_column(litem);
2066 litem->x -= style->boxspace;
2067 litem->y -= style->boxspace;
2069 if (w != 0) litem->w += 2 * style->boxspace;
2070 if (h != 0) litem->h += style->boxspace;
2072 /* roundbox around the sublayout */
2073 but = box->roundbox;
2074 but->rect.xmin = litem->x;
2075 but->rect.ymin = litem->y;
2076 but->rect.xmax = litem->x + litem->w;
2077 but->rect.ymax = litem->y + litem->h;
2080 /* multi-column layout, automatically flowing to the next */
2081 static void ui_litem_estimate_column_flow(uiLayout *litem)
2083 uiStyle *style = litem->root->style;
2084 uiLayoutItemFlow *flow = (uiLayoutItemFlow *)litem;
2086 int col, x, y, emh, emy, miny, itemw, itemh, maxw = 0;
2089 /* compute max needed width and total height */
2092 for (item = litem->items.first; item; item = item->next) {
2093 ui_item_size(item, &itemw, &itemh);
2094 maxw = MAX2(maxw, itemw);
2099 if (flow->number <= 0) {
2100 /* auto compute number of columns, not very good */
2106 flow->totcol = max_ii(litem->root->emw / maxw, 1);
2107 flow->totcol = min_ii(flow->totcol, totitem);
2110 flow->totcol = flow->number;
2119 emh = toth / flow->totcol;
2121 /* create column per column */
2123 for (item = litem->items.first; item; item = item->next) {
2124 ui_item_size(item, &itemw, &itemh);
2126 y -= itemh + style->buttonspacey;
2127 miny = min_ii(miny, y);
2129 maxw = max_ii(itemw, maxw);
2131 /* decide to go to next one */
2132 if (col < flow->totcol - 1 && emy <= -emh) {
2133 x += maxw + litem->space;
2136 emy = 0; /* need to reset height again for next column */
2142 litem->h = litem->y - miny;
2145 static void ui_litem_layout_column_flow(uiLayout *litem)
2147 uiStyle *style = litem->root->style;
2148 uiLayoutItemFlow *flow = (uiLayoutItemFlow *)litem;
2150 int col, x, y, w, emh, emy, miny, itemw, itemh;
2151 int toth, totitem, offset;
2153 /* compute max needed width and total height */
2156 for (item = litem->items.first; item; item = item->next) {
2157 ui_item_size(item, &itemw, &itemh);
2168 w = litem->w - (flow->totcol - 1) * style->columnspace;
2169 emh = toth / flow->totcol;
2171 /* create column per column */
2173 for (item = litem->items.first; item; item = item->next) {
2174 ui_item_size(item, NULL, &itemh);
2175 itemw = ui_item_fit(1, x - litem->x, flow->totcol, w, col == flow->totcol - 1, litem->alignment, &offset);
2179 ui_item_position(item, x + offset, y, itemw, itemh);
2180 y -= style->buttonspacey;
2181 miny = min_ii(miny, y);
2183 /* decide to go to next one */
2184 if (col < flow->totcol - 1 && emy <= -emh) {
2185 x += itemw + style->columnspace;
2187 emy = 0; /* need to reset height again for next column */
2192 litem->h = litem->y - miny;
2198 static void ui_litem_estimate_absolute(uiLayout *litem)
2201 int itemx, itemy, itemw, itemh, minx, miny;
2208 for (item = litem->items.first; item; item = item->next) {
2209 ui_item_offset(item, &itemx, &itemy);
2210 ui_item_size(item, &itemw, &itemh);
2212 minx = min_ii(minx, itemx);
2213 miny = min_ii(miny, itemy);
2215 litem->w = MAX2(litem->w, itemx + itemw);
2216 litem->h = MAX2(litem->h, itemy + itemh);
2223 static void ui_litem_layout_absolute(uiLayout *litem)
2226 float scalex = 1.0f, scaley = 1.0f;
2227 int x, y, newx, newy, itemx, itemy, itemh, itemw, minx, miny, totw, toth;
2234 for (item = litem->items.first; item; item = item->next) {
2235 ui_item_offset(item, &itemx, &itemy);
2236 ui_item_size(item, &itemw, &itemh);
2238 minx = min_ii(minx, itemx);
2239 miny = min_ii(miny, itemy);
2241 totw = max_ii(totw, itemx + itemw);
2242 toth = max_ii(toth, itemy + itemh);
2248 if (litem->w && totw > 0)
2249 scalex = (float)litem->w / (float)totw;
2250 if (litem->h && toth > 0)
2251 scaley = (float)litem->h / (float)toth;
2254 y = litem->y - scaley * toth;
2256 for (item = litem->items.first; item; item = item->next) {
2257 ui_item_offset(item, &itemx, &itemy);
2258 ui_item_size(item, &itemw, &itemh);
2260 if (scalex != 1.0f) {
2261 newx = (itemx - minx) * scalex;
2262 itemw = (itemx - minx + itemw) * scalex - newx;
2263 itemx = minx + newx;
2266 if (scaley != 1.0f) {
2267 newy = (itemy - miny) * scaley;
2268 itemh = (itemy - miny + itemh) * scaley - newy;
2269 itemy = miny + newy;
2272 ui_item_position(item, x + itemx - minx, y + itemy - miny, itemw, itemh);
2275 litem->w = scalex * totw;
2276 litem->h = litem->y - y;
2277 litem->x = x + litem->w;
2282 static void ui_litem_estimate_split(uiLayout *litem)
2284 ui_litem_estimate_row(litem);
2287 static void ui_litem_layout_split(uiLayout *litem)
2289 uiLayoutItemSplit *split = (uiLayoutItemSplit *)litem;
2292 const int tot = BLI_countlist(&litem->items);
2293 int itemh, x, y, w, colw = 0;
2301 percentage = (split->percentage == 0.0f) ? 1.0f / (float)tot : split->percentage;
2303 w = (litem->w - (tot - 1) * litem->space);
2304 colw = w * percentage;
2305 colw = MAX2(colw, 0);
2307 for (item = litem->items.first; item; item = item->next) {
2308 ui_item_size(item, NULL, &itemh);
2310 ui_item_position(item, x, y - itemh, colw, itemh);
2314 colw = (w - (int)(w * percentage)) / (tot - 1);
2315 colw = MAX2(colw, 0);
2321 litem->w = x - litem->x;
2322 litem->h = litem->y - y;
2327 /* overlap layout */
2328 static void ui_litem_estimate_overlap(uiLayout *litem)
2336 for (item = litem->items.first; item; item = item->next) {
2337 ui_item_size(item, &itemw, &itemh);
2339 litem->w = MAX2(itemw, litem->w);
2340 litem->h = MAX2(itemh, litem->h);
2344 static void ui_litem_layout_overlap(uiLayout *litem)
2347 int itemw, itemh, x, y;
2352 for (item = litem->items.first; item; item = item->next) {
2353 ui_item_size(item, &itemw, &itemh);
2354 ui_item_position(item, x, y - itemh, litem->w, itemh);
2356 litem->h = MAX2(litem->h, itemh);
2360 litem->y = y - litem->h;
2363 /* layout create functions */
2364 uiLayout *uiLayoutRow(uiLayout *layout, int align)
2368 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutRow");
2369 litem->item.type = ITEM_LAYOUT_ROW;
2370 litem->root = layout->root;
2371 litem->align = align;
2372 litem->active = true;
2373 litem->enabled = true;
2374 litem->context = layout->context;
2375 litem->space = (align) ? 0 : layout->root->style->buttonspacex;
2376 litem->redalert = layout->redalert;
2377 litem->w = layout->w;
2378 BLI_addtail(&layout->items, litem);
2380 uiBlockSetCurLayout(layout->root->block, litem);
2385 uiLayout *uiLayoutColumn(uiLayout *layout, int align)
2389 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutColumn");
2390 litem->item.type = ITEM_LAYOUT_COLUMN;
2391 litem->root = layout->root;
2392 litem->align = align;
2393 litem->active = true;
2394 litem->enabled = true;
2395 litem->context = layout->context;
2396 litem->space = (litem->align) ? 0 : layout->root->style->buttonspacey;
2397 litem->redalert = layout->redalert;
2398 litem->w = layout->w;
2399 BLI_addtail(&layout->items, litem);
2401 uiBlockSetCurLayout(layout->root->block, litem);
2406 uiLayout *uiLayoutColumnFlow(uiLayout *layout, int number, int align)
2408 uiLayoutItemFlow *flow;
2410 flow = MEM_callocN(sizeof(uiLayoutItemFlow), "uiLayoutItemFlow");
2411 flow->litem.item.type = ITEM_LAYOUT_COLUMN_FLOW;
2412 flow->litem.root = layout->root;
2413 flow->litem.align = align;
2414 flow->litem.active = true;
2415 flow->litem.enabled = true;
2416 flow->litem.context = layout->context;
2417 flow->litem.space = (flow->litem.align) ? 0 : layout->root->style->columnspace;
2418 flow->litem.redalert = layout->redalert;
2419 flow->litem.w = layout->w;
2420 flow->number = number;
2421 BLI_addtail(&layout->items, flow);
2423 uiBlockSetCurLayout(layout->root->block, &flow->litem);
2425 return &flow->litem;
2428 static uiLayoutItemBx *ui_layout_box(uiLayout *layout, int type)
2430 uiLayoutItemBx *box;
2432 box = MEM_callocN(sizeof(uiLayoutItemBx), "uiLayoutItemBx");
2433 box->litem.item.type = ITEM_LAYOUT_BOX;
2434 box->litem.root = layout->root;
2435 box->litem.active = 1;
2436 box->litem.enabled = 1;
2437 box->litem.context = layout->context;
2438 box->litem.space = layout->root->style->columnspace;
2439 box->litem.redalert = layout->redalert;
2440 box->litem.w = layout->w;
2441 BLI_addtail(&layout->items, box);
2443 uiBlockSetCurLayout(layout->root->block, &box->litem);
2445 box->roundbox = uiDefBut(layout->root->block, type, 0, "", 0, 0, 0, 0, NULL, 0.0, 0.0, 0, 0, "");
2450 uiLayout *uiLayoutBox(uiLayout *layout)
2452 return (uiLayout *)ui_layout_box(layout, ROUNDBOX);
2455 /* Check all buttons defined in this layout, and set labels as active/selected.
2456 * Needed to handle correctly text colors of list items. */
2457 void ui_layout_list_set_labels_active(uiLayout *layout)
2459 uiButtonItem *bitem;
2460 for (bitem = layout->items.first; bitem; bitem = bitem->item.next) {
2461 if (bitem->item.type != ITEM_BUTTON) {
2462 ui_layout_list_set_labels_active((uiLayout *)(&bitem->item));
2464 else if (bitem->but->flag & UI_BUT_LIST_ITEM) {
2465 uiButSetFlag(bitem->but, UI_SELECT);
2470 uiLayout *uiLayoutListBox(uiLayout *layout, uiList *ui_list, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *actptr,
2471 PropertyRNA *actprop)
2473 uiLayoutItemBx *box = ui_layout_box(layout, LISTBOX);
2474 uiBut *but = box->roundbox;
2476 but->custom_data = ui_list;
2478 but->rnasearchpoin = *ptr;
2479 but->rnasearchprop = prop;
2480 but->rnapoin = *actptr;
2481 but->rnaprop = actprop;
2483 /* Resizing data. */
2484 /* Note: we can't use usual "num button" value handling, as it only tries rnapoin when it is non-NULL... :/
2485 * So just setting but->poin, not but->pointype.
2487 but->poin = (void *)&ui_list->list_grip;
2488 but->hardmin = but->softmin = 0.0f;
2489 but->hardmax = but->softmax = 1000.0f; /* Should be more than enough! */
2492 /* only for the undo string */
2493 if (but->flag & UI_BUT_UNDO) {
2494 but->tip = RNA_property_description(actprop);
2497 return (uiLayout *)box;
2500 uiLayout *uiLayoutAbsolute(uiLayout *layout, int align)
2504 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutAbsolute");
2505 litem->item.type = ITEM_LAYOUT_ABSOLUTE;
2506 litem->root = layout->root;
2507 litem->align = align;
2510 litem->context = layout->context;
2511 litem->redalert = layout->redalert;
2512 BLI_addtail(&layout->items, litem);
2514 uiBlockSetCurLayout(layout->root->block, litem);
2519 uiBlock *uiLayoutAbsoluteBlock(uiLayout *layout)
2523 block = uiLayoutGetBlock(layout);
2524 uiLayoutAbsolute(layout, FALSE);
2529 uiLayout *uiLayoutOverlap(uiLayout *layout)
2533 litem = MEM_callocN(sizeof(uiLayout), "uiLayoutOverlap");
2534 litem->item.type = ITEM_LAYOUT_OVERLAP;
2535 litem->root = layout->root;
2536 litem->active = true;
2537 litem->enabled = true;
2538 litem->context = layout->context;
2539 litem->redalert = layout->redalert;
2540 BLI_addtail(&layout->items, litem);
2542 uiBlockSetCurLayout(layout->root->block, litem);
2547 uiLayout *uiLayoutSplit(uiLayout *layout, float percentage, int align)
2549 uiLayoutItemSplit *split;
2551 split = MEM_callocN(sizeof(uiLayoutItemSplit), "uiLayoutItemSplit");
2552 split->litem.item.type = ITEM_LAYOUT_SPLIT;
2553 split->litem.root = layout->root;
2554 split->litem.align = align;
2555 split->litem.active = true;
2556 split->litem.enabled = true;
2557 split->litem.context = layout->context;
2558 split->litem.space = layout->root->style->columnspace;
2559 split->litem.redalert = layout->redalert;
2560 split->litem.w = layout->w;
2561 split->percentage = percentage;
2562 BLI_addtail(&layout->items, split);
2564 uiBlockSetCurLayout(layout->root->block, &split->litem);
2566 return &split->litem;
2569 void uiLayoutSetActive(uiLayout *layout, bool active)
2571 layout->active = active;
2574 void uiLayoutSetEnabled(uiLayout *layout, bool enabled)
2576 layout->enabled = enabled;
2579 void uiLayoutSetRedAlert(uiLayout *layout, bool redalert)
2581 layout->redalert = redalert;
2584 void uiLayoutSetKeepAspect(uiLayout *layout, bool keepaspect)
2586 layout->keepaspect = keepaspect;
2589 void uiLayoutSetAlignment(uiLayout *layout, char alignment)
2591 layout->alignment = alignment;
2594 void uiLayoutSetScaleX(uiLayout *layout, float scale)
2596 layout->scale[0] = scale;
2599 void uiLayoutSetScaleY(uiLayout *layout, float scale)
2601 layout->scale[1] = scale;
2604 bool uiLayoutGetActive(uiLayout *layout)
2606 return layout->active;
2609 bool uiLayoutGetEnabled(uiLayout *layout)
2611 return layout->enabled;
2614 bool uiLayoutGetRedAlert(uiLayout *layout)
2616 return layout->redalert;
2619 bool uiLayoutGetKeepAspect(uiLayout *layout)
2621 return layout->keepaspect;
2624 int uiLayoutGetAlignment(uiLayout *layout)
2626 return layout->alignment;
2629 int uiLayoutGetWidth(uiLayout *layout)
2634 float uiLayoutGetScaleX(uiLayout *layout)
2636 return layout->scale[0];
2639 float uiLayoutGetScaleY(uiLayout *layout)
2641 return layout->scale[1];
2644 /********************** Layout *******************/
2646 static void ui_item_scale(uiLayout *litem, const float scale[2])
2651 for (item = litem->items.last; item; item = item->prev) {
2652 ui_item_size(item, &w, &h);
2653 ui_item_offset(item, &x, &y);
2655 if (scale[0] != 0.0f) {
2660 if (scale[1] != 0.0f) {
2665 ui_item_position(item, x, y, w, h);
2669 static void ui_item_estimate(uiItem *item)
2673 if (item->type != ITEM_BUTTON) {
2674 uiLayout *litem = (uiLayout *)item;
2676 for (subitem = litem->items.first; subitem; subitem = subitem->next)
2677 ui_item_estimate(subitem);
2679 if (litem->items.first == NULL)
2682 if (litem->scale[0] != 0.0f || litem->scale[1] != 0.0f)
2683 ui_item_scale(litem, litem->scale);
2685 switch (litem->item.type) {
2686 case ITEM_LAYOUT_COLUMN:
2687 ui_litem_estimate_column(litem);
2689 case ITEM_LAYOUT_COLUMN_FLOW:
2690 ui_litem_estimate_column_flow(litem);
2692 case ITEM_LAYOUT_ROW:
2693 ui_litem_estimate_row(litem);
2695 case ITEM_LAYOUT_BOX:
2696 ui_litem_estimate_box(litem);
2698 case ITEM_LAYOUT_ROOT:
2699 ui_litem_estimate_root(litem);
2701 case ITEM_LAYOUT_ABSOLUTE:
2702 ui_litem_estimate_absolute(litem);
2704 case ITEM_LAYOUT_SPLIT:
2705 ui_litem_estimate_split(litem);
2707 case ITEM_LAYOUT_OVERLAP:
2708 ui_litem_estimate_overlap(litem);
2716 static void ui_item_align(uiLayout *litem, short nr)
2719 uiButtonItem *bitem;
2720 uiLayoutItemBx *box;
2722 for (item = litem->items.last; item; item = item->prev) {
2723 if (item->type == ITEM_BUTTON) {
2724 bitem = (uiButtonItem *)item;
2725 if (ui_but_can_align(bitem->but))
2726 if (!bitem->but->alignnr)
2727 bitem->but->alignnr = nr;
2729 else if (item->type == ITEM_LAYOUT_ABSOLUTE) {
2732 else if (item->type == ITEM_LAYOUT_OVERLAP) {
2735 else if (item->type == ITEM_LAYOUT_BOX) {
2736 box = (uiLayoutItemBx *)item;
2737 box->roundbox->alignnr = nr;
2738 BLI_remlink(&litem->root->block->buttons, box->roundbox);
2739 BLI_addhead(&litem->root->block->buttons, box->roundbox);
2741 else if (((uiLayout *)item)->align) {
2742 ui_item_align((uiLayout *)item, nr);
2747 static void ui_item_flag(uiLayout *litem, int flag)
2750 uiButtonItem *bitem;
2752 for (item = litem->items.last; item; item = item->prev) {
2753 if (item->type == ITEM_BUTTON) {
2754 bitem = (uiButtonItem *)item;
2755 bitem->but->flag |= flag;
2758 ui_item_flag((uiLayout *)item, flag);
2762 static void ui_item_layout(uiItem *item)
2766 if (item->type != ITEM_BUTTON) {
2767 uiLayout *litem = (uiLayout *)item;
2769 if (litem->items.first == NULL)
2773 ui_item_align(litem, ++litem->root->block->alignnr);
2775 ui_item_flag(litem, UI_BUT_INACTIVE);
2776 if (!litem->enabled)
2777 ui_item_flag(litem, UI_BUT_DISABLED);
2779 switch (litem->item.type) {
2780 case ITEM_LAYOUT_COLUMN:
2781 ui_litem_layout_column(litem);
2783 case ITEM_LAYOUT_COLUMN_FLOW:
2784 ui_litem_layout_column_flow(litem);
2786 case ITEM_LAYOUT_ROW:
2787 ui_litem_layout_row(litem);
2789 case ITEM_LAYOUT_BOX:
2790 ui_litem_layout_box(litem);
2792 case ITEM_LAYOUT_ROOT:
2793 ui_litem_layout_root(litem);
2795 case ITEM_LAYOUT_ABSOLUTE:
2796 ui_litem_layout_absolute(litem);
2798 case ITEM_LAYOUT_SPLIT:
2799 ui_litem_layout_split(litem);
2801 case ITEM_LAYOUT_OVERLAP:
2802 ui_litem_layout_overlap(litem);
2808 for (subitem = litem->items.first; subitem; subitem = subitem->next)
2809 ui_item_layout(subitem);
2813 static void ui_layout_end(uiBlock *block, uiLayout *layout, int *x, int *y)
2815 if (layout->root->handlefunc)
2816 uiBlockSetHandleFunc(block, layout->root->handlefunc, layout->root->argv);
2818 ui_item_estimate(&layout->item);
2819 ui_item_layout(&layout->item);
2821 if (x) *x = layout->x;
2822 if (y) *y = layout->y;
2825 static void ui_layout_free(uiLayout *layout)
2827 uiItem *item, *next;
2829 for (item = layout->items.first; item; item = next) {
2832 if (item->type == ITEM_BUTTON)
2835 ui_layout_free((uiLayout *)item);
2841 uiLayout *uiBlockLayout(uiBlock *block, int dir, int type, int x, int y, int size, int em, uiStyle *style)
2846 root = MEM_callocN(sizeof(uiLayoutRoot), "uiLayoutRoot");
2848 root->style = style;
2849 root->block = block;
2850 root->opcontext = WM_OP_INVOKE_REGION_WIN;
2852 layout = MEM_callocN(sizeof(uiLayout), "uiLayout");
2853 layout->item.type = ITEM_LAYOUT_ROOT;
2857 layout->root = root;
2858 layout->space = style->templatespace;
2860 layout->enabled = 1;
2861 layout->context = NULL;
2863 if (type == UI_LAYOUT_MENU)
2866 if (dir == UI_LAYOUT_HORIZONTAL) {
2868 layout->root->emh = em * UI_UNIT_Y;
2872 layout->root->emw = em * UI_UNIT_X;
2875 block->curlayout = layout;
2876 root->layout = layout;
2877 BLI_addtail(&block->layouts, root);
2882 uiBlock *uiLayoutGetBlock(uiLayout *layout)
2884 return layout->root->block;
2887 int uiLayoutGetOperatorContext(uiLayout *layout)
2889 return layout->root->opcontext;
2893 void uiBlockSetCurLayout(uiBlock *block, uiLayout *layout)
2895 block->curlayout = layout;
2898 void ui_layout_add_but(uiLayout *layout, uiBut *but)
2900 uiButtonItem *bitem;
2902 bitem = MEM_callocN(sizeof(uiButtonItem), "uiButtonItem");
2903 bitem->item.type = ITEM_BUTTON;
2905 BLI_addtail(&layout->items, bitem);
2907 if (layout->context) {
2908 but->context = layout->context;
2909 but->context->used = TRUE;
2913 void uiLayoutSetOperatorContext(uiLayout *layout, int opcontext)
2915 layout->root->opcontext = opcontext;
2918 void uiLayoutSetFunc(uiLayout *layout, uiMenuHandleFunc handlefunc, void *argv)
2920 layout->root->handlefunc = handlefunc;
2921 layout->root->argv = argv;
2924 void uiBlockLayoutResolve(uiBlock *block, int *x, int *y)
2931 block->curlayout = NULL;
2933 for (root = block->layouts.first; root; root = root->next) {
2934 /* NULL in advance so we don't interfere when adding button */
2935 ui_layout_end(block, root->layout, x, y);
2936 ui_layout_free(root->layout);
2939 BLI_freelistN(&block->layouts);
2941 /* XXX silly trick, interface_templates.c doesn't get linked
2942 * because it's not used by other files in this module? */
2944 UI_template_fix_linking();
2948 void uiLayoutSetContextPointer(uiLayout *layout, const char *name, PointerRNA *ptr)
2950 uiBlock *block = layout->root->block;
2951 layout->context = CTX_store_add(&block->contexts, name, ptr);
2954 void uiLayoutContextCopy(uiLayout *layout, bContextStore *context)
2956 uiBlock *block = layout->root->block;
2957 layout->context = CTX_store_add_all(&block->contexts, context);
2961 /* introspect funcs */
2962 #include "BLI_dynstr.h"
2964 static void ui_intro_button(DynStr *ds, uiButtonItem *bitem)
2966 uiBut *but = bitem->but;
2967 BLI_dynstr_appendf(ds, "'type':%d, ", but->type); /* see ~ UI_interface.h:200 */
2968 BLI_dynstr_appendf(ds, "'draw_string':'''%s''', ", but->drawstr);
2969 BLI_dynstr_appendf(ds, "'tip':'''%s''', ", but->tip ? but->tip : ""); /* not exactly needed, rna has this */
2972 char *opstr = WM_operator_pystring_ex(but->block->evil_C, NULL, false, but->optype, but->opptr);
2973 BLI_dynstr_appendf(ds, "'operator':'''%s''', ", opstr ? opstr : "");
2978 BLI_dynstr_appendf(ds, "'rna':'%s.%s[%d]', ", RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop), but->rnaindex);
2983 static void ui_intro_items(DynStr *ds, ListBase *lb)
2987 BLI_dynstr_append(ds, "[");
2989 for (item = lb->first; item; item = item->next) {
2991 BLI_dynstr_append(ds, "{");
2993 /* could also use the INT but this is nicer*/
2994 switch (item->type) {
2995 case ITEM_BUTTON: BLI_dynstr_append(ds, "'type':'BUTTON', "); break;
2996 case ITEM_LAYOUT_ROW: BLI_dynstr_append(ds, "'type':'ROW', "); break;
2997 case ITEM_LAYOUT_COLUMN: BLI_dynstr_append(ds, "'type':'COLUMN', "); break;
2998 case ITEM_LAYOUT_COLUMN_FLOW: BLI_dynstr_append(ds, "'type':'COLUMN_FLOW', "); break;
2999 case ITEM_LAYOUT_ROW_FLOW: BLI_dynstr_append(ds, "'type':'ROW_FLOW', "); break;
3000 case ITEM_LAYOUT_BOX: BLI_dynstr_append(ds, "'type':'BOX', "); break;
3001 case ITEM_LAYOUT_ABSOLUTE: BLI_dynstr_append(ds, "'type':'ABSOLUTE', "); break;
3002 case ITEM_LAYOUT_SPLIT: BLI_dynstr_append(ds, "'type':'SPLIT', "); break;
3003 case ITEM_LAYOUT_OVERLAP: BLI_dynstr_append(ds, "'type':'OVERLAP', "); break;
3004 case ITEM_LAYOUT_ROOT: BLI_dynstr_append(ds, "'type':'ROOT', "); break;
3005 default: BLI_dynstr_append(ds, "'type':'UNKNOWN', "); break;
3008 switch (item->type) {
3010 ui_intro_button(ds, (uiButtonItem *)item);
3013 BLI_dynstr_append(ds, "'items':");
3014 ui_intro_items(ds, &((uiLayout *)item)->items);
3018 BLI_dynstr_append(ds, "}");
3020 if (item != lb->last)
3021 BLI_dynstr_append(ds, ", ");
3023 BLI_dynstr_append(ds, "], ");
3026 static void ui_intro_uiLayout(DynStr *ds, uiLayout *layout)
3028 ui_intro_items(ds, &layout->items);
3031 static char *str = NULL; /* XXX, constant re-freeing, far from ideal. */
3032 const char *uiLayoutIntrospect(uiLayout *layout)
3034 DynStr *ds = BLI_dynstr_new();
3040 ui_intro_uiLayout(ds, layout);
3042 str = BLI_dynstr_get_cstring(ds);
3043 BLI_dynstr_free(ds);
3048 #ifdef USE_OP_RESET_BUT
3049 static void ui_layout_operator_buts__reset_cb(bContext *UNUSED(C), void *op_pt, void *UNUSED(arg_dummy2))
3051 WM_operator_properties_reset((wmOperator *)op_pt);
3055 /* this function does not initialize the layout, functions can be called on the layout before and after */
3056 void uiLayoutOperatorButs(const bContext *C, uiLayout *layout, wmOperator *op,
3057 bool (*check_prop)(struct PointerRNA *, struct PropertyRNA *),
3058 const char label_align, const short flag)
3060 if (!op->properties) {
3061 IDPropertyTemplate val = {0};
3062 op->properties = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
3065 if (flag & UI_LAYOUT_OP_SHOW_TITLE) {
3066 uiItemL(layout, RNA_struct_ui_name(op->type->srna), ICON_NONE);
3069 /* poll() on this operator may still fail, at the moment there is no nice feedback when this happens
3070 * just fails silently */
3071 if (!WM_operator_repeat_check(C, op)) {
3072 uiBlockSetButLock(uiLayoutGetBlock(layout), true, "Operator can't' redo");
3074 /* XXX, could give some nicer feedback or not show redo panel at all? */
3075 uiItemL(layout, IFACE_("* Redo Unsupported *"), ICON_NONE);
3079 if (op->type->flag & OPTYPE_PRESET) {
3080 /* XXX, no simple way to get WM_MT_operator_presets.bl_label from python! Label remains the same always! */
3084 uiLayoutGetBlock(layout)->ui_operator = op;
3086 row = uiLayoutRow(layout, TRUE);
3087 uiItemM(row, (bContext *)C, "WM_MT_operator_presets", NULL, ICON_NONE);
3089 WM_operator_properties_create(&op_ptr, "WM_OT_operator_preset_add");
3090 RNA_string_set(&op_ptr, "operator", op->type->idname);
3091 uiItemFullO(row, "WM_OT_operator_preset_add", "", ICON_ZOOMIN, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
3093 WM_operator_properties_create(&op_ptr, "WM_OT_operator_preset_add");
3094 RNA_string_set(&op_ptr, "operator", op->type->idname);
3095 RNA_boolean_set(&op_ptr, "remove_active", TRUE);
3096 uiItemFullO(row, "WM_OT_operator_preset_add", "", ICON_ZOOMOUT, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
3100 op->layout = layout;
3101 op->type->ui((bContext *)C, op);
3104 /* UI_LAYOUT_OP_SHOW_EMPTY ignored */
3107 wmWindowManager *wm = CTX_wm_manager(C);
3111 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
3113 /* main draw call */
3114 empty = uiDefAutoButsRNA(layout, &ptr, check_prop, label_align) == 0;
3116 if (empty && (flag & UI_LAYOUT_OP_SHOW_EMPTY)) {
3117 uiItemL(layout, IFACE_("No Properties"), ICON_NONE);
3121 #ifdef USE_OP_RESET_BUT
3122 /* its possible that reset can do nothing if all have PROP_SKIP_SAVE enabled
3123 * but this is not so important if this button is drawn in those cases
3124 * (which isn't all that likely anyway) - campbell */
3125 if (op->properties->len) {
3128 uiLayout *col; /* needed to avoid alignment errors with previous buttons */
3130 col = uiLayoutColumn(layout, FALSE);
3131 block = uiLayoutGetBlock(col);
3132 but = uiDefIconTextBut(block, BUT, 0, ICON_FILE_REFRESH, IFACE_("Reset"), 0, 0, UI_UNIT_X, UI_UNIT_Y,
3133 NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Reset operator defaults"));
3134 uiButSetFunc(but, ui_layout_operator_buts__reset_cb, op, NULL);
3138 /* set various special settings for buttons */
3140 uiBlock *block = uiLayoutGetBlock(layout);
3141 const bool is_popup = (block->flag & UI_BLOCK_KEEP_OPEN) != 0;
3145 for (but = block->buttons.first; but; but = but->next) {
3146 /* no undo for buttons for operator redo panels */
3147 uiButClearFlag(but, UI_BUT_UNDO);
3149 /* only for popups, see [#36109] */