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_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_texture_types.h"
39 #include "DNA_userdef_types.h"
41 #include "BLI_arithb.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_dynstr.h"
45 #include "BKE_context.h"
46 #include "BKE_idprop.h"
47 #include "BKE_library.h"
48 #include "BKE_screen.h"
49 #include "BKE_texture.h"
50 #include "BKE_utildefines.h"
53 #include "BIF_glutil.h"
57 #include "UI_interface.h"
59 #include "ED_screen.h"
63 #include "wm_subwindow.h"
64 #include "wm_window.h"
66 #include "RNA_access.h"
67 #include "RNA_types.h"
69 #include "interface_intern.h"
71 #define MENU_WIDTH 120
72 #define MENU_ITEM_HEIGHT 20
73 #define MENU_SEP_HEIGHT 6
76 * a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt
78 * uiBlahBlah() external function
79 * ui_blah_blah() internal function
82 static void ui_free_but(const bContext *C, uiBut *but);
83 static void ui_rna_ID_autocomplete(bContext *C, char *str, void *arg_but);
85 /* ************* translation ************** */
87 int ui_translate_buttons()
89 return (U.transopts & USER_TR_BUTTONS);
92 int ui_translate_menus()
94 return (U.transopts & USER_TR_MENUS);
97 int ui_translate_tooltips()
99 return (U.transopts & USER_TR_TOOLTIPS);
102 /* ************* window matrix ************** */
104 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
107 int sx, sy, getsizex, getsizey;
109 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
110 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
118 gx += block->panel->ofsx;
119 gy += block->panel->ofsy;
122 *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
123 *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
126 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
133 ui_block_to_window_fl(ar, block, &fx, &fy);
139 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
144 ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
145 ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
147 winr->xmin= tmpr.xmin;
148 winr->ymin= tmpr.ymin;
149 winr->xmax= tmpr.xmax;
150 winr->ymax= tmpr.ymax;
153 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y) /* for mouse cursor */
155 float a, b, c, d, e, f, px, py;
156 int sx, sy, getsizex, getsizey;
158 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
159 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
163 a= .5*((float)getsizex)*block->winmat[0][0];
164 b= .5*((float)getsizex)*block->winmat[1][0];
165 c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
167 d= .5*((float)getsizey)*block->winmat[0][1];
168 e= .5*((float)getsizey)*block->winmat[1][1];
169 f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
174 *y= (a*(py-f) + d*(c-px))/(a*e-d*b);
175 *x= (px- b*(*y)- c)/a;
178 *x -= block->panel->ofsx;
179 *y -= block->panel->ofsy;
183 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
190 ui_window_to_block_fl(ar, block, &fx, &fy);
196 void ui_window_to_region(const ARegion *ar, int *x, int *y)
198 *x-= ar->winrct.xmin;
199 *y-= ar->winrct.ymin;
202 /* ******************* block calc ************************* */
204 void ui_block_translate(uiBlock *block, int x, int y)
208 for(bt= block->buttons.first; bt; bt=bt->next) {
221 static void ui_text_bounds_block(uiBlock *block, float offset)
223 uiStyle *style= U.uistyles.first; // XXX pass on as arg
225 int i = 0, j, x1addval= offset, nextcol;
227 uiStyleFontSet(&style->widget);
229 for(bt= block->buttons.first; bt; bt= bt->next) {
231 //int transopts= ui_translate_buttons();
232 //if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
234 j= BLF_width(bt->drawstr);
240 /* cope with multi collumns */
241 bt= block->buttons.first;
243 if(bt->next && bt->x1 < bt->next->x1)
248 bt->x2 = bt->x1 + i + block->bounds;
250 ui_check_but(bt); // clips text again
253 x1addval+= i + block->bounds;
259 void ui_bounds_block(uiBlock *block)
264 if(block->buttons.first==NULL) {
266 block->minx= 0.0; block->maxx= block->panel->sizex;
267 block->miny= 0.0; block->maxy= block->panel->sizey;
272 block->minx= block->miny= 10000;
273 block->maxx= block->maxy= -10000;
275 bt= block->buttons.first;
277 if(bt->x1 < block->minx) block->minx= bt->x1;
278 if(bt->y1 < block->miny) block->miny= bt->y1;
280 if(bt->x2 > block->maxx) block->maxx= bt->x2;
281 if(bt->y2 > block->maxy) block->maxy= bt->y2;
286 block->minx -= block->bounds;
287 block->miny -= block->bounds;
288 block->maxx += block->bounds;
289 block->maxy += block->bounds;
292 /* hardcoded exception... but that one is annoying with larger safety */
293 bt= block->buttons.first;
294 if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
297 block->safety.xmin= block->minx-xof;
298 block->safety.ymin= block->miny-xof;
299 block->safety.xmax= block->maxx+xof;
300 block->safety.ymax= block->maxy+xof;
303 static void ui_popup_bounds_block(const bContext *C, uiBlock *block, int menu)
305 wmWindow *window= CTX_wm_window(C);
306 int startx, starty, endx, endy, width, height;
307 int oldbounds, mx, my, xmax, ymax;
309 oldbounds= block->bounds;
311 /* compute mouse position with user defined offset */
312 ui_bounds_block(block);
313 mx= window->eventstate->x + block->minx + block->mx;
314 my= window->eventstate->y + block->miny + block->my;
316 wm_window_get_size(window, &xmax, &ymax);
318 /* first we ensure wide enough text bounds */
320 if(block->flag & UI_BLOCK_LOOP) {
322 ui_text_bounds_block(block, block->minx);
326 /* next we recompute bounds */
327 block->bounds= oldbounds;
328 ui_bounds_block(block);
330 /* and we adjust the position to fit within window */
331 width= block->maxx - block->minx;
332 height= block->maxy - block->miny;
334 startx= mx-(0.8*(width));
354 ui_block_translate(block, startx - block->minx, starty - block->miny);
356 /* now recompute bounds and safety */
357 ui_bounds_block(block);
360 /* used for various cases */
361 void uiBoundsBlock(uiBlock *block, int addval)
366 block->bounds= addval;
370 /* used for pulldowns */
371 void uiTextBoundsBlock(uiBlock *block, int addval)
373 block->bounds= addval;
377 /* used for block popups */
378 void uiPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
380 block->bounds= addval;
386 /* used for menu popups */
387 void uiMenuPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
389 block->bounds= addval;
395 /* ************** LINK LINE DRAWING ************* */
397 /* link line drawing is not part of buttons or theme.. so we stick with it here */
399 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
401 float vec1[2], vec2[2];
403 if(line->from==NULL || line->to==NULL) return;
405 vec1[0]= (line->from->x1+line->from->x2)/2.0;
406 vec1[1]= (line->from->y1+line->from->y2)/2.0;
407 vec2[0]= (line->to->x1+line->to->x2)/2.0;
408 vec2[1]= (line->to->y1+line->to->y2)/2.0;
410 if(line->flag & UI_SELECT) glColor3ub(100,100,100);
411 else glColor3ub(0,0,0);
412 fdrawline(vec1[0], vec1[1], vec2[0], vec2[1]);
415 static void ui_draw_links(uiBlock *block)
420 but= block->buttons.first;
422 if(but->type==LINK && but->link) {
423 line= but->link->lines.first;
425 ui_draw_linkline(but, line);
433 /* ************** BLOCK ENDING FUNCTION ************* */
435 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
437 /* various properties are being compared here, hopfully sufficient
438 * to catch all cases, but it is simple to add more checks later */
439 if(but->retval != oldbut->retval) return 0;
440 if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
441 if(but->rnaprop != oldbut->rnaprop)
442 if(but->rnaindex != oldbut->rnaindex) return 0;
443 if(but->func != oldbut->func) return 0;
444 if(but->funcN != oldbut->funcN) return 0;
445 if(oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return 0;
446 if(oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return 0;
447 if(!but->funcN && ((but->poin != oldbut->poin && (uiBut*)oldbut->poin != oldbut) || but->pointype != oldbut->pointype)) return 0;
452 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
458 oldblock= block->oldblock;
462 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
463 if(ui_but_equals_old(oldbut, but)) {
465 but->flag= oldbut->flag;
466 but->active= oldbut->active;
467 but->pos= oldbut->pos;
468 but->editstr= oldbut->editstr;
469 but->editval= oldbut->editval;
470 but->editvec= oldbut->editvec;
471 but->editcoba= oldbut->editcoba;
472 but->editcumap= oldbut->editcumap;
473 but->selsta= oldbut->selsta;
474 but->selend= oldbut->selend;
475 but->softmin= oldbut->softmin;
476 but->softmax= oldbut->softmax;
479 oldbut->active= NULL;
482 /* ensures one button can get activated, and in case the buttons
483 * draw are the same this gives O(1) lookup for each button */
484 BLI_remlink(&oldblock->buttons, oldbut);
485 ui_free_but(C, oldbut);
494 void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
498 char buf[512], *butstr;
500 /* only do it before bounding */
501 if(block->minx != block->maxx)
504 for(but=block->buttons.first; but; but=but->next) {
506 prop= (but->opptr)? but->opptr->data: NULL;
508 if(WM_key_event_operator_string(C, but->opname, but->opcontext, prop, buf, sizeof(buf))) {
509 butstr= MEM_mallocN(strlen(but->str)+strlen(buf)+2, "menu_block_set_keymaps");
510 strcpy(butstr, but->str);
514 but->str= but->strdata;
515 BLI_strncpy(but->str, butstr, sizeof(but->strdata));
524 void uiEndBlock(const bContext *C, uiBlock *block)
527 Scene *scene= CTX_data_scene(C);
529 /* inherit flags from 'old' buttons that was drawn here previous, based
530 * on matching buttons, we need this to make button event handling non
531 * blocking, while still alowing buttons to be remade each redraw as it
532 * is expected by blender code */
533 for(but=block->buttons.first; but; but=but->next) {
534 if(ui_but_update_from_old_block(C, block, but))
537 /* temp? Proper check for greying out */
539 wmOperatorType *ot= WM_operatortype_find(but->opname);
540 if(ot==NULL || (ot->poll && ot->poll((bContext *)C)==0)) {
541 but->flag |= UI_BUT_DISABLED;
546 /* only update soft range while not editing */
547 if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
548 ui_set_but_soft_range(but, ui_get_but_val(but));
550 ui_but_anim_flag(but, (scene)? scene->r.cfra: 0.0f);
553 if(block->oldblock) {
554 block->auto_open= block->oldblock->auto_open;
555 block->auto_open_last= block->oldblock->auto_open_last;
556 block->tooltipdisabled= block->oldblock->tooltipdisabled;
558 block->oldblock= NULL;
561 /* handle pending stuff */
562 if(block->flag & UI_BLOCK_LOOP) ui_menu_block_set_keymaps(C, block);
565 if(block->dobounds == 1) ui_bounds_block(block);
566 else if(block->dobounds == 2) ui_text_bounds_block(block, 0.0f);
567 else if(block->dobounds) ui_popup_bounds_block(C, block, (block->dobounds == 4));
569 if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
570 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
575 /* ************** BLOCK DRAWING FUNCTION ************* */
577 void ui_fontscale(short *points, float aspect)
579 if(aspect < 0.9f || aspect > 1.1f) {
580 float pointsf= *points;
582 /* for some reason scaling fonts goes too fast compared to widget size */
583 aspect= sqrt(aspect);
587 *points= ceil(pointsf);
589 *points= floor(pointsf);
593 /* project button or block (but==NULL) to pixels in regionspace */
594 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
597 float getsizex, getsizey;
602 gx= (but?but->x1:block->minx) + (block->panel?block->panel->ofsx:0.0f);
603 gy= (but?but->y1:block->miny) + (block->panel?block->panel->ofsy:0.0f);
605 rect->xmin= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
606 rect->ymin= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
608 gx= (but?but->x2:block->maxx) + (block->panel?block->panel->ofsx:0.0f);
609 gy= (but?but->y2:block->maxy) + (block->panel?block->panel->ofsy:0.0f);
611 rect->xmax= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
612 rect->ymax= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
615 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
616 void uiDrawBlock(const bContext *C, uiBlock *block)
618 uiStyle style= *((uiStyle *)U.uistyles.first); // XXX pass on as arg
623 /* get menu region or area region */
626 ar= CTX_wm_region(C);
629 uiEndBlock(C, block);
631 /* we set this only once */
632 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
635 ui_fontscale(&style.paneltitle.points, block->aspect);
636 ui_fontscale(&style.grouplabel.points, block->aspect);
637 ui_fontscale(&style.widgetlabel.points, block->aspect);
638 ui_fontscale(&style.widget.points, block->aspect);
640 /* scale block min/max to rect */
641 ui_but_to_pixelrect(&rect, ar, block, NULL);
643 /* pixel space for AA widgets */
647 wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
650 if(block->flag & UI_BLOCK_LOOP)
651 ui_draw_menu_back(&style, block, &rect);
652 else if(block->panel)
653 ui_draw_aligned_panel(ar, &style, block, &rect);
655 if(block->drawextra) block->drawextra(C, block);
658 for(but= block->buttons.first; but; but= but->next) {
659 ui_but_to_pixelrect(&rect, ar, block, but);
660 ui_draw_but(ar, &style, but, &rect);
666 ui_draw_links(block);
669 /* ************* EVENTS ************* */
671 static void ui_is_but_sel(uiBut *but)
675 short push=0, true=1;
677 value= ui_get_but_val(but);
679 if( but->type==TOGN || but->type==ICONTOGN) true= 0;
683 if( BTST(lvalue, (but->bitnr)) ) push= true;
692 if (value==-1) push= 1;
700 if(value!=but->hardmin) push= 1;
704 if(value==0.0) push= 1;
707 if(value == but->hardmax) push= 1;
719 else if(push==1) but->flag |= UI_SELECT;
720 else but->flag &= ~UI_SELECT;
723 /* XXX 2.50 no links supported yet */
726 static uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
730 /* find button to link to */
731 for (bt= block->buttons.first; bt; bt= bt->next)
732 if(bt!=but && uibut_contains_pt(bt, mval))
736 if (but->type==LINK && bt->type==INLINK) {
737 if( but->link->tocode == (int)bt->min ) {
741 else if(but->type==INLINK && bt->type==LINK) {
742 if( bt->link->tocode == (int)but->hardmin ) {
751 static int ui_is_a_link(uiBut *from, uiBut *to)
758 line= link->lines.first;
760 if(line->from==from && line->to==to) return 1;
767 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
771 but= block->buttons.first;
773 if(but->type==INLINK) {
774 if(but->poin == poin) return but;
781 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
785 line= MEM_callocN(sizeof(uiLinkLine), "linkline");
786 BLI_addtail(listb, line);
791 uiBut *uiFindInlink(uiBlock *block, void *poin)
793 return ui_find_inlink(block, poin);
796 void uiComposeLinks(uiBlock *block)
803 but= block->buttons.first;
805 if(but->type==LINK) {
808 /* for all pointers in the array */
812 for(a=0; a < *(link->totlink); a++) {
813 bt= ui_find_inlink(block, (*ppoin)[a] );
815 ui_add_link_line(&link->lines, but, bt);
819 else if(link->poin) {
820 bt= ui_find_inlink(block, *(link->poin) );
822 ui_add_link_line(&link->lines, but, bt);
831 static void ui_add_link(uiBut *from, uiBut *to)
833 /* in 'from' we have to add a link to 'to' */
838 if(ui_is_a_link(from, to)) {
839 printf("already exists\n");
845 /* are there more pointers allowed? */
847 oldppoin= *(link->ppoin);
849 (*(link->totlink))++;
850 *(link->ppoin)= MEM_callocN( *(link->totlink)*sizeof(void *), "new link");
852 for(a=0; a< (*(link->totlink))-1; a++) {
853 (*(link->ppoin))[a]= oldppoin[a];
855 (*(link->ppoin))[a]= to->poin;
857 if(oldppoin) MEM_freeN(oldppoin);
860 *(link->poin)= to->poin;
865 static int ui_do_but_LINK(uiBlock *block, uiBut *but)
868 * This button only visualizes, the dobutton mode
869 * can add a new link, but then the whole system
870 * should be redrawn/initialized.
873 uiBut *bt=0, *bto=NULL;
874 short sval[2], mval[2], mvalo[2], first= 1;
876 uiGetMouse(curarea->win, sval);
880 while (get_mbut() & L_MOUSE) {
881 uiGetMouse(curarea->win, mval);
883 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || first) {
884 /* clear completely, because of drawbuttons */
885 bt= ui_get_valid_link_button(block, but, mval);
887 bt->flag |= UI_ACTIVE;
891 bto->flag &= ~UI_ACTIVE;
892 ui_draw_but(ar, bto);
897 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
899 glutil_draw_front_xor_line(sval[0], sval[1], mval[0], mval[1]);
906 else UI_wait_for_statechange();
910 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
914 if(but->type==LINK) ui_add_link(but, bt);
915 else ui_add_link(bt, but);
917 scrarea_queue_winredraw(curarea);
924 /* ************************************************ */
926 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
930 block->lockstr= lockstr;
934 void uiBlockClearButLock(uiBlock *block)
937 block->lockstr= NULL;
940 /* *************************************************************** */
943 /* XXX 2.50 no links supported yet */
945 static void ui_delete_active_linkline(uiBlock *block)
949 uiLinkLine *line, *nline;
952 but= block->buttons.first;
954 if(but->type==LINK && but->link) {
955 line= but->link->lines.first;
960 if(line->flag & UI_SELECT) {
961 BLI_remlink(&but->link->lines, line);
963 link= line->from->link;
965 /* are there more pointers allowed? */
968 if(*(link->totlink)==1) {
970 MEM_freeN(*(link->ppoin));
971 *(link->ppoin)= NULL;
975 for(a=0; a< (*(link->totlink)); a++) {
977 if( (*(link->ppoin))[a] != line->to->poin ) {
978 (*(link->ppoin))[b]= (*(link->ppoin))[a];
982 (*(link->totlink))--;
997 /* temporal! these buttons can be everywhere... */
998 allqueue(REDRAWBUTSLOGIC, 0);
1001 static void ui_do_active_linklines(uiBlock *block, short *mval)
1004 uiLinkLine *line, *act= NULL;
1005 float mindist= 12.0, fac, v1[2], v2[2], v3[3];
1012 /* find a line close to the mouse */
1013 but= block->buttons.first;
1015 if(but->type==LINK && but->link) {
1017 line= but->link->lines.first;
1019 v2[0]= line->from->x2;
1020 v2[1]= (line->from->y1+line->from->y2)/2.0;
1021 v3[0]= line->to->x1;
1022 v3[1]= (line->to->y1+line->to->y2)/2.0;
1024 fac= PdistVL2Dfl(v1, v2, v3);
1036 /* check for a 'found one' to prevent going to 'frontbuffer' mode.
1037 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
1039 glDrawBuffer(GL_FRONT);
1042 but= block->buttons.first;
1044 if(but->type==LINK && but->link) {
1045 line= but->link->lines.first;
1048 if((line->flag & UI_SELECT)==0) {
1049 line->flag |= UI_SELECT;
1050 ui_draw_linkline(but, line);
1053 else if(line->flag & UI_SELECT) {
1054 line->flag &= ~UI_SELECT;
1055 ui_draw_linkline(but, line);
1063 glDrawBuffer(GL_BACK);
1068 /* ******************************************************* */
1070 /* XXX 2.50 no screendump supported yet */
1073 /* nasty but safe way to store screendump rect */
1074 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
1076 static void ui_set_screendump_bbox(uiBlock *block)
1081 scr_sizex= block->maxx - block->minx;
1082 scr_sizey= block->maxy - block->miny;
1085 scr_sizex= scr_sizey= 0;
1089 /* used for making screenshots for menus, called in screendump.c */
1090 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1092 if(scr_sizex!=0 && scr_sizey!=0) {
1104 /* *********************** data get/set ***********************
1105 * this either works with the pointed to data, or can work with
1106 * an edit override pointer while dragging for example */
1108 /* for buttons pointing to color for example */
1109 void ui_get_but_vectorf(uiBut *but, float *vec)
1115 VECCOPY(vec, but->editvec);
1122 vec[0]= vec[1]= vec[2]= 0.0f;
1124 if(RNA_property_type(prop) == PROP_FLOAT) {
1125 tot= RNA_property_array_length(prop);
1128 for(a=0; a<tot; a++)
1129 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1132 else if(but->pointype == CHA) {
1133 char *cp= (char *)but->poin;
1134 vec[0]= ((float)cp[0])/255.0;
1135 vec[1]= ((float)cp[1])/255.0;
1136 vec[2]= ((float)cp[2])/255.0;
1138 else if(but->pointype == FLO) {
1139 float *fp= (float *)but->poin;
1144 /* for buttons pointing to color for example */
1145 void ui_set_but_vectorf(uiBut *but, float *vec)
1151 VECCOPY(but->editvec, vec);
1158 if(RNA_property_type(prop) == PROP_FLOAT) {
1159 tot= RNA_property_array_length(prop);
1162 for(a=0; a<tot; a++)
1163 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1166 else if(but->pointype == CHA) {
1167 char *cp= (char *)but->poin;
1168 cp[0]= (char)(0.5 +vec[0]*255.0);
1169 cp[1]= (char)(0.5 +vec[1]*255.0);
1170 cp[2]= (char)(0.5 +vec[2]*255.0);
1172 else if(but->pointype == FLO) {
1173 float *fp= (float *)but->poin;
1178 int ui_is_but_float(uiBut *but)
1180 if(but->pointype==FLO && but->poin)
1183 if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1189 double ui_get_but_val(uiBut *but)
1194 if(but->editval) { return *(but->editval); }
1195 if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1200 switch(RNA_property_type(prop)) {
1202 if(RNA_property_array_length(prop))
1203 value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1205 value= RNA_property_boolean_get(&but->rnapoin, prop);
1208 if(RNA_property_array_length(prop))
1209 value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1211 value= RNA_property_int_get(&but->rnapoin, prop);
1214 if(RNA_property_array_length(prop))
1215 value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1217 value= RNA_property_float_get(&but->rnapoin, prop);
1220 value= RNA_property_enum_get(&but->rnapoin, prop);
1227 else if(but->type== HSVSLI) {
1230 fp= (but->editvec)? but->editvec: (float *)but->poin;
1231 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1233 switch(but->str[0]) {
1234 case 'H': value= h; break;
1235 case 'S': value= s; break;
1236 case 'V': value= v; break;
1239 else if( but->pointype == CHA ) {
1240 value= *(char *)but->poin;
1242 else if( but->pointype == SHO ) {
1243 value= *(short *)but->poin;
1245 else if( but->pointype == INT ) {
1246 value= *(int *)but->poin;
1248 else if( but->pointype == FLO ) {
1249 value= *(float *)but->poin;
1255 void ui_set_but_val(uiBut *but, double value)
1259 /* value is a hsv value: convert to rgb */
1263 if(RNA_property_editable(&but->rnapoin, prop)) {
1264 switch(RNA_property_type(prop)) {
1266 if(RNA_property_array_length(prop))
1267 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1269 RNA_property_boolean_set(&but->rnapoin, prop, value);
1272 if(RNA_property_array_length(prop))
1273 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, value);
1275 RNA_property_int_set(&but->rnapoin, prop, value);
1278 if(RNA_property_array_length(prop))
1279 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1281 RNA_property_float_set(&but->rnapoin, prop, value);
1284 RNA_property_enum_set(&but->rnapoin, prop, value);
1291 else if(but->pointype==0);
1292 else if(but->type==HSVSLI ) {
1295 fp= (but->editvec)? but->editvec: (float *)but->poin;
1296 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1298 switch(but->str[0]) {
1299 case 'H': h= value; break;
1300 case 'S': s= value; break;
1301 case 'V': v= value; break;
1304 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1308 /* first do rounding */
1309 if(but->pointype==CHA)
1310 value= (char)floor(value+0.5);
1311 else if(but->pointype==SHO ) {
1312 /* gcc 3.2.1 seems to have problems
1313 * casting a double like 32772.0 to
1314 * a short so we cast to an int, then
1317 gcckludge = (int) floor(value+0.5);
1318 value= (short)gcckludge;
1320 else if(but->pointype==INT )
1321 value= (int)floor(value+0.5);
1322 else if(but->pointype==FLO ) {
1323 float fval= (float)value;
1324 if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f; /* prevent negative zero */
1328 /* then set value with possible edit override */
1330 *but->editval= value;
1331 else if(but->pointype==CHA)
1332 *((char *)but->poin)= (char)value;
1333 else if(but->pointype==SHO)
1334 *((short *)but->poin)= (short)value;
1335 else if(but->pointype==INT)
1336 *((int *)but->poin)= (int)value;
1337 else if(but->pointype==FLO)
1338 *((float *)but->poin)= (float)value;
1341 /* update select flag */
1345 int ui_get_but_string_max_length(uiBut *but)
1347 if(but->type == TEX)
1348 return but->hardmax;
1349 else if(but->type == IDPOIN)
1350 return sizeof(((ID*)NULL)->name)-2;
1352 return UI_MAX_DRAW_STR;
1355 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1357 if(but->rnaprop && ELEM(but->type, TEX, IDPOIN)) {
1361 type= RNA_property_type(but->rnaprop);
1363 if(type == PROP_STRING) {
1365 buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1367 else if(type == PROP_POINTER) {
1369 PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1370 PropertyRNA *nameprop;
1372 if(ptr.data && (nameprop = RNA_struct_name_property(ptr.type)))
1373 buf= RNA_property_string_get_alloc(&ptr, nameprop, str, maxlen);
1375 BLI_strncpy(str, "", maxlen);
1378 BLI_strncpy(str, "", maxlen);
1380 if(buf && buf != str) {
1381 /* string was too long, we have to truncate */
1382 BLI_strncpy(str, buf, maxlen);
1386 else if(but->type == IDPOIN) {
1388 ID *id= *(but->idpoin_idpp);
1390 if(id) BLI_strncpy(str, id->name+2, maxlen);
1391 else BLI_strncpy(str, "", maxlen);
1395 else if(but->type == TEX) {
1397 BLI_strncpy(str, but->poin, maxlen);
1404 value= ui_get_but_val(but);
1406 if(ui_is_but_float(but)) {
1407 if(but->a2) { /* amount of digits defined */
1408 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1409 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1410 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1411 else BLI_snprintf(str, maxlen, "%.4f", value);
1414 BLI_snprintf(str, maxlen, "%.3f", value);
1417 BLI_snprintf(str, maxlen, "%d", (int)value);
1421 static void ui_rna_ID_collection(bContext *C, uiBut *but, PointerRNA *ptr, PropertyRNA **prop)
1423 CollectionPropertyIterator iter;
1424 PropertyRNA *iterprop, *iprop;
1427 /* look for collection property in Main */
1428 RNA_pointer_create(NULL, &RNA_Main, CTX_data_main(C), ptr);
1430 iterprop= RNA_struct_iterator_property(ptr->type);
1431 RNA_property_collection_begin(ptr, iterprop, &iter);
1434 for(; iter.valid; RNA_property_collection_next(&iter)) {
1435 iprop= iter.ptr.data;
1437 /* if it's a collection and has same pointer type, we've got it */
1438 if(RNA_property_type(iprop) == PROP_COLLECTION) {
1439 srna= RNA_property_pointer_type(iprop);
1441 if(RNA_property_pointer_type(but->rnaprop) == srna) {
1448 RNA_property_collection_end(&iter);
1451 /* autocomplete callback for RNA pointers */
1452 static void ui_rna_ID_autocomplete(bContext *C, char *str, void *arg_but)
1454 uiBut *but= arg_but;
1455 AutoComplete *autocpl;
1456 CollectionPropertyIterator iter;
1458 PropertyRNA *prop, *nameprop;
1461 if(str[0]==0) return;
1463 /* get the collection */
1464 ui_rna_ID_collection(C, but, &ptr, &prop);
1465 if(prop==NULL) return;
1467 autocpl= autocomplete_begin(str, ui_get_but_string_max_length(but));
1468 RNA_property_collection_begin(&ptr, prop, &iter);
1470 /* loop over items in collection */
1471 for(; iter.valid; RNA_property_collection_next(&iter)) {
1472 if(iter.ptr.data && (nameprop = RNA_struct_name_property(iter.ptr.type))) {
1473 name= RNA_property_string_get_alloc(&iter.ptr, nameprop, NULL, 0);
1476 /* test item name */
1477 autocomplete_do_name(autocpl, name);
1483 RNA_property_collection_end(&iter);
1484 autocomplete_end(autocpl, str);
1487 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1489 if(but->rnaprop && ELEM(but->type, TEX, IDPOIN)) {
1490 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1493 type= RNA_property_type(but->rnaprop);
1495 if(type == PROP_STRING) {
1497 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1500 else if(type == PROP_POINTER) {
1502 PointerRNA ptr, rptr;
1505 /* XXX only ID pointers at the moment, needs to support
1506 * custom collection too for bones, vertex groups, .. */
1507 ui_rna_ID_collection(C, but, &ptr, &prop);
1509 if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr)) {
1510 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1518 else if(but->type == IDPOIN) {
1520 but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1523 else if(but->type == TEX) {
1525 BLI_strncpy(but->poin, str, but->hardmax);
1531 /* XXX 2.50 missing python api */
1533 if(BPY_button_eval(str, &value)) {
1534 BKE_report(CTX_reports(C), RPT_WARNING, "Invalid Python expression, check console");
1535 value = 0.0f; /* Zero out value on error */
1544 if(!ui_is_but_float(but)) value= (int)value;
1545 if(but->type==NUMABS) value= fabs(value);
1547 /* not that we use hard limits here */
1548 if(value<but->hardmin) value= but->hardmin;
1549 if(value>but->hardmax) value= but->hardmax;
1551 ui_set_but_val(but, value);
1558 static double soft_range_round_up(double value, double max)
1560 /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1561 double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1563 if(newmax*0.2 >= max && newmax*0.2 >= value)
1565 else if(newmax*0.5 >= max && newmax*0.5 >= value)
1571 static double soft_range_round_down(double value, double max)
1573 /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1574 double newmax= pow(10.0, floor(log(value)/log(10.0)));
1576 if(newmax*5.0 <= max && newmax*5.0 <= value)
1578 else if(newmax*2.0 <= max && newmax*2.0 <= value)
1584 void ui_set_but_soft_range(uiBut *but, double value)
1587 double softmin, softmax, step, precision;
1590 type= RNA_property_type(but->rnaprop);
1592 if(type == PROP_INT) {
1593 int imin, imax, istep;
1595 RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1601 else if(type == PROP_FLOAT) {
1602 float fmin, fmax, fstep, fprecision;
1604 RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1608 precision= fprecision;
1613 /* clamp button range to something reasonable in case
1614 * we get -inf/inf from RNA properties */
1615 softmin= MAX2(softmin, -1e4);
1616 softmax= MIN2(softmax, 1e4);
1618 /* if the value goes out of the soft/max range, adapt the range */
1619 if(value+1e-10 < softmin) {
1621 softmin= -soft_range_round_up(-value, -softmin);
1623 softmin= soft_range_round_down(value, softmin);
1625 if(softmin < but->hardmin)
1626 softmin= but->hardmin;
1628 else if(value-1e-10 > softmax) {
1630 softmax= -soft_range_round_down(-value, -softmax);
1632 softmax= soft_range_round_up(value, softmax);
1634 if(softmax > but->hardmax)
1635 softmax= but->hardmax;
1638 but->softmin= softmin;
1639 but->softmax= softmax;
1643 /* ******************* Free ********************/
1645 static void ui_free_link(uiLink *link)
1648 BLI_freelistN(&link->lines);
1653 /* can be called with C==NULL */
1654 static void ui_free_but(const bContext *C, uiBut *but)
1657 WM_operator_properties_free(but->opptr);
1658 MEM_freeN(but->opptr);
1660 if(but->func_argN) MEM_freeN(but->func_argN);
1662 /* XXX solve later, buttons should be free-able without context? */
1664 ui_button_active_cancel(C, but);
1667 MEM_freeN(but->active);
1669 if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1670 ui_free_link(but->link);
1675 /* can be called with C==NULL */
1676 void uiFreeBlock(const bContext *C, uiBlock *block)
1680 while( (but= block->buttons.first) ) {
1681 BLI_remlink(&block->buttons, but);
1682 ui_free_but(C, but);
1685 BLI_freelistN(&block->saferct);
1690 /* can be called with C==NULL */
1691 void uiFreeBlocks(const bContext *C, ListBase *lb)
1695 while( (block= lb->first) ) {
1696 BLI_remlink(lb, block);
1697 uiFreeBlock(C, block);
1701 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1703 uiBlock *block, *nextblock;
1705 for(block=lb->first; block; block=nextblock) {
1706 nextblock= block->next;
1708 if(!block->handle) {
1709 if(!block->active) {
1710 BLI_remlink(lb, block);
1711 uiFreeBlock(C, block);
1719 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1722 uiBlock *block, *oldblock= NULL;
1724 int getsizex, getsizey;
1726 window= CTX_wm_window(C);
1727 lb= ®ion->uiblocks;
1729 /* each listbase only has one block with this name, free block
1730 * if is already there so it can be rebuilt from scratch */
1732 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1733 if (BLI_streq(oldblock->name, name))
1737 oldblock->active= 0;
1738 oldblock->panel= NULL;
1742 block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1743 block->oldblock= oldblock;
1747 /* at the beginning of the list! for dynamical menus/blocks */
1749 BLI_addhead(lb, block);
1751 BLI_strncpy(block->name, name, sizeof(block->name));
1753 /* window matrix and aspect */
1754 if(region->swinid) {
1755 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1756 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1758 /* TODO - investigate why block->winmat[0][0] is negative
1759 * in the image view when viewRedrawForce is called */
1760 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1763 /* no subwindow created yet, for menus for example, so we
1764 * use the main window instead, since buttons are created
1766 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1767 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1769 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1770 block->auto_open= 2;
1771 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1777 uiBlock *uiGetBlock(char *name, ARegion *ar)
1779 uiBlock *block= ar->uiblocks.first;
1782 if( strcmp(name, block->name)==0 ) return block;
1789 void uiBlockSetEmboss(uiBlock *block, short dt)
1794 void ui_check_but(uiBut *but)
1796 /* if something changed in the button */
1799 int transopts= ui_translate_buttons();
1803 if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1805 /* test for min and max, icon sliders, etc */
1806 switch( but->type ) {
1812 value= ui_get_but_val(but);
1813 if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1814 else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1818 value= fabs( ui_get_but_val(but) );
1819 if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1820 else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1825 if(but->flag & UI_SELECT) but->iconadd= 1;
1826 else but->iconadd= 0;
1830 value= ui_get_but_val(but);
1831 but->iconadd= (int)value- (int)(but->hardmin);
1835 value= ui_get_but_val(but);
1836 but->iconadd= (int)value- (int)(but->hardmin);
1841 /* safety is 4 to enable small number buttons (like 'users') */
1842 okwidth= -4 + (but->x2 - but->x1);
1845 switch( but->type ) {
1850 if(but->x2 - but->x1 > 24) {
1851 value= ui_get_but_val(but);
1852 ui_set_name_menu(but, (int)value);
1861 value= ui_get_but_val(but);
1863 if(ui_is_but_float(but)) {
1864 if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
1865 else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
1866 else if(but->a2) { /* amount of digits defined */
1867 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1868 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1869 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1870 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1873 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1874 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1878 sprintf(but->drawstr, "%s%d", but->str, (int)value);
1882 PropertySubType pstype = RNA_property_subtype(but->rnaprop);
1884 if (pstype == PROP_PERCENTAGE)
1885 strcat(but->drawstr, "%");
1890 if(ui_is_but_float(but)) {
1891 value= ui_get_but_val(but);
1892 if(but->a2) { /* amount of digits defined */
1893 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1894 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1895 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1896 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1899 sprintf(but->drawstr, "%s%.2f", but->str, value);
1902 else strcpy(but->drawstr, but->str);
1909 char str[UI_MAX_DRAW_STR];
1911 ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1913 strcpy(but->drawstr, but->str);
1914 strcat(but->drawstr, str);
1919 strcpy(but->drawstr, but->str);
1920 if (but->flag & UI_SELECT) {
1921 strcat(but->drawstr, "Press a key");
1923 strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1928 /* trying to get the dual-icon to left of text... not very nice */
1930 strcpy(but->drawstr, " ");
1931 strcpy(but->drawstr+2, but->str);
1935 strcpy(but->drawstr, but->str);
1939 /* if we are doing text editing, this will override the drawstr */
1941 strcpy(but->drawstr, but->str);
1942 strcat(but->drawstr, but->editstr);
1945 /* text clipping moved to widget drawing code itself */
1949 void uiBlockBeginAlign(uiBlock *block)
1951 /* if other align was active, end it */
1952 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1954 block->flag |= UI_BUT_ALIGN_DOWN;
1955 /* buttons declared after this call will this align flag */
1958 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1962 dx= fabs( but1->x2 - but2->x1);
1963 dy= fabs( but1->y1 - but2->y2);
1965 if(dx > dy) return 0;
1969 void uiBlockEndAlign(uiBlock *block)
1971 uiBut *prev, *but=NULL, *next;
1972 int flag= 0, cols=0, rows=0;
1975 - go back to first button of align start (ALIGN_DOWN)
1976 - compare triples, and define flags
1978 prev= block->buttons.last;
1980 if( (prev->flag & UI_BUT_ALIGN_DOWN)) but= prev;
1983 if(but && but->next) {
1984 if(buts_are_horiz(but, but->next)) cols++;
1990 if(but==NULL) return;
1992 /* rows==0: 1 row, cols==0: 1 collumn */
1994 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1999 /* clear old flag */
2000 but->flag &= ~UI_BUT_ALIGN_DOWN;
2002 if(flag==0) { /* first case */
2004 if(buts_are_horiz(but, next)) {
2006 flag= UI_BUT_ALIGN_RIGHT;
2008 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
2011 flag= UI_BUT_ALIGN_DOWN;
2015 else if(next==NULL) { /* last case */
2017 if(buts_are_horiz(prev, but)) {
2019 flag= UI_BUT_ALIGN_LEFT;
2021 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
2023 else flag= UI_BUT_ALIGN_TOP;
2026 else if(buts_are_horiz(but, next)) {
2027 /* check if this is already second row */
2028 if( prev && buts_are_horiz(prev, but)==0) {
2029 flag |= UI_BUT_ALIGN_TOP;
2030 /* exception case: bottom row */
2034 if(bt->next && buts_are_horiz(bt, bt->next)==0 ) break;
2037 if(bt==0) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
2040 else flag |= UI_BUT_ALIGN_LEFT;
2044 flag |= UI_BUT_ALIGN_TOP;
2046 else { /* next button switches to new row */
2047 if( (flag & UI_BUT_ALIGN_TOP)==0) { /* stil top row */
2049 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2051 flag |= UI_BUT_ALIGN_DOWN;
2054 flag |= UI_BUT_ALIGN_TOP;
2060 /* merge coordinates */
2064 but->x1= (prev->x2+but->x1)/2.0;
2068 but->y2= (prev->y1+but->y2)/2.0;
2072 if(buts_are_horiz(prev, but)) {
2073 but->x1= (prev->x2+but->x1)/2.0;
2075 /* copy height too */
2078 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2079 /* the previous button is a single one in its row */
2080 but->y2= (prev->y1+but->y2)/2.0;
2084 /* the previous button is not a single one in its row */
2094 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
2098 static void uiBlockEndAligno(uiBlock *block)
2102 /* correct last defined button */
2103 but= block->buttons.last;
2105 /* vertical align case */
2106 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_DOWN) ) {
2107 but->flag &= ~UI_BUT_ALIGN_DOWN;
2109 /* horizontal align case */
2110 if( (block->flag & UI_BUT_ALIGN) == (UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT) ) {
2111 but->flag &= ~UI_BUT_ALIGN_RIGHT;
2113 /* else do nothing, manually provided flags */
2115 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
2120 ui_def_but is the function that draws many button types
2123 "a1" Click Step (how much to change the value each click)
2124 "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2125 all greater values will be clamped to 4.
2128 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)
2133 if(type & BUTPOIN) { /* a pointer is required */
2138 but= MEM_callocN(sizeof(uiBut), "uiBut");
2140 but->type= type & BUTTYPE;
2141 but->pointype= type & BUTPOIN;
2142 but->bit= type & BIT;
2143 but->bitnr= type & 31;
2146 but->retval= retval;
2147 if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2148 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2149 strcpy(but->str, str);
2152 but->str= but->strdata;
2153 strcpy(but->str, str);
2161 but->hardmin= but->softmin= min;
2162 but->hardmax= but->softmax= max;
2167 but->lock= block->lock;
2168 but->lockstr= block->lockstr;
2171 but->aspect= 1.0f; //XXX block->aspect;
2172 but->block= block; // pointer back, used for frontbuffer status, and picker
2174 but->func= block->func;
2175 but->func_arg1= block->func_arg1;
2176 but->func_arg2= block->func_arg2;
2178 but->pos= -1; /* cursor invisible */
2180 if(ELEM(but->type, NUM, NUMABS)) { /* add a space to name */
2181 slen= strlen(but->str);
2182 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2183 if(but->str[slen-1]!=' ') {
2184 but->str[slen]= ' ';
2185 but->str[slen+1]= 0;
2190 if(but->type==HSVCUBE) { /* hsv buttons temp storage */
2192 ui_get_but_vectorf(but, rgb);
2193 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2196 if((block->flag & UI_BLOCK_LOOP) || ELEM6(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM)) {
2197 but->flag |= UI_TEXT_LEFT;
2200 if(but->type==BUT_TOGDUAL) {
2201 but->flag |= UI_ICON_LEFT;
2204 but->flag |= (block->flag & UI_BUT_ALIGN);
2205 if(block->flag & UI_BLOCK_NO_HILITE)
2206 but->flag |= UI_NO_HILITE;
2210 but->flag |= UI_BUT_DISABLED;
2214 if(but->type == ROUNDBOX) {
2215 but->flag |= UI_NO_HILITE;
2216 BLI_addhead(&block->buttons, but);
2219 BLI_addtail(&block->buttons, but);
2224 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)
2228 PropertyType proptype;
2231 prop= RNA_struct_find_property(ptr, propname);
2234 proptype= RNA_property_type(prop);
2236 /* use rna values if parameters are not specified */
2238 if(type == MENU && proptype == PROP_ENUM) {
2239 const EnumPropertyItem *item;
2243 RNA_property_enum_items(ptr, prop, &item, &totitem);
2245 dynstr= BLI_dynstr_new();
2246 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2247 for(i=0; i<totitem; i++)
2248 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2249 str= BLI_dynstr_get_cstring(dynstr);
2250 BLI_dynstr_free(dynstr);
2254 else if(type == ROW && proptype == PROP_ENUM) {
2255 const EnumPropertyItem *item;
2258 RNA_property_enum_items(ptr, prop, &item, &totitem);
2259 for(i=0; i<totitem; i++)
2260 if(item[i].value == (int)max)
2261 str= (char*)item[i].name;
2264 str= (char*)RNA_property_ui_name(prop);
2267 str= (char*)RNA_property_ui_name(prop);
2271 if(type == ROW && proptype == PROP_ENUM) {
2272 const EnumPropertyItem *item;
2275 RNA_property_enum_items(ptr, prop, &item, &totitem);
2277 for(i=0; i<totitem; i++) {
2278 if(item[i].value == (int)max) {
2279 if(item[i].description[0])
2280 tip= (char*)item[i].description;
2288 tip= (char*)RNA_property_ui_description(prop);
2290 if(min == max || a1 == -1 || a2 == -1) {
2291 if(proptype == PROP_INT) {
2292 int hardmin, hardmax, softmin, softmax, step;
2294 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2295 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2306 else if(proptype == PROP_FLOAT) {
2307 float hardmin, hardmax, softmin, softmax, step, precision;
2309 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2310 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2321 else if(proptype == PROP_STRING) {
2323 max= RNA_property_string_maxlength(prop);
2324 if(max == 0) /* interface code should ideally support unlimited length */
2325 max= UI_MAX_DRAW_STR;
2330 str= (char*)propname;
2332 /* now create button */
2333 but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2339 if(RNA_property_array_length(but->rnaprop))
2340 but->rnaindex= index;
2345 uiButSetCompleteFunc(but, ui_rna_ID_autocomplete, but);
2348 if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2349 but->flag |= UI_BUT_DISABLED;
2360 uiBut *ui_def_but_operator(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2365 ot= WM_operatortype_find(opname);
2368 if(ot) str= ot->name;
2372 if ((!tip || tip[0]=='\0') && ot && ot->description) {
2373 tip= ot->description;
2376 but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2377 but->opname= opname;
2378 but->opcontext= opcontext;
2381 but->flag |= UI_BUT_DISABLED;
2389 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)
2391 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2398 /* if _x_ is a power of two (only one bit) return the power,
2399 * otherwise return -1.
2400 * (1<<findBitIndex(x))==x for powers of two.
2402 static int findBitIndex(unsigned int x) {
2403 if (!x || (x&(x-1))!=0) { /* x&(x-1) strips lowest bit */
2408 if (x&0xFFFF0000) idx+=16, x>>=16;
2409 if (x&0xFF00) idx+=8, x>>=8;
2410 if (x&0xF0) idx+=4, x>>=4;
2411 if (x&0xC) idx+=2, x>>=2;
2418 /* autocomplete helper functions */
2419 struct AutoComplete {
2425 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2427 AutoComplete *autocpl;
2429 autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2430 autocpl->maxlen= maxlen;
2431 autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2432 autocpl->startname= startname;
2437 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2439 char *truncate= autocpl->truncate;
2440 char *startname= autocpl->startname;
2443 for(a=0; a<autocpl->maxlen-1; a++) {
2444 if(startname[a]==0 || startname[a]!=name[a])
2448 if(startname[a]==0) {
2451 BLI_strncpy(truncate, name, autocpl->maxlen);
2453 /* remove from truncate what is not in bone->name */
2454 for(a=0; a<autocpl->maxlen-1; a++) {
2455 if(truncate[a]!=name[a])
2462 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2464 if(autocpl->truncate[0])
2465 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2467 if (autoname != autocpl->startname) /* dont copy a string over its self */
2468 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2470 MEM_freeN(autocpl->truncate);
2474 /* autocomplete callback for ID buttons */
2475 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2477 int blocktype= (intptr_t)arg_v;
2478 ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2480 if(listb==NULL) return;
2482 /* search if str matches the beginning of an ID struct */
2484 AutoComplete *autocpl= autocomplete_begin(str, 22);
2487 for(id= listb->first; id; id= id->next)
2488 autocomplete_do_name(autocpl, id->name+2);
2490 autocomplete_end(autocpl, str);
2494 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)
2496 int bitIdx= findBitIndex(bit);
2500 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2503 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)
2505 return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2507 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)
2509 return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2511 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)
2513 return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2515 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)
2517 return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2519 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)
2521 return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2523 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)
2525 return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2527 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)
2529 return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2531 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)
2533 return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2535 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)
2539 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2545 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2549 but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2556 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)
2558 uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2560 but->icon= (BIFIconID) icon;
2561 but->flag|= UI_HAS_ICON;
2567 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)
2569 int bitIdx= findBitIndex(bit);
2573 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2577 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)
2579 return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2581 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)
2583 return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2585 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)
2587 return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2589 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)
2591 return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2593 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)
2595 return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2597 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)
2599 return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2601 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)
2603 return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2605 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)
2607 return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2609 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)
2613 but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2615 but->icon= (BIFIconID) icon;
2616 but->flag|= UI_HAS_ICON;
2622 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2626 but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2628 but->icon= (BIFIconID) icon;
2629 but->flag|= UI_HAS_ICON;
2636 /* Button containing both string label and icon */
2637 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)
2639 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2641 but->icon= (BIFIconID) icon;
2642 but->flag|= UI_HAS_ICON;
2644 but->flag|= UI_ICON_LEFT;
2650 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)
2652 int bitIdx= findBitIndex(bit);
2656 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2660 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)
2662 return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2664 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)
2666 return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2668 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)
2670 return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2672 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)
2674 return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2676 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)
2678 return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2680 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)
2682 return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2684 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)
2686 return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2688 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)
2690 return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2692 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)
2696 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2698 but->icon= (BIFIconID) icon;
2699 but->flag|= UI_HAS_ICON;
2700 but->flag|= UI_ICON_LEFT;
2706 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2710 but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2712 but->icon= (BIFIconID) icon;
2713 but->flag|= UI_HAS_ICON;
2714 but->flag|= UI_ICON_LEFT;
2721 static int ui_menu_y(uiBlock *block)
2723 uiBut *but= block->buttons.last;
2725 if(but) return but->y1;
2729 uiBut *uiDefMenuButO(uiBlock *block, char *opname, char *name)
2731 int y= ui_menu_y(block) - MENU_ITEM_HEIGHT;
2732 return uiDefIconTextButO(block, BUT, opname, WM_OP_INVOKE_REGION_WIN, ICON_BLANK1, name, 0, y, MENU_WIDTH, MENU_ITEM_HEIGHT-1, NULL);
2735 uiBut *uiDefMenuSep(uiBlock *block)
2737 int y= ui_menu_y(block) - MENU_SEP_HEIGHT;
2738 return uiDefBut(block, SEPR, 0, "", 0, y, MENU_WIDTH, MENU_SEP_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2741 uiBut *uiDefMenuSub(uiBlock *block, uiBlockCreateFunc func, char *name)
2743 int y= ui_menu_y(block) - MENU_ITEM_HEIGHT;
2744 return uiDefIconTextBlockBut(block, func, NULL, ICON_RIGHTARROW_THIN, name, 0, y, MENU_WIDTH, MENU_ITEM_HEIGHT-1, "");
2747 uiBut *uiDefMenuTogR(uiBlock *block, PointerRNA *ptr, char *propname, char *propvalue, char *name)
2752 const EnumPropertyItem *item;
2753 int a, value, totitem, icon= ICON_CHECKBOX_DEHLT;
2754 int y= ui_menu_y(block) - MENU_ITEM_HEIGHT;
2756 prop= RNA_struct_find_property(ptr, propname);
2758 type= RNA_property_type(prop);
2760 if(type == PROP_BOOLEAN) {
2761 if(RNA_property_boolean_get(ptr, prop))
2762 icon= ICON_CHECKBOX_HLT;
2764 return uiDefIconTextButR(block, TOG, 0, icon, name, 0, y, MENU_WIDTH, MENU_ITEM_HEIGHT-1, ptr, propname, 0, 0, 0, 0, 0, NULL);
2766 else if(type == PROP_ENUM) {
2767 RNA_property_enum_items(ptr, prop, &item, &totitem);
2770 for(a=0; a<totitem; a++) {
2771 if(propvalue && strcmp(propvalue, item[a].identifier) == 0) {
2772 value= item[a].value;
2774 name= (char*)item[a].name;
2776 if(RNA_property_enum_get(ptr, prop) == value)
2777 icon= ICON_CHECKBOX_HLT;
2783 return uiDefIconTextButR(block, ROW, 0, icon, name, 0, y, MENU_WIDTH, MENU_ITEM_HEIGHT-1, ptr, propname, 0, 0, value, 0, 0, NULL);
2788 uiBlockSetButLock(block, 1, "");
2789 but= uiDefIconTextBut(block, BUT, 0, ICON_BLANK1, propname, 0, y, MENU_WIDTH, MENU_ITEM_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2790 uiBlockClearButLock(block);
2795 /* END Button containing both string label and icon */
2797 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2801 link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2806 link->fromcode= from;
2810 /* cruft to make uiBlock and uiBut private */
2812 int uiBlocksGetYMin(ListBase *lb)
2817 for (block= lb->first; block; block= block->next)
2818 if (block==lb->first || block->miny<min)
2824 void uiBlockSetDirection(uiBlock *block, int direction)
2826 block->direction= direction;
2829 /* this call escapes if there's alignment flags */
2830 void uiBlockFlipOrder(uiBlock *block)
2834 float centy, miny=10000, maxy= -10000;
2836 // if(U.uiflag & USER_PLAINMENUS)
2839 for(but= block->buttons.first; but; but= but->next) {
2840 if(but->flag & UI_BUT_ALIGN) return;
2841 if(but->y1 < miny) miny= but->y1;
2842 if(but->y2 > maxy) maxy= but->y2;
2845 centy= (miny+maxy)/2.0;
2846 for(but= block->buttons.first; but; but= but->next) {
2847 but->y1 = centy-(but->y1-centy);
2848 but->y2 = centy-(but->y2-centy);
2849 SWAP(float, but->y1, but->y2);
2852 /* also flip order in block itself, for example for arrowkey */
2853 lb.first= lb.last= NULL;
2854 but= block->buttons.first;
2857 BLI_remlink(&block->buttons, but);
2858 BLI_addtail(&lb, but);
2865 void uiBlockSetFlag(uiBlock *block, int flag)
2870 void uiBlockClearFlag(uiBlock *block, int flag)
2872 block->flag&= ~flag;
2875 void uiBlockSetXOfs(uiBlock *block, int xofs)
2880 void uiButSetFlag(uiBut *but, int flag)
2885 void uiButClearFlag(uiBut *but, int flag)
2890 int uiButGetRetVal(uiBut *but)
2895 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2897 if(but->opname && !but->opptr) {
2898 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2899 WM_operator_properties_create(but->opptr, but->opname);
2905 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
2907 block->handle_func= func;
2908 block->handle_func_arg= arg;
2911 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
2913 block->butm_func= func;
2914 block->butm_func_arg= arg;
2917 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
2920 block->func_arg1= arg1;
2921 block->func_arg2= arg2;
2924 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)())
2926 block->drawextra= func;
2929 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
2932 but->func_arg1= arg1;
2933 but->func_arg2= arg2;
2936 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
2939 but->func_argN= argN;
2940 but->func_arg2= arg2;
2943 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
2945 but->autocomplete_func= func;
2946 but->autofunc_arg= arg;
2949 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2951 uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2952 but->idpoin_func= func;
2953 but->idpoin_idpp= (ID**) idpp;
2957 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2962 uiBut *uiDefBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2964 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2965 but->block_create_func= func;
2970 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2972 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2973 but->block_create_func= func;
2978 uiBut *uiDefMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2980 uiBut *but= ui_def_but(block, HMENU, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2981 but->menu_create_func= func;
2986 uiBut *uiDefIconTextMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2988 uiBut *but= ui_def_but(block, HMENU, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2990 but->icon= (BIFIconID) icon;
2991 but->flag|= UI_HAS_ICON;
2993 but->flag|= UI_ICON_LEFT;
2994 but->flag|= UI_ICON_RIGHT;
2996 but->menu_create_func= func;
3002 /* Block button containing both string label and icon */
3003 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
3005 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3007 but->icon= (BIFIconID) icon;
3008 but->flag|= UI_HAS_ICON;
3010 but->flag|= UI_ICON_LEFT;
3011 but->flag|= UI_ICON_RIGHT;
3013 but->block_create_func= func;
3019 /* Block button containing icon */
3020 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
3022 uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
3024 but->icon= (BIFIconID) icon;
3025 but->flag|= UI_HAS_ICON;
3027 but->flag|= UI_ICON_LEFT;
3028 but->flag|= UI_ICON_RIGHT;
3030 but->block_create_func= func;
3036 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
3038 uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
3042 /* Program Init/Exit */
3046 ui_resources_init();
3049 /* after reading userdef file */
3050 void UI_init_userdef(void)
3052 /* fix saved themes */
3053 init_userdef_do_versions();
3054 /* set default colors in default theme */
3055 ui_theme_init_userdef();
3063 ui_resources_free();