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;
424 if(but->func_arg3 != oldbut->func_arg3) return 0;
429 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
435 oldblock= block->oldblock;
439 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
440 if(ui_but_equals_old(oldbut, but)) {
442 but->flag= oldbut->flag;
443 but->active= oldbut->active;
444 but->pos= oldbut->pos;
445 but->editstr= oldbut->editstr;
446 but->editval= oldbut->editval;
447 but->editvec= oldbut->editvec;
448 but->editcoba= oldbut->editcoba;
449 but->editcumap= oldbut->editcumap;
450 but->selsta= oldbut->selsta;
451 but->selend= oldbut->selend;
454 oldbut->active= NULL;
457 /* ensures one button can get activated, and in case the buttons
458 * draw are the same this gives O(1) lookup for each button */
459 BLI_remlink(&oldblock->buttons, oldbut);
460 ui_free_but(C, oldbut);
469 void uiEndBlock(const bContext *C, uiBlock *block)
473 /* inherit flags from 'old' buttons that was drawn here previous, based
474 * on matching buttons, we need this to make button event handling non
475 * blocking, while still alowing buttons to be remade each redraw as it
476 * is expected by blender code */
477 for(but=block->buttons.first; but; but=but->next)
478 if(ui_but_update_from_old_block(C, block, but))
481 if(block->oldblock) {
482 block->auto_open= block->oldblock->auto_open;
483 block->auto_open_last= block->oldblock->auto_open_last;
484 block->tooltipdisabled= block->oldblock->tooltipdisabled;
486 uiFreeBlock(C, block->oldblock);
487 block->oldblock= NULL;
490 /* handle pending stuff */
491 if(block->autofill) ui_autofill(block);
492 if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
493 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
496 /* ************** BLOCK DRAWING FUNCTION ************* */
498 void uiDrawBlock(uiBlock *block)
502 /* we set this only once */
503 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
505 /* XXX 2.50 no panels yet */
506 //uiPanelPush(block); // panel matrix
508 if(block->flag & UI_BLOCK_LOOP) {
509 uiDrawMenuBox(block->minx, block->miny, block->maxx, block->maxy, block->flag);
512 /* XXX 2.50 no panels yet */
513 //if(block->panel) ui_draw_panel(block);
516 /* XXX 2.50 need context here? */
517 //if(block->drawextra) block->drawextra(curarea, block);
519 for (but= block->buttons.first; but; but= but->next)
522 ui_draw_links(block);
524 /* XXX 2.50 no panels yet */
525 //uiPanelPop(block); // matrix restored
528 /* ************* EVENTS ************* */
530 static void ui_is_but_sel(uiBut *but)
534 short push=0, true=1;
536 value= ui_get_but_val(but);
538 if( but->type==TOGN || but->type==ICONTOGN) true= 0;
542 if( BTST(lvalue, (but->bitnr)) ) push= true;
551 if (value==-1) push= 1;
558 if(value!=but->min) push= 1;
562 if(value==0.0) push= 1;
565 if(value == but->max) push= 1;
577 else if(push==1) but->flag |= UI_SELECT;
578 else but->flag &= ~UI_SELECT;
581 /* XXX 2.50 no links supported yet */
584 static uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
588 /* find button to link to */
589 for (bt= block->buttons.first; bt; bt= bt->next)
590 if(bt!=but && uibut_contains_pt(bt, mval))
594 if (but->type==LINK && bt->type==INLINK) {
595 if( but->link->tocode == (int)bt->min ) {
599 else if(but->type==INLINK && bt->type==LINK) {
600 if( bt->link->tocode == (int)but->min ) {
609 static int ui_is_a_link(uiBut *from, uiBut *to)
616 line= link->lines.first;
618 if(line->from==from && line->to==to) return 1;
625 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
629 but= block->buttons.first;
631 if(but->type==INLINK) {
632 if(but->poin == poin) return but;
639 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
643 line= MEM_callocN(sizeof(uiLinkLine), "linkline");
644 BLI_addtail(listb, line);
649 uiBut *uiFindInlink(uiBlock *block, void *poin)
651 return ui_find_inlink(block, poin);
654 void uiComposeLinks(uiBlock *block)
661 but= block->buttons.first;
663 if(but->type==LINK) {
666 /* for all pointers in the array */
670 for(a=0; a < *(link->totlink); a++) {
671 bt= ui_find_inlink(block, (*ppoin)[a] );
673 ui_add_link_line(&link->lines, but, bt);
677 else if(link->poin) {
678 bt= ui_find_inlink(block, *(link->poin) );
680 ui_add_link_line(&link->lines, but, bt);
689 static void ui_add_link(uiBut *from, uiBut *to)
691 /* in 'from' we have to add a link to 'to' */
696 if(ui_is_a_link(from, to)) {
697 printf("already exists\n");
703 /* are there more pointers allowed? */
705 oldppoin= *(link->ppoin);
707 (*(link->totlink))++;
708 *(link->ppoin)= MEM_callocN( *(link->totlink)*sizeof(void *), "new link");
710 for(a=0; a< (*(link->totlink))-1; a++) {
711 (*(link->ppoin))[a]= oldppoin[a];
713 (*(link->ppoin))[a]= to->poin;
715 if(oldppoin) MEM_freeN(oldppoin);
718 *(link->poin)= to->poin;
723 static int ui_do_but_LINK(uiBlock *block, uiBut *but)
726 * This button only visualizes, the dobutton mode
727 * can add a new link, but then the whole system
728 * should be redrawn/initialized.
731 uiBut *bt=0, *bto=NULL;
732 short sval[2], mval[2], mvalo[2], first= 1;
734 uiGetMouse(curarea->win, sval);
738 while (get_mbut() & L_MOUSE) {
739 uiGetMouse(curarea->win, mval);
741 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || first) {
742 /* clear completely, because of drawbuttons */
743 bt= ui_get_valid_link_button(block, but, mval);
745 bt->flag |= UI_ACTIVE;
749 bto->flag &= ~UI_ACTIVE;
755 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
757 glutil_draw_front_xor_line(sval[0], sval[1], mval[0], mval[1]);
764 else UI_wait_for_statechange();
768 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
772 if(but->type==LINK) ui_add_link(but, bt);
773 else ui_add_link(bt, but);
775 scrarea_queue_winredraw(curarea);
782 /* ************************************************ */
784 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
787 if(val) block->lockstr= lockstr;
790 void uiBlockClearButLock(uiBlock *block)
793 block->lockstr= NULL;
796 /* *************************************************************** */
798 /* XXX 2.50 no button editing */
801 static void setup_file(uiBlock *block)
806 fp= fopen("butsetup","w");
809 but= block->buttons.first;
812 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);
820 static void edit_but(uiBlock *block, uiBut *but, uiEvent *uevent)
822 short dx, dy, mval[2], mvalo[2], didit=0;
824 getmouseco_sc(mvalo);
826 if( !(get_mbut() & L_MOUSE) ) break;
829 dx= (mval[0]-mvalo[0]);
830 dy= (mval[1]-mvalo[1]);
837 glRectf(but->x1-2, but->y1-2, but->x2+2, but->y2+2);
839 if((uevent->qual & LR_SHIFTKEY)==0) {
847 ui_block_flush_back(but->block);
851 /* idle for this poor code */
852 else PIL_sleep_ms(30);
854 if(didit) setup_file(block);
858 /* XXX 2.50 no links supported yet */
860 static void ui_delete_active_linkline(uiBlock *block)
864 uiLinkLine *line, *nline;
867 but= block->buttons.first;
869 if(but->type==LINK && but->link) {
870 line= but->link->lines.first;
875 if(line->flag & UI_SELECT) {
876 BLI_remlink(&but->link->lines, line);
878 link= line->from->link;
880 /* are there more pointers allowed? */
883 if(*(link->totlink)==1) {
885 MEM_freeN(*(link->ppoin));
886 *(link->ppoin)= NULL;
890 for(a=0; a< (*(link->totlink)); a++) {
892 if( (*(link->ppoin))[a] != line->to->poin ) {
893 (*(link->ppoin))[b]= (*(link->ppoin))[a];
897 (*(link->totlink))--;
912 /* temporal! these buttons can be everywhere... */
913 allqueue(REDRAWBUTSLOGIC, 0);
916 static void ui_do_active_linklines(uiBlock *block, short *mval)
919 uiLinkLine *line, *act= NULL;
920 float mindist= 12.0, fac, v1[2], v2[2], v3[3];
927 /* find a line close to the mouse */
928 but= block->buttons.first;
930 if(but->type==LINK && but->link) {
932 line= but->link->lines.first;
934 v2[0]= line->from->x2;
935 v2[1]= (line->from->y1+line->from->y2)/2.0;
937 v3[1]= (line->to->y1+line->to->y2)/2.0;
939 fac= PdistVL2Dfl(v1, v2, v3);
951 /* check for a 'found one' to prevent going to 'frontbuffer' mode.
952 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
954 glDrawBuffer(GL_FRONT);
957 but= block->buttons.first;
959 if(but->type==LINK && but->link) {
960 line= but->link->lines.first;
963 if((line->flag & UI_SELECT)==0) {
964 line->flag |= UI_SELECT;
965 ui_draw_linkline(but, line);
968 else if(line->flag & UI_SELECT) {
969 line->flag &= ~UI_SELECT;
970 ui_draw_linkline(but, line);
978 glDrawBuffer(GL_BACK);
983 /* ******************************************************* */
985 /* XXX 2.50 no screendump supported yet */
988 /* nasty but safe way to store screendump rect */
989 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
991 static void ui_set_screendump_bbox(uiBlock *block)
996 scr_sizex= block->maxx - block->minx;
997 scr_sizey= block->maxy - block->miny;
1000 scr_sizex= scr_sizey= 0;
1004 /* used for making screenshots for menus, called in screendump.c */
1005 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1007 if(scr_sizex!=0 && scr_sizey!=0) {
1019 /* *********************** data get/set ***********************
1020 * this either works with the pointed to data, or can work with
1021 * an edit override pointer while dragging for example */
1023 /* for buttons pointing to color for example */
1024 void ui_get_but_vectorf(uiBut *but, float *vec)
1029 poin= (but->editvec)? (void*)but->editvec: but->poin;
1030 pointype= (but->editvec)? FLO: but->pointype;
1032 if(!but->editvec && pointype == CHA) {
1033 char *cp= (char *)poin;
1034 vec[0]= ((float)cp[0])/255.0;
1035 vec[1]= ((float)cp[1])/255.0;
1036 vec[2]= ((float)cp[2])/255.0;
1038 else if(pointype == FLO) {
1039 float *fp= (float *)poin;
1044 /* for buttons pointing to color for example */
1045 void ui_set_but_vectorf(uiBut *but, float *vec)
1050 poin= (but->editvec)? (void*)but->editvec: but->poin;
1051 pointype= (but->editvec)? FLO: but->pointype;
1053 if(!but->editvec && but->pointype == CHA) {
1054 char *cp= (char *)poin;
1055 cp[0]= (char)(0.5 +vec[0]*255.0);
1056 cp[1]= (char)(0.5 +vec[1]*255.0);
1057 cp[2]= (char)(0.5 +vec[2]*255.0);
1059 else if( but->pointype == FLO ) {
1060 float *fp= (float *)poin;
1065 int ui_is_but_float(uiBut *but)
1067 if(but->pointype==FLO && but->poin)
1070 if(but->rnaprop && RNA_property_type(&but->rnapoin, but->rnaprop) == PROP_FLOAT)
1076 double ui_get_but_val(uiBut *but)
1081 if(but->editval) { return *(but->editval); }
1082 if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1087 switch(RNA_property_type(&but->rnapoin, prop)) {
1089 if(RNA_property_array_length(&but->rnapoin, prop))
1090 value= RNA_property_boolean_get_array(&but->rnapoin, prop, but->rnaindex);
1092 value= RNA_property_boolean_get(&but->rnapoin, prop);
1095 if(RNA_property_array_length(&but->rnapoin, prop))
1096 value= RNA_property_int_get_array(&but->rnapoin, prop, but->rnaindex);
1098 value= RNA_property_int_get(&but->rnapoin, prop);
1101 if(RNA_property_array_length(&but->rnapoin, prop))
1102 value= RNA_property_float_get_array(&but->rnapoin, prop, but->rnaindex);
1104 value= RNA_property_float_get(&but->rnapoin, prop);
1107 value= RNA_property_enum_get(&but->rnapoin, prop);
1114 else if(but->type== HSVSLI) {
1117 fp= (but->editvec)? but->editvec: (float *)but->poin;
1118 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1120 switch(but->str[0]) {
1121 case 'H': value= h; break;
1122 case 'S': value= s; break;
1123 case 'V': value= v; break;
1126 else if( but->pointype == CHA ) {
1127 value= *(char *)but->poin;
1129 else if( but->pointype == SHO ) {
1130 value= *(short *)but->poin;
1132 else if( but->pointype == INT ) {
1133 value= *(int *)but->poin;
1135 else if( but->pointype == FLO ) {
1136 value= *(float *)but->poin;
1142 void ui_set_but_val(uiBut *but, double value)
1146 /* value is a hsv value: convert to rgb */
1150 if(RNA_property_editable(&but->rnapoin, prop)) {
1151 switch(RNA_property_type(&but->rnapoin, prop)) {
1153 if(RNA_property_array_length(&but->rnapoin, prop))
1154 RNA_property_boolean_set_array(&but->rnapoin, prop, but->rnaindex, value);
1156 RNA_property_boolean_set(&but->rnapoin, prop, value);
1159 if(RNA_property_array_length(&but->rnapoin, prop))
1160 RNA_property_int_set_array(&but->rnapoin, prop, but->rnaindex, value);
1162 RNA_property_int_set(&but->rnapoin, prop, value);
1165 if(RNA_property_array_length(&but->rnapoin, prop))
1166 RNA_property_float_set_array(&but->rnapoin, prop, but->rnaindex, value);
1168 RNA_property_float_set(&but->rnapoin, prop, value);
1171 RNA_property_enum_set(&but->rnapoin, prop, value);
1178 else if(but->pointype==0);
1179 else if(but->type==HSVSLI ) {
1182 fp= (but->editvec)? but->editvec: (float *)but->poin;
1183 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1185 switch(but->str[0]) {
1186 case 'H': h= value; break;
1187 case 'S': s= value; break;
1188 case 'V': v= value; break;
1191 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1195 /* first do rounding */
1196 if(but->pointype==CHA)
1197 value= (char)floor(value+0.5);
1198 else if(but->pointype==SHO ) {
1199 /* gcc 3.2.1 seems to have problems
1200 * casting a double like 32772.0 to
1201 * a short so we cast to an int, then
1204 gcckludge = (int) floor(value+0.5);
1205 value= (short)gcckludge;
1207 else if(but->pointype==INT )
1208 value= (int)floor(value+0.5);
1209 else if(but->pointype==FLO ) {
1210 float fval= (float)value;
1211 if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f; /* prevent negative zero */
1215 /* then set value with possible edit override */
1217 *but->editval= value;
1218 else if(but->pointype==CHA)
1219 *((char *)but->poin)= (char)value;
1220 else if(but->pointype==SHO)
1221 *((short *)but->poin)= (short)value;
1222 else if(but->pointype==INT)
1223 *((int *)but->poin)= (int)value;
1224 else if(but->pointype==FLO)
1225 *((float *)but->poin)= (float)value;
1228 /* update select flag */
1232 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1237 buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1240 /* string was too long, we have to truncate */
1241 BLI_strncpy(str, buf, maxlen);
1246 BLI_strncpy(str, but->poin, maxlen);
1250 void ui_set_but_string(uiBut *but, const char *str)
1253 if(RNA_property_editable(&but->rnapoin, but->rnaprop))
1254 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1257 BLI_strncpy(but->poin, str, but->max);
1260 /* ******************* Font ********************/
1262 static void ui_set_ftf_font(float aspect)
1264 #ifdef INTERNATIONAL
1266 FTF_SetFontSize('l');
1268 else if(aspect<1.59) {
1269 FTF_SetFontSize('m');
1272 FTF_SetFontSize('s');
1277 void uiSetCurFont(uiBlock *block, int index)
1279 ui_set_ftf_font(block->aspect);
1281 if(block->aspect<0.60) {
1282 block->curfont= UIfont[index].xl;
1284 else if(block->aspect<1.15) {
1285 block->curfont= UIfont[index].large;
1287 else if(block->aspect<1.59) {
1288 block->curfont= UIfont[index].medium;
1291 block->curfont= UIfont[index].small;
1294 if(block->curfont==NULL) block->curfont= UIfont[index].large;
1295 if(block->curfont==NULL) block->curfont= UIfont[index].medium;
1296 if(block->curfont==NULL) printf("error block no font %s\n", block->name);
1300 /* called by node editor */
1301 void *uiSetCurFont_ext(float aspect)
1305 ui_set_ftf_font(aspect);
1308 curfont= UIfont[0].xl;
1310 else if(aspect<1.15) {
1311 curfont= UIfont[0].large;
1313 else if(aspect<1.59) {
1314 curfont= UIfont[0].medium;
1317 curfont= UIfont[0].small;
1320 if(curfont==NULL) curfont= UIfont[0].large;
1321 if(curfont==NULL) curfont= UIfont[0].medium;
1326 void uiDefFont(unsigned int index, void *xl, void *large, void *medium, void *small)
1328 if(index>=UI_ARRAY) return;
1330 UIfont[index].xl= xl;
1331 UIfont[index].large= large;
1332 UIfont[index].medium= medium;
1333 UIfont[index].small= small;
1336 /* ******************* Free ********************/
1338 static void ui_free_link(uiLink *link)
1341 BLI_freelistN(&link->lines);
1346 static void ui_free_but(const bContext *C, uiBut *but)
1348 if(but->active) ui_button_active_cancel(C, but);
1349 if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1350 ui_free_link(but->link);
1355 void uiFreeBlock(const bContext *C, uiBlock *block)
1359 while( (but= block->buttons.first) ) {
1360 BLI_remlink(&block->buttons, but);
1361 ui_free_but(C, but);
1364 if(block->panel) block->panel->active= 0;
1365 BLI_freelistN(&block->saferct);
1371 void uiFreeBlocks(const bContext *C, ListBase *lb)
1375 while( (block= lb->first) ) {
1376 BLI_remlink(lb, block);
1377 uiFreeBlock(C, block);
1381 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, char *name, short dt, short font)
1384 uiBlock *block, *oldblock= NULL;
1386 int getsizex, getsizey;
1389 lb= ®ion->uiblocks;
1391 /* each listbase only has one block with this name, free block
1392 * if is already there so it can be rebuilt from scratch */
1394 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1395 if (BLI_streq(oldblock->name, name))
1399 BLI_remlink(lb, oldblock);
1402 block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1403 block->oldblock= oldblock;
1405 /* at the beginning of the list! for dynamical menus/blocks */
1407 BLI_addhead(lb, block);
1409 BLI_strncpy(block->name, name, sizeof(block->name));
1414 /* window where queue event should be added, pretty weak this way!
1415 this is because the 'mainwin' pup menu's */
1416 block->winq= mywinget();
1420 block->themecol= TH_AUTO;
1422 /* window matrix and aspect */
1423 if(region->swinid) {
1424 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1425 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1427 /* TODO - investigate why block->winmat[0][0] is negative
1428 * in the image view when viewRedrawForce is called */
1429 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1432 /* no subwindow created yet, for menus for example, so we
1433 * use the main window instead, since buttons are created
1435 wm_subwindow_getmatrix(window, window->winid, block->winmat);
1436 wm_subwindow_getsize(window, window->winid, &getsizex, &getsizey);
1438 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1439 block->auto_open= 2;
1442 uiSetCurFont(block, font);
1447 uiBlock *uiGetBlock(char *name, ARegion *ar)
1449 uiBlock *block= ar->uiblocks.first;
1452 if( strcmp(name, block->name)==0 ) return block;
1459 void ui_check_but(uiBut *but)
1461 /* if something changed in the button */
1465 int transopts= ui_translate_buttons();
1470 if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1472 /* test for min and max, icon sliders, etc */
1473 switch( but->type ) {
1479 value= ui_get_but_val(but);
1480 if(value < but->min) ui_set_but_val(but, but->min);
1481 else if(value > but->max) ui_set_but_val(but, but->max);
1485 value= fabs( ui_get_but_val(but) );
1486 if(value < but->min) ui_set_but_val(but, but->min);
1487 else if(value > but->max) ui_set_but_val(but, but->max);
1492 if(but->flag & UI_SELECT) but->iconadd= 1;
1493 else but->iconadd= 0;
1497 value= ui_get_but_val(but);
1498 but->iconadd= (int)value- (int)(but->min);
1502 value= ui_get_but_val(but);
1503 but->iconadd= (int)value- (int)(but->min);
1508 /* safety is 4 to enable small number buttons (like 'users') */
1509 if(but->type==NUMSLI || but->type==HSVSLI)
1510 okwidth= -4 + (but->x2 - but->x1)/2.0;
1512 okwidth= -4 + (but->x2 - but->x1);
1515 switch( but->type ) {
1520 if(but->x2 - but->x1 > 24) {
1521 value= ui_get_but_val(but);
1522 ui_set_name_menu(but, (int)value);
1531 value= ui_get_but_val(but);
1533 if(ui_is_but_float(but)) {
1534 if(value == FLT_MAX) sprintf(but->drawstr, "%sFLT_MAX", but->str);
1535 else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-FLT_MAX", but->str);
1536 else if(but->a2) { /* amount of digits defined */
1537 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1538 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1539 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1540 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1543 if(but->max<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1544 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1548 sprintf(but->drawstr, "%s%d", but->str, (int)value);
1553 if(ui_is_but_float(but)) {
1554 value= ui_get_but_val(but);
1555 if(but->a2) { /* amount of digits defined */
1556 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1557 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1558 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1559 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1562 sprintf(but->drawstr, "%s%.2f", but->str, value);
1565 else strcpy(but->drawstr, but->str);
1570 id= *(but->idpoin_idpp);
1571 strcpy(but->drawstr, but->str);
1572 if(id) strcat(but->drawstr, id->name+2);
1577 char str[UI_MAX_DRAW_STR];
1579 ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1581 strcpy(but->drawstr, but->str);
1582 strcat(but->drawstr, str);
1587 strcpy(but->drawstr, but->str);
1588 if (but->flag & UI_SELECT) {
1589 strcat(but->drawstr, "Press a key");
1591 /* XXX 2.50 function not supported */
1592 /* strcat(but->drawstr, key_event_to_string((short) ui_get_but_val(but))); */
1597 /* trying to get the dual-icon to left of text... not very nice */
1599 strcpy(but->drawstr, " ");
1600 strcpy(but->drawstr+2, but->str);
1604 strcpy(but->drawstr, but->str);
1608 /* if we are doing text editing, this will override the drawstr */
1610 strcpy(but->drawstr, but->str);
1611 strcat(but->drawstr, but->editstr);
1614 if(but->drawstr[0]) {
1615 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr, transopts);
1616 // here should be check for less space for icon offsets...
1617 if(but->type==MENU) okwidth -= 15;
1622 /* automatic width */
1623 if(but->x2==0.0f && but->x1 > 0.0f) {
1624 but->x2= (but->x1+but->strwidth+6);
1627 if(but->strwidth==0) but->drawstr[0]= 0;
1628 else if(but->type==BUTM || but->type==BLOCK); // no clip string, uiTextBoundsBlock is used (hack!)
1631 /* calc but->ofs, to draw the string shorter if too long */
1634 while(but->strwidth > (int)okwidth ) {
1636 if ELEM3(but->type, NUM, NUMABS, TEX) { // only these cut off left
1638 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr+but->ofs, transopts);
1640 /* textbut exception */
1641 if(but->editstr && but->pos != -1) {
1642 pos= but->pos+strlen(but->str);
1643 if(pos-1 < but->ofs) {
1644 pos= but->ofs-pos+1;
1650 but->drawstr[ strlen(but->drawstr)-pos ]= 0;
1655 but->drawstr[ strlen(but->drawstr)-1 ]= 0;
1656 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr, transopts);
1659 if(but->strwidth < 10) break;
1664 static int ui_auto_themecol(uiBut *but)
1669 return TH_BUT_ACTION;
1676 return TH_BUT_SETTING;
1684 return TH_BUT_TEXTFIELD;
1689 // (weak!) detect if it is a blockloop
1690 if(but->block->dt == UI_EMBOSSP) return TH_MENU_ITEM;
1691 return TH_BUT_POPUP;
1695 return TH_BUT_NEUTRAL;
1699 void uiBlockBeginAlign(uiBlock *block)
1701 /* if other align was active, end it */
1702 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1704 block->flag |= UI_BUT_ALIGN_DOWN;
1705 /* buttons declared after this call will this align flag */
1708 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1712 dx= fabs( but1->x2 - but2->x1);
1713 dy= fabs( but1->y1 - but2->y2);
1715 if(dx > dy) return 0;
1719 void uiBlockEndAlign(uiBlock *block)
1721 uiBut *prev, *but=NULL, *next;
1722 int flag= 0, cols=0, rows=0;
1723 int theme= UI_GetThemeValue(TH_BUT_DRAWTYPE);
1725 if ( !(ELEM3(theme, TH_MINIMAL, TH_SHADED, TH_ROUNDED)) ) {
1726 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1731 - go back to first button of align start (ALIGN_DOWN)
1732 - compare triples, and define flags
1734 prev= block->buttons.last;
1736 if( (prev->flag & UI_BUT_ALIGN_DOWN)) but= prev;
1739 if(but && but->next) {
1740 if(buts_are_horiz(but, but->next)) cols++;
1746 if(but==NULL) return;
1748 /* rows==0: 1 row, cols==0: 1 collumn */
1750 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1755 /* clear old flag */
1756 but->flag &= ~UI_BUT_ALIGN_DOWN;
1758 if(flag==0) { /* first case */
1760 if(buts_are_horiz(but, next)) {
1762 flag= UI_BUT_ALIGN_RIGHT;
1764 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
1767 flag= UI_BUT_ALIGN_DOWN;
1771 else if(next==NULL) { /* last case */
1773 if(buts_are_horiz(prev, but)) {
1775 flag= UI_BUT_ALIGN_LEFT;
1777 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
1779 else flag= UI_BUT_ALIGN_TOP;
1782 else if(buts_are_horiz(but, next)) {
1783 /* check if this is already second row */
1784 if( prev && buts_are_horiz(prev, but)==0) {
1785 flag |= UI_BUT_ALIGN_TOP;
1786 /* exception case: bottom row */
1790 if(bt->next && buts_are_horiz(bt, bt->next)==0 ) break;
1793 if(bt==0) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
1796 else flag |= UI_BUT_ALIGN_LEFT;
1800 flag |= UI_BUT_ALIGN_TOP;
1802 else { /* next button switches to new row */
1803 if( (flag & UI_BUT_ALIGN_TOP)==0) { /* stil top row */
1805 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
1807 flag |= UI_BUT_ALIGN_DOWN;
1810 flag |= UI_BUT_ALIGN_TOP;
1816 /* merge coordinates */
1820 but->x1= (prev->x2+but->x1)/2.0;
1824 but->y2= (prev->y1+but->y2)/2.0;
1828 if(buts_are_horiz(prev, but)) {
1829 but->x1= (prev->x2+but->x1)/2.0;
1831 /* copy height too */
1834 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
1835 /* the previous button is a single one in its row */
1836 but->y2= (prev->y1+but->y2)/2.0;
1840 /* the previous button is not a single one in its row */
1850 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1854 static void uiBlockEndAligno(uiBlock *block)
1858 /* correct last defined button */
1859 but= block->buttons.last;
1861 /* vertical align case */
1862 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_DOWN) ) {
1863 but->flag &= ~UI_BUT_ALIGN_DOWN;
1865 /* horizontal align case */
1866 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT) ) {
1867 but->flag &= ~UI_BUT_ALIGN_RIGHT;
1869 /* else do nothing, manually provided flags */
1871 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1876 ui_def_but is the function that draws many button types
1879 "a1" Click Step (how much to change the value each click)
1880 "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
1881 all greater values will be clamped to 4.
1884 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)
1889 if(type & BUTPOIN) { /* a pointer is required */
1891 /* if pointer is zero, button is removed and not drawn */
1892 UI_ThemeColor(block->themecol);
1893 glRects(x1, y1, x1+x2, y1+y2);
1898 but= MEM_callocN(sizeof(uiBut), "uiBut");
1900 but->type= type & BUTTYPE;
1901 but->pointype= type & BUTPOIN;
1902 but->bit= type & BIT;
1903 but->bitnr= type & 31;
1906 BLI_addtail(&block->buttons, but);
1908 but->retval= retval;
1909 if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
1910 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
1911 strcpy(but->str, str);
1914 but->str= but->strdata;
1915 strcpy(but->str, str);
1919 if(block->autofill) {
1934 but->font= block->curfont;
1936 but->lock= block->lock;
1937 but->lockstr= block->lockstr;
1939 but->aspect= block->aspect;
1940 but->win= block->win;
1941 but->block= block; // pointer back, used for frontbuffer status, and picker
1943 if(block->themecol==TH_AUTO) but->themecol= ui_auto_themecol(but);
1944 else but->themecol= block->themecol;
1946 if (but->type==BUTM) {
1947 but->butm_func= block->butm_func;
1948 but->butm_func_arg= block->butm_func_arg;
1950 but->func= block->func;
1951 but->func_arg1= block->func_arg1;
1952 but->func_arg2= block->func_arg2;
1955 ui_set_embossfunc(but, block->dt);
1957 but->pos= -1; /* cursor invisible */
1959 if(ELEM(but->type, NUM, NUMABS)) { /* add a space to name */
1960 slen= strlen(but->str);
1961 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
1962 if(but->str[slen-1]!=' ') {
1963 but->str[slen]= ' ';
1964 but->str[slen+1]= 0;
1969 if(but->type==HSVCUBE) { /* hsv buttons temp storage */
1971 ui_get_but_vectorf(but, rgb);
1972 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
1975 if ELEM8(but->type, HSVSLI , NUMSLI, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM) {
1976 but->flag |= UI_TEXT_LEFT;
1979 if(but->type==BUT_TOGDUAL) {
1980 but->flag |= UI_ICON_LEFT;
1983 if(but->type==ROUNDBOX)
1984 but->flag |= UI_NO_HILITE;
1986 but->flag |= (block->flag & UI_BUT_ALIGN);
1987 if(block->flag & UI_BLOCK_NO_HILITE)
1988 but->flag |= UI_NO_HILITE;
1993 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)
1995 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2002 /* if _x_ is a power of two (only one bit) return the power,
2003 * otherwise return -1.
2004 * (1<<findBitIndex(x))==x for powers of two.
2006 static int findBitIndex(unsigned int x) {
2007 if (!x || (x&(x-1))!=0) { /* x&(x-1) strips lowest bit */
2012 if (x&0xFFFF0000) idx+=16, x>>=16;
2013 if (x&0xFF00) idx+=8, x>>=8;
2014 if (x&0xF0) idx+=4, x>>=4;
2015 if (x&0xC) idx+=2, x>>=2;
2022 /* autocomplete helper functions */
2023 struct AutoComplete {
2029 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2031 AutoComplete *autocpl;
2033 autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2034 autocpl->maxlen= maxlen;
2035 autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2036 autocpl->startname= startname;
2041 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2043 char *truncate= autocpl->truncate;
2044 char *startname= autocpl->startname;
2047 for(a=0; a<autocpl->maxlen-1; a++) {
2048 if(startname[a]==0 || startname[a]!=name[a])
2052 if(startname[a]==0) {
2055 BLI_strncpy(truncate, name, autocpl->maxlen);
2057 /* remove from truncate what is not in bone->name */
2058 for(a=0; a<autocpl->maxlen-1; a++) {
2059 if(truncate[a]!=name[a])
2066 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2068 if(autocpl->truncate[0])
2069 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2071 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2073 MEM_freeN(autocpl->truncate);
2077 /* autocomplete callback for ID buttons */
2078 static void autocomplete_id(char *str, void *arg_v)
2080 /* int blocktype= (intptr_t)arg_v; */
2081 ListBase *listb= NULL /* XXX 2.50 needs context, wich_libbase(G.main, blocktype) */;
2083 if(listb==NULL) return;
2085 /* search if str matches the beginning of an ID struct */
2087 AutoComplete *autocpl= autocomplete_begin(str, 22);
2090 for(id= listb->first; id; id= id->next)
2091 autocomplete_do_name(autocpl, id->name+2);
2093 autocomplete_end(autocpl, str);
2097 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)
2099 int bitIdx= findBitIndex(bit);
2103 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2106 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)
2108 return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2110 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)
2112 return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2114 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)
2116 return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2118 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)
2120 return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2122 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)
2124 return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2126 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)
2128 return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2130 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)
2132 return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2134 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)
2136 return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2139 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)
2141 uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2143 but->icon= (BIFIconID) icon;
2144 but->flag|= UI_HAS_ICON;
2150 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)
2152 int bitIdx= findBitIndex(bit);
2156 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2160 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)
2162 return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2164 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)
2166 return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2168 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)
2170 return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2172 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)
2174 return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2176 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)
2178 return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2180 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)
2182 return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2184 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)
2186 return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2188 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)
2190 return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2193 /* Button containing both string label and icon */
2194 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)
2196 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2198 but->icon= (BIFIconID) icon;
2199 but->flag|= UI_HAS_ICON;
2201 but->flag|= UI_ICON_LEFT;
2207 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)
2209 int bitIdx= findBitIndex(bit);
2213 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2217 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)
2219 return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2221 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)
2223 return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2225 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)
2227 return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2229 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)
2231 return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2233 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)
2235 return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2237 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)
2239 return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2241 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)
2243 return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2245 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)
2247 return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2250 uiBut *uiDefRNABut(uiBlock *block, int retval, PointerRNA *ptr, PropertyRNA *prop, int index, short x1, short y1, short x2, short y2)
2254 switch(RNA_property_type(ptr, prop)) {
2255 case PROP_BOOLEAN: {
2258 length= RNA_property_array_length(ptr, prop);
2261 value= RNA_property_boolean_get_array(ptr, prop, index);
2263 value= RNA_property_boolean_get(ptr, prop);
2265 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));
2269 int softmin, softmax, step;
2271 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2272 but= ui_def_but(block, NUM, 0, "", x1, y1, x2, y2, NULL, softmin, softmax, step, 0, (char*)RNA_property_ui_description(ptr, prop));
2276 float softmin, softmax, step, precision;
2278 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2279 but= ui_def_but(block, NUM, 0, "", x1, y1, x2, y2, NULL, softmin, softmax, step, precision, (char*)RNA_property_ui_description(ptr, prop));
2283 const EnumPropertyItem *item;
2288 RNA_property_enum_items(ptr, prop, &item, &totitem);
2290 dynstr= BLI_dynstr_new();
2291 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(ptr, prop));
2292 for(i=0; i<totitem; i++)
2293 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2294 menu= BLI_dynstr_get_cstring(dynstr);
2295 BLI_dynstr_free(dynstr);
2297 but= ui_def_but(block, MENU, 0, menu, x1, y1, x2, y2, NULL, 0, 0, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2304 maxlength= RNA_property_string_maxlength(ptr, prop);
2306 /* interface code should ideally support unlimited length */
2307 maxlength= UI_MAX_DRAW_STR;
2309 but= ui_def_but(block, TEX, 0, "", x1, y1, x2, y2, NULL, 0, maxlength, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2312 case PROP_POINTER: {
2314 PropertyRNA *nameprop;
2315 char name[256]= "", *nameptr= name;
2317 RNA_property_pointer_get(ptr, prop, &pptr);
2320 nameprop= RNA_struct_name_property(&pptr);
2321 if(pptr.type && nameprop)
2322 nameptr= RNA_property_string_get_alloc(&pptr, nameprop, name, sizeof(name));
2324 strcpy(nameptr, "->");
2327 but= ui_def_but(block, BUT, 0, nameptr, x1, y1, x2, y2, NULL, 0, 0, 0, 0, (char*)RNA_property_ui_description(ptr, prop));
2328 but->flag |= UI_TEXT_LEFT;
2343 but->rnaindex= index;
2351 /* END Button containing both string label and icon */
2353 void uiAutoBlock(uiBlock *block, float minx, float miny, float sizex, float sizey, int flag)
2356 block->maxx= minx+sizex;
2358 block->maxy= miny+sizey;
2360 block->autofill= flag; /* also check for if it has to be done */
2364 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2368 link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2373 link->fromcode= from;
2377 /* cruft to make uiBlock and uiBut private */
2379 int uiBlocksGetYMin(ListBase *lb)
2384 for (block= lb->first; block; block= block->next)
2385 if (block==lb->first || block->miny<min)
2391 int uiBlockGetCol(uiBlock *block)
2393 return block->themecol;
2395 void uiBlockSetCol(uiBlock *block, int col)
2397 block->themecol= col;
2399 void uiBlockSetEmboss(uiBlock *block, int emboss)
2403 void uiBlockSetDirection(uiBlock *block, int direction)
2405 block->direction= direction;
2408 /* this call escapes if there's alignment flags */
2409 void uiBlockFlipOrder(uiBlock *block)
2413 float centy, miny=10000, maxy= -10000;
2415 // if(U.uiflag & USER_PLAINMENUS)
2418 for(but= block->buttons.first; but; but= but->next) {
2419 if(but->flag & UI_BUT_ALIGN) return;
2420 if(but->y1 < miny) miny= but->y1;
2421 if(but->y2 > maxy) maxy= but->y2;
2424 centy= (miny+maxy)/2.0;
2425 for(but= block->buttons.first; but; but= but->next) {
2426 but->y1 = centy-(but->y1-centy);
2427 but->y2 = centy-(but->y2-centy);
2428 SWAP(float, but->y1, but->y2);
2431 /* also flip order in block itself, for example for arrowkey */
2432 lb.first= lb.last= NULL;
2433 but= block->buttons.first;
2436 BLI_remlink(&block->buttons, but);
2437 BLI_addtail(&lb, but);
2444 void uiBlockSetFlag(uiBlock *block, int flag)
2448 void uiBlockSetXOfs(uiBlock *block, int xofs)
2452 void* uiBlockGetCurFont(uiBlock *block)
2454 return block->curfont;
2457 void uiButSetFlag(uiBut *but, int flag)
2461 void uiButClearFlag(uiBut *but, int flag)
2466 int uiButGetRetVal(uiBut *but)
2471 /* Call this function BEFORE adding buttons to the block */
2472 void uiBlockSetButmFunc(uiBlock *block, void (*menufunc)(void *arg, int event), void *arg)
2474 block->butm_func= menufunc;
2475 block->butm_func_arg= arg;
2478 void uiBlockSetFunc(uiBlock *block, void (*func)(void *arg1, void *arg2), void *arg1, void *arg2)
2481 block->func_arg1= arg1;
2482 block->func_arg2= arg2;
2485 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)())
2487 block->drawextra= func;
2490 void uiButSetFunc(uiBut *but, void (*func)(void *arg1, void *arg2), void *arg1, void *arg2)
2493 but->func_arg1= arg1;
2494 but->func_arg2= arg2;
2497 void uiButSetFunc3(uiBut *but, void (*func)(void *arg1, void *arg2, void *arg3), void *arg1, void *arg2, void *arg3)
2500 but->func_arg1= arg1;
2501 but->func_arg2= arg2;
2502 but->func_arg3= arg3;
2505 void uiButSetCompleteFunc(uiBut *but, void (*func)(char *str, void *arg), void *arg)
2507 but->autocomplete_func= func;
2508 but->autofunc_arg= arg;
2511 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2513 uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2514 but->idpoin_func= func;
2515 but->idpoin_idpp= (ID**) idpp;
2519 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2524 uiBut *uiDefBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2526 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2527 but->block_func= func;
2532 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2534 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2535 but->block_func= func;
2540 /* Block button containing both string label and icon */
2541 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2543 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2545 but->icon= (BIFIconID) icon;
2546 but->flag|= UI_HAS_ICON;
2548 but->flag|= UI_ICON_LEFT;
2549 but->flag|= UI_ICON_RIGHT;
2551 but->block_func= func;
2557 /* Block button containing icon */
2558 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
2560 uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2562 but->icon= (BIFIconID) icon;
2563 but->flag|= UI_HAS_ICON;
2565 but->flag|= UI_ICON_LEFT;
2566 but->flag|= UI_ICON_RIGHT;
2568 but->block_func= func;
2574 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
2576 uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
2580 /* Program Init/Exit */
2585 BMF_GetFont(BMF_kHelveticaBold14),
2586 BMF_GetFont(BMF_kHelveticaBold12),
2587 BMF_GetFont(BMF_kHelveticaBold10),
2588 BMF_GetFont(BMF_kHelveticaBold8));
2590 BMF_GetFont(BMF_kHelvetica12),
2591 BMF_GetFont(BMF_kHelvetica12),
2592 BMF_GetFont(BMF_kHelvetica10),
2593 BMF_GetFont(BMF_kHelveticaBold8));
2595 ui_resources_init();
2598 void UI_init_userdef()
2600 ui_text_init_userdef();
2601 ui_theme_init_userdef();
2606 ui_resources_free();