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"
39 #include "BLI_blenlib.h"
40 #include "BLI_dynstr.h"
41 #include "BLI_ghash.h"
43 #include "BKE_context.h"
44 #include "BKE_icons.h"
45 #include "BKE_report.h"
46 #include "BKE_screen.h"
47 #include "BKE_texture.h"
48 #include "BKE_utildefines.h"
53 #include "wm_subwindow.h"
54 #include "wm_window.h"
56 #include "RNA_access.h"
60 #include "UI_interface.h"
61 #include "UI_interface_icons.h"
62 #include "UI_view2d.h"
66 #include "ED_screen.h"
68 #include "interface_intern.h"
70 #define MENU_BUTTON_HEIGHT 20
71 #define MENU_SEPR_HEIGHT 6
73 #define MENU_SHADOW_SIDE 8
74 #define MENU_SHADOW_BOTTOM 10
77 /*********************** Menu Data Parsing ********************* */
79 typedef struct MenuEntry {
86 typedef struct MenuData {
92 int nitems, itemssize;
95 static MenuData *menudata_new(char *instr)
97 MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
103 md->nitems= md->itemssize= 0;
108 static void menudata_set_title(MenuData *md, char *title, int titleicon)
113 md->titleicon= titleicon;
116 static void menudata_add_item(MenuData *md, char *str, int retval, int icon, int sepr)
118 if (md->nitems==md->itemssize) {
119 int nsize= md->itemssize?(md->itemssize<<1):1;
120 MenuEntry *oitems= md->items;
122 md->items= MEM_mallocN(nsize*sizeof(*md->items), "md->items");
124 memcpy(md->items, oitems, md->nitems*sizeof(*md->items));
128 md->itemssize= nsize;
131 md->items[md->nitems].str= str;
132 md->items[md->nitems].retval= retval;
133 md->items[md->nitems].icon= icon;
134 md->items[md->nitems].sepr= sepr;
138 void menudata_free(MenuData *md)
140 MEM_freeN(md->instr);
142 MEM_freeN(md->items);
147 * Parse menu description strings, string is of the
148 * form "[sss%t|]{(sss[%xNN]|), (%l|), (sss%l|)}", ssss%t indicates the
149 * menu title, sss or sss%xNN indicates an option,
150 * if %xNN is given then NN is the return value if
151 * that option is selected otherwise the return value
152 * is the index of the option (starting with 1). %l
153 * indicates a seperator, sss%l indicates a label and
156 * @param str String to be parsed.
157 * @retval new menudata structure, free with menudata_free()
159 MenuData *decompose_menu_string(char *str)
161 char *instr= BLI_strdup(str);
162 MenuData *md= menudata_new(instr);
163 char *nitem= NULL, *s= instr;
164 int nicon=0, nretval= 1, nitem_is_title= 0, nitem_is_sepr= 0;
175 } else if (s[1]=='t') {
180 } else if (s[1]=='l') {
182 if(!nitem) nitem= "";
186 } else if (s[1]=='i') {
192 } else if (c=='|' || c == '\n' || c=='\0') {
197 menudata_set_title(md, nitem, nicon);
200 else if(nitem_is_sepr) {
201 /* prevent separator to get a value */
202 menudata_add_item(md, nitem, -1, nicon, 1);
203 nretval= md->nitems+1;
207 menudata_add_item(md, nitem, nretval, nicon, 0);
208 nretval= md->nitems+1;
226 void ui_set_name_menu(uiBut *but, int value)
231 md= decompose_menu_string(but->str);
232 for (i=0; i<md->nitems; i++)
233 if (md->items[i].retval==value)
234 strcpy(but->drawstr, md->items[i].str);
239 int ui_step_name_menu(uiBut *but, int step)
242 int value= ui_get_but_val(but);
245 md= decompose_menu_string(but->str);
246 for (i=0; i<md->nitems; i++)
247 if (md->items[i].retval==value)
251 /* skip separators */
252 for(; i<md->nitems-1; i++) {
253 if(md->items[i+1].retval != -1) {
254 value= md->items[i+1].retval;
261 /* skip separators */
263 if(md->items[i-1].retval != -1) {
264 value= md->items[i-1].retval;
277 /******************** Creating Temporary regions ******************/
279 ARegion *ui_add_temporary_region(bScreen *sc)
283 ar= MEM_callocN(sizeof(ARegion), "area region");
284 BLI_addtail(&sc->regionbase, ar);
286 ar->regiontype= RGN_TYPE_TEMPORARY;
287 ar->alignment= RGN_ALIGN_FLOAT;
292 void ui_remove_temporary_region(bContext *C, bScreen *sc, ARegion *ar)
295 wm_draw_region_clear(CTX_wm_window(C), ar);
297 ED_region_exit(C, ar);
298 BKE_area_region_free(NULL, ar); /* NULL: no spacetype */
299 BLI_freelinkN(&sc->regionbase, ar);
302 /************************* Creating Tooltips **********************/
304 #define MAX_TOOLTIP_LINES 8
306 typedef struct uiTooltipData {
309 char lines[MAX_TOOLTIP_LINES][512];
310 int linedark[MAX_TOOLTIP_LINES];
312 int toth, spaceh, lineh;
315 static void ui_tooltip_region_draw(const bContext *C, ARegion *ar)
317 uiTooltipData *data= ar->regiondata;
318 rcti bbox= data->bbox;
321 ui_draw_menu_back(U.uistyles.first, NULL, &data->bbox);
324 uiStyleFontSet(&data->fstyle);
326 bbox.ymax= bbox.ymax - 0.5f*((bbox.ymax - bbox.ymin) - data->toth);
327 bbox.ymin= bbox.ymax - data->lineh;
329 for(a=0; a<data->totline; a++) {
330 if(!data->linedark[a]) glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
331 else glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
333 uiStyleFontDraw(&data->fstyle, &bbox, data->lines[a]);
334 bbox.ymin -= data->lineh + data->spaceh;
335 bbox.ymax -= data->lineh + data->spaceh;
339 static void ui_tooltip_region_free(ARegion *ar)
343 data= ar->regiondata;
345 ar->regiondata= NULL;
348 ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
350 uiStyle *style= U.uistyles.first; // XXX pass on as arg
351 static ARegionType type;
356 float fonth, fontw, aspect= but->block->aspect;
357 float x1f, x2f, y1f, y2f;
358 int x1, x2, y1, y2, winx, winy, ofsx, ofsy, w, h, a;
360 if(but->flag & UI_BUT_NO_TOOLTIP)
363 /* create tooltip data */
364 data= MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
366 if(but->tip && strlen(but->tip)) {
367 BLI_strncpy(data->lines[data->totline], but->tip, sizeof(data->lines[0]));
371 if(but->optype && !(but->block->flag & UI_BLOCK_LOOP)) {
372 /* operator keymap (not menus, they already have it) */
373 prop= (but->opptr)? but->opptr->data: NULL;
375 if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
376 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Shortcut: %s", buf);
377 data->linedark[data->totline]= 1;
382 if(ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
384 ui_get_but_string(but, buf, sizeof(buf));
386 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Value: %s", buf);
387 data->linedark[data->totline]= 1;
393 if(but->flag & UI_BUT_DRIVEN) {
394 if(ui_but_anim_expression_get(but, buf, sizeof(buf))) {
396 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Expression: %s", buf);
397 data->linedark[data->totline]= 1;
403 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s.%s", RNA_struct_identifier(but->rnapoin.type), RNA_property_identifier(but->rnaprop));
404 data->linedark[data->totline]= 1;
407 else if (but->optype) {
410 opptr= uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
412 str= WM_operator_pystring(C, but->optype, opptr, 0);
415 BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), "Python: %s", str);
416 data->linedark[data->totline]= 1;
422 if(data->totline == 0) {
427 /* create area region */
428 ar= ui_add_temporary_region(CTX_wm_screen(C));
430 memset(&type, 0, sizeof(ARegionType));
431 type.draw= ui_tooltip_region_draw;
432 type.free= ui_tooltip_region_free;
435 /* set font, get bb */
436 data->fstyle= style->widget; /* copy struct */
437 data->fstyle.align= UI_STYLE_TEXT_CENTER;
438 ui_fontscale(&data->fstyle.points, aspect);
439 uiStyleFontSet(&data->fstyle);
441 h= BLF_height(data->lines[0]);
443 for(a=0, fontw=0, fonth=0; a<data->totline; a++) {
444 w= BLF_width(data->lines[a]);
445 fontw= MAX2(fontw, w);
446 fonth += (a == 0)? h: h+5;
452 ar->regiondata= data;
455 data->lineh= h*aspect;
456 data->spaceh= 5*aspect;
458 /* compute position */
459 ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
460 ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
462 x1f= (but->x1+but->x2)/2.0f + ofsx - 16.0f*aspect;
463 x2f= x1f + fontw + 16.0f*aspect;
464 y2f= but->y1 + ofsy - 15.0f*aspect;
465 y1f= y2f - fonth - 10.0f*aspect;
467 /* copy to int, gets projected if possible too */
468 x1= x1f; y1= y1f; x2= x2f; y2= y2f;
471 /* XXX temp, region v2ds can be empty still */
472 if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
473 UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
474 UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
477 x1 += butregion->winrct.xmin;
478 x2 += butregion->winrct.xmin;
479 y1 += butregion->winrct.ymin;
480 y2 += butregion->winrct.ymin;
483 wm_window_get_size(CTX_wm_window(C), &winx, &winy);
501 /* widget rect, in region coords */
502 data->bbox.xmin= MENU_SHADOW_SIDE;
503 data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
504 data->bbox.ymin= MENU_SHADOW_BOTTOM;
505 data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
507 /* region bigger for shadow */
508 ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
509 ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
510 ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
511 ar->winrct.ymax= y2 + MENU_TOP;
514 ED_region_init(C, ar);
516 /* notify change and redraw */
517 ED_region_tag_redraw(ar);
522 void ui_tooltip_free(bContext *C, ARegion *ar)
524 ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
528 /************************* Creating Search Box **********************/
530 struct uiSearchItems {
531 int maxitem, totitem, maxstrlen;
533 int offset, offset_i; /* offset for inserting in array */
534 int more; /* flag indicating there are more items */
540 AutoComplete *autocpl;
544 typedef struct uiSearchboxData {
548 int active; /* index in items array */
549 int noback; /* when menu opened with enough space for this */
550 int preview; /* draw thumbnail previews, rather than list */
551 int prv_rows, prv_cols;
554 #define SEARCH_ITEMS 10
556 /* exported for use by search callbacks */
557 /* returns zero if nothing to add */
558 int uiSearchItemAdd(uiSearchItems *items, const char *name, void *poin, int iconid)
560 /* hijack for autocomplete */
562 autocomplete_do_name(items->autocpl, name);
566 /* hijack for finding active item */
568 if(poin==items->active)
569 items->offset_i= items->totitem;
574 if(items->totitem>=items->maxitem) {
579 /* skip first items in list */
580 if(items->offset_i > 0) {
585 BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
586 items->pointers[items->totitem]= poin;
587 items->icons[items->totitem]= iconid;
594 int uiSearchBoxhHeight(void)
596 return SEARCH_ITEMS*MENU_BUTTON_HEIGHT + 2*MENU_TOP;
599 /* ar is the search box itself */
600 static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
602 uiSearchboxData *data= ar->regiondata;
607 if(data->items.totitem==0)
609 else if(data->active > data->items.totitem) {
610 if(data->items.more) {
611 data->items.offset++;
612 data->active= data->items.totitem;
613 ui_searchbox_update(C, ar, but, 0);
616 data->active= data->items.totitem;
618 else if(data->active < 1) {
619 if(data->items.offset) {
620 data->items.offset--;
622 ui_searchbox_update(C, ar, but, 0);
624 else if(data->active < 0)
628 ED_region_tag_redraw(ar);
631 static void ui_searchbox_butrect(rcti *rect, uiSearchboxData *data, int itemnr)
633 /* thumbnail preview */
635 int buth = (data->bbox.ymax - data->bbox.ymin - 2*MENU_TOP) / data->prv_rows;
636 int butw = (data->bbox.xmax - data->bbox.xmin) / data->prv_cols;
641 col = itemnr % data->prv_cols;
642 row = itemnr / data->prv_cols;
644 rect->xmin += col * butw;
645 rect->xmax = rect->xmin + butw;
647 rect->ymax = data->bbox.ymax - (row * buth);
648 rect->ymin = rect->ymax - buth;
652 int buth= (data->bbox.ymax-data->bbox.ymin - 2*MENU_TOP)/SEARCH_ITEMS;
655 rect->xmin= data->bbox.xmin + 3.0f;
656 rect->xmax= data->bbox.xmax - 3.0f;
658 rect->ymax= data->bbox.ymax - MENU_TOP - itemnr*buth;
659 rect->ymin= rect->ymax - buth;
664 /* x and y in screencoords */
665 int ui_searchbox_inside(ARegion *ar, int x, int y)
667 uiSearchboxData *data= ar->regiondata;
669 return(BLI_in_rcti(&data->bbox, x-ar->winrct.xmin, y-ar->winrct.ymin));
672 /* string validated to be of correct length (but->hardmax) */
673 void ui_searchbox_apply(uiBut *but, ARegion *ar)
675 uiSearchboxData *data= ar->regiondata;
677 but->func_arg2= NULL;
680 char *name= data->items.names[data->active-1];
681 char *cpoin= strchr(name, '|');
683 if(cpoin) cpoin[0]= 0;
684 BLI_strncpy(but->editstr, name, data->items.maxstrlen);
685 if(cpoin) cpoin[0]= '|';
687 but->func_arg2= data->items.pointers[data->active-1];
691 void ui_searchbox_event(bContext *C, ARegion *ar, uiBut *but, wmEvent *event)
693 uiSearchboxData *data= ar->regiondata;
695 switch(event->type) {
698 ui_searchbox_select(C, ar, but, -1);
702 ui_searchbox_select(C, ar, but, 1);
705 if(BLI_in_rcti(&ar->winrct, event->x, event->y)) {
709 for(a=0; a<data->items.totitem; a++) {
710 ui_searchbox_butrect(&rect, data, a);
711 if(BLI_in_rcti(&rect, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
712 if( data->active!= a+1) {
714 ui_searchbox_select(C, ar, but, 0);
724 /* ar is the search box itself */
725 void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, int reset)
727 uiSearchboxData *data= ar->regiondata;
730 data->items.totitem= 0;
733 data->items.offset_i= data->items.offset;
736 data->items.offset_i= data->items.offset= 0;
740 if(but->search_func && but->func_arg2) {
741 data->items.active= but->func_arg2;
742 but->search_func(C, but->search_arg, but->editstr, &data->items);
743 data->items.active= NULL;
745 /* found active item, calculate real offset by centering it */
746 if(data->items.totitem) {
747 /* first case, begin of list */
748 if(data->items.offset_i < data->items.maxitem) {
749 data->active= data->items.offset_i+1;
750 data->items.offset_i= 0;
753 /* second case, end of list */
754 if(data->items.totitem - data->items.offset_i <= data->items.maxitem) {
755 data->active= 1 + data->items.offset_i - data->items.totitem + data->items.maxitem;
756 data->items.offset_i= data->items.totitem - data->items.maxitem;
759 /* center active item */
760 data->items.offset_i -= data->items.maxitem/2;
761 data->active= 1 + data->items.maxitem/2;
765 data->items.offset= data->items.offset_i;
766 data->items.totitem= 0;
772 but->search_func(C, but->search_arg, but->editstr, &data->items);
774 /* handle case where editstr is equal to one of items */
775 if(reset && data->active==0) {
778 for(a=0; a<data->items.totitem; a++) {
779 char *cpoin= strchr(data->items.names[a], '|');
781 if(cpoin) cpoin[0]= 0;
782 if(0==strcmp(but->editstr, data->items.names[a]))
784 if(cpoin) cpoin[0]= '|';
786 if(data->items.totitem==1 && but->editstr[0])
790 /* validate selected item */
791 ui_searchbox_select(C, ar, but, 0);
793 ED_region_tag_redraw(ar);
796 void ui_searchbox_autocomplete(bContext *C, ARegion *ar, uiBut *but, char *str)
798 uiSearchboxData *data= ar->regiondata;
801 data->items.autocpl= autocomplete_begin(str, ui_get_but_string_max_length(but));
803 but->search_func(C, but->search_arg, but->editstr, &data->items);
805 autocomplete_end(data->items.autocpl, str);
806 data->items.autocpl= NULL;
810 static void ui_searchbox_region_draw(const bContext *C, ARegion *ar)
812 uiSearchboxData *data= ar->regiondata;
815 wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
818 ui_draw_search_back(U.uistyles.first, NULL, &data->bbox);
821 if(data->items.totitem) {
827 for(a=0; a<data->items.totitem; a++) {
828 ui_searchbox_butrect(&rect, data, a);
832 ui_draw_preview_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
834 ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
838 if(data->items.more) {
839 ui_searchbox_butrect(&rect, data, data->items.maxitem-1);
841 UI_icon_draw(rect.xmax-18, rect.ymin-7, ICON_TRIA_DOWN);
844 if(data->items.offset) {
845 ui_searchbox_butrect(&rect, data, 0);
847 UI_icon_draw(rect.xmin, rect.ymax-9, ICON_TRIA_UP);
853 for(a=0; a<data->items.totitem; a++) {
854 ui_searchbox_butrect(&rect, data, a);
857 ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a], (a+1)==data->active?UI_ACTIVE:0);
861 if(data->items.more) {
862 ui_searchbox_butrect(&rect, data, data->items.maxitem-1);
864 UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymin-9, ICON_TRIA_DOWN);
867 if(data->items.offset) {
868 ui_searchbox_butrect(&rect, data, 0);
870 UI_icon_draw((rect.xmax-rect.xmin)/2, rect.ymax-7, ICON_TRIA_UP);
877 static void ui_searchbox_region_free(ARegion *ar)
879 uiSearchboxData *data= ar->regiondata;
882 /* free search data */
883 for(a=0; a<data->items.maxitem; a++)
884 MEM_freeN(data->items.names[a]);
885 MEM_freeN(data->items.names);
886 MEM_freeN(data->items.pointers);
887 MEM_freeN(data->items.icons);
890 ar->regiondata= NULL;
893 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block);
895 ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
897 uiStyle *style= U.uistyles.first; // XXX pass on as arg
898 static ARegionType type;
900 uiSearchboxData *data;
901 float aspect= but->block->aspect;
902 float x1f, x2f, y1f, y2f;
903 int x1, x2, y1, y2, winx, winy, ofsx, ofsy;
905 /* create area region */
906 ar= ui_add_temporary_region(CTX_wm_screen(C));
908 memset(&type, 0, sizeof(ARegionType));
909 type.draw= ui_searchbox_region_draw;
910 type.free= ui_searchbox_region_free;
913 /* create searchbox data */
914 data= MEM_callocN(sizeof(uiSearchboxData), "uiSearchboxData");
916 /* set font, get bb */
917 data->fstyle= style->widget; /* copy struct */
918 data->fstyle.align= UI_STYLE_TEXT_CENTER;
919 ui_fontscale(&data->fstyle.points, aspect);
920 uiStyleFontSet(&data->fstyle);
922 ar->regiondata= data;
924 /* special case, hardcoded feature, not draw backdrop when called from menus,
925 assume for design that popup already added it */
926 if(but->block->flag & UI_BLOCK_LOOP)
929 if (but->a1 > 0 && but->a2 > 0) {
931 data->prv_rows = but->a1;
932 data->prv_cols = but->a2;
935 /* compute position */
936 if(but->block->flag & UI_BLOCK_LOOP) {
937 /* this case is search menu inside other menu */
938 /* we copy region size */
940 ar->winrct= butregion->winrct;
942 /* widget rect, in region coords */
943 data->bbox.xmin= MENU_SHADOW_SIDE;
944 data->bbox.xmax= (ar->winrct.xmax-ar->winrct.xmin) - MENU_SHADOW_SIDE;
945 data->bbox.ymin= MENU_SHADOW_BOTTOM;
946 data->bbox.ymax= (ar->winrct.ymax-ar->winrct.ymin) - MENU_SHADOW_BOTTOM;
948 /* check if button is lower half */
949 if( but->y2 < (but->block->minx+but->block->maxx)/2 ) {
950 data->bbox.ymin += (but->y2-but->y1);
953 data->bbox.ymax -= (but->y2-but->y1);
957 x1f= but->x1 - 5; /* align text with button */
958 x2f= but->x2 + 5; /* symmetrical */
960 y1f= y2f - uiSearchBoxhHeight();
962 ofsx= (but->block->panel)? but->block->panel->ofsx: 0;
963 ofsy= (but->block->panel)? but->block->panel->ofsy: 0;
971 if(x2f - x1f < 150) x2f= x1f+150; // XXX arbitrary
973 /* copy to int, gets projected if possible too */
974 x1= x1f; y1= y1f; x2= x2f; y2= y2f;
977 if(butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
978 UI_view2d_to_region_no_clip(&butregion->v2d, x1f, y1f, &x1, &y1);
979 UI_view2d_to_region_no_clip(&butregion->v2d, x2f, y2f, &x2, &y2);
982 x1 += butregion->winrct.xmin;
983 x2 += butregion->winrct.xmin;
984 y1 += butregion->winrct.ymin;
985 y2 += butregion->winrct.ymin;
988 wm_window_get_size(CTX_wm_window(C), &winx, &winy);
1003 UI_view2d_to_region_no_clip(&butregion->v2d, 0, but->y2 + ofsy, 0, &newy1);
1004 newy1 += butregion->winrct.ymin;
1010 /* widget rect, in region coords */
1011 data->bbox.xmin= MENU_SHADOW_SIDE;
1012 data->bbox.xmax= x2-x1 + MENU_SHADOW_SIDE;
1013 data->bbox.ymin= MENU_SHADOW_BOTTOM;
1014 data->bbox.ymax= y2-y1 + MENU_SHADOW_BOTTOM;
1016 /* region bigger for shadow */
1017 ar->winrct.xmin= x1 - MENU_SHADOW_SIDE;
1018 ar->winrct.xmax= x2 + MENU_SHADOW_SIDE;
1019 ar->winrct.ymin= y1 - MENU_SHADOW_BOTTOM;
1020 ar->winrct.ymax= y2;
1023 /* adds subwindow */
1024 ED_region_init(C, ar);
1026 /* notify change and redraw */
1027 ED_region_tag_redraw(ar);
1029 /* prepare search data */
1030 if (data->preview) {
1031 data->items.maxitem= data->prv_rows * data->prv_cols;
1033 data->items.maxitem= SEARCH_ITEMS;
1035 data->items.maxstrlen= but->hardmax;
1036 data->items.totitem= 0;
1037 data->items.names= MEM_callocN(data->items.maxitem*sizeof(void *), "search names");
1038 data->items.pointers= MEM_callocN(data->items.maxitem*sizeof(void *), "search pointers");
1039 data->items.icons= MEM_callocN(data->items.maxitem*sizeof(int), "search icons");
1040 for(x1=0; x1<data->items.maxitem; x1++)
1041 data->items.names[x1]= MEM_callocN(but->hardmax+1, "search pointers");
1046 void ui_searchbox_free(bContext *C, ARegion *ar)
1048 ui_remove_temporary_region(C, CTX_wm_screen(C), ar);
1052 /************************* Creating Menu Blocks **********************/
1054 /* position block relative to but, result is in window space */
1055 static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
1058 uiSafetyRct *saferct;
1061 int xsize, ysize, xof=0, yof=0, center;
1062 short dir1= 0, dir2=0;
1064 /* transform to window coordinates, using the source button region/block */
1065 butrct.xmin= but->x1; butrct.xmax= but->x2;
1066 butrct.ymin= but->y1; butrct.ymax= but->y2;
1068 ui_block_to_window_fl(butregion, but->block, &butrct.xmin, &butrct.ymin);
1069 ui_block_to_window_fl(butregion, but->block, &butrct.xmax, &butrct.ymax);
1071 /* calc block rect */
1072 if(block->minx == 0.0f && block->maxx == 0.0f) {
1073 if(block->buttons.first) {
1074 block->minx= block->miny= 10000;
1075 block->maxx= block->maxy= -10000;
1077 bt= block->buttons.first;
1079 if(bt->x1 < block->minx) block->minx= bt->x1;
1080 if(bt->y1 < block->miny) block->miny= bt->y1;
1082 if(bt->x2 > block->maxx) block->maxx= bt->x2;
1083 if(bt->y2 > block->maxy) block->maxy= bt->y2;
1089 /* we're nice and allow empty blocks too */
1090 block->minx= block->miny= 0;
1091 block->maxx= block->maxy= 20;
1095 aspect= (float)(block->maxx - block->minx + 4);
1096 ui_block_to_window_fl(butregion, but->block, &block->minx, &block->miny);
1097 ui_block_to_window_fl(butregion, but->block, &block->maxx, &block->maxy);
1099 //block->minx-= 2.0; block->miny-= 2.0;
1100 //block->maxx+= 2.0; block->maxy+= 2.0;
1102 xsize= block->maxx - block->minx+4; // 4 for shadow
1103 ysize= block->maxy - block->miny+4;
1104 aspect/= (float)xsize;
1107 int left=0, right=0, top=0, down=0;
1110 wm_window_get_size(window, &winx, &winy);
1112 if(block->direction & UI_CENTER) center= ysize/2;
1115 if( butrct.xmin-xsize > 0.0) left= 1;
1116 if( butrct.xmax+xsize < winx) right= 1;
1117 if( butrct.ymin-ysize+center > 0.0) down= 1;
1118 if( butrct.ymax+ysize-center < winy) top= 1;
1120 dir1= block->direction & UI_DIRECTION;
1122 /* secundary directions */
1123 if(dir1 & (UI_TOP|UI_DOWN)) {
1124 if(dir1 & UI_LEFT) dir2= UI_LEFT;
1125 else if(dir1 & UI_RIGHT) dir2= UI_RIGHT;
1126 dir1 &= (UI_TOP|UI_DOWN);
1129 if(dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
1130 if(dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
1132 /* no space at all? dont change */
1134 if(dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
1135 if(dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
1136 /* this is aligning, not append! */
1137 if(dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
1138 if(dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
1141 if(dir1==UI_TOP && top==0) dir1= UI_DOWN;
1142 if(dir1==UI_DOWN && down==0) dir1= UI_TOP;
1143 if(dir2==UI_TOP && top==0) dir2= UI_DOWN;
1144 if(dir2==UI_DOWN && down==0) dir2= UI_TOP;
1148 xof= butrct.xmin - block->maxx;
1149 if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
1150 else yof= butrct.ymax - block->maxy+center;
1152 else if(dir1==UI_RIGHT) {
1153 xof= butrct.xmax - block->minx;
1154 if(dir2==UI_TOP) yof= butrct.ymin - block->miny-center;
1155 else yof= butrct.ymax - block->maxy+center;
1157 else if(dir1==UI_TOP) {
1158 yof= butrct.ymax - block->miny;
1159 if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
1160 else xof= butrct.xmin - block->minx;
1161 // changed direction?
1162 if((dir1 & block->direction)==0) {
1163 if(block->direction & UI_SHIFT_FLIPPED)
1164 xof+= dir2==UI_LEFT?25:-25;
1165 uiBlockFlipOrder(block);
1168 else if(dir1==UI_DOWN) {
1169 yof= butrct.ymin - block->maxy;
1170 if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
1171 else xof= butrct.xmin - block->minx;
1172 // changed direction?
1173 if((dir1 & block->direction)==0) {
1174 if(block->direction & UI_SHIFT_FLIPPED)
1175 xof+= dir2==UI_LEFT?25:-25;
1176 uiBlockFlipOrder(block);
1180 /* and now we handle the exception; no space below or to top */
1181 if(top==0 && down==0) {
1182 if(dir1==UI_LEFT || dir1==UI_RIGHT) {
1183 // align with bottom of screen
1188 /* or no space left or right */
1189 if(left==0 && right==0) {
1190 if(dir1==UI_TOP || dir1==UI_DOWN) {
1191 // align with left size of screen
1192 xof= -block->minx+5;
1196 // apply requested offset in the block
1197 xof += block->xofs/block->aspect;
1198 yof += block->yofs/block->aspect;
1203 for(bt= block->buttons.first; bt; bt= bt->next) {
1204 ui_block_to_window_fl(butregion, but->block, &bt->x1, &bt->y1);
1205 ui_block_to_window_fl(butregion, but->block, &bt->x2, &bt->y2);
1213 // ui_check_but recalculates drawstring size in pixels
1222 /* safety calculus */
1224 float midx= (butrct.xmin+butrct.xmax)/2.0;
1225 float midy= (butrct.ymin+butrct.ymax)/2.0;
1227 /* when you are outside parent button, safety there should be smaller */
1229 // parent button to left
1230 if( midx < block->minx ) block->safety.xmin= block->minx-3;
1231 else block->safety.xmin= block->minx-40;
1232 // parent button to right
1233 if( midx > block->maxx ) block->safety.xmax= block->maxx+3;
1234 else block->safety.xmax= block->maxx+40;
1236 // parent button on bottom
1237 if( midy < block->miny ) block->safety.ymin= block->miny-3;
1238 else block->safety.ymin= block->miny-40;
1239 // parent button on top
1240 if( midy > block->maxy ) block->safety.ymax= block->maxy+3;
1241 else block->safety.ymax= block->maxy+40;
1243 // exception for switched pulldowns...
1244 if(dir1 && (dir1 & block->direction)==0) {
1245 if(dir2==UI_RIGHT) block->safety.xmax= block->maxx+3;
1246 if(dir2==UI_LEFT) block->safety.xmin= block->minx-3;
1248 block->direction= dir1;
1251 block->safety.xmin= block->minx-40;
1252 block->safety.ymin= block->miny-40;
1253 block->safety.xmax= block->maxx+40;
1254 block->safety.ymax= block->maxy+40;
1257 /* keep a list of these, needed for pulldown menus */
1258 saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1259 saferct->parent= butrct;
1260 saferct->safety= block->safety;
1261 BLI_freelistN(&block->saferct);
1263 BLI_duplicatelist(&block->saferct, &but->block->saferct);
1264 BLI_addhead(&block->saferct, saferct);
1267 static void ui_block_region_draw(const bContext *C, ARegion *ar)
1271 for(block=ar->uiblocks.first; block; block=block->next)
1272 uiDrawBlock(C, block);
1275 uiPopupBlockHandle *ui_popup_block_create(bContext *C, ARegion *butregion, uiBut *but, uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func, void *arg)
1277 wmWindow *window= CTX_wm_window(C);
1278 static ARegionType type;
1282 uiPopupBlockHandle *handle;
1283 uiSafetyRct *saferct;
1286 handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
1288 /* store context for operator */
1289 handle->ctx_area= CTX_wm_area(C);
1290 handle->ctx_region= CTX_wm_region(C);
1292 /* create area region */
1293 ar= ui_add_temporary_region(CTX_wm_screen(C));
1296 memset(&type, 0, sizeof(ARegionType));
1297 type.draw= ui_block_region_draw;
1300 UI_add_region_handlers(&ar->handlers);
1302 /* create ui block */
1304 block= create_func(C, handle->region, arg);
1306 block= handle_create_func(C, handle, arg);
1309 memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
1311 handle= block->handle;
1314 block->handle= handle;
1316 ar->regiondata= handle;
1318 if(!block->endblock)
1319 uiEndBlock(C, block);
1321 /* if this is being created from a button */
1323 if(ELEM(but->type, BLOCK, PULLDOWN))
1324 block->xofs = -2; /* for proper alignment */
1326 /* only used for automatic toolbox, so can set the shift flag */
1327 if(but->flag & UI_MAKE_TOP) {
1328 block->direction= UI_TOP|UI_SHIFT_FLIPPED;
1329 uiBlockFlipOrder(block);
1331 if(but->flag & UI_MAKE_DOWN) block->direction= UI_DOWN|UI_SHIFT_FLIPPED;
1332 if(but->flag & UI_MAKE_LEFT) block->direction |= UI_LEFT;
1333 if(but->flag & UI_MAKE_RIGHT) block->direction |= UI_RIGHT;
1335 ui_block_position(window, butregion, but, block);
1338 /* keep a list of these, needed for pulldown menus */
1339 saferct= MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
1340 saferct->safety= block->safety;
1341 BLI_addhead(&block->saferct, saferct);
1342 block->flag |= UI_BLOCK_POPUP;
1345 /* the block and buttons were positioned in window space as in 2.4x, now
1346 * these menu blocks are regions so we bring it back to region space.
1347 * additionally we add some padding for the menu shadow or rounded menus */
1348 ar->winrct.xmin= block->minx - MENU_SHADOW_SIDE;
1349 ar->winrct.xmax= block->maxx + MENU_SHADOW_SIDE;
1350 ar->winrct.ymin= block->miny - MENU_SHADOW_BOTTOM;
1351 ar->winrct.ymax= block->maxy + MENU_TOP;
1353 block->minx -= ar->winrct.xmin;
1354 block->maxx -= ar->winrct.xmin;
1355 block->miny -= ar->winrct.ymin;
1356 block->maxy -= ar->winrct.ymin;
1358 for(bt= block->buttons.first; bt; bt= bt->next) {
1359 bt->x1 -= ar->winrct.xmin;
1360 bt->x2 -= ar->winrct.xmin;
1361 bt->y1 -= ar->winrct.ymin;
1362 bt->y2 -= ar->winrct.ymin;
1365 block->flag |= UI_BLOCK_LOOP;
1367 /* adds subwindow */
1368 ED_region_init(C, ar);
1370 /* get winmat now that we actually have the subwindow */
1371 wmSubWindowSet(window, ar->swinid);
1373 wm_subwindow_getmatrix(window, ar->swinid, block->winmat);
1375 /* notify change and redraw */
1376 ED_region_tag_redraw(ar);
1381 void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
1383 ui_remove_temporary_region(C, CTX_wm_screen(C), handle->region);
1387 /***************************** Menu Button ***************************/
1389 static void ui_block_func_MENUSTR(bContext *C, uiLayout *layout, void *arg_str)
1391 uiBlock *block= uiLayoutGetBlock(layout);
1392 uiPopupBlockHandle *handle= block->handle;
1393 uiLayout *split, *column=NULL;
1397 char *instr= arg_str;
1398 int columns, rows, a, b;
1400 uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
1402 /* compute menu data */
1403 md= decompose_menu_string(instr);
1405 /* columns and row estimation */
1406 columns= (md->nitems+20)/20;
1410 columns= (md->nitems+25)/25;
1412 rows= md->nitems/columns;
1415 while(rows*columns<md->nitems)
1421 uiItemL(layout, md->title, md->titleicon);
1424 uiItemL(layout, md->title, 0);
1425 bt= block->buttons.last;
1426 bt->flag= UI_TEXT_LEFT;
1430 /* inconsistent, but menus with labels do not look good flipped */
1431 for(a=0, b=0; a<md->nitems; a++, b++) {
1432 entry= &md->items[a];
1434 if(entry->sepr && entry->str[0])
1435 block->flag |= UI_BLOCK_NO_FLIP;
1439 split= uiLayoutSplit(layout, 0, 0);
1441 for(a=0, b=0; a<md->nitems; a++, b++) {
1442 if(block->flag & UI_BLOCK_NO_FLIP)
1443 entry= &md->items[a];
1445 entry= &md->items[md->nitems-a-1];
1447 /* new column on N rows or on separation label */
1448 if((b % rows == 0) || (entry->sepr && entry->str[0])) {
1449 column= uiLayoutColumn(split, 0);
1454 uiItemL(column, entry->str, entry->icon);
1455 bt= block->buttons.last;
1456 bt->flag= UI_TEXT_LEFT;
1458 else if(entry->icon) {
1459 uiDefIconTextButF(block, BUTM|FLO, B_NOP, entry->icon, entry->str, 0, 0,
1460 UI_UNIT_X*5, UI_UNIT_Y, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1463 uiDefButF(block, BUTM|FLO, B_NOP, entry->str, 0, 0,
1464 UI_UNIT_X*5, UI_UNIT_X, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1471 void ui_block_func_ICONROW(bContext *C, uiLayout *layout, void *arg_but)
1473 uiBlock *block= uiLayoutGetBlock(layout);
1474 uiPopupBlockHandle *handle= block->handle;
1475 uiBut *but= arg_but;
1478 uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
1480 for(a=(int)but->hardmin; a<=(int)but->hardmax; a++)
1481 uiDefIconButF(block, BUTM|FLO, B_NOP, but->icon+(a-but->hardmin), 0, 0, UI_UNIT_X*5, UI_UNIT_Y,
1482 &handle->retvalue, (float)a, 0.0, 0, 0, "");
1485 void ui_block_func_ICONTEXTROW(bContext *C, uiLayout *layout, void *arg_but)
1487 uiBlock *block= uiLayoutGetBlock(layout);
1488 uiPopupBlockHandle *handle= block->handle;
1489 uiBut *but= arg_but, *bt;
1494 uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
1496 md= decompose_menu_string(but->str);
1500 bt= uiDefBut(block, LABEL, 0, md->title, 0, 0, UI_UNIT_X*5, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1501 bt->flag= UI_TEXT_LEFT;
1504 /* loop through the menu options and draw them out with icons & text labels */
1505 for(a=0; a<md->nitems; a++) {
1506 entry= &md->items[md->nitems-a-1];
1511 uiDefIconTextButF(block, BUTM|FLO, B_NOP, (short)((but->icon)+(entry->retval-but->hardmin)), entry->str,
1512 0, 0, UI_UNIT_X*5, UI_UNIT_Y, &handle->retvalue, (float) entry->retval, 0.0, 0, 0, "");
1519 static void ui_warp_pointer(short x, short y)
1521 /* XXX 2.50 which function to use for this? */
1522 /* OSX has very poor mousewarp support, it sends events;
1523 this causes a menu being pressed immediately ... */
1530 /********************* Color Button ****************/
1532 /* picker sizes S hsize, F full size, D spacer, B button/pallette height */
1538 /* for picker, while editing hsv */
1539 void ui_set_but_hsv(uiBut *but)
1543 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
1544 ui_set_but_vectorf(but, col);
1547 /* also used by small picker, be careful with name checks below... */
1548 void ui_update_block_buts_rgb(uiBlock *block, float *rgb)
1553 rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1555 // this updates button strings, is hackish... but button pointers are on stack of caller function
1556 for(bt= block->buttons.first; bt; bt= bt->next) {
1559 ui_set_but_vectorf(bt, rgb);
1562 else if(strcmp(bt->str, "Hex: ")==0) {
1565 sprintf(col, "%02X%02X%02X", (unsigned int)(rgb[0]*255.0), (unsigned int)(rgb[1]*255.0), (unsigned int)(rgb[2]*255.0));
1567 strcpy(bt->poin, col);
1569 else if(bt->str[1]==' ') {
1570 if(bt->str[0]=='R') {
1571 ui_set_but_val(bt, rgb[0]);
1573 else if(bt->str[0]=='G') {
1574 ui_set_but_val(bt, rgb[1]);
1576 else if(bt->str[0]=='B') {
1577 ui_set_but_val(bt, rgb[2]);
1579 else if(bt->str[0]=='H') {
1580 ui_set_but_val(bt, hsv[0]);
1582 else if(bt->str[0]=='S') {
1583 ui_set_but_val(bt, hsv[1]);
1585 else if(bt->str[0]=='V') {
1586 ui_set_but_val(bt, hsv[2]);
1594 static void do_picker_rna_cb(bContext *C, void *bt1, void *unused)
1596 uiBut *but= (uiBut *)bt1;
1597 uiPopupBlockHandle *popup= but->block->handle;
1598 PropertyRNA *prop = but->rnaprop;
1599 PointerRNA ptr = but->rnapoin;
1603 RNA_property_float_get_array(&ptr, prop, rgb);
1604 ui_update_block_buts_rgb(but->block, rgb);
1608 popup->menuretval= UI_RETURN_UPDATE;
1611 static void do_hsv_rna_cb(bContext *C, void *bt1, void *hsv_arg)
1613 uiBut *but= (uiBut *)bt1;
1614 uiPopupBlockHandle *popup= but->block->handle;
1615 float *hsv = (float *)hsv_arg;
1618 hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
1620 ui_update_block_buts_rgb(but->block, rgb);
1623 popup->menuretval= UI_RETURN_UPDATE;
1626 static void do_hex_rna_cb(bContext *C, void *bt1, void *hexcl)
1628 uiBut *but= (uiBut *)bt1;
1629 uiPopupBlockHandle *popup= but->block->handle;
1630 char *hexcol= (char *)hexcl;
1633 hex_to_rgb(hexcol, rgb, rgb+1, rgb+2);
1635 ui_update_block_buts_rgb(but->block, rgb);
1638 popup->menuretval= UI_RETURN_UPDATE;
1641 static void close_popup_cb(bContext *C, void *bt1, void *arg)
1643 uiBut *but= (uiBut *)bt1;
1644 uiPopupBlockHandle *popup= but->block->handle;
1647 popup->menuretval= UI_RETURN_OK;
1650 static void picker_new_hide_reveal(uiBlock *block, short colormode)
1655 for(bt= block->buttons.first; bt; bt= bt->next) {
1657 if(bt->type==NUMSLI || bt->type==TEX) {
1658 if( bt->str[1]=='e') {
1659 if(colormode==2) bt->flag &= ~UI_HIDDEN;
1660 else bt->flag |= UI_HIDDEN;
1662 else if( ELEM3(bt->str[0], 'R', 'G', 'B')) {
1663 if(colormode==0) bt->flag &= ~UI_HIDDEN;
1664 else bt->flag |= UI_HIDDEN;
1666 else if( ELEM3(bt->str[0], 'H', 'S', 'V')) {
1667 if(colormode==1) bt->flag &= ~UI_HIDDEN;
1668 else bt->flag |= UI_HIDDEN;
1674 static void do_picker_new_mode_cb(bContext *C, void *bt1, void *colv)
1677 short colormode= ui_get_but_val(bt);
1678 picker_new_hide_reveal(bt->block, colormode);
1681 /* picker sizes S hsize, F full size, D spacer, B button/pallette height */
1682 #define SPICK1 150.0
1685 #define PICKER_H 150
1686 #define PICKER_W 150
1687 #define PICKER_SPACE 6
1688 #define PICKER_BAR 14
1690 #define PICKER_TOTAL_W (PICKER_W+PICKER_SPACE+PICKER_BAR)
1692 static void circle_picker(uiBlock *block, PointerRNA *ptr, const char *propname)
1697 bt= uiDefButR(block, HSVCIRCLE, 0, "", 0, 0, PICKER_H, PICKER_W, ptr, propname, -1, 0.0, 0.0, 0, 0, "");
1698 uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1701 uiDefButR(block, HSVCUBE, 0, "", PICKER_W+PICKER_SPACE,0,PICKER_BAR,PICKER_H, ptr, propname, -1, 0.0, 0.0, 9, 0, "");
1702 uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1706 static void square_picker(uiBlock *block, PointerRNA *ptr, const char *propname, int type)
1709 int bartype = type + 3;
1712 bt= uiDefButR(block, HSVCUBE, 0, "", 0, PICKER_BAR+PICKER_SPACE, PICKER_TOTAL_W, PICKER_H, ptr, propname, -1, 0.0, 0.0, type, 0, "");
1713 uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1716 uiDefButR(block, HSVCUBE, 0, "", 0, 0, PICKER_TOTAL_W, PICKER_BAR, ptr, propname, -1, 0.0, 0.0, bartype, 0, "");
1717 uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1721 /* a HS circle, V slider, rgb/hsv/hex sliders */
1722 static void uiBlockPicker(uiBlock *block, float *rgb, PointerRNA *ptr, PropertyRNA *prop)
1724 static short colormode= 0; /* temp? 0=rgb, 1=hsv, 2=hex */
1726 int width, butwidth;
1727 static char tip[50];
1728 static float hsv[3];
1729 static char hexcol[128];
1730 const char *propname = RNA_property_identifier(prop);
1732 width= PICKER_TOTAL_W;
1733 butwidth = width - UI_UNIT_X - 10;
1735 /* existence of profile means storage is in linear colour space, with display correction */
1736 if (block->color_profile == BLI_PR_NONE)
1737 sprintf(tip, "Value in Display Color Space");
1739 sprintf(tip, "Value in Linear RGB Color Space");
1741 RNA_property_float_get_array(ptr, prop, rgb);
1742 rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1744 switch (U.color_picker_type) {
1745 case USER_CP_CIRCLE:
1746 circle_picker(block, ptr, propname);
1748 case USER_CP_SQUARE_SV:
1749 square_picker(block, ptr, propname, 0);
1751 case USER_CP_SQUARE_HS:
1752 square_picker(block, ptr, propname, 1);
1754 case USER_CP_SQUARE_HV:
1755 square_picker(block, ptr, propname, 2);
1760 uiBlockBeginAlign(block);
1761 bt= uiDefButS(block, ROW, 0, "RGB", 0, -30, width/3, UI_UNIT_Y, &colormode, 0.0, 0.0, 0, 0, "");
1762 uiButSetFunc(bt, do_picker_new_mode_cb, bt, rgb);
1763 bt= uiDefButS(block, ROW, 0, "HSV", width/3, -30, width/3, UI_UNIT_Y, &colormode, 0.0, 1.0, 0, 0, "");
1764 uiButSetFunc(bt, do_picker_new_mode_cb, bt, hsv);
1765 bt= uiDefButS(block, ROW, 0, "Hex", 2*width/3, -30, width/3, UI_UNIT_Y, &colormode, 0.0, 2.0, 0, 0, "");
1766 uiButSetFunc(bt, do_picker_new_mode_cb, bt, hexcol);
1767 uiBlockEndAlign(block);
1769 bt= uiDefIconButO(block, BUT, "UI_OT_eyedropper", WM_OP_INVOKE_DEFAULT, ICON_EYEDROPPER, butwidth+10, -60, UI_UNIT_X, UI_UNIT_Y, NULL);
1770 uiButSetFunc(bt, close_popup_cb, bt, NULL);
1773 uiBlockBeginAlign(block);
1774 bt= uiDefButR(block, NUMSLI, 0, "R ", 0, -60, butwidth, UI_UNIT_Y, ptr, propname, 0, 0.0, 0.0, 0, 0, "");
1775 uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1776 bt= uiDefButR(block, NUMSLI, 0, "G ", 0, -80, butwidth, UI_UNIT_Y, ptr, propname, 1, 0.0, 0.0, 0, 0, "");
1777 uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1778 bt= uiDefButR(block, NUMSLI, 0, "B ", 0, -100, butwidth, UI_UNIT_Y, ptr, propname, 2, 0.0, 0.0, 0, 0, "");
1779 uiButSetFunc(bt, do_picker_rna_cb, bt, NULL);
1780 // could use uiItemFullR(col, "", 0, ptr, prop, -1, 0, UI_ITEM_R_EXPAND|UI_ITEM_R_SLIDER);
1781 // but need to use uiButSetFunc for updating other fake buttons
1784 uiBlockBeginAlign(block);
1785 bt= uiDefButF(block, NUMSLI, 0, "H ", 0, -60, butwidth, UI_UNIT_Y, hsv, 0.0, 1.0, 10, 3, "");
1786 uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
1787 bt= uiDefButF(block, NUMSLI, 0, "S ", 0, -80, butwidth, UI_UNIT_Y, hsv+1, 0.0, 1.0, 10, 3, "");
1788 uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
1789 bt= uiDefButF(block, NUMSLI, 0, "V ", 0, -100, butwidth, UI_UNIT_Y, hsv+2, 0.0, 1.0, 10, 3, "");
1790 uiButSetFunc(bt, do_hsv_rna_cb, bt, hsv);
1791 uiBlockEndAlign(block);
1793 rgb_to_hsv(rgb[0], rgb[1], rgb[2], hsv, hsv+1, hsv+2);
1794 sprintf(hexcol, "%02X%02X%02X", (unsigned int)(rgb[0]*255.0), (unsigned int)(rgb[1]*255.0), (unsigned int)(rgb[2]*255.0));
1796 bt= uiDefBut(block, TEX, 0, "Hex: ", 0, -60, butwidth, UI_UNIT_Y, hexcol, 0, 8, 0, 0, "Hex triplet for color (#RRGGBB)");
1797 uiButSetFunc(bt, do_hex_rna_cb, bt, hexcol);
1799 picker_new_hide_reveal(block, colormode);
1803 static int ui_picker_small_wheel(const bContext *C, uiBlock *block, wmEvent *event)
1807 if(event->type==WHEELUPMOUSE)
1809 else if(event->type==WHEELDOWNMOUSE)
1815 for(but= block->buttons.first; but; but= but->next) {
1816 if(but->type==HSVCUBE && but->active==NULL) {
1817 uiPopupBlockHandle *popup= block->handle;
1820 ui_get_but_vectorf(but, col);
1822 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
1823 but->hsv[2]= CLAMPIS(but->hsv[2]+add, 0.0f, 1.0f);
1824 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
1826 ui_set_but_vectorf(but, col);
1828 ui_update_block_buts_rgb(block, col);
1830 popup->menuretval= UI_RETURN_UPDATE;
1839 uiBlock *ui_block_func_COL(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
1841 uiBut *but= arg_but;
1844 block= uiBeginBlock(C, handle->region, "colorpicker", UI_EMBOSS);
1847 if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA) {
1848 block->color_profile = BLI_PR_NONE;
1852 uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
1854 VECCOPY(handle->retvec, but->editvec);
1856 uiBlockPicker(block, handle->retvec, &but->rnapoin, but->rnaprop);
1857 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_KEEP_OPEN;
1858 uiBoundsBlock(block, 10);
1860 block->block_event_func= ui_picker_small_wheel;
1863 block->direction= UI_TOP;
1868 /************************ Popup Menu Memory ****************************/
1870 static int ui_popup_menu_hash(char *str)
1872 return BLI_ghashutil_strhash(str);
1875 /* but == NULL read, otherwise set */
1876 uiBut *ui_popup_menu_memory(uiBlock *block, uiBut *but)
1878 static char mem[256], first=1;
1879 int hash= block->puphash;
1883 memset(mem, -1, sizeof(mem));
1889 mem[hash & 255 ]= BLI_findindex(&block->buttons, but);
1894 return BLI_findlink(&block->buttons, mem[hash & 255]);
1898 /******************** Popup Menu with callback or string **********************/
1900 struct uiPopupMenu {
1905 int mx, my, popup, slideout;
1906 int startx, starty, maxrow;
1908 uiMenuCreateFunc menu_func;
1912 static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, void *arg_pup)
1918 uiPopupMenu *pup= arg_pup;
1919 int offset, direction, minwidth, flip;
1921 if(pup->menu_func) {
1922 pup->block->handle= handle;
1923 pup->menu_func(C, pup->layout, pup->menu_arg);
1924 pup->block->handle= NULL;
1928 /* minimum width to enforece */
1929 minwidth= pup->but->x2 - pup->but->x1;
1931 if(pup->but->type == PULLDOWN || pup->but->menu_create_func) {
1948 /* in some cases we create the block before the region,
1949 so we set it delayed here if necessary */
1950 if(BLI_findindex(&handle->region->uiblocks, block) == -1)
1951 uiBlockSetRegion(block, handle->region);
1953 block->direction= direction;
1955 uiBlockLayoutResolve(block, NULL, NULL);
1957 uiBlockSetFlag(block, UI_BLOCK_MOVEMOUSE_QUIT);
1960 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_RET_1);
1961 uiBlockSetDirection(block, direction);
1963 /* offset the mouse position, possibly based on earlier selection */
1964 offset= 1.5*MENU_BUTTON_HEIGHT;
1966 if(block->flag & UI_BLOCK_POPUP_MEMORY) {
1967 bt= ui_popup_menu_memory(block, NULL);
1970 offset= -bt->y1 - 0.5f*MENU_BUTTON_HEIGHT;
1973 block->minbounds= minwidth;
1974 uiMenuPopupBoundsBlock(block, 1, 20, offset);
1977 /* for a header menu we set the direction automatic */
1978 if(!pup->slideout && flip) {
1980 ar= CTX_wm_region(C);
1982 if(sa && sa->headertype==HEADERDOWN) {
1983 if(ar && ar->regiontype == RGN_TYPE_HEADER) {
1984 uiBlockSetDirection(block, UI_TOP);
1985 uiBlockFlipOrder(block);
1990 block->minbounds= minwidth;
1991 uiTextBoundsBlock(block, 50);
1994 /* if menu slides out of other menu, override direction */
1996 uiBlockSetDirection(block, UI_RIGHT);
1998 uiEndBlock(C, block);
2003 uiPopupBlockHandle *ui_popup_menu_create(bContext *C, ARegion *butregion, uiBut *but, uiMenuCreateFunc menu_func, void *arg, char *str)
2005 wmWindow *window= CTX_wm_window(C);
2006 uiStyle *style= U.uistyles.first;
2007 uiPopupBlockHandle *handle;
2010 pup= MEM_callocN(sizeof(uiPopupMenu), "menu dummy");
2011 pup->block= uiBeginBlock(C, NULL, "ui_button_menu_create", UI_EMBOSSP);
2012 pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
2013 pup->slideout= (but && (but->block->flag & UI_BLOCK_LOOP));
2015 uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
2018 /* no button to start from, means we are a popup */
2019 pup->mx= window->eventstate->x;
2020 pup->my= window->eventstate->y;
2025 /* menu is created from a string */
2026 pup->menu_func= ui_block_func_MENUSTR;
2028 // XXX pup->block->flag |= UI_BLOCK_NO_FLIP;
2031 /* menu is created from a callback */
2032 pup->menu_func= menu_func;
2036 handle= ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPUP, pup);
2041 UI_add_popup_handlers(C, &window->modalhandlers, handle);
2042 WM_event_add_mousemove(C);
2050 /******************** Popup Menu API with begin and end ***********************/
2052 /* only return handler, and set optional title */
2053 uiPopupMenu *uiPupMenuBegin(bContext *C, const char *title, int icon)
2055 uiStyle *style= U.uistyles.first;
2056 uiPopupMenu *pup= MEM_callocN(sizeof(uiPopupMenu), "popup menu");
2059 pup->block= uiBeginBlock(C, NULL, "uiPupMenuBegin", UI_EMBOSSP);
2060 pup->block->flag |= UI_BLOCK_POPUP_MEMORY;
2061 pup->block->puphash= ui_popup_menu_hash((char*)title);
2062 pup->layout= uiBlockLayout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, style);
2063 uiLayoutSetOperatorContext(pup->layout, WM_OP_EXEC_REGION_WIN);
2065 /* create in advance so we can let buttons point to retval already */
2066 pup->block->handle= MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
2068 /* create title button */
2069 if(title && title[0]) {
2073 sprintf(titlestr, " %s", title);
2074 uiDefIconTextBut(pup->block, LABEL, 0, icon, titlestr, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2077 but= uiDefBut(pup->block, LABEL, 0, (char*)title, 0, 0, 200, MENU_BUTTON_HEIGHT, NULL, 0.0, 0.0, 0, 0, "");
2078 but->flag= UI_TEXT_LEFT;
2085 /* set the whole structure to work */
2086 void uiPupMenuEnd(bContext *C, uiPopupMenu *pup)
2088 wmWindow *window= CTX_wm_window(C);
2089 uiPopupBlockHandle *menu;
2092 pup->mx= window->eventstate->x;
2093 pup->my= window->eventstate->y;
2095 menu= ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_POPUP, pup);
2098 UI_add_popup_handlers(C, &window->modalhandlers, menu);
2099 WM_event_add_mousemove(C);
2104 uiLayout *uiPupMenuLayout(uiPopupMenu *pup)
2109 /*************************** Standard Popup Menus ****************************/
2111 static void operator_name_cb(bContext *C, void *arg, int retval)
2113 const char *opname= arg;
2115 if(opname && retval > 0)
2116 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
2119 static void operator_cb(bContext *C, void *arg, int retval)
2121 wmOperator *op= arg;
2123 if(op && retval > 0)
2124 WM_operator_call(C, op);
2126 WM_operator_free(op);
2129 static void confirm_cancel_operator(void *opv)
2131 WM_operator_free(opv);
2134 static void vconfirm_opname(bContext *C, char *opname, char *title, char *itemfmt, va_list ap)
2136 uiPopupBlockHandle *handle;
2140 if (title) s+= sprintf(s, "%s%%t|", title);
2141 vsprintf(s, itemfmt, ap);
2143 handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
2145 handle->popup_func= operator_name_cb;
2146 handle->popup_arg= opname;
2149 static void confirm_operator(bContext *C, wmOperator *op, char *title, char *item)
2151 uiPopupBlockHandle *handle;
2155 if (title) s+= sprintf(s, "%s%%t|%s", title, item);
2157 handle= ui_popup_menu_create(C, NULL, NULL, NULL, NULL, buf);
2159 handle->popup_func= operator_cb;
2160 handle->popup_arg= op;
2161 handle->cancel_func= confirm_cancel_operator;
2164 void uiPupMenuOkee(bContext *C, char *opname, char *str, ...)
2169 sprintf(titlestr, "OK? %%i%d", ICON_QUESTION);
2172 vconfirm_opname(C, opname, titlestr, str, ap);
2176 void uiPupMenuSaveOver(bContext *C, wmOperator *op, char *filename)
2178 size_t len= strlen(filename);
2183 if(filename[len-1]=='/' || filename[len-1]=='\\') {
2184 uiPupMenuError(C, "Cannot overwrite a directory");
2185 WM_operator_free(op);
2188 if(BLI_exists(filename)==0)
2189 operator_cb(C, op, 1);
2191 confirm_operator(C, op, "Save Over", filename);
2194 void uiPupMenuNotice(bContext *C, char *str, ...)
2199 vconfirm_opname(C, NULL, NULL, str, ap);
2203 void uiPupMenuError(bContext *C, char *str, ...)
2209 sprintf(titlestr, "Error %%i%d", ICON_ERROR);
2211 sprintf(nfmt, "%s", str);
2214 vconfirm_opname(C, NULL, titlestr, nfmt, ap);
2218 void uiPupMenuReports(bContext *C, ReportList *reports)
2224 if(!reports || !reports->list.first)
2226 if(!CTX_wm_window(C))
2229 ds= BLI_dynstr_new();
2231 for(report=reports->list.first; report; report=report->next) {
2232 if(report->type <= reports->printlevel)
2234 else if(report->type >= RPT_ERROR)
2235 BLI_dynstr_appendf(ds, "Error %%i%d%%t|%s", ICON_ERROR, report->message);
2236 else if(report->type >= RPT_WARNING)
2237 BLI_dynstr_appendf(ds, "Warning %%i%d%%t|%s", ICON_ERROR, report->message);
2238 else if(report->type >= RPT_INFO)
2239 BLI_dynstr_appendf(ds, "Info %%t|%s", report->message);
2242 str= BLI_dynstr_get_cstring(ds);
2243 ui_popup_menu_create(C, NULL, NULL, NULL, NULL, str);
2246 BLI_dynstr_free(ds);
2249 void uiPupMenuInvoke(bContext *C, const char *idname)
2254 MenuType *mt= WM_menutype_find(idname, TRUE);
2257 printf("uiPupMenuInvoke: named menu \"%s\" not found\n", idname);
2261 if(mt->poll && mt->poll(C, mt)==0)
2264 pup= uiPupMenuBegin(C, mt->label, 0);
2265 layout= uiPupMenuLayout(pup);
2267 menu.layout= layout;
2272 uiPupMenuEnd(C, pup);
2276 /*************************** Popup Block API **************************/
2278 void uiPupBlockO(bContext *C, uiBlockCreateFunc func, void *arg, char *opname, int opcontext)
2280 wmWindow *window= CTX_wm_window(C);
2281 uiPopupBlockHandle *handle;
2283 handle= ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
2285 handle->optype= (opname)? WM_operatortype_find(opname, 0): NULL;
2286 handle->opcontext= opcontext;
2288 UI_add_popup_handlers(C, &window->modalhandlers, handle);
2289 WM_event_add_mousemove(C);
2292 void uiPupBlock(bContext *C, uiBlockCreateFunc func, void *arg)
2294 uiPupBlockO(C, func, arg, NULL, 0);
2297 void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int opcontext)
2299 wmWindow *window= CTX_wm_window(C);
2300 uiPopupBlockHandle *handle;
2302 handle= ui_popup_block_create(C, NULL, NULL, func, NULL, op);
2304 handle->retvalue= 1;
2306 handle->popup_arg= op;
2307 handle->popup_func= operator_cb;
2308 handle->cancel_func= confirm_cancel_operator;
2309 handle->opcontext= opcontext;
2311 UI_add_popup_handlers(C, &window->modalhandlers, handle);
2312 WM_event_add_mousemove(C);
2315 void uiPupBlockClose(bContext *C, uiBlock *block)
2318 UI_remove_popup_handlers(&CTX_wm_window(C)->modalhandlers, block->handle);
2319 ui_popup_block_free(C, block->handle);