4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation 2002-2008, full recode.
25 * ***** END GPL LICENSE BLOCK *****
32 #include "MEM_guardedalloc.h"
35 #include "DNA_listBase.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_texture_types.h"
39 #include "DNA_userdef_types.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_dynstr.h"
45 #include "BKE_context.h"
46 #include "BKE_idprop.h"
47 #include "BKE_library.h"
48 #include "BKE_screen.h"
49 #include "BKE_texture.h"
50 #include "BKE_utildefines.h"
54 #include "BIF_glutil.h"
58 #include "UI_interface.h"
60 #include "ED_screen.h"
64 #include "wm_subwindow.h"
65 #include "wm_window.h"
67 #include "RNA_access.h"
68 #include "RNA_types.h"
70 #include "BPY_extern.h"
72 #include "interface_intern.h"
74 #define MENU_WIDTH 120
75 #define MENU_ITEM_HEIGHT 20
76 #define MENU_SEP_HEIGHT 6
79 * a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt
81 * uiBlahBlah() external function
82 * ui_blah_blah() internal function
85 static void ui_free_but(const bContext *C, uiBut *but);
87 /* ************* translation ************** */
89 int ui_translate_buttons()
91 return (U.transopts & USER_TR_BUTTONS);
94 int ui_translate_menus()
96 return (U.transopts & USER_TR_MENUS);
99 int ui_translate_tooltips()
101 return (U.transopts & USER_TR_TOOLTIPS);
104 /* ************* window matrix ************** */
106 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
109 int sx, sy, getsizex, getsizey;
111 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
112 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
120 gx += block->panel->ofsx;
121 gy += block->panel->ofsy;
124 *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
125 *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
128 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
135 ui_block_to_window_fl(ar, block, &fx, &fy);
141 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
146 ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
147 ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
149 winr->xmin= tmpr.xmin;
150 winr->ymin= tmpr.ymin;
151 winr->xmax= tmpr.xmax;
152 winr->ymax= tmpr.ymax;
155 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y) /* for mouse cursor */
157 float a, b, c, d, e, f, px, py;
158 int sx, sy, getsizex, getsizey;
160 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
161 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
165 a= .5*((float)getsizex)*block->winmat[0][0];
166 b= .5*((float)getsizex)*block->winmat[1][0];
167 c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
169 d= .5*((float)getsizey)*block->winmat[0][1];
170 e= .5*((float)getsizey)*block->winmat[1][1];
171 f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
176 *y= (a*(py-f) + d*(c-px))/(a*e-d*b);
177 *x= (px- b*(*y)- c)/a;
180 *x -= block->panel->ofsx;
181 *y -= block->panel->ofsy;
185 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
192 ui_window_to_block_fl(ar, block, &fx, &fy);
198 void ui_window_to_region(const ARegion *ar, int *x, int *y)
200 *x-= ar->winrct.xmin;
201 *y-= ar->winrct.ymin;
204 /* ******************* block calc ************************* */
206 void ui_block_translate(uiBlock *block, int x, int y)
210 for(bt= block->buttons.first; bt; bt=bt->next) {
223 static void ui_text_bounds_block(uiBlock *block, float offset)
225 uiStyle *style= U.uistyles.first; // XXX pass on as arg
227 int i = 0, j, x1addval= offset, nextcol;
228 int lastcol= 0, col= 0;
230 uiStyleFontSet(&style->widget);
232 for(bt= block->buttons.first; bt; bt= bt->next) {
234 //int transopts= ui_translate_buttons();
235 //if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
237 j= BLF_width(bt->drawstr);
242 if(bt->next && bt->x1 < bt->next->x1)
246 /* cope with multi collumns */
247 bt= block->buttons.first;
249 if(bt->next && bt->x1 < bt->next->x1) {
256 bt->x2 = bt->x1 + i + block->bounds;
259 bt->x2= MAX2(bt->x2, offset + block->minbounds);
261 ui_check_but(bt); // clips text again
264 x1addval+= i + block->bounds;
270 void ui_bounds_block(uiBlock *block)
275 if(block->buttons.first==NULL) {
277 block->minx= 0.0; block->maxx= block->panel->sizex;
278 block->miny= 0.0; block->maxy= block->panel->sizey;
283 block->minx= block->miny= 10000;
284 block->maxx= block->maxy= -10000;
286 bt= block->buttons.first;
288 if(bt->x1 < block->minx) block->minx= bt->x1;
289 if(bt->y1 < block->miny) block->miny= bt->y1;
291 if(bt->x2 > block->maxx) block->maxx= bt->x2;
292 if(bt->y2 > block->maxy) block->maxy= bt->y2;
297 block->minx -= block->bounds;
298 block->miny -= block->bounds;
299 block->maxx += block->bounds;
300 block->maxy += block->bounds;
303 block->maxx= block->minx + MAX2(block->maxx - block->minx, block->minbounds);
305 /* hardcoded exception... but that one is annoying with larger safety */
306 bt= block->buttons.first;
307 if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
310 block->safety.xmin= block->minx-xof;
311 block->safety.ymin= block->miny-xof;
312 block->safety.xmax= block->maxx+xof;
313 block->safety.ymax= block->maxy+xof;
316 static void ui_popup_bounds_block(const bContext *C, uiBlock *block, int menu)
318 wmWindow *window= CTX_wm_window(C);
319 int startx, starty, endx, endy, width, height;
320 int oldbounds, mx, my, xmax, ymax;
322 oldbounds= block->bounds;
324 /* compute mouse position with user defined offset */
325 ui_bounds_block(block);
326 mx= window->eventstate->x + block->minx + block->mx;
327 my= window->eventstate->y + block->miny + block->my;
329 wm_window_get_size(window, &xmax, &ymax);
331 /* first we ensure wide enough text bounds */
333 if(block->flag & UI_BLOCK_LOOP) {
335 ui_text_bounds_block(block, block->minx);
339 /* next we recompute bounds */
340 block->bounds= oldbounds;
341 ui_bounds_block(block);
343 /* and we adjust the position to fit within window */
344 width= block->maxx - block->minx;
345 height= block->maxy - block->miny;
347 startx= mx-(0.8*(width));
367 ui_block_translate(block, startx - block->minx, starty - block->miny);
369 /* now recompute bounds and safety */
370 ui_bounds_block(block);
373 /* used for various cases */
374 void uiBoundsBlock(uiBlock *block, int addval)
379 block->bounds= addval;
383 /* used for pulldowns */
384 void uiTextBoundsBlock(uiBlock *block, int addval)
386 block->bounds= addval;
390 /* used for block popups */
391 void uiPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
393 block->bounds= addval;
399 /* used for menu popups */
400 void uiMenuPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
402 block->bounds= addval;
408 /* ************** LINK LINE DRAWING ************* */
410 /* link line drawing is not part of buttons or theme.. so we stick with it here */
412 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
416 if(line->from==NULL || line->to==NULL) return;
418 rect.xmin= (line->from->x1+line->from->x2)/2.0;
419 rect.ymin= (line->from->y1+line->from->y2)/2.0;
420 rect.xmax= (line->to->x1+line->to->x2)/2.0;
421 rect.ymax= (line->to->y1+line->to->y2)/2.0;
423 if(line->flag & UI_SELECT)
424 glColor3ub(100,100,100);
428 ui_draw_link_bezier(&rect);
431 static void ui_draw_links(uiBlock *block)
436 but= block->buttons.first;
438 if(but->type==LINK && but->link) {
439 line= but->link->lines.first;
441 ui_draw_linkline(but, line);
449 /* ************** BLOCK ENDING FUNCTION ************* */
451 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
452 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
454 /* various properties are being compared here, hopfully sufficient
455 * to catch all cases, but it is simple to add more checks later */
456 if(but->retval != oldbut->retval) return 0;
457 if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
458 if(but->rnaprop != oldbut->rnaprop)
459 if(but->rnaindex != oldbut->rnaindex) return 0;
460 if(but->func != oldbut->func) return 0;
461 if(but->funcN != oldbut->funcN) return 0;
462 if(oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return 0;
463 if(oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return 0;
464 if(!but->funcN && ((but->poin != oldbut->poin && (uiBut*)oldbut->poin != oldbut) || but->pointype != oldbut->pointype)) return 0;
469 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
475 oldblock= block->oldblock;
479 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
480 if(ui_but_equals_old(oldbut, but)) {
482 but->flag= oldbut->flag;
483 but->active= oldbut->active;
484 but->pos= oldbut->pos;
485 but->editstr= oldbut->editstr;
486 but->editval= oldbut->editval;
487 but->editvec= oldbut->editvec;
488 but->editcoba= oldbut->editcoba;
489 but->editcumap= oldbut->editcumap;
490 but->selsta= oldbut->selsta;
491 but->selend= oldbut->selend;
492 but->softmin= oldbut->softmin;
493 but->softmax= oldbut->softmax;
494 but->linkto[0]= oldbut->linkto[0];
495 but->linkto[1]= oldbut->linkto[1];
498 oldbut->active= NULL;
501 /* ensures one button can get activated, and in case the buttons
502 * draw are the same this gives O(1) lookup for each button */
503 BLI_remlink(&oldblock->buttons, oldbut);
504 ui_free_but(C, oldbut);
513 /* needed for temporarily rename buttons, such as in outliner or fileselect,
514 they should keep calling uiDefButs to keep them alive */
515 /* returns 0 when button removed */
516 int uiButActiveOnly(const bContext *C, uiBlock *block, uiBut *but)
520 int activate= 0, found= 0, isactive= 0;
522 oldblock= block->oldblock;
526 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
527 if(ui_but_equals_old(oldbut, but)) {
537 if(activate || found==0) {
538 ui_button_activate_do( (bContext *)C, CTX_wm_region(C), but);
540 else if(found && isactive==0) {
542 BLI_remlink(&block->buttons, but);
550 void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
554 char buf[512], *butstr;
556 /* only do it before bounding */
557 if(block->minx != block->maxx)
560 for(but=block->buttons.first; but; but=but->next) {
562 prop= (but->opptr)? but->opptr->data: NULL;
564 if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
565 butstr= MEM_mallocN(strlen(but->str)+strlen(buf)+2, "menu_block_set_keymaps");
566 strcpy(butstr, but->str);
570 but->str= but->strdata;
571 BLI_strncpy(but->str, butstr, sizeof(but->strdata));
580 void uiEndBlock(const bContext *C, uiBlock *block)
583 Scene *scene= CTX_data_scene(C);
585 /* inherit flags from 'old' buttons that was drawn here previous, based
586 * on matching buttons, we need this to make button event handling non
587 * blocking, while still alowing buttons to be remade each redraw as it
588 * is expected by blender code */
589 for(but=block->buttons.first; but; but=but->next) {
590 if(ui_but_update_from_old_block(C, block, but))
593 /* temp? Proper check for greying out */
595 wmOperatorType *ot= but->optype;
598 CTX_store_set((bContext*)C, but->context);
600 if(ot == NULL || WM_operator_poll((bContext*)C, ot)==0) {
601 but->flag |= UI_BUT_DISABLED;
606 CTX_store_set((bContext*)C, NULL);
609 /* only update soft range while not editing */
610 if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
611 ui_set_but_soft_range(but, ui_get_but_val(but));
613 ui_but_anim_flag(but, (scene)? scene->r.cfra: 0.0f);
616 if(block->oldblock) {
617 block->auto_open= block->oldblock->auto_open;
618 block->auto_open_last= block->oldblock->auto_open_last;
619 block->tooltipdisabled= block->oldblock->tooltipdisabled;
621 block->oldblock= NULL;
624 /* handle pending stuff */
625 if(block->layouts.first) uiBlockLayoutResolve(block, NULL, NULL);
626 ui_block_do_align(block);
627 if(block->flag & UI_BLOCK_LOOP) ui_menu_block_set_keymaps(C, block);
630 if(block->dobounds == 1) ui_bounds_block(block);
631 else if(block->dobounds == 2) ui_text_bounds_block(block, 0.0f);
632 else if(block->dobounds) ui_popup_bounds_block(C, block, (block->dobounds == 4));
634 if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
635 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
640 /* ************** BLOCK DRAWING FUNCTION ************* */
642 void ui_fontscale(short *points, float aspect)
644 if(aspect < 0.9f || aspect > 1.1f) {
645 float pointsf= *points;
647 /* for some reason scaling fonts goes too fast compared to widget size */
648 aspect= sqrt(aspect);
652 *points= ceil(pointsf);
654 *points= floor(pointsf);
658 /* project button or block (but==NULL) to pixels in regionspace */
659 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
662 float getsizex, getsizey;
667 gx= (but?but->x1:block->minx) + (block->panel?block->panel->ofsx:0.0f);
668 gy= (but?but->y1:block->miny) + (block->panel?block->panel->ofsy:0.0f);
670 rect->xmin= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
671 rect->ymin= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
673 gx= (but?but->x2:block->maxx) + (block->panel?block->panel->ofsx:0.0f);
674 gy= (but?but->y2:block->maxy) + (block->panel?block->panel->ofsy:0.0f);
676 rect->xmax= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
677 rect->ymax= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
681 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
682 void uiDrawBlock(const bContext *C, uiBlock *block)
684 uiStyle style= *((uiStyle *)U.uistyles.first); // XXX pass on as arg
689 /* get menu region or area region */
692 ar= CTX_wm_region(C);
695 uiEndBlock(C, block);
697 /* we set this only once */
698 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
701 ui_fontscale(&style.paneltitle.points, block->aspect);
702 ui_fontscale(&style.grouplabel.points, block->aspect);
703 ui_fontscale(&style.widgetlabel.points, block->aspect);
704 ui_fontscale(&style.widget.points, block->aspect);
706 /* scale block min/max to rect */
707 ui_but_to_pixelrect(&rect, ar, block, NULL);
709 /* pixel space for AA widgets */
713 wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
716 if(block->flag & UI_BLOCK_LOOP)
717 ui_draw_menu_back(&style, block, &rect);
718 else if(block->panel)
719 ui_draw_aligned_panel(ar, &style, block, &rect);
722 for(but= block->buttons.first; but; but= but->next) {
723 ui_but_to_pixelrect(&rect, ar, block, but);
724 if(!(but->flag & UI_HIDDEN))
725 ui_draw_but(C, ar, &style, but, &rect);
731 ui_draw_links(block);
734 /* ************* EVENTS ************* */
736 static void ui_is_but_sel(uiBut *but)
740 short push=0, true=1;
742 value= ui_get_but_val(but);
744 if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) true= 0;
748 if( BTST(lvalue, (but->bitnr)) ) push= true;
767 if(value!=but->hardmin) push= 1;
772 if(value==0.0) push= 1;
776 if(value == but->hardmax) push= 1;
788 else if(push==1) but->flag |= UI_SELECT;
789 else but->flag &= ~UI_SELECT;
792 /* XXX 2.50 no links supported yet */
794 static int uibut_contains_pt(uiBut *but, short *mval)
800 uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
804 /* find button to link to */
805 for (bt= block->buttons.first; bt; bt= bt->next)
806 if(bt!=but && uibut_contains_pt(bt, mval))
810 if (but->type==LINK && bt->type==INLINK) {
811 if( but->link->tocode == (int)bt->hardmin ) {
815 else if(but->type==INLINK && bt->type==LINK) {
816 if( bt->link->tocode == (int)but->hardmin ) {
826 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
830 but= block->buttons.first;
832 if(but->type==INLINK) {
833 if(but->poin == poin) return but;
840 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
844 line= MEM_callocN(sizeof(uiLinkLine), "linkline");
845 BLI_addtail(listb, line);
850 uiBut *uiFindInlink(uiBlock *block, void *poin)
852 return ui_find_inlink(block, poin);
855 void uiComposeLinks(uiBlock *block)
862 but= block->buttons.first;
864 if(but->type==LINK) {
867 /* for all pointers in the array */
871 for(a=0; a < *(link->totlink); a++) {
872 bt= ui_find_inlink(block, (*ppoin)[a] );
874 ui_add_link_line(&link->lines, but, bt);
878 else if(link->poin) {
879 bt= ui_find_inlink(block, *(link->poin) );
881 ui_add_link_line(&link->lines, but, bt);
891 /* ************************************************ */
893 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
897 block->lockstr= lockstr;
901 void uiBlockClearButLock(uiBlock *block)
904 block->lockstr= NULL;
907 /* *************************************************************** */
910 /* XXX 2.50 no links supported yet */
912 static void ui_delete_active_linkline(uiBlock *block)
916 uiLinkLine *line, *nline;
919 but= block->buttons.first;
921 if(but->type==LINK && but->link) {
922 line= but->link->lines.first;
927 if(line->flag & UI_SELECT) {
928 BLI_remlink(&but->link->lines, line);
930 link= line->from->link;
932 /* are there more pointers allowed? */
935 if(*(link->totlink)==1) {
937 MEM_freeN(*(link->ppoin));
938 *(link->ppoin)= NULL;
942 for(a=0; a< (*(link->totlink)); a++) {
944 if( (*(link->ppoin))[a] != line->to->poin ) {
945 (*(link->ppoin))[b]= (*(link->ppoin))[a];
949 (*(link->totlink))--;
964 /* temporal! these buttons can be everywhere... */
965 allqueue(REDRAWBUTSLOGIC, 0);
968 static void ui_do_active_linklines(uiBlock *block, short *mval)
971 uiLinkLine *line, *act= NULL;
972 float mindist= 12.0, fac, v1[2], v2[2], v3[3];
979 /* find a line close to the mouse */
980 but= block->buttons.first;
982 if(but->type==LINK && but->link) {
984 line= but->link->lines.first;
986 v2[0]= line->from->x2;
987 v2[1]= (line->from->y1+line->from->y2)/2.0;
989 v3[1]= (line->to->y1+line->to->y2)/2.0;
991 fac= dist_to_line_segment_v2(v1, v2, v3);
1003 /* check for a 'found one' to prevent going to 'frontbuffer' mode.
1004 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
1006 glDrawBuffer(GL_FRONT);
1009 but= block->buttons.first;
1011 if(but->type==LINK && but->link) {
1012 line= but->link->lines.first;
1015 if((line->flag & UI_SELECT)==0) {
1016 line->flag |= UI_SELECT;
1017 ui_draw_linkline(but, line);
1020 else if(line->flag & UI_SELECT) {
1021 line->flag &= ~UI_SELECT;
1022 ui_draw_linkline(but, line);
1030 glDrawBuffer(GL_BACK);
1035 /* ******************************************************* */
1037 /* XXX 2.50 no screendump supported yet */
1040 /* nasty but safe way to store screendump rect */
1041 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
1043 static void ui_set_screendump_bbox(uiBlock *block)
1048 scr_sizex= block->maxx - block->minx;
1049 scr_sizey= block->maxy - block->miny;
1052 scr_sizex= scr_sizey= 0;
1056 /* used for making screenshots for menus, called in screendump.c */
1057 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1059 if(scr_sizex!=0 && scr_sizey!=0) {
1071 /* *********************** data get/set ***********************
1072 * this either works with the pointed to data, or can work with
1073 * an edit override pointer while dragging for example */
1075 /* for buttons pointing to color for example */
1076 void ui_get_but_vectorf(uiBut *but, float *vec)
1082 VECCOPY(vec, but->editvec);
1089 vec[0]= vec[1]= vec[2]= 0.0f;
1091 if(RNA_property_type(prop) == PROP_FLOAT) {
1092 tot= RNA_property_array_length(&but->rnapoin, prop);
1095 for(a=0; a<tot; a++)
1096 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1099 else if(but->pointype == CHA) {
1100 char *cp= (char *)but->poin;
1101 vec[0]= ((float)cp[0])/255.0;
1102 vec[1]= ((float)cp[1])/255.0;
1103 vec[2]= ((float)cp[2])/255.0;
1105 else if(but->pointype == FLO) {
1106 float *fp= (float *)but->poin;
1111 /* for buttons pointing to color for example */
1112 void ui_set_but_vectorf(uiBut *but, float *vec)
1118 VECCOPY(but->editvec, vec);
1125 if(RNA_property_type(prop) == PROP_FLOAT) {
1126 tot= RNA_property_array_length(&but->rnapoin, prop);
1129 for(a=0; a<tot; a++)
1130 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1133 else if(but->pointype == CHA) {
1134 char *cp= (char *)but->poin;
1135 cp[0]= (char)(0.5 +vec[0]*255.0);
1136 cp[1]= (char)(0.5 +vec[1]*255.0);
1137 cp[2]= (char)(0.5 +vec[2]*255.0);
1139 else if(but->pointype == FLO) {
1140 float *fp= (float *)but->poin;
1145 int ui_is_but_float(uiBut *but)
1147 if(but->pointype==FLO && but->poin)
1150 if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1156 int ui_is_but_unit(uiBut *but)
1158 Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1159 if(scene->unit.system == USER_UNIT_NONE)
1162 if(but->rnaprop==NULL)
1165 if(RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop))==0)
1171 double ui_get_but_val(uiBut *but)
1176 if(but->editval) { return *(but->editval); }
1177 if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1182 switch(RNA_property_type(prop)) {
1184 if(RNA_property_array_length(&but->rnapoin, prop))
1185 value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1187 value= RNA_property_boolean_get(&but->rnapoin, prop);
1190 if(RNA_property_array_length(&but->rnapoin, prop))
1191 value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1193 value= RNA_property_int_get(&but->rnapoin, prop);
1196 if(RNA_property_array_length(&but->rnapoin, prop))
1197 value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1199 value= RNA_property_float_get(&but->rnapoin, prop);
1202 value= RNA_property_enum_get(&but->rnapoin, prop);
1209 else if(but->type== HSVSLI) {
1212 fp= (but->editvec)? but->editvec: (float *)but->poin;
1213 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1215 switch(but->str[0]) {
1216 case 'H': value= h; break;
1217 case 'S': value= s; break;
1218 case 'V': value= v; break;
1221 else if( but->pointype == CHA ) {
1222 value= *(char *)but->poin;
1224 else if( but->pointype == SHO ) {
1225 value= *(short *)but->poin;
1227 else if( but->pointype == INT ) {
1228 value= *(int *)but->poin;
1230 else if( but->pointype == FLO ) {
1231 value= *(float *)but->poin;
1237 void ui_set_but_val(uiBut *but, double value)
1241 /* value is a hsv value: convert to rgb */
1245 if(RNA_property_editable(&but->rnapoin, prop)) {
1246 switch(RNA_property_type(prop)) {
1248 if(RNA_property_array_length(&but->rnapoin, prop))
1249 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1251 RNA_property_boolean_set(&but->rnapoin, prop, value);
1254 if(RNA_property_array_length(&but->rnapoin, prop))
1255 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, value);
1257 RNA_property_int_set(&but->rnapoin, prop, value);
1260 if(RNA_property_array_length(&but->rnapoin, prop))
1261 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1263 RNA_property_float_set(&but->rnapoin, prop, value);
1266 RNA_property_enum_set(&but->rnapoin, prop, value);
1273 else if(but->pointype==0);
1274 else if(but->type==HSVSLI ) {
1277 fp= (but->editvec)? but->editvec: (float *)but->poin;
1278 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1280 switch(but->str[0]) {
1281 case 'H': h= value; break;
1282 case 'S': s= value; break;
1283 case 'V': v= value; break;
1286 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1290 /* first do rounding */
1291 if(but->pointype==CHA)
1292 value= (char)floor(value+0.5);
1293 else if(but->pointype==SHO ) {
1294 /* gcc 3.2.1 seems to have problems
1295 * casting a double like 32772.0 to
1296 * a short so we cast to an int, then
1299 gcckludge = (int) floor(value+0.5);
1300 value= (short)gcckludge;
1302 else if(but->pointype==INT )
1303 value= (int)floor(value+0.5);
1304 else if(but->pointype==FLO ) {
1305 float fval= (float)value;
1306 if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f; /* prevent negative zero */
1310 /* then set value with possible edit override */
1312 *but->editval= value;
1313 else if(but->pointype==CHA)
1314 *((char *)but->poin)= (char)value;
1315 else if(but->pointype==SHO)
1316 *((short *)but->poin)= (short)value;
1317 else if(but->pointype==INT)
1318 *((int *)but->poin)= (int)value;
1319 else if(but->pointype==FLO)
1320 *((float *)but->poin)= (float)value;
1323 /* update select flag */
1327 int ui_get_but_string_max_length(uiBut *but)
1329 if(but->type == TEX)
1330 return but->hardmax;
1331 else if(but->type == IDPOIN)
1332 return sizeof(((ID*)NULL)->name)-2;
1334 return UI_MAX_DRAW_STR;
1337 static double ui_get_but_scale_unit(uiBut *but, double value)
1339 Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1340 int subtype= RNA_property_subtype(but->rnaprop);
1342 if(subtype & PROP_UNIT_LENGTH) {
1343 return value * scene->unit.scale_length;
1345 else if(subtype & PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
1346 return FRA2TIME(value);
1353 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad)
1355 Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1356 int do_split= scene->unit.flag & USER_UNIT_OPT_SPLIT;
1357 int unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1358 int precission= but->a2;
1360 if(scene->unit.scale_length<0.0001) scene->unit.scale_length= 1.0; // XXX do_versions
1363 if(precission>4) precission= 4;
1364 else if(precission==0) precission= 2;
1366 bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precission, scene->unit.system, unit_type, do_split, pad);
1369 static float ui_get_but_step_unit(uiBut *but, double value, float step_default)
1371 Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1372 int unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1375 step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, value), scene->unit.system, unit_type);
1377 if(step > 0.0) { /* -1 is an error value */
1378 return (step/ui_get_but_scale_unit(but, 1.0))*100;
1381 return step_default;
1386 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1388 if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1392 type= RNA_property_type(but->rnaprop);
1394 if(type == PROP_STRING) {
1396 buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1398 else if(type == PROP_POINTER) {
1400 PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1401 buf= RNA_struct_name_get_alloc(&ptr, str, maxlen);
1405 BLI_strncpy(str, "", maxlen);
1407 else if(buf && buf != str) {
1408 /* string was too long, we have to truncate */
1409 BLI_strncpy(str, buf, maxlen);
1413 else if(but->type == IDPOIN) {
1415 ID *id= *(but->idpoin_idpp);
1417 if(id) BLI_strncpy(str, id->name+2, maxlen);
1418 else BLI_strncpy(str, "", maxlen);
1422 else if(but->type == TEX) {
1424 BLI_strncpy(str, but->poin, maxlen);
1427 else if(but->type == SEARCH_MENU) {
1429 BLI_strncpy(str, but->poin, maxlen);
1432 else if(ui_but_anim_expression_get(but, str, maxlen))
1433 ; /* driver expression */
1435 /* number editing */
1438 value= ui_get_but_val(but);
1440 if(ui_is_but_float(but)) {
1441 if(ui_is_but_unit(but)) {
1442 ui_get_but_string_unit(but, str, maxlen, value, 0);
1444 else if(but->a2) { /* amount of digits defined */
1445 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1446 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1447 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1448 else BLI_snprintf(str, maxlen, "%.4f", value);
1451 BLI_snprintf(str, maxlen, "%.3f", value);
1454 BLI_snprintf(str, maxlen, "%d", (int)value);
1458 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1460 if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1461 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1464 type= RNA_property_type(but->rnaprop);
1466 if(type == PROP_STRING) {
1468 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1471 else if(type == PROP_POINTER) {
1473 PointerRNA ptr, rptr;
1476 if(str == NULL || str[0] == '\0') {
1477 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1481 ptr= but->rnasearchpoin;
1482 prop= but->rnasearchprop;
1484 if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1485 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1494 else if(but->type == IDPOIN) {
1496 but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1499 else if(but->type == TEX) {
1501 BLI_strncpy(but->poin, str, but->hardmax);
1504 else if(but->type == SEARCH_MENU) {
1506 BLI_strncpy(but->poin, str, but->hardmax);
1509 else if(ui_but_anim_expression_set(but, str)) {
1510 /* driver expression */
1514 /* number editing */
1517 #ifndef DISABLE_PYTHON
1519 char str_unit_convert[256];
1521 Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
1524 unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
1528 BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
1530 if(scene->unit.system != USER_UNIT_NONE && unit_type) {
1531 /* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
1532 bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr, ui_get_but_scale_unit(but, 1.0), scene->unit.system, unit_type);
1535 if(BPY_button_eval(C, str_unit_convert, &value)) {
1536 value = ui_get_but_val(but); /* use its original value */
1546 if(!ui_is_but_float(but)) value= (int)value;
1547 if(but->type==NUMABS) value= fabs(value);
1549 /* not that we use hard limits here */
1550 if(value<but->hardmin) value= but->hardmin;
1551 if(value>but->hardmax) value= but->hardmax;
1553 ui_set_but_val(but, value);
1560 static double soft_range_round_up(double value, double max)
1562 /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1563 double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1565 if(newmax*0.2 >= max && newmax*0.2 >= value)
1567 else if(newmax*0.5 >= max && newmax*0.5 >= value)
1573 static double soft_range_round_down(double value, double max)
1575 /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1576 double newmax= pow(10.0, floor(log(value)/log(10.0)));
1578 if(newmax*5.0 <= max && newmax*5.0 <= value)
1580 else if(newmax*2.0 <= max && newmax*2.0 <= value)
1586 void ui_set_but_soft_range(uiBut *but, double value)
1589 double softmin, softmax, step, precision;
1592 type= RNA_property_type(but->rnaprop);
1594 if(type == PROP_INT) {
1595 int imin, imax, istep;
1597 RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1603 else if(type == PROP_FLOAT) {
1604 float fmin, fmax, fstep, fprecision;
1606 RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1610 precision= fprecision;
1615 /* clamp button range to something reasonable in case
1616 * we get -inf/inf from RNA properties */
1617 softmin= MAX2(softmin, -1e4);
1618 softmax= MIN2(softmax, 1e4);
1620 /* if the value goes out of the soft/max range, adapt the range */
1621 if(value+1e-10 < softmin) {
1623 softmin= -soft_range_round_up(-value, -softmin);
1625 softmin= soft_range_round_down(value, softmin);
1627 if(softmin < but->hardmin)
1628 softmin= but->hardmin;
1630 else if(value-1e-10 > softmax) {
1632 softmax= -soft_range_round_down(-value, -softmax);
1634 softmax= soft_range_round_up(value, softmax);
1636 if(softmax > but->hardmax)
1637 softmax= but->hardmax;
1640 but->softmin= softmin;
1641 but->softmax= softmax;
1645 /* ******************* Free ********************/
1647 static void ui_free_link(uiLink *link)
1650 BLI_freelistN(&link->lines);
1655 /* can be called with C==NULL */
1656 static void ui_free_but(const bContext *C, uiBut *but)
1659 WM_operator_properties_free(but->opptr);
1660 MEM_freeN(but->opptr);
1662 if(but->func_argN) MEM_freeN(but->func_argN);
1664 /* XXX solve later, buttons should be free-able without context? */
1666 ui_button_active_cancel(C, but);
1669 MEM_freeN(but->active);
1671 if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1672 ui_free_link(but->link);
1677 /* can be called with C==NULL */
1678 void uiFreeBlock(const bContext *C, uiBlock *block)
1682 while( (but= block->buttons.first) ) {
1683 BLI_remlink(&block->buttons, but);
1684 ui_free_but(C, but);
1687 if(block->func_argN)
1688 MEM_freeN(block->func_argN);
1690 CTX_store_free_list(&block->contexts);
1692 BLI_freelistN(&block->saferct);
1697 /* can be called with C==NULL */
1698 void uiFreeBlocks(const bContext *C, ListBase *lb)
1702 while( (block= lb->first) ) {
1703 BLI_remlink(lb, block);
1704 uiFreeBlock(C, block);
1708 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1710 uiBlock *block, *nextblock;
1712 for(block=lb->first; block; block=nextblock) {
1713 nextblock= block->next;
1715 if(!block->handle) {
1716 if(!block->active) {
1717 BLI_remlink(lb, block);
1718 uiFreeBlock(C, block);
1726 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1729 uiBlock *oldblock= NULL;
1731 lb= ®ion->uiblocks;
1733 /* each listbase only has one block with this name, free block
1734 * if is already there so it can be rebuilt from scratch */
1736 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1737 if (BLI_streq(oldblock->name, block->name))
1741 oldblock->active= 0;
1742 oldblock->panel= NULL;
1746 block->oldblock= oldblock;
1748 /* at the beginning of the list! for dynamical menus/blocks */
1750 BLI_addhead(lb, block);
1753 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1757 int getsizex, getsizey;
1759 window= CTX_wm_window(C);
1761 block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1764 block->evil_C= (void*)C; // XXX
1765 BLI_strncpy(block->name, name, sizeof(block->name));
1768 uiBlockSetRegion(block, region);
1770 /* window matrix and aspect */
1771 if(region && region->swinid) {
1772 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1773 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1775 /* TODO - investigate why block->winmat[0][0] is negative
1776 * in the image view when viewRedrawForce is called */
1777 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1780 /* no subwindow created yet, for menus for example, so we
1781 * use the main window instead, since buttons are created
1783 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1784 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1786 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1787 block->auto_open= 2;
1788 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1794 uiBlock *uiGetBlock(char *name, ARegion *ar)
1796 uiBlock *block= ar->uiblocks.first;
1799 if( strcmp(name, block->name)==0 ) return block;
1806 void uiBlockSetEmboss(uiBlock *block, short dt)
1811 void ui_check_but(uiBut *but)
1813 /* if something changed in the button */
1816 // int transopts= ui_translate_buttons();
1820 // if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1822 /* test for min and max, icon sliders, etc */
1823 switch( but->type ) {
1829 value= ui_get_but_val(but);
1830 if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1831 else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1835 value= fabs( ui_get_but_val(but) );
1836 if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1837 else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1842 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1843 if(but->flag & UI_SELECT) but->iconadd= 1;
1844 else but->iconadd= 0;
1849 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1850 value= ui_get_but_val(but);
1851 but->iconadd= (int)value- (int)(but->hardmin);
1856 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1857 value= ui_get_but_val(but);
1858 but->iconadd= (int)value- (int)(but->hardmin);
1864 /* safety is 4 to enable small number buttons (like 'users') */
1865 okwidth= -4 + (but->x2 - but->x1);
1868 switch( but->type ) {
1873 if(but->x2 - but->x1 > 24) {
1874 value= ui_get_but_val(but);
1875 ui_set_name_menu(but, (int)value);
1884 value= ui_get_but_val(but);
1886 if(ui_is_but_float(but)) {
1887 if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
1888 else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
1889 /* support length type buttons */
1890 else if(ui_is_but_unit(but)) {
1891 char new_str[sizeof(but->drawstr)];
1892 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE);
1893 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, new_str);
1895 else if(but->a2) { /* amount of digits defined */
1896 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1897 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1898 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1899 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1902 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1903 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1907 sprintf(but->drawstr, "%s%d", but->str, (int)value);
1911 PropertySubType pstype = RNA_property_subtype(but->rnaprop);
1913 if (pstype == PROP_PERCENTAGE)
1914 strcat(but->drawstr, "%");
1919 if(ui_is_but_float(but)) {
1920 value= ui_get_but_val(but);
1921 if(but->a2) { /* amount of digits defined */
1922 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1923 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1924 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1925 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1928 sprintf(but->drawstr, "%s%.2f", but->str, value);
1931 else strcpy(but->drawstr, but->str);
1939 char str[UI_MAX_DRAW_STR];
1941 ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1943 strcpy(but->drawstr, but->str);
1944 strcat(but->drawstr, str);
1949 strcpy(but->drawstr, but->str);
1950 if (but->flag & UI_SELECT) {
1951 strcat(but->drawstr, "Press a key");
1953 strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1958 if (but->flag & UI_SELECT) {
1959 short *sp= (short *)but->func_arg3;
1961 if(but->flag & UI_BUT_IMMEDIATE)
1962 strcpy(but->drawstr, but->str);
1964 strcpy(but->drawstr, "");
1967 char *str= but->drawstr;
1970 str= strcat(str, "Shift ");
1972 str= strcat(str, "Ctrl ");
1974 str= strcat(str, "Alt ");
1976 str= strcat(str, "Cmd ");
1979 strcat(but->drawstr, "Press a key ");
1982 strcpy(but->drawstr, but->str);
1987 /* trying to get the dual-icon to left of text... not very nice */
1989 strcpy(but->drawstr, " ");
1990 strcpy(but->drawstr+2, but->str);
1994 strcpy(but->drawstr, but->str);
1998 /* if we are doing text editing, this will override the drawstr */
2000 strcpy(but->drawstr, but->editstr);
2002 /* text clipping moved to widget drawing code itself */
2006 void uiBlockBeginAlign(uiBlock *block)
2008 /* if other align was active, end it */
2009 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
2011 block->flag |= UI_BUT_ALIGN_DOWN;
2014 /* buttons declared after this call will get this align nr */ // XXX flag?
2017 static int buts_are_horiz(uiBut *but1, uiBut *but2)
2021 dx= fabs( but1->x2 - but2->x1);
2022 dy= fabs( but1->y1 - but2->y2);
2024 if(dx > dy) return 0;
2028 void uiBlockEndAlign(uiBlock *block)
2030 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
2033 int ui_but_can_align(uiBut *but)
2035 return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
2038 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
2040 uiBut *prev, *but=NULL, *next;
2041 int flag= 0, cols=0, rows=0;
2045 for(but=first; but && but->alignnr == nr; but=but->next) {
2046 if(but->next && but->next->alignnr == nr) {
2047 if(buts_are_horiz(but, but->next)) cols++;
2052 /* rows==0: 1 row, cols==0: 1 collumn */
2054 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2055 for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
2057 if(next && next->alignnr != nr)
2060 /* clear old flag */
2061 but->flag &= ~UI_BUT_ALIGN;
2063 if(flag==0) { /* first case */
2065 if(buts_are_horiz(but, next)) {
2067 flag= UI_BUT_ALIGN_RIGHT;
2069 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2072 flag= UI_BUT_ALIGN_DOWN;
2076 else if(next==NULL) { /* last case */
2078 if(buts_are_horiz(prev, but)) {
2080 flag= UI_BUT_ALIGN_LEFT;
2082 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2084 else flag= UI_BUT_ALIGN_TOP;
2087 else if(buts_are_horiz(but, next)) {
2088 /* check if this is already second row */
2089 if( prev && buts_are_horiz(prev, but)==0) {
2090 flag &= ~UI_BUT_ALIGN_LEFT;
2091 flag |= UI_BUT_ALIGN_TOP;
2092 /* exception case: bottom row */
2095 while(bt && bt->alignnr == nr) {
2096 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break;
2099 if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2102 else flag |= UI_BUT_ALIGN_LEFT;
2106 flag |= UI_BUT_ALIGN_TOP;
2108 else { /* next button switches to new row */
2110 if(prev && buts_are_horiz(prev, but))
2111 flag |= UI_BUT_ALIGN_LEFT;
2113 if( (flag & UI_BUT_ALIGN_TOP)==0) { /* stil top row */
2115 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2117 flag |= UI_BUT_ALIGN_DOWN;
2120 flag |= UI_BUT_ALIGN_TOP;
2126 /* merge coordinates */
2130 but->x1= (prev->x2+but->x1)/2.0;
2134 but->y2= (prev->y1+but->y2)/2.0;
2138 if(buts_are_horiz(prev, but)) {
2139 but->x1= (prev->x2+but->x1)/2.0;
2141 /* copy height too */
2144 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2145 /* the previous button is a single one in its row */
2146 but->y2= (prev->y1+but->y2)/2.0;
2150 /* the previous button is not a single one in its row */
2158 void ui_block_do_align(uiBlock *block)
2163 /* align buttons with same align nr */
2164 for(but=block->buttons.first; but;) {
2167 ui_block_do_align_but(block, but, nr);
2169 /* skip with same number */
2170 for(; but && but->alignnr == nr; but=but->next);
2181 ui_def_but is the function that draws many button types
2184 "a1" Click Step (how much to change the value each click)
2185 "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2186 all greater values will be clamped to 4.
2189 static uiBut *ui_def_but(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, char *tip)
2194 if(type & BUTPOIN) { /* a pointer is required */
2199 but= MEM_callocN(sizeof(uiBut), "uiBut");
2201 but->type= type & BUTTYPE;
2202 but->pointype= type & BUTPOIN;
2203 but->bit= type & BIT;
2204 but->bitnr= type & 31;
2207 but->retval= retval;
2208 if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2209 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2210 strcpy(but->str, str);
2213 but->str= but->strdata;
2214 strcpy(but->str, str);
2222 but->hardmin= but->softmin= min;
2223 but->hardmax= but->softmax= max;
2228 but->lock= block->lock;
2229 but->lockstr= block->lockstr;
2232 but->aspect= 1.0f; //XXX block->aspect;
2233 but->block= block; // pointer back, used for frontbuffer status, and picker
2235 if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2236 but->alignnr= block->alignnr;
2238 but->func= block->func;
2239 but->func_arg1= block->func_arg1;
2240 but->func_arg2= block->func_arg2;
2242 but->funcN= block->funcN;
2243 if(block->func_argN)
2244 but->func_argN= MEM_dupallocN(block->func_argN);
2246 but->pos= -1; /* cursor invisible */
2248 if(ELEM(but->type, NUM, NUMABS)) { /* add a space to name */
2249 slen= strlen(but->str);
2250 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2251 if(but->str[slen-1]!=' ') {
2252 but->str[slen]= ' ';
2253 but->str[slen+1]= 0;
2258 if(ELEM(but->type, HSVCUBE, HSVCIRCLE)) { /* hsv buttons temp storage */
2260 ui_get_but_vectorf(but, rgb);
2261 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2264 if((block->flag & UI_BLOCK_LOOP) || ELEM7(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU))
2265 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2266 else if(but->type==BUT_TOGDUAL)
2267 but->flag |= UI_ICON_LEFT;
2269 but->flag |= (block->flag & UI_BUT_ALIGN);
2273 but->flag |= UI_BUT_DISABLED;
2277 if(ELEM8(but->type, BLOCK, BUT, LABEL, PULLDOWN, ROUNDBOX, LISTBOX, SEARCH_MENU, BUTM));
2278 else if(ELEM5(but->type, SCROLL, SEPR, LINK, INLINK, FTPREVIEW));
2279 else but->flag |= UI_BUT_UNDO;
2281 BLI_addtail(&block->buttons, but);
2283 if(block->curlayout)
2284 ui_layout_add_but(block->curlayout, but);
2289 uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, char *tip)
2293 PropertyType proptype;
2294 int freestr= 0, icon= 0;
2296 prop= RNA_struct_find_property(ptr, propname);
2299 proptype= RNA_property_type(prop);
2301 /* use rna values if parameters are not specified */
2303 if(type == MENU && proptype == PROP_ENUM) {
2304 EnumPropertyItem *item;
2306 int i, totitem, value, free;
2308 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2309 value= RNA_property_enum_get(ptr, prop);
2311 dynstr= BLI_dynstr_new();
2312 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2313 for(i=0; i<totitem; i++) {
2314 if(!item[i].identifier[0]) {
2316 BLI_dynstr_appendf(dynstr, "|%s%%l", item[i].name);
2318 BLI_dynstr_append(dynstr, "|%l");
2320 else if(item[i].icon)
2321 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2323 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2325 if(value == item[i].value)
2328 str= BLI_dynstr_get_cstring(dynstr);
2329 BLI_dynstr_free(dynstr);
2336 else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2337 EnumPropertyItem *item;
2338 int i, totitem, free;
2340 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2341 for(i=0; i<totitem; i++) {
2342 if(item[i].identifier[0] && item[i].value == (int)max) {
2343 str= (char*)item[i].name;
2349 str= (char*)RNA_property_ui_name(prop);
2354 str= (char*)RNA_property_ui_name(prop);
2355 icon= RNA_property_ui_icon(prop);
2360 if(type == ROW && proptype == PROP_ENUM) {
2361 EnumPropertyItem *item;
2362 int i, totitem, free;
2364 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2366 for(i=0; i<totitem; i++) {
2367 if(item[i].identifier[0] && item[i].value == (int)max) {
2368 if(item[i].description[0])
2369 tip= (char*)item[i].description;
2380 tip= (char*)RNA_property_ui_description(prop);
2382 if(min == max || a1 == -1 || a2 == -1) {
2383 if(proptype == PROP_INT) {
2384 int hardmin, hardmax, softmin, softmax, step;
2386 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2387 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2389 if(!ELEM(type, ROW, LISTROW) && min == max) {
2398 else if(proptype == PROP_FLOAT) {
2399 float hardmin, hardmax, softmin, softmax, step, precision;
2401 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2402 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2404 if(!ELEM(type, ROW, LISTROW) && min == max) {
2413 else if(proptype == PROP_STRING) {
2415 max= RNA_property_string_maxlength(prop);
2416 if(max == 0) /* interface code should ideally support unlimited length */
2417 max= UI_MAX_DRAW_STR;
2422 str= (char*)propname;
2424 /* now create button */
2425 but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2431 if(RNA_property_array_length(&but->rnapoin, but->rnaprop))
2432 but->rnaindex= index;
2438 but->icon= (BIFIconID)icon;
2439 but->flag |= UI_HAS_ICON;
2440 but->flag|= UI_ICON_LEFT;
2443 if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2444 but->flag |= UI_BUT_DISABLED;
2449 /* If this button uses units, calculate the step from this */
2450 if(ui_is_but_unit(but))
2451 but->a1= ui_get_but_step_unit(but, ui_get_but_val(but), but->a1);
2459 uiBut *ui_def_but_operator(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2464 ot= WM_operatortype_find(opname, 0);
2467 if(ot) str= ot->name;
2471 if ((!tip || tip[0]=='\0') && ot && ot->description) {
2472 tip= ot->description;
2475 but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2477 but->opcontext= opcontext;
2480 but->flag |= UI_BUT_DISABLED;
2488 uiBut *uiDefBut(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, char *tip)
2490 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2497 /* if _x_ is a power of two (only one bit) return the power,
2498 * otherwise return -1.
2499 * (1<<findBitIndex(x))==x for powers of two.
2501 static int findBitIndex(unsigned int x) {
2502 if (!x || (x&(x-1))!=0) { /* x&(x-1) strips lowest bit */
2507 if (x&0xFFFF0000) idx+=16, x>>=16;
2508 if (x&0xFF00) idx+=8, x>>=8;
2509 if (x&0xF0) idx+=4, x>>=4;
2510 if (x&0xC) idx+=2, x>>=2;
2517 /* autocomplete helper functions */
2518 struct AutoComplete {
2524 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2526 AutoComplete *autocpl;
2528 autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2529 autocpl->maxlen= maxlen;
2530 autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2531 autocpl->startname= startname;
2536 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2538 char *truncate= autocpl->truncate;
2539 char *startname= autocpl->startname;
2542 for(a=0; a<autocpl->maxlen-1; a++) {
2543 if(startname[a]==0 || startname[a]!=name[a])
2547 if(startname[a]==0) {
2550 BLI_strncpy(truncate, name, autocpl->maxlen);
2552 /* remove from truncate what is not in bone->name */
2553 for(a=0; a<autocpl->maxlen-1; a++) {
2558 else if(truncate[a]!=name[a])
2565 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2567 if(autocpl->truncate[0])
2568 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2570 if (autoname != autocpl->startname) /* dont copy a string over its self */
2571 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2573 MEM_freeN(autocpl->truncate);
2577 /* autocomplete callback for ID buttons */
2578 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2580 int blocktype= (intptr_t)arg_v;
2581 ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2583 if(listb==NULL) return;
2585 /* search if str matches the beginning of an ID struct */
2587 AutoComplete *autocpl= autocomplete_begin(str, 22);
2590 for(id= listb->first; id; id= id->next)
2591 autocomplete_do_name(autocpl, id->name+2);
2593 autocomplete_end(autocpl, str);
2597 static uiBut *uiDefButBit(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, char *tip)
2599 int bitIdx= findBitIndex(bit);
2603 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2606 uiBut *uiDefButF(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, char *tip)
2608 return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2610 uiBut *uiDefButBitF(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, char *tip)
2612 return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2614 uiBut *uiDefButI(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, char *tip)
2616 return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2618 uiBut *uiDefButBitI(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, char *tip)
2620 return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2622 uiBut *uiDefButS(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, char *tip)
2624 return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2626 uiBut *uiDefButBitS(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, char *tip)
2628 return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2630 uiBut *uiDefButC(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, char *tip)
2632 return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2634 uiBut *uiDefButBitC(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, char *tip)
2636 return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2638 uiBut *uiDefButR(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, char *tip)
2642 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2648 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2652 but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2659 uiBut *uiDefIconBut(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, char *tip)
2661 uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2663 but->icon= (BIFIconID) icon;
2664 but->flag|= UI_HAS_ICON;
2670 static uiBut *uiDefIconButBit(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, char *tip)
2672 int bitIdx= findBitIndex(bit);
2676 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2680 uiBut *uiDefIconButF(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, char *tip)
2682 return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2684 uiBut *uiDefIconButBitF(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, char *tip)
2686 return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2688 uiBut *uiDefIconButI(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, char *tip)
2690 return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2692 uiBut *uiDefIconButBitI(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, char *tip)
2694 return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2696 uiBut *uiDefIconButS(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, char *tip)
2698 return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2700 uiBut *uiDefIconButBitS(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, char *tip)
2702 return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2704 uiBut *uiDefIconButC(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, char *tip)
2706 return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2708 uiBut *uiDefIconButBitC(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, char *tip)
2710 return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2712 uiBut *uiDefIconButR(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, char *tip)
2716 but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2719 but->icon= (BIFIconID) icon;
2720 but->flag|= UI_HAS_ICON;
2727 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2731 but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2733 but->icon= (BIFIconID) icon;
2734 but->flag|= UI_HAS_ICON;
2741 /* Button containing both string label and icon */
2742 uiBut *uiDefIconTextBut(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, char *tip)
2744 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2746 but->icon= (BIFIconID) icon;
2747 but->flag|= UI_HAS_ICON;
2749 but->flag|= UI_ICON_LEFT;
2755 static uiBut *uiDefIconTextButBit(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2, char *tip)
2757 int bitIdx= findBitIndex(bit);
2761 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2765 uiBut *uiDefIconTextButF(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, char *tip)
2767 return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2769 uiBut *uiDefIconTextButBitF(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2, char *tip)
2771 return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2773 uiBut *uiDefIconTextButI(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, char *tip)
2775 return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2777 uiBut *uiDefIconTextButBitI(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2, char *tip)
2779 return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2781 uiBut *uiDefIconTextButS(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, char *tip)
2783 return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2785 uiBut *uiDefIconTextButBitS(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2, char *tip)
2787 return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2789 uiBut *uiDefIconTextButC(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, char *tip)
2791 return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2793 uiBut *uiDefIconTextButBitC(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2, char *tip)
2795 return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2797 uiBut *uiDefIconTextButR(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, char *tip)
2801 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2804 but->icon= (BIFIconID) icon;
2805 but->flag|= UI_HAS_ICON;
2807 but->flag|= UI_ICON_LEFT;
2813 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2817 but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2819 but->icon= (BIFIconID) icon;
2820 but->flag|= UI_HAS_ICON;
2821 but->flag|= UI_ICON_LEFT;
2828 /* END Button containing both string label and icon */
2830 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2834 link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2839 link->fromcode= from;
2843 /* cruft to make uiBlock and uiBut private */
2845 int uiBlocksGetYMin(ListBase *lb)
2850 for (block= lb->first; block; block= block->next)
2851 if (block==lb->first || block->miny<min)
2857 void uiBlockSetDirection(uiBlock *block, int direction)
2859 block->direction= direction;
2862 /* this call escapes if there's alignment flags */
2863 void uiBlockFlipOrder(uiBlock *block)
2867 float centy, miny=10000, maxy= -10000;
2869 if(U.uiflag & USER_MENUFIXEDORDER)
2871 else if(block->flag & UI_BLOCK_NO_FLIP)
2874 for(but= block->buttons.first; but; but= but->next) {
2875 if(but->flag & UI_BUT_ALIGN) return;
2876 if(but->y1 < miny) miny= but->y1;
2877 if(but->y2 > maxy) maxy= but->y2;
2880 centy= (miny+maxy)/2.0;
2881 for(but= block->buttons.first; but; but= but->next) {
2882 but->y1 = centy-(but->y1-centy);
2883 but->y2 = centy-(but->y2-centy);
2884 SWAP(float, but->y1, but->y2);
2887 /* also flip order in block itself, for example for arrowkey */
2888 lb.first= lb.last= NULL;
2889 but= block->buttons.first;
2892 BLI_remlink(&block->buttons, but);
2893 BLI_addtail(&lb, but);
2900 void uiBlockSetFlag(uiBlock *block, int flag)
2905 void uiBlockClearFlag(uiBlock *block, int flag)
2907 block->flag&= ~flag;
2910 void uiBlockSetXOfs(uiBlock *block, int xofs)
2915 void uiButSetFlag(uiBut *but, int flag)
2920 void uiButClearFlag(uiBut *but, int flag)
2925 int uiButGetRetVal(uiBut *but)
2930 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2932 if(but->optype && !but->opptr) {
2933 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2934 WM_operator_properties_create(but->opptr, but->optype->idname);
2940 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
2942 block->handle_func= func;
2943 block->handle_func_arg= arg;
2946 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
2948 block->butm_func= func;
2949 block->butm_func_arg= arg;
2952 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
2955 block->func_arg1= arg1;
2956 block->func_arg2= arg2;
2959 void uiBlockSetNFunc(uiBlock *block, uiButHandleFunc func, void *argN, void *arg2)
2961 if(block->func_argN)
2962 MEM_freeN(block->func_argN);
2965 block->func_argN= argN;
2966 block->func_arg2= arg2;
2969 void uiButSetRenameFunc(uiBut *but, uiButHandleRenameFunc func, void *arg1)
2971 but->rename_func= func;
2972 but->rename_arg1= arg1;
2975 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)(const bContext *C, void *idv, void *arg1, void *arg2, rcti *rect), void *arg1, void *arg2)
2977 block->drawextra= func;
2978 block->drawextra_arg1= arg1;
2979 block->drawextra_arg2= arg2;
2982 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
2985 but->func_arg1= arg1;
2986 but->func_arg2= arg2;
2989 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
2992 MEM_freeN(but->func_argN);
2995 but->func_argN= argN;
2996 but->func_arg2= arg2;
2999 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
3001 but->autocomplete_func= func;
3002 but->autofunc_arg= arg;
3005 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
3007 uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
3008 but->idpoin_func= func;
3009 but->idpoin_idpp= (ID**) idpp;
3013 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
3018 uiBut *uiDefBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
3020 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3021 but->block_create_func= func;
3026 uiBut *uiDefBlockButN(uiBlock *block, uiBlockCreateFunc func, void *argN, char *str, short x1, short y1, short x2, short y2, char *tip)
3028 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
3029 but->block_create_func= func;
3031 MEM_freeN(but->func_argN);
3032 but->func_argN= argN;
3038 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
3040 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3041 but->block_create_func= func;
3046 uiBut *uiDefMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
3048 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3049 but->menu_create_func= func;
3054 uiBut *uiDefIconTextMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
3056 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3058 but->icon= (BIFIconID) icon;
3059 but->flag|= UI_HAS_ICON;
3061 but->flag|= UI_ICON_LEFT;
3062 but->flag|= UI_ICON_SUBMENU;
3064 but->menu_create_func= func;
3070 uiBut *uiDefIconMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, short x1, short y1, short x2, short y2, char *tip)
3072 uiBut *but= ui_def_but(block, PULLDOWN, 0, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3074 but->icon= (BIFIconID) icon;
3075 but->flag |= UI_HAS_ICON;
3076 but->flag &=~ UI_ICON_LEFT;
3078 but->menu_create_func= func;
3084 /* Block button containing both string label and icon */
3085 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
3087 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3089 /* XXX temp, old menu calls pass on icon arrow, which is now UI_ICON_SUBMENU flag */
3090 if(icon!=ICON_RIGHTARROW_THIN) {
3091 but->icon= (BIFIconID) icon;
3092 but->flag|= UI_ICON_LEFT;
3094 but->flag|= UI_HAS_ICON;
3095 but->flag|= UI_ICON_SUBMENU;
3097 but->block_create_func= func;
3103 /* Block button containing icon */
3104 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
3106 uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3108 but->icon= (BIFIconID) icon;
3109 but->flag|= UI_HAS_ICON;
3111 but->flag|= UI_ICON_LEFT;
3113 but->block_create_func= func;
3119 uiBut *uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
3121 uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
3126 /* short pointers hardcoded */
3127 /* modkeypoin will be set to KM_SHIFT, KM_ALT, KM_CTRL, KM_OSKEY bits */
3128 uiBut *uiDefHotKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *keypoin, short *modkeypoin, char *tip)
3130 uiBut *but= ui_def_but(block, HOTKEYEVT|SHO, retval, str, x1, y1, x2, y2, keypoin, 0.0, 0.0, 0.0, 0.0, tip);
3131 but->func_arg3= modkeypoin; /* XXX hrmf, abuse! */
3137 /* arg is pointer to string/name, use uiButSetSearchFunc() below to make this work */
3138 uiBut *uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxlen, short x1, short y1, short x2, short y2, char *tip)
3140 uiBut *but= ui_def_but(block, SEARCH_MENU, retval, "", x1, y1, x2, y2, arg, 0.0, maxlen, 0.0, 0.0, tip);
3142 but->icon= (BIFIconID) icon;
3143 but->flag|= UI_HAS_ICON;
3145 but->flag|= UI_ICON_LEFT|UI_TEXT_LEFT;
3152 /* arg is user value, searchfunc and handlefunc both get it as arg */
3153 /* if active set, button opens with this item visible and selected */
3154 void uiButSetSearchFunc(uiBut *but, uiButSearchFunc sfunc, void *arg, uiButHandleFunc bfunc, void *active)
3156 but->search_func= sfunc;
3157 but->search_arg= arg;
3159 uiButSetFunc(but, bfunc, arg, active);
3162 /* Program Init/Exit */
3166 ui_resources_init();
3169 /* after reading userdef file */
3170 void UI_init_userdef(void)
3172 /* fix saved themes */
3173 init_userdef_do_versions();
3174 /* set default colors in default theme */
3175 ui_theme_init_userdef();