2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation
23 * ***** END GPL LICENSE BLOCK *****
31 #include "MEM_guardedalloc.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_view2d_types.h"
35 #include "DNA_userdef_types.h"
36 #include "DNA_windowmanager_types.h"
38 #include "BLI_arithb.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_dynstr.h"
42 #include "BKE_context.h"
43 #include "BKE_icons.h"
44 #include "BKE_report.h"
45 #include "BKE_screen.h"
46 #include "BKE_texture.h"
47 #include "BKE_utildefines.h"
52 #include "wm_subwindow.h"
53 #include "wm_window.h"
55 #include "RNA_access.h"
59 #include "UI_interface.h"
60 #include "UI_interface_icons.h"
61 #include "UI_view2d.h"
65 #include "ED_screen.h"
67 #include "interface_intern.h"
69 #define MENU_BUTTON_HEIGHT 20
70 #define MENU_SEPR_HEIGHT 6
72 #define MENU_SHADOW_SIDE 8
73 #define MENU_SHADOW_BOTTOM 10
76 /*********************** Menu Data Parsing ********************* */
90 int nitems, itemssize;
93 static MenuData *menudata_new(char *instr)
95 MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
101 md->nitems= md->itemssize= 0;
106 static void menudata_set_title(MenuData *md, char *title, int titleicon)
111 md->titleicon= titleicon;
114 static void menudata_add_item(MenuData *md, char *str, int retval, int icon)
116 if (md->nitems==md->itemssize) {
117 int nsize= md->itemssize?(md->itemssize<<1):1;
118 MenuEntry *oitems= md->items;
120 md->items= MEM_mallocN(nsize*sizeof(*md->items), "md->items");
122 memcpy(md->items, oitems, md->nitems*sizeof(*md->items));
126 md->itemssize= nsize;
129 md->items[md->nitems].str= str;
130 md->items[md->nitems].retval= retval;
131 md->items[md->nitems].icon= icon;
135 void menudata_free(MenuData *md)
137 MEM_freeN(md->instr);
139 MEM_freeN(md->items);
144 * Parse menu description strings, string is of the
145 * form "[sss%t|]{(sss[%xNN]|), (%l|)}", ssss%t indicates the
146 * menu title, sss or sss%xNN indicates an option,
147 * if %xNN is given then NN is the return value if
148 * that option is selected otherwise the return value
149 * is the index of the option (starting with 1). %l
150 * indicates a seperator.
152 * @param str String to be parsed.
153 * @retval new menudata structure, free with menudata_free()
155 MenuData *decompose_menu_string(char *str)
157 char *instr= BLI_strdup(str);
158 MenuData *md= menudata_new(instr);
159 char *nitem= NULL, *s= instr;
160 int nicon=0, nretval= 1, nitem_is_title= 0;
171 } else if (s[1]=='t') {
176 } else if (s[1]=='l') {
179 } else if (s[1]=='i') {
185 } else if (c=='|' || c == '\n' || c=='\0') {
189 if (nitem_is_title) {
190 menudata_set_title(md, nitem, nicon);
193 /* prevent separator to get a value */
194 if(nitem[0]=='%' && nitem[1]=='l')
195 menudata_add_item(md, nitem, -1, nicon);
197 menudata_add_item(md, nitem, nretval, nicon);
198 nretval= md->nitems+1;
216 void ui_set_name_menu(uiBut *but, int value)
221 md= decompose_menu_string(but->str);
222 for (i=0; i<md->nitems; i++)
223 if (md->items[i].retval==value)
224 strcpy(but->drawstr, md->items[i].str);
229 int ui_step_name_menu(uiBut *but, int step)
232 int value= ui_get_but_val(but);
235 md= decompose_menu_string(but->str);
236 for (i=0; i<md->nitems; i++)
237 if (md->items[i].retval==value)
241 /* skip separators */
242 for(; i<md->nitems-1; i++) {
243 if(md->items[i+1].retval != -1) {
244 value= md->items[i+1].retval;
251 /* skip separators */
253 if(md->items[i-1].retval != -1) {
254 value= md->items[i-1].retval;
267 /******************** Creating Temporary regions ******************/
269 ARegion *ui_add_temporary_region(bScreen *sc)
273 ar= MEM_callocN(sizeof(ARegion), "area region");
274 BLI_addtail(&sc->regionbase, ar);
276 ar->regiontype= RGN_TYPE_TEMPORARY;
277 ar->alignment= RGN_ALIGN_FLOAT;
282 void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
285 wm_draw_region_clear(CTX_wm_window(C), ar);
287 ED_region_exit(C, ar);
288 BKE_area_region_free(NULL, ar); /* NULL: no spacetype */
289 BLI_freelinkN(&sc->regionbase, ar);
292 /************************* Creating Tooltips **********************/
294 #define MAX_TOOLTIP_LINES 8
296 typedef struct uiTooltipData {
299 char lines[MAX_TOOLTIP_LINES][512];
300 int linedark[MAX_TOOLTIP_LINES];
302 int toth, spaceh, lineh;
305 static void ui_tooltip_region_draw(const bContext *C, ARegion *ar)
307 uiTooltipData *data= ar->regiondata;
308 rcti bbox= data->bbox;
311 ui_draw_menu_back(U.uistyles.first, NULL, &data->bbox);
314 uiStyleFontSet(&data->fstyle);
316 bbox.ymax= bbox.ymax - 0.5f*((bbox.ymax - bbox.ymin) - data->toth);
317 bbox.ymin= bbox.ymax - data->lineh;
319 for(a=0; a<data->totline; a++) {
320 if(!data->linedark[a]) glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
321 else glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
323 uiStyleFontDraw(&data->fstyle, &bbox, data->lines[a]);
324 bbox.ymin -= data->lineh + data->spaceh;
325 bbox.ymax -= data->lineh + data->spaceh;
329 static void ui_tooltip_region_free(ARegion *ar)
333 data= ar->regiondata;
335 ar->regiondata= NULL;
338 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
340 uiStyle *style= U.uistyles.first; // XXX pass on as arg
341 static ARegionType type;
346 float fonth, fontw, aspect= but->block->aspect;
347 float x1f, x2f, y1f, y2f;
348 int x1, x2, y1, y2, winx, winy, ofsx, ofsy, w, h, a;
350 /* create tooltip data */
351 data= MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
353 if(but->tip && strlen(but->tip)) {
354 BLI_strncpy(data->lines[data->totline], but->tip, sizeof(data->lines[0]));
358 if(but->optype && !(but->block->flag & UI_BLOCK_LOOP)) {
359 /* operator keymap (not menus, they already have it) */
360 prop= (but->opptr)? but->opptr->data: NULL;
362 if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
363 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Shortcut: %s", buf);
364 data->linedark[data->totline]= 1;
369 if(ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
371 ui_get_but_string(but, buf, sizeof(buf));
373 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Value: %s", buf);
374 data->linedark[data->totline]= 1;
380 if(but->flag & UI_BUT_DRIVEN) {
381 if(ui_but_anim_expression_get(but, buf, sizeof(buf))) {
383 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Expression: %s", buf);
384 data->linedark[data->totline]= 1;
390 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s.%s", RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop));
391 data->linedark[data->totline]= 1;
394 else if (but->optype) {
397 opptr= uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
399 str= WM_operator_pystring(C, but->optype, opptr, 0);
402 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s", str);
403 data->linedark[data->totline]= 1;
409 if(data->totline == 0) {
414 /* create area region */
415 ar= ui_add_temporary_region(CTX_wm_screen(C));
417 memset(&type, 0, sizeof(ARegionType));
418 type.draw= ui_tooltip_region_draw;
419 type.free= ui_tooltip_region_free;
422 /* set font, get bb */
423 data->fstyle= style->widget; /* copy struct */
424 data->fstyle.align= UI_STYLE_TEXT_CENTER;
425 ui_fontscale(&data->fstyle.points, aspect);
426 uiStyleFontSet(&data->fstyle);
428 h= BLF_height(data->lines[0]);
430 for(a=0, fontw=0, fonth=0; a<data->totline; a++) {
431 w= BLF_width(data->lines[a]);
432 fontw= MAX2(fontw, w);
433 fonth += (a == 0)? h: h+5;
439 ar->regiondata= data;
442 data->lineh= h*aspect;
443 data->spaceh= 5*aspect;
445 /* compute position */
446 ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
447 ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
449 x1f= (but->x1+but->x2)/2.0f + ofsx - 16.0f*aspect;
450 x2f= x1f + fontw + 16.0f*aspect;
451 y2f= but->y1 + ofsy - 15.0f*aspect;
452 y1f= y2f - fonth - 10.0f*aspect;
454 /* copy to int, gets projected if possible too */
455 x1= x1f; y1= y1f; x2= x2f; y2= y2f;
458 /* XXX temp, region v2ds can be empty still */
459 if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
460 UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
461 UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
464 x1 += butregion->winrct.xmin;
465 x2 += butregion->winrct.xmin;
466 y1 += butregion->winrct.ymin;
467 y2 += butregion->winrct.ymin;
470 wm_window_get_size(CTX_wm_window(C), &winx, &winy);
488 /* widget rect, in region coords */
489 data->bbox.xmin= MENU_SHADOW_SIDE;
490 data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
491 data->bbox.ymin= MENU_SHADOW_BOTTOM;
492 data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
494 /* region bigger for shadow */
495 ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
496 ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
497 ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
498 ar->winrct.ymax= y2 + MENU_TOP;
501 ED_region_init(C, ar);
503 /* notify change and redraw */
504 ED_region_tag_redraw(ar);
509 void ui_tooltip_free(bContext *C, ARegion *ar)
511 ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
515 /************************* Creating Search Box **********************/
517 struct uiSearchItems {
518 int maxitem, totitem, maxstrlen;
520 int offset, offset_i; /* offset for inserting in array */
521 int more; /* flag indicating there are more items */
527 AutoComplete *autocpl;
531 typedef struct uiSearchboxData {
535 int active; /* index in items array */
536 int noback; /* when menu opened with enough space for this */
539 #define SEARCH_ITEMS 10
541 /* exported for use by search callbacks */
542 /* returns zero if nothing to add */
543 int uiSearchItemAdd(uiSearchItems *items, const char *name, void *poin, int iconid)
545 /* hijack for autocomplete */
547 autocomplete_do_name(items->autocpl, name);
551 /* hijack for finding active item */
553 if(poin==items->active)
554 items->offset_i= items->totitem;
559 if(items->totitem>=items->maxitem) {
564 /* skip first items in list */
565 if(items->offset_i > 0) {
570 BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
571 items->pointers[items->totitem]= poin;
572 items->icons[items->totitem]= iconid;
579 int uiSearchBoxhHeight(void)
581 return SEARCH_ITEMS*MENU_BUTTON_HEIGHT + 2*MENU_TOP;
584 /* ar is the search box itself */
585 static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
587 uiSearchboxData *data= ar->regiondata;
592 if(data->items.totitem==0)
594 else if(data->active > data->items.totitem) {
595 if(data->items.more) {
596 data->items.offset++;
597 data->active= data->items.totitem;
598 ui_searchbox_update(C, ar, but, 0);
601 data->active= data->items.totitem;
603 else if(data->active < 1) {
604 if(data->items.offset) {
605 data->items.offset--;
607 ui_searchbox_update(C, ar, but, 0);
609 else if(data->active < 0)
613 ED_region_tag_redraw(ar);
616 static void ui_searchbox_butrect(rcti *rect, uiSearchboxData *data, int itemnr)
618 int buth= (data->bbox.ymax-data->bbox.ymin - 2*MENU_TOP)/SEARCH_ITEMS;
621 rect->xmin= data->bbox.xmin + 3.0f;
622 rect->xmax= data->bbox.xmax - 3.0f;
624 rect->ymax= data->bbox.ymax - MENU_TOP - itemnr*buth;
625 rect->ymin= rect->ymax - buth;
629 /* x and y in screencoords */
630 int ui_searchbox_inside(ARegion *ar, int x, int y)
632 uiSearchboxData *data= ar->regiondata;
634 return(BLI_in_rcti(&data->bbox, x-ar->winrct.xmin, y-ar->winrct.ymin));
637 /* string validated to be of correct length (but->hardmax) */
638 void ui_searchbox_apply(uiBut *but, ARegion *ar)
640 uiSearchboxData *data= ar->regiondata;
642 but->func_arg2= NULL;
645 char *name= data->items.names[data->active-1];
646 char *cpoin= strchr(name, '|');
648 if(cpoin) cpoin[0]= 0;
649 BLI_strncpy(but->editstr, name, data->items.maxstrlen);
650 if(cpoin) cpoin[0]= '|';
652 but->func_arg2= data->items.pointers[data->active-1];
656 void ui_searchbox_event(bContext *C, ARegion *ar, uiBut *but, wmEvent *event)
658 uiSearchboxData *data= ar->regiondata;
660 switch(event->type) {
663 ui_searchbox_select(C, ar, but, -1);
667 ui_searchbox_select(C, ar, but, 1);
670 if(BLI_in_rcti(&ar->winrct, event->x, event->y)) {
674 for(a=0; a<data->items.totitem; a++) {
675 ui_searchbox_butrect(&rect, data, a);
676 if(BLI_in_rcti(&rect, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
677 if( data->active!= a+1) {
679 ui_searchbox_select(C, ar, but, 0);
689 /* ar is the search box itself */
690 void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, int reset)
692 uiSearchboxData *data= ar->regiondata;
695 data->items.totitem= 0;
698 data->items.offset_i= data->items.offset;
701 data->items.offset_i= data->items.offset= 0;
705 if(but->search_func && but->func_arg2) {
706 data->items.active= but->func_arg2;
707 but->search_func(C, but->search_arg, but->editstr, &data->items);
708 data->items.active= NULL;
710 /* found active item, calculate real offset by centering it */
711 if(data->items.totitem) {
712 /* first case, begin of list */
713 if(data->items.offset_i < data->items.maxitem) {
714 data->active= data->items.offset_i+1;
715 data->items.offset_i= 0;
718 /* second case, end of list */
719 if(data->items.totitem - data->items.offset_i <= data->items.maxitem) {
720 data->active= 1 + data->items.offset_i - data->items.totitem + data->items.maxitem;
721 data->items.offset_i= data->items.totitem - data->items.maxitem;
724 /* center active item */
725 data->items.offset_i -= data->items.maxitem/2;
726 data->active= 1 + data->items.maxitem/2;
730 data->items.offset= data->items.offset_i;
731 data->items.totitem= 0;
737 but->search_func(C, but->search_arg, but->editstr, &data->items);
739 /* handle case where editstr is equal to one of items */
740 if(reset && data->active==0) {
743 for(a=0; a<data->items.totitem; a++) {
744 char *cpoin= strchr(data->items.names[a], '|');
746 if(cpoin) cpoin[0]= 0;
747 if(0==strcmp(but->editstr, data->items.names[a]))
749 if(cpoin) cpoin[0]= '|';
751 if(data->items.totitem==1)
755 /* validate selected item */
756 ui_searchbox_select(C, ar, but, 0);
758 ED_region_tag_redraw(ar);
761 void ui_searchbox_autocomplete(bContext *C, ARegion *ar, uiBut *but, char *str)
763 uiSearchboxData *data= ar->regiondata;
766 data->items.autocpl= autocomplete_begin(str, ui_get_but_string_max_length(but));
768 but->search_func(C, but->search_arg, but->editstr, &data->items);
770 autocomplete_end(data->items.autocpl, str);
771 data->items.autocpl= NULL;
775 static void ui_searchbox_region_draw(const bContext *C, ARegion *ar)
777 uiSearchboxData *data= ar->regiondata;
780 wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
783 ui_draw_search_back(U.uistyles.first, NULL, &data->bbox);
786 if(data->items.totitem) {
791 for(a=0; a<data->items.totitem; a++) {
792 ui_searchbox_butrect(&rect, data, a);
795 ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
799 if(data->items.more) {
800 ui_searchbox_butrect(&rect, data, data->items.maxitem-1);
802 UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymin-9, ICON_TRIA_DOWN);
805 if(data->items.offset) {
806 ui_searchbox_butrect(&rect, data, 0);
808 UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymax-7, ICON_TRIA_UP);
814 static void ui_searchbox_region_free(ARegion *ar)
816 uiSearchboxData *data= ar->regiondata;
819 /* free search data */
820 for(a=0; a<SEARCH_ITEMS; a++)
821 MEM_freeN(data->items.names[a]);
822 MEM_freeN(data->items.names);
823 MEM_freeN(data->items.pointers);
824 MEM_freeN(data->items.icons);
827 ar->regiondata= NULL;
830 ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
832 uiStyle *style= U.uistyles.first; // XXX pass on as arg
833 static ARegionType type;
835 uiSearchboxData *data;
836 float aspect= but->block->aspect;
837 float x1f, x2f, y1f, y2f;
838 int x1, x2, y1, y2, winx, winy, ofsx, ofsy;
840 /* create area region */
841 ar= ui_add_temporary_region(CTX_wm_screen(C));
843 memset(&type, 0, sizeof(ARegionType));
844 type.draw= ui_searchbox_region_draw;
845 type.free= ui_searchbox_region_free;
848 /* create searchbox data */
849 data= MEM_callocN(sizeof(uiSearchboxData), "uiSearchboxData");
851 /* set font, get bb */
852 data->fstyle= style->widget; /* copy struct */
853 data->fstyle.align= UI_STYLE_TEXT_CENTER;
854 ui_fontscale(&data->fstyle.points, aspect);
855 uiStyleFontSet(&data->fstyle);
857 ar->regiondata= data;
859 /* special case, hardcoded feature, not draw backdrop when called from menus,
860 assume for design that popup already added it */
861 if(but->block->flag & UI_BLOCK_LOOP)
864 /* compute position */
866 if(but->block->flag & UI_BLOCK_LOOP) {
867 /* this case is search menu inside other menu */
868 /* we copy region size */
870 ar->winrct= butregion->winrct;
872 /* widget rect, in region coords */
873 data->bbox.xmin= MENU_SHADOW_SIDE;
874 data->bbox.xmax= (ar->winrct.xmax-ar->winrct.xmin) - MENU_SHADOW_SIDE;
875 data->bbox.ymin= MENU_SHADOW_BOTTOM;
876 data->bbox.ymax= (ar->winrct.ymax-ar->winrct.ymin) - MENU_SHADOW_BOTTOM;
878 /* check if button is lower half */
879 if( but->y2 < (but->block->minx+but->block->maxx)/2 ) {
880 data->bbox.ymin += (but->y2-but->y1);
883 data->bbox.ymax -= (but->y2-but->y1);
887 x1f= but->x1 - 5; /* align text with button */
888 x2f= but->x2 + 5; /* symmetrical */
890 y1f= y2f - uiSearchBoxhHeight();
892 ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
893 ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
901 if(x2f - x1f < 150) x2f= x1f+150; // XXX arbitrary
903 /* copy to int, gets projected if possible too */
904 x1= x1f; y1= y1f; x2= x2f; y2= y2f;
907 if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
908 UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
909 UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
912 x1 += butregion->winrct.xmin;
913 x2 += butregion->winrct.xmin;
914 y1 += butregion->winrct.ymin;
915 y2 += butregion->winrct.ymin;
918 wm_window_get_size(CTX_wm_window(C), &winx, &winy);
936 /* widget rect, in region coords */
937 data->bbox.xmin= MENU_SHADOW_SIDE;
938 data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
939 data->bbox.ymin= MENU_SHADOW_BOTTOM;
940 data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
942 /* region bigger for shadow */
943 ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
944 ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
945 ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
950 ED_region_init(C, ar);
952 /* notify change and redraw */
953 ED_region_tag_redraw(ar);
955 /* prepare search data */
956 data->items.maxitem= SEARCH_ITEMS;
957 data->items.maxstrlen= but->hardmax;
958 data->items.totitem= 0;
959 data->items.names= MEM_callocN(SEARCH_ITEMS*sizeof(void *), "search names");
960 data->items.pointers= MEM_callocN(SEARCH_ITEMS*sizeof(void *), "search pointers");
961 data->items.icons= MEM_callocN(SEARCH_ITEMS*sizeof(int), "search icons");
962 for(x1=0; x1<SEARCH_ITEMS; x1++)
963 data->items.names[x1]= MEM_callocN(but->hardmax+1, "search pointers");
968 void ui_searchbox_free(bContext *C, ARegion *ar)
970 ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
974 /************************* Creating Menu Blocks **********************/
976 /* position block relative to but, result is in window space */
977 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
980 uiSafetyRct *saferct;
983 int xsize, ysize, xof=0, yof=0, center;
984 short dir1= 0, dir2=0;
986 /* transform to window coordinates, using the source button region/block */
987 butrct.xmin= but->x1; butrct.xmax= but->x2;
988 butrct.ymin= but->y1; butrct.ymax= but->y2;
990 ui_block_to_window_fl(butregion, but->block, &butrct.xmin, &butrct.ymin);
991 ui_block_to_window_fl(butregion, but->block, &butrct.xmax, &butrct.ymax);
993 /* calc block rect */
994 if(block->minx == 0.0f && block->maxx == 0.0f) {
995 if(block->buttons.first) {
996 block->minx= block->miny= 10000;
997 block->maxx= block->maxy= -10000;
999 bt= block->buttons.first;
1001 if(bt->x1 < block->minx) block->minx= bt->x1;
1002 if(bt->y1 < block->miny) block->miny= bt->y1;
1004 if(bt->x2 > block->maxx) block->maxx= bt->x2;
1005 if(bt->y2 > block->maxy) block->maxy= bt->y2;
1011 /* we're nice and allow empty blocks too */
1012 block->minx= block->miny= 0;
1013 block->maxx= block->maxy= 20;
1017 aspect= (float)(block->maxx - block->minx + 4);
1018 ui_block_to_window_fl(butregion, but->block, &block->minx, &block->miny);
1019 ui_block_to_window_fl(butregion, but->block, &block->maxx, &block->maxy);
1021 //block->minx-= 2.0; block->miny-= 2.0;
1022 //block->maxx+= 2.0; block->maxy+= 2.0;
1024 xsize= block->maxx - block->minx+4; // 4 for shadow
1025 ysize= block->maxy - block->miny+4;
1026 aspect/= (float)xsize;
1029 int left=0, right=0, top=0, down=0;
1032 wm_window_get_size(window, &winx, &winy);
1034 if(block->direction & UI_CENTER) center= ysize/2;
1037 if( butrct.xmin-xsize > 0.0) left= 1;
1038 if( butrct.xmax+xsize < winx) right= 1;
1039 if( butrct.ymin-ysize+center > 0.0) down= 1;
1040 if( butrct.ymax+ysize-center < winy) top= 1;
1042 dir1= block->direction & UI_DIRECTION;
1044 /* secundary directions */
1045 if(dir1 & (UI_TOP|UI_DOWN)) {
1046 if(dir1 & UI_LEFT) dir2= UI_LEFT;
1047 else if(dir1 & UI_RIGHT) dir2= UI_RIGHT;
1048 dir1 &= (UI_TOP|UI_DOWN);
1051 if(dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
1052 if(dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
1054 /* no space at all? dont change */
1056 if(dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
1057 if(dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
1058 /* this is aligning, not append! */
1059 if(dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
1060 if(dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
1063 if(dir1==UI_TOP && top==0) dir1= UI_DOWN;
1064 if(dir1==UI_DOWN && down==0) dir1= UI_TOP;
1065 if(dir2==UI_TOP && top==0) dir2= UI_DOWN;
1066 if(dir2==UI_DOWN && down==0) dir2= UI_TOP;
1070 xof= butrct.xmin - block->maxx;
1071 if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
1072 else yof= butrct.ymax - block->maxy+center;
1074 else if(dir1==UI_RIGHT) {
1075 xof= butrct.xmax - block->minx;
1076 if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
1077 else yof= butrct.ymax - block->maxy+center;
1079 else if(dir1==UI_TOP) {
1080 yof= butrct.ymax - block->miny;
1081 if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
1082 else xof= butrct.xmin - block->minx;
1083 // changed direction?
1084 if((dir1 & block->direction)==0) {
1085 if(block->direction & UI_SHIFT_FLIPPED)
1086 xof+= dir2==UI_LEFT?25:-25;
1087 uiBlockFlipOrder(block);
1090 else if(dir1==UI_DOWN) {
1091 yof= butrct.ymin - block->maxy;
1092 if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
1093 else xof= butrct.xmin - block->minx;
1094 // changed direction?
1095 if((dir1 & block->direction)==0) {
1096 if(block->direction & UI_SHIFT_FLIPPED)
1097 xof+= dir2==UI_LEFT?25:-25;
1098 uiBlockFlipOrder(block);
1102 /* and now we handle the exception; no space below or to top */
1103 if(top==0 && down==0) {
1104 if(dir1==UI_LEFT || dir1==UI_RIGHT) {
1105 // align with bottom of screen
1110 /* or no space left or right */
1111 if(left==0 && right==0) {
1112 if(dir1==UI_TOP || dir1==UI_DOWN) {
1113 // align with left size of screen
1114 xof= -block->minx+5;
1118 // apply requested offset in the block
1119 xof += block->xofs/block->aspect;
1120 yof += block->yofs/block->aspect;
1125 for(bt= block->buttons.first; bt; bt= bt->next) {
1126 ui_block_to_window_fl(butregion, but->block, &bt->x1, &bt->y1);
1127 ui_block_to_window_fl(butregion, but->block, &bt->x2, &bt->y2);
1135 // ui_check_but recalculates drawstring size in pixels
1144 /* safety calculus */
1146 float midx= (butrct.xmin+butrct.xmax)/2.0;
1147 float midy= (butrct.ymin+butrct.ymax)/2.0;
1149 /* when you are outside parent button, safety there should be smaller */
1151 // parent button to left
1152 if( midx < block->minx ) block->safety.xmin= block->minx-3;
1153 else block->safety.xmin= block->minx-40;
1154 // parent button to right
1155 if( midx > block->maxx ) block->safety.xmax= block->maxx+3;
1156 else block->safety.xmax= block->maxx+40;
1158 // parent button on bottom
1159 if( midy < block->miny ) block->safety.ymin= block->miny-3;
1160 else block->safety.ymin= block->miny-40;
1161 // parent button on top
1162 if( midy > block->maxy ) block->safety.ymax= block->maxy+3;
1163 else block->safety.ymax= block->maxy+40;
1165 // exception for switched pulldowns...
1166 if(dir1 && (dir1 & block->direction)==0) {
1167 if(dir2==UI_RIGHT) block->safety.xmax= block->maxx+3;
1168 if(dir2==UI_LEFT) block->safety.xmin= block->minx-3;
1170 block->direction= dir1;
1173 block->safety.xmin= block->minx-40;
1174 block->safety.ymin= block->miny-40;
1175 block->safety.xmax= block->maxx+40;
1176 block->safety.ymax= block->maxy+40;
1179 /* keep a list of these, needed for pulldown menus */
1180 saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1181 saferct->parent= butrct;
1182 saferct->safety= block->safety;
1183 BLI_freelistN(&block->saferct);
1185 BLI_duplicatelist(&block->saferct, &but->block->saferct);
1186 BLI_addhead(&block->saferct, saferct);
1189 static void ui_block_region_draw(const bContext *C, ARegion *ar)
1193 for(block=ar->uiblocks.first; block; block=block->next)
1194 uiDrawBlock(C, block);
1197 uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut *but, uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func, void *arg)
1199 wmWindow *window= CTX_wm_window(C);
1200 static ARegionType type;
1204 uiPopupBlockHandle *handle;
1205 uiSafetyRct *saferct;
1208 handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
1210 /* store context for operator */
1211 handle->ctx_area= CTX_wm_area(C);
1212 handle->ctx_region= CTX_wm_region(C);
1214 /* create area region */
1215 ar= ui_add_temporary_region(CTX_wm_screen(C));
1218 memset(&type, 0, sizeof(ARegionType));
1219 type.draw= ui_block_region_draw;
1222 UI_add_region_handlers(&ar->handlers);
1224 /* create ui block */
1226 block= create_func(C, handle->region, arg);
1228 block= handle_create_func(C, handle, arg);
1231 memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
1233 handle= block->handle;
1236 block->handle= handle;
1238 ar->regiondata= handle;
1240 if(!block->endblock)
1241 uiEndBlock(C, block);
1243 /* if this is being created from a button */
1245 if(ELEM(but->type, BLOCK, PULLDOWN))
1246 block->xofs = -2; /* for proper alignment */
1248 /* only used for automatic toolbox, so can set the shift flag */
1249 if(but->flag & UI_MAKE_TOP) {
1250 block->direction= UI_TOP|UI_SHIFT_FLIPPED;
1251 uiBlockFlipOrder(block);
1253 if(but->flag & UI_MAKE_DOWN) block->direction= UI_DOWN|UI_SHIFT_FLIPPED;
1254 if(but->flag & UI_MAKE_LEFT) block->direction |= UI_LEFT;
1255 if(but->flag & UI_MAKE_RIGHT) block->direction |= UI_RIGHT;
1257 ui_block_position(window, butregion, but, block);
1260 /* keep a list of these, needed for pulldown menus */
1261 saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1262 saferct->safety= block->safety;
1263 BLI_addhead(&block->saferct, saferct);
1264 block->flag |= UI_BLOCK_POPUP;
1267 /* the block and buttons were positioned in window space as in 2.4x, now
1268 * these menu blocks are regions so we bring it back to region space.
1269 * additionally we add some padding for the menu shadow or rounded menus */
1270 ar->winrct.xmin= block->minx - MENU_SHADOW_SIDE;
1271 ar->winrct.xmax= block->maxx + MENU_SHADOW_SIDE;
1272 ar->winrct.ymin= block->miny - MENU_SHADOW_BOTTOM;
1273 ar->winrct.ymax= block->maxy + MENU_TOP;
1275 block->minx -= ar->winrct.xmin;
1276 block->maxx -= ar->winrct.xmin;
1277 block->miny -= ar->winrct.ymin;
1278 block->maxy -= ar->winrct.ymin;
1280 for(bt= block->buttons.first; bt; bt= bt->next) {
1281 bt->x1 -= ar->winrct.xmin;
1282 bt->x2 -= ar->winrct.xmin;
1283 bt->y1 -= ar->winrct.ymin;
1284 bt->y2 -= ar->winrct.ymin;
1287 block->flag |= UI_BLOCK_LOOP|UI_BLOCK_MOVEMOUSE_QUIT;
1289 /* adds subwindow */
1290 ED_region_init(C, ar);
1292 /* get winmat now that we actually have the subwindow */
1293 wmSubWindowSet(window, ar->swinid);
1295 wm_subwindow_getmatrix(window, ar->swinid, block->winmat);
1297 /* notify change and redraw */
1298 ED_region_tag_redraw(ar);
1303 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
1305 /* XXX ton added, chrash on load file with popup open... need investigate */
1306 if(CTX_wm_screen(C))
1307 ui_remove_temporary_region(C, CTX_wm_screen(C), handle->region);
1311 /***************************** Menu Button ***************************/
1313 uiBlock *ui_block_func_MENU(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
1315 uiBut *but= arg_but;
1321 int width, height, boxh, columns, rows, startx, starty, x1, y1, xmax, a;
1323 /* create the block */
1324 block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
1325 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1327 /* compute menu data */
1328 md= decompose_menu_string(but->str);
1330 /* columns and row calculation */
1331 columns= (md->nitems+20)/20;
1335 columns= (md->nitems+25)/25;
1337 rows= md->nitems/columns;
1340 while(rows*columns<md->nitems)
1343 /* prevent scaling up of pupmenu */
1344 aspect= but->block->aspect;
1348 /* size and location */
1350 width= 1.5*aspect*strlen(md->title)+UI_GetStringWidth(md->title);
1354 for(a=0; a<md->nitems; a++) {
1355 xmax= aspect*UI_GetStringWidth(md->items[a].str);
1356 if(md->items[a].icon)
1363 if(width < (but->x2 - but->x1))
1364 width = (but->x2 - but->x1);
1368 boxh= MENU_BUTTON_HEIGHT;
1381 if (md->titleicon) {
1382 bt= uiDefIconTextBut(block, LABEL, 0, md->titleicon, md->title, startx, (short)(starty+rows*boxh), (short)width, (short)boxh, NULL, 0.0, 0.0, 0, 0, "");
1384 bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+rows*boxh), (short)width, (short)boxh, NULL, 0.0, 0.0, 0, 0, "");
1385 bt->flag= UI_TEXT_LEFT;
1389 for(a=0; a<md->nitems; a++) {
1391 x1= startx + width*((int)(md->nitems-a-1)/rows);
1392 y1= starty - boxh*(rows - ((md->nitems - a - 1)%rows)) + (rows*boxh);
1394 if (strcmp(md->items[md->nitems-a-1].str, "%l")==0) {
1395 bt= uiDefBut(block, SEPR, B_NOP, "", x1, y1,(short)(width-(rows>1)), (short)(boxh-1), NULL, 0.0, 0.0, 0, 0, "");
1397 else if(md->items[md->nitems-a-1].icon) {
1398 bt= uiDefIconTextButF(block, BUTM|FLO, B_NOP, md->items[md->nitems-a-1].icon ,md->items[md->nitems-a-1].str, x1, y1,(short)(width-(rows>1)), (short)(boxh-1), &handle->retvalue, (float) md->items[md->nitems-a-1].retval, 0.0, 0, 0, "");
1401 bt= uiDefButF(block, BUTM|FLO, B_NOP, md->items[md->nitems-a-1].str, x1, y1,(short)(width-(rows>1)), (short)(boxh-1), &handle->retvalue, (float) md->items[md->nitems-a-1].retval, 0.0, 0, 0, "");
1407 /* the code up here has flipped locations, because of change of preferred order */
1408 /* thats why we have to switch list order too, to make arrowkeys work */
1410 lb.first= lb.last= NULL;
1411 bt= block->buttons.first;
1413 uiBut *next= bt->next;
1414 BLI_remlink(&block->buttons, bt);
1415 BLI_addhead(&lb, bt);
1420 block->direction= UI_TOP;
1421 uiEndBlock(C, block);
1426 uiBlock *ui_block_func_ICONROW(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
1428 uiBut *but= arg_but;
1432 block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
1433 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1435 for(a=(int)but->hardmin; a<=(int)but->hardmax; a++) {
1436 uiDefIconButF(block, BUTM|FLO, B_NOP, but->icon+(a-but->hardmin), 0, (short)(18*a), (short)(but->x2-but->x1-4), 18, &handle->retvalue, (float)a, 0.0, 0, 0, "");
1439 block->direction= UI_TOP;
1441 uiEndBlock(C, block);
1446 uiBlock *ui_block_func_ICONTEXTROW(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
1448 uiBut *but= arg_but;
1451 int width, xmax, ypos, a;
1453 block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
1454 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1456 md= decompose_menu_string(but->str);
1458 /* size and location */
1459 /* expand menu width to fit labels */
1461 width= 2*strlen(md->title)+UI_GetStringWidth(md->title);
1465 for(a=0; a<md->nitems; a++) {
1466 xmax= UI_GetStringWidth(md->items[a].str);
1467 if(xmax>width) width= xmax;
1471 if (width<50) width=50;
1475 /* loop through the menu options and draw them out with icons & text labels */
1476 for(a=0; a<md->nitems; a++) {
1478 /* add a space if there's a separator (%l) */
1479 if (strcmp(md->items[a].str, "%l")==0) {
1483 uiDefIconTextButF(block, BUTM|FLO, B_NOP, (short)((but->icon)+(md->items[a].retval-but->hardmin)), md->items[a].str, 0, ypos,(short)width, 19, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
1491 bt= uiDefBut(block, LABEL, 0, md->title, 0, ypos, (short)width, 19, NULL, 0.0, 0.0, 0, 0, "");
1492 bt->flag= UI_TEXT_LEFT;
1497 block->direction= UI_TOP;
1499 uiBoundsBlock(block, 3);
1500 uiEndBlock(C, block);
1505 static void ui_warp_pointer(short x, short y)
1507 /* XXX 2.50 which function to use for this? */
1509 /* OSX has very poor mousewarp support, it sends events;
1510 this causes a menu being pressed immediately ... */
1517 /********************* Color Button ****************/
1519 /* picker sizes S hsize, F full size, D spacer, B button/pallette height */
1525 #define UI_PALETTE_TOT 16
1526 /* note; in tot+1 the old color is stored */
1527 static float palette[UI_PALETTE_TOT+1][3]= {
1528 {0.93, 0.83, 0.81}, {0.88, 0.89, 0.73}, {0.69, 0.81, 0.57}, {0.51, 0.76, 0.64},
1529 {0.37, 0.56, 0.61}, {0.33, 0.29, 0.55}, {0.46, 0.21, 0.51}, {0.40, 0.12, 0.18},
1530 {1.0, 1.0, 1.0}, {0.85, 0.85, 0.85}, {0.7, 0.7, 0.7}, {0.56, 0.56, 0.56},
1531 {0.42, 0.42, 0.42}, {0.28, 0.28, 0.28}, {0.14, 0.14, 0.14}, {0.0, 0.0, 0.0}
1534 /* for picker, while editing hsv */
1535 void ui_set_but_hsv(uiBut *but)
1539 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
1540 ui_set_but_vectorf(but, col);
1543 static void update_picker_hex(uiBlock *block, float *rgb)
1548 sprintf(col, "%02X%02X%02X", (unsigned int)(rgb[0]*255.0), (unsigned int)(rgb[1]*255.0), (unsigned int)(rgb[2]*255.0));
1550 // this updates button strings, is hackish... but button pointers are on stack of caller function
1552 for(bt= block->buttons.first; bt; bt= bt->next) {
1553 if(strcmp(bt->str, "Hex: ")==0)
1554 strcpy(bt->poin, col);
1560 /* also used by small picker, be careful with name checks below... */
1561 void ui_update_block_buts_hsv(uiBlock *block, float *hsv)
1567 // this updates button strings, is hackish... but button pointers are on stack of caller function
1568 hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r, &g, &b);
1570 rgb[0] = r; rgb[1] = g; rgb[2] = b;
1571 update_picker_hex(block, rgb);
1573 for(bt= block->buttons.first; bt; bt= bt->next) {
1574 if(ELEM(bt->type, HSVCUBE, HSVCIRCLE)) {
1575 VECCOPY(bt->hsv, hsv);
1578 else if(bt->str[1]==' ') {
1579 if(bt->str[0]=='R') {
1580 ui_set_but_val(bt, r);
1582 else if(bt->str[0]=='G') {
1583 ui_set_but_val(bt, g);
1585 else if(bt->str[0]=='B') {
1586 ui_set_but_val(bt, b);
1588 else if(bt->str[0]=='H') {
1589 ui_set_but_val(bt, hsv[0]);
1591 else if(bt->str[0]=='S') {
1592 ui_set_but_val(bt, hsv[1]);
1594 else if(bt->str[0]=='V') {
1595 ui_set_but_val(bt, hsv[2]);
1603 static void ui_update_block_buts_hex(uiBlock *block, char *hexcol)
1606 float r=0, g=0, b=0;
1610 // this updates button strings, is hackish... but button pointers are on stack of caller function
1611 hex_to_rgb(hexcol, &r, &g, &b);
1612 rgb_to_hsv(r, g, b, &h, &s, &v);
1614 for(bt= block->buttons.first; bt; bt= bt->next) {
1615 if(bt->type==HSVCUBE) {
1621 else if(bt->str[1]==' ') {
1622 if(bt->str[0]=='R') {
1623 ui_set_but_val(bt, r);
1625 else if(bt->str[0]=='G') {
1626 ui_set_but_val(bt, g);
1628 else if(bt->str[0]=='B') {
1629 ui_set_but_val(bt, b);
1631 else if(bt->str[0]=='H') {
1632 ui_set_but_val(bt, h);
1634 else if(bt->str[0]=='S') {
1635 ui_set_but_val(bt, s);
1637 else if(bt->str[0]=='V') {
1638 ui_set_but_val(bt, v);
1646 /* bt1 is palette but, col1 is original color */
1647 /* callback to copy from/to palette */
1648 static void do_palette_cb(bContext *C, void *bt1, void *col1)
1650 wmWindow *win= CTX_wm_window(C);
1651 uiBut *but1= (uiBut *)bt1;
1652 uiPopupBlockHandle *popup= but1->block->handle;
1653 float *col= (float *)col1;
1656 fp= (float *)but1->poin;
1658 if(win->eventstate->ctrl) {
1665 rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1666 ui_update_block_buts_hsv(but1->block, hsv);
1667 update_picker_hex(but1->block, col);
1670 popup->menuretval= UI_RETURN_UPDATE;
1673 static void do_hsv_cb(bContext *C, void *bt1, void *unused)
1675 uiBut *but1= (uiBut *)bt1;
1676 uiPopupBlockHandle *popup= but1->block->handle;
1679 popup->menuretval= UI_RETURN_UPDATE;
1682 /* bt1 is num but, hsv1 is pointer to original color in hsv space*/
1683 /* callback to handle changes in num-buts in picker */
1684 static void do_palette1_cb(bContext *C, void *bt1, void *hsv1)
1686 uiBut *but1= (uiBut *)bt1;
1687 uiPopupBlockHandle *popup= but1->block->handle;
1688 float *hsv= (float *)hsv1;
1691 if(but1->str[1]==' ') {
1692 if(but1->str[0]=='R') fp= (float *)but1->poin;
1693 else if(but1->str[0]=='G') fp= ((float *)but1->poin)-1;
1694 else if(but1->str[0]=='B') fp= ((float *)but1->poin)-2;
1697 rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
1699 ui_update_block_buts_hsv(but1->block, hsv);
1702 popup->menuretval= UI_RETURN_UPDATE;
1705 /* bt1 is num but, col1 is pointer to original color */
1706 /* callback to handle changes in num-buts in picker */
1707 static void do_palette2_cb(bContext *C, void *bt1, void *col1)
1709 uiBut *but1= (uiBut *)bt1;
1710 uiPopupBlockHandle *popup= but1->block->handle;
1711 float *rgb= (float *)col1;
1714 if(but1->str[1]==' ') {
1715 if(but1->str[0]=='H') fp= (float *)but1->poin;
1716 else if(but1->str[0]=='S') fp= ((float *)but1->poin)-1;
1717 else if(but1->str[0]=='V') fp= ((float *)but1->poin)-2;
1720 hsv_to_rgb(fp[0], fp[1], fp[2], rgb, rgb+1, rgb+2);
1722 ui_update_block_buts_hsv(but1->block, fp);
1725 popup->menuretval= UI_RETURN_UPDATE;
1728 static void do_palette_hex_cb(bContext *C, void *bt1, void *hexcl)
1730 uiBut *but1= (uiBut *)bt1;
1731 uiPopupBlockHandle *popup= but1->block->handle;
1732 char *hexcol= (char *)hexcl;
1734 ui_update_block_buts_hex(but1->block, hexcol);
1737 popup->menuretval= UI_RETURN_UPDATE;
1740 /* used for both 3d view and image window */
1741 static void do_palette_sample_cb(bContext *C, void *bt1, void *col1) /* frontbuf */
1743 /* XXX 2.50 this should become an operator? */
1745 uiBut *but1= (uiBut *)bt1;
1756 oldcursor=get_cursor();
1757 win=winlay_get_active_window();
1759 while (get_mbut() & L_MOUSE) UI_wait_for_statechange();
1761 SetBlenderCursor(BC_EYEDROPPER_CURSOR);
1763 /* loop and wait for a mouse click */
1769 dev = extern_qread_ext(&val, &ascii);
1771 if(dev==INPUTCHANGE) break;
1772 if(get_mbut() & R_MOUSE) break;
1773 else if(get_mbut() & L_MOUSE) {
1774 uiGetMouse(mywinget(), mval);
1775 x= mval[0]; y= mval[1];
1780 else if(dev==ESCKEY) break;
1782 window_set_cursor(win, oldcursor);
1784 if(capturing) return;
1786 if(x<0 || y<0) return;
1788 /* if we've got a glick, use OpenGL to sample the color under the mouse pointer */
1789 glReadBuffer(GL_FRONT);
1790 glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, tempcol);
1791 glReadBuffer(GL_BACK);
1793 /* and send that color back to the picker */
1794 rgb_to_hsv(tempcol[0], tempcol[1], tempcol[2], hsv, hsv+1, hsv+2);
1795 ui_update_block_buts_hsv(but1->block, hsv);
1796 update_picker_hex(but1->block, tempcol);
1798 for (but= but1->block->buttons.first; but; but= but->next) {
1803 but= but1->block->buttons.first;
1804 ui_block_flush_back(but->block);
1808 /* color picker, Gimp version. mode: 'f' = floating panel, 'p' = popup */
1809 /* col = read/write to, hsv/old/hexcol = memory for temporal use */
1810 void uiBlockPickerButtons(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1816 VECCOPY(old, col); // old color stored there, for palette_cb to work
1818 // the cube intersection
1819 bt= uiDefButF(block, HSVCUBE, retval, "", 0,DPICK+BPICK,FPICK,FPICK, col, 0.0, 0.0, 2, 0, "");
1820 uiButSetFunc(bt, do_hsv_cb, bt, NULL);
1822 bt= uiDefButF(block, HSVCUBE, retval, "", 0,0,FPICK,BPICK, col, 0.0, 0.0, 3, 0, "");
1823 uiButSetFunc(bt, do_hsv_cb, bt, NULL);
1827 bt=uiDefButF(block, COL, retval, "", FPICK+DPICK, 0, BPICK,BPICK, old, 0.0, 0.0, -1, 0, "Old color, click to restore");
1828 uiButSetFunc(bt, do_palette_cb, bt, col);
1829 uiDefButF(block, COL, retval, "", FPICK+DPICK, BPICK+DPICK, BPICK,60-BPICK-DPICK, col, 0.0, 0.0, -1, 0, "Active color");
1831 h= (DPICK+BPICK+FPICK-64)/(UI_PALETTE_TOT/2.0);
1832 uiBlockBeginAlign(block);
1833 for(a= -1+UI_PALETTE_TOT/2; a>=0; a--) {
1834 bt= uiDefButF(block, COL, retval, "", FPICK+DPICK, 65.0+(float)a*h, BPICK/2, h, palette[a+UI_PALETTE_TOT/2], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");
1835 uiButSetFunc(bt, do_palette_cb, bt, col);
1836 bt= uiDefButF(block, COL, retval, "", FPICK+DPICK+BPICK/2, 65.0+(float)a*h, BPICK/2, h, palette[a], 0.0, 0.0, -1, 0, "Click to choose, hold CTRL to store in palette");
1837 uiButSetFunc(bt, do_palette_cb, bt, col);
1839 uiBlockEndAlign(block);
1842 rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1843 sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));
1845 offs= FPICK+2*DPICK+BPICK;
1847 /* note; made this a TOG now, with NULL pointer. Is because BUT now gets handled with a afterfunc */
1848 bt= uiDefIconTextBut(block, TOG, UI_RETURN_OK, ICON_EYEDROPPER, "Sample", offs+55, 170, 85, 20, NULL, 0, 0, 0, 0, "Sample the color underneath the following mouse click (ESC or RMB to cancel)");
1849 uiButSetFunc(bt, do_palette_sample_cb, bt, col);
1850 uiButSetFlag(bt, UI_TEXT_LEFT);
1852 bt= uiDefBut(block, TEX, retval, "Hex: ", offs, 140, 140, 20, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1853 uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
1855 uiBlockBeginAlign(block);
1856 bt= uiDefButF(block, NUMSLI, retval, "R ", offs, 110, 140,20, col, 0.0, 1.0, 10, 3, "");
1857 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1858 bt= uiDefButF(block, NUMSLI, retval, "G ", offs, 90, 140,20, col+1, 0.0, 1.0, 10, 3, "");
1859 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1860 bt= uiDefButF(block, NUMSLI, retval, "B ", offs, 70, 140,20, col+2, 0.0, 1.0, 10, 3, "");
1861 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1863 uiBlockBeginAlign(block);
1864 bt= uiDefButF(block, NUMSLI, retval, "H ", offs, 40, 140,20, hsv, 0.0, 1.0, 10, 3, "");
1865 uiButSetFunc(bt, do_palette2_cb, bt, col);
1866 bt= uiDefButF(block, NUMSLI, retval, "S ", offs, 20, 140,20, hsv+1, 0.0, 1.0, 10, 3, "");
1867 uiButSetFunc(bt, do_palette2_cb, bt, col);
1868 bt= uiDefButF(block, NUMSLI, retval, "V ", offs, 0, 140,20, hsv+2, 0.0, 1.0, 10, 3, "");
1869 uiButSetFunc(bt, do_palette2_cb, bt, col);
1870 uiBlockEndAlign(block);
1873 /* bt1 is num but, hsv1 is pointer to original color in hsv space*/
1874 /* callback to handle changes */
1875 static void do_picker_small_cb(bContext *C, void *bt1, void *hsv1)
1877 uiBut *but1= (uiBut *)bt1;
1878 uiPopupBlockHandle *popup= but1->block->handle;
1879 float *hsv= (float *)hsv1;
1882 fp= (float *)but1->poin;
1883 rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
1885 ui_update_block_buts_hsv(but1->block, hsv);
1888 popup->menuretval= UI_RETURN_UPDATE;
1891 /* picker sizes S hsize, F full size, D spacer, B button/pallette height */
1892 #define SPICK1 150.0
1895 /* only the color, a HS circle and V slider */
1896 static void uiBlockPickerSmall(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1900 VECCOPY(old, col); // old color stored there, for palette_cb to work
1903 bt= uiDefButF(block, HSVCIRCLE, retval, "", 0, 0,SPICK1,SPICK1, col, 0.0, 0.0, 0, 0, "");
1904 uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1907 bt= uiDefButF(block, HSVCUBE, retval, "", SPICK1+DPICK1,0,14,SPICK1, col, 0.0, 0.0, 4, 0, "");
1908 uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1912 static void picker_new_hide_reveal(uiBlock *block, short colormode)
1917 for(bt= block->buttons.first; bt; bt= bt->next) {
1919 if(bt->type==NUMSLI || bt->type==TEX) {
1920 if( bt->str[1]=='e') {
1921 if(colormode==2) bt->flag &= ~UI_HIDDEN;
1922 else bt->flag |= UI_HIDDEN;
1924 else if( ELEM3(bt->str[0], 'R', 'G', 'B')) {
1925 if(colormode==0) bt->flag &= ~UI_HIDDEN;
1926 else bt->flag |= UI_HIDDEN;
1928 else if( ELEM3(bt->str[0], 'H', 'S', 'V')) {
1929 if(colormode==1) bt->flag &= ~UI_HIDDEN;
1930 else bt->flag |= UI_HIDDEN;
1936 static void do_picker_new_mode_cb(bContext *C, void *bt1, void *colv)
1939 short colormode= ui_get_but_val(bt);
1941 picker_new_hide_reveal(bt->block, colormode);
1945 /* a HS circle, V slider, rgb/hsv/hex sliders */
1946 static void uiBlockPickerNew(uiBlock *block, float *col, float *hsv, float *old, char *hexcol, char mode, short retval)
1948 static short colormode= 0; /* temp? 0=rgb, 1=hsv, 2=hex */
1952 VECCOPY(old, col); // old color stored there, for palette_cb to work
1955 bt= uiDefButF(block, HSVCIRCLE, retval, "", 0, 0,SPICK1,SPICK1, col, 0.0, 0.0, 0, 0, "");
1956 uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1959 bt= uiDefButF(block, HSVCUBE, retval, "", SPICK1+DPICK1,0,14,SPICK1, col, 0.0, 0.0, 4, 0, "");
1960 uiButSetFunc(bt, do_picker_small_cb, bt, hsv);
1963 width= (SPICK1+DPICK1+14)/3;
1964 uiBlockBeginAlign(block);
1965 bt= uiDefButS(block, ROW, retval, "RGB", 0, -30, width, 19, &colormode, 0.0, 0.0, 0, 0, "");
1966 uiButSetFunc(bt, do_picker_new_mode_cb, bt, col);
1967 bt= uiDefButS(block, ROW, retval, "HSV", width, -30, width, 19, &colormode, 0.0, 1.0, 0, 0, "");
1968 uiButSetFunc(bt, do_picker_new_mode_cb, bt, hsv);
1969 bt= uiDefButS(block, ROW, retval, "Hex", 2*width, -30, width, 19, &colormode, 0.0, 2.0, 0, 0, "");
1970 uiButSetFunc(bt, do_picker_new_mode_cb, bt, hexcol);
1971 uiBlockEndAlign(block);
1973 /* sliders or hex */
1974 width= (SPICK1+DPICK1+14);
1975 rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
1976 sprintf(hexcol, "%02X%02X%02X", (unsigned int)(col[0]*255.0), (unsigned int)(col[1]*255.0), (unsigned int)(col[2]*255.0));
1978 uiBlockBeginAlign(block);
1979 bt= uiDefButF(block, NUMSLI, 0, "R ", 0, -60, width, 19, col, 0.0, 1.0, 10, 3, "");
1980 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1981 bt= uiDefButF(block, NUMSLI, 0, "G ", 0, -80, width, 19, col+1, 0.0, 1.0, 10, 3, "");
1982 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1983 bt= uiDefButF(block, NUMSLI, 0, "B ", 0, -100, width, 19, col+2, 0.0, 1.0, 10, 3, "");
1984 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
1985 uiBlockEndAlign(block);
1987 uiBlockBeginAlign(block);
1988 bt= uiDefButF(block, NUMSLI, 0, "H ", 0, -60, width, 19, hsv, 0.0, 1.0, 10, 3, "");
1989 uiButSetFunc(bt, do_palette2_cb, bt, col);
1990 bt= uiDefButF(block, NUMSLI, 0, "S ", 0, -80, width, 19, hsv+1, 0.0, 1.0, 10, 3, "");
1991 uiButSetFunc(bt, do_palette2_cb, bt, col);
1992 bt= uiDefButF(block, NUMSLI, 0, "V ", 0, -100, width, 19, hsv+2, 0.0, 1.0, 10, 3, "");
1993 uiButSetFunc(bt, do_palette2_cb, bt, col);
1994 uiBlockEndAlign(block);
1996 bt= uiDefBut(block, TEX, 0, "Hex: ", 0, -80, width, 19, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1997 uiButSetFunc(bt, do_palette_hex_cb, bt, hexcol);
1999 picker_new_hide_reveal(block, colormode);
2003 static int ui_picker_small_wheel(const bContext *C, uiBlock *block, wmEvent *event)
2007 if(event->type==WHEELUPMOUSE)
2009 else if(event->type==WHEELDOWNMOUSE)
2015 for(but= block->buttons.first; but; but= but->next) {
2016 if(but->type==HSVCUBE && but->active==NULL) {
2017 uiPopupBlockHandle *popup= block->handle;
2020 ui_get_but_vectorf(but, col);
2022 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
2023 but->hsv[2]= CLAMPIS(but->hsv[2]+add, 0.0f, 1.0f);
2024 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
2026 ui_set_but_vectorf(but, col);
2028 ui_update_block_buts_hsv(block, but->hsv);
2030 popup->menuretval= UI_RETURN_UPDATE;
2039 uiBlock *ui_block_func_COL(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
2041 wmWindow *win= CTX_wm_window(C); // XXX temp, needs to become keymap to detect type?
2042 uiBut *but= arg_but;
2044 static float hsvcol[3], oldcol[3];
2045 static char hexcol[128];
2047 block= uiBeginBlock(C, handle->region, "colorpicker", UI_EMBOSS);
2049 VECCOPY(handle->retvec, but->editvec);
2050 if(win->eventstate->shift) {
2051 uiBlockPickerButtons(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2052 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
2053 uiBoundsBlock(block, 3);
2055 else if(win->eventstate->alt) {
2056 uiBlockPickerSmall(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2057 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_OUT_1;
2058 uiBoundsBlock(block, 10);
2060 block->block_event_func= ui_picker_small_wheel;
2063 uiBlockPickerNew(block, handle->retvec, hsvcol, oldcol, hexcol, 'p', 0);
2064 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
2065 uiBoundsBlock(block, 10);
2067 block->block_event_func= ui_picker_small_wheel;
2072 block->direction= UI_TOP;
2077 /* ******************** Color band *************** */
2079 static int vergcband(const void *a1, const void *a2)
2081 const CBData *x1=a1, *x2=a2;
2083 if( x1->pos > x2->pos ) return 1;
2084 else if( x1->pos < x2->pos) return -1;
2088 static void colorband_pos_cb(bContext *C, void *coba_v, void *unused_v)
2090 ColorBand *coba= coba_v;
2093 if(coba->tot<2) return;
2095 for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2096 qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
2097 for(a=0; a<coba->tot; a++) {
2098 if(coba->data[a].cur==coba->cur) {
2099 /* if(coba->cur!=a) addqueue(curarea->win, REDRAW, 0); */ /* button cur */
2106 static void colorband_add_cb(bContext *C, void *coba_v, void *unused_v)
2108 ColorBand *coba= coba_v;
2110 if(coba->tot < MAXCOLORBAND-1) coba->tot++;
2111 coba->cur= coba->tot-1;
2113 colorband_pos_cb(C, coba, NULL);
2114 // BIF_undo_push("Add colorband");
2118 static void colorband_del_cb(bContext *C, void *coba_v, void *unused_v)
2120 ColorBand *coba= coba_v;
2123 if(coba->tot<2) return;
2125 for(a=coba->cur; a<coba->tot; a++) {
2126 coba->data[a]= coba->data[a+1];
2128 if(coba->cur) coba->cur--;
2131 // BIF_undo_push("Delete colorband");
2132 // BIF_preview_changed(ID_TE);
2135 void uiBlockColorbandButtons(uiBlock *block, ColorBand *coba, rctf *butr, int event)
2139 float unit= (butr->xmax-butr->xmin)/14.0f;
2140 float xs= butr->xmin;
2142 cbd= coba->data + coba->cur;
2144 uiBlockBeginAlign(block);
2145 uiDefButF(block, COL, event, "", xs,butr->ymin+20.0f,2.0f*unit,20, &(cbd->r), 0, 0, 0, 0, "");
2146 uiDefButF(block, NUM, event, "A:", xs+2.0f*unit,butr->ymin+20.0f,4.0f*unit,20, &(cbd->a), 0.0f, 1.0f, 10, 2, "");
2147 bt= uiDefBut(block, BUT, event, "Add", xs+6.0f*unit,butr->ymin+20.0f,2.0f*unit,20, NULL, 0, 0, 0, 0, "Adds a new color position to the colorband");
2148 uiButSetFunc(bt, colorband_add_cb, coba, NULL);
2149 bt= uiDefBut(block, BUT, event, "Del", xs+8.0f*unit, butr->ymin+20.0f, 2.0f*unit, 20, NULL, 0, 0, 0, 0, "Deletes the active position");
2150 uiButSetFunc(bt, colorband_del_cb, coba, NULL);
2152 uiDefButS(block, MENU, event, "Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4",
2153 xs + 10.0f*unit, butr->ymin+20.0f, unit*4.0f, 20, &coba->ipotype, 0.0, 0.0, 0, 0, "Sets interpolation type");
2155 uiDefBut(block, BUT_COLORBAND, event, "", xs, butr->ymin, butr->xmax-butr->xmin, 20.0f, coba, 0, 0, 0, 0, "");
2156 uiBlockEndAlign(block);
2161 /* ******************** PUPmenu ****************** */
2163 static int pupmenu_set= 0;
2165 void uiPupMenuSetActive(int val)
2170 /* value== -1 read, otherwise set */
2171 static int pupmenu_memory(char *str, int value)
2173 static char mem[256], first=1;
2177 memset(mem, 0, 256);
2185 if(value >= 0) mem[ val & 255 ]= value;
2186 else return mem[ val & 255 ];
2191 #define PUP_LABELH 6
2193 typedef struct uiPupMenuInfo {
2200 uiBlock *ui_block_func_PUPMENU(bContext *C, uiPopupBlockHandle *handle, void *arg_info)
2203 uiPupMenuInfo *info;
2204 int columns, rows, mousemove[2]= {0, 0}, mousewarp= 0;
2205 int width, height, xmax, ymax, maxrow;
2206 int a, startx, starty, endx, endy, x1, y1;
2211 maxrow= info->maxrow;
2214 /* block stuff first, need to know the font */
2215 block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
2216 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_NUMSELECT);
2217 block->direction= UI_DOWN;
2219 md= decompose_menu_string(info->instr);
2227 /* size and location, title slightly bigger for bold */
2229 width= 2*strlen(md->title)+UI_GetStringWidth(md->title);
2234 for(a=0; a<md->nitems; a++) {
2235 xmax= UI_GetStringWidth(md->items[a].str);
2236 if(xmax>width) width= xmax;
2238 if(strcmp(md->items[a].str, "%l")==0) height+= PUP_LABELH;
2239 else height+= MENU_BUTTON_HEIGHT;
2243 if (width<50) width=50;
2245 wm_window_get_size(CTX_wm_window(C), &xmax, &ymax);
2247 /* set first item */
2250 lastselected= pupmenu_set-1;
2253 else if(md->nitems>1) {
2254 lastselected= pupmenu_memory(info->instr, -1);
2257 startx= info->mx-(0.8*(width));
2258 starty= info->my-height+MENU_BUTTON_HEIGHT/2;
2259 if(lastselected>=0 && lastselected<md->nitems) {
2260 for(a=0; a<md->nitems; a++) {
2261 if(a==lastselected) break;
2262 if( strcmp(md->items[a].str, "%l")==0) starty+= PUP_LABELH;
2263 else starty+=MENU_BUTTON_HEIGHT;
2266 //starty= info->my-height+MENU_BUTTON_HEIGHT/2+lastselected*MENU_BUTTON_HEIGHT;
2273 mousemove[1]= 10-starty;
2277 endx= startx+width*columns;
2278 endy= starty+height;
2282 startx= endx-width*columns;
2285 mousemove[1]= ymax-endy-20;
2287 starty= endy-height;
2290 if(mousemove[0] || mousemove[1]) {
2291 ui_warp_pointer(info->mx+mousemove[0], info->my+mousemove[1]);
2292 mousemove[0]= info->mx;
2293 mousemove[1]= info->my;
2304 sprintf(titlestr, " %s", md->title);
2305 uiDefIconTextBut(block, LABEL, 0, md->titleicon, titlestr, startx, (short)(starty+height), width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2308 bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+height), columns*width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2309 bt->flag= UI_TEXT_LEFT;
2312 //uiDefBut(block, SEPR, 0, "", startx, (short)(starty+height)-MENU_SEPR_HEIGHT, width, MENU_SEPR_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2315 x1= startx + width*((int)a/rows);
2316 y1= starty + height - MENU_BUTTON_HEIGHT; // - MENU_SEPR_HEIGHT;
2318 for(a=0; a<md->nitems; a++) {
2319 char *name= md->items[a].str;
2320 int icon = md->items[a].icon;
2322 if(strcmp(name, "%l")==0) {
2323 uiDefBut(block, SEPR, B_NOP, "", x1, y1, width, PUP_LABELH, NULL, 0, 0.0, 0, 0, "");
2327 uiDefIconButF(block, BUTM, B_NOP, icon, x1, y1, width+16, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
2328 y1 -= MENU_BUTTON_HEIGHT;
2331 uiDefButF(block, BUTM, B_NOP, name, x1, y1, width, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
2332 y1 -= MENU_BUTTON_HEIGHT;
2336 uiBoundsBlock(block, 1);
2337 uiEndBlock(C, block);
2341 /* XXX 2.5 need to store last selected */
2343 /* calculate last selected */
2344 if(event & ui_return_ok) {
2346 for(a=0; a<md->nitems; a++) {
2347 if(val==md->items[a].retval) lastselected= a;
2350 pupmenu_memory(info->instr, lastselected);
2354 /* XXX 2.5 need to warp back */
2356 if(mousemove[1] && (event & ui_return_out)==0)
2357 ui_warp_pointer(mousemove[0], mousemove[1]);
2364 uiBlock *ui_block_func_PUPMENUCOL(bContext *C, uiPopupBlockHandle *handle, void *arg_info)
2367 uiPupMenuInfo *info;
2368 int columns, rows, mousemove[2]= {0, 0}, mousewarp;
2369 int width, height, xmax, ymax, maxrow;
2370 int a, startx, starty, endx, endy, x1, y1;
2375 maxrow= info->maxrow;
2378 /* block stuff first, need to know the font */
2379 block= uiBeginBlock(C, handle->region, "menu", UI_EMBOSSP);
2380 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1|UI_BLOCK_NUMSELECT);
2381 block->direction= UI_DOWN;
2383 md= decompose_menu_string(info->instr);
2385 /* columns and row calculation */
2386 columns= (md->nitems+maxrow)/maxrow;
2387 if (columns<1) columns= 1;
2391 columns= (md->nitems+maxrow)/maxrow;
2394 rows= (int) md->nitems/columns;
2395 if (rows<1) rows= 1;
2397 while (rows*columns<(md->nitems+columns) ) rows++;
2399 /* size and location, title slightly bigger for bold */
2401 width= 2*strlen(md->title)+UI_GetStringWidth(md->title);
2406 for(a=0; a<md->nitems; a++) {
2407 xmax= UI_GetStringWidth(md->items[a].str);
2408 if(xmax>width) width= xmax;
2412 if (width<50) width=50;
2414 height= rows*MENU_BUTTON_HEIGHT;
2415 if (md->title) height+= MENU_BUTTON_HEIGHT;
2417 wm_window_get_size(CTX_wm_window(C), &xmax, &ymax);
2419 /* find active item */
2420 fvalue= handle->retvalue;
2421 for(a=0; a<md->nitems; a++) {
2422 if( md->items[a].retval== (int)fvalue ) break;
2425 /* no active item? */
2427 if(md->title) a= -1;
2432 startx = info->mx-width/2 - ((int)(a)/rows)*width;
2434 startx= info->mx-width/2;
2435 starty = info->my-height + MENU_BUTTON_HEIGHT/2 + ((a)%rows)*MENU_BUTTON_HEIGHT;
2437 if (md->title) starty+= MENU_BUTTON_HEIGHT;
2440 mousemove[0]= 10-startx;
2444 mousemove[1]= 10-starty;
2448 endx= startx+width*columns;
2449 endy= starty+height;
2452 mousemove[0]= xmax-endx-10;
2454 startx= endx-width*columns;
2457 mousemove[1]= ymax-endy-10;
2459 starty= endy-height;
2462 if(mousemove[0] || mousemove[1]) {
2463 ui_warp_pointer(info->mx+mousemove[0], info->my+mousemove[1]);
2464 mousemove[0]= info->mx;
2465 mousemove[1]= info->my;
2476 bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+rows*MENU_BUTTON_HEIGHT), columns*width, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2477 bt->flag= UI_TEXT_LEFT;
2481 for(a=0; a<md->nitems; a++) {
2482 char *name= md->items[a].str;
2483 int icon = md->items[a].icon;
2485 x1= startx + width*((int)a/rows);
2486 y1= starty - MENU_BUTTON_HEIGHT*(a%rows) + (rows-1)*MENU_BUTTON_HEIGHT;
2488 if(strcmp(name, "%l")==0) {
2489 uiDefBut(block, SEPR, B_NOP, "", x1, y1, width, PUP_LABELH, NULL, 0, 0.0, 0, 0, "");
2493 uiDefIconButF(block, BUTM, B_NOP, icon, x1, y1, width+16, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
2494 y1 -= MENU_BUTTON_HEIGHT;
2497 uiDefButF(block, BUTM, B_NOP, name, x1, y1, width, MENU_BUTTON_HEIGHT-1, &handle->retvalue, (float) md->items[a].retval, 0.0, 0, 0, "");
2498 y1 -= MENU_BUTTON_HEIGHT;
2502 uiBoundsBlock(block, 1);
2503 uiEndBlock(C, block);
2507 /* XXX 2.5 need to warp back */
2509 if((event & UI_RETURN_OUT)==0)
2510 ui_warp_pointer(mousemove[0], mousemove[1]);
2516 /************************** Menu Definitions ***************************/
2519 static uiBlock *ui_block_func_MENU_ITEM(bContext *C, uiPopupBlockHandle *handle, void *arg_info);
2521 struct uiPopupMenu {
2526 typedef struct uiMenuInfo {
2528 int mx, my, popup, slideout;
2532 /************************ Menu Definitions to uiBlocks ***********************/
2534 static uiBlock *ui_block_func_MENU_ITEM(bContext *C, uiPopupBlockHandle *handle, void *arg_info)
2537 uiMenuInfo *info= arg_info;
2545 /* block stuff first, need to know the font */
2546 uiBlockSetRegion(block, handle->region);
2547 block->direction= UI_DOWN;
2549 uiBlockLayoutResolve(C, block, NULL, NULL);
2552 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_RET_1);
2553 uiBlockSetDirection(block, UI_DOWN);
2555 /* here we set an offset for the mouse position */
2556 uiMenuPopupBoundsBlock(block, 1, 0, 1.5*MENU_BUTTON_HEIGHT);
2559 /* for a header menu we set the direction automatic */
2560 if(!info->slideout) {
2562 ar= CTX_wm_region(C);
2564 if(sa && sa->headertype==HEADERDOWN) {
2565 if(ar && ar->regiontype == RGN_TYPE_HEADER) {
2566 uiBlockSetDirection(block, UI_TOP);
2567 uiBlockFlipOrder(block);
2572 uiTextBoundsBlock(block, 50);
2575 /* if menu slides out of other menu, override direction */
2577 uiBlockSetDirection(block, UI_RIGHT);
2579 uiEndBlock(C, block);
2584 uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut *but, uiMenuCreateFunc menu_func, void *arg)
2586 uiStyle *style= U.uistyles.first;
2587 uiPopupBlockHandle *handle;
2591 pup= MEM_callocN(sizeof(uiPopupMenu), "menu dummy");
2592 pup->block= uiBeginBlock(C, NULL, "ui_popup_menu_create", UI_EMBOSSP);
2593 pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
2594 uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
2596 /* create in advance so we can let buttons point to retval already */
2597 pup->block->handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
2599 menu_func(C, pup->layout, arg);
2601 memset(&info, 0, sizeof(info));
2603 info.slideout= (but && (but->block->flag & UI_BLOCK_LOOP));
2605 handle= ui_popup_block_create(C, butregion, but, NULL, ui_block_func_MENU_ITEM, &info);
2612 /*************************** Menu Creating API **************************/
2615 /*************************** Popup Menu API **************************/
2617 /* only return handler, and set optional title */
2618 uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
2620 uiStyle *style= U.uistyles.first;
2621 uiPopupMenu *pup= MEM_callocN(sizeof(uiPopupMenu), "menu start");
2624 pup->block= uiBeginBlock(C, NULL, "uiPupMenuBegin", UI_EMBOSSP);
2625 pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
2626 uiLayoutSetOperatorContext(pup->layout, WM_OP_EXEC_REGION_WIN);
2628 /* create in advance so we can let buttons point to retval already */
2629 pup->block->handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
2631 /* create title button */
2632 if(title && title[0]) {
2636 sprintf(titlestr, " %s", title);
2637 uiDefIconTextBut(pup->block, LABEL, 0, icon, titlestr, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2640 but= uiDefBut(pup->block, LABEL, 0, (char*)title, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2641 but->flag= UI_TEXT_LEFT;
2644 //uiDefBut(block, SEPR, 0, "", startx, (short)(starty+height)-MENU_SEPR_HEIGHT, width, MENU_SEPR_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2650 /* set the whole structure to work */
2651 void uiPupMenuEnd(bContext *C, uiPopupMenu *pup)
2653 wmWindow *window= CTX_wm_window(C);
2655 uiPopupBlockHandle *menu;
2657 memset(&info, 0, sizeof(info));
2659 info.mx= window->eventstate->x;
2660 info.my= window->eventstate->y;
2663 menu= ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_MENU_ITEM, &info);
2666 UI_add_popup_handlers(C, &window->handlers, menu);
2667 WM_event_add_mousemove(C);
2672 uiLayout *uiPupMenuLayout(uiPopupMenu *pup)
2677 /* ************** standard pupmenus *************** */
2679 /* this one can called with operatortype name and operators */
2680 static uiPopupBlockHandle *ui_pup_menu(bContext *C, int maxrow, uiMenuHandleFunc func, void *arg, char *str, ...)
2682 wmWindow *window= CTX_wm_window(C);
2684 uiPopupBlockHandle *menu;
2686 memset(&info, 0, sizeof(info));
2687 info.mx= window->eventstate->x;
2688 info.my= window->eventstate->y;
2689 info.maxrow= maxrow;
2692 menu= ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_PUPMENU, &info);
2695 UI_add_popup_handlers(C, &window->handlers, menu);
2696 WM_event_add_mousemove(C);
2698 menu->popup_func= func;
2699 menu->popup_arg= arg;
2704 static void operator_name_cb(bContext *C, void *arg, int retval)
2706 const char *opname= arg;
2708 if(opname && retval > 0)
2709 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
2712 static void vconfirm_opname(bContext *C, char *opname, char *title, char *itemfmt, va_list ap)
2717 if (title) s+= sprintf(s, "%s%%t|", title);
2718 vsprintf(s, itemfmt, ap);
2720 ui_pup_menu(C, 0, operator_name_cb, opname, buf);
2723 static void operator_cb(bContext *C, void *arg, int retval)
2725 wmOperator *op= arg;
2727 if(op && retval > 0)
2728 WM_operator_call(C, op);
2730 WM_operator_free(op);
2733 static void confirm_cancel_operator(void *opv)
2735 WM_operator_free(opv);
2738 static void confirm_operator(bContext *C, wmOperator *op, char *title, char *item)
2740 uiPopupBlockHandle *handle;
2744 if (title) s+= sprintf(s, "%s%%t|%s", title, item);
2746 handle= ui_pup_menu(C, 0, operator_cb, op, buf);
2747 handle->cancel_func= confirm_cancel_operator;
2751 void uiPupMenuOkee(bContext *C, char *opname, char *str, ...)
2756 sprintf(titlestr, "OK? %%i%d", ICON_QUESTION);
2759 vconfirm_opname(C, opname, titlestr, str, ap);
2764 void uiPupMenuSaveOver(bContext *C, wmOperator *op, char *filename)
2766 size_t len= strlen(filename);
2771 if(filename[len-1]=='/' || filename[len-1]=='\\') {
2772 uiPupMenuError(C, "Cannot overwrite a directory");
2773 WM_operator_free(op);
2776 if(BLI_exists(filename)==0)
2777 operator_cb(C, op, 1);
2779 confirm_operator(C, op, "Save over", filename);
2782 void uiPupMenuNotice(bContext *C, char *str, ...)
2787 vconfirm_opname(C, NULL, NULL, str, ap);
2791 void uiPupMenuError(bContext *C, char *str, ...)
2797 sprintf(titlestr, "Error %%i%d", ICON_ERROR);
2799 sprintf(nfmt, "%s", str);
2802 vconfirm_opname(C, NULL, titlestr, nfmt, ap);
2806 void uiPupMenuReports(bContext *C, ReportList *reports)
2812 if(!reports || !reports->list.first)
2814 if(!CTX_wm_window(C))
2817 ds= BLI_dynstr_new();
2819 for(report=reports->list.first; report; report=report->next) {
2820 if(report->type >= RPT_ERROR)
2821 BLI_dynstr_appendf(ds, "Error %%i%d%%t|%s", ICON_ERROR, report->message);
2822 else if(report->type >= RPT_WARNING)
2823 BLI_dynstr_appendf(ds, "Warning %%i%d%%t|%s", ICON_ERROR, report->message);
2824 else if(report->type >= RPT_INFO)
2825 BLI_dynstr_appendf(ds, "Info %%t|%s", report->message);
2828 str= BLI_dynstr_get_cstring(ds);
2829 ui_pup_menu(C, 0, NULL, NULL, str);
2832 BLI_dynstr_free(ds);
2835 /*************************** Popup Block API **************************/
2837 void uiPupBlockO(bContext *C, uiBlockCreateFunc func, void *arg, char *opname, int opcontext)
2839 wmWindow *window= CTX_wm_window(C);
2840 uiPopupBlockHandle *handle;
2842 handle= ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
2844 handle->optype= (opname)? WM_operatortype_find(opname, 0): NULL;
2845 handle->opcontext= opcontext;
2847 UI_add_popup_handlers(C, &window->handlers, handle);
2848 WM_event_add_mousemove(C);
2851 void uiPupBlock(bContext *C, uiBlockCreateFunc func, void *arg)
2853 uiPupBlockO(C, func, arg, NULL, 0);
2856 void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int opcontext)
2858 wmWindow *window= CTX_wm_window(C);
2859 uiPopupBlockHandle *handle;
2861 handle= ui_popup_block_create(C, NULL, NULL, func, NULL, op);
2863 handle->retvalue= 1;
2865 handle->popup_arg= op;
2866 handle->popup_func= operator_cb;
2867 handle->cancel_func= confirm_cancel_operator;
2868 handle->opcontext= opcontext;
2870 UI_add_popup_handlers(C, &window->handlers, handle);
2871 WM_event_add_mousemove(C);