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 * Contributor(s): Blender Foundation 2002-2008, full recode.
25 * ***** END GPL LICENSE BLOCK *****
32 #include "MEM_guardedalloc.h"
35 #include "DNA_listBase.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_texture_types.h"
38 #include "DNA_userdef_types.h"
40 #include "BLI_arithb.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_dynstr.h"
44 #include "BKE_context.h"
45 #include "BKE_idprop.h"
46 #include "BKE_library.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_intern.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 int ui_translate_buttons()
91 return (U.transopts & USER_TR_BUTTONS);
94 int ui_translate_menus()
96 return (U.transopts & USER_TR_MENUS);
99 int ui_translate_tooltips()
101 return (U.transopts & USER_TR_TOOLTIPS);
104 /* ************* window matrix ************** */
106 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
109 int sx, sy, getsizex, getsizey;
111 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
112 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
120 gx += block->panel->ofsx;
121 gy += block->panel->ofsy;
124 *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
125 *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
128 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
135 ui_block_to_window_fl(ar, block, &fx, &fy);
141 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
146 ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
147 ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
149 winr->xmin= tmpr.xmin;
150 winr->ymin= tmpr.ymin;
151 winr->xmax= tmpr.xmax;
152 winr->ymax= tmpr.ymax;
155 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y) /* for mouse cursor */
157 float a, b, c, d, e, f, px, py;
158 int sx, sy, getsizex, getsizey;
160 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
161 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
165 a= .5*((float)getsizex)*block->winmat[0][0];
166 b= .5*((float)getsizex)*block->winmat[1][0];
167 c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
169 d= .5*((float)getsizey)*block->winmat[0][1];
170 e= .5*((float)getsizey)*block->winmat[1][1];
171 f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
176 *y= (a*(py-f) + d*(c-px))/(a*e-d*b);
177 *x= (px- b*(*y)- c)/a;
180 *x -= block->panel->ofsx;
181 *y -= block->panel->ofsy;
185 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
192 ui_window_to_block_fl(ar, block, &fx, &fy);
198 void ui_window_to_region(const ARegion *ar, int *x, int *y)
200 *x-= ar->winrct.xmin;
201 *y-= ar->winrct.ymin;
204 /* ******************* block calc ************************* */
206 /* only for pulldowns */
207 void uiTextBoundsBlock(uiBlock *block, int addval)
210 int i = 0, j, x1addval= 0, nextcol;
212 bt= block->buttons.first;
215 int transopts= ui_translate_buttons();
216 if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
217 j= UI_GetStringWidth(bt->font, bt->drawstr, transopts);
224 /* cope with multi collumns */
225 bt= block->buttons.first;
227 if(bt->next && bt->x1 < bt->next->x1)
232 bt->x2 = bt->x1 + i + addval;
234 ui_check_but(bt); // clips text again
237 x1addval+= i + addval;
243 void uiBoundsBlock(uiBlock *block, int addval)
251 if(block->buttons.first==NULL) {
253 block->minx= 0.0; block->maxx= block->panel->sizex;
254 block->miny= 0.0; block->maxy= block->panel->sizey;
259 block->minx= block->miny= 10000;
260 block->maxx= block->maxy= -10000;
262 bt= block->buttons.first;
264 if(bt->x1 < block->minx) block->minx= bt->x1;
265 if(bt->y1 < block->miny) block->miny= bt->y1;
267 if(bt->x2 > block->maxx) block->maxx= bt->x2;
268 if(bt->y2 > block->maxy) block->maxy= bt->y2;
273 block->minx -= addval;
274 block->miny -= addval;
275 block->maxx += addval;
276 block->maxy += addval;
279 /* hardcoded exception... but that one is annoying with larger safety */
280 bt= block->buttons.first;
281 if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
284 block->safety.xmin= block->minx-xof;
285 block->safety.ymin= block->miny-xof;
286 block->safety.xmax= block->maxx+xof;
287 block->safety.ymax= block->maxy+xof;
290 void uiBlockTranslate(uiBlock *block, int x, int y)
294 for(bt= block->buttons.first; bt; bt=bt->next) {
307 void uiBlockOrigin(uiBlock *block)
310 int minx= 10000, miny= 10000;
312 for(bt= block->buttons.first; bt; bt=bt->next) {
313 if(bt->x1 < minx) minx= bt->x1;
314 if(bt->y1 < miny) miny= bt->y1;
317 uiBlockTranslate(block, -minx, -miny);
320 void ui_autofill(uiBlock *block)
323 float *maxw, *maxh, startx = 0, starty, height = 0;
325 int rows=0, /* cols=0, */ i, lasti;
327 /* first count rows */
328 but= block->buttons.last;
331 /* calculate max width / height for each row */
332 maxw= MEM_callocN(sizeof(float)*rows, "maxw");
333 maxh= MEM_callocN(sizeof(float)*rows, "maxh");
334 but= block->buttons.first;
337 if( maxh[i] < but->y2) maxh[i]= but->y2;
343 for(i=0; i<rows; i++) totmaxh+= maxh[i];
345 /* apply widths/heights */
347 but= block->buttons.first;
350 // signal for aligning code
351 but->flag |= UI_BUT_ALIGN_DOWN;
357 height= (maxh[i]*(block->maxy-block->miny))/totmaxh;
362 but->y1= starty+but->aspect;
363 but->y2= but->y1+height-but->aspect;
365 but->x2= (but->x2*(block->maxx-block->minx))/maxw[i];
366 but->x1= startx+but->aspect;
369 but->x2+= but->x1-but->aspect;
376 uiBlockEndAlign(block);
378 MEM_freeN(maxw); MEM_freeN(maxh);
382 /* ************** LINK LINE DRAWING ************* */
384 /* link line drawing is not part of buttons or theme.. so we stick with it here */
386 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
388 float vec1[2], vec2[2];
390 if(line->from==NULL || line->to==NULL) return;
392 vec1[0]= (line->from->x1+line->from->x2)/2.0;
393 vec1[1]= (line->from->y1+line->from->y2)/2.0;
394 vec2[0]= (line->to->x1+line->to->x2)/2.0;
395 vec2[1]= (line->to->y1+line->to->y2)/2.0;
397 if(line->flag & UI_SELECT) UI_ThemeColorShade(but->themecol, 80);
398 else glColor3ub(0,0,0);
399 fdrawline(vec1[0], vec1[1], vec2[0], vec2[1]);
402 static void ui_draw_links(uiBlock *block)
407 but= block->buttons.first;
409 if(but->type==LINK && but->link) {
410 line= but->link->lines.first;
412 ui_draw_linkline(but, line);
420 /* ************** BLOCK ENDING FUNCTION ************* */
422 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
424 /* various properties are being compared here, hopfully sufficient
425 * to catch all cases, but it is simple to add more checks later */
426 if(but->retval != oldbut->retval) return 0;
427 if(but->poin != oldbut->poin || but->pointype != oldbut->pointype) return 0;
428 if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
429 if(but->rnaprop != oldbut->rnaprop)
430 if(but->rnaindex != oldbut->rnaindex) return 0;
431 if(but->func != oldbut->func) return 0;
432 if(but->func_arg1 != oldbut->func_arg1) return 0;
433 if(but->func_arg2 != oldbut->func_arg2) return 0;
438 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
444 oldblock= block->oldblock;
448 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
449 if(ui_but_equals_old(oldbut, but)) {
451 but->flag= oldbut->flag;
452 but->active= oldbut->active;
453 but->pos= oldbut->pos;
454 but->editstr= oldbut->editstr;
455 but->editval= oldbut->editval;
456 but->editvec= oldbut->editvec;
457 but->editcoba= oldbut->editcoba;
458 but->editcumap= oldbut->editcumap;
459 but->selsta= oldbut->selsta;
460 but->selend= oldbut->selend;
463 oldbut->active= NULL;
466 /* ensures one button can get activated, and in case the buttons
467 * draw are the same this gives O(1) lookup for each button */
468 BLI_remlink(&oldblock->buttons, oldbut);
469 ui_free_but(C, oldbut);
478 void uiEndBlock(const bContext *C, uiBlock *block)
482 /* inherit flags from 'old' buttons that was drawn here previous, based
483 * on matching buttons, we need this to make button event handling non
484 * blocking, while still alowing buttons to be remade each redraw as it
485 * is expected by blender code */
486 for(but=block->buttons.first; but; but=but->next)
487 if(ui_but_update_from_old_block(C, block, but))
490 if(block->oldblock) {
491 block->auto_open= block->oldblock->auto_open;
492 block->auto_open_last= block->oldblock->auto_open_last;
493 block->tooltipdisabled= block->oldblock->tooltipdisabled;
495 block->oldblock= NULL;
498 /* handle pending stuff */
499 if(block->autofill) ui_autofill(block);
500 if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
501 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
504 /* ************** BLOCK DRAWING FUNCTION ************* */
506 void uiDrawBlock(const bContext *C, uiBlock *block)
510 /* we set this only once */
511 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
513 if(block->flag & UI_BLOCK_LOOP)
514 uiDrawMenuBox(block->minx, block->miny, block->maxx, block->maxy, block->flag);
515 else if(block->panel)
516 ui_draw_panel(CTX_wm_region(C), block);
518 if(block->drawextra) block->drawextra(C, block);
520 for (but= block->buttons.first; but; but= but->next)
523 ui_draw_links(block);
526 /* ************* EVENTS ************* */
528 static void ui_is_but_sel(uiBut *but)
532 short push=0, true=1;
534 value= ui_get_but_val(but);
536 if( but->type==TOGN || but->type==ICONTOGN) true= 0;
540 if( BTST(lvalue, (but->bitnr)) ) push= true;
549 if (value==-1) push= 1;
556 if(value!=but->min) push= 1;
560 if(value==0.0) push= 1;
563 if(value == but->max) push= 1;
575 else if(push==1) but->flag |= UI_SELECT;
576 else but->flag &= ~UI_SELECT;
579 /* XXX 2.50 no links supported yet */
582 static uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
586 /* find button to link to */
587 for (bt= block->buttons.first; bt; bt= bt->next)
588 if(bt!=but && uibut_contains_pt(bt, mval))
592 if (but->type==LINK && bt->type==INLINK) {
593 if( but->link->tocode == (int)bt->min ) {
597 else if(but->type==INLINK && bt->type==LINK) {
598 if( bt->link->tocode == (int)but->min ) {
607 static int ui_is_a_link(uiBut *from, uiBut *to)
614 line= link->lines.first;
616 if(line->from==from && line->to==to) return 1;
623 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
627 but= block->buttons.first;
629 if(but->type==INLINK) {
630 if(but->poin == poin) return but;
637 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
641 line= MEM_callocN(sizeof(uiLinkLine), "linkline");
642 BLI_addtail(listb, line);
647 uiBut *uiFindInlink(uiBlock *block, void *poin)
649 return ui_find_inlink(block, poin);
652 void uiComposeLinks(uiBlock *block)
659 but= block->buttons.first;
661 if(but->type==LINK) {
664 /* for all pointers in the array */
668 for(a=0; a < *(link->totlink); a++) {
669 bt= ui_find_inlink(block, (*ppoin)[a] );
671 ui_add_link_line(&link->lines, but, bt);
675 else if(link->poin) {
676 bt= ui_find_inlink(block, *(link->poin) );
678 ui_add_link_line(&link->lines, but, bt);
687 static void ui_add_link(uiBut *from, uiBut *to)
689 /* in 'from' we have to add a link to 'to' */
694 if(ui_is_a_link(from, to)) {
695 printf("already exists\n");
701 /* are there more pointers allowed? */
703 oldppoin= *(link->ppoin);
705 (*(link->totlink))++;
706 *(link->ppoin)= MEM_callocN( *(link->totlink)*sizeof(void *), "new link");
708 for(a=0; a< (*(link->totlink))-1; a++) {
709 (*(link->ppoin))[a]= oldppoin[a];
711 (*(link->ppoin))[a]= to->poin;
713 if(oldppoin) MEM_freeN(oldppoin);
716 *(link->poin)= to->poin;
721 static int ui_do_but_LINK(uiBlock *block, uiBut *but)
724 * This button only visualizes, the dobutton mode
725 * can add a new link, but then the whole system
726 * should be redrawn/initialized.
729 uiBut *bt=0, *bto=NULL;
730 short sval[2], mval[2], mvalo[2], first= 1;
732 uiGetMouse(curarea->win, sval);
736 while (get_mbut() & L_MOUSE) {
737 uiGetMouse(curarea->win, mval);
739 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || first) {
740 /* clear completely, because of drawbuttons */
741 bt= ui_get_valid_link_button(block, but, mval);
743 bt->flag |= UI_ACTIVE;
747 bto->flag &= ~UI_ACTIVE;
753 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
755 glutil_draw_front_xor_line(sval[0], sval[1], mval[0], mval[1]);
762 else UI_wait_for_statechange();
766 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
770 if(but->type==LINK) ui_add_link(but, bt);
771 else ui_add_link(bt, but);
773 scrarea_queue_winredraw(curarea);
780 /* ************************************************ */
782 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
785 if(val) block->lockstr= lockstr;
788 void uiBlockClearButLock(uiBlock *block)
791 block->lockstr= NULL;
794 /* *************************************************************** */
796 /* XXX 2.50 no button editing */
799 static void setup_file(uiBlock *block)
804 fp= fopen("butsetup","w");
807 but= block->buttons.first;
810 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);
818 static void edit_but(uiBlock *block, uiBut *but, uiEvent *uevent)
820 short dx, dy, mval[2], mvalo[2], didit=0;
822 getmouseco_sc(mvalo);
824 if( !(get_mbut() & L_MOUSE) ) break;
827 dx= (mval[0]-mvalo[0]);
828 dy= (mval[1]-mvalo[1]);
835 glRectf(but->x1-2, but->y1-2, but->x2+2, but->y2+2);
837 if((uevent->qual & LR_SHIFTKEY)==0) {
845 ui_block_flush_back(but->block);
849 /* idle for this poor code */
850 else PIL_sleep_ms(30);
852 if(didit) setup_file(block);
856 /* XXX 2.50 no links supported yet */
858 static void ui_delete_active_linkline(uiBlock *block)
862 uiLinkLine *line, *nline;
865 but= block->buttons.first;
867 if(but->type==LINK && but->link) {
868 line= but->link->lines.first;
873 if(line->flag & UI_SELECT) {
874 BLI_remlink(&but->link->lines, line);
876 link= line->from->link;
878 /* are there more pointers allowed? */
881 if(*(link->totlink)==1) {
883 MEM_freeN(*(link->ppoin));
884 *(link->ppoin)= NULL;
888 for(a=0; a< (*(link->totlink)); a++) {
890 if( (*(link->ppoin))[a] != line->to->poin ) {
891 (*(link->ppoin))[b]= (*(link->ppoin))[a];
895 (*(link->totlink))--;
910 /* temporal! these buttons can be everywhere... */
911 allqueue(REDRAWBUTSLOGIC, 0);
914 static void ui_do_active_linklines(uiBlock *block, short *mval)
917 uiLinkLine *line, *act= NULL;
918 float mindist= 12.0, fac, v1[2], v2[2], v3[3];
925 /* find a line close to the mouse */
926 but= block->buttons.first;
928 if(but->type==LINK && but->link) {
930 line= but->link->lines.first;
932 v2[0]= line->from->x2;
933 v2[1]= (line->from->y1+line->from->y2)/2.0;
935 v3[1]= (line->to->y1+line->to->y2)/2.0;
937 fac= PdistVL2Dfl(v1, v2, v3);
949 /* check for a 'found one' to prevent going to 'frontbuffer' mode.
950 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
952 glDrawBuffer(GL_FRONT);
955 but= block->buttons.first;
957 if(but->type==LINK && but->link) {
958 line= but->link->lines.first;
961 if((line->flag & UI_SELECT)==0) {
962 line->flag |= UI_SELECT;
963 ui_draw_linkline(but, line);
966 else if(line->flag & UI_SELECT) {
967 line->flag &= ~UI_SELECT;
968 ui_draw_linkline(but, line);
976 glDrawBuffer(GL_BACK);
981 /* ******************************************************* */
983 /* XXX 2.50 no screendump supported yet */
986 /* nasty but safe way to store screendump rect */
987 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
989 static void ui_set_screendump_bbox(uiBlock *block)
994 scr_sizex= block->maxx - block->minx;
995 scr_sizey= block->maxy - block->miny;
998 scr_sizex= scr_sizey= 0;
1002 /* used for making screenshots for menus, called in screendump.c */
1003 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1005 if(scr_sizex!=0 && scr_sizey!=0) {
1017 /* *********************** data get/set ***********************
1018 * this either works with the pointed to data, or can work with
1019 * an edit override pointer while dragging for example */
1021 /* for buttons pointing to color for example */
1022 void ui_get_but_vectorf(uiBut *but, float *vec)
1028 VECCOPY(vec, but->editvec);
1035 vec[0]= vec[1]= vec[2]= 0.0f;
1037 if(RNA_property_type(&but->rnapoin, prop) == PROP_FLOAT) {
1038 tot= RNA_property_array_length(&but->rnapoin, prop);
1041 for(a=0; a<tot; a++)
1042 vec[a]= RNA_property_float_get_array(&but->rnapoin, prop, a);
1045 else if(but->pointype == CHA) {
1046 char *cp= (char *)but->poin;
1047 vec[0]= ((float)cp[0])/255.0;
1048 vec[1]= ((float)cp[1])/255.0;
1049 vec[2]= ((float)cp[2])/255.0;
1051 else if(but->pointype == FLO) {
1052 float *fp= (float *)but->poin;
1057 /* for buttons pointing to color for example */
1058 void ui_set_but_vectorf(uiBut *but, float *vec)
1064 VECCOPY(but->editvec, vec);
1071 if(RNA_property_type(&but->rnapoin, prop) == PROP_FLOAT) {
1072 tot= RNA_property_array_length(&but->rnapoin, prop);
1075 for(a=0; a<tot; a++)
1076 RNA_property_float_set_array(&but->rnapoin, prop, a, vec[a]);
1079 else if(but->pointype == CHA) {
1080 char *cp= (char *)but->poin;
1081 cp[0]= (char)(0.5 +vec[0]*255.0);
1082 cp[1]= (char)(0.5 +vec[1]*255.0);
1083 cp[2]= (char)(0.5 +vec[2]*255.0);
1085 else if(but->pointype == FLO) {
1086 float *fp= (float *)but->poin;
1091 int ui_is_but_float(uiBut *but)
1093 if(but->pointype==FLO && but->poin)
1096 if(but->rnaprop && RNA_property_type(&but->rnapoin, but->rnaprop) == PROP_FLOAT)
1102 double ui_get_but_val(uiBut *but)
1107 if(but->editval) { return *(but->editval); }
1108 if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1113 switch(RNA_property_type(&but->rnapoin, prop)) {
1115 if(RNA_property_array_length(&but->rnapoin, prop))
1116 value= RNA_property_boolean_get_array(&but->rnapoin, prop, but->rnaindex);
1118 value= RNA_property_boolean_get(&but->rnapoin, prop);
1121 if(RNA_property_array_length(&but->rnapoin, prop))
1122 value= RNA_property_int_get_array(&but->rnapoin, prop, but->rnaindex);
1124 value= RNA_property_int_get(&but->rnapoin, prop);
1127 if(RNA_property_array_length(&but->rnapoin, prop))
1128 value= RNA_property_float_get_array(&but->rnapoin, prop, but->rnaindex);
1130 value= RNA_property_float_get(&but->rnapoin, prop);
1133 value= RNA_property_enum_get(&but->rnapoin, prop);
1140 else if(but->type== HSVSLI) {
1143 fp= (but->editvec)? but->editvec: (float *)but->poin;
1144 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1146 switch(but->str[0]) {
1147 case 'H': value= h; break;
1148 case 'S': value= s; break;
1149 case 'V': value= v; break;
1152 else if( but->pointype == CHA ) {
1153 value= *(char *)but->poin;
1155 else if( but->pointype == SHO ) {
1156 value= *(short *)but->poin;
1158 else if( but->pointype == INT ) {
1159 value= *(int *)but->poin;
1161 else if( but->pointype == FLO ) {
1162 value= *(float *)but->poin;
1168 void ui_set_but_val(uiBut *but, double value)
1172 /* value is a hsv value: convert to rgb */
1176 if(RNA_property_editable(&but->rnapoin, prop)) {
1177 switch(RNA_property_type(&but->rnapoin, prop)) {
1179 if(RNA_property_array_length(&but->rnapoin, prop))
1180 RNA_property_boolean_set_array(&but->rnapoin, prop, but->rnaindex, value);
1182 RNA_property_boolean_set(&but->rnapoin, prop, value);
1185 if(RNA_property_array_length(&but->rnapoin, prop))
1186 RNA_property_int_set_array(&but->rnapoin, prop, but->rnaindex, value);
1188 RNA_property_int_set(&but->rnapoin, prop, value);
1191 if(RNA_property_array_length(&but->rnapoin, prop))
1192 RNA_property_float_set_array(&but->rnapoin, prop, but->rnaindex, value);
1194 RNA_property_float_set(&but->rnapoin, prop, value);
1197 RNA_property_enum_set(&but->rnapoin, prop, value);
1204 else if(but->pointype==0);
1205 else if(but->type==HSVSLI ) {
1208 fp= (but->editvec)? but->editvec: (float *)but->poin;
1209 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1211 switch(but->str[0]) {
1212 case 'H': h= value; break;
1213 case 'S': s= value; break;
1214 case 'V': v= value; break;
1217 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1221 /* first do rounding */
1222 if(but->pointype==CHA)
1223 value= (char)floor(value+0.5);
1224 else if(but->pointype==SHO ) {
1225 /* gcc 3.2.1 seems to have problems
1226 * casting a double like 32772.0 to
1227 * a short so we cast to an int, then
1230 gcckludge = (int) floor(value+0.5);
1231 value= (short)gcckludge;
1233 else if(but->pointype==INT )
1234 value= (int)floor(value+0.5);
1235 else if(but->pointype==FLO ) {
1236 float fval= (float)value;
1237 if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f; /* prevent negative zero */
1241 /* then set value with possible edit override */
1243 *but->editval= value;
1244 else if(but->pointype==CHA)
1245 *((char *)but->poin)= (char)value;
1246 else if(but->pointype==SHO)
1247 *((short *)but->poin)= (short)value;
1248 else if(but->pointype==INT)
1249 *((int *)but->poin)= (int)value;
1250 else if(but->pointype==FLO)
1251 *((float *)but->poin)= (float)value;
1254 /* update select flag */
1258 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1263 buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1266 /* string was too long, we have to truncate */
1267 BLI_strncpy(str, buf, maxlen);
1272 BLI_strncpy(str, but->poin, maxlen);
1276 void ui_set_but_string(uiBut *but, const char *str)
1279 if(RNA_property_editable(&but->rnapoin, but->rnaprop))
1280 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1283 BLI_strncpy(but->poin, str, but->max);
1286 /* ******************* Font ********************/
1288 static void ui_set_ftf_font(float aspect)
1290 #ifdef INTERNATIONAL
1292 FTF_SetFontSize('l');
1294 else if(aspect<1.59) {
1295 FTF_SetFontSize('m');
1298 FTF_SetFontSize('s');
1303 void uiSetCurFont(uiBlock *block, int index)
1305 ui_set_ftf_font(block->aspect);
1307 if(block->aspect<0.60) {
1308 block->curfont= UIfont[index].xl;
1310 else if(block->aspect<1.15) {
1311 block->curfont= UIfont[index].large;
1313 else if(block->aspect<1.59) {
1314 block->curfont= UIfont[index].medium;
1317 block->curfont= UIfont[index].small;
1320 if(block->curfont==NULL) block->curfont= UIfont[index].large;
1321 if(block->curfont==NULL) block->curfont= UIfont[index].medium;
1322 if(block->curfont==NULL) printf("error block no font %s\n", block->name);
1326 /* called by node editor */
1327 void *uiSetCurFont_ext(float aspect)
1331 ui_set_ftf_font(aspect);
1334 curfont= UIfont[0].xl;
1336 else if(aspect<1.15) {
1337 curfont= UIfont[0].large;
1339 else if(aspect<1.59) {
1340 curfont= UIfont[0].medium;
1343 curfont= UIfont[0].small;
1346 if(curfont==NULL) curfont= UIfont[0].large;
1347 if(curfont==NULL) curfont= UIfont[0].medium;
1352 void uiDefFont(unsigned int index, void *xl, void *large, void *medium, void *small)
1354 if(index>=UI_ARRAY) return;
1356 UIfont[index].xl= xl;
1357 UIfont[index].large= large;
1358 UIfont[index].medium= medium;
1359 UIfont[index].small= small;
1362 /* ******************* Free ********************/
1364 static void ui_free_link(uiLink *link)
1367 BLI_freelistN(&link->lines);
1372 static void ui_free_but(const bContext *C, uiBut *but)
1375 WM_operator_properties_free(but->opptr);
1376 MEM_freeN(but->opptr);
1378 if(but->active) ui_button_active_cancel(C, but);
1379 if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1380 ui_free_link(but->link);
1385 void uiFreeBlock(const bContext *C, uiBlock *block)
1389 while( (but= block->buttons.first) ) {
1390 BLI_remlink(&block->buttons, but);
1391 ui_free_but(C, but);
1395 block->panel->active= 0;
1397 BLI_freelistN(&block->saferct);
1402 void uiFreeBlocks(const bContext *C, ListBase *lb)
1406 while( (block= lb->first) ) {
1407 BLI_remlink(lb, block);
1408 uiFreeBlock(C, block);
1412 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1414 uiBlock *block, *nextblock;
1416 for(block=lb->first; block; block=nextblock) {
1417 nextblock= block->next;
1419 if(!block->handle) {
1420 if(!block->active) {
1421 BLI_remlink(lb, block);
1422 uiFreeBlock(C, block);
1430 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, char *name, short dt, short font)
1433 uiBlock *block, *oldblock= NULL;
1435 int getsizex, getsizey;
1437 window= CTX_wm_window(C);
1438 lb= ®ion->uiblocks;
1440 /* each listbase only has one block with this name, free block
1441 * if is already there so it can be rebuilt from scratch */
1443 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1444 if (BLI_streq(oldblock->name, name))
1448 oldblock->active= 0;
1449 oldblock->panel= NULL;
1453 block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1454 block->oldblock= oldblock;
1457 /* at the beginning of the list! for dynamical menus/blocks */
1459 BLI_addhead(lb, block);
1461 BLI_strncpy(block->name, name, sizeof(block->name));
1466 /* window where queue event should be added, pretty weak this way!
1467 this is because the 'mainwin' pup menu's */
1468 block->winq= mywinget();
1472 block->themecol= TH_AUTO;
1474 /* window matrix and aspect */
1475 if(region->swinid) {
1476 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1477 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1479 /* TODO - investigate why block->winmat[0][0] is negative
1480 * in the image view when viewRedrawForce is called */
1481 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1484 /* no subwindow created yet, for menus for example, so we
1485 * use the main window instead, since buttons are created
1487 wm_subwindow_getmatrix(window, window->winid, block->winmat);
1488 wm_subwindow_getsize(window, window->winid, &getsizex, &getsizey);
1490 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1491 block->auto_open= 2;
1494 uiSetCurFont(block, font);
1499 uiBlock *uiGetBlock(char *name, ARegion *ar)
1501 uiBlock *block= ar->uiblocks.first;
1504 if( strcmp(name, block->name)==0 ) return block;
1511 void ui_check_but(uiBut *but)
1513 /* if something changed in the button */
1517 int transopts= ui_translate_buttons();
1522 if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1524 /* test for min and max, icon sliders, etc */
1525 switch( but->type ) {
1531 value= ui_get_but_val(but);
1532 if(value < but->min) ui_set_but_val(but, but->min);
1533 else if(value > but->max) ui_set_but_val(but, but->max);
1537 value= fabs( ui_get_but_val(but) );
1538 if(value < but->min) ui_set_but_val(but, but->min);
1539 else if(value > but->max) ui_set_but_val(but, but->max);
1544 if(but->flag & UI_SELECT) but->iconadd= 1;
1545 else but->iconadd= 0;
1549 value= ui_get_but_val(but);
1550 but->iconadd= (int)value- (int)(but->min);
1554 value= ui_get_but_val(but);
1555 but->iconadd= (int)value- (int)(but->min);
1560 /* safety is 4 to enable small number buttons (like 'users') */
1561 if(but->type==NUMSLI || but->type==HSVSLI)
1562 okwidth= -4 + (but->x2 - but->x1)/2.0;
1564 okwidth= -4 + (but->x2 - but->x1);
1567 switch( but->type ) {
1572 if(but->x2 - but->x1 > 24) {
1573 value= ui_get_but_val(but);
1574 ui_set_name_menu(but, (int)value);
1583 value= ui_get_but_val(but);
1585 if(ui_is_but_float(but)) {
1586 if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
1587 else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
1588 else if(but->a2) { /* amount of digits defined */
1589 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1590 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1591 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1592 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1595 if(but->max<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1596 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1600 sprintf(but->drawstr, "%s%d", but->str, (int)value);
1605 if(ui_is_but_float(but)) {
1606 value= ui_get_but_val(but);
1607 if(but->a2) { /* amount of digits defined */
1608 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1609 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1610 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1611 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1614 sprintf(but->drawstr, "%s%.2f", but->str, value);
1617 else strcpy(but->drawstr, but->str);
1622 id= *(but->idpoin_idpp);
1623 strcpy(but->drawstr, but->str);
1624 if(id) strcat(but->drawstr, id->name+2);
1629 char str[UI_MAX_DRAW_STR];
1631 ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1633 strcpy(but->drawstr, but->str);
1634 strcat(but->drawstr, str);
1639 strcpy(but->drawstr, but->str);
1640 if (but->flag & UI_SELECT) {
1641 strcat(but->drawstr, "Press a key");
1643 strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1648 /* trying to get the dual-icon to left of text... not very nice */
1650 strcpy(but->drawstr, " ");
1651 strcpy(but->drawstr+2, but->str);
1655 strcpy(but->drawstr, but->str);
1659 /* if we are doing text editing, this will override the drawstr */
1661 strcpy(but->drawstr, but->str);
1662 strcat(but->drawstr, but->editstr);
1665 if(but->drawstr[0]) {
1666 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr, transopts);
1667 // here should be check for less space for icon offsets...
1668 if(but->type==MENU) okwidth -= 15;
1673 /* automatic width */
1674 if(but->x2==0.0f && but->x1 > 0.0f) {
1675 but->x2= (but->x1+but->strwidth+6);
1678 if(but->strwidth==0) but->drawstr[0]= 0;
1679 else if(but->type==BUTM || but->type==BLOCK); // no clip string, uiTextBoundsBlock is used (hack!)
1682 /* calc but->ofs, to draw the string shorter if too long */
1685 while(but->strwidth > (int)okwidth ) {
1687 if ELEM3(but->type, NUM, NUMABS, TEX) { // only these cut off left
1689 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr+but->ofs, transopts);
1691 /* textbut exception */
1692 if(but->editstr && but->pos != -1) {
1693 pos= but->pos+strlen(but->str);
1694 if(pos-1 < but->ofs) {
1695 pos= but->ofs-pos+1;
1701 but->drawstr[ strlen(but->drawstr)-pos ]= 0;
1706 but->drawstr[ strlen(but->drawstr)-1 ]= 0;
1707 but->strwidth= but->aspect*UI_GetStringWidth(but->font, but->drawstr, transopts);
1710 if(but->strwidth < 10) break;
1715 static int ui_auto_themecol(uiBut *but)
1720 return TH_BUT_ACTION;
1727 return TH_BUT_SETTING;
1735 return TH_BUT_TEXTFIELD;
1740 // (weak!) detect if it is a blockloop
1741 if(but->block->dt == UI_EMBOSSP) return TH_MENU_ITEM;
1742 return TH_BUT_POPUP;
1746 return TH_BUT_NEUTRAL;
1750 void uiBlockBeginAlign(uiBlock *block)
1752 /* if other align was active, end it */
1753 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1755 block->flag |= UI_BUT_ALIGN_DOWN;
1756 /* buttons declared after this call will this align flag */
1759 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1763 dx= fabs( but1->x2 - but2->x1);
1764 dy= fabs( but1->y1 - but2->y2);
1766 if(dx > dy) return 0;
1770 void uiBlockEndAlign(uiBlock *block)
1772 uiBut *prev, *but=NULL, *next;
1773 int flag= 0, cols=0, rows=0;
1774 int theme= UI_GetThemeValue(TH_BUT_DRAWTYPE);
1776 if ( !(ELEM4(theme, TH_MINIMAL, TH_SHADED, TH_ROUNDED, TH_ROUNDSHADED)) ) {
1777 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1782 - go back to first button of align start (ALIGN_DOWN)
1783 - compare triples, and define flags
1785 prev= block->buttons.last;
1787 if( (prev->flag & UI_BUT_ALIGN_DOWN)) but= prev;
1790 if(but && but->next) {
1791 if(buts_are_horiz(but, but->next)) cols++;
1797 if(but==NULL) return;
1799 /* rows==0: 1 row, cols==0: 1 collumn */
1801 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1806 /* clear old flag */
1807 but->flag &= ~UI_BUT_ALIGN_DOWN;
1809 if(flag==0) { /* first case */
1811 if(buts_are_horiz(but, next)) {
1813 flag= UI_BUT_ALIGN_RIGHT;
1815 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
1818 flag= UI_BUT_ALIGN_DOWN;
1822 else if(next==NULL) { /* last case */
1824 if(buts_are_horiz(prev, but)) {
1826 flag= UI_BUT_ALIGN_LEFT;
1828 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
1830 else flag= UI_BUT_ALIGN_TOP;
1833 else if(buts_are_horiz(but, next)) {
1834 /* check if this is already second row */
1835 if( prev && buts_are_horiz(prev, but)==0) {
1836 flag |= UI_BUT_ALIGN_TOP;
1837 /* exception case: bottom row */
1841 if(bt->next && buts_are_horiz(bt, bt->next)==0 ) break;
1844 if(bt==0) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
1847 else flag |= UI_BUT_ALIGN_LEFT;
1851 flag |= UI_BUT_ALIGN_TOP;
1853 else { /* next button switches to new row */
1854 if( (flag & UI_BUT_ALIGN_TOP)==0) { /* stil top row */
1856 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
1858 flag |= UI_BUT_ALIGN_DOWN;
1861 flag |= UI_BUT_ALIGN_TOP;
1867 /* merge coordinates */
1871 but->x1= (prev->x2+but->x1)/2.0;
1875 but->y2= (prev->y1+but->y2)/2.0;
1879 if(buts_are_horiz(prev, but)) {
1880 but->x1= (prev->x2+but->x1)/2.0;
1882 /* copy height too */
1885 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
1886 /* the previous button is a single one in its row */
1887 but->y2= (prev->y1+but->y2)/2.0;
1891 /* the previous button is not a single one in its row */
1901 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1905 static void uiBlockEndAligno(uiBlock *block)
1909 /* correct last defined button */
1910 but= block->buttons.last;
1912 /* vertical align case */
1913 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_DOWN) ) {
1914 but->flag &= ~UI_BUT_ALIGN_DOWN;
1916 /* horizontal align case */
1917 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT) ) {
1918 but->flag &= ~UI_BUT_ALIGN_RIGHT;
1920 /* else do nothing, manually provided flags */
1922 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1927 ui_def_but is the function that draws many button types
1930 "a1" Click Step (how much to change the value each click)
1931 "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
1932 all greater values will be clamped to 4.
1935 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)
1940 if(type & BUTPOIN) { /* a pointer is required */
1942 /* if pointer is zero, button is removed and not drawn */
1943 UI_ThemeColor(block->themecol);
1944 glRects(x1, y1, x1+x2, y1+y2);
1949 but= MEM_callocN(sizeof(uiBut), "uiBut");
1951 but->type= type & BUTTYPE;
1952 but->pointype= type & BUTPOIN;
1953 but->bit= type & BIT;
1954 but->bitnr= type & 31;
1957 BLI_addtail(&block->buttons, but);
1959 but->retval= retval;
1960 if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
1961 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
1962 strcpy(but->str, str);
1965 but->str= but->strdata;
1966 strcpy(but->str, str);
1970 if(block->autofill) {
1985 but->font= block->curfont;
1987 but->lock= block->lock;
1988 but->lockstr= block->lockstr;
1990 but->aspect= block->aspect;
1991 but->win= block->win;
1992 but->block= block; // pointer back, used for frontbuffer status, and picker
1994 if(block->themecol==TH_AUTO) but->themecol= ui_auto_themecol(but);
1995 else but->themecol= block->themecol;
1997 if(but->type != BUTM) {
1998 but->func= block->func;
1999 but->func_arg1= block->func_arg1;
2000 but->func_arg2= block->func_arg2;
2003 ui_set_embossfunc(but, block->dt);
2005 but->pos= -1; /* cursor invisible */
2007 if(ELEM(but->type, NUM, NUMABS)) { /* add a space to name */
2008 slen= strlen(but->str);
2009 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2010 if(but->str[slen-1]!=' ') {
2011 but->str[slen]= ' ';
2012 but->str[slen+1]= 0;
2017 if(but->type==HSVCUBE) { /* hsv buttons temp storage */
2019 ui_get_but_vectorf(but, rgb);
2020 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2023 if ELEM8(but->type, HSVSLI , NUMSLI, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM) {
2024 but->flag |= UI_TEXT_LEFT;
2027 if(but->type==BUT_TOGDUAL) {
2028 but->flag |= UI_ICON_LEFT;
2031 if(but->type==ROUNDBOX)
2032 but->flag |= UI_NO_HILITE;
2034 but->flag |= (block->flag & UI_BUT_ALIGN);
2035 if(block->flag & UI_BLOCK_NO_HILITE)
2036 but->flag |= UI_NO_HILITE;
2041 uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, char *tip)
2045 PropertyType proptype;
2048 prop= RNA_struct_find_property(ptr, propname);
2052 proptype= RNA_property_type(ptr, prop);
2054 /* use rna values if parameters are not specified */
2056 if(type == MENU && proptype == PROP_ENUM) {
2057 const EnumPropertyItem *item;
2061 RNA_property_enum_items(ptr, prop, &item, &totitem);
2063 dynstr= BLI_dynstr_new();
2064 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(ptr, prop));
2065 for(i=0; i<totitem; i++)
2066 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2067 str= BLI_dynstr_get_cstring(dynstr);
2068 BLI_dynstr_free(dynstr);
2073 str= (char*)RNA_property_ui_name(ptr, prop);
2077 tip= (char*)RNA_property_ui_description(ptr, prop);
2079 if(min == max || a1 == -1 || a2 == -1) {
2080 if(proptype == PROP_INT) {
2081 int softmin, softmax, step;
2083 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2094 else if(proptype == PROP_FLOAT) {
2095 float softmin, softmax, step, precision;
2097 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2108 else if(proptype == PROP_STRING) {
2110 max= RNA_property_string_maxlength(ptr, prop);
2111 if(max == 0) /* interface code should ideally support unlimited length */
2112 max= UI_MAX_DRAW_STR;
2116 /* now create button */
2117 but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2120 but->rnaindex= index;
2128 uiBut *ui_def_but_operator(bContext *C, uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2131 char buf[100], *butstr;
2134 wmOperatorType *ot= WM_operatortype_find(opname);
2135 str= (ot)? ot->name: "";
2141 if(WM_key_event_operator_string(C, opname, opcontext, buf, sizeof(buf))) {
2142 butstr= MEM_mallocN(strlen(str)+strlen(buf)+2, "ui_def_but_operator");
2143 strcpy(butstr, str);
2144 strcat(butstr, "|");
2145 strcat(butstr, buf);
2149 but= ui_def_but(block, type, -1, butstr, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2150 but->opname= opname;
2151 but->opcontext= opcontext;
2159 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)
2161 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2168 /* if _x_ is a power of two (only one bit) return the power,
2169 * otherwise return -1.
2170 * (1<<findBitIndex(x))==x for powers of two.
2172 static int findBitIndex(unsigned int x) {
2173 if (!x || (x&(x-1))!=0) { /* x&(x-1) strips lowest bit */
2178 if (x&0xFFFF0000) idx+=16, x>>=16;
2179 if (x&0xFF00) idx+=8, x>>=8;
2180 if (x&0xF0) idx+=4, x>>=4;
2181 if (x&0xC) idx+=2, x>>=2;
2188 /* autocomplete helper functions */
2189 struct AutoComplete {
2195 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2197 AutoComplete *autocpl;
2199 autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2200 autocpl->maxlen= maxlen;
2201 autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2202 autocpl->startname= startname;
2207 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2209 char *truncate= autocpl->truncate;
2210 char *startname= autocpl->startname;
2213 for(a=0; a<autocpl->maxlen-1; a++) {
2214 if(startname[a]==0 || startname[a]!=name[a])
2218 if(startname[a]==0) {
2221 BLI_strncpy(truncate, name, autocpl->maxlen);
2223 /* remove from truncate what is not in bone->name */
2224 for(a=0; a<autocpl->maxlen-1; a++) {
2225 if(truncate[a]!=name[a])
2232 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2234 if(autocpl->truncate[0])
2235 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2237 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2239 MEM_freeN(autocpl->truncate);
2243 /* autocomplete callback for ID buttons */
2244 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2246 int blocktype= (intptr_t)arg_v;
2247 ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2249 if(listb==NULL) return;
2251 /* search if str matches the beginning of an ID struct */
2253 AutoComplete *autocpl= autocomplete_begin(str, 22);
2256 for(id= listb->first; id; id= id->next)
2257 autocomplete_do_name(autocpl, id->name+2);
2259 autocomplete_end(autocpl, str);
2263 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)
2265 int bitIdx= findBitIndex(bit);
2269 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2272 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)
2274 return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2276 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)
2278 return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2280 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)
2282 return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2284 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)
2286 return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2288 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)
2290 return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2292 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)
2294 return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2296 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)
2298 return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2300 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)
2302 return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2304 uiBut *uiDefButR(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, char *tip)
2308 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2314 uiBut *uiDefButO(bContext *C, uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2318 but= ui_def_but_operator(C, block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2325 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)
2327 uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2329 but->icon= (BIFIconID) icon;
2330 but->flag|= UI_HAS_ICON;
2336 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)
2338 int bitIdx= findBitIndex(bit);
2342 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2346 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)
2348 return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2350 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)
2352 return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2354 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)
2356 return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2358 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)
2360 return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2362 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)
2364 return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2366 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)
2368 return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2370 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)
2372 return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2374 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)
2376 return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2378 uiBut *uiDefIconButR(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, char *tip)
2382 but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2384 but->icon= (BIFIconID) icon;
2385 but->flag|= UI_HAS_ICON;
2391 uiBut *uiDefIconButO(bContext *C, uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2395 but= ui_def_but_operator(C, block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2397 but->icon= (BIFIconID) icon;
2398 but->flag|= UI_HAS_ICON;
2405 /* Button containing both string label and icon */
2406 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)
2408 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2410 but->icon= (BIFIconID) icon;
2411 but->flag|= UI_HAS_ICON;
2413 but->flag|= UI_ICON_LEFT;
2419 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)
2421 int bitIdx= findBitIndex(bit);
2425 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2429 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)
2431 return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2433 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)
2435 return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2437 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)
2439 return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2441 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)
2443 return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2445 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)
2447 return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2449 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)
2451 return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2453 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)
2455 return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2457 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)
2459 return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2461 uiBut *uiDefIconTextButR(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2, char *tip)
2465 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2467 but->icon= (BIFIconID) icon;
2468 but->flag|= UI_HAS_ICON;
2469 but->flag|= UI_ICON_LEFT;
2475 uiBut *uiDefIconTextButO(bContext *C, uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2479 but= ui_def_but_operator(C, block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2481 but->icon= (BIFIconID) icon;
2482 but->flag|= UI_HAS_ICON;
2483 but->flag|= UI_ICON_LEFT;
2490 /* END Button containing both string label and icon */
2492 void uiAutoBlock(uiBlock *block, float minx, float miny, float sizex, float sizey, int flag)
2495 block->maxx= minx+sizex;
2497 block->maxy= miny+sizey;
2499 block->autofill= flag; /* also check for if it has to be done */
2503 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2507 link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2512 link->fromcode= from;
2516 /* cruft to make uiBlock and uiBut private */
2518 int uiBlocksGetYMin(ListBase *lb)
2523 for (block= lb->first; block; block= block->next)
2524 if (block==lb->first || block->miny<min)
2530 int uiBlockGetCol(uiBlock *block)
2532 return block->themecol;
2534 void uiBlockSetCol(uiBlock *block, int col)
2536 block->themecol= col;
2538 void uiBlockSetEmboss(uiBlock *block, int emboss)
2542 void uiBlockSetDirection(uiBlock *block, int direction)
2544 block->direction= direction;
2547 /* this call escapes if there's alignment flags */
2548 void uiBlockFlipOrder(uiBlock *block)
2552 float centy, miny=10000, maxy= -10000;
2554 // if(U.uiflag & USER_PLAINMENUS)
2557 for(but= block->buttons.first; but; but= but->next) {
2558 if(but->flag & UI_BUT_ALIGN) return;
2559 if(but->y1 < miny) miny= but->y1;
2560 if(but->y2 > maxy) maxy= but->y2;
2563 centy= (miny+maxy)/2.0;
2564 for(but= block->buttons.first; but; but= but->next) {
2565 but->y1 = centy-(but->y1-centy);
2566 but->y2 = centy-(but->y2-centy);
2567 SWAP(float, but->y1, but->y2);
2570 /* also flip order in block itself, for example for arrowkey */
2571 lb.first= lb.last= NULL;
2572 but= block->buttons.first;
2575 BLI_remlink(&block->buttons, but);
2576 BLI_addtail(&lb, but);
2583 void uiBlockSetFlag(uiBlock *block, int flag)
2587 void uiBlockSetXOfs(uiBlock *block, int xofs)
2591 void* uiBlockGetCurFont(uiBlock *block)
2593 return block->curfont;
2596 void uiButSetFlag(uiBut *but, int flag)
2600 void uiButClearFlag(uiBut *but, int flag)
2605 int uiButGetRetVal(uiBut *but)
2610 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2612 if(but->opname && !but->opptr) {
2613 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2614 WM_operator_properties_create(but->opptr, but->opname);
2620 void uiBlockSetHandleFunc(uiBlock *block, void (*func)(struct bContext *C, void *arg, int event), void *arg)
2622 block->handle_func= func;
2623 block->handle_func_arg= arg;
2626 void uiBlockSetButmFunc(uiBlock *block, void (*func)(struct bContext *C, void *arg, int but_a2), void *arg)
2628 block->butm_func= func;
2629 block->butm_func_arg= arg;
2632 void uiBlockSetFunc(uiBlock *block, void (*func)(struct bContext *C, void *arg1, void *arg2), void *arg1, void *arg2)
2635 block->func_arg1= arg1;
2636 block->func_arg2= arg2;
2639 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)())
2641 block->drawextra= func;
2644 void uiButSetFunc(uiBut *but, void (*func)(struct bContext *C, void *arg1, void *arg2), void *arg1, void *arg2)
2647 but->func_arg1= arg1;
2648 but->func_arg2= arg2;
2651 void uiButSetCompleteFunc(uiBut *but, void (*func)(struct bContext *C, char *str, void *arg), void *arg)
2653 but->autocomplete_func= func;
2654 but->autofunc_arg= arg;
2657 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2659 uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2660 but->idpoin_func= func;
2661 but->idpoin_idpp= (ID**) idpp;
2665 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2670 uiBut *uiDefBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2672 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2673 but->block_func= func;
2678 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2680 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2681 but->block_func= func;
2686 /* Block button containing both string label and icon */
2687 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2689 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2691 but->icon= (BIFIconID) icon;
2692 but->flag|= UI_HAS_ICON;
2694 but->flag|= UI_ICON_LEFT;
2695 but->flag|= UI_ICON_RIGHT;
2697 but->block_func= func;
2703 /* Block button containing icon */
2704 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
2706 uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2708 but->icon= (BIFIconID) icon;
2709 but->flag|= UI_HAS_ICON;
2711 but->flag|= UI_ICON_LEFT;
2712 but->flag|= UI_ICON_RIGHT;
2714 but->block_func= func;
2720 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
2722 uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
2726 /* Program Init/Exit */
2731 BMF_GetFont(BMF_kHelveticaBold14),
2732 BMF_GetFont(BMF_kHelveticaBold12),
2733 BMF_GetFont(BMF_kHelveticaBold10),
2734 BMF_GetFont(BMF_kHelveticaBold8));
2736 BMF_GetFont(BMF_kHelvetica12),
2737 BMF_GetFont(BMF_kHelvetica12),
2738 BMF_GetFont(BMF_kHelvetica10),
2739 BMF_GetFont(BMF_kHelveticaBold8));
2741 ui_resources_init();
2744 void UI_init_userdef()
2746 ui_text_init_userdef();
2747 ui_theme_init_userdef();
2752 ui_resources_free();