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 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation 2002-2008, full recode.
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/interface/interface.c
27 * \ingroup edinterface
36 #include <stddef.h> /* offsetof() */
38 #include "MEM_guardedalloc.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_userdef_types.h"
45 #include "BLI_listbase.h"
46 #include "BLI_string.h"
47 #include "BLI_string_utf8.h"
48 #include "BLI_path_util.h"
51 #include "BLI_dynstr.h"
52 #include "BLI_utildefines.h"
54 #include "BKE_context.h"
55 #include "BKE_library.h"
57 #include "BKE_screen.h"
58 #include "BKE_idprop.h"
63 #include "BLF_translation.h"
65 #include "UI_interface.h"
67 #include "IMB_imbuf.h"
71 #include "wm_subwindow.h"
72 #include "wm_window.h"
74 #include "RNA_access.h"
76 #include "BPY_extern.h"
78 #include "interface_intern.h"
80 #define MENU_WIDTH 120
81 #define MENU_ITEM_HEIGHT 20
82 #define MENU_SEP_HEIGHT 6
84 #define PRECISION_FLOAT_MAX 6
85 #define PRECISION_FLOAT_MAX_POW 1000000 /* pow(10, PRECISION_FLOAT_MAX) */
87 /* avoid unneeded calls to ui_get_but_val */
88 #define UI_BUT_VALUE_UNSET DBL_MAX
89 #define UI_GET_BUT_VALUE_INIT(_but, _value) if (_value == DBL_MAX) { (_value) = ui_get_but_val(_but); } (void)0
92 * a full doc with API notes can be found in bf-blender/trunk/blender/doc/guides/interface_API.txt
94 * uiBlahBlah() external function
95 * ui_blah_blah() internal function
98 static void ui_free_but(const bContext *C, uiBut *but);
100 /* ************* window matrix ************** */
102 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
105 int sx, sy, getsizex, getsizey;
107 getsizex = BLI_RCT_SIZE_X(&ar->winrct) + 1;
108 getsizey = BLI_RCT_SIZE_Y(&ar->winrct) + 1;
109 sx = ar->winrct.xmin;
110 sy = ar->winrct.ymin;
116 gx += block->panel->ofsx;
117 gy += block->panel->ofsy;
120 *x = ((float)sx) + ((float)getsizex) * (0.5f + 0.5f * (gx * block->winmat[0][0] + gy * block->winmat[1][0] + block->winmat[3][0]));
121 *y = ((float)sy) + ((float)getsizey) * (0.5f + 0.5f * (gx * block->winmat[0][1] + gy * block->winmat[1][1] + block->winmat[3][1]));
124 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
131 ui_block_to_window_fl(ar, block, &fx, &fy);
133 *x = (int)(fx + 0.5f);
134 *y = (int)(fy + 0.5f);
137 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
142 ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
143 ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
145 BLI_rcti_rctf_copy(winr, &tmpr);
148 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y) /* for mouse cursor */
150 float a, b, c, d, e, f, px, py;
151 int sx, sy, getsizex, getsizey;
153 getsizex = BLI_RCT_SIZE_X(&ar->winrct) + 1;
154 getsizey = BLI_RCT_SIZE_Y(&ar->winrct) + 1;
155 sx = ar->winrct.xmin;
156 sy = ar->winrct.ymin;
158 a = 0.5f * ((float)getsizex) * block->winmat[0][0];
159 b = 0.5f * ((float)getsizex) * block->winmat[1][0];
160 c = 0.5f * ((float)getsizex) * (1.0f + block->winmat[3][0]);
162 d = 0.5f * ((float)getsizey) * block->winmat[0][1];
163 e = 0.5f * ((float)getsizey) * block->winmat[1][1];
164 f = 0.5f * ((float)getsizey) * (1.0f + block->winmat[3][1]);
169 *y = (a * (py - f) + d * (c - px)) / (a * e - d * b);
170 *x = (px - b * (*y) - c) / a;
173 *x -= block->panel->ofsx;
174 *y -= block->panel->ofsy;
178 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
185 ui_window_to_block_fl(ar, block, &fx, &fy);
187 *x = (int)(fx + 0.5f);
188 *y = (int)(fy + 0.5f);
191 void ui_window_to_region(const ARegion *ar, int *x, int *y)
193 *x -= ar->winrct.xmin;
194 *y -= ar->winrct.ymin;
197 /* ******************* block calc ************************* */
199 void ui_block_translate(uiBlock *block, int x, int y)
203 for (but = block->buttons.first; but; but = but->next) {
204 BLI_rctf_translate(&but->rect, x, y);
207 BLI_rctf_translate(&block->rect, x, y);
210 static void ui_text_bounds_block(uiBlock *block, float offset)
212 uiStyle *style = UI_GetStyle();
214 int i = 0, j, x1addval = offset, nextcol;
215 int lastcol = 0, col = 0;
217 uiStyleFontSet(&style->widget);
219 for (bt = block->buttons.first; bt; bt = bt->next) {
220 if (bt->type != SEPR) {
221 j = BLF_width(style->widget.uifont_id, bt->drawstr);
226 if (bt->next && bt->rect.xmin < bt->next->rect.xmin)
230 /* cope with multi collumns */
231 bt = block->buttons.first;
233 if (bt->next && bt->rect.xmin < bt->next->rect.xmin) {
239 bt->rect.xmin = x1addval;
240 bt->rect.xmax = bt->rect.xmin + i + block->bounds;
242 if (col == lastcol) {
243 bt->rect.xmax = maxf(bt->rect.xmax, offset + block->minbounds);
246 ui_check_but(bt); /* clips text again */
249 x1addval += i + block->bounds;
255 void ui_bounds_block(uiBlock *block)
260 if (block->buttons.first == NULL) {
262 block->rect.xmin = 0.0; block->rect.xmax = block->panel->sizex;
263 block->rect.ymin = 0.0; block->rect.ymax = block->panel->sizey;
268 BLI_rctf_init_minmax(&block->rect);
270 for (bt = block->buttons.first; bt; bt = bt->next) {
271 BLI_rctf_union(&block->rect, &bt->rect);
274 block->rect.xmin -= block->bounds;
275 block->rect.ymin -= block->bounds;
276 block->rect.xmax += block->bounds;
277 block->rect.ymax += block->bounds;
280 block->rect.xmax = block->rect.xmin + maxf(BLI_RCT_SIZE_X(&block->rect), block->minbounds);
282 /* hardcoded exception... but that one is annoying with larger safety */
283 bt = block->buttons.first;
284 if (bt && strncmp(bt->str, "ERROR", 5) == 0) xof = 10;
287 block->safety.xmin = block->rect.xmin - xof;
288 block->safety.ymin = block->rect.ymin - xof;
289 block->safety.xmax = block->rect.xmax + xof;
290 block->safety.ymax = block->rect.ymax + xof;
293 static void ui_centered_bounds_block(const bContext *C, uiBlock *block)
295 wmWindow *window = CTX_wm_window(C);
300 /* note: this is used for the splash where window bounds event has not been
301 * updated by ghost, get the window bounds from ghost directly */
303 // wm_window_get_size(window, &xmax, &ymax);
304 wm_window_get_size_ghost(window, &xmax, &ymax);
306 ui_bounds_block(block);
308 width = BLI_RCT_SIZE_X(&block->rect);
309 height = BLI_RCT_SIZE_Y(&block->rect);
311 startx = (xmax * 0.5f) - (width * 0.5f);
312 starty = (ymax * 0.5f) - (height * 0.5f);
314 ui_block_translate(block, startx - block->rect.xmin, starty - block->rect.ymin);
316 /* now recompute bounds and safety */
317 ui_bounds_block(block);
320 static void ui_popup_bounds_block(const bContext *C, uiBlock *block, int bounds_calc)
322 wmWindow *window = CTX_wm_window(C);
323 int startx, starty, endx, endy, width, height, oldwidth, oldheight;
324 int oldbounds, xmax, ymax;
326 oldbounds = block->bounds;
328 /* compute mouse position with user defined offset */
329 ui_bounds_block(block);
331 wm_window_get_size(window, &xmax, &ymax);
333 oldwidth = BLI_RCT_SIZE_X(&block->rect);
334 oldheight = BLI_RCT_SIZE_Y(&block->rect);
336 /* first we ensure wide enough text bounds */
337 if (bounds_calc == UI_BLOCK_BOUNDS_POPUP_MENU) {
338 if (block->flag & UI_BLOCK_LOOP) {
340 ui_text_bounds_block(block, block->rect.xmin);
344 /* next we recompute bounds */
345 block->bounds = oldbounds;
346 ui_bounds_block(block);
348 /* and we adjust the position to fit within window */
349 width = BLI_RCT_SIZE_X(&block->rect);
350 height = BLI_RCT_SIZE_Y(&block->rect);
352 /* avoid divide by zero below, caused by calling with no UI, but better not crash */
353 oldwidth = oldwidth > 0 ? oldwidth : MAX2(1, width);
354 oldheight = oldheight > 0 ? oldheight : MAX2(1, height);
356 /* offset block based on mouse position, user offset is scaled
357 * along in case we resized the block in ui_text_bounds_block */
358 startx = window->eventstate->x + block->rect.xmin + (block->mx * width) / oldwidth;
359 starty = window->eventstate->y + block->rect.ymin + (block->my * height) / oldheight;
366 endx = startx + width;
367 endy = starty + height;
371 startx = endx - width;
373 if (endy > ymax - 20) {
375 starty = endy - height;
378 ui_block_translate(block, startx - block->rect.xmin, starty - block->rect.ymin);
380 /* now recompute bounds and safety */
381 ui_bounds_block(block);
384 /* used for various cases */
385 void uiBoundsBlock(uiBlock *block, int addval)
390 block->bounds = addval;
391 block->dobounds = UI_BLOCK_BOUNDS;
394 /* used for pulldowns */
395 void uiTextBoundsBlock(uiBlock *block, int addval)
397 block->bounds = addval;
398 block->dobounds = UI_BLOCK_BOUNDS_TEXT;
401 /* used for block popups */
402 void uiPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
404 block->bounds = addval;
405 block->dobounds = UI_BLOCK_BOUNDS_POPUP_MOUSE;
410 /* used for menu popups */
411 void uiMenuPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
413 block->bounds = addval;
414 block->dobounds = UI_BLOCK_BOUNDS_POPUP_MENU;
419 /* used for centered popups, i.e. splash */
420 void uiCenteredBoundsBlock(uiBlock *block, int addval)
422 block->bounds = addval;
423 block->dobounds = UI_BLOCK_BOUNDS_POPUP_CENTER;
426 void uiExplicitBoundsBlock(uiBlock *block, int minx, int miny, int maxx, int maxy)
428 block->rect.xmin = minx;
429 block->rect.ymin = miny;
430 block->rect.xmax = maxx;
431 block->rect.ymax = maxy;
435 /* ************** LINK LINE DRAWING ************* */
437 /* link line drawing is not part of buttons or theme.. so we stick with it here */
439 static int ui_but_float_precision(uiBut *but, double value)
443 /* first check if prec is 0 and fallback to a simple default */
444 if ((prec = (int)but->a2) == 0) {
445 prec = (but->hardmax < 10.001f) ? 3 : 2;
448 /* check on the number of decimal places need to display
449 * the number, this is so 0.00001 is not displayed as 0.00,
450 * _but_, this is only for small values si 10.0001 will not get
451 * the same treatment */
452 if (value != 0.0 && (value = ABS(value)) < 0.1) {
453 int value_i = (int)((value * PRECISION_FLOAT_MAX_POW) + 0.5);
455 const int prec_span = 3; /* show: 0.01001, 5 would allow 0.0100001 for eg. */
459 int i = PRECISION_FLOAT_MAX;
460 while (i && value_i) {
469 /* even though its a small value, if the second last digit is not 0, use it */
470 test_prec = prec_min;
472 dec_flag = (dec_flag >> (prec_min + 1)) & ((1 << prec_span) - 1);
476 dec_flag = dec_flag >> 1;
479 if (test_prec > prec) {
485 CLAMP(prec, 1, PRECISION_FLOAT_MAX);
490 static void ui_draw_linkline(uiLinkLine *line, int highlightActiveLines)
494 if (line->from == NULL || line->to == NULL) return;
496 rect.xmin = BLI_RCT_CENTER_X(&line->from->rect);
497 rect.ymin = BLI_RCT_CENTER_Y(&line->from->rect);
498 rect.xmax = BLI_RCT_CENTER_X(&line->to->rect);
499 rect.ymax = BLI_RCT_CENTER_Y(&line->to->rect);
501 if (line->flag & UI_SELECT)
502 glColor3ub(100, 100, 100);
503 else if (highlightActiveLines && ((line->from->flag & UI_ACTIVE) || (line->to->flag & UI_ACTIVE)))
504 UI_ThemeColor(TH_TEXT_HI);
508 ui_draw_link_bezier(&rect);
511 static void ui_draw_links(uiBlock *block)
516 /* Draw the inactive lines (lines with neither button being hovered over).
517 * As we go, remember if we see any active or selected lines. */
518 int foundselectline = FALSE;
519 int foundactiveline = FALSE;
520 for (but = block->buttons.first; but; but = but->next) {
521 if (but->type == LINK && but->link) {
522 for (line = but->link->lines.first; line; line = line->next) {
523 if (!(line->from->flag & UI_ACTIVE) && !(line->to->flag & UI_ACTIVE))
524 ui_draw_linkline(line, 0);
526 foundactiveline = TRUE;
528 if ((line->from->flag & UI_SELECT) || (line->to->flag & UI_SELECT))
529 foundselectline = TRUE;
534 /* Draw any active lines (lines with either button being hovered over).
535 * Do this last so they appear on top of inactive lines. */
536 if (foundactiveline) {
537 for (but = block->buttons.first; but; but = but->next) {
538 if (but->type == LINK && but->link) {
539 for (line = but->link->lines.first; line; line = line->next) {
540 if ((line->from->flag & UI_ACTIVE) || (line->to->flag & UI_ACTIVE))
541 ui_draw_linkline(line, !foundselectline);
548 /* ************** BLOCK ENDING FUNCTION ************* */
550 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
551 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
553 /* various properties are being compared here, hopefully sufficient
554 * to catch all cases, but it is simple to add more checks later */
555 if (but->retval != oldbut->retval) return 0;
556 if (but->rnapoin.data != oldbut->rnapoin.data) return 0;
557 if (but->rnaprop != oldbut->rnaprop)
558 if (but->rnaindex != oldbut->rnaindex) return 0;
559 if (but->func != oldbut->func) return 0;
560 if (but->funcN != oldbut->funcN) return 0;
561 if (oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return 0;
562 if (oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return 0;
563 if (!but->funcN && ((but->poin != oldbut->poin && (uiBut *)oldbut->poin != oldbut) || but->pointype != oldbut->pointype)) return 0;
564 if (but->optype != oldbut->optype) return 0;
569 /* oldbut is being inserted in new block, so we use the lines from new button, and replace button pointers */
570 static void ui_but_update_linklines(uiBlock *block, uiBut *oldbut, uiBut *newbut)
575 /* if active button is LINK */
576 if (newbut->type == LINK && newbut->link) {
578 SWAP(uiLink *, oldbut->link, newbut->link);
580 for (line = oldbut->link->lines.first; line; line = line->next) {
581 if (line->to == newbut)
583 if (line->from == newbut)
588 /* check all other button links */
589 for (but = block->buttons.first; but; but = but->next) {
590 if (but != newbut && but->type == LINK && but->link) {
591 for (line = but->link->lines.first; line; line = line->next) {
592 if (line->to == newbut)
594 if (line->from == newbut)
601 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut **butpp)
604 uiBut *oldbut, *but = *butpp;
607 oldblock = block->oldblock;
611 for (oldbut = oldblock->buttons.first; oldbut; oldbut = oldbut->next) {
612 if (ui_but_equals_old(oldbut, but)) {
613 if (oldbut->active) {
615 // but->flag= oldbut->flag;
617 /* exception! redalert flag can't be update from old button.
618 * perhaps it should only copy specific flags rather than all. */
619 // but->flag= (oldbut->flag & ~UI_BUT_REDALERT) | (but->flag & UI_BUT_REDALERT);
621 // but->active= oldbut->active;
622 // but->pos= oldbut->pos;
623 // but->ofs= oldbut->ofs;
624 // but->editstr= oldbut->editstr;
625 // but->editval= oldbut->editval;
626 // but->editvec= oldbut->editvec;
627 // but->editcoba= oldbut->editcoba;
628 // but->editcumap= oldbut->editcumap;
629 // but->selsta= oldbut->selsta;
630 // but->selend= oldbut->selend;
631 // but->softmin= oldbut->softmin;
632 // but->softmax= oldbut->softmax;
633 // but->linkto[0]= oldbut->linkto[0];
634 // but->linkto[1]= oldbut->linkto[1];
636 // oldbut->active= NULL;
638 /* move button over from oldblock to new block */
639 BLI_remlink(&oldblock->buttons, oldbut);
640 BLI_insertlink(&block->buttons, but, oldbut);
641 oldbut->block = block;
644 /* still stuff needs to be copied */
645 oldbut->rect = but->rect;
646 oldbut->context = but->context; /* set by Layout */
648 /* typically the same pointers, but not on undo/redo */
649 /* XXX some menu buttons store button itself in but->poin. Ugly */
650 if (oldbut->poin != (char *)oldbut) {
651 SWAP(char *, oldbut->poin, but->poin);
652 SWAP(void *, oldbut->func_argN, but->func_argN);
655 /* copy hardmin for list rows to prevent 'sticking' highlight to mouse position
656 * when scrolling without moving mouse (see [#28432]) */
657 if (ELEM(oldbut->type, ROW, LISTROW))
658 oldbut->hardmax = but->hardmax;
660 ui_but_update_linklines(block, oldbut, but);
662 BLI_remlink(&block->buttons, but);
665 /* note: if layout hasn't been applied yet, it uses old button pointers... */
668 /* ensures one button can get activated, and in case the buttons
669 * draw are the same this gives O(1) lookup for each button */
670 BLI_remlink(&oldblock->buttons, oldbut);
671 ui_free_but(C, oldbut);
681 /* needed for temporarily rename buttons, such as in outliner or file-select,
682 * they should keep calling uiDefButs to keep them alive */
683 /* returns 0 when button removed */
684 int uiButActiveOnly(const bContext *C, uiBlock *block, uiBut *but)
688 int activate = FALSE, found = FALSE, isactive = FALSE;
690 oldblock = block->oldblock;
694 for (oldbut = oldblock->buttons.first; oldbut; oldbut = oldbut->next) {
695 if (ui_but_equals_old(oldbut, but)) {
705 if ((activate == TRUE) || (found == FALSE)) {
706 ui_button_activate_do((bContext *)C, CTX_wm_region(C), but);
708 else if ((found == TRUE) && (isactive == FALSE)) {
709 BLI_remlink(&block->buttons, but);
717 /* use to check if we need to disable undo, but don't make any changes
718 * returns FALSE if undo needs to be disabled. */
719 static int ui_but_is_rna_undo(uiBut *but)
721 if (but->rnapoin.id.data) {
722 /* avoid undo push for buttons who's ID are screen or wm level
723 * we could disable undo for buttons with no ID too but may have
724 * unforeseen consequences, so best check for ID's we _know_ are not
725 * handled by undo - campbell */
726 ID *id = but->rnapoin.id.data;
727 if (ID_CHECK_UNDO(id) == FALSE) {
734 else if (but->rnapoin.type && !RNA_struct_undo_check(but->rnapoin.type)) {
741 /* assigns automatic keybindings to menu items for fast access
742 * (underline key in menu) */
743 static void ui_menu_block_set_keyaccels(uiBlock *block)
747 unsigned int menu_key_mask = 0;
748 unsigned char menu_key;
753 /* only do it before bounding */
754 if (block->rect.xmin != block->rect.xmax)
757 for (pass = 0; pass < 2; pass++) {
758 /* 2 Passes, on for first letter only, second for any letter if first fails
759 * fun first pass on all buttons so first word chars always get first priority */
761 for (but = block->buttons.first; but; but = but->next) {
762 if (!ELEM4(but->type, BUT, MENU, BLOCK, PULLDOWN) || (but->flag & UI_HIDDEN)) {
765 else if (but->menu_key == '\0') {
767 for (str_pt = but->str; *str_pt; ) {
768 menu_key = tolower(*str_pt);
769 if ((menu_key >= 'a' && menu_key <= 'z') && !(menu_key_mask & 1 << (menu_key - 'a'))) {
770 menu_key_mask |= 1 << (menu_key - 'a');
775 /* Skip to next delimiter on first pass (be picky) */
776 while (isalpha(*str_pt))
783 /* just step over every char second pass and find first usable key */
789 but->menu_key = menu_key;
792 /* run second pass */
796 /* if all keys have been used just exit, unlikely */
797 if (menu_key_mask == (1 << 26) - 1) {
804 /* check if second pass is needed */
811 /* XXX, this code will shorten any allocated string to 'UI_MAX_NAME_STR'
812 * since this is really long its unlikely to be an issue,
813 * but this could be supported */
814 void ui_but_add_shortcut(uiBut *but, const char *shortcut_str, const short do_strip)
818 char *cpoin = strchr(but->str, '|');
824 /* without this, just allow stripping of the shortcut */
828 if (but->str != but->strdata) {
829 butstr_orig = but->str; /* free after using as source buffer */
832 butstr_orig = BLI_strdup(but->str);
834 BLI_snprintf(but->strdata,
835 sizeof(but->strdata),
837 butstr_orig, shortcut_str);
838 MEM_freeN(butstr_orig);
839 but->str = but->strdata;
844 static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
851 IDProperty *prop_menu = NULL;
852 IDProperty *prop_menu_name = NULL;
854 /* only do it before bounding */
855 if (block->rect.xmin != block->rect.xmax)
858 for (but = block->buttons.first; but; but = but->next) {
860 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
862 if (WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, TRUE,
865 ui_but_add_shortcut(but, buf, FALSE);
868 else if ((mt = uiButGetMenuType(but))) {
869 /* only allocate menu property once */
870 if (prop_menu == NULL) {
871 /* annoying, create a property */
872 IDPropertyTemplate val = {0};
873 prop_menu = IDP_New(IDP_GROUP, &val, __func__); /* dummy, name is unimportant */
874 IDP_AddToGroup(prop_menu, (prop_menu_name = IDP_NewString("", "name", sizeof(mt->idname))));
877 IDP_AssignString(prop_menu_name, mt->idname, sizeof(mt->idname));
879 if (WM_key_event_operator_string(C, "WM_OT_call_menu", WM_OP_INVOKE_REGION_WIN, prop_menu, FALSE,
882 ui_but_add_shortcut(but, buf, FALSE);
888 IDP_FreeProperty(prop_menu);
889 MEM_freeN(prop_menu);
892 #undef UI_MENU_KEY_STR_CAT
896 void uiEndBlock(const bContext *C, uiBlock *block)
899 Scene *scene = CTX_data_scene(C);
901 /* inherit flags from 'old' buttons that was drawn here previous, based
902 * on matching buttons, we need this to make button event handling non
903 * blocking, while still allowing buttons to be remade each redraw as it
904 * is expected by blender code */
905 for (but = block->buttons.first; but; but = but->next) {
906 if (ui_but_update_from_old_block(C, block, &but))
909 /* temp? Proper check for graying out */
911 wmOperatorType *ot = but->optype;
914 CTX_store_set((bContext *)C, but->context);
916 if (ot == NULL || WM_operator_poll_context((bContext *)C, ot, but->opcontext) == 0) {
917 but->flag |= UI_BUT_DISABLED;
922 CTX_store_set((bContext *)C, NULL);
925 ui_but_anim_flag(but, (scene) ? scene->r.cfra : 0.0f);
928 if (block->oldblock) {
929 block->auto_open = block->oldblock->auto_open;
930 block->auto_open_last = block->oldblock->auto_open_last;
931 block->tooltipdisabled = block->oldblock->tooltipdisabled;
933 block->oldblock = NULL;
936 /* handle pending stuff */
937 if (block->layouts.first) uiBlockLayoutResolve(block, NULL, NULL);
938 ui_block_do_align(block);
939 if ((block->flag & UI_BLOCK_LOOP) && (block->flag & UI_BLOCK_NUMSELECT)) {
940 ui_menu_block_set_keyaccels(block); /* could use a different flag to check */
942 if (block->flag & UI_BLOCK_LOOP) ui_menu_block_set_keymaps(C, block);
945 if (block->dobounds == UI_BLOCK_BOUNDS) ui_bounds_block(block);
946 else if (block->dobounds == UI_BLOCK_BOUNDS_TEXT) ui_text_bounds_block(block, 0.0f);
947 else if (block->dobounds == UI_BLOCK_BOUNDS_POPUP_CENTER) ui_centered_bounds_block(C, block);
948 else if (block->dobounds) ui_popup_bounds_block(C, block, block->dobounds);
950 if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) uiBoundsBlock(block, 0);
951 if (block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
956 /* ************** BLOCK DRAWING FUNCTION ************* */
958 void ui_fontscale(short *points, float aspect)
960 if (aspect < 0.9f || aspect > 1.1f) {
961 float pointsf = *points;
963 /* for some reason scaling fonts goes too fast compared to widget size */
964 aspect = sqrt(aspect);
968 *points = ceilf(pointsf);
970 *points = floorf(pointsf);
974 /* project button or block (but==NULL) to pixels in regionspace */
975 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
978 float getsizex, getsizey;
983 gx = (but ? but->rect.xmin : block->rect.xmin) + (block->panel ? block->panel->ofsx : 0.0f);
984 gy = (but ? but->rect.ymin : block->rect.ymin) + (block->panel ? block->panel->ofsy : 0.0f);
986 rect->xmin = floorf(getsizex * (0.5f + 0.5f * (gx * block->winmat[0][0] + gy * block->winmat[1][0] + block->winmat[3][0])));
987 rect->ymin = floorf(getsizey * (0.5f + 0.5f * (gx * block->winmat[0][1] + gy * block->winmat[1][1] + block->winmat[3][1])));
989 gx = (but ? but->rect.xmax : block->rect.xmax) + (block->panel ? block->panel->ofsx : 0.0f);
990 gy = (but ? but->rect.ymax : block->rect.ymax) + (block->panel ? block->panel->ofsy : 0.0f);
992 rect->xmax = floorf(getsizex * (0.5f + 0.5f * (gx * block->winmat[0][0] + gy * block->winmat[1][0] + block->winmat[3][0])));
993 rect->ymax = floorf(getsizey * (0.5f + 0.5f * (gx * block->winmat[0][1] + gy * block->winmat[1][1] + block->winmat[3][1])));
997 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
998 void uiDrawBlock(const bContext *C, uiBlock *block)
1000 uiStyle style = *UI_GetStyle(); /* XXX pass on as arg */
1004 int multisample_enabled;
1006 /* get menu region or area region */
1007 ar = CTX_wm_menu(C);
1009 ar = CTX_wm_region(C);
1011 if (!block->endblock)
1012 uiEndBlock(C, block);
1014 /* disable AA, makes widgets too blurry */
1015 multisample_enabled = glIsEnabled(GL_MULTISAMPLE_ARB);
1016 if (multisample_enabled)
1017 glDisable(GL_MULTISAMPLE_ARB);
1019 /* we set this only once */
1020 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1023 ui_fontscale(&style.paneltitle.points, block->aspect);
1024 ui_fontscale(&style.grouplabel.points, block->aspect);
1025 ui_fontscale(&style.widgetlabel.points, block->aspect);
1026 ui_fontscale(&style.widget.points, block->aspect);
1028 /* scale block min/max to rect */
1029 ui_but_to_pixelrect(&rect, ar, block, NULL);
1031 /* pixel space for AA widgets */
1032 glMatrixMode(GL_PROJECTION);
1034 glMatrixMode(GL_MODELVIEW);
1038 wmOrtho2(-0.01f, ar->winx - 0.01f, -0.01f, ar->winy - 0.01f);
1041 if (block->flag & UI_BLOCK_LOOP)
1042 ui_draw_menu_back(&style, block, &rect);
1043 else if (block->panel)
1044 ui_draw_aligned_panel(&style, block, &rect);
1047 for (but = block->buttons.first; but; but = but->next) {
1048 if (!(but->flag & (UI_HIDDEN | UI_SCROLLED))) {
1049 ui_but_to_pixelrect(&rect, ar, block, but);
1051 /* XXX: figure out why invalid coordinates happen when closing render window */
1052 /* and material preview is redrawn in main window (temp fix for bug #23848) */
1053 if (rect.xmin < rect.xmax && rect.ymin < rect.ymax)
1054 ui_draw_but(C, ar, &style, but, &rect);
1058 /* restore matrix */
1059 glMatrixMode(GL_PROJECTION);
1061 glMatrixMode(GL_MODELVIEW);
1064 if (multisample_enabled)
1065 glEnable(GL_MULTISAMPLE_ARB);
1067 ui_draw_links(block);
1070 /* ************* EVENTS ************* */
1072 static void ui_is_but_sel(uiBut *but, double *value)
1074 short is_push = 0; /* (0 == UNSELECT), (1 == SELECT), (2 == DO-NOHING) */
1075 short is_true = TRUE;
1077 if (ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) {
1083 UI_GET_BUT_VALUE_INIT(but, *value);
1084 lvalue = (int)*value;
1085 if (UI_BITBUT_TEST(lvalue, (but->bitnr))) {
1093 switch (but->type) {
1108 UI_GET_BUT_VALUE_INIT(but, *value);
1109 if (*value != (double)but->hardmin) is_push = 1;
1114 UI_GET_BUT_VALUE_INIT(but, *value);
1115 if (*value == 0.0) is_push = 1;
1119 UI_GET_BUT_VALUE_INIT(but, *value);
1120 /* support for rna enum buts */
1121 if (but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
1122 if ((int)*value & (int)but->hardmax) is_push = 1;
1125 if (*value == (double)but->hardmax) is_push = 1;
1138 else if (is_push == 1) but->flag |= UI_SELECT;
1139 else but->flag &= ~UI_SELECT;
1142 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
1146 but = block->buttons.first;
1148 if (but->type == INLINK) {
1149 if (but->poin == poin) return but;
1156 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
1160 line = MEM_callocN(sizeof(uiLinkLine), "linkline");
1161 BLI_addtail(listb, line);
1166 uiBut *uiFindInlink(uiBlock *block, void *poin)
1168 return ui_find_inlink(block, poin);
1171 void uiComposeLinks(uiBlock *block)
1178 but = block->buttons.first;
1180 if (but->type == LINK) {
1183 /* for all pointers in the array */
1186 ppoin = link->ppoin;
1187 for (a = 0; a < *(link->totlink); a++) {
1188 bt = ui_find_inlink(block, (*ppoin)[a]);
1190 ui_add_link_line(&link->lines, but, bt);
1194 else if (link->poin) {
1195 bt = ui_find_inlink(block, *(link->poin) );
1197 ui_add_link_line(&link->lines, but, bt);
1207 /* ************************************************ */
1209 void uiBlockSetButLock(uiBlock *block, int val, const char *lockstr)
1212 block->lock = val ? TRUE : FALSE;
1213 block->lockstr = lockstr;
1217 void uiBlockClearButLock(uiBlock *block)
1219 block->lock = FALSE;
1220 block->lockstr = NULL;
1223 /* *************************************************************** */
1225 void ui_delete_linkline(uiLinkLine *line, uiBut *but)
1230 BLI_remlink(&but->link->lines, line);
1232 link = line->from->link;
1234 /* are there more pointers allowed? */
1237 if (*(link->totlink) == 1) {
1238 *(link->totlink) = 0;
1239 MEM_freeN(*(link->ppoin));
1240 *(link->ppoin) = NULL;
1244 for (a = 0; a < (*(link->totlink)); a++) {
1246 if ( (*(link->ppoin))[a] != line->to->poin) {
1247 (*(link->ppoin))[b] = (*(link->ppoin))[a];
1251 (*(link->totlink))--;
1255 *(link->poin) = NULL;
1262 /* *********************** data get/set ***********************
1263 * this either works with the pointed to data, or can work with
1264 * an edit override pointer while dragging for example */
1266 /* for buttons pointing to color for example */
1267 void ui_get_but_vectorf(uiBut *but, float vec[3])
1273 copy_v3_v3(vec, but->editvec);
1277 prop = but->rnaprop;
1279 vec[0] = vec[1] = vec[2] = 0.0f;
1281 if (RNA_property_type(prop) == PROP_FLOAT) {
1282 tot = RNA_property_array_length(&but->rnapoin, prop);
1285 for (a = 0; a < tot; a++)
1286 vec[a] = RNA_property_float_get_index(&but->rnapoin, prop, a);
1289 else if (but->pointype == CHA) {
1290 char *cp = (char *)but->poin;
1291 vec[0] = ((float)cp[0]) / 255.0f;
1292 vec[1] = ((float)cp[1]) / 255.0f;
1293 vec[2] = ((float)cp[2]) / 255.0f;
1295 else if (but->pointype == FLO) {
1296 float *fp = (float *)but->poin;
1297 copy_v3_v3(vec, fp);
1300 if (but->editvec == NULL) {
1301 fprintf(stderr, "ui_get_but_vectorf: can't get color, should never happen\n");
1302 vec[0] = vec[1] = vec[2] = 0.0f;
1306 if (but->type == BUT_NORMAL) {
1311 /* for buttons pointing to color for example */
1312 void ui_set_but_vectorf(uiBut *but, const float vec[3])
1317 copy_v3_v3(but->editvec, vec);
1321 prop = but->rnaprop;
1323 if (RNA_property_type(prop) == PROP_FLOAT) {
1327 tot = RNA_property_array_length(&but->rnapoin, prop);
1330 for (a = 0; a < tot; a++) {
1331 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1335 else if (but->pointype == CHA) {
1336 char *cp = (char *)but->poin;
1337 cp[0] = (char)(0.5f + vec[0] * 255.0f);
1338 cp[1] = (char)(0.5f + vec[1] * 255.0f);
1339 cp[2] = (char)(0.5f + vec[2] * 255.0f);
1341 else if (but->pointype == FLO) {
1342 float *fp = (float *)but->poin;
1343 copy_v3_v3(fp, vec);
1347 int ui_is_but_float(uiBut *but)
1349 if (but->pointype == FLO && but->poin)
1352 if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1358 int ui_is_but_unit(uiBut *but)
1360 UnitSettings *unit = but->block->unit;
1361 const int unit_type = uiButGetUnitType(but);
1363 if (unit_type == PROP_UNIT_NONE)
1366 #if 1 /* removed so angle buttons get correct snapping */
1367 if (unit->system_rotation == USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION)
1371 /* for now disable time unit conversion */
1372 if (unit_type == PROP_UNIT_TIME)
1375 if (unit->system == USER_UNIT_NONE) {
1376 if (unit_type != PROP_UNIT_ROTATION) {
1384 int ui_is_but_rna_valid(uiBut *but)
1386 if (but->rnaprop == NULL || RNA_struct_contains_property(&but->rnapoin, but->rnaprop)) {
1390 printf("property removed %s: %p\n", but->drawstr, but->rnaprop);
1395 double ui_get_but_val(uiBut *but)
1400 if (but->editval) { return *(but->editval); }
1401 if (but->poin == NULL && but->rnapoin.data == NULL) return 0.0;
1404 prop = but->rnaprop;
1406 switch (RNA_property_type(prop)) {
1408 if (RNA_property_array_check(prop))
1409 value = RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1411 value = RNA_property_boolean_get(&but->rnapoin, prop);
1414 if (RNA_property_array_check(prop))
1415 value = RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1417 value = RNA_property_int_get(&but->rnapoin, prop);
1420 if (RNA_property_array_check(prop))
1421 value = RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1423 value = RNA_property_float_get(&but->rnapoin, prop);
1426 value = RNA_property_enum_get(&but->rnapoin, prop);
1433 else if (but->type == HSVSLI) {
1436 fp = (but->editvec) ? but->editvec : (float *)but->poin;
1437 rgb_to_hsv_v(fp, hsv);
1439 switch (but->str[0]) {
1440 case 'H': value = hsv[0]; break;
1441 case 'S': value = hsv[1]; break;
1442 case 'V': value = hsv[2]; break;
1445 else if (but->pointype == CHA) {
1446 value = *(char *)but->poin;
1448 else if (but->pointype == SHO) {
1449 value = *(short *)but->poin;
1451 else if (but->pointype == INT) {
1452 value = *(int *)but->poin;
1454 else if (but->pointype == FLO) {
1455 value = *(float *)but->poin;
1461 void ui_set_but_val(uiBut *but, double value)
1465 /* value is a hsv value: convert to rgb */
1467 prop = but->rnaprop;
1469 if (RNA_property_editable(&but->rnapoin, prop)) {
1470 switch (RNA_property_type(prop)) {
1472 if (RNA_property_array_length(&but->rnapoin, prop))
1473 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1475 RNA_property_boolean_set(&but->rnapoin, prop, value);
1478 if (RNA_property_array_length(&but->rnapoin, prop))
1479 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, (int)value);
1481 RNA_property_int_set(&but->rnapoin, prop, (int)value);
1484 if (RNA_property_array_length(&but->rnapoin, prop))
1485 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1487 RNA_property_float_set(&but->rnapoin, prop, value);
1490 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1491 int ivalue = (int)value;
1492 ivalue ^= RNA_property_enum_get(&but->rnapoin, prop); /* toggle for enum/flag buttons */
1493 RNA_property_enum_set(&but->rnapoin, prop, ivalue);
1496 RNA_property_enum_set(&but->rnapoin, prop, value);
1504 /* we can't be sure what RNA set functions actually do,
1505 * so leave this unset */
1506 value = UI_BUT_VALUE_UNSET;
1508 else if (but->pointype == 0) ;
1509 else if (but->type == HSVSLI) {
1512 fp = (but->editvec) ? but->editvec : (float *)but->poin;
1513 rgb_to_hsv_v(fp, hsv);
1515 switch (but->str[0]) {
1516 case 'H': hsv[0] = value; break;
1517 case 'S': hsv[1] = value; break;
1518 case 'V': hsv[2] = value; break;
1521 hsv_to_rgb_v(hsv, fp);
1525 /* first do rounding */
1526 if (but->pointype == CHA)
1527 value = (char)floor(value + 0.5);
1528 else if (but->pointype == SHO) {
1529 /* gcc 3.2.1 seems to have problems
1530 * casting a double like 32772.0 to
1531 * a short so we cast to an int, then
1534 * Update: even in gcc.4.6 using intermediate int cast gives -32764,
1535 * where as a direct cast from double to short gives -32768,
1536 * if this difference isn't important we could remove this hack,
1537 * since we dont support gcc3 anymore - Campbell */
1539 gcckludge = (int) floor(value + 0.5);
1540 value = (short)gcckludge;
1542 else if (but->pointype == INT)
1543 value = (int)floor(value + 0.5);
1544 else if (but->pointype == FLO) {
1545 float fval = (float)value;
1546 if (fval >= -0.00001f && fval <= 0.00001f) fval = 0.0f; /* prevent negative zero */
1550 /* then set value with possible edit override */
1552 value = *but->editval = value;
1553 else if (but->pointype == CHA)
1554 value = *((char *)but->poin) = (char)value;
1555 else if (but->pointype == SHO)
1556 value = *((short *)but->poin) = (short)value;
1557 else if (but->pointype == INT)
1558 value = *((int *)but->poin) = (int)value;
1559 else if (but->pointype == FLO)
1560 value = *((float *)but->poin) = (float)value;
1563 /* update select flag */
1564 ui_is_but_sel(but, &value);
1567 int ui_get_but_string_max_length(uiBut *but)
1569 if (ELEM(but->type, TEX, SEARCH_MENU))
1570 return but->hardmax;
1571 else if (but->type == IDPOIN)
1572 return MAX_ID_NAME - 2;
1574 return UI_MAX_DRAW_STR;
1577 static double ui_get_but_scale_unit(uiBut *but, double value)
1579 UnitSettings *unit = but->block->unit;
1580 int unit_type = uiButGetUnitType(but);
1582 if (unit_type == PROP_UNIT_LENGTH) {
1583 return value * (double)unit->scale_length;
1585 else if (unit_type == PROP_UNIT_AREA) {
1586 return value * pow(unit->scale_length, 2);
1588 else if (unit_type == PROP_UNIT_VOLUME) {
1589 return value * pow(unit->scale_length, 3);
1591 else if (unit_type == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
1592 Scene *scene = CTX_data_scene(but->block->evil_C);
1593 return FRA2TIME(value);
1600 /* str will be overwritten */
1601 void ui_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen)
1603 if (ui_is_but_unit(but)) {
1604 UnitSettings *unit = but->block->unit;
1605 int unit_type = uiButGetUnitType(but);
1608 orig_str = MEM_callocN(sizeof(char) * maxlen + 1, "textedit sub str");
1609 memcpy(orig_str, str, maxlen);
1611 bUnit_ToUnitAltName(str, maxlen, orig_str, unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
1613 MEM_freeN(orig_str);
1617 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad)
1619 UnitSettings *unit = but->block->unit;
1620 int do_split = unit->flag & USER_UNIT_OPT_SPLIT;
1621 int unit_type = uiButGetUnitType(but);
1622 int precision = but->a2;
1624 if (unit->scale_length < 0.0001f) unit->scale_length = 1.0f; // XXX do_versions
1627 if (precision > PRECISION_FLOAT_MAX) precision = PRECISION_FLOAT_MAX;
1628 else if (precision == 0) precision = 2;
1630 bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision,
1631 unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type), do_split, pad);
1634 static float ui_get_but_step_unit(uiBut *but, float step_default)
1636 int unit_type = RNA_SUBTYPE_UNIT_VALUE(uiButGetUnitType(but));
1639 step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, step_default), but->block->unit->system, unit_type);
1641 if (step > 0.0f) { /* -1 is an error value */
1642 return (float)((double)step / ui_get_but_scale_unit(but, 1.0)) * 100.0f;
1645 return step_default;
1650 void ui_get_but_string(uiBut *but, char *str, size_t maxlen)
1652 if (but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1657 type = RNA_property_type(but->rnaprop);
1659 if (type == PROP_STRING) {
1661 buf = RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen, &buf_len);
1663 else if (type == PROP_POINTER) {
1665 PointerRNA ptr = RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1666 buf = RNA_struct_name_get_alloc(&ptr, str, maxlen, &buf_len);
1672 else if (buf && buf != str) {
1673 /* string was too long, we have to truncate */
1674 memcpy(str, buf, MIN2(maxlen, buf_len + 1));
1678 else if (but->type == IDPOIN) {
1680 if (but->idpoin_idpp) { /* Can be NULL for ID properties by python */
1681 ID *id = *(but->idpoin_idpp);
1683 BLI_strncpy(str, id->name + 2, maxlen);
1690 else if (but->type == TEX) {
1692 BLI_strncpy(str, but->poin, maxlen);
1695 else if (but->type == SEARCH_MENU) {
1697 BLI_strncpy(str, but->poin, maxlen);
1700 else if (ui_but_anim_expression_get(but, str, maxlen))
1701 ; /* driver expression */
1703 /* number editing */
1706 value = ui_get_but_val(but);
1708 if (ui_is_but_float(but)) {
1709 if (ui_is_but_unit(but)) {
1710 ui_get_but_string_unit(but, str, maxlen, value, 0);
1713 const int prec = ui_but_float_precision(but, value);
1714 BLI_snprintf(str, maxlen, "%.*f", prec, value);
1718 BLI_snprintf(str, maxlen, "%d", (int)value);
1724 static int ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *value)
1726 char str_unit_convert[256];
1727 const int unit_type = uiButGetUnitType(but);
1729 BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
1731 /* ugly, use the draw string to get the value,
1732 * this could cause problems if it includes some text which resolves to a unit */
1733 bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr,
1734 ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
1736 return (BPY_button_exec(C, str_unit_convert, value, TRUE) != -1);
1739 #endif /* WITH_PYTHON */
1742 int ui_set_but_string_eval_num(bContext *C, uiBut *but, const char *str, double *value)
1748 if (str[0] != '\0') {
1749 int is_unit_but = ui_is_but_unit(but);
1750 /* only enable verbose if we won't run again with units */
1751 if (BPY_button_exec(C, str, value, is_unit_but == FALSE) != -1) {
1752 /* if the value parsed ok without unit conversion this button may still need a unit multiplier */
1756 BLI_snprintf(str_new, sizeof(str_new), "%f", *value);
1757 ok = ui_set_but_string_eval_num_unit(C, but, str_new, value);
1760 ok = TRUE; /* parse normal string via py (no unit conversion needed) */
1763 else if (is_unit_but) {
1764 /* parse failed, this is a unit but so run replacements and parse again */
1765 ok = ui_set_but_string_eval_num_unit(C, but, str, value);
1769 #else /* WITH_PYTHON */
1777 #endif /* WITH_PYTHON */
1783 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1785 if (but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1786 if (RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1789 type = RNA_property_type(but->rnaprop);
1791 if (type == PROP_STRING) {
1793 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1796 else if (type == PROP_POINTER) {
1798 PointerRNA ptr, rptr;
1801 if (str == NULL || str[0] == '\0') {
1802 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1806 ptr = but->rnasearchpoin;
1807 prop = but->rnasearchprop;
1809 if (prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1810 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1819 else if (but->type == IDPOIN) {
1821 but->idpoin_func(C, str, but->idpoin_idpp);
1824 else if (but->type == TEX) {
1826 if (ui_is_but_utf8(but)) BLI_strncpy_utf8(but->poin, str, but->hardmax);
1827 else BLI_strncpy(but->poin, str, but->hardmax);
1831 else if (but->type == SEARCH_MENU) {
1833 BLI_strncpy(but->poin, str, but->hardmax);
1836 else if (ui_but_anim_expression_set(but, str)) {
1837 /* driver expression */
1840 else if (str[0] == '#') {
1841 /* shortcut to create new driver expression (versus immediate Py-execution) */
1842 return ui_but_anim_expression_create(but, str + 1);
1845 /* number editing */
1848 if (ui_set_but_string_eval_num(C, but, str, &value) == FALSE) {
1852 if (!ui_is_but_float(but)) value = (int)floor(value + 0.5);
1853 if (but->type == NUMABS) value = fabs(value);
1855 /* not that we use hard limits here */
1856 if (value < (double)but->hardmin) value = but->hardmin;
1857 if (value > (double)but->hardmax) value = but->hardmax;
1859 ui_set_but_val(but, value);
1866 void ui_set_but_default(bContext *C, short all)
1870 WM_operator_properties_create(&ptr, "UI_OT_reset_default_button");
1871 RNA_boolean_set(&ptr, "all", all);
1872 WM_operator_name_call(C, "UI_OT_reset_default_button", WM_OP_EXEC_DEFAULT, &ptr);
1873 WM_operator_properties_free(&ptr);
1876 static double soft_range_round_up(double value, double max)
1878 /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1879 double newmax = pow(10.0, ceil(log(value) / M_LN10));
1881 if (newmax * 0.2 >= max && newmax * 0.2 >= value)
1882 return newmax * 0.2;
1883 else if (newmax * 0.5 >= max && newmax * 0.5 >= value)
1884 return newmax * 0.5;
1889 static double soft_range_round_down(double value, double max)
1891 /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1892 double newmax = pow(10.0, floor(log(value) / M_LN10));
1894 if (newmax * 5.0 <= max && newmax * 5.0 <= value)
1895 return newmax * 5.0;
1896 else if (newmax * 2.0 <= max && newmax * 2.0 <= value)
1897 return newmax * 2.0;
1902 void ui_set_but_soft_range(uiBut *but, double value)
1904 /* ideally we would not limit this but practically, its more then
1905 * enough worst case is very long vectors wont use a smart soft-range
1906 * which isn't so bad. */
1909 const PropertyType type = RNA_property_type(but->rnaprop);
1910 double softmin, softmax /*, step, precision*/;
1911 double value_min = value;
1912 double value_max = value;
1914 /* clamp button range to something reasonable in case
1915 * we get -inf/inf from RNA properties */
1916 if (type == PROP_INT) {
1917 int imin, imax, istep;
1918 const int array_len = RNA_property_array_length(&but->rnapoin, but->rnaprop);
1920 RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1921 softmin = (imin == INT_MIN) ? -1e4 : imin;
1922 softmax = (imin == INT_MAX) ? 1e4 : imax;
1923 /*step= istep;*/ /*UNUSED*/
1924 /*precision= 1;*/ /*UNUSED*/
1926 if (array_len >= 2) {
1928 RNA_property_int_get_array_range(&but->rnapoin, but->rnaprop, value_range);
1929 value_min = (double)value_range[0];
1930 value_max = (double)value_range[1];
1933 else if (type == PROP_FLOAT) {
1934 float fmin, fmax, fstep, fprecision;
1935 const int array_len = RNA_property_array_length(&but->rnapoin, but->rnaprop);
1937 RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1938 softmin = (fmin == -FLT_MAX) ? (float)-1e4 : fmin;
1939 softmax = (fmax == FLT_MAX) ? (float)1e4 : fmax;
1940 /*step= fstep;*/ /*UNUSED*/
1941 /*precision= fprecision;*/ /*UNUSED*/
1943 if (array_len >= 2) {
1944 float value_range[2];
1945 RNA_property_float_get_array_range(&but->rnapoin, but->rnaprop, value_range);
1946 value_min = (double)value_range[0];
1947 value_max = (double)value_range[1];
1953 /* if the value goes out of the soft/max range, adapt the range */
1954 if (value_min + 1e-10 < softmin) {
1955 if (value_min < 0.0)
1956 softmin = -soft_range_round_up(-value_min, -softmin);
1958 softmin = soft_range_round_down(value_min, softmin);
1960 if (softmin < (double)but->hardmin)
1961 softmin = (double)but->hardmin;
1963 if (value_max - 1e-10 > softmax) {
1964 if (value_max < 0.0)
1965 softmax = -soft_range_round_down(-value_max, -softmax);
1967 softmax = soft_range_round_up(value_max, softmax);
1969 if (softmax > (double)but->hardmax)
1970 softmax = but->hardmax;
1973 but->softmin = softmin;
1974 but->softmax = softmax;
1978 /* ******************* Free ********************/
1980 static void ui_free_link(uiLink *link)
1983 BLI_freelistN(&link->lines);
1988 /* can be called with C==NULL */
1989 static void ui_free_but(const bContext *C, uiBut *but)
1992 WM_operator_properties_free(but->opptr);
1993 MEM_freeN(but->opptr);
1996 if (but->func_argN) {
1997 MEM_freeN(but->func_argN);
2001 /* XXX solve later, buttons should be free-able without context ideally,
2002 * however they may have open tooltips or popup windows, which need to
2003 * be closed using a context pointer */
2005 ui_button_active_free(C, but);
2009 MEM_freeN(but->active);
2013 if (but->str && but->str != but->strdata) {
2014 MEM_freeN(but->str);
2016 ui_free_link(but->link);
2018 if ((but->type == BUT_IMAGE) && but->poin) {
2019 IMB_freeImBuf((struct ImBuf *)but->poin);
2025 /* can be called with C==NULL */
2026 void uiFreeBlock(const bContext *C, uiBlock *block)
2030 while ( (but = block->buttons.first) ) {
2031 BLI_remlink(&block->buttons, but);
2032 ui_free_but(C, but);
2036 MEM_freeN(block->unit);
2039 if (block->func_argN) {
2040 MEM_freeN(block->func_argN);
2043 CTX_store_free_list(&block->contexts);
2045 BLI_freelistN(&block->saferct);
2050 /* can be called with C==NULL */
2051 void uiFreeBlocks(const bContext *C, ListBase *lb)
2055 while ( (block = lb->first) ) {
2056 BLI_remlink(lb, block);
2057 uiFreeBlock(C, block);
2061 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
2063 uiBlock *block, *nextblock;
2065 for (block = lb->first; block; block = nextblock) {
2066 nextblock = block->next;
2068 if (!block->handle) {
2069 if (!block->active) {
2070 BLI_remlink(lb, block);
2071 uiFreeBlock(C, block);
2079 void uiBlockSetRegion(uiBlock *block, ARegion *region)
2081 ListBase *lb = ®ion->uiblocks;
2082 uiBlock *oldblock = NULL;
2084 /* each listbase only has one block with this name, free block
2085 * if is already there so it can be rebuilt from scratch */
2087 oldblock = BLI_findstring(lb, block->name, offsetof(uiBlock, name));
2090 oldblock->active = 0;
2091 oldblock->panel = NULL;
2094 /* at the beginning of the list! for dynamical menus/blocks */
2095 BLI_addhead(lb, block);
2098 block->oldblock = oldblock;
2101 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
2106 int getsizex, getsizey;
2108 window = CTX_wm_window(C);
2109 scn = CTX_data_scene(C);
2111 block = MEM_callocN(sizeof(uiBlock), "uiBlock");
2114 block->evil_C = (void *)C; /* XXX */
2117 block->color_profile = (scn->r.color_mgt_flag & R_COLOR_MANAGEMENT);
2119 /* copy to avoid crash when scene gets deleted with ui still open */
2120 block->unit = MEM_mallocN(sizeof(scn->unit), "UI UnitSettings");
2121 memcpy(block->unit, &scn->unit, sizeof(scn->unit));
2124 BLI_strncpy(block->name, name, sizeof(block->name));
2127 uiBlockSetRegion(block, region);
2129 /* window matrix and aspect */
2130 if (region && region->swinid) {
2131 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
2132 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
2134 /* TODO - investigate why block->winmat[0][0] is negative
2135 * in the image view when viewRedrawForce is called */
2136 block->aspect = 2.0f / fabsf(getsizex * block->winmat[0][0]);
2139 /* no subwindow created yet, for menus for example, so we
2140 * use the main window instead, since buttons are created
2142 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
2143 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
2145 block->aspect = 2.0f / fabsf(getsizex * block->winmat[0][0]);
2146 block->auto_open = TRUE;
2147 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
2153 uiBlock *uiGetBlock(const char *name, ARegion *ar)
2155 return BLI_findstring(&ar->uiblocks, name, offsetof(uiBlock, name));
2158 void uiBlockSetEmboss(uiBlock *block, char dt)
2163 void ui_check_but(uiBut *but)
2165 /* if something changed in the button */
2166 double value = UI_BUT_VALUE_UNSET;
2167 // float okwidth; // UNUSED
2169 ui_is_but_sel(but, &value);
2171 /* only update soft range while not editing */
2172 if (but->rnaprop && !(but->editval || but->editstr || but->editvec)) {
2173 UI_GET_BUT_VALUE_INIT(but, value);
2174 ui_set_but_soft_range(but, value);
2177 /* test for min and max, icon sliders, etc */
2178 switch (but->type) {
2184 UI_GET_BUT_VALUE_INIT(but, value);
2185 if (value < (double)but->hardmin) ui_set_but_val(but, but->hardmin);
2186 else if (value > (double)but->hardmax) ui_set_but_val(but, but->hardmax);
2192 UI_GET_BUT_VALUE_INIT(but, value);
2193 value_abs = fabs(value);
2194 if (value_abs < (double)but->hardmin) ui_set_but_val(but, but->hardmin);
2195 else if (value_abs > (double)but->hardmax) ui_set_but_val(but, but->hardmax);
2200 if (!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2201 if (but->flag & UI_SELECT) but->iconadd = 1;
2202 else but->iconadd = 0;
2207 if (!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2208 UI_GET_BUT_VALUE_INIT(but, value);
2209 but->iconadd = (int)value - (int)(but->hardmin);
2214 if (!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2215 UI_GET_BUT_VALUE_INIT(but, value);
2216 but->iconadd = (int)value - (int)(but->hardmin);
2222 /* safety is 4 to enable small number buttons (like 'users') */
2223 // okwidth= -4 + (BLI_RCT_SIZE_X(&but->rect)); // UNUSED
2226 switch (but->type) {
2231 if (BLI_RCT_SIZE_X(&but->rect) > 24.0f) {
2232 UI_GET_BUT_VALUE_INIT(but, value);
2233 ui_set_name_menu(but, (int)value);
2242 UI_GET_BUT_VALUE_INIT(but, value);
2244 if (ui_is_but_float(but)) {
2245 if (value == (double) FLT_MAX) {
2246 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%sinf", but->str);
2248 else if (value == (double) -FLT_MAX) {
2249 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s-inf", but->str);
2251 /* support length type buttons */
2252 else if (ui_is_but_unit(but)) {
2253 char new_str[sizeof(but->drawstr)];
2254 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE);
2255 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, new_str);
2258 const int prec = ui_but_float_precision(but, value);
2259 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2263 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%d", but->str, (int)value);
2267 PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2269 if (pstype == PROP_PERCENTAGE)
2270 strcat(but->drawstr, "%");
2275 if (ui_is_but_float(but)) {
2277 UI_GET_BUT_VALUE_INIT(but, value);
2278 prec = ui_but_float_precision(but, value);
2279 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2282 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2290 if (!but->editstr) {
2291 char str[UI_MAX_DRAW_STR];
2293 ui_get_but_string(but, str, UI_MAX_DRAW_STR - strlen(but->str));
2295 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, str);
2300 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2301 if (but->flag & UI_SELECT) {
2302 strcat(but->drawstr, "Press a key");
2305 UI_GET_BUT_VALUE_INIT(but, value);
2306 strcat(but->drawstr, WM_key_event_string((short)value));
2311 if (but->flag & UI_SELECT) {
2312 but->drawstr[0] = '\0';
2314 if (but->modifier_key) {
2315 char *str = but->drawstr;
2317 if (but->modifier_key & KM_SHIFT)
2318 str = strcat(str, "Shift ");
2319 if (but->modifier_key & KM_CTRL)
2320 str = strcat(str, "Ctrl ");
2321 if (but->modifier_key & KM_ALT)
2322 str = strcat(str, "Alt ");
2323 if (but->modifier_key & KM_OSKEY)
2324 str = strcat(str, "Cmd ");
2326 (void)str; /* UNUSED */
2329 strcat(but->drawstr, "Press a key ");
2332 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2337 /* trying to get the dual-icon to left of text... not very nice */
2339 BLI_strncpy(but->drawstr, " ", UI_MAX_DRAW_STR);
2340 BLI_strncpy(but->drawstr + 2, but->str, UI_MAX_DRAW_STR - 2);
2348 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2352 /* if we are doing text editing, this will override the drawstr */
2354 BLI_strncpy(but->drawstr, but->editstr, UI_MAX_DRAW_STR);
2356 /* text clipping moved to widget drawing code itself */
2360 void uiBlockBeginAlign(uiBlock *block)
2362 /* if other align was active, end it */
2363 if (block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2365 block->flag |= UI_BUT_ALIGN_DOWN;
2368 /* buttons declared after this call will get this align nr */ // XXX flag?
2371 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2375 dx = fabs(but1->rect.xmax - but2->rect.xmin);
2376 dy = fabs(but1->rect.ymin - but2->rect.ymax);
2378 if (dx > dy) return 0;
2382 void uiBlockEndAlign(uiBlock *block)
2384 block->flag &= ~UI_BUT_ALIGN; /* all 4 flags */
2387 int ui_but_can_align(uiBut *but)
2389 return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2392 static void ui_block_do_align_but(uiBut *first, short nr)
2394 uiBut *prev, *but = NULL, *next;
2395 int flag = 0, cols = 0, rows = 0;
2399 for (but = first; but && but->alignnr == nr; but = but->next) {
2400 if (but->next && but->next->alignnr == nr) {
2401 if (buts_are_horiz(but, but->next)) cols++;
2406 /* rows==0: 1 row, cols==0: 1 column */
2408 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2409 for (but = first, prev = NULL; but && but->alignnr == nr; prev = but, but = but->next) {
2411 if (next && next->alignnr != nr)
2414 /* clear old flag */
2415 but->flag &= ~UI_BUT_ALIGN;
2417 if (flag == 0) { /* first case */
2419 if (buts_are_horiz(but, next)) {
2421 flag = UI_BUT_ALIGN_RIGHT;
2423 flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_RIGHT;
2426 flag = UI_BUT_ALIGN_DOWN;
2430 else if (next == NULL) { /* last case */
2432 if (buts_are_horiz(prev, but)) {
2434 flag = UI_BUT_ALIGN_LEFT;
2436 flag = UI_BUT_ALIGN_TOP | UI_BUT_ALIGN_LEFT;
2438 else flag = UI_BUT_ALIGN_TOP;
2441 else if (buts_are_horiz(but, next)) {
2442 /* check if this is already second row */
2443 if (prev && buts_are_horiz(prev, but) == 0) {
2444 flag &= ~UI_BUT_ALIGN_LEFT;
2445 flag |= UI_BUT_ALIGN_TOP;
2446 /* exception case: bottom row */
2449 while (bt && bt->alignnr == nr) {
2450 if (bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next) == 0) break;
2453 if (bt == NULL || bt->alignnr != nr) flag = UI_BUT_ALIGN_TOP | UI_BUT_ALIGN_RIGHT;
2456 else flag |= UI_BUT_ALIGN_LEFT;
2460 flag |= UI_BUT_ALIGN_TOP;
2462 else { /* next button switches to new row */
2464 if (prev && buts_are_horiz(prev, but))
2465 flag |= UI_BUT_ALIGN_LEFT;
2467 flag &= ~UI_BUT_ALIGN_LEFT;
2468 flag |= UI_BUT_ALIGN_TOP;
2471 if ( (flag & UI_BUT_ALIGN_TOP) == 0) { /* stil top row */
2473 if (next && buts_are_horiz(but, next))
2474 flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_LEFT | UI_BUT_ALIGN_RIGHT;
2476 /* last button in top row */
2477 flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_LEFT;
2481 flag |= UI_BUT_ALIGN_DOWN;
2484 flag |= UI_BUT_ALIGN_TOP;
2490 /* merge coordinates */
2494 but->rect.xmin = (prev->rect.xmax + but->rect.xmin) / 2.0f;
2495 prev->rect.xmax = but->rect.xmin;
2497 else if (cols == 0) {
2498 but->rect.ymax = (prev->rect.ymin + but->rect.ymax) / 2.0f;
2499 prev->rect.ymin = but->rect.ymax;
2502 if (buts_are_horiz(prev, but)) {
2503 but->rect.xmin = (prev->rect.xmax + but->rect.xmin) / 2.0f;
2504 prev->rect.xmax = but->rect.xmin;
2505 /* copy height too */
2506 but->rect.ymax = prev->rect.ymax;
2508 else if (prev->prev && buts_are_horiz(prev->prev, prev) == 0) {
2509 /* the previous button is a single one in its row */
2510 but->rect.ymax = (prev->rect.ymin + but->rect.ymax) / 2.0f;
2511 prev->rect.ymin = but->rect.ymax;
2513 but->rect.xmin = prev->rect.xmin;
2514 if (next && buts_are_horiz(but, next) == 0)
2515 but->rect.xmax = prev->rect.xmax;
2518 /* the previous button is not a single one in its row */
2519 but->rect.ymax = prev->rect.ymin;
2526 void ui_block_do_align(uiBlock *block)
2531 /* align buttons with same align nr */
2532 for (but = block->buttons.first; but; ) {
2535 ui_block_do_align_but(but, nr);
2537 /* skip with same number */
2538 for (; but && but->alignnr == nr; but = but->next) ;
2549 * \brief ui_def_but is the function that draws many button types
2551 * \param x,y The lower left hand corner of the button (X axis)
2552 * \param width,height The size of the button.
2554 * for float buttons:
2555 * - \a a1 Click Step (how much to change the value each click)
2556 * - \a a2 Number of decimal point values to display. 0 defaults to 3 (0.000)
2557 * 1,2,3, and a maximum of 4, all greater values will be clamped to 4.
2559 static uiBut *ui_def_but(uiBlock *block, int type, int retval, const char *str,
2560 int x, int y, short width, short height,
2561 void *poin, float min, float max, float a1, float a2, const char *tip)
2566 if (type & BUTPOIN) { /* a pointer is required */
2571 but = MEM_callocN(sizeof(uiBut), "uiBut");
2573 but->type = type & BUTTYPE;
2574 but->pointype = type & BUTPOIN;
2575 but->bit = type & BIT;
2576 but->bitnr = type & 31;
2577 but->icon = ICON_NONE;
2580 but->retval = retval;
2583 if (slen >= UI_MAX_NAME_STR - 1) {
2584 but->str = MEM_mallocN(slen + 2, "ui_def_but str"); /* why +2 ? */
2587 but->str = but->strdata;
2589 memcpy(but->str, str, slen + 1);
2593 but->rect.xmax = but->rect.xmin + width;
2594 but->rect.ymax = but->rect.ymin + height;
2597 but->hardmin = but->softmin = min;
2598 but->hardmax = but->softmax = max;
2603 but->lock = block->lock;
2604 but->lockstr = block->lockstr;
2605 but->dt = block->dt;
2607 but->aspect = 1.0f; /* XXX block->aspect; */
2608 but->block = block; /* pointer back, used for frontbuffer status, and picker */
2610 if ((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2611 but->alignnr = block->alignnr;
2613 but->func = block->func;
2614 but->func_arg1 = block->func_arg1;
2615 but->func_arg2 = block->func_arg2;
2617 but->funcN = block->funcN;
2618 if (block->func_argN)
2619 but->func_argN = MEM_dupallocN(block->func_argN);
2621 but->pos = -1; /* cursor invisible */
2623 if (ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) { /* add a space to name */
2624 /* slen remains unchanged from previous assignment, ensure this stays true */
2625 if (slen > 0 && slen < UI_MAX_NAME_STR - 2) {
2626 if (but->str[slen - 1] != ' ') {
2627 but->str[slen] = ' ';
2628 but->str[slen + 1] = 0;
2633 if ((block->flag & UI_BLOCK_LOOP) ||
2634 ELEM8(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU, PROGRESSBAR))
2636 but->flag |= (UI_TEXT_LEFT | UI_ICON_LEFT);
2638 else if (but->type == BUT_TOGDUAL) {
2639 but->flag |= UI_ICON_LEFT;
2642 but->flag |= (block->flag & UI_BUT_ALIGN);
2644 if (but->lock == TRUE) {
2646 but->flag |= UI_BUT_DISABLED;
2650 /* keep track of UI_interface.h */
2651 if (ELEM7(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, BUTM)) ;
2652 else if (ELEM3(but->type, SCROLL, SEPR, FTPREVIEW)) ;
2653 else if (but->type >= SEARCH_MENU) ;
2654 else but->flag |= UI_BUT_UNDO;
2656 BLI_addtail(&block->buttons, but);
2658 if (block->curlayout)
2659 ui_layout_add_but(block->curlayout, but);
2662 /* if the 'UI_OT_editsource' is running, extract the source info from the button */
2663 if (UI_editsource_enable_check()) {
2664 UI_editsource_active_but_test(but);
2671 /* ui_def_but_rna_propname and ui_def_but_rna
2672 * both take the same args except for propname vs prop, this is done so we can
2673 * avoid an extra lookup on 'prop' when its already available.
2675 * When this kind of change won't disrupt branches, best look into making more
2676 * of our UI functions take prop rather then propname.
2679 #define UI_DEF_BUT_RNA_DISABLE(but) { \
2680 but->flag |= UI_BUT_DISABLED; \
2682 but->lockstr = ""; \
2686 static uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, const char *str,
2687 int x, int y, short width, short height,
2688 PointerRNA *ptr, PropertyRNA *prop, int index,
2689 float min, float max, float a1, float a2, const char *tip)
2691 const PropertyType proptype = RNA_property_type(prop);
2693 int freestr = 0, icon = 0;
2695 /* use rna values if parameters are not specified */
2697 if (type == MENU && proptype == PROP_ENUM) {
2698 EnumPropertyItem *item;
2700 int i, totitem, value, free;
2702 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item, &totitem, &free);
2703 value = RNA_property_enum_get(ptr, prop);
2705 dynstr = BLI_dynstr_new();
2706 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2707 for (i = 0; i < totitem; i++) {
2708 if (!item[i].identifier[0]) {
2710 BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2712 BLI_dynstr_append(dynstr, "|%l");
2714 else if (item[i].icon)
2715 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2717 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2719 if (value == item[i].value)
2720 icon = item[i].icon;
2722 str = BLI_dynstr_get_cstring(dynstr);
2723 BLI_dynstr_free(dynstr);
2731 else if (ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2732 EnumPropertyItem *item;
2733 int i, totitem, free;
2735 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item, &totitem, &free);
2736 for (i = 0; i < totitem; i++) {
2737 if (item[i].identifier[0] && item[i].value == (int)max) {
2739 icon = item[i].icon;
2744 str = RNA_property_ui_name(prop);
2751 str = RNA_property_ui_name(prop);
2752 icon = RNA_property_ui_icon(prop);
2756 if (!tip && proptype != PROP_ENUM)
2757 tip = RNA_property_ui_description(prop);
2759 if (min == max || a1 == -1 || a2 == -1) {
2760 if (proptype == PROP_INT) {
2761 int hardmin, hardmax, softmin, softmax, step;
2763 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2764 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2766 if (!ELEM(type, ROW, LISTROW) && min == max) {
2775 else if (proptype == PROP_FLOAT) {
2776 float hardmin, hardmax, softmin, softmax, step, precision;
2778 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2779 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2781 if (!ELEM(type, ROW, LISTROW) && min == max) {
2790 else if (proptype == PROP_STRING) {
2792 max = RNA_property_string_maxlength(prop);
2793 if (max == 0) /* interface code should ideally support unlimited length */
2794 max = UI_MAX_DRAW_STR;
2798 /* now create button */
2799 but = ui_def_but(block, type, retval, str, x, y, width, height, NULL, min, max, a1, a2, tip);
2801 but->rnapoin = *ptr;
2802 but->rnaprop = prop;
2804 if (RNA_property_array_length(&but->rnapoin, but->rnaprop))
2805 but->rnaindex = index;
2810 but->icon = (BIFIconID)icon;
2811 but->flag |= UI_HAS_ICON;
2812 but->flag |= UI_ICON_LEFT;
2815 if (!RNA_property_editable(&but->rnapoin, prop)) {
2816 UI_DEF_BUT_RNA_DISABLE(but);
2819 if (but->flag & UI_BUT_UNDO && (ui_but_is_rna_undo(but) == FALSE)) {
2820 but->flag &= ~UI_BUT_UNDO;
2823 /* If this button uses units, calculate the step from this */
2824 if ((proptype == PROP_FLOAT) && ui_is_but_unit(but)) {
2825 but->a1 = ui_get_but_step_unit(but, but->a1);
2829 MEM_freeN((void *)str);
2835 static uiBut *ui_def_but_rna_propname(uiBlock *block, int type, int retval, const char *str, int x, int y, short width, short height, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, const char *tip)
2837 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2841 but = ui_def_but_rna(block, type, retval, str, x, y, width, height, ptr, prop, index, min, max, a1, a2, tip);
2844 but = ui_def_but(block, type, retval, propname, x, y, width, height, NULL, min, max, a1, a2, tip);
2846 UI_DEF_BUT_RNA_DISABLE(but);
2852 static uiBut *ui_def_but_operator_ptr(uiBlock *block, int type, wmOperatorType *ot, int opcontext, const char *str, int x, int y, short width, short height, const char *tip)
2858 str = RNA_struct_ui_name(ot->srna);
2863 if ((!tip || tip[0] == '\0') && ot && ot->srna) {
2864 tip = RNA_struct_ui_description(ot->srna);
2867 but = ui_def_but(block, type, -1, str, x, y, width, height, NULL, 0, 0, 0, 0, tip);
2869 but->opcontext = opcontext;
2870 but->flag &= ~UI_BUT_UNDO; /* no need for ui_but_is_undo(), we never need undo here */
2873 but->flag |= UI_BUT_DISABLED;
2882 static uiBut *UNUSED_FUNCTION(ui_def_but_operator) (uiBlock * block, int type, const char *opname, int opcontext, const char *str, int x, int y, short width, short height, const char *tip)
2884 wmOperatorType *ot = WM_operatortype_find(opname, 0);
2885 if (str == NULL && ot == NULL) str = opname;
2886 return ui_def_but_operator_ptr(block, type, ot, opcontext, str, x, y, width, height, tip);
2890 static uiBut *ui_def_but_operator_text(uiBlock *block, int type, const char *opname, int opcontext, const char *str, int x, int y, short width, short height, void *poin, float min, float max, float a1, float a2, const char *tip)
2895 ot = WM_operatortype_find(opname, 0);
2898 if (ot) str = ot->name;
2902 if ((!tip || tip[0] == '\0') && ot && ot->description) {
2903 tip = ot->description;
2906 but = ui_def_but(block, type, -1, str, x, y, width, height, poin, min, max, a1, a2, tip);
2908 but->opcontext = opcontext;
2909 but->flag &= ~UI_BUT_UNDO; /* no need for ui_but_is_undo(), we never need undo here */
2912 but->flag |= UI_BUT_DISABLED;
2920 uiBut *uiDefBut(uiBlock *block, int type, int retval, const char *str, int x, int y, short width, short height, void *poin, float min, float max, float a1, float a2, const char *tip)
2922 uiBut *but = ui_def_but(block, type, retval, str, x, y, width, height, poin, min, max, a1, a2, tip);
2929 /* if _x_ is a power of two (only one bit) return the power,
2930 * otherwise return -1.
2931 * (1<<findBitIndex(x))==x for powers of two.
2933 static int findBitIndex(unsigned int x)
2935 if (!x || !is_power_of_2_i(x)) { /* is_power_of_2_i(x) strips lowest bit */
2941 if (x & 0xFFFF0000) idx += 16, x >>= 16;
2942 if (x & 0xFF00) idx += 8, x >>= 8;
2943 if (x & 0xF0) idx += 4, x >>= 4;
2944 if (x & 0xC) idx += 2, x >>= 2;
2945 if (x & 0x2) idx += 1;
2951 /* autocomplete helper functions */
2952 struct AutoComplete {
2955 const char *startname;
2958 AutoComplete *autocomplete_begin(const char *startname, size_t maxlen)
2960 AutoComplete *autocpl;
2962 autocpl = MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2963 autocpl->maxlen = maxlen;
2964 autocpl->truncate = MEM_callocN(sizeof(char) * maxlen, "AutoCompleteTruncate");
2965 autocpl->startname = startname;
2970 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2972 char *truncate = autocpl->truncate;
2973 const char *startname = autocpl->startname;
2976 for (a = 0; a < autocpl->maxlen - 1; a++) {
2977 if (startname[a] == 0 || startname[a] != name[a])
2981 if (startname[a] == 0) {
2983 if (truncate[0] == 0)
2984 BLI_strncpy(truncate, name, autocpl->maxlen);
2986 /* remove from truncate what is not in bone->name */
2987 for (a = 0; a < autocpl->maxlen - 1; a++) {
2992 else if (truncate[a] != name[a])
2999 void autocomplete_end(AutoComplete *autocpl, char *autoname)
3001 if (autocpl->truncate[0])
3002 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
3004 if (autoname != autocpl->startname) /* don't copy a string over its self */
3005 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
3007 MEM_freeN(autocpl->truncate);
3011 /* autocomplete callback for ID buttons */
3012 static void autocomplete_id(bContext *C, char *str, void *arg_v)
3014 int blocktype = (intptr_t)arg_v;
3015 ListBase *listb = which_libbase(CTX_data_main(C), blocktype);
3017 if (listb == NULL) return;
3019 /* search if str matches the beginning of an ID struct */
3021 AutoComplete *autocpl = autocomplete_begin(str, MAX_ID_NAME - 2);
3024 for (id = listb->first; id; id = id->next)
3025 autocomplete_do_name(autocpl, id->name + 2);
3027 autocomplete_end(autocpl, str);
3031 static void ui_check_but_and_iconize(uiBut *but, int icon)
3034 but->icon = (BIFIconID) icon;
3035 but->flag |= UI_HAS_ICON;
3041 static uiBut *uiDefButBit(uiBlock *block, int type, int bit, int retval, const char *str, int x, int y, short width, short height, void *poin, float min, float max, float a1, float a2, const char *tip)
3043 int bitIdx = findBitIndex(bit);
3048 return uiDefBut(block, type | BIT | bitIdx, retval, str, x, y, width, height, poin, min, max, a1, a2, tip);
3051 uiBut *uiDefButF(uiBlock *block, int type, int retval, const char *str, int x, int y, short width, short height, float *poin, float min, float max, float a1, float a2, const char *tip)
3053 return uiDefBut(block, type | FLO, retval, str, x, y, width, height, (void *) poin, min, max, a1, a2, tip);
3055 uiBut *uiDefButBitF(uiBlock *block, int type, int bit, int retval, const char *str, int x, int y, short width, short height, float *poin, float min, float max, float a1, float a2, const char *tip)
3057 return uiDefButBit(block, type | FLO, bit, retval, str, x, y, width, height, (void *) poin, min, max, a1, a2, tip);
3059 uiBut *uiDefButI(uiBlock *block, int type, int retval, const char *str, int x, int y, short width, short height, int *poin, float min, float max, float a1, float a2, const char *tip)
3061 return uiDefBut(block, type | INT, retval, str, x, y, width, height, (void *) poin, min, max, a1, a2, tip);
3063 uiBut *uiDefButBitI(uiBlock *block, int type, int bit, int retval, const char *str, int x, int y, short width, short height, int *poin, float min, float max, float a1, float a2, const char *tip)
3065 return uiDefButBit(block, type | INT, bit, retval, str, x, y, width, height, (void *) poin, min, max, a1, a2, tip);
3067 uiBut *uiDefButS(uiBlock *block, int type, int retval, const char *str, int x, int y, short width, short height, short *poin, float min, float max, float a1, float a2, const char *tip)
3069 return uiDefBut(block, type | SHO, retval, str, x, y, width, height, (void *) poin, min, max, a1, a2, tip);
3071 uiBut *uiDefButBitS(uiBlock *block, int type, int bit, int retval, const char *str, int x, int y, short width, short height, short *poin, float min, float max, float a1, float a2, const char *tip)
3073 return uiDefButBit(block, type | SHO, bit, retval, str, x, y, width, height, (void *) poin, min, max, a1, a2, tip);
3075 uiBut *uiDefButC(uiBlock *block, int type, int retval, const char *str, int x, int y, short width, short height, char *poin, float min, float max, float a1, float a2, const char *tip)
3077 return uiDefBut(block, type | CHA, retval, str, x, y, width, height, (void *) poin, min, max, a1, a2, tip);
3079 uiBut *uiDefButBitC(uiBlock *block, int type, int bit, int retval, const char *str, int x, int y, short width, short height, char *poin, float min, float max, float a1, float a2, const char *tip)
3081 return uiDefButBit(block, type | CHA, bit, retval, str, x, y, width, height, (void *) poin, min, max, a1, a2, tip);
3083 uiBut *uiDefButR(uiBlock *block, int type, int retval, const char *str, int x, int y, short width, short height, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, const char *tip)