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);
84 /* ************* translation ************** */
86 int ui_translate_buttons()
88 return (U.transopts & USER_TR_BUTTONS);
91 int ui_translate_menus()
93 return (U.transopts & USER_TR_MENUS);
96 int ui_translate_tooltips()
98 return (U.transopts & USER_TR_TOOLTIPS);
101 /* ************* window matrix ************** */
103 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
106 int sx, sy, getsizex, getsizey;
108 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
109 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
117 gx += block->panel->ofsx;
118 gy += block->panel->ofsy;
121 *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
122 *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
125 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
132 ui_block_to_window_fl(ar, block, &fx, &fy);
138 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
143 ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
144 ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
146 winr->xmin= tmpr.xmin;
147 winr->ymin= tmpr.ymin;
148 winr->xmax= tmpr.xmax;
149 winr->ymax= tmpr.ymax;
152 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y) /* for mouse cursor */
154 float a, b, c, d, e, f, px, py;
155 int sx, sy, getsizex, getsizey;
157 getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
158 getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
162 a= .5*((float)getsizex)*block->winmat[0][0];
163 b= .5*((float)getsizex)*block->winmat[1][0];
164 c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
166 d= .5*((float)getsizey)*block->winmat[0][1];
167 e= .5*((float)getsizey)*block->winmat[1][1];
168 f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
173 *y= (a*(py-f) + d*(c-px))/(a*e-d*b);
174 *x= (px- b*(*y)- c)/a;
177 *x -= block->panel->ofsx;
178 *y -= block->panel->ofsy;
182 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
189 ui_window_to_block_fl(ar, block, &fx, &fy);
195 void ui_window_to_region(const ARegion *ar, int *x, int *y)
197 *x-= ar->winrct.xmin;
198 *y-= ar->winrct.ymin;
201 /* ******************* block calc ************************* */
203 void ui_block_translate(uiBlock *block, int x, int y)
207 for(bt= block->buttons.first; bt; bt=bt->next) {
220 static void ui_text_bounds_block(uiBlock *block, float offset)
222 uiStyle *style= U.uistyles.first; // XXX pass on as arg
224 int i = 0, j, x1addval= offset, nextcol;
226 uiStyleFontSet(&style->widget);
228 for(bt= block->buttons.first; bt; bt= bt->next) {
230 //int transopts= ui_translate_buttons();
231 //if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
233 j= BLF_width(bt->drawstr);
239 /* cope with multi collumns */
240 bt= block->buttons.first;
242 if(bt->next && bt->x1 < bt->next->x1)
247 bt->x2 = bt->x1 + i + block->bounds;
249 ui_check_but(bt); // clips text again
252 x1addval+= i + block->bounds;
258 void ui_bounds_block(uiBlock *block)
263 if(block->buttons.first==NULL) {
265 block->minx= 0.0; block->maxx= block->panel->sizex;
266 block->miny= 0.0; block->maxy= block->panel->sizey;
271 block->minx= block->miny= 10000;
272 block->maxx= block->maxy= -10000;
274 bt= block->buttons.first;
276 if(bt->x1 < block->minx) block->minx= bt->x1;
277 if(bt->y1 < block->miny) block->miny= bt->y1;
279 if(bt->x2 > block->maxx) block->maxx= bt->x2;
280 if(bt->y2 > block->maxy) block->maxy= bt->y2;
285 block->minx -= block->bounds;
286 block->miny -= block->bounds;
287 block->maxx += block->bounds;
288 block->maxy += block->bounds;
291 /* hardcoded exception... but that one is annoying with larger safety */
292 bt= block->buttons.first;
293 if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
296 block->safety.xmin= block->minx-xof;
297 block->safety.ymin= block->miny-xof;
298 block->safety.xmax= block->maxx+xof;
299 block->safety.ymax= block->maxy+xof;
302 static void ui_popup_bounds_block(const bContext *C, uiBlock *block, int menu)
304 wmWindow *window= CTX_wm_window(C);
305 int startx, starty, endx, endy, width, height;
306 int oldbounds, mx, my, xmax, ymax;
308 oldbounds= block->bounds;
310 /* compute mouse position with user defined offset */
311 ui_bounds_block(block);
312 mx= window->eventstate->x + block->minx + block->mx;
313 my= window->eventstate->y + block->miny + block->my;
315 wm_window_get_size(window, &xmax, &ymax);
317 /* first we ensure wide enough text bounds */
319 if(block->flag & UI_BLOCK_LOOP) {
321 ui_text_bounds_block(block, block->minx);
325 /* next we recompute bounds */
326 block->bounds= oldbounds;
327 ui_bounds_block(block);
329 /* and we adjust the position to fit within window */
330 width= block->maxx - block->minx;
331 height= block->maxy - block->miny;
333 startx= mx-(0.8*(width));
353 ui_block_translate(block, startx - block->minx, starty - block->miny);
355 /* now recompute bounds and safety */
356 ui_bounds_block(block);
359 /* used for various cases */
360 void uiBoundsBlock(uiBlock *block, int addval)
365 block->bounds= addval;
369 /* used for pulldowns */
370 void uiTextBoundsBlock(uiBlock *block, int addval)
372 block->bounds= addval;
376 /* used for block popups */
377 void uiPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
379 block->bounds= addval;
385 /* used for menu popups */
386 void uiMenuPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
388 block->bounds= addval;
394 /* ************** LINK LINE DRAWING ************* */
396 /* link line drawing is not part of buttons or theme.. so we stick with it here */
398 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
402 if(line->from==NULL || line->to==NULL) return;
404 rect.xmin= (line->from->x1+line->from->x2)/2.0;
405 rect.ymin= (line->from->y1+line->from->y2)/2.0;
406 rect.xmax= (line->to->x1+line->to->x2)/2.0;
407 rect.ymax= (line->to->y1+line->to->y2)/2.0;
409 if(line->flag & UI_SELECT)
410 glColor3ub(100,100,100);
414 ui_draw_link_bezier(&rect);
417 static void ui_draw_links(uiBlock *block)
422 but= block->buttons.first;
424 if(but->type==LINK && but->link) {
425 line= but->link->lines.first;
427 ui_draw_linkline(but, line);
435 /* ************** BLOCK ENDING FUNCTION ************* */
437 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
438 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
440 /* various properties are being compared here, hopfully sufficient
441 * to catch all cases, but it is simple to add more checks later */
442 if(but->retval != oldbut->retval) return 0;
443 if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
444 if(but->rnaprop != oldbut->rnaprop)
445 if(but->rnaindex != oldbut->rnaindex) return 0;
446 if(but->func != oldbut->func) return 0;
447 if(but->funcN != oldbut->funcN) return 0;
448 if(oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return 0;
449 if(oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return 0;
450 if(!but->funcN && ((but->poin != oldbut->poin && (uiBut*)oldbut->poin != oldbut) || but->pointype != oldbut->pointype)) return 0;
455 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
461 oldblock= block->oldblock;
465 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
466 if(ui_but_equals_old(oldbut, but)) {
468 but->flag= oldbut->flag;
469 but->active= oldbut->active;
470 but->pos= oldbut->pos;
471 but->editstr= oldbut->editstr;
472 but->editval= oldbut->editval;
473 but->editvec= oldbut->editvec;
474 but->editcoba= oldbut->editcoba;
475 but->editcumap= oldbut->editcumap;
476 but->selsta= oldbut->selsta;
477 but->selend= oldbut->selend;
478 but->softmin= oldbut->softmin;
479 but->softmax= oldbut->softmax;
480 but->linkto[0]= oldbut->linkto[0];
481 but->linkto[1]= oldbut->linkto[1];
484 oldbut->active= NULL;
487 /* ensures one button can get activated, and in case the buttons
488 * draw are the same this gives O(1) lookup for each button */
489 BLI_remlink(&oldblock->buttons, oldbut);
490 ui_free_but(C, oldbut);
499 /* needed for temporarily rename buttons, such as in outliner or fileselect,
500 they should keep calling uiDefButs to keep them alive */
501 /* returns 0 when button removed */
502 int uiButActiveOnly(const bContext *C, uiBlock *block, uiBut *but)
506 int activate= 0, found= 0, isactive= 0;
508 oldblock= block->oldblock;
512 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
513 if(ui_but_equals_old(oldbut, but)) {
523 if(activate || found==0) {
524 ui_button_activate_do( (bContext *)C, CTX_wm_region(C), but);
526 else if(found && isactive==0) {
528 BLI_remlink(&block->buttons, but);
536 void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
540 char buf[512], *butstr;
542 /* only do it before bounding */
543 if(block->minx != block->maxx)
546 for(but=block->buttons.first; but; but=but->next) {
548 prop= (but->opptr)? but->opptr->data: NULL;
550 if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
551 butstr= MEM_mallocN(strlen(but->str)+strlen(buf)+2, "menu_block_set_keymaps");
552 strcpy(butstr, but->str);
556 but->str= but->strdata;
557 BLI_strncpy(but->str, butstr, sizeof(but->strdata));
566 void uiEndBlock(const bContext *C, uiBlock *block)
569 Scene *scene= CTX_data_scene(C);
571 /* inherit flags from 'old' buttons that was drawn here previous, based
572 * on matching buttons, we need this to make button event handling non
573 * blocking, while still alowing buttons to be remade each redraw as it
574 * is expected by blender code */
575 for(but=block->buttons.first; but; but=but->next) {
576 if(ui_but_update_from_old_block(C, block, but))
579 /* temp? Proper check for greying out */
581 wmOperatorType *ot= but->optype;
584 CTX_store_set((bContext*)C, but->context);
586 if(ot==NULL || (ot->poll && ot->poll((bContext *)C)==0)) {
587 but->flag |= UI_BUT_DISABLED;
592 CTX_store_set((bContext*)C, NULL);
595 /* only update soft range while not editing */
596 if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
597 ui_set_but_soft_range(but, ui_get_but_val(but));
599 ui_but_anim_flag(but, (scene)? scene->r.cfra: 0.0f);
602 if(block->oldblock) {
603 block->auto_open= block->oldblock->auto_open;
604 block->auto_open_last= block->oldblock->auto_open_last;
605 block->tooltipdisabled= block->oldblock->tooltipdisabled;
607 block->oldblock= NULL;
610 /* handle pending stuff */
611 if(block->layouts.first) uiBlockLayoutResolve(C, block, NULL, NULL);
612 ui_block_do_align(block);
613 if(block->flag & UI_BLOCK_LOOP) ui_menu_block_set_keymaps(C, block);
616 if(block->dobounds == 1) ui_bounds_block(block);
617 else if(block->dobounds == 2) ui_text_bounds_block(block, 0.0f);
618 else if(block->dobounds) ui_popup_bounds_block(C, block, (block->dobounds == 4));
620 if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
621 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
626 /* ************** BLOCK DRAWING FUNCTION ************* */
628 void ui_fontscale(short *points, float aspect)
630 if(aspect < 0.9f || aspect > 1.1f) {
631 float pointsf= *points;
633 /* for some reason scaling fonts goes too fast compared to widget size */
634 aspect= sqrt(aspect);
638 *points= ceil(pointsf);
640 *points= floor(pointsf);
644 /* project button or block (but==NULL) to pixels in regionspace */
645 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
648 float getsizex, getsizey;
653 gx= (but?but->x1:block->minx) + (block->panel?block->panel->ofsx:0.0f);
654 gy= (but?but->y1:block->miny) + (block->panel?block->panel->ofsy:0.0f);
656 rect->xmin= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
657 rect->ymin= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
659 gx= (but?but->x2:block->maxx) + (block->panel?block->panel->ofsx:0.0f);
660 gy= (but?but->y2:block->maxy) + (block->panel?block->panel->ofsy:0.0f);
662 rect->xmax= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
663 rect->ymax= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
667 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
668 void uiDrawBlock(const bContext *C, uiBlock *block)
670 uiStyle style= *((uiStyle *)U.uistyles.first); // XXX pass on as arg
675 /* get menu region or area region */
678 ar= CTX_wm_region(C);
681 uiEndBlock(C, block);
683 /* we set this only once */
684 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
687 ui_fontscale(&style.paneltitle.points, block->aspect);
688 ui_fontscale(&style.grouplabel.points, block->aspect);
689 ui_fontscale(&style.widgetlabel.points, block->aspect);
690 ui_fontscale(&style.widget.points, block->aspect);
692 /* scale block min/max to rect */
693 ui_but_to_pixelrect(&rect, ar, block, NULL);
695 /* pixel space for AA widgets */
699 wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
702 if(block->flag & UI_BLOCK_LOOP)
703 ui_draw_menu_back(&style, block, &rect);
704 else if(block->panel)
705 ui_draw_aligned_panel(ar, &style, block, &rect);
708 for(but= block->buttons.first; but; but= but->next) {
709 ui_but_to_pixelrect(&rect, ar, block, but);
710 if(!(but->flag & UI_HIDDEN))
711 ui_draw_but(C, ar, &style, but, &rect);
717 ui_draw_links(block);
720 /* ************* EVENTS ************* */
722 static void ui_is_but_sel(uiBut *but)
726 short push=0, true=1;
728 value= ui_get_but_val(but);
730 if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) true= 0;
734 if( BTST(lvalue, (but->bitnr)) ) push= true;
752 if(value!=but->hardmin) push= 1;
757 if(value==0.0) push= 1;
761 if(value == but->hardmax) push= 1;
773 else if(push==1) but->flag |= UI_SELECT;
774 else but->flag &= ~UI_SELECT;
777 /* XXX 2.50 no links supported yet */
779 static int uibut_contains_pt(uiBut *but, short *mval)
785 uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
789 /* find button to link to */
790 for (bt= block->buttons.first; bt; bt= bt->next)
791 if(bt!=but && uibut_contains_pt(bt, mval))
795 if (but->type==LINK && bt->type==INLINK) {
796 if( but->link->tocode == (int)bt->hardmin ) {
800 else if(but->type==INLINK && bt->type==LINK) {
801 if( bt->link->tocode == (int)but->hardmin ) {
811 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
815 but= block->buttons.first;
817 if(but->type==INLINK) {
818 if(but->poin == poin) return but;
825 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
829 line= MEM_callocN(sizeof(uiLinkLine), "linkline");
830 BLI_addtail(listb, line);
835 uiBut *uiFindInlink(uiBlock *block, void *poin)
837 return ui_find_inlink(block, poin);
840 void uiComposeLinks(uiBlock *block)
847 but= block->buttons.first;
849 if(but->type==LINK) {
852 /* for all pointers in the array */
856 for(a=0; a < *(link->totlink); a++) {
857 bt= ui_find_inlink(block, (*ppoin)[a] );
859 ui_add_link_line(&link->lines, but, bt);
863 else if(link->poin) {
864 bt= ui_find_inlink(block, *(link->poin) );
866 ui_add_link_line(&link->lines, but, bt);
876 /* ************************************************ */
878 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
882 block->lockstr= lockstr;
886 void uiBlockClearButLock(uiBlock *block)
889 block->lockstr= NULL;
892 /* *************************************************************** */
895 /* XXX 2.50 no links supported yet */
897 static void ui_delete_active_linkline(uiBlock *block)
901 uiLinkLine *line, *nline;
904 but= block->buttons.first;
906 if(but->type==LINK && but->link) {
907 line= but->link->lines.first;
912 if(line->flag & UI_SELECT) {
913 BLI_remlink(&but->link->lines, line);
915 link= line->from->link;
917 /* are there more pointers allowed? */
920 if(*(link->totlink)==1) {
922 MEM_freeN(*(link->ppoin));
923 *(link->ppoin)= NULL;
927 for(a=0; a< (*(link->totlink)); a++) {
929 if( (*(link->ppoin))[a] != line->to->poin ) {
930 (*(link->ppoin))[b]= (*(link->ppoin))[a];
934 (*(link->totlink))--;
949 /* temporal! these buttons can be everywhere... */
950 allqueue(REDRAWBUTSLOGIC, 0);
953 static void ui_do_active_linklines(uiBlock *block, short *mval)
956 uiLinkLine *line, *act= NULL;
957 float mindist= 12.0, fac, v1[2], v2[2], v3[3];
964 /* find a line close to the mouse */
965 but= block->buttons.first;
967 if(but->type==LINK && but->link) {
969 line= but->link->lines.first;
971 v2[0]= line->from->x2;
972 v2[1]= (line->from->y1+line->from->y2)/2.0;
974 v3[1]= (line->to->y1+line->to->y2)/2.0;
976 fac= PdistVL2Dfl(v1, v2, v3);
988 /* check for a 'found one' to prevent going to 'frontbuffer' mode.
989 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
991 glDrawBuffer(GL_FRONT);
994 but= block->buttons.first;
996 if(but->type==LINK && but->link) {
997 line= but->link->lines.first;
1000 if((line->flag & UI_SELECT)==0) {
1001 line->flag |= UI_SELECT;
1002 ui_draw_linkline(but, line);
1005 else if(line->flag & UI_SELECT) {
1006 line->flag &= ~UI_SELECT;
1007 ui_draw_linkline(but, line);
1015 glDrawBuffer(GL_BACK);
1020 /* ******************************************************* */
1022 /* XXX 2.50 no screendump supported yet */
1025 /* nasty but safe way to store screendump rect */
1026 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
1028 static void ui_set_screendump_bbox(uiBlock *block)
1033 scr_sizex= block->maxx - block->minx;
1034 scr_sizey= block->maxy - block->miny;
1037 scr_sizex= scr_sizey= 0;
1041 /* used for making screenshots for menus, called in screendump.c */
1042 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1044 if(scr_sizex!=0 && scr_sizey!=0) {
1056 /* *********************** data get/set ***********************
1057 * this either works with the pointed to data, or can work with
1058 * an edit override pointer while dragging for example */
1060 /* for buttons pointing to color for example */
1061 void ui_get_but_vectorf(uiBut *but, float *vec)
1067 VECCOPY(vec, but->editvec);
1074 vec[0]= vec[1]= vec[2]= 0.0f;
1076 if(RNA_property_type(prop) == PROP_FLOAT) {
1077 tot= RNA_property_array_length(prop);
1080 for(a=0; a<tot; a++)
1081 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1084 else if(but->pointype == CHA) {
1085 char *cp= (char *)but->poin;
1086 vec[0]= ((float)cp[0])/255.0;
1087 vec[1]= ((float)cp[1])/255.0;
1088 vec[2]= ((float)cp[2])/255.0;
1090 else if(but->pointype == FLO) {
1091 float *fp= (float *)but->poin;
1096 /* for buttons pointing to color for example */
1097 void ui_set_but_vectorf(uiBut *but, float *vec)
1103 VECCOPY(but->editvec, vec);
1110 if(RNA_property_type(prop) == PROP_FLOAT) {
1111 tot= RNA_property_array_length(prop);
1114 for(a=0; a<tot; a++)
1115 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1118 else if(but->pointype == CHA) {
1119 char *cp= (char *)but->poin;
1120 cp[0]= (char)(0.5 +vec[0]*255.0);
1121 cp[1]= (char)(0.5 +vec[1]*255.0);
1122 cp[2]= (char)(0.5 +vec[2]*255.0);
1124 else if(but->pointype == FLO) {
1125 float *fp= (float *)but->poin;
1130 int ui_is_but_float(uiBut *but)
1132 if(but->pointype==FLO && but->poin)
1135 if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1141 double ui_get_but_val(uiBut *but)
1146 if(but->editval) { return *(but->editval); }
1147 if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1152 switch(RNA_property_type(prop)) {
1154 if(RNA_property_array_length(prop))
1155 value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1157 value= RNA_property_boolean_get(&but->rnapoin, prop);
1160 if(RNA_property_array_length(prop))
1161 value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1163 value= RNA_property_int_get(&but->rnapoin, prop);
1166 if(RNA_property_array_length(prop))
1167 value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1169 value= RNA_property_float_get(&but->rnapoin, prop);
1172 value= RNA_property_enum_get(&but->rnapoin, prop);
1179 else if(but->type== HSVSLI) {
1182 fp= (but->editvec)? but->editvec: (float *)but->poin;
1183 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1185 switch(but->str[0]) {
1186 case 'H': value= h; break;
1187 case 'S': value= s; break;
1188 case 'V': value= v; break;
1191 else if( but->pointype == CHA ) {
1192 value= *(char *)but->poin;
1194 else if( but->pointype == SHO ) {
1195 value= *(short *)but->poin;
1197 else if( but->pointype == INT ) {
1198 value= *(int *)but->poin;
1200 else if( but->pointype == FLO ) {
1201 value= *(float *)but->poin;
1207 void ui_set_but_val(uiBut *but, double value)
1211 /* value is a hsv value: convert to rgb */
1215 if(RNA_property_editable(&but->rnapoin, prop)) {
1216 switch(RNA_property_type(prop)) {
1218 if(RNA_property_array_length(prop))
1219 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1221 RNA_property_boolean_set(&but->rnapoin, prop, value);
1224 if(RNA_property_array_length(prop))
1225 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, value);
1227 RNA_property_int_set(&but->rnapoin, prop, value);
1230 if(RNA_property_array_length(prop))
1231 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1233 RNA_property_float_set(&but->rnapoin, prop, value);
1236 RNA_property_enum_set(&but->rnapoin, prop, value);
1243 else if(but->pointype==0);
1244 else if(but->type==HSVSLI ) {
1247 fp= (but->editvec)? but->editvec: (float *)but->poin;
1248 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1250 switch(but->str[0]) {
1251 case 'H': h= value; break;
1252 case 'S': s= value; break;
1253 case 'V': v= value; break;
1256 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1260 /* first do rounding */
1261 if(but->pointype==CHA)
1262 value= (char)floor(value+0.5);
1263 else if(but->pointype==SHO ) {
1264 /* gcc 3.2.1 seems to have problems
1265 * casting a double like 32772.0 to
1266 * a short so we cast to an int, then
1269 gcckludge = (int) floor(value+0.5);
1270 value= (short)gcckludge;
1272 else if(but->pointype==INT )
1273 value= (int)floor(value+0.5);
1274 else if(but->pointype==FLO ) {
1275 float fval= (float)value;
1276 if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f; /* prevent negative zero */
1280 /* then set value with possible edit override */
1282 *but->editval= value;
1283 else if(but->pointype==CHA)
1284 *((char *)but->poin)= (char)value;
1285 else if(but->pointype==SHO)
1286 *((short *)but->poin)= (short)value;
1287 else if(but->pointype==INT)
1288 *((int *)but->poin)= (int)value;
1289 else if(but->pointype==FLO)
1290 *((float *)but->poin)= (float)value;
1293 /* update select flag */
1297 int ui_get_but_string_max_length(uiBut *but)
1299 if(but->type == TEX)
1300 return but->hardmax;
1301 else if(but->type == IDPOIN)
1302 return sizeof(((ID*)NULL)->name)-2;
1304 return UI_MAX_DRAW_STR;
1307 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1309 if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1313 type= RNA_property_type(but->rnaprop);
1315 if(type == PROP_STRING) {
1317 buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1319 else if(type == PROP_POINTER) {
1321 PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1322 buf= RNA_struct_name_get_alloc(&ptr, str, maxlen);
1326 BLI_strncpy(str, "", maxlen);
1328 else if(buf && buf != str) {
1329 /* string was too long, we have to truncate */
1330 BLI_strncpy(str, buf, maxlen);
1334 else if(but->type == IDPOIN) {
1336 ID *id= *(but->idpoin_idpp);
1338 if(id) BLI_strncpy(str, id->name+2, maxlen);
1339 else BLI_strncpy(str, "", maxlen);
1343 else if(but->type == TEX) {
1345 BLI_strncpy(str, but->poin, maxlen);
1348 else if(but->type == SEARCH_MENU) {
1350 BLI_strncpy(str, but->poin, maxlen);
1353 else if(ui_but_anim_expression_get(but, str, maxlen))
1354 ; /* driver expression */
1356 /* number editing */
1359 value= ui_get_but_val(but);
1361 if(ui_is_but_float(but)) {
1362 if(but->a2) { /* amount of digits defined */
1363 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1364 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1365 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1366 else BLI_snprintf(str, maxlen, "%.4f", value);
1369 BLI_snprintf(str, maxlen, "%.3f", value);
1372 BLI_snprintf(str, maxlen, "%d", (int)value);
1376 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1378 if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1379 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1382 type= RNA_property_type(but->rnaprop);
1384 if(type == PROP_STRING) {
1386 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1389 else if(type == PROP_POINTER) {
1391 PointerRNA ptr, rptr;
1394 if(str == NULL || str[0] == '\0') {
1395 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1399 ptr= but->rnasearchpoin;
1400 prop= but->rnasearchprop;
1402 if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1403 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1412 else if(but->type == IDPOIN) {
1414 but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1417 else if(but->type == TEX) {
1419 BLI_strncpy(but->poin, str, but->hardmax);
1422 else if(but->type == SEARCH_MENU) {
1424 BLI_strncpy(but->poin, str, but->hardmax);
1427 else if(ui_but_anim_expression_set(but, str)) {
1428 /* driver expression */
1432 /* number editing */
1435 /* XXX 2.50 missing python api */
1437 if(BPY_button_eval(str, &value)) {
1438 BKE_report(CTX_reports(C), RPT_WARNING, "Invalid Python expression, check console");
1439 value = 0.0f; /* Zero out value on error */
1448 if(!ui_is_but_float(but)) value= (int)value;
1449 if(but->type==NUMABS) value= fabs(value);
1451 /* not that we use hard limits here */
1452 if(value<but->hardmin) value= but->hardmin;
1453 if(value>but->hardmax) value= but->hardmax;
1455 ui_set_but_val(but, value);
1462 static double soft_range_round_up(double value, double max)
1464 /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1465 double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1467 if(newmax*0.2 >= max && newmax*0.2 >= value)
1469 else if(newmax*0.5 >= max && newmax*0.5 >= value)
1475 static double soft_range_round_down(double value, double max)
1477 /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1478 double newmax= pow(10.0, floor(log(value)/log(10.0)));
1480 if(newmax*5.0 <= max && newmax*5.0 <= value)
1482 else if(newmax*2.0 <= max && newmax*2.0 <= value)
1488 void ui_set_but_soft_range(uiBut *but, double value)
1491 double softmin, softmax, step, precision;
1494 type= RNA_property_type(but->rnaprop);
1496 if(type == PROP_INT) {
1497 int imin, imax, istep;
1499 RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1505 else if(type == PROP_FLOAT) {
1506 float fmin, fmax, fstep, fprecision;
1508 RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1512 precision= fprecision;
1517 /* clamp button range to something reasonable in case
1518 * we get -inf/inf from RNA properties */
1519 softmin= MAX2(softmin, -1e4);
1520 softmax= MIN2(softmax, 1e4);
1522 /* if the value goes out of the soft/max range, adapt the range */
1523 if(value+1e-10 < softmin) {
1525 softmin= -soft_range_round_up(-value, -softmin);
1527 softmin= soft_range_round_down(value, softmin);
1529 if(softmin < but->hardmin)
1530 softmin= but->hardmin;
1532 else if(value-1e-10 > softmax) {
1534 softmax= -soft_range_round_down(-value, -softmax);
1536 softmax= soft_range_round_up(value, softmax);
1538 if(softmax > but->hardmax)
1539 softmax= but->hardmax;
1542 but->softmin= softmin;
1543 but->softmax= softmax;
1547 /* ******************* Free ********************/
1549 static void ui_free_link(uiLink *link)
1552 BLI_freelistN(&link->lines);
1557 /* can be called with C==NULL */
1558 static void ui_free_but(const bContext *C, uiBut *but)
1561 WM_operator_properties_free(but->opptr);
1562 MEM_freeN(but->opptr);
1564 if(but->func_argN) MEM_freeN(but->func_argN);
1566 /* XXX solve later, buttons should be free-able without context? */
1568 ui_button_active_cancel(C, but);
1571 MEM_freeN(but->active);
1573 if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1574 ui_free_link(but->link);
1579 /* can be called with C==NULL */
1580 void uiFreeBlock(const bContext *C, uiBlock *block)
1584 while( (but= block->buttons.first) ) {
1585 BLI_remlink(&block->buttons, but);
1586 ui_free_but(C, but);
1589 CTX_store_free_list(&block->contexts);
1591 BLI_freelistN(&block->saferct);
1596 /* can be called with C==NULL */
1597 void uiFreeBlocks(const bContext *C, ListBase *lb)
1601 while( (block= lb->first) ) {
1602 BLI_remlink(lb, block);
1603 uiFreeBlock(C, block);
1607 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1609 uiBlock *block, *nextblock;
1611 for(block=lb->first; block; block=nextblock) {
1612 nextblock= block->next;
1614 if(!block->handle) {
1615 if(!block->active) {
1616 BLI_remlink(lb, block);
1617 uiFreeBlock(C, block);
1625 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1628 uiBlock *oldblock= NULL;
1630 lb= ®ion->uiblocks;
1632 /* each listbase only has one block with this name, free block
1633 * if is already there so it can be rebuilt from scratch */
1635 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1636 if (BLI_streq(oldblock->name, block->name))
1640 oldblock->active= 0;
1641 oldblock->panel= NULL;
1645 block->oldblock= oldblock;
1647 /* at the beginning of the list! for dynamical menus/blocks */
1649 BLI_addhead(lb, block);
1652 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1656 int getsizex, getsizey;
1658 window= CTX_wm_window(C);
1660 block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1663 block->evil_C= (void*)C; // XXX
1664 BLI_strncpy(block->name, name, sizeof(block->name));
1667 uiBlockSetRegion(block, region);
1669 /* window matrix and aspect */
1670 if(region && region->swinid) {
1671 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1672 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1674 /* TODO - investigate why block->winmat[0][0] is negative
1675 * in the image view when viewRedrawForce is called */
1676 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1679 /* no subwindow created yet, for menus for example, so we
1680 * use the main window instead, since buttons are created
1682 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1683 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1685 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1686 block->auto_open= 2;
1687 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1693 uiBlock *uiGetBlock(char *name, ARegion *ar)
1695 uiBlock *block= ar->uiblocks.first;
1698 if( strcmp(name, block->name)==0 ) return block;
1705 void uiBlockSetEmboss(uiBlock *block, short dt)
1710 void ui_check_but(uiBut *but)
1712 /* if something changed in the button */
1715 // int transopts= ui_translate_buttons();
1719 // if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1721 /* test for min and max, icon sliders, etc */
1722 switch( but->type ) {
1728 value= ui_get_but_val(but);
1729 if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1730 else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1734 value= fabs( ui_get_but_val(but) );
1735 if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1736 else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1741 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1742 if(but->flag & UI_SELECT) but->iconadd= 1;
1743 else but->iconadd= 0;
1748 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1749 value= ui_get_but_val(but);
1750 but->iconadd= (int)value- (int)(but->hardmin);
1755 if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1756 value= ui_get_but_val(but);
1757 but->iconadd= (int)value- (int)(but->hardmin);
1763 /* safety is 4 to enable small number buttons (like 'users') */
1764 okwidth= -4 + (but->x2 - but->x1);
1767 switch( but->type ) {
1772 if(but->x2 - but->x1 > 24) {
1773 value= ui_get_but_val(but);
1774 ui_set_name_menu(but, (int)value);
1783 value= ui_get_but_val(but);
1785 if(ui_is_but_float(but)) {
1786 if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
1787 else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
1788 else if(but->a2) { /* amount of digits defined */
1789 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1790 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1791 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1792 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1795 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1796 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1800 sprintf(but->drawstr, "%s%d", but->str, (int)value);
1804 PropertySubType pstype = RNA_property_subtype(but->rnaprop);
1806 if (pstype == PROP_PERCENTAGE)
1807 strcat(but->drawstr, "%");
1812 if(ui_is_but_float(but)) {
1813 value= ui_get_but_val(but);
1814 if(but->a2) { /* amount of digits defined */
1815 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1816 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1817 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1818 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1821 sprintf(but->drawstr, "%s%.2f", but->str, value);
1824 else strcpy(but->drawstr, but->str);
1832 char str[UI_MAX_DRAW_STR];
1834 ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1836 strcpy(but->drawstr, but->str);
1837 strcat(but->drawstr, str);
1842 strcpy(but->drawstr, but->str);
1843 if (but->flag & UI_SELECT) {
1844 strcat(but->drawstr, "Press a key");
1846 strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1851 /* trying to get the dual-icon to left of text... not very nice */
1853 strcpy(but->drawstr, " ");
1854 strcpy(but->drawstr+2, but->str);
1858 strcpy(but->drawstr, but->str);
1862 /* if we are doing text editing, this will override the drawstr */
1864 strcpy(but->drawstr, but->str);
1865 strcat(but->drawstr, but->editstr);
1868 /* text clipping moved to widget drawing code itself */
1872 void uiBlockBeginAlign(uiBlock *block)
1874 /* if other align was active, end it */
1875 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1877 block->flag |= UI_BUT_ALIGN_DOWN;
1880 /* buttons declared after this call will get this align nr */ // XXX flag?
1883 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1887 dx= fabs( but1->x2 - but2->x1);
1888 dy= fabs( but1->y1 - but2->y2);
1890 if(dx > dy) return 0;
1894 void uiBlockEndAlign(uiBlock *block)
1896 block->flag &= ~UI_BUT_ALIGN; // all 4 flags
1899 int ui_but_can_align(uiBut *but)
1901 return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
1904 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
1906 uiBut *prev, *but=NULL, *next;
1907 int flag= 0, cols=0, rows=0;
1911 for(but=first; but && but->alignnr == nr; but=but->next) {
1912 if(but->next && but->next->alignnr == nr) {
1913 if(buts_are_horiz(but, but->next)) cols++;
1918 /* rows==0: 1 row, cols==0: 1 collumn */
1920 /* note; how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1921 for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
1923 if(next && next->alignnr != nr)
1926 /* clear old flag */
1927 but->flag &= ~UI_BUT_ALIGN;
1929 if(flag==0) { /* first case */
1931 if(buts_are_horiz(but, next)) {
1933 flag= UI_BUT_ALIGN_RIGHT;
1935 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
1938 flag= UI_BUT_ALIGN_DOWN;
1942 else if(next==NULL) { /* last case */
1944 if(buts_are_horiz(prev, but)) {
1946 flag= UI_BUT_ALIGN_LEFT;
1948 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
1950 else flag= UI_BUT_ALIGN_TOP;
1953 else if(buts_are_horiz(but, next)) {
1954 /* check if this is already second row */
1955 if( prev && buts_are_horiz(prev, but)==0) {
1956 flag &= ~UI_BUT_ALIGN_LEFT;
1957 flag |= UI_BUT_ALIGN_TOP;
1958 /* exception case: bottom row */
1961 while(bt && bt->alignnr == nr) {
1962 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break;
1965 if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
1968 else flag |= UI_BUT_ALIGN_LEFT;
1972 flag |= UI_BUT_ALIGN_TOP;
1974 else { /* next button switches to new row */
1976 if(prev && buts_are_horiz(prev, but))
1977 flag |= UI_BUT_ALIGN_LEFT;
1979 if( (flag & UI_BUT_ALIGN_TOP)==0) { /* stil top row */
1981 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
1983 flag |= UI_BUT_ALIGN_DOWN;
1986 flag |= UI_BUT_ALIGN_TOP;
1992 /* merge coordinates */
1996 but->x1= (prev->x2+but->x1)/2.0;
2000 but->y2= (prev->y1+but->y2)/2.0;
2004 if(buts_are_horiz(prev, but)) {
2005 but->x1= (prev->x2+but->x1)/2.0;
2007 /* copy height too */
2010 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2011 /* the previous button is a single one in its row */
2012 but->y2= (prev->y1+but->y2)/2.0;
2016 /* the previous button is not a single one in its row */
2024 void ui_block_do_align(uiBlock *block)
2029 /* align buttons with same align nr */
2030 for(but=block->buttons.first; but;) {
2033 ui_block_do_align_but(block, but, nr);
2035 /* skip with same number */
2036 for(; but && but->alignnr == nr; but=but->next);
2047 ui_def_but is the function that draws many button types
2050 "a1" Click Step (how much to change the value each click)
2051 "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2052 all greater values will be clamped to 4.
2055 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)
2060 if(type & BUTPOIN) { /* a pointer is required */
2065 but= MEM_callocN(sizeof(uiBut), "uiBut");
2067 but->type= type & BUTTYPE;
2068 but->pointype= type & BUTPOIN;
2069 but->bit= type & BIT;
2070 but->bitnr= type & 31;
2073 but->retval= retval;
2074 if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2075 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2076 strcpy(but->str, str);
2079 but->str= but->strdata;
2080 strcpy(but->str, str);
2088 but->hardmin= but->softmin= min;
2089 but->hardmax= but->softmax= max;
2094 but->lock= block->lock;
2095 but->lockstr= block->lockstr;
2098 but->aspect= 1.0f; //XXX block->aspect;
2099 but->block= block; // pointer back, used for frontbuffer status, and picker
2101 if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2102 but->alignnr= block->alignnr;
2104 but->func= block->func;
2105 but->func_arg1= block->func_arg1;
2106 but->func_arg2= block->func_arg2;
2108 but->pos= -1; /* cursor invisible */
2110 if(ELEM(but->type, NUM, NUMABS)) { /* add a space to name */
2111 slen= strlen(but->str);
2112 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2113 if(but->str[slen-1]!=' ') {
2114 but->str[slen]= ' ';
2115 but->str[slen+1]= 0;
2120 if(ELEM(but->type, HSVCUBE, HSVCIRCLE)) { /* hsv buttons temp storage */
2122 ui_get_but_vectorf(but, rgb);
2123 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2126 if((block->flag & UI_BLOCK_LOOP) || ELEM7(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU))
2127 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2128 else if(but->type==BUT_TOGDUAL)
2129 but->flag |= UI_ICON_LEFT;
2131 but->flag |= (block->flag & UI_BUT_ALIGN);
2135 but->flag |= UI_BUT_DISABLED;
2139 BLI_addtail(&block->buttons, but);
2141 if(block->curlayout)
2142 ui_layout_add_but(block->curlayout, but);
2147 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)
2151 PropertyType proptype;
2152 int freestr= 0, icon= 0;
2154 prop= RNA_struct_find_property(ptr, propname);
2157 proptype= RNA_property_type(prop);
2159 /* use rna values if parameters are not specified */
2161 if(type == MENU && proptype == PROP_ENUM) {
2162 EnumPropertyItem *item;
2164 int i, totitem, value, free;
2166 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2167 value= RNA_property_enum_get(ptr, prop);
2169 dynstr= BLI_dynstr_new();
2170 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2171 for(i=0; i<totitem; i++) {
2172 if(!item[i].identifier[0])
2173 BLI_dynstr_append(dynstr, "|%l");
2174 else if(item[i].icon)
2175 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2177 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2179 if(value == item[i].value)
2182 str= BLI_dynstr_get_cstring(dynstr);
2183 BLI_dynstr_free(dynstr);
2190 else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2191 EnumPropertyItem *item;
2192 int i, totitem, free;
2194 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2195 for(i=0; i<totitem; i++) {
2196 if(item[i].identifier[0] && item[i].value == (int)max) {
2197 str= (char*)item[i].name;
2203 str= (char*)RNA_property_ui_name(prop);
2208 str= (char*)RNA_property_ui_name(prop);
2209 icon= RNA_property_ui_icon(prop);
2214 if(type == ROW && proptype == PROP_ENUM) {
2215 EnumPropertyItem *item;
2216 int i, totitem, free;
2218 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2220 for(i=0; i<totitem; i++) {
2221 if(item[i].identifier[0] && item[i].value == (int)max) {
2222 if(item[i].description[0])
2223 tip= (char*)item[i].description;
2234 tip= (char*)RNA_property_ui_description(prop);
2236 if(min == max || a1 == -1 || a2 == -1) {
2237 if(proptype == PROP_INT) {
2238 int hardmin, hardmax, softmin, softmax, step;
2240 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2241 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2243 if(!ELEM(type, ROW, LISTROW) && min == max) {
2252 else if(proptype == PROP_FLOAT) {
2253 float hardmin, hardmax, softmin, softmax, step, precision;
2255 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2256 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2258 if(!ELEM(type, ROW, LISTROW) && min == max) {
2267 else if(proptype == PROP_STRING) {
2269 max= RNA_property_string_maxlength(prop);
2270 if(max == 0) /* interface code should ideally support unlimited length */
2271 max= UI_MAX_DRAW_STR;
2276 str= (char*)propname;
2278 /* now create button */
2279 but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2285 if(RNA_property_array_length(but->rnaprop))
2286 but->rnaindex= index;
2292 but->icon= (BIFIconID)icon;
2293 but->flag |= UI_HAS_ICON;
2294 but->flag|= UI_ICON_LEFT;
2297 if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2298 but->flag |= UI_BUT_DISABLED;
2309 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)
2314 ot= WM_operatortype_find(opname, 0);
2317 if(ot) str= ot->name;
2321 if ((!tip || tip[0]=='\0') && ot && ot->description) {
2322 tip= ot->description;
2325 but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2327 but->opcontext= opcontext;
2330 but->flag |= UI_BUT_DISABLED;
2338 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)
2340 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2347 /* if _x_ is a power of two (only one bit) return the power,
2348 * otherwise return -1.
2349 * (1<<findBitIndex(x))==x for powers of two.
2351 static int findBitIndex(unsigned int x) {
2352 if (!x || (x&(x-1))!=0) { /* x&(x-1) strips lowest bit */
2357 if (x&0xFFFF0000) idx+=16, x>>=16;
2358 if (x&0xFF00) idx+=8, x>>=8;
2359 if (x&0xF0) idx+=4, x>>=4;
2360 if (x&0xC) idx+=2, x>>=2;
2367 /* autocomplete helper functions */
2368 struct AutoComplete {
2374 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2376 AutoComplete *autocpl;
2378 autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2379 autocpl->maxlen= maxlen;
2380 autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2381 autocpl->startname= startname;
2386 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2388 char *truncate= autocpl->truncate;
2389 char *startname= autocpl->startname;
2392 for(a=0; a<autocpl->maxlen-1; a++) {
2393 if(startname[a]==0 || startname[a]!=name[a])
2397 if(startname[a]==0) {
2400 BLI_strncpy(truncate, name, autocpl->maxlen);
2402 /* remove from truncate what is not in bone->name */
2403 for(a=0; a<autocpl->maxlen-1; a++) {
2408 else if(truncate[a]!=name[a])
2415 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2417 if(autocpl->truncate[0])
2418 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2420 if (autoname != autocpl->startname) /* dont copy a string over its self */
2421 BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2423 MEM_freeN(autocpl->truncate);
2427 /* autocomplete callback for ID buttons */
2428 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2430 int blocktype= (intptr_t)arg_v;
2431 ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2433 if(listb==NULL) return;
2435 /* search if str matches the beginning of an ID struct */
2437 AutoComplete *autocpl= autocomplete_begin(str, 22);
2440 for(id= listb->first; id; id= id->next)
2441 autocomplete_do_name(autocpl, id->name+2);
2443 autocomplete_end(autocpl, str);
2447 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)
2449 int bitIdx= findBitIndex(bit);
2453 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2456 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)
2458 return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2460 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)
2462 return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2464 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)
2466 return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2468 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)
2470 return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2472 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)
2474 return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2476 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)
2478 return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2480 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)
2482 return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2484 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)
2486 return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2488 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)
2492 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2498 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2502 but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2509 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)
2511 uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2513 but->icon= (BIFIconID) icon;
2514 but->flag|= UI_HAS_ICON;
2520 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)
2522 int bitIdx= findBitIndex(bit);
2526 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2530 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)
2532 return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2534 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)
2536 return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2538 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)
2540 return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2542 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)
2544 return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2546 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)
2548 return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2550 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)
2552 return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2554 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)
2556 return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2558 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)
2560 return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2562 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)
2566 but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2569 but->icon= (BIFIconID) icon;
2570 but->flag|= UI_HAS_ICON;
2577 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2581 but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2583 but->icon= (BIFIconID) icon;
2584 but->flag|= UI_HAS_ICON;
2591 /* Button containing both string label and icon */
2592 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)
2594 uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2596 but->icon= (BIFIconID) icon;
2597 but->flag|= UI_HAS_ICON;
2599 but->flag|= UI_ICON_LEFT;
2605 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)
2607 int bitIdx= findBitIndex(bit);
2611 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2615 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)
2617 return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2619 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)
2621 return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2623 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)
2625 return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2627 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)
2629 return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2631 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)
2633 return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2635 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)
2637 return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2639 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)
2641 return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2643 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)
2645 return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2647 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)
2651 but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2654 but->icon= (BIFIconID) icon;
2655 but->flag|= UI_HAS_ICON;
2657 but->flag|= UI_ICON_LEFT;
2663 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2667 but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2669 but->icon= (BIFIconID) icon;
2670 but->flag|= UI_HAS_ICON;
2671 but->flag|= UI_ICON_LEFT;
2678 /* END Button containing both string label and icon */
2680 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2684 link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2689 link->fromcode= from;
2693 /* cruft to make uiBlock and uiBut private */
2695 int uiBlocksGetYMin(ListBase *lb)
2700 for (block= lb->first; block; block= block->next)
2701 if (block==lb->first || block->miny<min)
2707 void uiBlockSetDirection(uiBlock *block, int direction)
2709 block->direction= direction;
2712 /* this call escapes if there's alignment flags */
2713 void uiBlockFlipOrder(uiBlock *block)
2717 float centy, miny=10000, maxy= -10000;
2719 if(U.uiflag & USER_MENUFIXEDORDER)
2722 for(but= block->buttons.first; but; but= but->next) {
2723 if(but->flag & UI_BUT_ALIGN) return;
2724 if(but->y1 < miny) miny= but->y1;
2725 if(but->y2 > maxy) maxy= but->y2;
2728 centy= (miny+maxy)/2.0;
2729 for(but= block->buttons.first; but; but= but->next) {
2730 but->y1 = centy-(but->y1-centy);
2731 but->y2 = centy-(but->y2-centy);
2732 SWAP(float, but->y1, but->y2);
2735 /* also flip order in block itself, for example for arrowkey */
2736 lb.first= lb.last= NULL;
2737 but= block->buttons.first;
2740 BLI_remlink(&block->buttons, but);
2741 BLI_addtail(&lb, but);
2748 void uiBlockSetFlag(uiBlock *block, int flag)
2753 void uiBlockClearFlag(uiBlock *block, int flag)
2755 block->flag&= ~flag;
2758 void uiBlockSetXOfs(uiBlock *block, int xofs)
2763 void uiButSetFlag(uiBut *but, int flag)
2768 void uiButClearFlag(uiBut *but, int flag)
2773 int uiButGetRetVal(uiBut *but)
2778 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2780 if(but->optype && !but->opptr) {
2781 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2782 WM_operator_properties_create(but->opptr, but->optype->idname);
2788 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
2790 block->handle_func= func;
2791 block->handle_func_arg= arg;
2794 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
2796 block->butm_func= func;
2797 block->butm_func_arg= arg;
2800 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
2803 block->func_arg1= arg1;
2804 block->func_arg2= arg2;
2807 void uiButSetRenameFunc(uiBut *but, uiButHandleRenameFunc func, void *arg1)
2809 but->rename_func= func;
2810 but->rename_arg1= arg1;
2813 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)(const bContext *C, void *idv, void *argv, rcti *rect), void *arg)
2815 block->drawextra= func;
2816 block->drawextra_arg= arg;
2819 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
2822 but->func_arg1= arg1;
2823 but->func_arg2= arg2;
2826 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
2829 but->func_argN= argN;
2830 but->func_arg2= arg2;
2833 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
2835 but->autocomplete_func= func;
2836 but->autofunc_arg= arg;
2839 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2841 uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2842 but->idpoin_func= func;
2843 but->idpoin_idpp= (ID**) idpp;
2847 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2852 uiBut *uiDefBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2854 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2855 but->block_create_func= func;
2860 uiBut *uiDefBlockButN(uiBlock *block, uiBlockCreateFunc func, void *argN, char *str, short x1, short y1, short x2, short y2, char *tip)
2862 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2863 but->block_create_func= func;
2864 but->func_argN= argN;
2870 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2872 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2873 but->block_create_func= func;
2878 uiBut *uiDefMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2880 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2881 but->menu_create_func= func;
2886 uiBut *uiDefIconTextMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2888 uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2890 but->icon= (BIFIconID) icon;
2891 but->flag|= UI_HAS_ICON;
2893 but->flag|= UI_ICON_LEFT;
2894 but->flag|= UI_ICON_SUBMENU;
2896 but->menu_create_func= func;
2902 /* Block button containing both string label and icon */
2903 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2905 uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2907 /* XXX temp, old menu calls pass on icon arrow, which is now UI_ICON_SUBMENU flag */
2908 if(icon!=ICON_RIGHTARROW_THIN) {
2909 but->icon= (BIFIconID) icon;
2910 but->flag|= UI_ICON_LEFT;
2912 but->flag|= UI_HAS_ICON;
2913 but->flag|= UI_ICON_SUBMENU;
2915 but->block_create_func= func;
2921 /* Block button containing icon */
2922 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
2924 uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2926 but->icon= (BIFIconID) icon;
2927 but->flag|= UI_HAS_ICON;
2929 but->flag|= UI_ICON_LEFT;
2930 but->flag|= UI_ICON_SUBMENU;
2932 but->block_create_func= func;
2938 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
2940 uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
2944 /* arg is pointer to string/name, use uiButSetSearchFunc() below to make this work */
2945 uiBut *uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxlen, short x1, short y1, short x2, short y2, char *tip)
2947 uiBut *but= ui_def_but(block, SEARCH_MENU, retval, "", x1, y1, x2, y2, arg, 0.0, maxlen, 0.0, 0.0, tip);
2949 but->icon= (BIFIconID) icon;
2950 but->flag|= UI_HAS_ICON;
2952 but->flag|= UI_ICON_LEFT|UI_TEXT_LEFT;
2959 /* arg is user value, searchfunc and handlefunc both get it as arg */
2960 /* if active set, button opens with this item visible and selected */
2961 void uiButSetSearchFunc(uiBut *but, uiButSearchFunc sfunc, void *arg, uiButHandleFunc bfunc, void *active)
2963 but->search_func= sfunc;
2964 but->search_arg= arg;
2966 uiButSetFunc(but, bfunc, arg, active);
2969 /* Program Init/Exit */
2973 ui_resources_init();
2976 /* after reading userdef file */
2977 void UI_init_userdef(void)
2979 /* fix saved themes */
2980 init_userdef_do_versions();
2981 /* set default colors in default theme */
2982 ui_theme_init_userdef();
2989 ui_resources_free();