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
37 #include "MEM_guardedalloc.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_userdef_types.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_dynstr.h"
46 #include "BLI_utildefines.h"
48 #include "BKE_context.h"
49 #include "BKE_library.h"
51 #include "BKE_screen.h"
52 #include "BKE_idprop.h"
53 #include "BKE_utildefines.h" /* FILE_MAX */
58 #include "BLF_translation.h"
60 #include "UI_interface.h"
62 #include "IMB_imbuf.h"
66 #include "wm_subwindow.h"
67 #include "wm_window.h"
69 #include "RNA_access.h"
71 #include "BPY_extern.h"
73 #include "interface_intern.h"
75 #define MENU_WIDTH 120
76 #define MENU_ITEM_HEIGHT 20
77 #define MENU_SEP_HEIGHT 6
79 #define PRECISION_FLOAT_MAX 6
80 #define PRECISION_FLOAT_MAX_POW 1000000 /* pow(10, PRECISION_FLOAT_MAX) */
82 /* avoid unneeded calls to ui_get_but_val */
83 #define UI_BUT_VALUE_UNSET DBL_MAX
84 #define UI_GET_BUT_VALUE_INIT(_but, _value) if(_value == DBL_MAX) { (_value)= ui_get_but_val(_but); }
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_free_but(const bContext *C, uiBut *but);
95 /* ************* window matrix ************** */
97 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
100 int sx, sy, getsizex, getsizey;
102 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
103 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
111 gx += block->panel->ofsx;
112 gy += block->panel->ofsy;
115 *x= ((float)sx) + ((float)getsizex)*(0.5f+ 0.5f*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
116 *y= ((float)sy) + ((float)getsizey)*(0.5f+ 0.5f*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
119 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
126 ui_block_to_window_fl(ar, block, &fx, &fy);
132 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
137 ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
138 ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
140 winr->xmin= tmpr.xmin;
141 winr->ymin= tmpr.ymin;
142 winr->xmax= tmpr.xmax;
143 winr->ymax= tmpr.ymax;
146 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y) /* for mouse cursor */
148 float a, b, c, d, e, f, px, py;
149 int sx, sy, getsizex, getsizey;
151 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
152 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
156 a= 0.5f*((float)getsizex)*block->winmat[0][0];
157 b= 0.5f*((float)getsizex)*block->winmat[1][0];
158 c= 0.5f*((float)getsizex)*(1.0f+block->winmat[3][0]);
160 d= 0.5f*((float)getsizey)*block->winmat[0][1];
161 e= 0.5f*((float)getsizey)*block->winmat[1][1];
162 f= 0.5f*((float)getsizey)*(1.0f+block->winmat[3][1]);
167 *y= (a*(py-f) + d*(c-px))/(a*e-d*b);
168 *x= (px- b*(*y)- c)/a;
171 *x -= block->panel->ofsx;
172 *y -= block->panel->ofsy;
176 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
183 ui_window_to_block_fl(ar, block, &fx, &fy);
189 void ui_window_to_region(const ARegion *ar, int *x, int *y)
191 *x-= ar->winrct.xmin;
192 *y-= ar->winrct.ymin;
195 /* ******************* block calc ************************* */
197 void ui_block_translate(uiBlock *block, int x, int y)
201 for(bt= block->buttons.first; bt; bt=bt->next) {
214 static void ui_text_bounds_block(uiBlock *block, float offset)
216 uiStyle *style=UI_GetStyle();
218 int i = 0, j, x1addval= offset, nextcol;
219 int lastcol= 0, col= 0;
221 uiStyleFontSet(&style->widget);
223 for(bt= block->buttons.first; bt; bt= bt->next) {
225 j= BLF_width(style->widget.uifont_id, bt->drawstr);
230 if(bt->next && bt->x1 < bt->next->x1)
234 /* cope with multi collumns */
235 bt= block->buttons.first;
237 if(bt->next && bt->x1 < bt->next->x1) {
244 bt->x2 = bt->x1 + i + block->bounds;
247 bt->x2= MAX2(bt->x2, offset + block->minbounds);
249 ui_check_but(bt); // clips text again
252 x1addval+= i + block->bounds;
258 void ui_bounds_block(uiBlock *block)
263 if(block->buttons.first==NULL) {
265 block->minx= 0.0; block->maxx= block->panel->sizex;
266 block->miny= 0.0; block->maxy= block->panel->sizey;
271 block->minx= block->miny= 10000;
272 block->maxx= block->maxy= -10000;
274 bt= block->buttons.first;
276 if(bt->x1 < block->minx) block->minx= bt->x1;
277 if(bt->y1 < block->miny) block->miny= bt->y1;
279 if(bt->x2 > block->maxx) block->maxx= bt->x2;
280 if(bt->y2 > block->maxy) block->maxy= bt->y2;
285 block->minx -= block->bounds;
286 block->miny -= block->bounds;
287 block->maxx += block->bounds;
288 block->maxy += block->bounds;
291 block->maxx= block->minx + MAX2(block->maxx - block->minx, block->minbounds);
293 /* hardcoded exception... but that one is annoying with larger safety */
294 bt= block->buttons.first;
295 if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
298 block->safety.xmin= block->minx-xof;
299 block->safety.ymin= block->miny-xof;
300 block->safety.xmax= block->maxx+xof;
301 block->safety.ymax= block->maxy+xof;
304 static void ui_centered_bounds_block(const bContext *C, uiBlock *block)
306 wmWindow *window= CTX_wm_window(C);
311 /* note: this is used for the splash where window bounds event has not been
312 * updated by ghost, get the window bounds from ghost directly */
314 // wm_window_get_size(window, &xmax, &ymax);
315 wm_window_get_size_ghost(window, &xmax, &ymax);
317 ui_bounds_block(block);
319 width= block->maxx - block->minx;
320 height= block->maxy - block->miny;
322 startx = (xmax * 0.5f) - (width * 0.5f);
323 starty = (ymax * 0.5f) - (height * 0.5f);
325 ui_block_translate(block, startx - block->minx, starty - block->miny);
327 /* now recompute bounds and safety */
328 ui_bounds_block(block);
331 static void ui_popup_bounds_block(const bContext *C, uiBlock *block, int bounds_calc)
333 wmWindow *window= CTX_wm_window(C);
334 int startx, starty, endx, endy, width, height, oldwidth, oldheight;
335 int oldbounds, xmax, ymax;
337 oldbounds= block->bounds;
339 /* compute mouse position with user defined offset */
340 ui_bounds_block(block);
342 wm_window_get_size(window, &xmax, &ymax);
344 oldwidth= block->maxx - block->minx;
345 oldheight= block->maxy - block->miny;
347 /* first we ensure wide enough text bounds */
348 if(bounds_calc==UI_BLOCK_BOUNDS_POPUP_MENU) {
349 if(block->flag & UI_BLOCK_LOOP) {
351 ui_text_bounds_block(block, block->minx);
355 /* next we recompute bounds */
356 block->bounds= oldbounds;
357 ui_bounds_block(block);
359 /* and we adjust the position to fit within window */
360 width= block->maxx - block->minx;
361 height= block->maxy - block->miny;
363 /* avoid divide by zero below, caused by calling with no UI, but better not crash */
364 oldwidth= oldwidth > 0 ? oldwidth : MAX2(1, width);
365 oldheight= oldheight > 0 ? oldheight : MAX2(1, height);
367 /* offset block based on mouse position, user offset is scaled
368 * along in case we resized the block in ui_text_bounds_block */
369 startx= window->eventstate->x + block->minx + (block->mx*width)/oldwidth;
370 starty= window->eventstate->y + block->miny + (block->my*height)/oldheight;
389 ui_block_translate(block, startx - block->minx, starty - block->miny);
391 /* now recompute bounds and safety */
392 ui_bounds_block(block);
395 /* used for various cases */
396 void uiBoundsBlock(uiBlock *block, int addval)
401 block->bounds= addval;
402 block->dobounds= UI_BLOCK_BOUNDS;
405 /* used for pulldowns */
406 void uiTextBoundsBlock(uiBlock *block, int addval)
408 block->bounds= addval;
409 block->dobounds= UI_BLOCK_BOUNDS_TEXT;
412 /* used for block popups */
413 void uiPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
415 block->bounds= addval;
416 block->dobounds= UI_BLOCK_BOUNDS_POPUP_MOUSE;
421 /* used for menu popups */
422 void uiMenuPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
424 block->bounds= addval;
425 block->dobounds= UI_BLOCK_BOUNDS_POPUP_MENU;
430 /* used for centered popups, i.e. splash */
431 void uiCenteredBoundsBlock(uiBlock *block, int addval)
433 block->bounds= addval;
434 block->dobounds= UI_BLOCK_BOUNDS_POPUP_CENTER;
437 void uiExplicitBoundsBlock(uiBlock *block, int minx, int miny, int maxx, int maxy)
446 /* ************** LINK LINE DRAWING ************* */
448 /* link line drawing is not part of buttons or theme.. so we stick with it here */
450 static int ui_but_float_precision(uiBut *but, double value)
454 /* first check if prec is 0 and fallback to a simple default */
455 if((prec= (int)but->a2) == 0) {
456 prec= (but->hardmax < 10.001f) ? 3 : 2;
459 /* check on the number of decimal places neede to display
460 * the number, this is so 0.00001 is not displayed as 0.00,
461 * _but_, this is only for small values si 10.0001 will not get
462 * the same treatment */
463 if(value != 0.0 && (value= ABS(value)) < 0.1) {
464 int value_i= (int)((value * PRECISION_FLOAT_MAX_POW) + 0.5);
466 const int prec_span= 3; /* show: 0.01001, 5 would allow 0.0100001 for eg. */
470 int i= PRECISION_FLOAT_MAX;
471 while(i && value_i) {
480 /* even though its a small value, if the second last digit is not 0, use it */
481 test_prec = prec_min;
483 dec_flag= (dec_flag >> (prec_min + 1)) & ((1 << prec_span) - 1);
487 dec_flag = dec_flag >> 1;
490 if(test_prec > prec) {
496 CLAMP(prec, 1, PRECISION_FLOAT_MAX);
501 static void ui_draw_linkline(uiLinkLine *line)
505 if(line->from==NULL || line->to==NULL) return;
507 rect.xmin= (line->from->x1+line->from->x2)/2.0f;
508 rect.ymin= (line->from->y1+line->from->y2)/2.0f;
509 rect.xmax= (line->to->x1+line->to->x2)/2.0f;
510 rect.ymax= (line->to->y1+line->to->y2)/2.0f;
512 if(line->flag & UI_SELECT)
513 glColor3ub(100,100,100);
517 ui_draw_link_bezier(&rect);
520 static void ui_draw_links(uiBlock *block)
525 but= block->buttons.first;
527 if(but->type==LINK && but->link) {
528 line= but->link->lines.first;
530 ui_draw_linkline(line);
538 /* ************** BLOCK ENDING FUNCTION ************* */
540 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
541 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
543 /* various properties are being compared here, hopefully sufficient
544 * to catch all cases, but it is simple to add more checks later */
545 if(but->retval != oldbut->retval) return 0;
546 if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
547 if(but->rnaprop != oldbut->rnaprop)
548 if(but->rnaindex != oldbut->rnaindex) return 0;
549 if(but->func != oldbut->func) return 0;
550 if(but->funcN != oldbut->funcN) return 0;
551 if(oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return 0;
552 if(oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return 0;
553 if(!but->funcN && ((but->poin != oldbut->poin && (uiBut*)oldbut->poin != oldbut) || but->pointype != oldbut->pointype)) return 0;
554 if(but->optype != oldbut->optype) return 0;
559 /* oldbut is being inserted in new block, so we use the lines from new button, and replace button pointers */
560 static void ui_but_update_linklines(uiBlock *block, uiBut *oldbut, uiBut *newbut)
565 /* if active button is LINK */
566 if(newbut->type==LINK && newbut->link) {
568 SWAP(uiLink *, oldbut->link, newbut->link);
570 for(line= oldbut->link->lines.first; line; line= line->next) {
573 if(line->from==newbut)
578 /* check all other button links */
579 for(but= block->buttons.first; but; but= but->next) {
580 if(but!=newbut && but->type==LINK && but->link) {
581 for(line= but->link->lines.first; line; line= line->next) {
584 if(line->from==newbut)
591 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut **butpp)
594 uiBut *oldbut, *but= *butpp;
597 oldblock= block->oldblock;
601 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
602 if(ui_but_equals_old(oldbut, but)) {
605 // but->flag= oldbut->flag;
607 /* exception! redalert flag can't be update from old button.
608 * perhaps it should only copy specific flags rather than all. */
609 // but->flag= (oldbut->flag & ~UI_BUT_REDALERT) | (but->flag & UI_BUT_REDALERT);
611 // but->active= oldbut->active;
612 // but->pos= oldbut->pos;
613 // but->ofs= oldbut->ofs;
614 // but->editstr= oldbut->editstr;
615 // but->editval= oldbut->editval;
616 // but->editvec= oldbut->editvec;
617 // but->editcoba= oldbut->editcoba;
618 // but->editcumap= oldbut->editcumap;
619 // but->selsta= oldbut->selsta;
620 // but->selend= oldbut->selend;
621 // but->softmin= oldbut->softmin;
622 // but->softmax= oldbut->softmax;
623 // but->linkto[0]= oldbut->linkto[0];
624 // but->linkto[1]= oldbut->linkto[1];
626 // oldbut->active= NULL;
628 /* move button over from oldblock to new block */
629 BLI_remlink(&oldblock->buttons, oldbut);
630 BLI_insertlink(&block->buttons, but, oldbut);
631 oldbut->block= block;
634 /* still stuff needs to be copied */
635 oldbut->x1= but->x1; oldbut->y1= but->y1;
636 oldbut->x2= but->x2; oldbut->y2= but->y2;
637 oldbut->context= but->context; /* set by Layout */
639 /* typically the same pointers, but not on undo/redo */
640 /* XXX some menu buttons store button itself in but->poin. Ugly */
641 if(oldbut->poin != (char *)oldbut) {
642 SWAP(char *, oldbut->poin, but->poin)
643 SWAP(void *, oldbut->func_argN, but->func_argN)
646 /* copy hardmin for list rows to prevent 'sticking' highlight to mouse position
647 * when scrolling without moving mouse (see [#28432]) */
648 if(ELEM(oldbut->type, ROW, LISTROW))
649 oldbut->hardmax= but->hardmax;
651 ui_but_update_linklines(block, oldbut, but);
653 BLI_remlink(&block->buttons, but);
656 /* note: if layout hasn't been applied yet, it uses old button pointers... */
659 /* ensures one button can get activated, and in case the buttons
660 * draw are the same this gives O(1) lookup for each button */
661 BLI_remlink(&oldblock->buttons, oldbut);
662 ui_free_but(C, oldbut);
672 /* needed for temporarily rename buttons, such as in outliner or fileselect,
673 * they should keep calling uiDefButs to keep them alive */
674 /* returns 0 when button removed */
675 int uiButActiveOnly(const bContext *C, uiBlock *block, uiBut *but)
679 int activate= 0, found= 0, isactive= 0;
681 oldblock= block->oldblock;
685 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
686 if(ui_but_equals_old(oldbut, but)) {
696 if(activate || found==0) {
697 ui_button_activate_do( (bContext *)C, CTX_wm_region(C), but);
699 else if(found && isactive==0) {
701 BLI_remlink(&block->buttons, but);
709 /* use to check if we need to disable undo, but dont make any changes
710 * returns FALSE if undo needs to be disabled. */
711 static int ui_but_is_rna_undo(uiBut *but)
713 if(but->rnapoin.id.data) {
714 /* avoid undo push for buttons who's ID are screen or wm level
715 * we could disable undo for buttons with no ID too but may have
716 * unforeseen consequences, so best check for ID's we _know_ are not
717 * handled by undo - campbell */
718 ID *id= but->rnapoin.id.data;
719 if(ID_CHECK_UNDO(id) == FALSE) {
726 else if (but->rnapoin.type && !RNA_struct_undo_check(but->rnapoin.type)) {
733 /* assigns automatic keybindings to menu items for fast access
734 * (underline key in menu) */
735 static void ui_menu_block_set_keyaccels(uiBlock *block)
739 unsigned int menu_key_mask= 0;
740 unsigned char menu_key;
745 /* only do it before bounding */
746 if(block->minx != block->maxx)
749 for(pass=0; pass<2; pass++) {
750 /* 2 Passes, on for first letter only, second for any letter if first fails
751 * fun first pass on all buttons so first word chars always get first priority */
753 for(but=block->buttons.first; but; but=but->next) {
754 if(!ELEM4(but->type, BUT, MENU, BLOCK, PULLDOWN) || (but->flag & UI_HIDDEN)) {
757 else if(but->menu_key=='\0') {
759 for(str_pt= but->str; *str_pt; ) {
760 menu_key= tolower(*str_pt);
761 if((menu_key >= 'a' && menu_key <= 'z') && !(menu_key_mask & 1<<(menu_key-'a'))) {
762 menu_key_mask |= 1<<(menu_key-'a');
767 /* Skip to next delimeter on first pass (be picky) */
768 while(isalpha(*str_pt))
775 /* just step over every char second pass and find first usable key */
781 but->menu_key= menu_key;
784 /* run second pass */
788 /* if all keys have been used just exit, unlikely */
789 if(menu_key_mask == (1<<26)-1) {
796 /* check if second pass is needed */
804 static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
811 IDProperty *prop_menu= NULL;
812 IDProperty *prop_menu_name= NULL;
814 /* only do it before bounding */
815 if(block->minx != block->maxx)
819 #define UI_MENU_KEY_STR_CAT \
820 char *butstr_orig= BLI_strdup(but->str); \
821 BLI_snprintf(but->strdata, \
822 sizeof(but->strdata), \
825 MEM_freeN(butstr_orig); \
826 but->str= but->strdata; \
830 for(but=block->buttons.first; but; but=but->next) {
832 IDProperty *prop= (but->opptr)? but->opptr->data: NULL;
834 if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, TRUE,
840 else if ((mt= uiButGetMenuType(but))) {
841 /* only allocate menu property once */
842 if (prop_menu == NULL) {
843 /* annoying, create a property */
844 IDPropertyTemplate val = {0};
845 prop_menu= IDP_New(IDP_GROUP, &val, __func__); /* dummy, name is unimportant */
846 IDP_AddToGroup(prop_menu, (prop_menu_name= IDP_NewString("", "name", sizeof(mt->idname))));
849 IDP_AssignString(prop_menu_name, mt->idname, sizeof(mt->idname));
851 if(WM_key_event_operator_string(C, "WM_OT_call_menu", WM_OP_INVOKE_REGION_WIN, prop_menu, FALSE,
860 IDP_FreeProperty(prop_menu);
861 MEM_freeN(prop_menu);
864 #undef UI_MENU_KEY_STR_CAT
868 void uiEndBlock(const bContext *C, uiBlock *block)
871 Scene *scene= CTX_data_scene(C);
873 /* inherit flags from 'old' buttons that was drawn here previous, based
874 * on matching buttons, we need this to make button event handling non
875 * blocking, while still allowing buttons to be remade each redraw as it
876 * is expected by blender code */
877 for(but=block->buttons.first; but; but=but->next) {
878 if(ui_but_update_from_old_block(C, block, &but))
881 /* temp? Proper check for greying out */
883 wmOperatorType *ot= but->optype;
886 CTX_store_set((bContext*)C, but->context);
888 if(ot == NULL || WM_operator_poll_context((bContext*)C, ot, but->opcontext)==0) {
889 but->flag |= UI_BUT_DISABLED;
894 CTX_store_set((bContext*)C, NULL);
897 ui_but_anim_flag(but, (scene)? scene->r.cfra: 0.0f);
900 if(block->oldblock) {
901 block->auto_open= block->oldblock->auto_open;
902 block->auto_open_last= block->oldblock->auto_open_last;
903 block->tooltipdisabled= block->oldblock->tooltipdisabled;
905 block->oldblock= NULL;
908 /* handle pending stuff */
909 if(block->layouts.first) uiBlockLayoutResolve(block, NULL, NULL);
910 ui_block_do_align(block);
911 if((block->flag & UI_BLOCK_LOOP) && (block->flag & UI_BLOCK_NUMSELECT)) {
912 ui_menu_block_set_keyaccels(block); /* could use a different flag to check */
914 if(block->flag & UI_BLOCK_LOOP) ui_menu_block_set_keymaps(C, block);
917 if(block->dobounds == UI_BLOCK_BOUNDS) ui_bounds_block(block);
918 else if(block->dobounds == UI_BLOCK_BOUNDS_TEXT) ui_text_bounds_block(block, 0.0f);
919 else if(block->dobounds == UI_BLOCK_BOUNDS_POPUP_CENTER) ui_centered_bounds_block(C, block);
920 else if(block->dobounds) ui_popup_bounds_block(C, block, block->dobounds);
922 if(block->minx==0.0f && block->maxx==0.0f) uiBoundsBlock(block, 0);
923 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
928 /* ************** BLOCK DRAWING FUNCTION ************* */
930 void ui_fontscale(short *points, float aspect)
932 if(aspect < 0.9f || aspect > 1.1f) {
933 float pointsf= *points;
935 /* for some reason scaling fonts goes too fast compared to widget size */
936 aspect= sqrt(aspect);
940 *points= ceilf(pointsf);
942 *points= floorf(pointsf);
946 /* project button or block (but==NULL) to pixels in regionspace */
947 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
950 float getsizex, getsizey;
955 gx= (but?but->x1:block->minx) + (block->panel?block->panel->ofsx:0.0f);
956 gy= (but?but->y1:block->miny) + (block->panel?block->panel->ofsy:0.0f);
958 rect->xmin= floorf(getsizex*(0.5f+ 0.5f*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
959 rect->ymin= floorf(getsizey*(0.5f+ 0.5f*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
961 gx= (but?but->x2:block->maxx) + (block->panel?block->panel->ofsx:0.0f);
962 gy= (but?but->y2:block->maxy) + (block->panel?block->panel->ofsy:0.0f);
964 rect->xmax= floorf(getsizex*(0.5f+ 0.5f*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
965 rect->ymax= floorf(getsizey*(0.5f+ 0.5f*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
969 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
970 void uiDrawBlock(const bContext *C, uiBlock *block)
972 uiStyle style= *UI_GetStyle(); // XXX pass on as arg
976 int multisample_enabled;
978 /* get menu region or area region */
981 ar= CTX_wm_region(C);
984 uiEndBlock(C, block);
986 /* disable AA, makes widgets too blurry */
987 multisample_enabled= glIsEnabled(GL_MULTISAMPLE_ARB);
988 if(multisample_enabled)
989 glDisable(GL_MULTISAMPLE_ARB);
991 /* we set this only once */
992 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
995 ui_fontscale(&style.paneltitle.points, block->aspect);
996 ui_fontscale(&style.grouplabel.points, block->aspect);
997 ui_fontscale(&style.widgetlabel.points, block->aspect);
998 ui_fontscale(&style.widget.points, block->aspect);
1000 /* scale block min/max to rect */
1001 ui_but_to_pixelrect(&rect, ar, block, NULL);
1003 /* pixel space for AA widgets */
1004 glMatrixMode(GL_PROJECTION);
1006 glMatrixMode(GL_MODELVIEW);
1010 wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
1013 if(block->flag & UI_BLOCK_LOOP)
1014 ui_draw_menu_back(&style, block, &rect);
1015 else if(block->panel)
1016 ui_draw_aligned_panel(&style, block, &rect);
1019 for(but= block->buttons.first; but; but= but->next) {
1020 if(!(but->flag & (UI_HIDDEN|UI_SCROLLED))) {
1021 ui_but_to_pixelrect(&rect, ar, block, but);
1023 /* XXX: figure out why invalid coordinates happen when closing render window */
1024 /* and material preview is redrawn in main window (temp fix for bug #23848) */
1025 if(rect.xmin < rect.xmax && rect.ymin < rect.ymax)
1026 ui_draw_but(C, ar, &style, but, &rect);
1030 /* restore matrix */
1031 glMatrixMode(GL_PROJECTION);
1033 glMatrixMode(GL_MODELVIEW);
1036 if(multisample_enabled)
1037 glEnable(GL_MULTISAMPLE_ARB);
1039 ui_draw_links(block);
1042 /* ************* EVENTS ************* */
1044 static void ui_is_but_sel(uiBut *but, double *value)
1046 short is_push=0, is_true=1;
1048 if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) is_true= 0;
1052 UI_GET_BUT_VALUE_INIT(but, *value)
1053 lvalue= (int)*value;
1054 if( BTST(lvalue, (but->bitnr)) ) is_push= is_true;
1055 else is_push= !is_true;
1073 UI_GET_BUT_VALUE_INIT(but, *value)
1074 if(*value != (double)but->hardmin) is_push= 1;
1079 UI_GET_BUT_VALUE_INIT(but, *value)
1080 if(*value==0.0) is_push= 1;
1084 UI_GET_BUT_VALUE_INIT(but, *value)
1085 /* support for rna enum buts */
1086 if(but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
1087 if((int)*value & (int)but->hardmax) is_push= 1;
1090 if(*value == (double)but->hardmax) is_push= 1;
1103 else if(is_push==1) but->flag |= UI_SELECT;
1104 else but->flag &= ~UI_SELECT;
1107 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
1111 but= block->buttons.first;
1113 if(but->type==INLINK) {
1114 if(but->poin == poin) return but;
1121 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
1125 line= MEM_callocN(sizeof(uiLinkLine), "linkline");
1126 BLI_addtail(listb, line);
1131 uiBut *uiFindInlink(uiBlock *block, void *poin)
1133 return ui_find_inlink(block, poin);
1136 void uiComposeLinks(uiBlock *block)
1143 but= block->buttons.first;
1145 if(but->type==LINK) {
1148 /* for all pointers in the array */
1152 for(a=0; a < *(link->totlink); a++) {
1153 bt= ui_find_inlink(block, (*ppoin)[a] );
1155 ui_add_link_line(&link->lines, but, bt);
1159 else if(link->poin) {
1160 bt= ui_find_inlink(block, *(link->poin) );
1162 ui_add_link_line(&link->lines, but, bt);
1172 /* ************************************************ */
1174 void uiBlockSetButLock(uiBlock *block, int val, const char *lockstr)
1177 block->lock= val ? 1:0;
1178 block->lockstr= lockstr;
1182 void uiBlockClearButLock(uiBlock *block)
1185 block->lockstr= NULL;
1188 /* *************************************************************** */
1190 void ui_delete_linkline(uiLinkLine *line, uiBut *but)
1195 BLI_remlink(&but->link->lines, line);
1197 link= line->from->link;
1199 /* are there more pointers allowed? */
1202 if(*(link->totlink)==1) {
1203 *(link->totlink)= 0;
1204 MEM_freeN(*(link->ppoin));
1205 *(link->ppoin)= NULL;
1209 for(a=0; a< (*(link->totlink)); a++) {
1211 if( (*(link->ppoin))[a] != line->to->poin ) {
1212 (*(link->ppoin))[b]= (*(link->ppoin))[a];
1216 (*(link->totlink))--;
1220 *(link->poin)= NULL;
1227 /* *********************** data get/set ***********************
1228 * this either works with the pointed to data, or can work with
1229 * an edit override pointer while dragging for example */
1231 /* for buttons pointing to color for example */
1232 void ui_get_but_vectorf(uiBut *but, float vec[3])
1238 copy_v3_v3(vec, but->editvec);
1244 vec[0]= vec[1]= vec[2]= 0.0f;
1246 if(RNA_property_type(prop) == PROP_FLOAT) {
1247 tot= RNA_property_array_length(&but->rnapoin, prop);
1250 for(a=0; a<tot; a++)
1251 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1254 else if(but->pointype == CHA) {
1255 char *cp= (char *)but->poin;
1256 vec[0]= ((float)cp[0])/255.0f;
1257 vec[1]= ((float)cp[1])/255.0f;
1258 vec[2]= ((float)cp[2])/255.0f;
1260 else if(but->pointype == FLO) {
1261 float *fp= (float *)but->poin;
1262 copy_v3_v3(vec, fp);
1265 if (but->editvec==NULL) {
1266 fprintf(stderr, "ui_get_but_vectorf: can't get color, should never happen\n");
1267 vec[0]= vec[1]= vec[2]= 0.0f;
1271 if (but->type == BUT_NORMAL) {
1276 /* for buttons pointing to color for example */
1277 void ui_set_but_vectorf(uiBut *but, const float vec[3])
1282 copy_v3_v3(but->editvec, vec);
1288 if(RNA_property_type(prop) == PROP_FLOAT) {
1292 tot= RNA_property_array_length(&but->rnapoin, prop);
1295 for (a=0; a<tot; a++) {
1296 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1300 else if(but->pointype == CHA) {
1301 char *cp= (char *)but->poin;
1302 cp[0]= (char)(0.5f + vec[0]*255.0f);
1303 cp[1]= (char)(0.5f + vec[1]*255.0f);
1304 cp[2]= (char)(0.5f + vec[2]*255.0f);
1306 else if(but->pointype == FLO) {
1307 float *fp= (float *)but->poin;
1308 copy_v3_v3(fp, vec);
1312 int ui_is_but_float(uiBut *but)
1314 if(but->pointype==FLO && but->poin)
1317 if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1323 int ui_is_but_unit(uiBut *but)
1325 UnitSettings *unit= but->block->unit;
1326 const int unit_type= uiButGetUnitType(but);
1328 if(unit_type == PROP_UNIT_NONE)
1331 #if 1 // removed so angle buttons get correct snapping
1332 if (unit->system_rotation == USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION)
1336 /* for now disable time unit conversion */
1337 if (unit_type == PROP_UNIT_TIME)
1340 if (unit->system == USER_UNIT_NONE) {
1341 if (unit_type != PROP_UNIT_ROTATION) {
1349 int ui_is_but_rna_valid(uiBut *but)
1351 if (but->rnaprop==NULL || RNA_struct_contains_property(&but->rnapoin, but->rnaprop)) {
1355 printf("property removed %s: %p\n", but->drawstr, but->rnaprop);
1360 double ui_get_but_val(uiBut *but)
1365 if(but->editval) { return *(but->editval); }
1366 if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1371 switch(RNA_property_type(prop)) {
1373 if(RNA_property_array_check(prop))
1374 value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1376 value= RNA_property_boolean_get(&but->rnapoin, prop);
1379 if(RNA_property_array_check(prop))
1380 value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1382 value= RNA_property_int_get(&but->rnapoin, prop);
1385 if(RNA_property_array_check(prop))
1386 value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1388 value= RNA_property_float_get(&but->rnapoin, prop);
1391 value= RNA_property_enum_get(&but->rnapoin, prop);
1398 else if(but->type== HSVSLI) {
1401 fp= (but->editvec)? but->editvec: (float *)but->poin;
1402 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1404 switch(but->str[0]) {
1405 case 'H': value= h; break;
1406 case 'S': value= s; break;
1407 case 'V': value= v; break;
1410 else if( but->pointype == CHA ) {
1411 value= *(char *)but->poin;
1413 else if( but->pointype == SHO ) {
1414 value= *(short *)but->poin;
1416 else if( but->pointype == INT ) {
1417 value= *(int *)but->poin;
1419 else if( but->pointype == FLO ) {
1420 value= *(float *)but->poin;
1426 void ui_set_but_val(uiBut *but, double value)
1430 /* value is a hsv value: convert to rgb */
1434 if(RNA_property_editable(&but->rnapoin, prop)) {
1435 switch(RNA_property_type(prop)) {
1437 if(RNA_property_array_length(&but->rnapoin, prop))
1438 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1440 RNA_property_boolean_set(&but->rnapoin, prop, value);
1443 if(RNA_property_array_length(&but->rnapoin, prop))
1444 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, (int)value);
1446 RNA_property_int_set(&but->rnapoin, prop, (int)value);
1449 if(RNA_property_array_length(&but->rnapoin, prop))
1450 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1452 RNA_property_float_set(&but->rnapoin, prop, value);
1455 if(RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1456 int ivalue= (int)value;
1457 ivalue ^= RNA_property_enum_get(&but->rnapoin, prop); /* toggle for enum/flag buttons */
1458 RNA_property_enum_set(&but->rnapoin, prop, ivalue);
1461 RNA_property_enum_set(&but->rnapoin, prop, value);
1469 /* we can't be sure what RNA set functions actually do,
1470 * so leave this unset */
1471 value= UI_BUT_VALUE_UNSET;
1473 else if(but->pointype==0);
1474 else if(but->type==HSVSLI ) {
1477 fp= (but->editvec)? but->editvec: (float *)but->poin;
1478 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1480 switch(but->str[0]) {
1481 case 'H': h= value; break;
1482 case 'S': s= value; break;
1483 case 'V': v= value; break;
1486 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1490 /* first do rounding */
1491 if(but->pointype==CHA)
1492 value= (char)floor(value+0.5);
1493 else if(but->pointype==SHO ) {
1494 /* gcc 3.2.1 seems to have problems
1495 * casting a double like 32772.0 to
1496 * a short so we cast to an int, then
1499 gcckludge = (int) floor(value+0.5);
1500 value= (short)gcckludge;
1502 else if(but->pointype==INT )
1503 value= (int)floor(value+0.5);
1504 else if(but->pointype==FLO ) {
1505 float fval= (float)value;
1506 if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f; /* prevent negative zero */
1510 /* then set value with possible edit override */
1512 value= *but->editval= value;
1513 else if(but->pointype==CHA)
1514 value= *((char *)but->poin)= (char)value;
1515 else if(but->pointype==SHO)
1516 value= *((short *)but->poin)= (short)value;
1517 else if(but->pointype==INT)
1518 value= *((int *)but->poin)= (int)value;
1519 else if(but->pointype==FLO)
1520 value= *((float *)but->poin)= (float)value;
1523 /* update select flag */
1524 ui_is_but_sel(but, &value);
1527 int ui_get_but_string_max_length(uiBut *but)
1529 if(ELEM(but->type, TEX, SEARCH_MENU))
1530 return but->hardmax;
1531 else if(but->type == IDPOIN)
1532 return MAX_ID_NAME-2;
1534 return UI_MAX_DRAW_STR;
1537 static double ui_get_but_scale_unit(uiBut *but, double value)
1539 UnitSettings *unit= but->block->unit;
1540 int unit_type= uiButGetUnitType(but);
1542 if(unit_type == PROP_UNIT_LENGTH) {
1543 return value * (double)unit->scale_length;
1545 else if(unit_type == PROP_UNIT_AREA) {
1546 return value * pow(unit->scale_length, 2);
1548 else if(unit_type == PROP_UNIT_VOLUME) {
1549 return value * pow(unit->scale_length, 3);
1551 else if(unit_type == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
1552 Scene *scene= CTX_data_scene(but->block->evil_C);
1553 return FRA2TIME(value);
1560 /* str will be overwritten */
1561 void ui_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen)
1563 if(ui_is_but_unit(but)) {
1564 UnitSettings *unit= but->block->unit;
1565 int unit_type= uiButGetUnitType(but);
1568 orig_str= MEM_callocN(sizeof(char)*maxlen + 1, "textedit sub str");
1569 memcpy(orig_str, str, maxlen);
1571 bUnit_ToUnitAltName(str, maxlen, orig_str, unit->system, unit_type>>16);
1573 MEM_freeN(orig_str);
1577 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad)
1579 UnitSettings *unit= but->block->unit;
1580 int do_split= unit->flag & USER_UNIT_OPT_SPLIT;
1581 int unit_type= uiButGetUnitType(but);
1582 int precision= but->a2;
1584 if(unit->scale_length<0.0001f) unit->scale_length= 1.0f; // XXX do_versions
1587 if(precision > PRECISION_FLOAT_MAX) precision= PRECISION_FLOAT_MAX;
1588 else if(precision==0) precision= 2;
1590 bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision, unit->system, unit_type>>16, do_split, pad);
1593 static float ui_get_but_step_unit(uiBut *but, float step_default)
1595 int unit_type= uiButGetUnitType(but)>>16;
1598 step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, step_default), but->block->unit->system, unit_type);
1600 if(step > 0.0f) { /* -1 is an error value */
1601 return (float)((double)step/ui_get_but_scale_unit(but, 1.0))*100.0f;
1604 return step_default;
1609 void ui_get_but_string(uiBut *but, char *str, size_t maxlen)
1611 if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1616 type= RNA_property_type(but->rnaprop);
1618 if(type == PROP_STRING) {
1620 buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen, &buf_len);
1622 else if(type == PROP_POINTER) {
1624 PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1625 buf= RNA_struct_name_get_alloc(&ptr, str, maxlen, &buf_len);
1631 else if(buf && buf != str) {
1632 /* string was too long, we have to truncate */
1633 memcpy(str, buf, MIN2(maxlen, buf_len+1));
1637 else if(but->type == IDPOIN) {
1639 if(but->idpoin_idpp) { /* Can be NULL for ID properties by python */
1640 ID *id= *(but->idpoin_idpp);
1642 BLI_strncpy(str, id->name+2, maxlen);
1649 else if(but->type == TEX) {
1651 BLI_strncpy(str, but->poin, maxlen);
1654 else if(but->type == SEARCH_MENU) {
1656 BLI_strncpy(str, but->poin, maxlen);
1659 else if(ui_but_anim_expression_get(but, str, maxlen))
1660 ; /* driver expression */
1662 /* number editing */
1665 value= ui_get_but_val(but);
1667 if(ui_is_but_float(but)) {
1668 if(ui_is_but_unit(but)) {
1669 ui_get_but_string_unit(but, str, maxlen, value, 0);
1672 const int prec= ui_but_float_precision(but, value);
1673 BLI_snprintf(str, maxlen, "%.*f", prec, value);
1677 BLI_snprintf(str, maxlen, "%d", (int)value);
1683 static int ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *value)
1685 char str_unit_convert[256];
1686 const int unit_type= uiButGetUnitType(but);
1688 BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
1690 /* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
1691 bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr, ui_get_but_scale_unit(but, 1.0), but->block->unit->system, unit_type>>16);
1693 return (BPY_button_exec(C, str_unit_convert, value, TRUE) != -1);
1696 static int ui_set_but_string_eval_num(bContext *C, uiBut *but, const char *str, double *value)
1700 if(str[0] != '\0') {
1701 int is_unit_but= ui_is_but_unit(but);
1702 /* only enable verbose if we won't run again with units */
1703 if(BPY_button_exec(C, str, value, is_unit_but==FALSE) != -1) {
1704 /* if the value parsed ok without unit conversion this button may still need a unit multiplier */
1708 BLI_snprintf(str_new, sizeof(str_new), "%f", *value);
1709 ok= ui_set_but_string_eval_num_unit(C, but, str_new, value);
1712 ok= TRUE; /* parse normal string via py (no unit conversion needed) */
1715 else if(is_unit_but) {
1716 /* parse failed, this is a unit but so run replacements and parse again */
1717 ok= ui_set_but_string_eval_num_unit(C, but, str, value);
1724 #endif // WITH_PYTHON
1726 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1728 if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1729 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1732 type= RNA_property_type(but->rnaprop);
1734 if(type == PROP_STRING) {
1736 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1739 else if(type == PROP_POINTER) {
1741 PointerRNA ptr, rptr;
1744 if(str == NULL || str[0] == '\0') {
1745 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1749 ptr= but->rnasearchpoin;
1750 prop= but->rnasearchprop;
1752 if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1753 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1762 else if(but->type == IDPOIN) {
1764 but->idpoin_func(C, str, but->idpoin_idpp);
1767 else if(but->type == TEX) {
1769 if(ui_is_but_utf8(but)) BLI_strncpy_utf8(but->poin, str, but->hardmax);
1770 else BLI_strncpy(but->poin, str, but->hardmax);
1774 else if(but->type == SEARCH_MENU) {
1776 BLI_strncpy(but->poin, str, but->hardmax);
1779 else if(ui_but_anim_expression_set(but, str)) {
1780 /* driver expression */
1783 else if(str[0]=='#') {
1784 /* shortcut to create new driver expression (versus immediate Py-execution) */
1785 return ui_but_anim_expression_create(but, str+1);
1788 /* number editing */
1792 if(ui_set_but_string_eval_num(C, but, str, &value) == FALSE) {
1797 #endif // WITH_PYTHON
1799 if(!ui_is_but_float(but)) value= (int)floor(value + 0.5);
1800 if(but->type==NUMABS) value= fabs(value);
1802 /* not that we use hard limits here */
1803 if(value < (double)but->hardmin) value= but->hardmin;
1804 if(value > (double)but->hardmax) value= but->hardmax;
1806 ui_set_but_val(but, value);
1813 void ui_set_but_default(bContext *C, short all)
1817 WM_operator_properties_create(&ptr, "UI_OT_reset_default_button");
1818 RNA_boolean_set(&ptr, "all", all);
1819 WM_operator_name_call(C, "UI_OT_reset_default_button", WM_OP_EXEC_DEFAULT, &ptr);
1820 WM_operator_properties_free(&ptr);
1823 static double soft_range_round_up(double value, double max)
1825 /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1826 double newmax= pow(10.0, ceil(log(value)/M_LN10));
1828 if(newmax*0.2 >= max && newmax*0.2 >= value)
1830 else if(newmax*0.5 >= max && newmax*0.5 >= value)
1836 static double soft_range_round_down(double value, double max)
1838 /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1839 double newmax= pow(10.0, floor(log(value)/M_LN10));
1841 if(newmax*5.0 <= max && newmax*5.0 <= value)
1843 else if(newmax*2.0 <= max && newmax*2.0 <= value)
1849 void ui_set_but_soft_range(uiBut *but, double value)
1851 /* ideally we would not limit this but practially, its more then
1852 * enough worst case is very long vectors wont use a smart soft-range
1853 * which isnt so bad. */
1856 const PropertyType type= RNA_property_type(but->rnaprop);
1857 double softmin, softmax /*, step, precision*/;
1858 double value_min= value;
1859 double value_max= value;
1861 /* clamp button range to something reasonable in case
1862 * we get -inf/inf from RNA properties */
1863 if(type == PROP_INT) {
1864 int imin, imax, istep;
1865 const int array_len= RNA_property_array_length(&but->rnapoin, but->rnaprop);
1867 RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1868 softmin= (imin == INT_MIN)? -1e4: imin;
1869 softmax= (imin == INT_MAX)? 1e4: imax;
1870 /*step= istep;*/ /*UNUSED*/
1871 /*precision= 1;*/ /*UNUSED*/
1873 if(array_len >= 2) {
1875 RNA_property_int_get_array_range(&but->rnapoin, but->rnaprop, value_range);
1876 value_min= (double)value_range[0];
1877 value_max= (double)value_range[1];
1880 else if(type == PROP_FLOAT) {
1881 float fmin, fmax, fstep, fprecision;
1882 const int array_len= RNA_property_array_length(&but->rnapoin, but->rnaprop);
1884 RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1885 softmin= (fmin == -FLT_MAX)? (float)-1e4: fmin;
1886 softmax= (fmax == FLT_MAX)? (float)1e4: fmax;
1887 /*step= fstep;*/ /*UNUSED*/
1888 /*precision= fprecision;*/ /*UNUSED*/
1890 if(array_len >= 2) {
1891 float value_range[2];
1892 RNA_property_float_get_array_range(&but->rnapoin, but->rnaprop, value_range);
1893 value_min= (double)value_range[0];
1894 value_max= (double)value_range[1];
1900 /* if the value goes out of the soft/max range, adapt the range */
1901 if(value_min+1e-10 < softmin) {
1903 softmin= -soft_range_round_up(-value_min, -softmin);
1905 softmin= soft_range_round_down(value_min, softmin);
1907 if(softmin < (double)but->hardmin)
1908 softmin= (double)but->hardmin;
1910 if(value_max-1e-10 > softmax) {
1912 softmax= -soft_range_round_down(-value_max, -softmax);
1914 softmax= soft_range_round_up(value_max, softmax);
1916 if(softmax > (double)but->hardmax)
1917 softmax= but->hardmax;
1920 but->softmin= softmin;
1921 but->softmax= softmax;
1925 /* ******************* Free ********************/
1927 static void ui_free_link(uiLink *link)
1930 BLI_freelistN(&link->lines);
1935 /* can be called with C==NULL */
1936 static void ui_free_but(const bContext *C, uiBut *but)
1939 WM_operator_properties_free(but->opptr);
1940 MEM_freeN(but->opptr);
1942 if(but->func_argN) MEM_freeN(but->func_argN);
1944 /* XXX solve later, buttons should be free-able without context ideally,
1945 * however they may have open tooltips or popup windows, which need to
1946 * be closed using a context pointer */
1948 ui_button_active_free(C, but);
1951 MEM_freeN(but->active);
1953 if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1954 ui_free_link(but->link);
1956 if((but->type == BUT_IMAGE) && but->poin) IMB_freeImBuf((struct ImBuf *)but->poin);
1961 /* can be called with C==NULL */
1962 void uiFreeBlock(const bContext *C, uiBlock *block)
1966 while( (but= block->buttons.first) ) {
1967 BLI_remlink(&block->buttons, but);
1968 ui_free_but(C, but);
1972 MEM_freeN(block->unit);
1974 if(block->func_argN)
1975 MEM_freeN(block->func_argN);
1977 CTX_store_free_list(&block->contexts);
1979 BLI_freelistN(&block->saferct);
1984 /* can be called with C==NULL */
1985 void uiFreeBlocks(const bContext *C, ListBase *lb)
1989 while( (block= lb->first) ) {
1990 BLI_remlink(lb, block);
1991 uiFreeBlock(C, block);
1995 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1997 uiBlock *block, *nextblock;
1999 for(block=lb->first; block; block=nextblock) {
2000 nextblock= block->next;
2002 if(!block->handle) {
2003 if(!block->active) {
2004 BLI_remlink(lb, block);
2005 uiFreeBlock(C, block);
2013 void uiBlockSetRegion(uiBlock *block, ARegion *region)
2015 ListBase *lb= ®ion->uiblocks;
2016 uiBlock *oldblock= NULL;
2018 /* each listbase only has one block with this name, free block
2019 * if is already there so it can be rebuilt from scratch */
2021 oldblock= BLI_findstring(lb, block->name, offsetof(uiBlock, name));
2024 oldblock->active= 0;
2025 oldblock->panel= NULL;
2028 /* at the beginning of the list! for dynamical menus/blocks */
2029 BLI_addhead(lb, block);
2032 block->oldblock= oldblock;
2035 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
2040 int getsizex, getsizey;
2042 window= CTX_wm_window(C);
2043 scn = CTX_data_scene(C);
2045 block= MEM_callocN(sizeof(uiBlock), "uiBlock");
2048 block->evil_C= (void*)C; // XXX
2051 block->color_profile= (scn->r.color_mgt_flag & R_COLOR_MANAGEMENT);
2053 /* copy to avoid crash when scene gets deleted with ui still open */
2054 block->unit= MEM_mallocN(sizeof(scn->unit), "UI UnitSettings");
2055 memcpy(block->unit, &scn->unit, sizeof(scn->unit));
2058 BLI_strncpy(block->name, name, sizeof(block->name));
2061 uiBlockSetRegion(block, region);
2063 /* window matrix and aspect */
2064 if(region && region->swinid) {
2065 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
2066 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
2068 /* TODO - investigate why block->winmat[0][0] is negative
2069 * in the image view when viewRedrawForce is called */
2070 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
2073 /* no subwindow created yet, for menus for example, so we
2074 * use the main window instead, since buttons are created
2076 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
2077 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
2079 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
2080 block->auto_open= TRUE;
2081 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
2087 uiBlock *uiGetBlock(const char *name, ARegion *ar)
2089 return BLI_findstring(&ar->uiblocks, name, offsetof(uiBlock, name));
2092 void uiBlockSetEmboss(uiBlock *block, char dt)
2097 void ui_check_but(uiBut *but)
2099 /* if something changed in the button */
2100 double value= UI_BUT_VALUE_UNSET;
2101 // float okwidth; // UNUSED
2103 ui_is_but_sel(but, &value);
2105 /* only update soft range while not editing */
2106 if(but->rnaprop && !(but->editval || but->editstr || but->editvec)) {
2107 UI_GET_BUT_VALUE_INIT(but, value)
2108 ui_set_but_soft_range(but, value);
2111 /* test for min and max, icon sliders, etc */
2112 switch( but->type ) {
2118 UI_GET_BUT_VALUE_INIT(but, value)
2119 if(value < (double)but->hardmin) ui_set_but_val(but, but->hardmin);
2120 else if(value > (double)but->hardmax) ui_set_but_val(but, but->hardmax);
2126 UI_GET_BUT_VALUE_INIT(but, value)
2127 value_abs= fabs(value);
2128 if(value_abs < (double)but->hardmin) ui_set_but_val(but, but->hardmin);
2129 else if(value_abs > (double)but->hardmax) ui_set_but_val(but, but->hardmax);
2134 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2135 if(but->flag & UI_SELECT) but->iconadd= 1;
2136 else but->iconadd= 0;
2141 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2142 UI_GET_BUT_VALUE_INIT(but, value)
2143 but->iconadd= (int)value- (int)(but->hardmin);
2148 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2149 UI_GET_BUT_VALUE_INIT(but, value)
2150 but->iconadd= (int)value- (int)(but->hardmin);
2156 /* safety is 4 to enable small number buttons (like 'users') */
2157 // okwidth= -4 + (but->x2 - but->x1); // UNUSED
2160 switch( but->type ) {
2165 if(but->x2 - but->x1 > 24) {
2166 UI_GET_BUT_VALUE_INIT(but, value)
2167 ui_set_name_menu(but, (int)value);
2176 UI_GET_BUT_VALUE_INIT(but, value)
2178 if(ui_is_but_float(but)) {
2179 if(value == (double) FLT_MAX) BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%sinf", but->str);
2180 else if(value == (double) -FLT_MAX) BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s-inf", but->str);
2181 /* support length type buttons */
2182 else if(ui_is_but_unit(but)) {
2183 char new_str[sizeof(but->drawstr)];
2184 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE);
2185 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, new_str);
2188 const int prec= ui_but_float_precision(but, value);
2189 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2193 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%d", but->str, (int)value);
2197 PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2199 if (pstype == PROP_PERCENTAGE)
2200 strcat(but->drawstr, "%");
2205 if(ui_is_but_float(but)) {
2207 UI_GET_BUT_VALUE_INIT(but, value)
2208 prec= ui_but_float_precision(but, value);
2209 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2212 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2221 char str[UI_MAX_DRAW_STR];
2223 ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
2225 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, str);
2230 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2231 if (but->flag & UI_SELECT) {
2232 strcat(but->drawstr, "Press a key");
2235 UI_GET_BUT_VALUE_INIT(but, value)
2236 strcat(but->drawstr, WM_key_event_string((short)value));
2241 if (but->flag & UI_SELECT) {
2242 but->drawstr[0]= '\0';
2244 if(but->modifier_key) {
2245 char *str= but->drawstr;
2247 if(but->modifier_key & KM_SHIFT)
2248 str= strcat(str, "Shift ");
2249 if(but->modifier_key & KM_CTRL)
2250 str= strcat(str, "Ctrl ");
2251 if(but->modifier_key & KM_ALT)
2252 str= strcat(str, "Alt ");
2253 if(but->modifier_key & KM_OSKEY)
2254 str= strcat(str, "Cmd ");
2256 (void)str; /* UNUSED */
2259 strcat(but->drawstr, "Press a key ");
2262 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2267 /* trying to get the dual-icon to left of text... not very nice */
2269 BLI_strncpy(but->drawstr, " ", UI_MAX_DRAW_STR);
2270 BLI_strncpy(but->drawstr+2, but->str, UI_MAX_DRAW_STR-2);
2278 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2282 /* if we are doing text editing, this will override the drawstr */
2284 BLI_strncpy(but->drawstr, but->editstr, UI_MAX_DRAW_STR);
2286 /* text clipping moved to widget drawing code itself */
2290 void uiBlockBeginAlign(uiBlock *block)
2292 /* if other align was active, end it */
2293 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2295 block->flag |= UI_BUT_ALIGN_DOWN;
2298 /* buttons declared after this call will get this align nr */ // XXX flag?
2301 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2305 dx= fabs( but1->x2 - but2->x1);
2306 dy= fabs( but1->y1 - but2->y2);
2308 if(dx > dy) return 0;
2312 void uiBlockEndAlign(uiBlock *block)
2314 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
2317 int ui_but_can_align(uiBut *but)
2319 return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2322 static void ui_block_do_align_but(uiBut *first, short nr)
2324 uiBut *prev, *but=NULL, *next;
2325 int flag= 0, cols=0, rows=0;
2329 for(but=first; but && but->alignnr == nr; but=but->next) {
2330 if(but->next && but->next->alignnr == nr) {
2331 if(buts_are_horiz(but, but->next)) cols++;
2336 /* rows==0: 1 row, cols==0: 1 collumn */
2338 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2339 for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2341 if(next && next->alignnr != nr)
2344 /* clear old flag */
2345 but->flag &= ~UI_BUT_ALIGN;
2347 if(flag==0) { /* first case */
2349 if(buts_are_horiz(but, next)) {
2351 flag= UI_BUT_ALIGN_RIGHT;
2353 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2356 flag= UI_BUT_ALIGN_DOWN;
2360 else if(next==NULL) { /* last case */
2362 if(buts_are_horiz(prev, but)) {
2364 flag= UI_BUT_ALIGN_LEFT;
2366 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2368 else flag= UI_BUT_ALIGN_TOP;
2371 else if(buts_are_horiz(but, next)) {
2372 /* check if this is already second row */
2373 if( prev && buts_are_horiz(prev, but)==0) {
2374 flag &= ~UI_BUT_ALIGN_LEFT;
2375 flag |= UI_BUT_ALIGN_TOP;
2376 /* exception case: bottom row */
2379 while(bt && bt->alignnr == nr) {
2380 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break;
2383 if(bt==NULL || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2386 else flag |= UI_BUT_ALIGN_LEFT;
2390 flag |= UI_BUT_ALIGN_TOP;
2392 else { /* next button switches to new row */
2394 if(prev && buts_are_horiz(prev, but))
2395 flag |= UI_BUT_ALIGN_LEFT;
2397 flag &= ~UI_BUT_ALIGN_LEFT;
2398 flag |= UI_BUT_ALIGN_TOP;
2401 if( (flag & UI_BUT_ALIGN_TOP)==0) { /* stil top row */
2403 if(next && buts_are_horiz(but, next))
2404 flag = UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT;
2406 /* last button in top row */
2407 flag = UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2411 flag |= UI_BUT_ALIGN_DOWN;
2414 flag |= UI_BUT_ALIGN_TOP;
2420 /* merge coordinates */
2424 but->x1= (prev->x2+but->x1)/2.0f;
2428 but->y2= (prev->y1+but->y2)/2.0f;
2432 if(buts_are_horiz(prev, but)) {
2433 but->x1= (prev->x2+but->x1)/2.0f;
2435 /* copy height too */
2438 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2439 /* the previous button is a single one in its row */
2440 but->y2= (prev->y1+but->y2)/2.0f;
2444 if(next && buts_are_horiz(but, next)==0)
2448 /* the previous button is not a single one in its row */
2456 void ui_block_do_align(uiBlock *block)
2461 /* align buttons with same align nr */
2462 for(but=block->buttons.first; but;) {
2465 ui_block_do_align_but(but, nr);
2467 /* skip with same number */
2468 for(; but && but->alignnr == nr; but=but->next);
2479 * ui_def_but is the function that draws many button types
2481 * for float buttons:
2482 * "a1" Click Step (how much to change the value each click)
2483 * "a2" Number of decimal point values to display. 0 defaults to 3 (0.000)
2484 * 1,2,3, and a maximum of 4, all greater values will be clamped to 4.
2486 static uiBut *ui_def_but(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
2491 if(type & BUTPOIN) { /* a pointer is required */
2496 but= MEM_callocN(sizeof(uiBut), "uiBut");
2498 but->type= type & BUTTYPE;
2499 but->pointype= type & BUTPOIN;
2500 but->bit= type & BIT;
2501 but->bitnr= type & 31;
2502 but->icon = ICON_NONE;
2505 but->retval= retval;
2508 if(slen >= UI_MAX_NAME_STR-1) {
2509 but->str= MEM_mallocN(slen+2, "ui_def_but str"); /* why +2 ? */
2512 but->str= but->strdata;
2514 memcpy(but->str, str, slen+1);
2522 but->hardmin= but->softmin= min;
2523 but->hardmax= but->softmax= max;
2528 but->lock= block->lock;
2529 but->lockstr= block->lockstr;
2532 but->aspect= 1.0f; //XXX block->aspect;
2533 but->block= block; // pointer back, used for frontbuffer status, and picker
2535 if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2536 but->alignnr= block->alignnr;
2538 but->func= block->func;
2539 but->func_arg1= block->func_arg1;
2540 but->func_arg2= block->func_arg2;
2542 but->funcN= block->funcN;
2543 if(block->func_argN)
2544 but->func_argN= MEM_dupallocN(block->func_argN);
2546 but->pos= -1; /* cursor invisible */
2548 if(ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) { /* add a space to name */
2549 /* slen remains unchanged from previous assignment, ensure this stays true */
2550 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2551 if(but->str[slen-1]!=' ') {
2552 but->str[slen]= ' ';
2553 but->str[slen+1]= 0;
2558 if((block->flag & UI_BLOCK_LOOP) || ELEM8(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU, PROGRESSBAR))
2559 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2560 else if(but->type==BUT_TOGDUAL)
2561 but->flag |= UI_ICON_LEFT;
2563 but->flag |= (block->flag & UI_BUT_ALIGN);
2567 but->flag |= UI_BUT_DISABLED;
2571 /* keep track of UI_interface.h */
2572 if(ELEM7(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, BUTM));
2573 else if(ELEM3(but->type, SCROLL, SEPR, FTPREVIEW));
2574 else if(but->type >= SEARCH_MENU);
2575 else but->flag |= UI_BUT_UNDO;
2577 BLI_addtail(&block->buttons, but);
2579 if(block->curlayout)
2580 ui_layout_add_but(block->curlayout, but);
2583 /* if the 'UI_OT_editsource' is running, extract the source info from the button */
2584 if (UI_editsource_enable_check()) {
2585 UI_editsource_active_but_test(but);
2592 /* ui_def_but_rna_propname and ui_def_but_rna
2593 * both take the same args except for propname vs prop, this is done so we can
2594 * avoid an extra lookup on 'prop' when its already available.
2596 * When this kind of change won't disrupt branches, best look into making more
2597 * of our UI functions take prop rather then propname.
2600 #define UI_DEF_BUT_RNA_DISABLE(but) \
2601 but->flag |= UI_BUT_DISABLED; \
2606 static uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, float a1, float a2, const char *tip)
2608 const PropertyType proptype= RNA_property_type(prop);
2610 int freestr= 0, icon= 0;
2612 /* use rna values if parameters are not specified */
2614 if(type == MENU && proptype == PROP_ENUM) {
2615 EnumPropertyItem *item;
2617 int i, totitem, value, free;
2619 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item, &totitem, &free);
2620 value= RNA_property_enum_get(ptr, prop);
2622 dynstr= BLI_dynstr_new();
2623 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2624 for(i=0; i<totitem; i++) {
2625 if(!item[i].identifier[0]) {
2627 BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2629 BLI_dynstr_append(dynstr, "|%l");
2631 else if(item[i].icon)
2632 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2634 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2636 if(value == item[i].value)
2639 str= BLI_dynstr_get_cstring(dynstr);
2640 BLI_dynstr_free(dynstr);
2647 else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2648 EnumPropertyItem *item;
2649 int i, totitem, free;
2651 RNA_property_enum_items_gettexted(block->evil_C, ptr, prop, &item, &totitem, &free);
2652 for(i=0; i<totitem; i++) {
2653 if(item[i].identifier[0] && item[i].value == (int)max) {
2660 str= RNA_property_ui_name(prop);
2665 str= RNA_property_ui_name(prop);
2666 icon= RNA_property_ui_icon(prop);
2670 if(!tip && proptype != PROP_ENUM)
2671 tip= RNA_property_ui_description(prop);
2673 if(min == max || a1 == -1 || a2 == -1) {
2674 if(proptype == PROP_INT) {
2675 int hardmin, hardmax, softmin, softmax, step;
2677 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2678 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2680 if(!ELEM(type, ROW, LISTROW) && min == max) {
2689 else if(proptype == PROP_FLOAT) {
2690 float hardmin, hardmax, softmin, softmax, step, precision;
2692 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2693 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2695 if(!ELEM(type, ROW, LISTROW) && min == max) {
2704 else if(proptype == PROP_STRING) {
2706 max= RNA_property_string_maxlength(prop);
2707 if(max == 0) /* interface code should ideally support unlimited length */
2708 max= UI_MAX_DRAW_STR;
2712 /* now create button */
2713 but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2718 if(RNA_property_array_length(&but->rnapoin, but->rnaprop))
2719 but->rnaindex= index;
2724 but->icon= (BIFIconID)icon;
2725 but->flag |= UI_HAS_ICON;
2726 but->flag|= UI_ICON_LEFT;
2729 if (!RNA_property_editable(&but->rnapoin, prop)) {
2730 UI_DEF_BUT_RNA_DISABLE(but);
2733 if (but->flag & UI_BUT_UNDO && (ui_but_is_rna_undo(but) == FALSE)) {
2734 but->flag &= ~UI_BUT_UNDO;
2737 /* If this button uses units, calculate the step from this */
2738 if((proptype == PROP_FLOAT) && ui_is_but_unit(but)) {
2739 but->a1= ui_get_but_step_unit(but, but->a1);
2743 MEM_freeN((void *)str);
2748 static uiBut *ui_def_but_rna_propname(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, const char *tip)
2750 PropertyRNA *prop= RNA_struct_find_property(ptr, propname);
2754 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, prop, index, min, max, a1, a2, tip);
2757 but= ui_def_but(block, type, retval, propname, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2759 UI_DEF_BUT_RNA_DISABLE(but);
2765 static uiBut *ui_def_but_operator(uiBlock *block, int type, const char *opname, int opcontext, const char *str, int x1, int y1, short x2, short y2, const char *tip)
2770 ot= WM_operatortype_find(opname, 0);
2773 if(ot) str= ot->name;
2777 if ((!tip || tip[0]=='\0') && ot && ot->description) {
2778 tip= ot->description;
2783 but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2785 but->opcontext= opcontext;
2786 but->flag &= ~UI_BUT_UNDO; /* no need for ui_but_is_undo(), we never need undo here */
2789 but->flag |= UI_BUT_DISABLED;
2797 static uiBut *ui_def_but_operator_text(uiBlock *block, int type, const char *opname, int opcontext, const char *str, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
2802 ot= WM_operatortype_find(opname, 0);
2805 if(ot) str= ot->name;
2809 if ((!tip || tip[0]=='\0') && ot && ot->description) {
2810 tip= ot->description;
2813 but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2815 but->opcontext= opcontext;
2816 but->flag &= ~UI_BUT_UNDO; /* no need for ui_but_is_undo(), we never need undo here */
2819 but->flag |= UI_BUT_DISABLED;
2827 uiBut *uiDefBut(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
2829 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2836 /* if _x_ is a power of two (only one bit) return the power,
2837 * otherwise return -1.
2838 * (1<<findBitIndex(x))==x for powers of two.
2840 static int findBitIndex(unsigned int x)
2842 if (!x || !is_power_of_2_i(x)) { /* is_power_of_2_i(x) strips lowest bit */
2847 if (x&0xFFFF0000) idx+=16, x>>=16;
2848 if (x&0xFF00) idx+=8, x>>=8;
2849 if (x&0xF0) idx+=4, x>>=4;
2850 if (x&0xC) idx+=2, x>>=2;
2857 /* autocomplete helper functions */
2858 struct AutoComplete {
2861 const char *startname;
2864 AutoComplete *autocomplete_begin(const char *startname, size_t maxlen)
2866 AutoComplete *autocpl;
2868 autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2869 autocpl->maxlen= maxlen;
2870 autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2871 autocpl->startname= startname;
2876 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2878 char *truncate= autocpl->truncate;
2879 const char *startname= autocpl->startname;
2882 for(a=0; a<autocpl->maxlen-1; a++) {
2883 if(startname[a]==0 || startname[a]!=name[a])
2887 if(startname[a]==0) {
2890 BLI_strncpy(truncate, name, autocpl->maxlen);
2892 /* remove from truncate what is not in bone->name */
2893 for(a=0; a<autocpl->maxlen-1; a++) {
2898 else if(truncate[a]!=name[a])
2905 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2907 if(autocpl->truncate[0])
2908 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2910 if (autoname != autocpl->startname) /* dont copy a string over its self */
2911 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2913 MEM_freeN(autocpl->truncate);
2917 /* autocomplete callback for ID buttons */
2918 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2920 int blocktype= (intptr_t)arg_v;
2921 ListBase *listb= which_libbase(CTX_data_main(C), blocktype);
2923 if(listb==NULL) return;
2925 /* search if str matches the beginning of an ID struct */
2927 AutoComplete *autocpl= autocomplete_begin(str, MAX_ID_NAME-2);
2930 for(id= listb->first; id; id= id->next)
2931 autocomplete_do_name(autocpl, id->name+2);
2933 autocomplete_end(autocpl, str);
2937 static void ui_check_but_and_iconize(uiBut *but, int icon)
2940 but->icon= (BIFIconID) icon;
2941 but->flag|= UI_HAS_ICON;
2947 static uiBut *uiDefButBit(uiBlock *block, int type, int bit, int retval, const char *str, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
2949 int bitIdx= findBitIndex(bit);
2953 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2956 uiBut *uiDefButF(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, const char *tip)
2958 return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2960 uiBut *uiDefButBitF(uiBlock *block, int type, int bit, int retval, const char *str, int x1, int y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, const char *tip)
2962 return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2964 uiBut *uiDefButI(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, const char *tip)
2966 return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2968 uiBut *uiDefButBitI(uiBlock *block, int type, int bit, int retval, const char *str, int x1, int y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, const char *tip)
2970 return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2972 uiBut *uiDefButS(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, const char *tip)
2974 return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2976 uiBut *uiDefButBitS(uiBlock *block, int type, int bit, int retval, const char *str, int x1, int y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, const char *tip)
2978 return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2980 uiBut *uiDefButC(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, const char *tip)
2982 return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2984 uiBut *uiDefButBitC(uiBlock *block, int type, int bit, int retval, const char *str, int x1, int y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, const char *tip)
2986 return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2988 uiBut *uiDefButR(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, const char *tip)
2991 but= ui_def_but_rna_propname(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2995 uiBut *uiDefButR_prop(uiBlock *block, int type, int retval, const char *str, int x1, int y1, short x2, short y2, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, float a1, float a2, const char *tip)
2998 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, prop, index, min, max, a1, a2, tip);
3002 uiBut *uiDefButO(uiBlock *block, int type, const char *opname, int opcontext, const char *str, int x1, int y1, short x2, short y2, const char *tip)
3005 but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
3010 uiBut *uiDefButTextO(uiBlock *block, int type, const char *opname, int opcontext, const char *str, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
3012 uiBut *but= ui_def_but_operator_text(block, type, opname, opcontext, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
3017 /* if a1==1.0 then a2 is an extra icon blending factor (alpha 0.0 - 1.0) */
3018 uiBut *uiDefIconBut(uiBlock *block, int type, int retval, int icon, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
3020 uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
3021 ui_check_but_and_iconize(but, icon);
3024 static uiBut *uiDefIconButBit(uiBlock *block, int type, int bit, int retval, int icon, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
3026 int bitIdx= findBitIndex(bit);
3030 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
3034 uiBut *uiDefIconButF(uiBlock *block, int type, int retval, int icon, int x1, int y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, const char *tip)
3036 return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3038 uiBut *uiDefIconButBitF(uiBlock *block, int type, int bit, int retval, int icon, int x1, int y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, const char *tip)
3040 return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3042 uiBut *uiDefIconButI(uiBlock *block, int type, int retval, int icon, int x1, int y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, const char *tip)
3044 return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3046 uiBut *uiDefIconButBitI(uiBlock *block, int type, int bit, int retval, int icon, int x1, int y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, const char *tip)
3048 return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3050 uiBut *uiDefIconButS(uiBlock *block, int type, int retval, int icon, int x1, int y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, const char *tip)
3052 return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3054 uiBut *uiDefIconButBitS(uiBlock *block, int type, int bit, int retval, int icon, int x1, int y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, const char *tip)
3056 return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3058 uiBut *uiDefIconButC(uiBlock *block, int type, int retval, int icon, int x1, int y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, const char *tip)
3060 return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3062 uiBut *uiDefIconButBitC(uiBlock *block, int type, int bit, int retval, int icon, int x1, int y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, const char *tip)
3064 return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3066 uiBut *uiDefIconButR(uiBlock *block, int type, int retval, int icon, int x1, int y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, const char *tip)
3069 but= ui_def_but_rna_propname(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
3070 ui_check_but_and_iconize(but, icon);
3073 uiBut *uiDefIconButR_prop(uiBlock *block, int type, int retval, int icon, int x1, int y1, short x2, short y2, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, float a1, float a2, const char *tip)
3076 but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, prop, index, min, max, a1, a2, tip);
3077 ui_check_but_and_iconize(but, icon);
3080 uiBut *uiDefIconButO(uiBlock *block, int type, const char *opname, int opcontext, int icon, int x1, int y1, short x2, short y2, const char *tip)
3083 but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
3084 ui_check_but_and_iconize(but, icon);
3088 /* Button containing both string label and icon */
3089 uiBut *uiDefIconTextBut(uiBlock *block, int type, int retval, int icon, const char *str, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
3091 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
3092 ui_check_but_and_iconize(but, icon);
3093 but->flag|= UI_ICON_LEFT;
3096 static uiBut *uiDefIconTextButBit(uiBlock *block, int type, int bit, int retval, int icon, const char *str, int x1, int y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, const char *tip)
3098 int bitIdx= findBitIndex(bit);
3102 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
3106 uiBut *uiDefIconTextButF(uiBlock *block, int type, int retval, int icon, const char *str, int x1, int y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, const char *tip)
3108 return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3110 uiBut *uiDefIconTextButBitF(uiBlock *block, int type, int bit, int retval, int icon, const char *str, int x1, int y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, const char *tip)
3112 return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3114 uiBut *uiDefIconTextButI(uiBlock *block, int type, int retval, int icon, const char *str, int x1, int y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, const char *tip)
3116 return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3118 uiBut *uiDefIconTextButBitI(uiBlock *block, int type, int bit, int retval, int icon, const char *str, int x1, int y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, const char *tip)
3120 return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
3122 uiBut *uiDefIconTextButS(uiBlock *block, int type, int retval, int&nb