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_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_userdef_types.h"
46 #include "BLI_listbase.h"
47 #include "BLI_string.h"
48 #include "BLI_string_utf8.h"
51 #include "BLI_utildefines.h"
53 #include "BKE_context.h"
55 #include "BKE_scene.h"
56 #include "BKE_screen.h"
57 #include "BKE_idprop.h"
62 #include "BLF_translation.h"
64 #include "UI_interface.h"
66 #include "IMB_imbuf.h"
70 #include "wm_subwindow.h"
72 #include "RNA_access.h"
74 #include "BPY_extern.h"
76 #include "IMB_colormanagement.h"
78 #include "interface_intern.h"
80 /* avoid unneeded calls to ui_but_value_get */
81 #define UI_BUT_VALUE_UNSET DBL_MAX
82 #define UI_GET_BUT_VALUE_INIT(_but, _value) if (_value == DBL_MAX) { (_value) = ui_but_value_get(_but); } (void)0
87 * a full doc with API notes can be found in bf-blender/trunk/blender/doc/guides/interface_API.txt
89 * uiBlahBlah() external function
90 * ui_blah_blah() internal function
93 static void ui_but_free(const bContext *C, uiBut *but);
95 bool ui_block_is_menu(const uiBlock *block)
97 return (((block->flag & UI_BLOCK_LOOP) != 0) &&
98 /* non-menu popups use keep-open, so check this is off */
99 ((block->flag & UI_BLOCK_KEEP_OPEN) == 0));
102 bool ui_block_is_pie_menu(const uiBlock *block)
104 return ((block->flag & UI_BLOCK_RADIAL) != 0);
107 static bool ui_but_is_unit_radians_ex(UnitSettings *unit, const int unit_type)
109 return (unit->system_rotation == USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION);
112 static bool ui_but_is_unit_radians(const uiBut *but)
114 UnitSettings *unit = but->block->unit;
115 const int unit_type = UI_but_unit_type_get(but);
117 return ui_but_is_unit_radians_ex(unit, unit_type);
120 /* ************* window matrix ************** */
122 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
125 int sx, sy, getsizex, getsizey;
127 getsizex = BLI_rcti_size_x(&ar->winrct) + 1;
128 getsizey = BLI_rcti_size_y(&ar->winrct) + 1;
129 sx = ar->winrct.xmin;
130 sy = ar->winrct.ymin;
136 gx += block->panel->ofsx;
137 gy += block->panel->ofsy;
140 *x = ((float)sx) + ((float)getsizex) * (0.5f + 0.5f * (gx * block->winmat[0][0] + gy * block->winmat[1][0] + block->winmat[3][0]));
141 *y = ((float)sy) + ((float)getsizey) * (0.5f + 0.5f * (gx * block->winmat[0][1] + gy * block->winmat[1][1] + block->winmat[3][1]));
144 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
151 ui_block_to_window_fl(ar, block, &fx, &fy);
153 *x = (int)(fx + 0.5f);
154 *y = (int)(fy + 0.5f);
157 void ui_block_to_window_rctf(const ARegion *ar, uiBlock *block, rctf *rct_dst, const rctf *rct_src)
160 ui_block_to_window_fl(ar, block, &rct_dst->xmin, &rct_dst->ymin);
161 ui_block_to_window_fl(ar, block, &rct_dst->xmax, &rct_dst->ymax);
164 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y) /* for mouse cursor */
166 float a, b, c, d, e, f, px, py;
167 int sx, sy, getsizex, getsizey;
169 getsizex = BLI_rcti_size_x(&ar->winrct) + 1;
170 getsizey = BLI_rcti_size_y(&ar->winrct) + 1;
171 sx = ar->winrct.xmin;
172 sy = ar->winrct.ymin;
174 a = 0.5f * ((float)getsizex) * block->winmat[0][0];
175 b = 0.5f * ((float)getsizex) * block->winmat[1][0];
176 c = 0.5f * ((float)getsizex) * (1.0f + block->winmat[3][0]);
178 d = 0.5f * ((float)getsizey) * block->winmat[0][1];
179 e = 0.5f * ((float)getsizey) * block->winmat[1][1];
180 f = 0.5f * ((float)getsizey) * (1.0f + block->winmat[3][1]);
185 *y = (a * (py - f) + d * (c - px)) / (a * e - d * b);
186 *x = (px - b * (*y) - c) / a;
189 *x -= block->panel->ofsx;
190 *y -= block->panel->ofsy;
194 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
201 ui_window_to_block_fl(ar, block, &fx, &fy);
203 *x = (int)(fx + 0.5f);
204 *y = (int)(fy + 0.5f);
207 void ui_window_to_region(const ARegion *ar, int *x, int *y)
209 *x -= ar->winrct.xmin;
210 *y -= ar->winrct.ymin;
213 void ui_region_to_window(const ARegion *ar, int *x, int *y)
215 *x += ar->winrct.xmin;
216 *y += ar->winrct.ymin;
218 /* ******************* block calc ************************* */
220 void ui_block_translate(uiBlock *block, int x, int y)
224 for (but = block->buttons.first; but; but = but->next) {
225 BLI_rctf_translate(&but->rect, x, y);
228 BLI_rctf_translate(&block->rect, x, y);
231 static void ui_block_bounds_calc_text(uiBlock *block, float offset)
233 uiStyle *style = UI_style_get();
234 uiBut *bt, *init_col_bt, *col_bt;
235 int i = 0, j, x1addval = offset;
237 UI_fontstyle_set(&style->widget);
239 for (init_col_bt = bt = block->buttons.first; bt; bt = bt->next) {
240 if (!ELEM(bt->type, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE)) {
241 j = BLF_width(style->widget.uifont_id, bt->drawstr, sizeof(bt->drawstr));
247 if (bt->next && bt->rect.xmin < bt->next->rect.xmin) {
248 /* End of this column, and it’s not the last one. */
249 for (col_bt = init_col_bt; col_bt->prev != bt; col_bt = col_bt->next) {
250 col_bt->rect.xmin = x1addval;
251 col_bt->rect.xmax = x1addval + i + block->bounds;
253 ui_but_update(col_bt); /* clips text again */
256 /* And we prepare next column. */
257 x1addval += i + block->bounds;
259 init_col_bt = col_bt;
264 for (col_bt = init_col_bt; col_bt; col_bt = col_bt->next) {
265 col_bt->rect.xmin = x1addval;
266 col_bt->rect.xmax = max_ff(x1addval + i + block->bounds, offset + block->minbounds);
268 ui_but_update(col_bt); /* clips text again */
272 void ui_block_bounds_calc(uiBlock *block)
277 if (BLI_listbase_is_empty(&block->buttons)) {
279 block->rect.xmin = 0.0; block->rect.xmax = block->panel->sizex;
280 block->rect.ymin = 0.0; block->rect.ymax = block->panel->sizey;
285 BLI_rctf_init_minmax(&block->rect);
287 for (bt = block->buttons.first; bt; bt = bt->next) {
288 BLI_rctf_union(&block->rect, &bt->rect);
291 block->rect.xmin -= block->bounds;
292 block->rect.ymin -= block->bounds;
293 block->rect.xmax += block->bounds;
294 block->rect.ymax += block->bounds;
297 block->rect.xmax = block->rect.xmin + max_ff(BLI_rctf_size_x(&block->rect), block->minbounds);
299 /* hardcoded exception... but that one is annoying with larger safety */
300 bt = block->buttons.first;
301 if (bt && STREQLEN(bt->str, "ERROR", 5)) xof = 10;
304 block->safety.xmin = block->rect.xmin - xof;
305 block->safety.ymin = block->rect.ymin - xof;
306 block->safety.xmax = block->rect.xmax + xof;
307 block->safety.ymax = block->rect.ymax + xof;
310 static void ui_block_bounds_calc_centered(wmWindow *window, uiBlock *block)
316 /* note: this is used for the splash where window bounds event has not been
317 * updated by ghost, get the window bounds from ghost directly */
319 xmax = WM_window_pixels_x(window);
320 ymax = WM_window_pixels_y(window);
322 ui_block_bounds_calc(block);
324 width = BLI_rctf_size_x(&block->rect);
325 height = BLI_rctf_size_y(&block->rect);
327 startx = (xmax * 0.5f) - (width * 0.5f);
328 starty = (ymax * 0.5f) - (height * 0.5f);
330 ui_block_translate(block, startx - block->rect.xmin, starty - block->rect.ymin);
332 /* now recompute bounds and safety */
333 ui_block_bounds_calc(block);
337 static void ui_block_bounds_calc_centered_pie(uiBlock *block)
340 block->pie_data.pie_center_spawned[0],
341 block->pie_data.pie_center_spawned[1]
344 ui_block_translate(block, xy[0], xy[1]);
346 /* now recompute bounds and safety */
347 ui_block_bounds_calc(block);
350 static void ui_block_bounds_calc_popup(
351 wmWindow *window, uiBlock *block,
352 eBlockBoundsCalc bounds_calc, const int xy[2])
354 int startx, starty, endx, endy, width, height, oldwidth, oldheight;
355 int oldbounds, xmax, ymax;
356 const int margin = UI_SCREEN_MARGIN;
358 oldbounds = block->bounds;
360 /* compute mouse position with user defined offset */
361 ui_block_bounds_calc(block);
363 xmax = WM_window_pixels_x(window);
364 ymax = WM_window_pixels_y(window);
366 oldwidth = BLI_rctf_size_x(&block->rect);
367 oldheight = BLI_rctf_size_y(&block->rect);
369 /* first we ensure wide enough text bounds */
370 if (bounds_calc == UI_BLOCK_BOUNDS_POPUP_MENU) {
371 if (block->flag & UI_BLOCK_LOOP) {
372 block->bounds = 2.5f * UI_UNIT_X;
373 ui_block_bounds_calc_text(block, block->rect.xmin);
377 /* next we recompute bounds */
378 block->bounds = oldbounds;
379 ui_block_bounds_calc(block);
381 /* and we adjust the position to fit within window */
382 width = BLI_rctf_size_x(&block->rect);
383 height = BLI_rctf_size_y(&block->rect);
385 /* avoid divide by zero below, caused by calling with no UI, but better not crash */
386 oldwidth = oldwidth > 0 ? oldwidth : MAX2(1, width);
387 oldheight = oldheight > 0 ? oldheight : MAX2(1, height);
389 /* offset block based on mouse position, user offset is scaled
390 * along in case we resized the block in ui_block_bounds_calc_text */
391 startx = xy[0] + block->rect.xmin + (block->mx * width) / oldwidth;
392 starty = xy[1] + block->rect.ymin + (block->my * height) / oldheight;
399 endx = startx + width;
400 endy = starty + height;
403 endx = xmax - margin;
404 startx = endx - width;
406 if (endy > ymax - margin) {
407 endy = ymax - margin;
408 starty = endy - height;
411 ui_block_translate(block, startx - block->rect.xmin, starty - block->rect.ymin);
413 /* now recompute bounds and safety */
414 ui_block_bounds_calc(block);
417 /* used for various cases */
418 void UI_block_bounds_set_normal(uiBlock *block, int addval)
423 block->bounds = addval;
424 block->bounds_type = UI_BLOCK_BOUNDS;
427 /* used for pulldowns */
428 void UI_block_bounds_set_text(uiBlock *block, int addval)
430 block->bounds = addval;
431 block->bounds_type = UI_BLOCK_BOUNDS_TEXT;
434 /* used for block popups */
435 void UI_block_bounds_set_popup(uiBlock *block, int addval, int mx, int my)
437 block->bounds = addval;
438 block->bounds_type = UI_BLOCK_BOUNDS_POPUP_MOUSE;
443 /* used for menu popups */
444 void UI_block_bounds_set_menu(uiBlock *block, int addval, int mx, int my)
446 block->bounds = addval;
447 block->bounds_type = UI_BLOCK_BOUNDS_POPUP_MENU;
452 /* used for centered popups, i.e. splash */
453 void UI_block_bounds_set_centered(uiBlock *block, int addval)
455 block->bounds = addval;
456 block->bounds_type = UI_BLOCK_BOUNDS_POPUP_CENTER;
459 void UI_block_bounds_set_explicit(uiBlock *block, int minx, int miny, int maxx, int maxy)
461 block->rect.xmin = minx;
462 block->rect.ymin = miny;
463 block->rect.xmax = maxx;
464 block->rect.ymax = maxy;
465 block->bounds_type = UI_BLOCK_BOUNDS_NONE;
468 static int ui_but_calc_float_precision(uiBut *but, double value)
470 int prec = (int)but->a2;
472 /* first check for various special cases:
473 * * If button is radians, we want additional precision (see T39861).
474 * * If prec is not set, we fallback to a simple default */
475 if (ui_but_is_unit_radians(but) && prec < 5) {
478 else if (prec == -1) {
479 prec = (but->hardmax < 10.001f) ? 3 : 2;
482 return UI_calc_float_precision(prec, value);
485 /* ************** LINK LINE DRAWING ************* */
487 /* link line drawing is not part of buttons or theme.. so we stick with it here */
489 static void ui_draw_linkline(uiLinkLine *line, int highlightActiveLines, int dashInactiveLines)
493 if (line->from == NULL || line->to == NULL) return;
495 rect.xmin = BLI_rctf_cent_x(&line->from->rect);
496 rect.ymin = BLI_rctf_cent_y(&line->from->rect);
497 rect.xmax = BLI_rctf_cent_x(&line->to->rect);
498 rect.ymax = BLI_rctf_cent_y(&line->to->rect);
500 if (dashInactiveLines)
501 UI_ThemeColor(TH_GRID);
502 else if (line->flag & UI_SELECT)
503 glColor3ub(100, 100, 100);
504 else if (highlightActiveLines && ((line->from->flag & UI_ACTIVE) || (line->to->flag & UI_ACTIVE)))
505 UI_ThemeColor(TH_TEXT_HI);
509 ui_draw_link_bezier(&rect);
512 static void ui_draw_links(uiBlock *block)
517 /* Draw the gray out lines. Do this first so they appear at the
518 * bottom of inactive or active lines.
519 * As we go, remember if we see any active or selected lines. */
520 bool found_selectline = false;
521 bool found_activeline = false;
523 for (but = block->buttons.first; but; but = but->next) {
524 if (but->type == UI_BTYPE_LINK && but->link) {
525 for (line = but->link->lines.first; line; line = line->next) {
526 if (!(line->from->flag & UI_ACTIVE) && !(line->to->flag & UI_ACTIVE)) {
528 ui_draw_linkline(line, 0, true);
531 found_activeline = true;
533 if ((line->from->flag & UI_SELECT) || (line->to->flag & UI_SELECT))
534 found_selectline = true;
539 /* Draw the inactive lines (lines with neither button being hovered over) */
540 for (but = block->buttons.first; but; but = but->next) {
541 if (but->type == UI_BTYPE_LINK && but->link) {
542 for (line = but->link->lines.first; line; line = line->next) {
543 if (!(line->from->flag & UI_ACTIVE) && !(line->to->flag & UI_ACTIVE)) {
545 ui_draw_linkline(line, 0, false);
551 /* Draw any active lines (lines with either button being hovered over).
552 * Do this last so they appear on top of inactive and gray out lines. */
553 if (found_activeline) {
554 for (but = block->buttons.first; but; but = but->next) {
555 if (but->type == UI_BTYPE_LINK && but->link) {
556 for (line = but->link->lines.first; line; line = line->next) {
557 if ((line->from->flag & UI_ACTIVE) || (line->to->flag & UI_ACTIVE))
558 ui_draw_linkline(line, !found_selectline, false);
565 /* ************** BLOCK ENDING FUNCTION ************* */
567 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
568 static bool ui_but_equals_old(const uiBut *but, const uiBut *oldbut)
570 /* various properties are being compared here, hopefully sufficient
571 * to catch all cases, but it is simple to add more checks later */
572 if (but->retval != oldbut->retval) return false;
573 if (but->rnapoin.data != oldbut->rnapoin.data) return false;
574 if (but->rnaprop != oldbut->rnaprop || but->rnaindex != oldbut->rnaindex) return false;
575 if (but->func != oldbut->func) return false;
576 if (but->funcN != oldbut->funcN) return false;
577 if (oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return false;
578 if (oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return false;
579 if (!but->funcN && ((but->poin != oldbut->poin && (uiBut *)oldbut->poin != oldbut) ||
580 (but->pointype != oldbut->pointype))) return false;
581 if (but->optype != oldbut->optype) return false;
586 uiBut *ui_but_find_old(uiBlock *block_old, const uiBut *but_new)
589 for (but_old = block_old->buttons.first; but_old; but_old = but_old->next) {
590 if (ui_but_equals_old(but_new, but_old)) {
596 uiBut *ui_but_find_new(uiBlock *block_new, const uiBut *but_old)
599 for (but_new = block_new->buttons.first; but_new; but_new = but_new->next) {
600 if (ui_but_equals_old(but_new, but_old)) {
607 /* oldbut is being inserted in new block, so we use the lines from new button, and replace button pointers */
608 static void ui_but_update_linklines(uiBlock *block, uiBut *oldbut, uiBut *newbut)
613 /* if active button is UI_BTYPE_LINK */
614 if (newbut->type == UI_BTYPE_LINK && newbut->link) {
616 SWAP(uiLink *, oldbut->link, newbut->link);
618 for (line = oldbut->link->lines.first; line; line = line->next) {
619 if (line->to == newbut)
621 if (line->from == newbut)
626 /* check all other button links */
627 for (but = block->buttons.first; but; but = but->next) {
628 if (but != newbut && but->type == UI_BTYPE_LINK && but->link) {
629 for (line = but->link->lines.first; line; line = line->next) {
630 if (line->to == newbut)
632 if (line->from == newbut)
640 * \return true when \a but_p is set (only done for active buttons).
642 static bool ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut **but_p, uiBut **but_old_p)
644 const int drawflag_copy = 0; /* None currently. */
646 uiBlock *oldblock = block->oldblock;
647 uiBut *oldbut = NULL, *but = *but_p;
648 bool found_active = false;
652 /* simple/stupid - search every time */
653 oldbut = ui_but_find_old(oldblock, but);
656 BLI_assert(*but_old_p == NULL || BLI_findindex(&oldblock->buttons, *but_old_p) != -1);
658 /* fastpath - avoid loop-in-loop, calling 'ui_but_find_old'
659 * as long as old/new buttons are aligned */
660 if (LIKELY(*but_old_p && ui_but_equals_old(but, *but_old_p))) {
664 /* fallback to block search */
665 oldbut = ui_but_find_old(oldblock, but);
667 (*but_old_p) = oldbut ? oldbut->next : NULL;
675 if (oldbut->active) {
676 /* flags from the buttons we want to refresh, may want to add more here... */
677 const int flag_copy = UI_BUT_REDALERT;
682 but->flag = oldbut->flag;
683 but->active = oldbut->active;
684 but->pos = oldbut->pos;
685 but->ofs = oldbut->ofs;
686 but->editstr = oldbut->editstr;
687 but->editval = oldbut->editval;
688 but->editvec = oldbut->editvec;
689 but->editcoba = oldbut->editcoba;
690 but->editcumap = oldbut->editcumap;
691 but->selsta = oldbut->selsta;
692 but->selend = oldbut->selend;
693 but->softmin = oldbut->softmin;
694 but->softmax = oldbut->softmax;
695 but->linkto[0] = oldbut->linkto[0];
696 but->linkto[1] = oldbut->linkto[1];
697 oldbut->active = NULL;
700 /* move button over from oldblock to new block */
701 BLI_remlink(&oldblock->buttons, oldbut);
702 BLI_insertlinkafter(&block->buttons, but, oldbut);
703 oldbut->block = block;
706 /* still stuff needs to be copied */
707 oldbut->rect = but->rect;
708 oldbut->context = but->context; /* set by Layout */
711 oldbut->icon = but->icon;
712 oldbut->iconadd = but->iconadd;
713 oldbut->alignnr = but->alignnr;
715 /* typically the same pointers, but not on undo/redo */
716 /* XXX some menu buttons store button itself in but->poin. Ugly */
717 if (oldbut->poin != (char *)oldbut) {
718 SWAP(char *, oldbut->poin, but->poin);
719 SWAP(void *, oldbut->func_argN, but->func_argN);
720 SWAP(void *, oldbut->tip_argN, but->tip_argN);
723 oldbut->flag = (oldbut->flag & ~flag_copy) | (but->flag & flag_copy);
724 oldbut->drawflag = (oldbut->drawflag & ~drawflag_copy) | (but->drawflag & drawflag_copy);
726 /* copy hardmin for list rows to prevent 'sticking' highlight to mouse position
727 * when scrolling without moving mouse (see [#28432]) */
728 if (ELEM(oldbut->type, UI_BTYPE_ROW, UI_BTYPE_LISTROW))
729 oldbut->hardmax = but->hardmax;
731 ui_but_update_linklines(block, oldbut, but);
733 if (!BLI_listbase_is_empty(&block->butstore)) {
734 UI_butstore_register_update(block, oldbut, but);
737 /* move/copy string from the new button to the old */
738 /* needed for alt+mouse wheel over enums */
739 if (but->str != but->strdata) {
740 if (oldbut->str != oldbut->strdata) {
741 SWAP(char *, but->str, oldbut->str);
744 oldbut->str = but->str;
745 but->str = but->strdata;
749 if (oldbut->str != oldbut->strdata) {
750 MEM_freeN(oldbut->str);
751 oldbut->str = oldbut->strdata;
753 BLI_strncpy(oldbut->strdata, but->strdata, sizeof(oldbut->strdata));
756 BLI_remlink(&block->buttons, but);
759 /* note: if layout hasn't been applied yet, it uses old button pointers... */
762 const int flag_copy = UI_BUT_DRAG_MULTI;
764 but->flag = (but->flag & ~flag_copy) | (oldbut->flag & flag_copy);
766 /* ensures one button can get activated, and in case the buttons
767 * draw are the same this gives O(1) lookup for each button */
768 BLI_remlink(&oldblock->buttons, oldbut);
769 ui_but_free(C, oldbut);
775 /* needed for temporarily rename buttons, such as in outliner or file-select,
776 * they should keep calling uiDefButs to keep them alive */
777 /* returns 0 when button removed */
778 bool UI_but_active_only(const bContext *C, ARegion *ar, uiBlock *block, uiBut *but)
782 bool activate = false, found = false, isactive = false;
784 oldblock = block->oldblock;
789 oldbut = ui_but_find_old(oldblock, but);
793 if (oldbut->active) {
798 if ((activate == true) || (found == false)) {
799 ui_but_activate_event((bContext *)C, ar, but);
801 else if ((found == true) && (isactive == false)) {
802 BLI_remlink(&block->buttons, but);
810 /* simulate button click */
811 void UI_but_execute(const bContext *C, uiBut *but)
813 ARegion *ar = CTX_wm_region(C);
815 ui_but_execute_begin((bContext *)C, ar, but, &active_back);
816 /* Value is applied in begin. No further action required. */
817 ui_but_execute_end((bContext *)C, ar, but, active_back);
820 /* use to check if we need to disable undo, but don't make any changes
821 * returns false if undo needs to be disabled. */
822 static bool ui_but_is_rna_undo(const uiBut *but)
824 if (but->rnapoin.id.data) {
825 /* avoid undo push for buttons who's ID are screen or wm level
826 * we could disable undo for buttons with no ID too but may have
827 * unforeseen consequences, so best check for ID's we _know_ are not
828 * handled by undo - campbell */
829 ID *id = but->rnapoin.id.data;
830 if (ID_CHECK_UNDO(id) == false) {
837 else if (but->rnapoin.type && !RNA_struct_undo_check(but->rnapoin.type)) {
844 /* assigns automatic keybindings to menu items for fast access
845 * (underline key in menu) */
846 static void ui_menu_block_set_keyaccels(uiBlock *block)
850 unsigned int menu_key_mask = 0;
851 unsigned char menu_key;
856 /* only do it before bounding */
857 if (block->rect.xmin != block->rect.xmax)
860 for (pass = 0; pass < 2; pass++) {
861 /* 2 Passes, on for first letter only, second for any letter if first fails
862 * fun first pass on all buttons so first word chars always get first priority */
864 for (but = block->buttons.first; but; but = but->next) {
868 UI_BTYPE_MENU, UI_BTYPE_BLOCK,
869 UI_BTYPE_PULLDOWN) ||
870 (but->flag & UI_HIDDEN))
874 else if (but->menu_key == '\0') {
876 for (str_pt = but->str; *str_pt; ) {
877 menu_key = tolower(*str_pt);
878 if ((menu_key >= 'a' && menu_key <= 'z') && !(menu_key_mask & 1 << (menu_key - 'a'))) {
879 menu_key_mask |= 1 << (menu_key - 'a');
884 /* Skip to next delimiter on first pass (be picky) */
885 while (isalpha(*str_pt))
892 /* just step over every char second pass and find first usable key */
898 but->menu_key = menu_key;
901 /* run second pass */
905 /* if all keys have been used just exit, unlikely */
906 if (menu_key_mask == (1 << 26) - 1) {
913 /* check if second pass is needed */
920 /* XXX, this code will shorten any allocated string to 'UI_MAX_NAME_STR'
921 * since this is really long its unlikely to be an issue,
922 * but this could be supported */
923 void ui_but_add_shortcut(uiBut *but, const char *shortcut_str, const bool do_strip)
926 if (do_strip && (but->flag & UI_BUT_HAS_SEP_CHAR)) {
927 char *cpoin = strrchr(but->str, UI_SEP_CHAR);
931 but->flag &= ~UI_BUT_HAS_SEP_CHAR;
934 /* without this, just allow stripping of the shortcut */
938 if (but->str != but->strdata) {
939 butstr_orig = but->str; /* free after using as source buffer */
942 butstr_orig = BLI_strdup(but->str);
944 BLI_snprintf(but->strdata,
945 sizeof(but->strdata),
946 "%s" UI_SEP_CHAR_S "%s",
947 butstr_orig, shortcut_str);
948 MEM_freeN(butstr_orig);
949 but->str = but->strdata;
950 but->flag |= UI_BUT_HAS_SEP_CHAR;
955 static bool ui_but_event_operator_string(const bContext *C, uiBut *but, char *buf, const size_t buf_len)
961 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
963 if (WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, true,
969 else if ((mt = UI_but_menutype_get(but))) {
970 IDProperty *prop_menu;
971 IDProperty *prop_menu_name;
973 /* annoying, create a property */
974 IDPropertyTemplate val = {0};
975 prop_menu = IDP_New(IDP_GROUP, &val, __func__); /* dummy, name is unimportant */
976 IDP_AddToGroup(prop_menu, (prop_menu_name = IDP_NewString("", "name", sizeof(mt->idname))));
978 IDP_AssignString(prop_menu_name, mt->idname, sizeof(mt->idname));
980 if (WM_key_event_operator_string(C, "WM_OT_call_menu", WM_OP_INVOKE_REGION_WIN, prop_menu, true,
986 IDP_FreeProperty(prop_menu);
987 MEM_freeN(prop_menu);
993 static bool ui_but_event_property_operator_string(const bContext *C, uiBut *but, char *buf, const size_t buf_len)
995 /* context toggle operator names to check... */
996 const char *ctx_toggle_opnames[] = {
997 "WM_OT_context_toggle",
998 "WM_OT_context_toggle_enum",
999 "WM_OT_context_cycle_int",
1000 "WM_OT_context_cycle_enum",
1001 "WM_OT_context_cycle_array",
1002 "WM_OT_context_menu_enum",
1005 const size_t num_ops = sizeof(ctx_toggle_opnames) / sizeof(const char *);
1009 /* this version is only for finding hotkeys for properties (which get set via context using operators) */
1011 /* to avoid massive slowdowns on property panels, for now, we only check the
1012 * hotkeys for Editor / Scene settings...
1014 * TODO: userpref settings?
1016 // TODO: value (for enum stuff)?
1017 char *data_path = NULL;
1019 if (but->rnapoin.id.data) {
1020 ID *id = but->rnapoin.id.data;
1022 if (GS(id->name) == ID_SCR) {
1023 /* screen/editor property
1024 * NOTE: in most cases, there is actually no info for backwards tracing
1025 * how to get back to ID from the editor data we may be dealing with
1027 if (RNA_struct_is_a(but->rnapoin.type, &RNA_Space)) {
1028 /* data should be directly on here... */
1029 data_path = BLI_sprintfN("space_data.%s", RNA_property_identifier(but->rnaprop));
1032 /* special exceptions for common nested data in editors... */
1033 if (RNA_struct_is_a(but->rnapoin.type, &RNA_DopeSheet)) {
1034 /* dopesheet filtering options... */
1035 data_path = BLI_sprintfN("space_data.dopesheet.%s", RNA_property_identifier(but->rnaprop));
1039 else if (GS(id->name) == ID_SCE) {
1040 if (RNA_struct_is_a(but->rnapoin.type, &RNA_ToolSettings)) {
1041 /* toolsettings property
1042 * NOTE: toolsettings is usually accessed directly (i.e. not through scene)
1044 data_path = RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
1047 /* scene property */
1048 char *path = RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
1051 data_path = BLI_sprintfN("scene.%s", path);
1056 printf("ERROR in %s(): Couldn't get path for scene property - %s\n",
1057 __func__, RNA_property_identifier(but->rnaprop));
1066 //printf("prop shortcut: '%s' (%s)\n", RNA_property_identifier(but->rnaprop), data_path);
1069 /* we have a datapath! */
1073 /* create a property to host the "datapath" property we're sending to the operators */
1074 IDProperty *prop_path;
1075 IDProperty *prop_path_value;
1077 IDPropertyTemplate val = {0};
1078 prop_path = IDP_New(IDP_GROUP, &val, __func__);
1079 prop_path_value = IDP_NewString(data_path, "data_path", strlen(data_path) + 1);
1080 IDP_AddToGroup(prop_path, prop_path_value);
1082 /* check each until one works... */
1083 for (i = 0; (i < num_ops) && (ctx_toggle_opnames[i]); i++) {
1084 //printf("\t%s\n", ctx_toggle_opnames[i]);
1085 if (WM_key_event_operator_string(C, ctx_toggle_opnames[i], WM_OP_INVOKE_REGION_WIN, prop_path, false,
1094 IDP_FreeProperty(prop_path);
1095 MEM_freeN(prop_path);
1096 MEM_freeN(data_path);
1103 /* this goes in a seemingly weird pattern:
1111 * but it's actually quite logical. It's designed to be 'upwards compatible'
1112 * for muscle memory so that the menu item locations are fixed and don't move
1113 * as new items are added to the menu later on. It also optimises efficiency -
1114 * a radial menu is best kept symmetrical, with as large an angle between
1115 * items as possible, so that the gestural mouse movements can be fast and inexact.
1117 * It starts off with two opposite sides for the first two items
1118 * then joined by the one below for the third (this way, even with three items,
1119 * the menu seems to still be 'in order' reading left to right). Then the fourth is
1120 * added to complete the compass directions. From here, it's just a matter of
1121 * subdividing the rest of the angles for the last 4 items.
1125 const char ui_radial_dir_order[8] = {
1126 UI_RADIAL_W, UI_RADIAL_E, UI_RADIAL_S, UI_RADIAL_N,
1127 UI_RADIAL_NW, UI_RADIAL_NE, UI_RADIAL_SW, UI_RADIAL_SE};
1129 const char ui_radial_dir_to_numpad[8] = {8, 9, 6, 3, 2, 1, 4, 7};
1130 const short ui_radial_dir_to_angle[8] = {90, 45, 0, 315, 270, 225, 180, 135};
1132 static void ui_but_pie_direction_string(uiBut *but, char *buf, int size)
1134 BLI_assert(but->pie_dir < ARRAY_SIZE(ui_radial_dir_to_numpad));
1135 BLI_snprintf(buf, size, "%d", ui_radial_dir_to_numpad[but->pie_dir]);
1138 static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
1143 /* only do it before bounding */
1144 if (block->rect.xmin != block->rect.xmax)
1147 if (block->flag & UI_BLOCK_RADIAL) {
1148 for (but = block->buttons.first; but; but = but->next) {
1149 if (but->pie_dir != UI_RADIAL_NONE) {
1150 ui_but_pie_direction_string(but, buf, sizeof(buf));
1151 ui_but_add_shortcut(but, buf, false);
1156 for (but = block->buttons.first; but; but = but->next) {
1158 if (ui_but_event_operator_string(C, but, buf, sizeof(buf))) {
1159 ui_but_add_shortcut(but, buf, false);
1161 else if (ui_but_event_property_operator_string(C, but, buf, sizeof(buf))) {
1162 ui_but_add_shortcut(but, buf, false);
1168 void UI_block_update_from_old(const bContext *C, uiBlock *block)
1173 if (!block->oldblock)
1176 but_old = block->oldblock->buttons.first;
1178 if (BLI_listbase_is_empty(&block->oldblock->butstore) == false) {
1179 UI_butstore_update(block);
1182 for (but = block->buttons.first; but; but = but->next) {
1183 if (ui_but_update_from_old_block(C, block, &but, &but_old)) {
1188 block->auto_open = block->oldblock->auto_open;
1189 block->auto_open_last = block->oldblock->auto_open_last;
1190 block->tooltipdisabled = block->oldblock->tooltipdisabled;
1191 BLI_movelisttolist(&block->color_pickers.list, &block->oldblock->color_pickers.list);
1193 block->oldblock = NULL;
1196 void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2])
1198 wmWindow *window = CTX_wm_window(C);
1199 Scene *scene = CTX_data_scene(C);
1202 BLI_assert(block->active);
1204 UI_block_update_from_old(C, block);
1206 /* inherit flags from 'old' buttons that was drawn here previous, based
1207 * on matching buttons, we need this to make button event handling non
1208 * blocking, while still allowing buttons to be remade each redraw as it
1209 * is expected by blender code */
1210 for (but = block->buttons.first; but; but = but->next) {
1211 /* temp? Proper check for graying out */
1213 wmOperatorType *ot = but->optype;
1216 CTX_store_set((bContext *)C, but->context);
1218 if (ot == NULL || WM_operator_poll_context((bContext *)C, ot, but->opcontext) == 0) {
1219 but->flag |= UI_BUT_DISABLED;
1224 CTX_store_set((bContext *)C, NULL);
1227 ui_but_anim_flag(but, (scene) ? scene->r.cfra : 0.0f);
1232 /* handle pending stuff */
1233 if (block->layouts.first) {
1234 UI_block_layout_resolve(block, NULL, NULL);
1236 ui_block_align_calc(block);
1237 if ((block->flag & UI_BLOCK_LOOP) && (block->flag & UI_BLOCK_NUMSELECT)) {
1238 ui_menu_block_set_keyaccels(block); /* could use a different flag to check */
1241 if (block->flag & UI_BLOCK_LOOP) {
1242 ui_menu_block_set_keymaps(C, block);
1245 /* after keymaps! */
1246 switch (block->bounds_type) {
1247 case UI_BLOCK_BOUNDS_NONE:
1249 case UI_BLOCK_BOUNDS:
1250 ui_block_bounds_calc(block);
1252 case UI_BLOCK_BOUNDS_TEXT:
1253 ui_block_bounds_calc_text(block, 0.0f);
1255 case UI_BLOCK_BOUNDS_POPUP_CENTER:
1256 ui_block_bounds_calc_centered(window, block);
1258 case UI_BLOCK_BOUNDS_PIE_CENTER:
1259 ui_block_bounds_calc_centered_pie(block);
1263 case UI_BLOCK_BOUNDS_POPUP_MOUSE:
1264 case UI_BLOCK_BOUNDS_POPUP_MENU:
1265 ui_block_bounds_calc_popup(window, block, block->bounds_type, xy);
1269 if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
1270 UI_block_bounds_set_normal(block, 0);
1272 if (block->flag & UI_BUT_ALIGN) {
1273 UI_block_align_end(block);
1276 block->endblock = 1;
1279 void UI_block_end(const bContext *C, uiBlock *block)
1281 wmWindow *window = CTX_wm_window(C);
1283 UI_block_end_ex(C, block, &window->eventstate->x);
1286 /* ************** BLOCK DRAWING FUNCTION ************* */
1288 void ui_fontscale(short *points, float aspect)
1290 if (aspect < 0.9f || aspect > 1.1f) {
1291 float pointsf = *points;
1293 /* for some reason scaling fonts goes too fast compared to widget size */
1294 /* XXX not true anymore? (ton) */
1295 //aspect = sqrt(aspect);
1299 *points = ceilf(pointsf);
1301 *points = floorf(pointsf);
1305 /* project button or block (but==NULL) to pixels in regionspace */
1306 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
1310 ui_block_to_window_rctf(ar, block, &rectf, (but) ? &but->rect : &block->rect);
1312 rectf.xmin -= ar->winrct.xmin;
1313 rectf.ymin -= ar->winrct.ymin;
1314 rectf.xmax -= ar->winrct.xmin;
1315 rectf.ymax -= ar->winrct.ymin;
1317 rect->xmin = floorf(rectf.xmin);
1318 rect->ymin = floorf(rectf.ymin);
1319 rect->xmax = floorf(rectf.xmax);
1320 rect->ymax = floorf(rectf.ymax);
1323 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
1324 void UI_block_draw(const bContext *C, uiBlock *block)
1326 uiStyle style = *UI_style_get_dpi(); /* XXX pass on as arg */
1330 int multisample_enabled;
1332 /* get menu region or area region */
1333 ar = CTX_wm_menu(C);
1335 ar = CTX_wm_region(C);
1337 if (!block->endblock)
1338 UI_block_end(C, block);
1340 /* disable AA, makes widgets too blurry */
1341 multisample_enabled = glIsEnabled(GL_MULTISAMPLE_ARB);
1342 if (multisample_enabled)
1343 glDisable(GL_MULTISAMPLE_ARB);
1345 /* we set this only once */
1346 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1349 ui_fontscale(&style.paneltitle.points, block->aspect);
1350 ui_fontscale(&style.grouplabel.points, block->aspect);
1351 ui_fontscale(&style.widgetlabel.points, block->aspect);
1352 ui_fontscale(&style.widget.points, block->aspect);
1354 /* scale block min/max to rect */
1355 ui_but_to_pixelrect(&rect, ar, block, NULL);
1357 /* pixel space for AA widgets */
1358 glMatrixMode(GL_PROJECTION);
1360 glMatrixMode(GL_MODELVIEW);
1364 wmOrtho2_region_ui(ar);
1367 if (block->flag & UI_BLOCK_RADIAL)
1368 ui_draw_pie_center(block);
1369 else if (block->flag & UI_BLOCK_LOOP)
1370 ui_draw_menu_back(&style, block, &rect);
1371 else if (block->panel)
1372 ui_draw_aligned_panel(&style, block, &rect, UI_panel_category_is_visible(ar));
1375 for (but = block->buttons.first; but; but = but->next) {
1376 if (!(but->flag & (UI_HIDDEN | UI_SCROLLED))) {
1377 ui_but_to_pixelrect(&rect, ar, block, but);
1379 /* XXX: figure out why invalid coordinates happen when closing render window */
1380 /* and material preview is redrawn in main window (temp fix for bug #23848) */
1381 if (rect.xmin < rect.xmax && rect.ymin < rect.ymax)
1382 ui_draw_but(C, ar, &style, but, &rect);
1386 /* restore matrix */
1387 glMatrixMode(GL_PROJECTION);
1389 glMatrixMode(GL_MODELVIEW);
1392 if (multisample_enabled)
1393 glEnable(GL_MULTISAMPLE_ARB);
1395 ui_draw_links(block);
1398 /* ************* EVENTS ************* */
1401 * Check if the button is pushed, this is only meaningful for some button types.
1403 * \return (0 == UNSELECT), (1 == SELECT), (-1 == DO-NOTHING)
1405 int ui_but_is_pushed_ex(uiBut *but, double *value)
1410 const bool state = ELEM(but->type, UI_BTYPE_TOGGLE_N, UI_BTYPE_ICON_TOGGLE_N, UI_BTYPE_CHECKBOX_N) ? false : true;
1412 UI_GET_BUT_VALUE_INIT(but, *value);
1413 lvalue = (int)*value;
1414 if (UI_BITBUT_TEST(lvalue, (but->bitnr))) {
1422 switch (but->type) {
1424 case UI_BTYPE_HOTKEY_EVENT:
1425 case UI_BTYPE_KEY_EVENT:
1426 case UI_BTYPE_COLOR:
1429 case UI_BTYPE_BUT_TOGGLE:
1430 case UI_BTYPE_TOGGLE:
1431 case UI_BTYPE_ICON_TOGGLE:
1432 case UI_BTYPE_CHECKBOX:
1433 UI_GET_BUT_VALUE_INIT(but, *value);
1434 if (*value != (double)but->hardmin) is_push = true;
1436 case UI_BTYPE_ICON_TOGGLE_N:
1437 case UI_BTYPE_TOGGLE_N:
1438 case UI_BTYPE_CHECKBOX_N:
1439 UI_GET_BUT_VALUE_INIT(but, *value);
1440 if (*value == 0.0) is_push = true;
1443 case UI_BTYPE_LISTROW:
1444 UI_GET_BUT_VALUE_INIT(but, *value);
1445 /* support for rna enum buts */
1446 if (but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
1447 if ((int)*value & (int)but->hardmax) is_push = true;
1450 if (*value == (double)but->hardmax) is_push = true;
1461 int ui_but_is_pushed(uiBut *but)
1463 double value = UI_BUT_VALUE_UNSET;
1464 return ui_but_is_pushed_ex(but, &value);
1467 static void ui_but_update_select_flag(uiBut *but, double *value)
1469 switch (ui_but_is_pushed_ex(but, value)) {
1471 but->flag |= UI_SELECT;
1474 but->flag &= ~UI_SELECT;
1479 static uiBut *ui_linkline_find_inlink(uiBlock *block, void *poin)
1483 but = block->buttons.first;
1485 if (but->type == UI_BTYPE_INLINK) {
1486 if (but->poin == poin) return but;
1493 static void ui_linkline_add(ListBase *listb, uiBut *but, uiBut *bt, short deactive)
1497 line = MEM_callocN(sizeof(uiLinkLine), "linkline");
1498 BLI_addtail(listb, line);
1501 line->deactive = deactive;
1504 uiBut *UI_block_links_find_inlink(uiBlock *block, void *poin)
1506 return ui_linkline_find_inlink(block, poin);
1509 void UI_block_links_compose(uiBlock *block)
1516 but = block->buttons.first;
1518 if (but->type == UI_BTYPE_LINK) {
1521 /* for all pointers in the array */
1524 ppoin = link->ppoin;
1525 for (a = 0; a < *(link->totlink); a++) {
1526 bt = ui_linkline_find_inlink(block, (*ppoin)[a]);
1528 if ((but->flag & UI_BUT_SCA_LINK_GREY) || (bt->flag & UI_BUT_SCA_LINK_GREY)) {
1529 ui_linkline_add(&link->lines, but, bt, true);
1532 ui_linkline_add(&link->lines, but, bt, false);
1538 else if (link->poin) {
1539 bt = ui_linkline_find_inlink(block, *link->poin);
1541 if ((but->flag & UI_BUT_SCA_LINK_GREY) || (bt->flag & UI_BUT_SCA_LINK_GREY)) {
1542 ui_linkline_add(&link->lines, but, bt, true);
1545 ui_linkline_add(&link->lines, but, bt, false);
1556 /* ************************************************ */
1558 void UI_block_lock_set(uiBlock *block, bool val, const char *lockstr)
1562 block->lockstr = lockstr;
1566 void UI_block_lock_clear(uiBlock *block)
1568 block->lock = false;
1569 block->lockstr = NULL;
1572 /* *************************************************************** */
1574 void ui_linkline_remove(uiLinkLine *line, uiBut *but)
1579 BLI_remlink(&but->link->lines, line);
1581 link = line->from->link;
1583 /* are there more pointers allowed? */
1586 if (*(link->totlink) == 1) {
1587 *(link->totlink) = 0;
1588 MEM_freeN(*(link->ppoin));
1589 *(link->ppoin) = NULL;
1593 for (a = 0; a < (*(link->totlink)); a++) {
1594 if ((*(link->ppoin))[a] != line->to->poin) {
1595 (*(link->ppoin))[b] = (*(link->ppoin))[a];
1599 (*(link->totlink))--;
1603 *(link->poin) = NULL;
1610 /* *********************** data get/set ***********************
1611 * this either works with the pointed to data, or can work with
1612 * an edit override pointer while dragging for example */
1614 /* for buttons pointing to color for example */
1615 void ui_but_v3_get(uiBut *but, float vec[3])
1621 copy_v3_v3(vec, but->editvec);
1625 prop = but->rnaprop;
1629 if (RNA_property_type(prop) == PROP_FLOAT) {
1630 int tot = RNA_property_array_length(&but->rnapoin, prop);
1631 BLI_assert(tot > 0);
1633 RNA_property_float_get_array(&but->rnapoin, prop, vec);
1636 tot = min_ii(tot, 3);
1637 for (a = 0; a < tot; a++) {
1638 vec[a] = RNA_property_float_get_index(&but->rnapoin, prop, a);
1643 else if (but->pointype == UI_BUT_POIN_CHAR) {
1644 const char *cp = (char *)but->poin;
1646 vec[0] = ((float)cp[0]) / 255.0f;
1647 vec[1] = ((float)cp[1]) / 255.0f;
1648 vec[2] = ((float)cp[2]) / 255.0f;
1650 else if (but->pointype == UI_BUT_POIN_FLOAT) {
1651 const float *fp = (float *)but->poin;
1652 copy_v3_v3(vec, fp);
1655 if (but->editvec == NULL) {
1656 fprintf(stderr, "%s: can't get color, should never happen\n", __func__);
1661 if (but->type == UI_BTYPE_UNITVEC) {
1666 /* for buttons pointing to color for example */
1667 void ui_but_v3_set(uiBut *but, const float vec[3])
1672 copy_v3_v3(but->editvec, vec);
1676 prop = but->rnaprop;
1678 if (RNA_property_type(prop) == PROP_FLOAT) {
1682 tot = RNA_property_array_length(&but->rnapoin, prop);
1683 BLI_assert(tot > 0);
1685 RNA_property_float_set_array(&but->rnapoin, prop, vec);
1688 tot = min_ii(tot, 3);
1689 for (a = 0; a < tot; a++) {
1690 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1695 else if (but->pointype == UI_BUT_POIN_CHAR) {
1696 char *cp = (char *)but->poin;
1697 cp[0] = (char)(0.5f + vec[0] * 255.0f);
1698 cp[1] = (char)(0.5f + vec[1] * 255.0f);
1699 cp[2] = (char)(0.5f + vec[2] * 255.0f);
1701 else if (but->pointype == UI_BUT_POIN_FLOAT) {
1702 float *fp = (float *)but->poin;
1703 copy_v3_v3(fp, vec);
1707 bool ui_but_is_float(const uiBut *but)
1709 if (but->pointype == UI_BUT_POIN_FLOAT && but->poin)
1712 if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1718 bool ui_but_is_bool(const uiBut *but)
1720 if (ELEM(but->type, UI_BTYPE_TOGGLE, UI_BTYPE_TOGGLE_N, UI_BTYPE_ICON_TOGGLE, UI_BTYPE_ICON_TOGGLE_N))
1723 if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_BOOLEAN)
1726 if ((but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) && (but->type == UI_BTYPE_ROW))
1733 bool ui_but_is_unit(const uiBut *but)
1735 UnitSettings *unit = but->block->unit;
1736 const int unit_type = UI_but_unit_type_get(but);
1738 if (unit_type == PROP_UNIT_NONE)
1741 #if 1 /* removed so angle buttons get correct snapping */
1742 if (ui_but_is_unit_radians_ex(unit, unit_type))
1746 /* for now disable time unit conversion */
1747 if (unit_type == PROP_UNIT_TIME)
1750 if (unit->system == USER_UNIT_NONE) {
1751 if (unit_type != PROP_UNIT_ROTATION) {
1760 * Check if this button is similar enough to be grouped with another.
1762 bool ui_but_is_compatible(const uiBut *but_a, const uiBut *but_b)
1764 if (but_a->type != but_b->type)
1766 if (but_a->pointype != but_b->pointype)
1769 if (but_a->rnaprop) {
1770 /* skip 'rnapoin.data', 'rnapoin.id.data'
1771 * allow different data to have the same props edited at once */
1772 if (but_a->rnapoin.type != but_b->rnapoin.type)
1774 if (RNA_property_type(but_a->rnaprop) != RNA_property_type(but_b->rnaprop))
1776 if (RNA_property_subtype(but_a->rnaprop) != RNA_property_subtype(but_b->rnaprop))
1783 bool ui_but_is_rna_valid(uiBut *but)
1785 if (but->rnaprop == NULL || RNA_struct_contains_property(&but->rnapoin, but->rnaprop)) {
1789 printf("property removed %s: %p\n", but->drawstr, but->rnaprop);
1794 double ui_but_value_get(uiBut *but)
1799 if (but->editval) { return *(but->editval); }
1800 if (but->poin == NULL && but->rnapoin.data == NULL) return 0.0;
1803 prop = but->rnaprop;
1805 BLI_assert(but->rnaindex != -1);
1807 switch (RNA_property_type(prop)) {
1809 if (RNA_property_array_check(prop))
1810 value = RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1812 value = RNA_property_boolean_get(&but->rnapoin, prop);
1815 if (RNA_property_array_check(prop))
1816 value = RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1818 value = RNA_property_int_get(&but->rnapoin, prop);
1821 if (RNA_property_array_check(prop))
1822 value = RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1824 value = RNA_property_float_get(&but->rnapoin, prop);
1827 value = RNA_property_enum_get(&but->rnapoin, prop);
1834 else if (but->pointype == UI_BUT_POIN_CHAR) {
1835 value = *(char *)but->poin;
1837 else if (but->pointype == UI_BUT_POIN_SHORT) {
1838 value = *(short *)but->poin;
1840 else if (but->pointype == UI_BUT_POIN_INT) {
1841 value = *(int *)but->poin;
1843 else if (but->pointype == UI_BUT_POIN_FLOAT) {
1844 value = *(float *)but->poin;
1850 void ui_but_value_set(uiBut *but, double value)
1854 /* value is a hsv value: convert to rgb */
1856 prop = but->rnaprop;
1858 if (RNA_property_editable(&but->rnapoin, prop)) {
1859 switch (RNA_property_type(prop)) {
1861 if (RNA_property_array_check(prop))
1862 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1864 RNA_property_boolean_set(&but->rnapoin, prop, value);
1867 if (RNA_property_array_check(prop))
1868 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, (int)value);
1870 RNA_property_int_set(&but->rnapoin, prop, (int)value);
1873 if (RNA_property_array_check(prop))
1874 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1876 RNA_property_float_set(&but->rnapoin, prop, value);
1879 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1880 int ivalue = (int)value;
1881 ivalue ^= RNA_property_enum_get(&but->rnapoin, prop); /* toggle for enum/flag buttons */
1882 RNA_property_enum_set(&but->rnapoin, prop, ivalue);
1885 RNA_property_enum_set(&but->rnapoin, prop, value);
1893 /* we can't be sure what RNA set functions actually do,
1894 * so leave this unset */
1895 value = UI_BUT_VALUE_UNSET;
1897 else if (but->pointype == 0) {
1901 /* first do rounding */
1902 if (but->pointype == UI_BUT_POIN_CHAR) {
1903 value = (char)floor(value + 0.5);
1905 else if (but->pointype == UI_BUT_POIN_SHORT) {
1906 value = (short)floor(value + 0.5);
1908 else if (but->pointype == UI_BUT_POIN_INT)
1909 value = (int)floor(value + 0.5);
1910 else if (but->pointype == UI_BUT_POIN_FLOAT) {
1911 float fval = (float)value;
1912 if (fval >= -0.00001f && fval <= 0.00001f) fval = 0.0f; /* prevent negative zero */
1916 /* then set value with possible edit override */
1918 value = *but->editval = value;
1919 else if (but->pointype == UI_BUT_POIN_CHAR)
1920 value = *((char *)but->poin) = (char)value;
1921 else if (but->pointype == UI_BUT_POIN_SHORT)
1922 value = *((short *)but->poin) = (short)value;
1923 else if (but->pointype == UI_BUT_POIN_INT)
1924 value = *((int *)but->poin) = (int)value;
1925 else if (but->pointype == UI_BUT_POIN_FLOAT)
1926 value = *((float *)but->poin) = (float)value;
1929 ui_but_update_select_flag(but, &value);
1932 int ui_but_string_get_max_length(uiBut *but)
1934 if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU))
1935 return but->hardmax;
1937 return UI_MAX_DRAW_STR;
1940 uiBut *ui_but_drag_multi_edit_get(uiBut *but)
1944 BLI_assert(but->flag & UI_BUT_DRAG_MULTI);
1946 for (but_iter = but->block->buttons.first; but_iter; but_iter = but_iter->next) {
1947 if (but_iter->editstr) {
1955 /** \name Check to show extra icons
1957 * Extra icons are shown on the right hand side of buttons.
1960 static bool ui_but_icon_extra_is_visible_search_unlink(const uiBut *but)
1962 BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
1963 return ((but->editstr == NULL) &&
1964 (but->drawstr[0] != '\0') &&
1965 (but->flag & UI_BUT_SEARCH_UNLINK));
1968 static bool ui_but_icon_extra_is_visible_eyedropper(uiBut *but)
1973 BLI_assert(but->type == UI_BTYPE_SEARCH_MENU && (but->flag & UI_BUT_SEARCH_UNLINK));
1975 if (but->rnaprop == NULL) {
1979 type = RNA_property_pointer_type(&but->rnapoin, but->rnaprop);
1980 idcode = RNA_type_to_ID_code(type);
1983 return ((but->editstr == NULL) &&
1984 (idcode == ID_OB || OB_DATA_SUPPORT_ID(idcode)));
1987 uiButExtraIconType ui_but_icon_extra_get(uiBut *but)
1989 if ((but->flag & UI_BUT_SEARCH_UNLINK) == 0) {
1992 else if (ui_but_icon_extra_is_visible_search_unlink(but)) {
1993 return UI_BUT_ICONEXTRA_UNLINK;
1995 else if (ui_but_icon_extra_is_visible_eyedropper(but)) {
1996 return UI_BUT_ICONEXTRA_EYEDROPPER;
1999 return UI_BUT_ICONEXTRA_NONE;
2005 static double ui_get_but_scale_unit(uiBut *but, double value)
2007 UnitSettings *unit = but->block->unit;
2008 int unit_type = UI_but_unit_type_get(but);
2010 /* Time unit is a bit special, not handled by BKE_scene_unit_scale() for now. */
2011 if (unit_type == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
2012 Scene *scene = CTX_data_scene(but->block->evil_C);
2013 return FRA2TIME(value);
2016 return BKE_scene_unit_scale(unit, RNA_SUBTYPE_UNIT_VALUE(unit_type), value);
2020 /* str will be overwritten */
2021 void ui_but_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen)
2023 if (ui_but_is_unit(but)) {
2024 UnitSettings *unit = but->block->unit;
2025 int unit_type = UI_but_unit_type_get(but);
2028 orig_str = BLI_strdup(str);
2030 bUnit_ToUnitAltName(str, maxlen, orig_str, unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
2032 MEM_freeN(orig_str);
2037 * \param float_precision Override the button precision.
2039 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, bool pad, int float_precision)
2041 UnitSettings *unit = but->block->unit;
2042 const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
2043 int unit_type = UI_but_unit_type_get(but);
2046 if (unit->scale_length < 0.0001f) unit->scale_length = 1.0f; // XXX do_versions
2048 /* Use precision override? */
2049 if (float_precision == -1) {
2051 precision = (int)but->a2;
2052 if (precision > UI_PRECISION_FLOAT_MAX) precision = UI_PRECISION_FLOAT_MAX;
2053 else if (precision == -1) precision = 2;
2056 precision = float_precision;
2059 bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision,
2060 unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type), do_split, pad);
2063 static float ui_get_but_step_unit(uiBut *but, float step_default)
2065 int unit_type = RNA_SUBTYPE_UNIT_VALUE(UI_but_unit_type_get(but));
2068 step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, step_default), but->block->unit->system, unit_type);
2070 /* -1 is an error value */
2072 BLI_assert(step > 0.0);
2073 return (float)(step / ui_get_but_scale_unit(but, 1.0)) * 100.0f;
2076 return step_default;
2081 * \param float_precision For number buttons the precision to use or -1 to fallback to the button default.
2083 void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision)
2085 if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2087 const char *buf = NULL;
2090 type = RNA_property_type(but->rnaprop);
2092 if (type == PROP_STRING) {
2094 buf = RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen, &buf_len);
2096 else if (type == PROP_ENUM) {
2098 int value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
2099 if (RNA_property_enum_name(but->block->evil_C, &but->rnapoin, but->rnaprop, value, &buf)) {
2100 BLI_strncpy(str, buf, maxlen);
2104 else if (type == PROP_POINTER) {
2106 PointerRNA ptr = RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
2107 buf = RNA_struct_name_get_alloc(&ptr, str, maxlen, &buf_len);
2116 else if (buf && buf != str) {
2117 /* string was too long, we have to truncate */
2118 memcpy(str, buf, MIN2(maxlen, (size_t)(buf_len + 1)));
2119 MEM_freeN((void *)buf);
2122 else if (but->type == UI_BTYPE_TEXT) {
2124 BLI_strncpy(str, but->poin, maxlen);
2127 else if (but->type == UI_BTYPE_SEARCH_MENU) {
2129 BLI_strncpy(str, but->poin, maxlen);
2132 else if (ui_but_anim_expression_get(but, str, maxlen)) {
2133 /* driver expression */
2136 /* number editing */
2139 value = ui_but_value_get(but);
2141 if (ui_but_is_float(but)) {
2142 if (ui_but_is_unit(but)) {
2143 ui_get_but_string_unit(but, str, maxlen, value, false, float_precision);
2146 const int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision;
2147 BLI_snprintf(str, maxlen, "%.*f", prec, value);
2151 BLI_snprintf(str, maxlen, "%d", (int)value);
2154 void ui_but_string_get(uiBut *but, char *str, const size_t maxlen)
2156 ui_but_string_get_ex(but, str, maxlen, -1);
2161 static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *value)
2163 char str_unit_convert[256];
2164 const int unit_type = UI_but_unit_type_get(but);
2166 BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
2168 /* ugly, use the draw string to get the value,
2169 * this could cause problems if it includes some text which resolves to a unit */
2170 bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr,
2171 ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
2173 return (BPY_button_exec(C, str_unit_convert, value, true) != -1);
2176 #endif /* WITH_PYTHON */
2179 bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double *value)
2185 if (str[0] != '\0') {
2186 bool is_unit_but = (ui_but_is_float(but) && ui_but_is_unit(but));
2187 /* only enable verbose if we won't run again with units */
2188 if (BPY_button_exec(C, str, value, is_unit_but == false) != -1) {
2189 /* if the value parsed ok without unit conversion this button may still need a unit multiplier */
2193 BLI_snprintf(str_new, sizeof(str_new), "%f", *value);
2194 ok = ui_set_but_string_eval_num_unit(C, but, str_new, value);
2197 ok = true; /* parse normal string via py (no unit conversion needed) */
2200 else if (is_unit_but) {
2201 /* parse failed, this is a unit but so run replacements and parse again */
2202 ok = ui_set_but_string_eval_num_unit(C, but, str, value);
2206 #else /* WITH_PYTHON */
2214 #endif /* WITH_PYTHON */
2219 /* just the assignment/free part */
2220 static void ui_but_string_set_internal(uiBut *but, const char *str, size_t str_len)
2222 BLI_assert(str_len == strlen(str));
2223 BLI_assert(but->str == NULL);
2226 if (str_len > UI_MAX_NAME_STR) {
2227 but->str = MEM_mallocN(str_len, "ui_def_but str");
2230 but->str = but->strdata;
2232 memcpy(but->str, str, str_len);
2235 static void ui_but_string_free_internal(uiBut *but)
2238 if (but->str != but->strdata) {
2239 MEM_freeN(but->str);
2241 /* must call 'ui_but_string_set_internal' after */
2246 bool ui_but_string_set(bContext *C, uiBut *but, const char *str)
2248 if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2249 if (RNA_property_editable(&but->rnapoin, but->rnaprop)) {
2252 type = RNA_property_type(but->rnaprop);
2254 if (type == PROP_STRING) {
2256 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
2259 else if (type == PROP_POINTER) {
2261 PointerRNA ptr, rptr;
2264 if (str[0] == '\0') {
2265 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
2269 ptr = but->rnasearchpoin;
2270 prop = but->rnasearchprop;
2272 if (prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
2273 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
2280 else if (type == PROP_ENUM) {
2282 if (RNA_property_enum_value(but->block->evil_C, &but->rnapoin, but->rnaprop, str, &value)) {
2283 RNA_property_enum_set(&but->rnapoin, but->rnaprop, value);
2293 else if (but->type == UI_BTYPE_TEXT) {
2295 if (ui_but_is_utf8(but)) BLI_strncpy_utf8(but->poin, str, but->hardmax);
2296 else BLI_strncpy(but->poin, str, but->hardmax);
2300 else if (but->type == UI_BTYPE_SEARCH_MENU) {
2302 BLI_strncpy(but->poin, str, but->hardmax);
2305 else if (ui_but_anim_expression_set(but, str)) {
2306 /* driver expression */
2309 else if (str[0] == '#') {
2310 /* shortcut to create new driver expression (versus immediate Py-execution) */
2311 return ui_but_anim_expression_create(but, str + 1);
2314 /* number editing */
2317 if (ui_but_string_set_eval_num(C, but, str, &value) == false) {
2321 if (!ui_but_is_float(but)) value = (int)floor(value + 0.5);
2323 /* not that we use hard limits here */
2324 if (value < (double)but->hardmin) value = but->hardmin;
2325 if (value > (double)but->hardmax) value = but->hardmax;
2327 ui_but_value_set(but, value);
2334 void ui_but_default_set(bContext *C, const bool all, const bool use_afterfunc)
2336 wmOperatorType *ot = WM_operatortype_find("UI_OT_reset_default_button", true);
2338 if (use_afterfunc) {
2340 ptr = ui_handle_afterfunc_add_operator(ot, WM_OP_EXEC_DEFAULT, true);
2341 RNA_boolean_set(ptr, "all", all);
2345 WM_operator_properties_create_ptr(&ptr, ot);
2346 RNA_boolean_set(&ptr, "all", all);
2347 WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &ptr);
2348 WM_operator_properties_free(&ptr);
2352 static double soft_range_round_up(double value, double max)
2354 /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, ..
2355 * checking for 0.0 prevents floating point exceptions */
2356 double newmax = (value != 0.0) ? pow(10.0, ceil(log(value) / M_LN10)) : 0.0;
2358 if (newmax * 0.2 >= max && newmax * 0.2 >= value)
2359 return newmax * 0.2;
2360 else if (newmax * 0.5 >= max && newmax * 0.5 >= value)
2361 return newmax * 0.5;
2366 static double soft_range_round_down(double value, double max)
2368 /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, ..
2369 * checking for 0.0 prevents floating point exceptions */
2370 double newmax = (value != 0.0) ? pow(10.0, floor(log(value) / M_LN10)) : 0.0;
2372 if (newmax * 5.0 <= max && newmax * 5.0 <= value)
2373 return newmax * 5.0;
2374 else if (newmax * 2.0 <= max && newmax * 2.0 <= value)
2375 return newmax * 2.0;
2380 /* note: this could be split up into functions which handle arrays and not */
2381 static void ui_set_but_soft_range(uiBut *but)
2383 /* ideally we would not limit this but practically, its more than
2384 * enough worst case is very long vectors wont use a smart soft-range
2385 * which isn't so bad. */
2388 const PropertyType type = RNA_property_type(but->rnaprop);
2389 double softmin, softmax /*, step, precision*/;
2393 /* clamp button range to something reasonable in case
2394 * we get -inf/inf from RNA properties */
2395 if (type == PROP_INT) {
2396 const bool is_array = RNA_property_array_check(but->rnaprop);
2397 int imin, imax, istep;
2399 RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
2400 softmin = (imin == INT_MIN) ? -1e4 : imin;
2401 softmax = (imin == INT_MAX) ? 1e4 : imax;
2402 /*step = istep;*/ /*UNUSED*/
2403 /*precision = 1;*/ /*UNUSED*/
2407 RNA_property_int_get_array_range(&but->rnapoin, but->rnaprop, value_range);
2408 value_min = (double)value_range[0];
2409 value_max = (double)value_range[1];
2412 value_min = value_max = (double)RNA_property_int_get(&but->rnapoin, but->rnaprop);
2415 else if (type == PROP_FLOAT) {
2416 const bool is_array = RNA_property_array_check(but->rnaprop);
2417 float fmin, fmax, fstep, fprecision;
2419 RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
2420 softmin = (fmin == -FLT_MAX) ? (float)-1e4 : fmin;
2421 softmax = (fmax == FLT_MAX) ? (float)1e4 : fmax;
2422 /*step = fstep;*/ /*UNUSED*/
2423 /*precision = fprecision;*/ /*UNUSED*/
2426 float value_range[2];
2427 RNA_property_float_get_array_range(&but->rnapoin, but->rnaprop, value_range);
2428 value_min = (double)value_range[0];
2429 value_max = (double)value_range[1];
2432 value_min = value_max = (double)RNA_property_float_get(&but->rnapoin, but->rnaprop);
2439 /* if the value goes out of the soft/max range, adapt the range */
2440 if (value_min + 1e-10 < softmin) {
2441 if (value_min < 0.0)
2442 softmin = -soft_range_round_up(-value_min, -softmin);
2444 softmin = soft_range_round_down(value_min, softmin);
2446 if (softmin < (double)but->hardmin)
2447 softmin = (double)but->hardmin;
2449 if (value_max - 1e-10 > softmax) {
2450 if (value_max < 0.0)
2451 softmax = -soft_range_round_down(-value_max, -softmax);
2453 softmax = soft_range_round_up(value_max, softmax);
2455 if (softmax > (double)but->hardmax)
2456 softmax = but->hardmax;
2459 but->softmin = softmin;
2460 but->softmax = softmax;
2462 else if (but->poin && (but->pointype & UI_BUT_POIN_TYPES)) {
2463 float value = ui_but_value_get(but);
2464 CLAMP(value, but->hardmin, but->hardmax);
2465 but->softmin = min_ff(but->softmin, value);
2466 but->softmax = max_ff(but->softmax, value);
2473 /* ******************* Free ********************/
2475 static void ui_free_link(uiLink *link)
2478 BLI_freelistN(&link->lines);
2483 /* can be called with C==NULL */
2484 static void ui_but_free(const bContext *C, uiBut *but)
2487 WM_operator_properties_free(but->opptr);
2488 MEM_freeN(but->opptr);
2491 if (but->func_argN) {
2492 MEM_freeN(but->func_argN);
2495 if (but->tip_argN) {
2496 MEM_freeN(but->tip_argN);
2500 /* XXX solve later, buttons should be free-able without context ideally,
2501 * however they may have open tooltips or popup windows, which need to
2502 * be closed using a context pointer */
2504 ui_but_active_free(C, but);
2508 MEM_freeN(but->active);
2512 if (but->str && but->str != but->strdata) {
2513 MEM_freeN(but->str);
2515 ui_free_link(but->link);
2517 if ((but->type == UI_BTYPE_IMAGE) && but->poin) {
2518 IMB_freeImBuf((struct ImBuf *)but->poin);
2521 BLI_assert(UI_butstore_is_registered(but->block, but) == false);
2526 /* can be called with C==NULL */
2527 void UI_block_free(const bContext *C, uiBlock *block)
2531 UI_butstore_clear(block);
2533 while ((but = BLI_pophead(&block->buttons))) {
2534 ui_but_free(C, but);
2538 MEM_freeN(block->unit);
2541 if (block->func_argN) {
2542 MEM_freeN(block->func_argN);
2545 CTX_store_free_list(&block->contexts);
2547 BLI_freelistN(&block->saferct);
2548 BLI_freelistN(&block->color_pickers.list);
2553 /* can be called with C==NULL */
2554 void UI_blocklist_free(const bContext *C, ListBase *lb)
2558 while ((block = BLI_pophead(lb))) {
2559 UI_block_free(C, block);
2563 void UI_blocklist_free_inactive(const bContext *C, ListBase *lb)
2565 uiBlock *block, *nextblock;
2567 for (block = lb->first; block; block = nextblock) {
2568 nextblock = block->next;
2570 if (!block->handle) {
2571 if (!block->active) {
2572 BLI_remlink(lb, block);
2573 UI_block_free(C, block);
2581 void UI_block_region_set(uiBlock *block, ARegion *region)
2583 ListBase *lb = ®ion->uiblocks;
2584 uiBlock *oldblock = NULL;
2586 /* each listbase only has one block with this name, free block
2587 * if is already there so it can be rebuilt from scratch */
2589 oldblock = BLI_findstring(lb, block->name, offsetof(uiBlock, name));
2592 oldblock->active = 0;
2593 oldblock->panel = NULL;
2594 oldblock->handle = NULL;
2597 /* at the beginning of the list! for dynamical menus/blocks */
2598 BLI_addhead(lb, block);
2601 block->oldblock = oldblock;
2604 uiBlock *UI_block_begin(const bContext *C, ARegion *region, const char *name, short dt)
2609 int getsizex, getsizey;
2611 window = CTX_wm_window(C);
2612 scn = CTX_data_scene(C);
2614 block = MEM_callocN(sizeof(uiBlock), "uiBlock");
2617 block->evil_C = (void *)C; /* XXX */
2620 block->color_profile = true;
2622 /* store display device name, don't lookup for transformations yet
2623 * block could be used for non-color displays where looking up for transformation
2624 * would slow down redraw, so only lookup for actual transform when it's indeed
2627 BLI_strncpy(block->display_device, scn->display_settings.display_device, sizeof(block->display_device));
2629 /* copy to avoid crash when scene gets deleted with ui still open */
2630 block->unit = MEM_mallocN(sizeof(scn->unit), "UI UnitSettings");
2631 memcpy(block->unit, &scn->unit, sizeof(scn->unit));
2634 BLI_strncpy(block->name, name, sizeof(block->name));
2637 UI_block_region_set(block, region);
2639 /* window matrix and aspect */
2640 if (region && region->swinid) {
2641 wm_subwindow_matrix_get(window, region->swinid, block->winmat);
2642 wm_subwindow_size_get(window, region->swinid, &getsizex, &getsizey);
2644 block->aspect = 2.0f / fabsf(getsizex * block->winmat[0][0]);
2647 /* no subwindow created yet, for menus for example, so we
2648 * use the main window instead, since buttons are created
2650 wm_subwindow_matrix_get(window, window->screen->mainwin, block->winmat);
2651 wm_subwindow_size_get(window, window->screen->mainwin, &getsizex, &getsizey);
2653 block->aspect = 2.0f / fabsf(getsizex * block->winmat[0][0]);
2654 block->auto_open = true;
2655 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
2661 uiBlock *UI_block_find_in_region(const char *name, ARegion *ar)
2663 return BLI_findstring(&ar->uiblocks, name, offsetof(uiBlock, name));
2666 void UI_block_emboss_set(uiBlock *block, char dt)
2671 void ui_but_update(uiBut *but)
2673 /* if something changed in the button */
2674 double value = UI_BUT_VALUE_UNSET;
2675 // float okwidth; // UNUSED
2677 ui_but_update_select_flag(but, &value);
2679 /* only update soft range while not editing */
2680 if (!(but->editval || but->editstr || but->editvec)) {
2681 if ((but->rnaprop != NULL) ||
2682 (but->poin && (but->pointype & UI_BUT_POIN_TYPES)))
2684 ui_set_but_soft_range(but);
2688 /* test for min and max, icon sliders, etc */
2689 switch (but->type) {
2691 case UI_BTYPE_SCROLL:
2692 case UI_BTYPE_NUM_SLIDER:
2693 UI_GET_BUT_VALUE_INIT(but, value);
2694 if (value < (double)but->hardmin) ui_but_value_set(but, but->hardmin);
2695 else if (value > (double)but->hardmax) ui_but_value_set(but, but->hardmax);
2697 /* max must never be smaller than min! Both being equal is allowed though */
2698 BLI_assert(but->softmin <= but->softmax &&
2699 but->hardmin <= but->hardmax);
2702 case UI_BTYPE_ICON_TOGGLE:
2703 case UI_BTYPE_ICON_TOGGLE_N:
2704 if (!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2705 if (but->flag & UI_SELECT) but->iconadd = 1;
2706 else but->iconadd = 0;
2710 /* quiet warnings for unhandled types */
2716 /* safety is 4 to enable small number buttons (like 'users') */
2717 // okwidth = -4 + (BLI_rcti_size_x(&but->rect)); // UNUSED
2720 switch (but->type) {
2723 if (BLI_rctf_size_x(&but->rect) > 24.0f) {
2724 /* only needed for menus in popup blocks that don't recreate buttons on redraw */
2725 if (but->block->flag & UI_BLOCK_LOOP) {
2726 if (but->rnaprop && (RNA_property_type(but->rnaprop) == PROP_ENUM)) {
2727 int value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
2729 if (RNA_property_enum_name_gettexted(but->block->evil_C,
2730 &but->rnapoin, but->rnaprop, value, &buf))
2732 size_t slen = strlen(buf);
2733 ui_but_string_free_internal(but);
2734 ui_but_string_set_internal(but, buf, slen);
2738 BLI_strncpy(but->drawstr, but->str, sizeof(but->drawstr));
2743 case UI_BTYPE_NUM_SLIDER:
2745 if (!but->editstr) {
2746 const char *drawstr_suffix = NULL;
2749 UI_GET_BUT_VALUE_INIT(but, value);
2751 slen = BLI_strncpy_rlen(but->drawstr, but->str, sizeof(but->drawstr));
2753 if (ui_but_is_float(but)) {
2754 if (value == (double) FLT_MAX) {
2755 slen += BLI_strncpy_rlen(but->drawstr + slen, "inf", sizeof(but->drawstr) - slen);
2757 else if (value == (double) -FLT_MAX) {
2758 slen += BLI_strncpy_rlen(but->drawstr + slen, "-inf", sizeof(but->drawstr) - slen);
2760 /* support length type buttons */
2761 else if (ui_but_is_unit(but)) {
2762 char new_str[sizeof(but->drawstr)];
2763 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, true, -1);
2764 slen += BLI_strncpy_rlen(but->drawstr + slen, new_str, sizeof(but->drawstr) - slen);
2767 const int prec = ui_but_calc_float_precision(but, value);
2768 slen += BLI_snprintf(but->drawstr + slen, sizeof(but->drawstr) - slen, "%.*f", prec, value);
2772 slen += BLI_snprintf(but->drawstr + slen, sizeof(but->drawstr) - slen, "%d", (int)value);
2776 PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2778 if (pstype == PROP_PERCENTAGE) {
2779 drawstr_suffix = "%";
2781 else if (pstype == PROP_PIXEL) {
2782 drawstr_suffix = " px";
2786 if (drawstr_suffix) {
2787 BLI_strncpy(but->drawstr + slen, drawstr_suffix, sizeof(but->drawstr) - slen);
2793 case UI_BTYPE_LABEL:
2794 if (ui_but_is_float(but)) {
2796 UI_GET_BUT_VALUE_INIT(but, value);
2797 prec = ui_but_calc_float_precision(but, value);
2798 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2801 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2807 case UI_BTYPE_SEARCH_MENU:
2808 if (!but->editstr) {
2809 char str[UI_MAX_DRAW_STR];
2811 ui_but_string_get(but, str, UI_MAX_DRAW_STR);
2812 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, str);
2816 case UI_BTYPE_KEY_EVENT:
2819 if (but->flag & UI_SELECT) {
2820 str = "Press a key";
2823 UI_GET_BUT_VALUE_INIT(but, value);
2824 str = WM_key_event_string((short)value);
2826 BLI_snprintf(but->drawstr, UI_MAX_DRAW_STR, "%s%s", but->str, str);
2829 case UI_BTYPE_HOTKEY_EVENT:
2830 if (but->flag & UI_SELECT) {
2832 if (but->modifier_key) {
2833 char *str = but->drawstr;
2834 but->drawstr[0] = '\0';
2836 if (but->modifier_key & KM_SHIFT)
2837 str += BLI_strcpy_rlen(str, "Shift ");
2838 if (but->modifier_key & KM_CTRL)
2839 str += BLI_strcpy_rlen(str, "Ctrl ");
2840 if (but->modifier_key & KM_ALT)
2841 str += BLI_strcpy_rlen(str, "Alt ");
2842 if (but->modifier_key & KM_OSKEY)
2843 str += BLI_strcpy_rlen(str, "Cmd ");
2845 (void)str; /* UNUSED */
2848 BLI_strncpy(but->drawstr, "Press a key", UI_MAX_DRAW_STR);
2852 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2856 case UI_BTYPE_HSVCUBE:
2857 case UI_BTYPE_HSVCIRCLE:
2860 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2865 /* if we are doing text editing, this will override the drawstr */
2867 but->drawstr[0] = '\0';
2869 /* text clipping moved to widget drawing code itself */
2873 void UI_block_align_begin(uiBlock *block)
2875 /* if other align was active, end it */
2876 if (block->flag & UI_BUT_ALIGN) UI_block_align_end(block);
2878 block->flag |= UI_BUT_ALIGN_DOWN;
2881 /* buttons declared after this call will get this align nr */ // XXX flag?
2884 static bool buts_are_horiz(uiBut *but1, uiBut *but2)
2888 /* simple case which can fail if buttons shift apart
2889 * with proportional layouts, see: [#38602] */
2890 if ((but1->rect.ymin == but2->rect.ymin) &&
2891 (but1->rect.xmin != but2->rect.xmin))
2896 dx = fabsf(but1->rect.xmax - but2->rect.xmin);
2897 dy = fabsf(but1->rect.ymin - but2->rect.ymax);
2902 void UI_block_align_end(uiBlock *block)
2904 block->flag &= ~UI_BUT_ALIGN; /* all 4 flags */
2907 bool ui_but_can_align(uiBut *but)
2909 return !ELEM(but->type, UI_BTYPE_LABEL, UI_BTYPE_CHECKBOX, UI_BTYPE_CHECKBOX_N, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE);
2912 static void ui_block_align_calc_but(uiBut *first, short nr)
2914 uiBut *prev, *but = NULL, *next;
2915 int flag = 0, cols = 0, rows = 0;
2919 for (but = first; but && but->alignnr == nr; but = but->next) {
2920 if (but->next && but->next->alignnr == nr) {
2921 if (buts_are_horiz(but, but->next)) cols++;
2926 /* rows == 0: 1 row, cols == 0: 1 column */
2928 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2929 for (but = first, prev = NULL; but && but->alignnr == nr; prev = but, but = but->next) {
2931 if (next && next->alignnr != nr)
2934 /* clear old flag */
2935 but->drawflag &= ~UI_BUT_ALIGN;
2937 if (flag == 0) { /* first case */
2939 if (buts_are_horiz(but, next)) {
2941 flag = UI_BUT_ALIGN_RIGHT;
2943 flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_RIGHT;
2946 flag = UI_BUT_ALIGN_DOWN;
2950 else if (next == NULL) { /* last case */
2952 if (buts_are_horiz(prev, but)) {
2954 flag = UI_BUT_ALIGN_LEFT;
2956 flag = UI_BUT_ALIGN_TOP | UI_BUT_ALIGN_LEFT;
2959 flag = UI_BUT_ALIGN_TOP;
2963 else if (buts_are_horiz(but, next)) {
2964 /* check if this is already second row */
2965 if (prev && buts_are_horiz(prev, but) == 0) {
2966 flag &= ~UI_BUT_ALIGN_LEFT;
2967 flag |= UI_BUT_ALIGN_TOP;
2968 /* exception case: bottom row */
2971 while (bt && bt->alignnr == nr) {
2972 if (bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next) == 0) {
2977 if (bt == NULL || bt->alignnr != nr) flag = UI_BUT_ALIGN_TOP | UI_BUT_ALIGN_RIGHT;
2981 flag |= UI_BUT_ALIGN_LEFT;
2986 flag |= UI_BUT_ALIGN_TOP;
2988 else { /* next button switches to new row */
2990 if (prev && buts_are_horiz(prev, but))
2991 flag |= UI_BUT_ALIGN_LEFT;
2993 flag &= ~UI_BUT_ALIGN_LEFT;
2994 flag |= UI_BUT_ALIGN_TOP;
2997 if ((flag & UI_BUT_ALIGN_TOP) == 0) { /* stil top row */
2999 if (next && buts_are_horiz(but, next))
3000 flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_LEFT | UI_BUT_ALIGN_RIGHT;
3002 /* last button in top row */
3003 flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_LEFT;
3007 flag |= UI_BUT_ALIGN_DOWN;
3010 flag |= UI_BUT_ALIGN_TOP;
3014 but->drawflag |= flag;
3016 /* merge coordinates */