2 * $Id: interface.c 16882 2008-10-02 12:29:45Z ton $
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 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
34 #include "MEM_guardedalloc.h"
37 #include "DNA_listBase.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_userdef_types.h"
42 #include "BLI_arithb.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_dynstr.h"
46 #include "BKE_global.h"
47 #include "BKE_screen.h"
48 #include "BKE_texture.h"
49 #include "BKE_utildefines.h"
52 #include "BIF_glutil.h"
54 #include "UI_interface.h"
62 #include "ED_screen.h"
66 #include "wm_subwindow.h"
67 #include "wm_window.h"
69 #include "RNA_access.h"
70 #include "RNA_types.h"
72 #include "interface.h"
75 * a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt
77 * uiBlahBlah() external function
78 * ui_blah_blah() internal function
81 static void ui_free_but(const bContext *C, uiBut *but);
83 /* ************ GLOBALS ************* */
85 static uiFont UIfont[UI_ARRAY]; // no init needed
87 /* ************* translation ************** */
89 /* XXX 2.50 missing from context */
90 int ui_translate_buttons()
92 return (U.transopts & USER_TR_BUTTONS);
95 int ui_translate_menus()
97 return (U.transopts & USER_TR_MENUS);
100 int ui_translate_tooltips()
102 return (U.transopts & USER_TR_TOOLTIPS);
105 /* ************* window matrix ************** */
107 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
110 int sx, sy, getsizex, getsizey;
112 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
113 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
119 *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
120 *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
123 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
130 ui_block_to_window_fl(ar, block, &fx, &fy);
136 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
141 ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
142 ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
144 winr->xmin= tmpr.xmin;
145 winr->ymin= tmpr.ymin;
146 winr->xmax= tmpr.xmax;
147 winr->ymax= tmpr.ymax;
150 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y) /* for mouse cursor */
152 float a, b, c, d, e, f, px, py;
153 int sx, sy, getsizex, getsizey;
155 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
156 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
160 a= .5*((float)getsizex)*block->winmat[0][0];
161 b= .5*((float)getsizex)*block->winmat[1][0];
162 c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
164 d= .5*((float)getsizey)*block->winmat[0][1];
165 e= .5*((float)getsizey)*block->winmat[1][1];
166 f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
171 *y= (a*(py-f) + d*(c-px))/(a*e-d*b);
172 *x= (px- b*(*y)- c)/a;
175 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
182 ui_window_to_block_fl(ar, block, &fx, &fy);
188 void ui_window_to_region(const ARegion *ar, int *x, int *y)
190 *x-= ar->winrct.xmin;
191 *y-= ar->winrct.ymin;
194 /* ******************* block calc ************************* */
196 /* only for pulldowns */
197 void uiTextBoundsBlock(uiBlock *block, int addval)
200 int i = 0, j, x1addval= 0, nextcol;
202 bt= block->buttons.first;
205 int transopts= ui_translate_buttons();
206 if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
207 j= UI_GetStringWidth(bt->font, bt->drawstr, transopts);
214 /* cope with multi collumns */
215 bt= block->buttons.first;
217 if(bt->next && bt->x1 < bt->next->x1)
222 bt->x2 = bt->x1 + i + addval;
224 ui_check_but(bt); // clips text again
227 x1addval+= i + addval;
233 void uiBoundsBlock(uiBlock *block, int addval)
241 if(block->buttons.first==NULL) {
243 block->minx= 0.0; block->maxx= block->panel->sizex;
244 block->miny= 0.0; block->maxy= block->panel->sizey;
249 block->minx= block->miny= 10000;
250 block->maxx= block->maxy= -10000;
252 bt= block->buttons.first;
254 if(bt->x1 < block->minx) block->minx= bt->x1;
255 if(bt->y1 < block->miny) block->miny= bt->y1;
257 if(bt->x2 > block->maxx) block->maxx= bt->x2;
258 if(bt->y2 > block->maxy) block->maxy= bt->y2;
263 block->minx -= addval;
264 block->miny -= addval;
265 block->maxx += addval;
266 block->maxy += addval;
269 /* hardcoded exception... but that one is annoying with larger safety */
270 bt= block->buttons.first;
271 if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
274 block->safety.xmin= block->minx-xof;
275 block->safety.ymin= block->miny-xof;
276 block->safety.xmax= block->maxx+xof;
277 block->safety.ymax= block->maxy+xof;
280 void uiBlockTranslate(uiBlock *block, int x, int y)
284 for(bt= block->buttons.first; bt; bt=bt->next) {
297 void uiBlockOrigin(uiBlock *block)
300 int minx= 10000, miny= 10000;
302 for(bt= block->buttons.first; bt; bt=bt->next) {
303 if(bt->x1 < minx) minx= bt->x1;
304 if(bt->y1 < miny) miny= bt->y1;
307 uiBlockTranslate(block, -minx, -miny);
310 void ui_autofill(uiBlock *block)
313 float *maxw, *maxh, startx = 0, starty, height = 0;
315 int rows=0, /* cols=0, */ i, lasti;
317 /* first count rows */
318 but= block->buttons.last;
321 /* calculate max width / height for each row */
322 maxw= MEM_callocN(sizeof(float)*rows, "maxw");
323 maxh= MEM_callocN(sizeof(float)*rows, "maxh");
324 but= block->buttons.first;
327 if( maxh[i] < but->y2) maxh[i]= but->y2;
333 for(i=0; i<rows; i++) totmaxh+= maxh[i];
335 /* apply widths/heights */
337 but= block->buttons.first;
340 // signal for aligning code
341 but->flag |= UI_BUT_ALIGN_DOWN;
347 height= (maxh[i]*(block->maxy-block->miny))/totmaxh;
352 but->y1= starty+but->aspect;
353 but->y2= but->y1+height-but->aspect;
355 but->x2= (but->x2*(block->maxx-block->minx))/maxw[i];
356 but->x1= startx+but->aspect;
359 but->x2+= but->x1-but->aspect;
366 uiBlockEndAlign(block);
368 MEM_freeN(maxw); MEM_freeN(maxh);
372 /* ************** LINK LINE DRAWING ************* */
374 /* link line drawing is not part of buttons or theme.. so we stick with it here */
376 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
378 float vec1[2], vec2[2];
380 if(line->from==NULL || line->to==NULL) return;
382 vec1[0]= (line->from->x1+line->from->x2)/2.0;
383 vec1[1]= (line->from->y1+line->from->y2)/2.0;
384 vec2[0]= (line->to->x1+line->to->x2)/2.0;
385 vec2[1]= (line->to->y1+line->to->y2)/2.0;
387 if(line->flag & UI_SELECT) UI_ThemeColorShade(but->themecol, 80);
388 else glColor3ub(0,0,0);
389 fdrawline(vec1[0], vec1[1], vec2[0], vec2[1]);
392 static void ui_draw_links(uiBlock *block)
397 but= block->buttons.first;
399 if(but->type==LINK && but->link) {
400 line= but->link->lines.first;
402 ui_draw_linkline(but, line);
410 /* ************** BLOCK ENDING FUNCTION ************* */
412 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
414 /* various properties are being compared here, hopfully sufficient
415 * to catch all cases, but it is simple to add more checks later */
416 if(but->retval != oldbut->retval) return 0;
417 if(but->poin != oldbut->poin || but->pointype != oldbut->pointype) return 0;
418 if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
419 if(but->rnaprop != oldbut->rnaprop)
420 if(but->rnaindex != oldbut->rnaindex) return 0;
421 if(but->func != oldbut->func) return 0;
422 if(but->func_arg1 != oldbut->func_arg1) return 0;
423 if(but->func_arg2 != oldbut->func_arg2) return 0;
428 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
434 oldblock= block->oldblock;
438 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
439 if(ui_but_equals_old(oldbut, but)) {
441 but->flag= oldbut->flag;
442 but->active= oldbut->active;
443 but->pos= oldbut->pos;
444 but->editstr= oldbut->editstr;
445 but->editval= oldbut->editval;
446 but->editvec= oldbut->editvec;
447 but->editcoba= oldbut->editcoba;
448 but->editcumap= oldbut->editcumap;
449 but->selsta= oldbut->selsta;
450 but->selend= oldbut->selend;
453 oldbut->active= NULL;
456 /* ensures one button can get activated, and in case the buttons
457 * draw are the same this gives O(1) lookup for each button */
458 BLI_remlink(&oldblock->buttons, oldbut);
459 ui_free_but(C, oldbut);
468 void uiEndBlock(const bContext *C, uiBlock *block)
472 /* inherit flags from 'old' buttons that was drawn here previous, based
473 * on matching buttons, we need this to make button event handling non
474 * blocking, while still alowing buttons to be remade each redraw as it
475 * is expected by blender code */
476 for(but=block->buttons.first; but; but=but->next)
477 if(ui_but_update_from_old_block(C, block, but))
480 if(block->oldblock) {
481 block->auto_open= block->oldblock->auto_open;
482 block->auto_open_last= block->oldblock->auto_open_last;
483 block->tooltipdisabled= block->oldblock->tooltipdisabled;
485 uiFreeBlock(C, block->oldblock);
486 block->oldblock= NULL;
489 /* handle pending stuff */
490 if(block->autofill) ui_autofill(block);
491 if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
492 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
495 /* ************** BLOCK DRAWING FUNCTION ************* */
497 void uiDrawBlock(uiBlock *block)
501 /* we set this only once */
502 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
504 /* XXX 2.50 no panels yet */
505 //uiPanelPush(block); // panel matrix
507 if(block->flag & UI_BLOCK_LOOP) {
508 uiDrawMenuBox(block->minx, block->miny, block->maxx, block->maxy, block->flag);
511 /* XXX 2.50 no panels yet */
512 //if(block->panel) ui_draw_panel(block);
515 /* XXX 2.50 need context here? */
516 //if(block->drawextra) block->drawextra(curarea, block);
518 for (but= block->buttons.first; but; but= but->next)
521 ui_draw_links(block);
523 /* XXX 2.50 no panels yet */
524 //uiPanelPop(block); // matrix restored
527 /* ************* EVENTS ************* */
529 static void ui_is_but_sel(uiBut *but)
533 short push=0, true=1;
535 value= ui_get_but_val(but);
537 if( but->type==TOGN || but->type==ICONTOGN) true= 0;
541 if( BTST(lvalue, (but->bitnr)) ) push= true;
550 if (value==-1) push= 1;
557 if(value!=but->min) push= 1;
561 if(value==0.0) push= 1;
564 if(value == but->max) push= 1;
576 else if(push==1) but->flag |= UI_SELECT;
577 else but->flag &= ~UI_SELECT;
580 /* XXX 2.50 no links supported yet */
583 static uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
587 /* find button to link to */
588 for (bt= block->buttons.first; bt; bt= bt->next)
589 if(bt!=but && uibut_contains_pt(bt, mval))
593 if (but->type==LINK && bt->type==INLINK) {
594 if( but->link->tocode == (int)bt->min ) {
598 else if(but->type==INLINK && bt->type==LINK) {
599 if( bt->link->tocode == (int)but->min ) {
608 static int ui_is_a_link(uiBut *from, uiBut *to)
615 line= link->lines.first;
617 if(line->from==from && line->to==to) return 1;
624 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
628 but= block->buttons.first;
630 if(but->type==INLINK) {
631 if(but->poin == poin) return but;
638 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
642 line= MEM_callocN(sizeof(uiLinkLine), "linkline");
643 BLI_addtail(listb, line);
648 uiBut *uiFindInlink(uiBlock *block, void *poin)
650 return ui_find_inlink(block, poin);
653 void uiComposeLinks(uiBlock *block)
660 but= block->buttons.first;
662 if(but->type==LINK) {
665 /* for all pointers in the array */
669 for(a=0; a < *(link->totlink); a++) {
670 bt= ui_find_inlink(block, (*ppoin)[a] );
672 ui_add_link_line(&link->lines, but, bt);
676 else if(link->poin) {
677 bt= ui_find_inlink(block, *(link->poin) );
679 ui_add_link_line(&link->lines, but, bt);
688 static void ui_add_link(uiBut *from, uiBut *to)
690 /* in 'from' we have to add a link to 'to' */
695 if(ui_is_a_link(from, to)) {
696 printf("already exists\n");
702 /* are there more pointers allowed? */
704 oldppoin= *(link->ppoin);
706 (*(link->totlink))++;
707 *(link->ppoin)= MEM_callocN( *(link->totlink)*sizeof(void *), "new link");
709 for(a=0; a< (*(link->totlink))-1; a++) {
710 (*(link->ppoin))[a]= oldppoin[a];
712 (*(link->ppoin))[a]= to->poin;
714 if(oldppoin) MEM_freeN(oldppoin);
717 *(link->poin)= to->poin;
722 static int ui_do_but_LINK(uiBlock *block, uiBut *but)
725 * This button only visualizes, the dobutton mode
726 * can add a new link, but then the whole system
727 * should be redrawn/initialized.
730 uiBut *bt=0, *bto=NULL;
731 short sval[2], mval[2], mvalo[2], first= 1;
733 uiGetMouse(curarea->win, sval);
737 while (get_mbut() & L_MOUSE) {
738 uiGetMouse(curarea->win, mval);
740 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || first) {
741 /* clear completely, because of drawbuttons */
742 bt= ui_get_valid_link_button(block, but, mval);
744 bt->flag |= UI_ACTIVE;
748 bto->flag &= ~UI_ACTIVE;
754 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
756 glutil_draw_front_xor_line(sval[0], sval[1], mval[0], mval[1]);
763 else UI_wait_for_statechange();
767 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
771 if(but->type==LINK) ui_add_link(but, bt);
772 else ui_add_link(bt, but);
774 scrarea_queue_winredraw(curarea);
781 /* ************************************************ */
783 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
786 if(val) block->lockstr= lockstr;
789 void uiBlockClearButLock(uiBlock *block)
792 block->lockstr= NULL;
795 /* *************************************************************** */
797 /* XXX 2.50 no button editing */
800 static void setup_file(uiBlock *block)
805 fp= fopen("butsetup","w");
808 but= block->buttons.first;
811 fprintf(fp,"%d,%d,%d,%d %s %s\n", (int)but->x1, (int)but->y1, (int)( but->x2-but->x1), (int)(but->y2-but->y1), but->str, but->tip);
819 static void edit_but(uiBlock *block, uiBut *but, uiEvent *uevent)
821 short dx, dy, mval[2], mvalo[2], didit=0;
823 getmouseco_sc(mvalo);
825 if( !(get_mbut() & L_MOUSE) ) break;
828 dx= (mval[0]-mvalo[0]);
829 dy= (mval[1]-mvalo[1]);
836 glRectf(but->x1-2, but->y1-2, but->x2+2, but->y2+2);
838 if((uevent->qual & LR_SHIFTKEY)==0) {
846 ui_block_flush_back(but->block);
850 /* idle for this poor code */
851 else PIL_sleep_ms(30);
853 if(didit) setup_file(block);
857 /* XXX 2.50 no links supported yet */
859 static void ui_delete_active_linkline(uiBlock *block)
863 uiLinkLine *line, *nline;
866 but= block->buttons.first;
868 if(but->type==LINK && but->link) {
869 line= but->link->lines.first;
874 if(line->flag & UI_SELECT) {
875 BLI_remlink(&but->link->lines, line);
877 link= line->from->link;
879 /* are there more pointers allowed? */
882 if(*(link->totlink)==1) {
884 MEM_freeN(*(link->ppoin));
885 *(link->ppoin)= NULL;
889 for(a=0; a< (*(link->totlink)); a++) {
891 if( (*(link->ppoin))[a] != line->to->poin ) {
892 (*(link->ppoin))[b]= (*(link->ppoin))[a];
896 (*(link->totlink))--;
911 /* temporal! these buttons can be everywhere... */
912 allqueue(REDRAWBUTSLOGIC, 0);
915 static void ui_do_active_linklines(uiBlock *block, short *mval)
918 uiLinkLine *line, *act= NULL;
919 float mindist= 12.0, fac, v1[2], v2[2], v3[3];
926 /* find a line close to the mouse */
927 but= block->buttons.first;
929 if(but->type==LINK && but->link) {
931 line= but->link->lines.first;
933 v2[0]= line->from->x2;
934 v2[1]= (line->from->y1+line->from->y2)/2.0;
936 v3[1]= (line->to->y1+line->to->y2)/2.0;
938 fac= PdistVL2Dfl(v1, v2, v3);
950 /* check for a 'found one' to prevent going to 'frontbuffer' mode.
951 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
953 glDrawBuffer(GL_FRONT);
956 but= block->buttons.first;
958 if(but->type==LINK && but->link) {
959 line= but->link->lines.first;
962 if((line->flag & UI_SELECT)==0) {
963 line->flag |= UI_SELECT;
964 ui_draw_linkline(but, line);
967 else if(line->flag & UI_SELECT) {
968 line->flag &= ~UI_SELECT;
969 ui_draw_linkline(but, line);
977 glDrawBuffer(GL_BACK);
982 /* ******************************************************* */
984 /* XXX 2.50 no screendump supported yet */
987 /* nasty but safe way to store screendump rect */
988 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
990 static void ui_set_screendump_bbox(uiBlock *block)
995 scr_sizex= block->maxx - block->minx;
996 scr_sizey= block->maxy - block->miny;
999 scr_sizex= scr_sizey= 0;
1003 /* used for making screenshots for menus, called in screendump.c */
1004 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1006 if(scr_sizex!=0 && scr_sizey!=0) {
1018 /* *********************** data get/set ***********************
1019 * this either works with the pointed to data, or can work with
1020 * an edit override pointer while dragging for example */
1022 /* for buttons pointing to color for example */
1023 void ui_get_but_vectorf(uiBut *but, float *vec)
1028 poin= (but->editvec)? (void*)but->editvec: but->poin;
1029 pointype= (but->editvec)? FLO: but->pointype;
1031 if(!but->editvec && pointype == CHA) {
1032 char *cp= (char *)poin;
1033 vec[0]= ((float)cp[0])/255.0;
1034 vec[1]= ((float)cp[1])/255.0;
1035 vec[2]= ((float)cp[2])/255.0;
1037 else if(pointype == FLO) {
1038 float *fp= (float *)poin;
1043 /* for buttons pointing to color for example */
1044 void ui_set_but_vectorf(uiBut *but, float *vec)
1049 poin= (but->editvec)? (void*)but->editvec: but->poin;
1050 pointype= (but->editvec)? FLO: but->pointype;
1052 if(!but->editvec && but->pointype == CHA) {
1053 char *cp= (char *)poin;
1054 cp[0]= (char)(0.5 +vec[0]*255.0);
1055 cp[1]= (char)(0.5 +vec[1]*255.0);
1056 cp[2]= (char)(0.5 +vec[2]*255.0);
1058 else if( but->pointype == FLO ) {
1059 float *fp= (float *)poin;
1064 int ui_is_but_float(uiBut *but)
1066 if(but->pointype==FLO && but->poin)
1069 if(but->rnaprop && RNA_property_type(&but->rnapoin, but->rnaprop) == PROP_FLOAT)
1075 double ui_get_but_val(uiBut *but)
1080 if(but->editval) { return *(but->editval); }
1081 if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1086 switch(RNA_property_type(&but->rnapoin, prop)) {
1088 if(RNA_property_array_length(&but->rnapoin, prop))
1089 value= RNA_property_boolean_get_array(&but->rnapoin, prop, but->rnaindex);
1091 value= RNA_property_boolean_get(&but->rnapoin, prop);
1094 if(RNA_property_array_length(&but->rnapoin, prop))
1095 value= RNA_property_int_get_array(&but->rnapoin, prop, but->rnaindex);
1097 value= RNA_property_int_get(&but->rnapoin, prop);
1100 if(RNA_property_array_length(&but->rnapoin, prop))
1101 value= RNA_property_float_get_array(&but->rnapoin, prop, but->rnaindex);
1103 value= RNA_property_float_get(&but->rnapoin, prop);
1106 value= RNA_property_enum_get(&but->rnapoin, prop);
1113 else if(but->type== HSVSLI) {
1116 fp= (but->editvec)? but->editvec: (float *)but->poin;
1117 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1119 switch(but->str[0]) {
1120 case 'H': value= h; break;
1121 case 'S': value= s; break;
1122 case 'V': value= v; break;
1125 else if( but->pointype == CHA ) {
1126 value= *(char *)but->poin;
1128 else if( but->pointype == SHO ) {
1129 value= *(short *)but->poin;
1131 else if( but->pointype == INT ) {
1132 value= *(int *)but->poin;
1134 else if( but->pointype == FLO ) {
1135 value= *(float *)but->poin;
1141 void ui_set_but_val(uiBut *but, double value)
1145 /* value is a hsv value: convert to rgb */
1149 if(RNA_property_editable(&but->rnapoin, prop)) {
1150 switch(RNA_property_type(&but->rnapoin, prop)) {
1152 if(RNA_property_array_length(&but->rnapoin, prop))
1153 RNA_property_boolean_set_array(&but->rnapoin, prop, but->rnaindex, value);
1155 RNA_property_boolean_set(&but->rnapoin, prop, value);
1158 if(RNA_property_array_length(&but->rnapoin, prop))
1159 RNA_property_int_set_array(&but->rnapoin, prop, but->rnaindex, value);
1161 RNA_property_int_set(&but->rnapoin, prop, value);
1164 if(RNA_property_array_length(&but->rnapoin, prop))
1165 RNA_property_float_set_array(&but->rnapoin, prop, but->rnaindex, value);
1167 RNA_property_float_set(&but->rnapoin, prop, value);
1170 RNA_property_enum_set(&but->rnapoin, prop, value);
1177 else if(but->pointype==0);
1178 else if(but->type==HSVSLI ) {
1181 fp= (but->editvec)? but->editvec: (float *)but->poin;
1182 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1184 switch(but->str[0]) {
1185 case 'H': h= value; break;
1186 case 'S': s= value; break;
1187 case 'V': v= value; break;
1190 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1194 /* first do rounding */
1195 if(but->pointype==CHA)
1196 value= (char)floor(value+0.5);
1197 else if(but->pointype==SHO ) {
1198 /* gcc 3.2.1 seems to have problems
1199 * casting a double like 32772.0 to
1200 * a short so we cast to an int, then
1203 gcckludge = (int) floor(value+0.5);
1204 value= (short)gcckludge;
1206 else if(but->pointype==INT )
1207 value= (int)floor(value+0.5);
1208 else if(but->pointype==FLO ) {
1209 float fval= (float)value;
1210 if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f; /* prevent negative zero */
1214 /* then set value with possible edit override */
1216 *but->editval= value;
1217 else if(but->pointype==CHA)
1218 *((char *)but->poin)= (char)value;
1219 else if(but->pointype==SHO)
1220 *((short *)but->poin)= (short)value;
1221 else if(but->pointype==INT)
1222 *((int *)but->poin)= (int)value;
1223 else if(but->pointype==FLO)
1224 *((float *)but->poin)= (float)value;
1227 /* update select flag */
1231 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1236 buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1239 /* string was too long, we have to truncate */
1240 BLI_strncpy(str, buf, maxlen);
1245 BLI_strncpy(str, but->poin, maxlen);
1249 void ui_set_but_string(uiBut *but, const char *str)
1252 if(RNA_property_editable(&but->rnapoin, but->rnaprop))
1253 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1256 BLI_strncpy(but->poin, str, but->max);
1259 /* ******************* Font ********************/
1261 static void ui_set_ftf_font(float aspect)
1263 #ifdef INTERNATIONAL
1265 FTF_SetFontSize('l');
1267 else if(aspect<1.59) {
1268 FTF_SetFontSize('m');
1271 FTF_SetFontSize('s');
1276 void uiSetCurFont(uiBlock *block, int index)
1278 ui_set_ftf_font(block->aspect);
1280 if(block->aspect<0.60) {
1281 block->curfont= UIfont[index].xl;
1283 else if(block->aspect<1.15) {
1284 block->curfont= UIfont[index].large;
1286 else if(block->aspect<1.59) {
1287 block->curfont= UIfont[index].medium;
1290 block->curfont= UIfont[index].small;
1293 if(block->curfont==NULL) block->curfont= UIfont[index].large;
1294 if(block->curfont==NULL) block->curfont= UIfont[index].medium;
1295 if(block->curfont==NULL) printf("error block no font %s\n", block->name);
1299 /* called by node editor */
1300 void *uiSetCurFont_ext(float aspect)
1304 ui_set_ftf_font(aspect);
1307 curfont= UIfont[0].xl;
1309 else if(aspect<1.15) {
1310 curfont= UIfont[0].large;
1312 else if(aspect<1.59) {
1313 curfont= UIfont[0].medium;
1316 curfont= UIfont[0].small;
1319 if(curfont==NULL) curfont= UIfont[0].large;
1320 if(curfont==NULL) curfont= UIfont[0].medium;
1325 void uiDefFont(unsigned int index, void *xl, void *large, void *medium, void *small)
1327 if(index>=UI_ARRAY) return;
1329 UIfont[index].xl= xl;
1330 UIfont[index].large= large;
1331 UIfont[index].medium= medium;
1332 UIfont[index].small= small;
1335 /* ******************* Free ********************/
1337 static void ui_free_link(uiLink *link)
1340 BLI_freelistN(&link->lines);
1345 static void ui_free_but(const bContext *C, uiBut *but)
1347 if(but->active) ui_button_active_cancel(C, but);
1348 if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1349 ui_free_link(but->link);
1354 void uiFreeBlock(const bContext *C, uiBlock *block)
1358 while( (but= block->buttons.first) ) {
1359 BLI_remlink(&block->buttons, but);
1360 ui_free_but(C, but);
1363 if(block->panel) block->panel->active= 0;
1364 BLI_freelistN(&block->saferct);
1370 void uiFreeBlocks(const bContext *C, ListBase *lb)
1374 while( (block= lb->first) ) {
1375 BLI_remlink(lb, block);
1376 uiFreeBlock(C, block);
1380 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, char *name, short dt, short font)
1383 uiBlock *block, *oldblock= NULL;
1385 int getsizex, getsizey;
1388 lb= ®ion->uiblocks;
1390 /* each listbase only has one block with this name, free block
1391 * if is already there so it can be rebuilt from scratch */
1393 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1394 if (BLI_streq(oldblock->name, name))
1398 BLI_remlink(lb, oldblock);
1401 block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1402 block->oldblock= oldblock;
1404 /* at the beginning of the list! for dynamical menus/blocks */
1406 BLI_addhead(lb, block);
1408 BLI_strncpy(block->name, name, sizeof(block->name));
1413 /* window where queue event should be added, pretty weak this way!
1414 this is because the 'mainwin' pup menu's */
1415 block->winq= mywinget();
1419 block->themecol= TH_AUTO;
1421 /* window matrix and aspect */
1422 if(region->swinid) {
1423 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1424 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1426 /* TODO - investigate why block->winmat[0][0] is negative
1427 * in the image view when viewRedrawForce is called */
1428 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1431 /* no subwindow created yet, for menus for example, so we
1432 * use the main window instead, since buttons are created
1434 wm_subwindow_getmatrix(window, window->winid, block->winmat);
1435 wm_subwindow_getsize(window, window->winid, &getsizex, &getsizey);
1437 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1438 block->auto_open= 2;
1441 uiSetCurFont(block, font);
1446 uiBlock *uiGetBlock(char *name, ARegion *ar)
1448 uiBlock *block= ar->uiblocks.first;
1451 if( strcmp(name, block->name)==0 ) return block;
1458 void ui_check_but(uiBut *but)
1460 /* if something changed in the button */
1464 int transopts= ui_translate_buttons();
1469 if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1471 /* test for min and max, icon sliders, etc */
1472 switch( but->type ) {
1478 value= ui_get_but_val(but);
1479 if(value < but->min) ui_set_but_val(but, but->min);
1480 else if(value > but->max) ui_set_but_val(but, but->max);
1484 value= fabs( ui_get_but_val(but) );
1485 if(value < but->min) ui_set_but_val(but, but->min);
1486 else if(value > but->max) ui_set_but_val(but, but->max);
1491 if(but->flag & UI_SELECT) but->iconadd= 1;
1492 else but->iconadd= 0;
1496 value= ui_get_but_val(but);
1497 but->iconadd= (int)value- (int)(but->min);
1501 value= ui_get_but_val(but);
1502 but->iconadd= (int)value- (int)(but->min);
1507 /* safety is 4 to enable small number buttons (like 'users') */
1508 if(but->type==NUMSLI || but->type==HSVSLI)
1509 okwidth= -4 + (but->x2 - but->x1)/2.0;
1511 okwidth= -4 + (but->x2 - but->x1);
1514 switch( but->type ) {
1519 if(but->x2 - but->x1 > 24) {
1520 value= ui_get_but_val(but);
1521 ui_set_name_menu(but, (int)value);
1530 value= ui_get_but_val(but);
1532 if(ui_is_but_float(but)) {
1533 if(value == FLT_MAX) sprintf(but->drawstr, "%sFLT_MAX", but->str);
1534 else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-FLT_MAX", but->str);
1535 else if(but->a2) { /* amount of digits defined */
1536 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1537 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1538 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1539 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1542 if(but->max<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1543 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1547 sprintf(but->drawstr, "%s%d", but->str, (int)value);
1552 if(ui_is_but_float(but)) {
1553 value= ui_get_but_val(but);
1554 if(but->a2) { /* amount of digits defined */
1555 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1556 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1557 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1558 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1561 sprintf(but->drawstr, "%s%.2f", but->str, value);
1564 else strcpy(but->drawstr, but->str);
1569 id= *(but->idpoin_idpp);
1570 strcpy(but->drawstr, but->str);
1571 if(id) strcat(but->drawstr, id->name+2);
1576 char str[UI_MAX_DRAW_STR];
1578 ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1580 strcpy(but->drawstr, but->str);
1581 strcat(but->drawstr, str);
1586 strcpy(but->drawstr, but->str);
1587 if (but->flag & UI_SELECT) {
1588 strcat(but->drawstr, "Press a key");
1590 /* XXX 2.50 function not supported */
1591 /* strcat(but->drawstr, key_event_to_string((short) ui_get_but_val(but))); */
1596 /* trying to get the dual-icon to left of text... not very nice */
1598 strcpy(but->drawstr, " ");
1599 strcpy(but->drawstr+2, but->str);
1603 strcpy(but->drawstr, but->str);
1607 /* if we are doing text editing, this will override the drawstr */
1609 strcpy(but->drawstr, but->str);
1610 strcat(but->drawstr, but->editstr);
1613 if(but->drawstr[0]) {
1614 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr, transopts);
1615 // here should be check for less space for icon offsets...
1616 if(but->type==MENU) okwidth -= 15;
1621 /* automatic width */
1622 if(but->x2==0.0f && but->x1 > 0.0f) {
1623 but->x2= (but->x1+but->strwidth+6);
1626 if(but->strwidth==0) but->drawstr[0]= 0;
1627 else if(but->type==BUTM || but->type==BLOCK); // no clip string, uiTextBoundsBlock is used (hack!)
1630 /* calc but->ofs, to draw the string shorter if too long */
1633 while(but->strwidth > (int)okwidth ) {
1635 if ELEM3(but->type, NUM, NUMABS, TEX) { // only these cut off left
1637 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr+but->ofs, transopts);
1639 /* textbut exception */
1640 if(but->editstr && but->pos != -1) {
1641 pos= but->pos+strlen(but->str);
1642 if(pos-1 < but->ofs) {
1643 pos= but->ofs-pos+1;
1649 but->drawstr[ strlen(but->drawstr)-pos ]= 0;
1654 but->drawstr[ strlen(but->drawstr)-1 ]= 0;
1655 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr, transopts);
1658 if(but->strwidth < 10) break;
1663 static int ui_auto_themecol(uiBut *but)
1668 return TH_BUT_ACTION;
1675 return TH_BUT_SETTING;
1683 return TH_BUT_TEXTFIELD;
1688 // (weak!) detect if it is a blockloop
1689 if(but->block->dt == UI_EMBOSSP) return TH_MENU_ITEM;
1690 return TH_BUT_POPUP;
1694 return TH_BUT_NEUTRAL;
1698 void uiBlockBeginAlign(uiBlock *block)
1700 /* if other align was active, end it */
1701 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1703 block->flag |= UI_BUT_ALIGN_DOWN;
1704 /* buttons declared after this call will this align flag */
1707 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1711 dx= fabs( but1->x2 - but2->x1);
1712 dy= fabs( but1->y1 - but2->y2);
1714 if(dx > dy) return 0;
1718 void uiBlockEndAlign(uiBlock *block)
1720 uiBut *prev, *but=NULL, *next;
1721 int flag= 0, cols=0, rows=0;
1722 int theme= UI_GetThemeValue(TH_BUT_DRAWTYPE);
1724 if ( !(ELEM3(theme, TH_MINIMAL, TH_SHADED, TH_ROUNDED)) ) {
1725 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1730 - go back to first button of align start (ALIGN_DOWN)
1731 - compare triples, and define flags
1733 prev= block->buttons.last;
1735 if( (prev->flag & UI_BUT_ALIGN_DOWN)) but= prev;
1738 if(but && but->next) {
1739 if(buts_are_horiz(but, but->next)) cols++;
1745 if(but==NULL) return;
1747 /* rows==0: 1 row, cols==0: 1 collumn */
1749 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1754 /* clear old flag */
1755 but->flag &= ~UI_BUT_ALIGN_DOWN;
1757 if(flag==0) { /* first case */
1759 if(buts_are_horiz(but, next)) {
1761 flag= UI_BUT_ALIGN_RIGHT;
1763 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
1766 flag= UI_BUT_ALIGN_DOWN;
1770 else if(next==NULL) { /* last case */
1772 if(buts_are_horiz(prev, but)) {
1774 flag= UI_BUT_ALIGN_LEFT;
1776 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
1778 else flag= UI_BUT_ALIGN_TOP;
1781 else if(buts_are_horiz(but, next)) {
1782 /* check if this is already second row */
1783 if( prev && buts_are_horiz(prev, but)==0) {
1784 flag |= UI_BUT_ALIGN_TOP;
1785 /* exception case: bottom row */
1789 if(bt->next && buts_are_horiz(bt, bt->next)==0 ) break;
1792 if(bt==0) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
1795 else flag |= UI_BUT_ALIGN_LEFT;
1799 flag |= UI_BUT_ALIGN_TOP;
1801 else { /* next button switches to new row */
1802 if( (flag & UI_BUT_ALIGN_TOP)==0) { /* stil top row */
1804 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
1806 flag |= UI_BUT_ALIGN_DOWN;
1809 flag |= UI_BUT_ALIGN_TOP;
1815 /* merge coordinates */
1819 but->x1= (prev->x2+but->x1)/2.0;
1823 but->y2= (prev->y1+but->y2)/2.0;
1827 if(buts_are_horiz(prev, but)) {
1828 but->x1= (prev->x2+but->x1)/2.0;
1830 /* copy height too */
1833 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
1834 /* the previous button is a single one in its row */
1835 but->y2= (prev->y1+but->y2)/2.0;
1839 /* the previous button is not a single one in its row */
1849 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1853 static void uiBlockEndAligno(uiBlock *block)
1857 /* correct last defined button */
1858 but= block->buttons.last;
1860 /* vertical align case */
1861 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_DOWN) ) {
1862 but->flag &= ~UI_BUT_ALIGN_DOWN;
1864 /* horizontal align case */
1865 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT) ) {
1866 but->flag &= ~UI_BUT_ALIGN_RIGHT;
1868 /* else do nothing, manually provided flags */
1870 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1875 ui_def_but is the function that draws many button types
1878 "a1" Click Step (how much to change the value each click)
1879 "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
1880 all greater values will be clamped to 4.
1883 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)
1888 if(type & BUTPOIN) { /* a pointer is required */
1890 /* if pointer is zero, button is removed and not drawn */
1891 UI_ThemeColor(block->themecol);
1892 glRects(x1, y1, x1+x2, y1+y2);
1897 but= MEM_callocN(sizeof(uiBut), "uiBut");
1899 but->type= type & BUTTYPE;
1900 but->pointype= type & BUTPOIN;
1901 but->bit= type & BIT;
1902 but->bitnr= type & 31;
1905 BLI_addtail(&block->buttons, but);
1907 but->retval= retval;
1908 if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
1909 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
1910 strcpy(but->str, str);
1913 but->str= but->strdata;
1914 strcpy(but->str, str);
1918 if(block->autofill) {
1933 but->font= block->curfont;
1935 but->lock= block->lock;
1936 but->lockstr= block->lockstr;
1938 but->aspect= block->aspect;
1939 but->win= block->win;
1940 but->block= block; // pointer back, used for frontbuffer status, and picker
1942 if(block->themecol==TH_AUTO) but->themecol= ui_auto_themecol(but);
1943 else but->themecol= block->themecol;
1945 if(but->type != BUTM) {
1946 but->func= block->func;
1947 but->func_arg1= block->func_arg1;
1948 but->func_arg2= block->func_arg2;
1951 but->handle_func= block->handle_func;
1952 but->handle_func_arg= block->handle_func_arg;
1954 ui_set_embossfunc(but, block->dt);
1956 but->pos= -1; /* cursor invisible */
1958 if(ELEM(but->type, NUM, NUMABS)) { /* add a space to name */
1959 slen= strlen(but->str);
1960 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
1961 if(but->str[slen-1]!=' ') {
1962 but->str[slen]= ' ';
1963 but->str[slen+1]= 0;
1968 if(but->type==HSVCUBE) { /* hsv buttons temp storage */
1970 ui_get_but_vectorf(but, rgb);
1971 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
1974 if ELEM8(but->type, HSVSLI , NUMSLI, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM) {
1975 but->flag |= UI_TEXT_LEFT;
1978 if(but->type==BUT_TOGDUAL) {
1979 but->flag |= UI_ICON_LEFT;
1982 if(but->type==ROUNDBOX)
1983 but->flag |= UI_NO_HILITE;
1985 but->flag |= (block->flag & UI_BUT_ALIGN);
1986 if(block->flag & UI_BLOCK_NO_HILITE)
1987 but->flag |= UI_NO_HILITE;
1992 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)
1994 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2001 /* if _x_ is a power of two (only one bit) return the power,
2002 * otherwise return -1.
2003 * (1<<findBitIndex(x))==x for powers of two.
2005 static int findBitIndex(unsigned int x) {
2006 if (!x || (x&(x-1))!=0) { /* x&(x-1) strips lowest bit */
2011 if (x&0xFFFF0000) idx+=16, x>>=16;
2012 if (x&0xFF00) idx+=8, x>>=8;
2013 if (x&0xF0) idx+=4, x>>=4;
2014 if (x&0xC) idx+=2, x>>=2;
2021 /* autocomplete helper functions */
2022 struct AutoComplete {
2028 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2030 AutoComplete *autocpl;
2032 autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2033 autocpl->maxlen= maxlen;
2034 autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2035 autocpl->startname= startname;
2040 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2042 char *truncate= autocpl->truncate;
2043 char *startname= autocpl->startname;
2046 for(a=0; a<autocpl->maxlen-1; a++) {
2047 if(startname[a]==0 || startname[a]!=name[a])
2051 if(startname[a]==0) {
2054 BLI_strncpy(truncate, name, autocpl->maxlen);
2056 /* remove from truncate what is not in bone->name */
2057 for(a=0; a<autocpl->maxlen-1; a++) {
2058 if(truncate[a]!=name[a])
2065 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2067 if(autocpl->truncate[0])
2068 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2070 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2072 MEM_freeN(autocpl->truncate);
2076 /* autocomplete callback for ID buttons */
2077 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2079 /* int blocktype= (intptr_t)arg_v; */
2080 ListBase *listb= NULL /* XXX 2.50 needs context, wich_libbase(G.main, blocktype) */;
2082 if(listb==NULL) return;
2084 /* search if str matches the beginning of an ID struct */
2086 AutoComplete *autocpl= autocomplete_begin(str, 22);
2089 for(id= listb->first; id; id= id->next)
2090 autocomplete_do_name(autocpl, id->name+2);
2092 autocomplete_end(autocpl, str);
2096 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)
2098 int bitIdx= findBitIndex(bit);
2102 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2105 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)
2107 return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2109 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)
2111 return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2113 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)
2115 return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2117 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)
2119 return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2121 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)
2123 return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2125 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)
2127 return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2129 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)
2131 return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2133 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)
2135 return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2138 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)
2140 uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2142 but->icon= (BIFIconID) icon;
2143 but->flag|= UI_HAS_ICON;
2149 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)
2151 int bitIdx= findBitIndex(bit);
2155 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2159 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)
2161 return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2163 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)
2165 return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2167 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)
2169 return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2171 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)
2173 return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2175 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)
2177 return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2179 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)
2181 return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2183 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)
2185 return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2187 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)
2189 return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2192 /* Button containing both string label and icon */
2193 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)
2195 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2197 but->icon= (BIFIconID) icon;
2198 but->flag|= UI_HAS_ICON;
2200 but->flag|= UI_ICON_LEFT;
2206 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)
2208 int bitIdx= findBitIndex(bit);
2212 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2216 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)
2218 return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2220 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)
2222 return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2224 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)
2226 return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2228 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)
2230 return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2232 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)
2234 return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2236 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)
2238 return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2240 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)
2242 return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2244 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)
2246 return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2249 uiBut *uiDefRNABut(uiBlock *block, int retval, PointerRNA *ptr, PropertyRNA *prop, int index, short x1, short y1, short x2, short y2)
2253 switch(RNA_property_type(ptr, prop)) {
2254 case PROP_BOOLEAN: {
2257 length= RNA_property_array_length(ptr, prop);
2260 value= RNA_property_boolean_get_array(ptr, prop, index);
2262 value= RNA_property_boolean_get(ptr, prop);
2264 but= ui_def_but(block, TOG, 0, (value)? "True": "False", x1, y1, x2, y2, NULL, 0, 0, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2268 int softmin, softmax, step;
2270 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2271 but= ui_def_but(block, NUM, 0, "", x1, y1, x2, y2, NULL, softmin, softmax, step, 0, (char*)RNA_property_ui_description(ptr, prop));
2275 float softmin, softmax, step, precision;
2277 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2278 but= ui_def_but(block, NUM, 0, "", x1, y1, x2, y2, NULL, softmin, softmax, step, precision, (char*)RNA_property_ui_description(ptr, prop));
2282 const EnumPropertyItem *item;
2287 RNA_property_enum_items(ptr, prop, &item, &totitem);
2289 dynstr= BLI_dynstr_new();
2290 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(ptr, prop));
2291 for(i=0; i<totitem; i++)
2292 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2293 menu= BLI_dynstr_get_cstring(dynstr);
2294 BLI_dynstr_free(dynstr);
2296 but= ui_def_but(block, MENU, 0, menu, x1, y1, x2, y2, NULL, 0, 0, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2303 maxlength= RNA_property_string_maxlength(ptr, prop);
2305 /* interface code should ideally support unlimited length */
2306 maxlength= UI_MAX_DRAW_STR;
2308 but= ui_def_but(block, TEX, 0, "", x1, y1, x2, y2, NULL, 0, maxlength, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2311 case PROP_POINTER: {
2313 PropertyRNA *nameprop;
2314 char name[256]= "", *nameptr= name;
2316 RNA_property_pointer_get(ptr, prop, &pptr);
2319 nameprop= RNA_struct_name_property(&pptr);
2320 if(pptr.type && nameprop)
2321 nameptr= RNA_property_string_get_alloc(&pptr, nameprop, name, sizeof(name));
2323 strcpy(nameptr, "->");
2326 but= ui_def_but(block, BUT, 0, nameptr, x1, y1, x2, y2, NULL, 0, 0, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2327 but->flag |= UI_TEXT_LEFT;
2342 but->rnaindex= index;
2350 /* END Button containing both string label and icon */
2352 void uiAutoBlock(uiBlock *block, float minx, float miny, float sizex, float sizey, int flag)
2355 block->maxx= minx+sizex;
2357 block->maxy= miny+sizey;
2359 block->autofill= flag; /* also check for if it has to be done */
2363 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2367 link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2372 link->fromcode= from;
2376 /* cruft to make uiBlock and uiBut private */
2378 int uiBlocksGetYMin(ListBase *lb)
2383 for (block= lb->first; block; block= block->next)
2384 if (block==lb->first || block->miny<min)
2390 int uiBlockGetCol(uiBlock *block)
2392 return block->themecol;
2394 void uiBlockSetCol(uiBlock *block, int col)
2396 block->themecol= col;
2398 void uiBlockSetEmboss(uiBlock *block, int emboss)
2402 void uiBlockSetDirection(uiBlock *block, int direction)
2404 block->direction= direction;
2407 /* this call escapes if there's alignment flags */
2408 void uiBlockFlipOrder(uiBlock *block)
2412 float centy, miny=10000, maxy= -10000;
2414 // if(U.uiflag & USER_PLAINMENUS)
2417 for(but= block->buttons.first; but; but= but->next) {
2418 if(but->flag & UI_BUT_ALIGN) return;
2419 if(but->y1 < miny) miny= but->y1;
2420 if(but->y2 > maxy) maxy= but->y2;
2423 centy= (miny+maxy)/2.0;
2424 for(but= block->buttons.first; but; but= but->next) {
2425 but->y1 = centy-(but->y1-centy);
2426 but->y2 = centy-(but->y2-centy);
2427 SWAP(float, but->y1, but->y2);
2430 /* also flip order in block itself, for example for arrowkey */
2431 lb.first= lb.last= NULL;
2432 but= block->buttons.first;
2435 BLI_remlink(&block->buttons, but);
2436 BLI_addtail(&lb, but);
2443 void uiBlockSetFlag(uiBlock *block, int flag)
2447 void uiBlockSetXOfs(uiBlock *block, int xofs)
2451 void* uiBlockGetCurFont(uiBlock *block)
2453 return block->curfont;
2456 void uiButSetFlag(uiBut *but, int flag)
2460 void uiButClearFlag(uiBut *but, int flag)
2465 int uiButGetRetVal(uiBut *but)
2470 void uiBlockSetHandleFunc(uiBlock *block, void (*func)(struct bContext *C, void *arg, int event), void *arg)
2472 block->handle_func= func;
2473 block->handle_func_arg= arg;
2476 void uiBlockSetButmFunc(uiBlock *block, void (*func)(struct bContext *C, void *arg, int but_a2), void *arg)
2478 block->butm_func= func;
2479 block->butm_func_arg= arg;
2482 void uiBlockSetFunc(uiBlock *block, void (*func)(struct bContext *C, void *arg1, void *arg2), void *arg1, void *arg2)
2485 block->func_arg1= arg1;
2486 block->func_arg2= arg2;
2489 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)())
2491 block->drawextra= func;
2494 void uiButSetFunc(uiBut *but, void (*func)(struct bContext *C, void *arg1, void *arg2), void *arg1, void *arg2)
2497 but->func_arg1= arg1;
2498 but->func_arg2= arg2;
2501 void uiButSetCompleteFunc(uiBut *but, void (*func)(struct bContext *C, char *str, void *arg), void *arg)
2503 but->autocomplete_func= func;
2504 but->autofunc_arg= arg;
2507 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2509 uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2510 but->idpoin_func= func;
2511 but->idpoin_idpp= (ID**) idpp;
2515 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2520 uiBut *uiDefBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2522 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2523 but->block_func= func;
2528 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2530 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2531 but->block_func= func;
2536 /* Block button containing both string label and icon */
2537 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2539 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2541 but->icon= (BIFIconID) icon;
2542 but->flag|= UI_HAS_ICON;
2544 but->flag|= UI_ICON_LEFT;
2545 but->flag|= UI_ICON_RIGHT;
2547 but->block_func= func;
2553 /* Block button containing icon */
2554 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
2556 uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2558 but->icon= (BIFIconID) icon;
2559 but->flag|= UI_HAS_ICON;
2561 but->flag|= UI_ICON_LEFT;
2562 but->flag|= UI_ICON_RIGHT;
2564 but->block_func= func;
2570 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
2572 uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
2576 /* Program Init/Exit */
2581 BMF_GetFont(BMF_kHelveticaBold14),
2582 BMF_GetFont(BMF_kHelveticaBold12),
2583 BMF_GetFont(BMF_kHelveticaBold10),
2584 BMF_GetFont(BMF_kHelveticaBold8));
2586 BMF_GetFont(BMF_kHelvetica12),
2587 BMF_GetFont(BMF_kHelvetica12),
2588 BMF_GetFont(BMF_kHelvetica10),
2589 BMF_GetFont(BMF_kHelveticaBold8));
2591 ui_resources_init();
2594 void UI_init_userdef()
2596 ui_text_init_userdef();
2597 ui_theme_init_userdef();
2602 ui_resources_free();