4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
34 a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt
54 #include "MEM_guardedalloc.h"
59 #include "BIF_language.h"
62 #endif // INTERNATIONAL
64 #include "BLI_blenlib.h"
65 #include "BLI_arithb.h"
67 #include "DNA_screen_types.h"
68 #include "DNA_space_types.h"
69 #include "DNA_userdef_types.h"
70 #include "DNA_vec_types.h"
72 #include "BKE_blender.h"
73 #include "BKE_utildefines.h"
74 #include "BKE_global.h"
77 #include "BIF_graphics.h"
78 #include "BIF_keyval.h"
79 #include "BIF_mainqueue.h"
80 #include "BIF_resources.h"
81 #include "BIF_screen.h"
82 #include "BIF_toolbox.h"
83 #include "BIF_mywindow.h"
84 #include "BIF_space.h"
85 #include "BIF_glutil.h"
86 #include "BIF_interface.h"
87 #include "BIF_butspace.h"
92 #include "interface.h"
95 /* naming conventions:
97 * uiBlahBlah() external function
98 * ui_blah_blah() internal function
102 /* ************ GLOBALS ************* */
104 float UIwinmat[4][4];
105 static int UIlock= 0, UIafterval;
106 static char *UIlockstr=NULL;
107 static void (*UIafterfunc)(void *arg, int event);
108 static void *UIafterfunc_arg;
110 static uiFont UIfont[UI_ARRAY]; // no init needed
113 /* ************* PROTOTYPES ***************** */
115 static void ui_set_but_val(uiBut *but, double value);
116 static void ui_set_ftf_font(uiBlock *block);
117 static void ui_do_but_tip(uiBut *buttip);
119 /* ****************************** */
121 static int uibut_contains_pt(uiBut *but, short *pt)
123 return ((but->x1<pt[0] && but->x2>=pt[0]) &&
124 (but->y1<pt[1] && but->y2>=pt[1]));
127 static void uibut_do_func(uiBut *but)
130 but->func(but->func_arg1, but->func_arg2);
134 /* ************* window matrix ************** */
137 void ui_graphics_to_window(int win, float *x, float *y) /* for rectwrite */
141 int getsizex, getsizey;
143 bwin_getsize(win, &getsizex, &getsizey);
144 bwin_getsuborigin(win, &sx, &sy);
148 *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*UIwinmat[0][0]+ gy*UIwinmat[1][0]+ UIwinmat[3][0]));
149 *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*UIwinmat[0][1]+ gy*UIwinmat[1][1]+ UIwinmat[3][1]));
154 void ui_window_to_graphics(int win, float *x, float *y) /* for mouse cursor */
156 float a, b, c, d, e, f, px, py;
157 int getsizex, getsizey;
159 bwin_getsize(win, &getsizex, &getsizey);
161 a= .5*((float)getsizex)*UIwinmat[0][0];
162 b= .5*((float)getsizex)*UIwinmat[1][0];
163 c= .5*((float)getsizex)*(1.0+UIwinmat[3][0]);
165 d= .5*((float)getsizey)*UIwinmat[0][1];
166 e= .5*((float)getsizey)*UIwinmat[1][1];
167 f= .5*((float)getsizey)*(1.0+UIwinmat[3][1]);
172 *y= (a*(py-f) + d*(c-px))/(a*e-d*b);
173 *x= (px- b*(*y)- c)/a;
178 /* ************* SAVE UNDER ************ */
182 OverDraw *ui_begin_overdraw(int minx, int miny, int maxx, int maxy);
183 - enforces mainwindow to become active
184 - grabs copy from frontbuffer, pastes in back
186 void ui_flush_overdraw(OverDraw *od);
187 - copies backbuffer to front
189 void ui_refresh_overdraw(Overdraw *od);
190 - pastes in back copy of frontbuffer again for fresh drawing
192 void ui_end_overdraw(OverDraw *od);
193 - puts back on frontbuffer saved image
195 - sets back active blender area
196 - signals backbuffer to be corrupt (sel buffer!)
200 /* frontbuffer updates now glCopyPixels too, with block->flush rect */
202 /* new idea for frontbuffer updates:
204 - hilites: with blended poly?
206 - full updates... thats harder, but:
208 - before draw, always paste to backbuf
210 - always end with redraw event for full update
214 static void myglCopyPixels(int a, int b, int c, int d, int e)
217 unsigned int *buf= MEM_mallocN(4*c*d, "temp glcopypixels");
218 glReadPixels(a, b, c, d, GL_RGBA, GL_UNSIGNED_BYTE, buf);
219 glDrawPixels(c, d, GL_RGBA, GL_UNSIGNED_BYTE, buf);
222 else glCopyPixels(a, b, c, d, e);
226 short x, y, sx, sy, oldwin;
231 static uiOverDraw *ui_begin_overdraw(int minx, int miny, int maxx, int maxy)
235 // dirty patch removed for sun and sgi to mywindow.c commented out
237 /* clip with actual window size */
238 if(minx < 0) minx= 0;
239 if(miny < 0) miny= 0;
240 if(maxx >= G.curscreen->sizex) maxx= G.curscreen->sizex-1;
241 if(maxy >= G.curscreen->sizey) maxy= G.curscreen->sizey-1;
243 if(minx<maxx && miny<maxy) {
244 od= MEM_callocN(sizeof(uiOverDraw), "overdraw");
250 od->rect= MEM_mallocN(od->sx*od->sy*4, "temp_frontbuffer_image");
252 od->oldwin= mywinget();
253 mywinset(G.curscreen->mainwin);
255 glReadBuffer(GL_FRONT);
256 glReadPixels(od->x, od->y, od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
257 glReadBuffer(GL_BACK);
259 glDisable(GL_DITHER);
260 glRasterPos2f(od->x, od->y);
261 glDrawPixels(od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
268 static void ui_flush_overdraw(uiOverDraw *od)
272 glDisable(GL_DITHER);
273 glReadBuffer(GL_BACK);
274 glDrawBuffer(GL_FRONT);
275 glRasterPos2s(od->x, od->y);
276 myglCopyPixels(od->x, od->y, od->sx, od->sy, GL_COLOR);
279 glDrawBuffer(GL_BACK);
282 /* special flush version to enable transparent menus */
283 static void ui_block_flush_overdraw(uiBlock *block)
286 if(block->flag & UI_BLOCK_LOOP) {
289 BIF_GetThemeColor4ubv(TH_MENU_BACK, col);
292 uiOverDraw *od= block->overdraw;
294 /* completely draw all! */
295 glRasterPos2s(od->x, od->y);
296 glDrawPixels(od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
298 uiDrawMenuBox(block->minx, block->miny, block->maxx, block->maxy, block->flag);
299 for (bt= block->buttons.first; bt; bt= bt->next) {
305 ui_flush_overdraw(block->overdraw);
308 static void ui_end_overdraw(uiOverDraw *od)
312 glDisable(GL_DITHER);
315 glRasterPos2s(od->x, od->y);
316 glDrawPixels(od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
319 glDrawBuffer(GL_FRONT);
320 glRasterPos2s(od->x, od->y);
321 glDrawPixels(od->sx, od->sy, GL_RGBA, GL_UNSIGNED_BYTE, od->rect);
324 glDrawBuffer(GL_BACK);
327 if(od->oldwin) mywinset(od->oldwin);
332 markdirty_all_back(); // sets flags only
335 /* ****************** live updates for hilites and button presses *********** */
337 void ui_block_flush_back(uiBlock *block)
339 int minx, miny, sizex, sizey;
341 /* note; this routine also has to work for block loop */
342 if(block->needflush==0) return;
344 /* exception, when we cannot use backbuffer for draw... */
345 if(block->flag & UI_BLOCK_FRONTBUFFER) {
347 glDrawBuffer(GL_BACK);
352 /* copy pixels works on window coords, so we move to window space */
354 ui_graphics_to_window(block->win, &block->flush.xmin, &block->flush.ymin);
355 ui_graphics_to_window(block->win, &block->flush.xmax, &block->flush.ymax);
356 minx= floor(block->flush.xmin);
357 miny= floor(block->flush.ymin);
358 sizex= ceil(block->flush.xmax-block->flush.xmin);
359 sizey= ceil(block->flush.ymax-block->flush.ymin);
361 if(sizex>0 && sizey>0) {
363 mywinset(G.curscreen->mainwin);
365 glDisable(GL_DITHER);
366 glReadBuffer(GL_BACK);
367 glDrawBuffer(GL_FRONT);
368 glRasterPos2i(minx, miny);
370 myglCopyPixels(minx, miny+1, sizex, sizey, GL_COLOR);
372 myglCopyPixels(minx, miny, sizex, sizey, GL_COLOR);
376 glDrawBuffer(GL_BACK);
378 mywinset(block->win);
381 markdirty_win_back(block->win);
387 /* merge info for live updates in frontbuf */
388 void ui_block_set_flush(uiBlock *block, uiBut *but)
394 block->flush.xmin= 0.0;
395 block->flush.xmax= 0.0;
398 /* exception, when we cannot use backbuffer for draw... */
399 if(block->flag & UI_BLOCK_FRONTBUFFER) {
400 glDrawBuffer(GL_FRONT);
402 else if(block->needflush==0) {
404 block->flush.xmin= but->x1;
405 block->flush.xmax= but->x2;
406 block->flush.ymin= but->y1;
407 block->flush.ymax= but->y2;
412 if(block->flush.xmin > but->x1) block->flush.xmin= but->x1;
413 if(block->flush.xmax < but->x2) block->flush.xmax= but->x2;
414 if(block->flush.ymin > but->y1) block->flush.ymin= but->y1;
415 if(block->flush.ymax < but->y2) block->flush.ymax= but->y2;
423 /* ******************* copy and paste ******************** */
425 /* c = copy, v = paste */
426 /* return 1 when something changed */
427 static int ui_but_copy_paste(uiBut *but, char mode)
429 static char str[256]="";
430 static double butval=0.0;
437 if ELEM3(but->type, NUM, NUMSLI, HSVSLI) {
440 butval= ui_get_but_val(but);
443 ui_set_but_val(but, butval);
448 else if(but->type==COL) {
451 if(but->pointype==FLO) {
452 float *fp= (float *) poin;
457 else if (but->pointype==CHA) {
458 char *cp= (char *) poin;
459 rgb[0]= (float)(cp[0]/255.0);
460 rgb[1]= (float)(cp[1]/255.0);
461 rgb[2]= (float)(cp[2]/255.0);
466 if(but->pointype==FLO) {
467 float *fp= (float *) poin;
473 else if (but->pointype==CHA) {
474 char *cp= (char *) poin;
475 cp[0] = (char)(rgb[0]*255.0);
476 cp[1] = (char)(rgb[1]*255.0);
477 cp[2] = (char)(rgb[2]*255.0);
484 else if(but->type==TEX) {
487 strncpy(str, but->poin, but->max);
490 strncpy(but->poin, str, but->max);
496 else if(but->type==IDPOIN) {
499 ID *id= *but->idpoin_idpp;
500 if(id) strncpy(str, id->name+2, 22);
503 but->idpoin_func(str, but->idpoin_idpp);
513 /* ******************* block calc ************************* */
515 void uiTextBoundsBlock(uiBlock *block, int addval)
520 bt= block->buttons.first;
523 int transopts= (U.transopts & USER_TR_BUTTONS);
524 if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
525 j= BIF_GetStringWidth(bt->font, bt->drawstr, transopts);
533 bt= block->buttons.first;
536 ui_check_but(bt); // clips text again
542 void uiBoundsBlock(uiBlock *block, int addval)
547 if(block->buttons.first==NULL) {
549 block->minx= 0.0; block->maxx= block->panel->sizex;
550 block->miny= 0.0; block->maxy= block->panel->sizey;
555 block->minx= block->miny= 10000;
556 block->maxx= block->maxy= -10000;
558 bt= block->buttons.first;
560 if(bt->x1 < block->minx) block->minx= bt->x1;
561 if(bt->y1 < block->miny) block->miny= bt->y1;
563 if(bt->x2 > block->maxx) block->maxx= bt->x2;
564 if(bt->y2 > block->maxy) block->maxy= bt->y2;
569 block->minx -= addval;
570 block->miny -= addval;
571 block->maxx += addval;
572 block->maxy += addval;
575 /* hardcoded exception... but that one is annoying with larger safety */
576 bt= block->buttons.first;
577 if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
580 block->safety.xmin= block->minx-xof;
581 block->safety.ymin= block->miny-xof;
582 block->safety.xmax= block->maxx+xof;
583 block->safety.ymax= block->maxy+xof;
586 static void ui_positionblock(uiBlock *block, uiBut *but)
588 /* position block relative to but */
591 int xsize, ysize, xof=0, yof=0, centre;
592 short dir1= 0, dir2=0;
594 /* first transform to screen coords, assuming matrix is stil OK */
595 /* the UIwinmat is in panelspace */
597 butrct.xmin= but->x1; butrct.xmax= but->x2;
598 butrct.ymin= but->y1; butrct.ymax= but->y2;
600 ui_graphics_to_window(block->win, &butrct.xmin, &butrct.ymin);
601 ui_graphics_to_window(block->win, &butrct.xmax, &butrct.ymax);
602 block->parentrct= butrct; // will use that for pulldowns later
604 /* calc block rect */
605 if(block->buttons.first) {
606 block->minx= block->miny= 10000;
607 block->maxx= block->maxy= -10000;
609 bt= block->buttons.first;
611 if(bt->x1 < block->minx) block->minx= bt->x1;
612 if(bt->y1 < block->miny) block->miny= bt->y1;
614 if(bt->x2 > block->maxx) block->maxx= bt->x2;
615 if(bt->y2 > block->maxy) block->maxy= bt->y2;
621 /* we're nice and allow empty blocks too */
622 block->minx= block->miny= 0;
623 block->maxx= block->maxy= 20;
626 ui_graphics_to_window(block->win, &block->minx, &block->miny);
627 ui_graphics_to_window(block->win, &block->maxx, &block->maxy);
629 //block->minx-= 2.0; block->miny-= 2.0;
630 //block->maxx+= 2.0; block->maxy+= 2.0;
632 xsize= block->maxx - block->minx+4; // 4 for shadow
633 ysize= block->maxy - block->miny+4;
636 short left=0, right=0, top=0, down=0;
638 if(block->direction & UI_CENTRE) centre= ysize/2;
641 if( butrct.xmin-xsize > 0.0) left= 1;
642 if( butrct.xmax+xsize < G.curscreen->sizex) right= 1;
643 if( butrct.ymin-ysize+centre > 0.0) down= 1;
644 if( butrct.ymax+ysize-centre < G.curscreen->sizey) top= 1;
646 dir1= block->direction & UI_DIRECTION;
648 /* secundary directions */
649 if(dir1 & (UI_TOP|UI_DOWN)) {
650 if(dir1 & UI_LEFT) dir2= UI_LEFT;
651 else if(dir1 & UI_RIGHT) dir2= UI_RIGHT;
652 dir1 &= (UI_TOP|UI_DOWN);
655 if(dir2==0) if(dir1==UI_LEFT || dir1==UI_RIGHT) dir2= UI_DOWN;
656 if(dir2==0) if(dir1==UI_TOP || dir1==UI_DOWN) dir2= UI_LEFT;
658 /* no space at all? dont change */
660 if(dir1==UI_LEFT && left==0) dir1= UI_RIGHT;
661 if(dir1==UI_RIGHT && right==0) dir1= UI_LEFT;
662 /* this is aligning, not append! */
663 if(dir2==UI_LEFT && right==0) dir2= UI_RIGHT;
664 if(dir2==UI_RIGHT && left==0) dir2= UI_LEFT;
667 if(dir1==UI_TOP && top==0) dir1= UI_DOWN;
668 if(dir1==UI_DOWN && down==0) dir1= UI_TOP;
669 if(dir2==UI_TOP && top==0) dir2= UI_DOWN;
670 if(dir2==UI_DOWN && down==0) dir2= UI_TOP;
674 xof= butrct.xmin - block->maxx;
675 if(dir2==UI_TOP) yof= butrct.ymin - block->miny-centre;
676 else yof= butrct.ymax - block->maxy+centre;
678 else if(dir1==UI_RIGHT) {
679 xof= butrct.xmax - block->minx;
680 if(dir2==UI_TOP) yof= butrct.ymin - block->miny-centre;
681 else yof= butrct.ymax - block->maxy+centre;
683 else if(dir1==UI_TOP) {
684 yof= butrct.ymax - block->miny;
685 if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
686 else xof= butrct.xmin - block->minx;
687 // changed direction?
688 if((dir1 & block->direction)==0) {
689 if(block->direction & UI_SHIFT_FLIPPED)
690 xof+= dir2==UI_LEFT?25:-25;
691 uiBlockFlipOrder(block);
694 else if(dir1==UI_DOWN) {
695 yof= butrct.ymin - block->maxy;
696 if(dir2==UI_RIGHT) xof= butrct.xmax - block->maxx;
697 else xof= butrct.xmin - block->minx;
698 // changed direction?
699 if((dir1 & block->direction)==0) {
700 if(block->direction & UI_SHIFT_FLIPPED)
701 xof+= dir2==UI_LEFT?25:-25;
702 uiBlockFlipOrder(block);
706 /* and now we handle the exception; no space below or to right */
707 if(top==0 && down==0) {
708 if(dir1==UI_LEFT || dir1==UI_RIGHT) {
709 // align with bottom of screen
714 // apply requested offset in the block
715 xof += block->xofs/block->aspect;
716 yof += block->yofs/block->aspect;
721 bt= block->buttons.first;
724 ui_graphics_to_window(block->win, &bt->x1, &bt->y1);
725 ui_graphics_to_window(block->win, &bt->x2, &bt->y2);
733 // ui_check_but recalculates drawstring size in pixels
744 /* safety calculus */
746 float midx= (block->parentrct.xmin+block->parentrct.xmax)/2.0;
747 float midy= (block->parentrct.ymin+block->parentrct.ymax)/2.0;
749 /* when you are outside parent button, safety there should be smaller */
751 // parent button to left
752 if( midx < block->minx ) block->safety.xmin= block->minx-3;
753 else block->safety.xmin= block->minx-40;
754 // parent button to right
755 if( midx > block->maxx ) block->safety.xmax= block->maxx+3;
756 else block->safety.xmax= block->maxx+40;
758 // parent button on bottom
759 if( midy < block->miny ) block->safety.ymin= block->miny-3;
760 else block->safety.ymin= block->miny-40;
761 // parent button on top
762 if( midy > block->maxy ) block->safety.ymax= block->maxy+3;
763 else block->safety.ymax= block->maxy+40;
765 // exception for switched pulldowns...
766 if(dir1 && (dir1 & block->direction)==0) {
767 if(dir2==UI_RIGHT) block->safety.xmax= block->maxx+3;
768 if(dir2==UI_LEFT) block->safety.xmin= block->minx-3;
770 block->direction= dir1;
773 block->safety.xmin= block->minx-40;
774 block->safety.ymin= block->miny-40;
775 block->safety.xmax= block->maxx+40;
776 block->safety.ymax= block->maxy+40;
782 void ui_autofill(uiBlock *block)
785 float *maxw, *maxh, startx = 0, starty, height = 0;
787 int rows=0, /* cols=0, */ i, lasti;
789 /* first count rows */
790 but= block->buttons.last;
793 /* calculate max width / height for each row */
794 maxw= MEM_callocN(sizeof(float)*rows, "maxw");
795 maxh= MEM_callocN(sizeof(float)*rows, "maxh");
796 but= block->buttons.first;
799 if( maxh[i] < but->y2) maxh[i]= but->y2;
805 for(i=0; i<rows; i++) totmaxh+= maxh[i];
807 /* apply widths/heights */
809 but= block->buttons.first;
812 // signal for aligning code
813 but->flag |= UI_BUT_ALIGN_DOWN;
819 height= (maxh[i]*(block->maxy-block->miny))/totmaxh;
824 but->y1= starty+but->aspect;
825 but->y2= but->y1+height-but->aspect;
827 but->x2= (but->x2*(block->maxx-block->minx))/maxw[i];
828 but->x1= startx+but->aspect;
831 but->x2+= but->x1-but->aspect;
838 uiBlockEndAlign(block);
840 MEM_freeN(maxw); MEM_freeN(maxh);
844 /* ************** LINK LINE DRAWING ************* */
846 /* link line drawing is not part of buttons or theme.. so we stick with it here */
848 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
850 float vec1[2], vec2[2];
852 if(line->from==NULL || line->to==NULL) return;
854 vec1[0]= (line->from->x1+line->from->x2)/2.0;
855 vec1[1]= (line->from->y1+line->from->y2)/2.0;
856 vec2[0]= (line->to->x1+line->to->x2)/2.0;
857 vec2[1]= (line->to->y1+line->to->y2)/2.0;
859 if(line->flag & UI_SELECT) BIF_ThemeColorShade(but->themecol, 80);
860 else glColor3ub(0,0,0);
861 fdrawline(vec1[0], vec1[1], vec2[0], vec2[1]);
864 static void ui_draw_links(uiBlock *block)
869 but= block->buttons.first;
871 if(but->type==LINK && but->link) {
872 line= but->link->lines.first;
874 ui_draw_linkline(but, line);
882 /* ************** BLOCK DRAWING FUNCTION ************* */
885 void uiDrawBlock(uiBlock *block)
889 /* handle pending stuff */
890 if(block->autofill) ui_autofill(block);
891 if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
892 if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
894 uiPanelPush(block); // panel matrix
896 if(block->flag & UI_BLOCK_LOOP) {
897 uiDrawMenuBox(block->minx, block->miny, block->maxx, block->maxy, block->flag);
899 else if(block->panel) ui_draw_panel(block);
901 if(block->drawextra) block->drawextra();
903 for (but= block->buttons.first; but; but= but->next) {
907 ui_draw_links(block);
909 uiPanelPop(block); // matrix restored
912 /* ************* MENUBUTS *********** */
925 int nitems, itemssize;
928 static MenuData *menudata_new(char *instr) {
929 MenuData *md= MEM_mallocN(sizeof(*md), "MenuData");
934 md->nitems= md->itemssize= 0;
939 static void menudata_set_title(MenuData *md, char *title) {
944 static void menudata_add_item(MenuData *md, char *str, int retval, int icon) {
945 if (md->nitems==md->itemssize) {
946 int nsize= md->itemssize?(md->itemssize<<1):1;
947 MenuEntry *oitems= md->items;
949 md->items= MEM_mallocN(nsize*sizeof(*md->items), "md->items");
951 memcpy(md->items, oitems, md->nitems*sizeof(*md->items));
955 md->itemssize= nsize;
958 md->items[md->nitems].str= str;
959 md->items[md->nitems].retval= retval;
960 md->items[md->nitems].icon= icon;
964 static void menudata_free(MenuData *md) {
965 MEM_freeN(md->instr);
967 MEM_freeN(md->items);
972 * Parse menu description strings, string is of the
973 * form "[sss%t|]{(sss[%xNN]|), (%l|)}", ssss%t indicates the
974 * menu title, sss or sss%xNN indicates an option,
975 * if %xNN is given then NN is the return value if
976 * that option is selected otherwise the return value
977 * is the index of the option (starting with 1). %l
978 * indicates a seperator.
980 * @param str String to be parsed.
981 * @retval new menudata structure, free with menudata_free()
983 static MenuData *decompose_menu_string(char *str)
985 char *instr= BLI_strdup(str);
986 MenuData *md= menudata_new(instr);
987 char *nitem= NULL, *s= instr;
988 int nicon=0, nretval= 1, nitem_is_title= 0;
999 } else if (s[1]=='t') {
1004 } else if (s[1]=='l') {
1007 } else if (s[1]=='i') {
1011 } else if (c=='|' || c=='\0') {
1015 if (nitem_is_title) {
1016 menudata_set_title(md, nitem);
1019 menudata_add_item(md, nitem, nretval, nicon);
1020 nretval= md->nitems+1;
1038 static void ui_set_name_menu(uiBut *but, int value)
1043 md= decompose_menu_string(but->str);
1044 for (i=0; i<md->nitems; i++)
1045 if (md->items[i].retval==value)
1046 strcpy(but->drawstr, md->items[i].str);
1050 static void ui_warp_pointer(short x, short y)
1052 /* OSX has very poor mousewarp support, it sends events;
1053 this causes a menu being pressed immediately ... */
1060 static int ui_do_but_MENU(uiBut *but)
1064 ListBase listb={NULL, NULL}, lb;
1066 int width, height=0, a, xmax, starty;
1068 int columns=1, rows=0, boxh, event;
1069 short x1, y1, active= -1;
1073 but->flag |= UI_SELECT;
1075 ui_block_flush_back(but->block); // flush because this button creates own blocks loop
1077 block= uiNewBlock(&listb, "menu", UI_EMBOSSP, UI_HELV, but->win);
1078 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1079 block->themecol= TH_MENU_ITEM;
1081 md= decompose_menu_string(but->str);
1083 /* columns and row calculation */
1084 columns= (md->nitems+20)/20;
1085 if (columns<1) columns= 1;
1087 rows= (int) md->nitems/columns;
1088 if (rows<1) rows= 1;
1090 while (rows*columns<md->nitems) rows++;
1092 /* size and location */
1094 width= 1.5*but->aspect*strlen(md->title)+BIF_GetStringWidth(block->curfont, md->title, (U.transopts & USER_TR_MENUS));
1098 for(a=0; a<md->nitems; a++) {
1099 xmax= but->aspect*BIF_GetStringWidth(block->curfont, md->items[a].str, (U.transopts & USER_TR_MENUS));
1100 if(xmax>width) width= xmax;
1104 if (width < (but->x2 - but->x1)) width = (but->x2 - but->x1);
1105 if (width<50) width=50;
1110 if (md->title) height+= boxh;
1112 getmouseco_sc(mval);
1114 /* find active item */
1115 fvalue= ui_get_but_val(but);
1116 for(active=0; active<md->nitems; active++) {
1117 if( md->items[active].retval== (int)fvalue ) break;
1119 /* no active item? */
1120 if(active==md->nitems) {
1121 if(md->title) active= -1;
1125 /* for now disabled... works confusing because you think it's a title or so.... */
1134 uiSetCurFont(block, block->font+1);
1135 bt= uiDefBut(block, LABEL, 0, md->title, startx, (short)(starty+rows*boxh), (short)width, (short)boxh, NULL, 0.0, 0.0, 0, 0, "");
1136 uiSetCurFont(block, block->font);
1137 bt->flag= UI_TEXT_LEFT;
1140 for(a=0; a<md->nitems; a++) {
1142 x1= but->x1 + width*((int)(md->nitems-a-1)/rows);
1143 y1= but->y1 - boxh*(rows - ((md->nitems - a - 1)%rows)) + (rows*boxh);
1145 if (strcmp(md->items[md->nitems-a-1].str, "%l")==0) {
1146 uiDefBut(block, SEPR, B_NOP, "", x1, y1,(short)(width-(rows>1)), (short)(boxh-1), NULL, 0.0, 0.0, 0, 0, "");
1148 else if(md->items[md->nitems-a-1].icon) {
1149 uiBut *bt= uiDefIconTextBut(block, BUTM|but->pointype, but->retval, md->items[md->nitems-a-1].icon ,md->items[md->nitems-a-1].str, x1, y1,(short)(width-(rows>1)), (short)(boxh-1), but->poin, (float) md->items[md->nitems-a-1].retval, 0.0, 0, 0, "");
1150 if(active==a) bt->flag |= UI_ACTIVE;
1153 uiBut *bt= uiDefBut(block, BUTM|but->pointype, but->retval, md->items[md->nitems-a-1].str, x1, y1,(short)(width-(rows>1)), (short)(boxh-1), but->poin, (float) md->items[md->nitems-a-1].retval, 0.0, 0, 0, "");
1154 if(active==a) bt->flag |= UI_ACTIVE;
1158 /* the code up here has flipped locations, because of change of preferred order */
1159 /* thats why we have to switch list order too, to make arrowkeys work */
1161 lb.first= lb.last= NULL;
1162 bt= block->buttons.first;
1164 uiBut *next= bt->next;
1165 BLI_remlink(&block->buttons, bt);
1166 BLI_addhead(&lb, bt);
1172 block->direction= UI_TOP;
1173 ui_positionblock(block, but);
1175 /* blocks can come (and get scaled) from a normal window, now we go to screenspace */
1176 block->win= G.curscreen->mainwin;
1177 for(bt= block->buttons.first; bt; bt= bt->next) bt->win= block->win;
1178 bwin_getsinglematrix(block->win, block->winmat);
1180 event= uiDoBlocks(&listb, 0);
1184 but->flag &= ~UI_SELECT;
1195 /* ************* EVENTS ************* */
1197 void uiGetMouse(int win, short *adr)
1203 if (win == G.curscreen->mainwin) return;
1205 bwin_getsuborigin(win, &x, &y);
1213 ui_window_to_graphics(win, &xwin, &ywin);
1215 adr[0]= (short)(xwin+0.5);
1216 adr[1]= (short)(ywin+0.5);
1219 static void ui_is_but_sel(uiBut *but)
1223 short push=0, true=1;
1225 value= ui_get_but_val(but);
1227 if( but->type==TOGN ) true= 0;
1231 if( BTST(lvalue, (but->bitnr)) ) push= true;
1240 if (value==-1) push= 1;
1246 if(value!=but->min) push= 1;
1249 if(value==0.0) push= 1;
1252 if(value == but->max) push= 1;
1264 else if(push==1) but->flag |= UI_SELECT;
1265 else but->flag &= ~UI_SELECT;
1268 static int ui_do_but_BUT(uiBut *but)
1273 int oflag= but->flag;
1276 uiGetMouse(mywinget(), mval);
1278 if (uibut_contains_pt(but, mval))
1279 but->flag |= UI_SELECT;
1281 but->flag &= ~UI_SELECT;
1283 if (but->flag != oflag) {
1285 ui_block_flush_back(but->block);
1289 } while (get_mbut() & L_MOUSE);
1291 activated= (but->flag & UI_SELECT);
1297 but->flag &= ~UI_SELECT;
1300 return activated?but->retval:0;
1303 static int ui_do_but_KEYEVT(uiBut *but)
1305 unsigned short event= 0;
1308 /* flag for ui_check_but */
1309 ui_set_but_val(but, -1);
1312 ui_block_flush_back(but->block);
1315 event= extern_qread(&val);
1316 } while (!event || !val || ELEM(event, MOUSEX, MOUSEY));
1318 if (!key_event_to_string(event)[0]) event= 0;
1320 ui_set_but_val(but, (double) event);
1327 static int ui_do_but_TOG(uiBlock *block, uiBut *but)
1331 int w, lvalue, push;
1333 value= ui_get_but_val(but);
1337 w= BTST(lvalue, but->bitnr);
1338 if(w) lvalue = BCLR(lvalue, but->bitnr);
1339 else lvalue = BSET(lvalue, but->bitnr);
1341 if(but->type==TOGR) {
1342 if( (get_qual() & LR_SHIFTKEY)==0 ) {
1343 lvalue= 1<<(but->bitnr);
1345 ui_set_but_val(but, (double)lvalue);
1347 bt= block->buttons.first;
1349 if( bt!=but && bt->poin==but->poin ) {
1357 if(lvalue==0) lvalue= 1<<(but->bitnr);
1360 ui_set_but_val(but, (double)lvalue);
1361 if(but->type==ICONTOG) ui_check_but(but);
1362 // no frontbuffer draw for this one
1363 if((but->flag & UI_NO_HILITE)==0) ui_draw_but(but);
1367 if(value==0.0) push= 1;
1370 if(but->type==TOGN) push= !push;
1371 ui_set_but_val(but, (double)push);
1372 if(but->type==ICONTOG) ui_check_but(but);
1373 // no frontbuffer draw for this one
1374 if((but->flag & UI_NO_HILITE)==0) ui_draw_but(but);
1377 /* no while loop...this button is used for viewmove */
1384 static int ui_do_but_ROW(uiBlock *block, uiBut *but)
1388 ui_set_but_val(but, but->max);
1391 bt= block->buttons.first;
1393 if( bt!=but && bt->type==ROW ) {
1394 if(bt->min==but->min) {
1404 static int ui_do_but_TEX(uiBut *but)
1407 short x, mval[2], len=0, dodraw;
1408 char *str, backstr[UI_MAX_DRAW_STR];
1411 str= (char *)but->poin;
1413 but->flag |= UI_SELECT;
1415 uiGetMouse(mywinget(), mval);
1417 /* calculate cursor pos with current mousecoords */
1418 BLI_strncpy(backstr, but->drawstr, UI_MAX_DRAW_STR);
1419 but->pos= strlen(backstr)-but->ofs;
1421 while((but->aspect*BIF_GetStringWidth(but->font, backstr+but->ofs, 0) + but->x1) > mval[0]) {
1422 if (but->pos <= 0) break;
1424 backstr[but->pos+but->ofs] = 0;
1427 but->pos -= strlen(but->str);
1428 but->pos += but->ofs;
1429 if(but->pos<0) but->pos= 0;
1432 BLI_strncpy(backstr, but->poin, UI_MAX_DRAW_STR);
1435 ui_block_flush_back(but->block);
1437 while (get_mbut() & L_MOUSE) BIF_wait_for_statechange();
1447 dev = extern_qread_ext(&val, &ascii);
1449 if(dev==INPUTCHANGE) break;
1450 else if(get_mbut() & L_MOUSE) break;
1451 else if(get_mbut() & R_MOUSE) break;
1452 else if(dev==ESCKEY) break;
1453 else if(dev==MOUSEX) val= 0;
1454 else if(dev==MOUSEY) val= 0;
1457 if(len < but->max) {
1458 for(x= but->max; x>but->pos; x--)
1460 str[but->pos]= ascii;
1472 if(G.qual & LR_SHIFTKEY) but->pos= strlen(str);
1474 if(but->pos>strlen(str)) but->pos= strlen(str);
1479 if(G.qual & LR_SHIFTKEY) but->pos= 0;
1480 else if(but->pos>0) but->pos--;
1485 but->pos= strlen(str);
1500 if(but->pos>=0 && but->pos<strlen(str)) {
1501 for(x=but->pos; x<=strlen(str); x++)
1510 if(get_qual() & LR_SHIFTKEY) {
1516 else if(but->pos>0) {
1517 for(x=but->pos; x<=strlen(str); x++)
1532 ui_block_flush_back(but->block);
1536 if(dev==ESCKEY) strcpy(but->poin, backstr);
1538 but->flag &= ~UI_SELECT;
1545 if(dev!=ESCKEY) return but->retval;
1550 static int uiActAsTextBut(uiBut *but)
1555 int temp, retval, textleft;
1556 char str[UI_MAX_DRAW_STR], *point;
1559 value= ui_get_but_val(but);
1560 if( but->pointype==FLO ) {
1561 if(but->a2) { /* amount of digits defined */
1562 if(but->a2==1) sprintf(str, "%.1f", value);
1563 else if(but->a2==2) sprintf(str, "%.2f", value);
1564 else if(but->a2==3) sprintf(str, "%.3f", value);
1565 else sprintf(str, "%.4f", value);
1567 else sprintf(str, "%.3f", value);
1570 sprintf(str, "%d", (int)value);
1572 /* store values before calling as text button */
1575 but_func= but->func;
1583 textleft= but->flag & UI_TEXT_LEFT;
1584 but->flag |= UI_TEXT_LEFT;
1587 retval= ui_do_but_TEX(but);
1589 /* restore values */
1592 but->func= but_func;
1595 if(textleft==0) but->flag &= ~UI_TEXT_LEFT;
1597 if( but->pointype==FLO ) value= atof(str);
1598 else value= atoi(str);
1600 if(value<min) value= min;
1601 if(value>max) value= max;
1603 ui_set_but_val(but, value);
1610 static int ui_do_but_NUM(uiBut *but)
1613 float deler, fstart, f, tempf;
1614 int lvalue, temp; /* , firsttime=1; */
1615 short retval=0, qual, sx, mval[2], pos=0;
1617 but->flag |= UI_SELECT;
1619 ui_block_flush_back(but->block);
1621 uiGetMouse(mywinget(), mval);
1622 value= ui_get_but_val(but);
1625 fstart= (value - but->min)/(but->max-but->min);
1631 if(get_qual() & LR_SHIFTKEY) { /* make it textbut */
1632 if( uiActAsTextBut(but) ) retval= but->retval;
1635 retval= but->retval;
1636 /* firsttime: this button can be approached with enter as well */
1637 while (get_mbut() & L_MOUSE) {
1641 if( but->pointype!=FLO ) {
1643 if( (but->max-but->min)<100 ) deler= 200.0;
1644 if( (but->max-but->min)<25 ) deler= 50.0;
1647 if(qual & LR_SHIFTKEY) deler*= 10.0;
1648 if(qual & LR_ALTKEY) deler*= 20.0;
1650 uiGetMouse(mywinget(), mval);
1654 f+= ((float)(mval[0]-sx))/deler;
1658 tempf= ( but->min + f*(but->max-but->min));
1660 if( but->pointype!=FLO ) {
1662 temp= floor(tempf+.5);
1664 if(tempf==but->min || tempf==but->max);
1665 else if(qual & LR_CTRLKEY) {
1666 if(qual & LR_SHIFTKEY) temp= 100*(temp/100);
1667 else temp= 10*(temp/10);
1669 if( temp>=but->min && temp<=but->max) {
1671 value= ui_get_but_val(but);
1674 if(temp != lvalue ) {
1676 ui_set_but_val(but, (double)temp);
1679 ui_block_flush_back(but->block);
1688 if(qual & LR_CTRLKEY) {
1689 if(qual & LR_SHIFTKEY) {
1690 if(tempf==but->min || tempf==but->max);
1691 else if(but->max-but->min < 2.10) tempf= 0.01*floor(100.0*tempf);
1692 else if(but->max-but->min < 21.0) tempf= 0.1*floor(10.0*tempf);
1693 else tempf= floor(tempf);
1696 if(tempf==but->min || tempf==but->max);
1697 else if(but->max-but->min < 2.10) tempf= 0.1*floor(10*tempf);
1698 else if(but->max-but->min < 21.0) tempf= floor(tempf);
1699 else tempf= 10.0*floor(tempf/10.0);
1703 if( tempf>=but->min && tempf<=but->max) {
1704 value= ui_get_but_val(but);
1706 if(tempf != value ) {
1708 ui_set_but_val(but, tempf);
1711 ui_block_flush_back(but->block);
1717 BIF_wait_for_statechange();
1720 /* click on the side arrows to increment/decrement, click inside
1721 * to edit the value directly */
1722 if(pos==0) { /* plus 1 or minus 1 */
1723 if( but->pointype!=FLO ) {
1725 if(sx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1727 if( temp>=but->min && temp<=but->max) ui_set_but_val(but, (double)temp);
1729 else if(sx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
1731 if( temp>=but->min && temp<=but->max) ui_set_but_val(but, (double)temp);
1734 if( uiActAsTextBut(but) ); else retval= 0;
1739 if(sx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1740 tempf-= 0.01*but->a1;
1741 if (tempf < but->min) tempf = but->min;
1742 ui_set_but_val(but, tempf);
1744 else if(sx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
1745 tempf+= 0.01*but->a1;
1746 if (tempf < but->min) tempf = but->min;
1747 ui_set_but_val(but, tempf);
1750 if( uiActAsTextBut(but) ); else retval= 0;
1756 but->flag &= ~UI_SELECT;
1759 ui_block_flush_back(but->block);
1766 static int ui_do_but_TOG3(uiBut *but)
1769 if( but->pointype==SHO ) {
1770 short *sp= (short *)but->poin;
1772 if( BTST(sp[1], but->bitnr)) {
1773 sp[1]= BCLR(sp[1], but->bitnr);
1774 sp[0]= BCLR(sp[0], but->bitnr);
1776 else if( BTST(sp[0], but->bitnr)) {
1777 sp[1]= BSET(sp[1], but->bitnr);
1779 sp[0]= BSET(sp[0], but->bitnr);
1783 if( BTST(*(but->poin+2), but->bitnr)) {
1784 *(but->poin+2)= BCLR(*(but->poin+2), but->bitnr);
1785 *(but->poin)= BCLR(*(but->poin), but->bitnr);
1787 else if( BTST(*(but->poin), but->bitnr)) {
1788 *(but->poin+2)= BSET(*(but->poin+2), but->bitnr);
1790 *(but->poin)= BSET(*(but->poin), but->bitnr);
1800 static int ui_do_but_ICONROW(uiBut *but)
1802 ListBase listb= {NULL, NULL};
1806 but->flag |= UI_SELECT;
1808 ui_block_flush_back(but->block); // flush because this button creates own blocks loop
1811 block= uiNewBlock(&listb, "menu", UI_EMBOSSP, UI_HELV, but->win);
1812 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1813 block->themecol= TH_MENU_ITEM;
1815 for(a=(int)but->min; a<=(int)but->max; a++) {
1816 uiDefIconBut(block, BUTM|but->pointype, but->retval, but->icon+(a-but->min), 0, (short)(18*a), (short)(but->x2-but->x1-4), 18, but->poin, (float)a, 0.0, 0, 0, "");
1818 block->direction= UI_TOP;
1819 ui_positionblock(block, but);
1821 /* the block is made with but-win, but is handled in mainwin space...
1822 this is needs better implementation */
1823 block->win= G.curscreen->mainwin;
1825 uiDoBlocks(&listb, 0);
1827 but->flag &= ~UI_SELECT;
1834 static int ui_do_but_ICONTEXTROW(uiBut *but)
1837 ListBase listb={NULL, NULL};
1838 int width, a, xmax, ypos;
1841 but->flag |= UI_SELECT;
1843 ui_block_flush_back(but->block); // flush because this button creates own blocks loop
1845 block= uiNewBlock(&listb, "menu", UI_EMBOSSP, UI_HELV, but->win);
1846 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT;
1847 block->themecol= TH_MENU_ITEM;
1849 md= decompose_menu_string(but->str);
1851 /* size and location */
1852 /* expand menu width to fit labels */
1854 width= 2*strlen(md->title)+BIF_GetStringWidth(block->curfont, md->title, (U.transopts & USER_TR_MENUS));
1858 for(a=0; a<md->nitems; a++) {
1859 xmax= BIF_GetStringWidth(block->curfont, md->items[a].str, (U.transopts & USER_TR_MENUS));
1860 if(xmax>width) width= xmax;
1864 if (width<50) width=50;
1868 /* loop through the menu options and draw them out with icons & text labels */
1869 for(a=0; a<md->nitems; a++) {
1871 /* add a space if there's a separator (%l) */
1872 if (strcmp(md->items[a].str, "%l")==0) {
1876 uiDefIconTextBut(block, BUTM|but->pointype, but->retval, (short)((but->icon)+(md->items[a].retval-but->min)), md->items[a].str, 0, ypos,(short)width, 19, but->poin, (float) md->items[a].retval, 0.0, 0, 0, "");
1881 block->direction= UI_TOP;
1882 ui_positionblock(block, but);
1884 /* the block is made with but-win, but is handled in mainwin space...
1885 this is needs better implementation */
1886 block->win= G.curscreen->mainwin;
1888 uiBoundsBlock(block, 3);
1890 uiDoBlocks(&listb, 0);
1894 but->flag &= ~UI_SELECT;
1904 static int ui_do_but_IDPOIN(uiBut *but)
1906 char str[UI_MAX_DRAW_STR];
1909 id= *but->idpoin_idpp;
1910 if(id) strcpy(str, id->name+2);
1921 but->idpoin_func(str, but->idpoin_idpp);
1928 static int ui_do_but_SLI(uiBut *but)
1930 float f, fstart, tempf = 0.0, deler, value;
1931 int sx, h, temp, pos=0, lvalue, redraw;
1932 short mval[2], qual;
1933 float curmatrix[4][4];
1935 value= ui_get_but_val(but);
1936 uiGetMouse(mywinget(), mval);
1940 fstart= but->max-but->min;
1941 fstart= (value - but->min)/fstart;
1944 if( but->type==NUMSLI) deler= ( (but->x2-but->x1)/2 - 5.0*but->aspect);
1945 else if( but->type==HSVSLI) deler= ( (but->x2-but->x1)/2 - 5.0*but->aspect);
1946 else deler= (but->x2-but->x1- 5.0*but->aspect);
1949 while (get_mbut() & L_MOUSE) {
1952 uiGetMouse(mywinget(), mval);
1954 f= (float)(mval[0]-sx)/deler +fstart;
1956 if (qual & LR_SHIFTKEY) {
1957 f= (f-fstart)/10.0 + fstart;
1961 tempf= but->min+f*(but->max-but->min);
1962 temp= floor(tempf+.5);
1964 if(qual & LR_CTRLKEY) {
1965 if(tempf==but->min || tempf==but->max);
1966 else if( but->pointype==FLO ) {
1968 if(qual & LR_SHIFTKEY) {
1969 if(tempf==but->min || tempf==but->max);
1970 else if(but->max-but->min < 2.10) tempf= 0.01*floor(100.0*tempf);
1971 else if(but->max-but->min < 21.0) tempf= 0.1*floor(10.0*tempf);
1972 else tempf= floor(tempf);
1975 if(but->max-but->min < 2.10) tempf= 0.1*floor(10*tempf);
1976 else if(but->max-but->min < 21.0) tempf= floor(tempf);
1977 else tempf= 10.0*floor(tempf/10.0);
1986 value= ui_get_but_val(but);
1987 lvalue= floor(value+0.5);
1989 if( but->pointype!=FLO )
1990 redraw= (temp != lvalue);
1992 redraw= (tempf != value);
1996 ui_set_but_val(but, tempf);
1999 ui_block_flush_back(but->block);
2001 if(but->a1) { /* color number */
2002 uiBut *bt= but->prev;
2004 if(bt->a2 == but->a1) ui_draw_but(bt);
2009 if(bt->a2 == but->a1) ui_draw_but(bt);
2013 /* save current window matrix (global UIwinmat)
2014 because button callback function MIGHT change it
2015 - which has until now occured through the Python API
2017 Mat4CpyMat4(curmatrix, UIwinmat);
2019 Mat4CpyMat4(UIwinmat, curmatrix);
2021 else BIF_wait_for_statechange();
2025 if(temp!=32767 && pos==0) { /* plus 1 or minus 1 */
2027 if( but->type==SLI) f= (float)(mval[0]-but->x1)/(but->x2-but->x1-h);
2028 else f= (float)(mval[0]- (but->x1+but->x2)/2)/( (but->x2-but->x1)/2 - h);
2030 f= but->min+f*(but->max-but->min);
2032 if( but->pointype!=FLO ) {
2036 if( temp>=but->min && temp<=but->max)
2037 ui_set_but_val(but, (float)temp);
2042 if(f<tempf) tempf-=.01;
2044 if( tempf>=but->min && tempf<=but->max)
2045 ui_set_but_val(but, tempf);
2051 ui_block_flush_back(but->block);
2056 static int ui_do_but_NUMSLI(uiBut *but)
2060 /* first define if it's a slider or textbut */
2061 uiGetMouse(mywinget(), mval);
2063 if(mval[0]>= -6+(but->x1+but->x2)/2 ) { /* slider */
2064 but->flag |= UI_SELECT;
2067 but->flag &= ~UI_SELECT;
2070 uiActAsTextBut(but);
2071 uibut_do_func(but); // this is done in ui_do_but_SLI() not in uiActAsTextBut()
2074 while(get_mbut() & L_MOUSE) BIF_wait_for_statechange();
2079 if(but->type==HSVSLI) {
2081 if(but->str[0]=='H') {
2082 ui_draw_but(but->next);
2083 ui_draw_but(but->next->next);
2085 else if(but->str[0]=='S') {
2086 ui_draw_but(but->next);
2087 ui_draw_but(but->prev);
2089 else if(but->str[0]=='V') {
2090 ui_draw_but(but->prev);
2091 ui_draw_but(but->prev->prev);
2098 static int ui_do_but_BLOCK(uiBut *but)
2103 but->flag |= UI_SELECT;
2106 block= but->block_func(but->poin);
2108 block->xofs = -2; /* for proper alignment */
2110 /* only used for automatic toolbox, so can set the shift flag */
2111 if(but->flag & UI_MAKE_TOP) {
2112 block->direction= UI_TOP|UI_SHIFT_FLIPPED;
2113 uiBlockFlipOrder(block);
2115 if(but->flag & UI_MAKE_DOWN) block->direction= UI_DOWN|UI_SHIFT_FLIPPED;
2116 if(but->flag & UI_MAKE_LEFT) block->direction |= UI_LEFT;
2117 if(but->flag & UI_MAKE_RIGHT) block->direction |= UI_RIGHT;
2119 ui_positionblock(block, but);
2120 block->flag |= UI_BLOCK_LOOP;
2122 /* blocks can come (and get scaled) from a normal window, now we go to screenspace */
2123 block->win= G.curscreen->mainwin;
2124 for(bt= block->buttons.first; bt; bt= bt->next) bt->win= block->win;
2125 bwin_getsinglematrix(block->win, block->winmat);
2127 /* postpone draw, this will cause a new window matrix, first finish all other buttons */
2128 block->flag |= UI_BLOCK_REDRAW;
2130 but->flag &= ~UI_SELECT;
2136 static int ui_do_but_BUTM(uiBut *but)
2139 ui_set_but_val(but, but->min);
2140 UIafterfunc= but->butm_func;
2141 UIafterfunc_arg= but->butm_func_arg;
2142 UIafterval= but->a2;
2147 static int ui_do_but_LABEL(uiBut *but)
2154 static uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
2158 /* find button to link to */
2159 for (bt= block->buttons.first; bt; bt= bt->next)
2160 if(bt!=but && uibut_contains_pt(bt, mval))
2164 if (but->type==LINK && bt->type==INLINK) {
2165 if( but->link->tocode == (int)bt->min ) {
2169 else if(but->type==INLINK && bt->type==LINK) {
2170 if( bt->link->tocode == (int)but->min ) {
2179 static int ui_is_a_link(uiBut *from, uiBut *to)
2186 line= link->lines.first;
2188 if(line->from==from && line->to==to) return 1;
2195 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
2199 but= block->buttons.first;
2201 if(but->type==INLINK) {
2202 if(but->poin == poin) return but;
2209 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
2213 line= MEM_callocN(sizeof(uiLinkLine), "linkline");
2214 BLI_addtail(listb, line);
2220 void uiComposeLinks(uiBlock *block)
2227 but= block->buttons.first;
2229 if(but->type==LINK) {
2232 /* for all pointers in the array */
2236 for(a=0; a < *(link->totlink); a++) {
2237 bt= ui_find_inlink(block, (*ppoin)[a] );
2239 ui_add_link_line(&link->lines, but, bt);
2243 else if(link->poin) {
2244 bt= ui_find_inlink(block, *(link->poin) );
2246 ui_add_link_line(&link->lines, but, bt);
2255 static void ui_add_link(uiBut *from, uiBut *to)
2257 /* in 'from' we have to add a link to 'to' */
2262 if(ui_is_a_link(from, to)) {
2263 printf("already exists\n");
2269 /* are there more pointers allowed? */
2271 oldppoin= *(link->ppoin);
2273 (*(link->totlink))++;
2274 *(link->ppoin)= MEM_callocN( *(link->totlink)*sizeof(void *), "new link");
2276 for(a=0; a< (*(link->totlink))-1; a++) {
2277 (*(link->ppoin))[a]= oldppoin[a];
2279 (*(link->ppoin))[a]= to->poin;
2281 if(oldppoin) MEM_freeN(oldppoin);
2284 *(link->poin)= to->poin;
2289 static int ui_do_but_LINK(uiBlock *block, uiBut *but)
2292 * This button only visualizes, the dobutton mode
2293 * can add a new link, but then the whole system
2294 * should be redrawn/initialized.
2297 uiBut *bt=0, *bto=NULL;
2298 short sval[2], mval[2], mvalo[2], first= 1;
2300 uiGetMouse(curarea->win, sval);
2304 while (get_mbut() & L_MOUSE) {
2305 uiGetMouse(curarea->win, mval);
2307 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || first) {
2308 /* clear completely, because of drawbuttons */
2309 bt= ui_get_valid_link_button(block, but, mval);
2311 bt->flag |= UI_ACTIVE;
2314 if(bto && bto!=bt) {
2315 bto->flag &= ~UI_ACTIVE;
2321 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
2323 glutil_draw_front_xor_line(sval[0], sval[1], mval[0], mval[1]);
2330 else BIF_wait_for_statechange();
2334 glutil_draw_front_xor_line(sval[0], sval[1], mvalo[0], mvalo[1]);
2338 if(but->type==LINK) ui_add_link(but, bt);
2339 else ui_add_link(bt, but);
2341 scrarea_queue_winredraw(curarea);
2347 /* picker sizes S hsize, F full size, D spacer, B button/pallette height */
2353 #define UI_PALETTE_TOT 16
2354 /* note; in tot+1 the old color is stored */
2355 static float palette[UI_PALETTE_TOT+1][3]= {
2356 {0.93, 0.83, 0.81}, {0.88, 0.89, 0.73}, {0.69, 0.81, 0.57}, {0.51, 0.76, 0.64},
2357 {0.37, 0.56, 0.61}, {0.33, 0.29, 0.55}, {0.46, 0.21, 0.51}, {0.40, 0.12, 0.18},
2358 {1.0, 1.0, 1.0}, {0.85, 0.85, 0.85}, {0.7, 0.7, 0.7}, {0.56, 0.56, 0.56},
2359 {0.42, 0.42, 0.42}, {0.28, 0.28, 0.28}, {0.14, 0.14, 0.14}, {0.0, 0.0, 0.0}
2362 /* for picker, while editing hsv */
2363 static void ui_set_but_hsv(uiBut *but)
2367 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], col, col+1, col+2);
2368 ui_set_but_vectorf(but, col);
2371 static void update_picker_buts(uiBlock *block, float *hsv)
2376 // this updates button strings, is hackish... but button pointers are on stack of caller function
2377 hsv_to_rgb(hsv[0], hsv[1], hsv[2], &r, &g, &b);
2379 for(bt= block->buttons.first; bt; bt= bt->next) {
2380 if(bt->type==HSVCUBE) {
2381 VECCOPY(bt->hsv, hsv);
2384 else if(bt->str[1]==' ') {
2385 if(bt->str[0]=='R') {
2386 ui_set_but_val(bt, r);
2389 else if(bt->str[0]=='G') {
2390 ui_set_but_val(bt, g);
2393 else if(bt->str[0]=='B') {
2394 ui_set_but_val(bt, b);
2397 else if(bt->str[0]=='H') {
2398 ui_set_but_val(bt, hsv[0]);
2401 else if(bt->str[0]=='S') {
2402 ui_set_but_val(bt, hsv[1]);
2405 else if(bt->str[0]=='V') {
2406 ui_set_but_val(bt, hsv[2]);
2413 /* bt1 is palette but, col1 is original color */
2414 /* callback to copy from/to palette */
2415 static void do_palette_cb(void *bt1, void *col1)
2417 uiBut *but1= (uiBut *)bt1;
2419 float *col= (float *)col1;
2422 fp= (float *)but1->poin;
2424 if( (get_qual() & LR_CTRLKEY) ) {
2431 rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
2432 update_picker_buts(but1->block, hsv);
2434 for (but= but1->block->buttons.first; but; but= but->next) {
2437 but= but1->block->buttons.first;
2438 ui_block_flush_back(but->block);
2441 /* bt1 is num but, col1 is pointer to original color */
2442 /* callback to handle changes in num-buts in picker */
2443 static void do_palette1_cb(void *bt1, void *hsv1)
2445 uiBut *but1= (uiBut *)bt1;
2447 float *hsv= (float *)hsv1;
2450 if(but1->str[0]=='R') fp= (float *)but1->poin;
2451 else if(but1->str[0]=='G') fp= ((float *)but1->poin)-1;
2452 else if(but1->str[0]=='B') fp= ((float *)but1->poin)-2;
2455 rgb_to_hsv(fp[0], fp[1], fp[2], hsv, hsv+1, hsv+2);
2458 update_picker_buts(but1->block, hsv);
2460 for (but= but1->block->buttons.first; but; but= but->next) {
2464 but= but1->block->buttons.first;
2465 ui_block_flush_back(but->block);
2469 /* color picker, Gimp version. mode: 'f' = floating panel, 'p' = popup */
2470 /* col = read/write to, hsv/old = memory for temporal use */
2471 void uiBlockPickerButtons(uiBlock *block, float *col, float *hsv, float *old, char mode, short retval)
2477 VECCOPY(old, col); // old color stored there, for palette_cb to work
2479 // the cube intersection
2480 bt= uiDefButF(block, HSVCUBE, retval, "", 0,DPICK+BPICK,FPICK,FPICK, col, 0.0, 0.0, 2, 0, "");
2481 uiButSetFlag(bt, UI_NO_HILITE);
2483 bt= uiDefButF(block, HSVCUBE, retval, "", 0,0,FPICK,BPICK, col, 0.0, 0.0, 3, 0, "");
2484 uiButSetFlag(bt, UI_NO_HILITE);
2488 uiBlockSetEmboss(block, UI_EMBOSSP);
2490 bt=uiDefButF(block, COL, retval, "", FPICK+DPICK, 0, BPICK,BPICK, old, 0.0, 0.0, -1, 0, "Old color, click to restore");
2491 uiButSetFunc(bt, do_palette_cb, bt, col);
2492 uiDefButF(block, COL, retval, "", FPICK+DPICK, BPICK+DPICK, BPICK,60-BPICK-DPICK, col, 0.0, 0.0, -1, 0, "Active color");
2494 h= (DPICK+BPICK+FPICK-64)/(UI_PALETTE_TOT/2.0);
2495 uiBlockBeginAlign(block);
2496 for(a= -1+UI_PALETTE_TOT/2; a>=0; a--) {
2497 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");
2498 uiButSetFunc(bt, do_palette_cb, bt, col);
2499 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");
2500 uiButSetFunc(bt, do_palette_cb, bt, col);
2502 uiBlockEndAlign(block);
2504 uiBlockSetEmboss(block, UI_EMBOSSX);
2507 rgb_to_hsv(col[0], col[1], col[2], hsv, hsv+1, hsv+2);
2509 offs= FPICK+2*DPICK+BPICK;
2512 uiBlockBeginAlign(block);
2513 bt= uiDefButF(block, NUM, retval, "R ", offs, 110, 80,20, col, 0.0, 1.0, 10, 2, "");
2514 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2515 bt= uiDefButF(block, NUM, retval, "G ", offs, 90, 80,20, col+1, 0.0, 1.0, 10, 2, "");
2516 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2517 bt= uiDefButF(block, NUM, retval, "B ", offs, 70, 80,20, col+2, 0.0, 1.0, 10, 2, "");
2518 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2520 uiBlockBeginAlign(block);
2521 bt= uiDefButF(block, NUM, retval, "H ", offs, 40, 80,20, hsv, 0.0, 1.0, 10, 2, "");
2522 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2523 bt= uiDefButF(block, NUM, retval, "S ", offs, 20, 80,20, hsv+1, 0.0, 1.0, 10, 2, "");
2524 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2525 bt= uiDefButF(block, NUM, retval, "V ", offs, 0, 80,20, hsv+2, 0.0, 1.0, 10, 2, "");
2526 uiButSetFunc(bt, do_palette1_cb, bt, hsv);
2527 uiBlockEndAlign(block);
2531 static int ui_do_but_COL(uiBut *but)
2535 ListBase listb={NULL, NULL};
2536 float hsv[3], old[3], *poin= NULL, colstore[3];
2539 // signal to prevent calling up color picker
2545 // enable char button too, use temporal colstore for color
2546 if(but->pointype!=FLO) {
2547 if(but->pointype==CHA) {
2548 ui_get_but_vectorf(but, colstore);
2551 else return but->retval;
2553 else poin= (float *)but->poin;
2555 block= uiNewBlock(&listb, "colorpicker", UI_EMBOSSX, UI_HELV, but->win);
2556 block->flag= UI_BLOCK_LOOP|UI_BLOCK_REDRAW;
2557 block->themecol= TH_BUT_NUM;
2559 uiBlockPickerButtons(block, poin, hsv, old, 'p', 0);
2562 block->direction= UI_TOP;
2563 ui_positionblock(block, but);
2564 uiBoundsBlock(block, 3);
2566 /* blocks can come from a normal window, but we go to screenspace */
2567 block->win= G.curscreen->mainwin;
2568 for(bt= block->buttons.first; bt; bt= bt->next) bt->win= block->win;
2569 bwin_getsinglematrix(block->win, block->winmat);
2571 event= uiDoBlocks(&listb, 0);
2573 if(but->pointype==CHA) ui_set_but_vectorf(but, colstore);
2579 static int ui_do_but_HSVCUBE(uiBut *but)
2583 short mval[2], mvalo[2];
2585 mvalo[0]= mvalo[1]= -32000;
2587 /* we work on persistant hsv, to prevent it being converted back and forth all the time */
2589 while (get_mbut() & L_MOUSE) {
2591 uiGetMouse(mywinget(), mval);
2593 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1]) {
2597 /* relative position within box */
2598 x= ((float)mval[0]-but->x1)/(but->x2-but->x1);
2599 y= ((float)mval[1]-but->y1)/(but->y2-but->y1);
2606 // hsv_to_rgb(x, s, y, col, col+1, col+2);
2608 else if(but->a1==1) {
2611 // hsv_to_rgb(x, y, v, col, col+1, col+2);
2613 else if(but->a1==2) {
2616 // hsv_to_rgb(h, y, x, col, col+1, col+2);
2620 // hsv_to_rgb(x, s, v, col, col+1, col+2);
2623 ui_set_but_hsv(but); // converts to rgb
2625 // update button values and strings
2626 update_picker_buts(but->block, but->hsv);
2628 /* we redraw the entire block */
2629 for (bt= but->block->buttons.first; bt; bt= bt->next) {
2630 if(but->poin == bt->poin) VECCOPY(bt->hsv, but->hsv);
2633 ui_block_flush_back(but->block);
2635 else BIF_wait_for_statechange();
2642 /* ************************************************ */
2644 void uiSetButLock(int val, char *lockstr)
2647 if (val) UIlockstr= lockstr;
2650 void uiClearButLock()
2656 /* ********************** NEXT/PREV for arrowkeys etc ************** */
2658 static uiBut *ui_but_prev(uiBut *but)
2662 if(but->type!=LABEL && but->type!=SEPR) return but;
2667 static uiBut *ui_but_next(uiBut *but)
2671 if(but->type!=LABEL && but->type!=SEPR) return but;
2676 static uiBut *ui_but_first(uiBlock *block)
2680 but= block->buttons.first;
2682 if(but->type!=LABEL && but->type!=SEPR) return but;
2688 static uiBut *ui_but_last(uiBlock *block)
2692 but= block->buttons.last;
2694 if(but->type!=LABEL && but->type!=SEPR) return but;
2700 /* *************************************************************** */
2702 static void setup_file(uiBlock *block)
2707 fp= fopen("butsetup","w");
2710 but= block->buttons.first;
2713 fprintf(fp,"%d,%d,%d,%d %s %s\n", (int)but->x1, (int)but->y1, (int)( but->x2-but->x1), (int)(but->y2-but->y1), but->str, but->tip);
2721 static void edit_but(uiBlock *block, uiBut *but, uiEvent *uevent)
2723 short dx, dy, mval[2], mvalo[2], didit=0;
2725 getmouseco_sc(mvalo);
2727 if( !(get_mbut() & L_MOUSE) ) break;
2729 getmouseco_sc(mval);
2730 dx= (mval[0]-mvalo[0]);
2731 dy= (mval[1]-mvalo[1]);
2733 if(dx!=0 || dy!=0) {
2738 glRectf(but->x1-2, but->y1-2, but->x2+2, but->y2+2);
2740 if((uevent->qual & LR_SHIFTKEY)==0) {
2748 ui_block_flush_back(but->block);
2752 /* idle for this poor code */
2753 else PIL_sleep_ms(30);
2755 if(didit) setup_file(block);
2759 /* is called when LEFTMOUSE is pressed or released
2760 * return: butval or zero
2762 static int ui_do_button(uiBlock *block, uiBut *but, uiEvent *uevent)
2768 error("%s", but->lockstr);
2773 if( but->pointype ) { /* there's a pointer needed */
2775 printf("DoButton pointer error: %s\n",but->str);
2781 if(G.rt==1 && (uevent->qual & LR_CTRLKEY)) {
2782 edit_but(block, but, uevent);
2786 block->flag |= UI_BLOCK_BUSY;
2790 if(uevent->val) retval= ui_do_but_BUT(but);
2794 if(uevent->val) retval= ui_do_but_KEYEVT(but);
2802 retval= ui_do_but_TOG(block, but);
2807 if(uevent->val) retval= ui_do_but_ROW(block, but);
2811 /* DrawBut(b, 1); */
2812 /* do_scrollbut(b); */
2817 if(uevent->val) retval= ui_do_but_NUM(but);
2823 if(uevent->val) retval= ui_do_but_NUMSLI(but);
2827 if(uevent->val) retval= ui_do_but_LABEL(but);
2831 if(uevent->val) retval= ui_do_but_TOG3(but);
2835 if(uevent->val) retval= ui_do_but_TEX(but);
2839 if(uevent->val) retval= ui_do_but_MENU(but);
2843 if(uevent->val) retval= ui_do_but_ICONROW(but);
2847 if(uevent->val) retval= ui_do_but_ICONTEXTROW(but);
2851 if(uevent->val) retval= ui_do_but_IDPOIN(but);
2857 retval= ui_do_but_BLOCK(but);
2858 if(block->auto_open==0) block->auto_open= 1;
2863 retval= ui_do_but_BUTM(but);
2868 retval= ui_do_but_LINK(block, but);
2872 if(uevent->val) retval= ui_do_but_COL(but);
2876 retval= ui_do_but_HSVCUBE(but);
2880 block->flag &= ~UI_BLOCK_BUSY;
2885 static void ui_delete_active_linkline(uiBlock *block)
2889 uiLinkLine *line, *nline;
2892 but= block->buttons.first;
2894 if(but->type==LINK && but->link) {
2895 line= but->link->lines.first;
2900 if(line->flag & UI_SELECT) {
2901 BLI_remlink(&but->link->lines, line);
2903 link= line->from->link;
2905 /* are there more pointers allowed? */
2908 if(*(link->totlink)==1) {
2909 *(link->totlink)= 0;
2910 MEM_freeN(*(link->ppoin));
2911 *(link->ppoin)= NULL;
2915 for(a=0; a< (*(link->totlink)); a++) {
2917 if( (*(link->ppoin))[a] != line->to->poin ) {
2918 (*(link->ppoin))[b]= (*(link->ppoin))[a];
2922 (*(link->totlink))--;
2926 *(link->poin)= NULL;
2937 /* temporal! these buttons can be everywhere... */
2938 allqueue(REDRAWBUTSLOGIC, 0);
2941 static void ui_do_active_linklines(uiBlock *block, short *mval)
2944 uiLinkLine *line, *act= NULL;
2945 float mindist= 12.0, fac, v1[2], v2[2], v3[3];
2952 /* find a line close to the mouse */
2953 but= block->buttons.first;
2955 if(but->type==LINK && but->link) {
2957 line= but->link->lines.first;
2959 v2[0]= line->from->x2;
2960 v2[1]= (line->from->y1+line->from->y2)/2.0;
2961 v3[0]= line->to->x1;
2962 v3[1]= (line->to->y1+line->to->y2)/2.0;
2964 fac= PdistVL2Dfl(v1, v2, v3);
2976 /* check for a 'found one' to prevent going to 'frontbuffer' mode.
2977 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
2979 glDrawBuffer(GL_FRONT);
2982 but= block->buttons.first;
2984 if(but->type==LINK && but->link) {
2985 line= but->link->lines.first;
2988 if((line->flag & UI_SELECT)==0) {
2989 line->flag |= UI_SELECT;
2990 ui_draw_linkline(but, line);
2993 else if(line->flag & UI_SELECT) {
2994 line->flag &= ~UI_SELECT;
2995 ui_draw_linkline(but, line);
3003 glDrawBuffer(GL_BACK);
3008 /* only to be used to prevent an 'outside' event when using nested pulldowns */
3010 - while mouse moves in triangular area defined old mouse position and left/right side of new menu
3013 return 0: check outside
3015 static int ui_mouse_motion_towards_block(uiBlock *block, uiEvent *uevent)
3017 short mvalo[2], dx, dy, domx, domy;
3020 if((block->direction & UI_TOP) || (block->direction & UI_DOWN)) return 0;
3021 if(uevent->event!= MOUSEX && uevent->event!= MOUSEY) return 0;
3023 /* calculate dominant direction */
3024 domx= ( -uevent->mval[0] + (block->maxx+block->minx)/2 );
3025 domy= ( -uevent->mval[1] + (block->maxy+block->miny)/2 );
3026 /* we need some accuracy */
3027 if( abs(domx)<4 ) return 0;
3029 uiGetMouse(mywinget(), mvalo);
3032 uiGetMouse(mywinget(), uevent->mval);
3034 /* check inside, if so return */
3035 if( block->minx <= uevent->mval[0] && block->maxx >= uevent->mval[0] ) {
3036 if( block->miny <= uevent->mval[1] && block->maxy >= uevent->mval[1] ) {
3041 /* check direction */
3042 dx= uevent->mval[0] - mvalo[0];
3043 dy= uevent->mval[1] - mvalo[1];
3045 if( abs(dx)+abs(dy)>4 ) { // threshold
3048 int fac= (uevent->mval[0] - mvalo[0])*(mvalo[1] - (short)(block->maxy +20)) + (uevent->mval[1] - mvalo[1])*(-mvalo[0] + (short)block->minx);
3050 // printf("Left outside 1, Fac %d\n", fac);
3054 fac= (uevent->mval[0] - mvalo[0])*(mvalo[1] - (short)(block->miny-20)) + (uevent->mval[1] - mvalo[1])*(-mvalo[0] + (short)block->minx);
3056 //printf("Left outside 2, Fac %d\n", fac);
3061 int fac= (uevent->mval[0] - mvalo[0])*(mvalo[1] - (short)(block->maxy+20)) + (uevent->mval[1] - mvalo[1])*(-mvalo[0] + (short)block->maxx);
3063 // printf("Left outside 1, Fac %d\n", fac);
3067 fac= (uevent->mval[0] - mvalo[0])*(mvalo[1] - (short)(block->miny-20)) + (uevent->mval[1] - mvalo[1])*(-mvalo[0] + (short)block->maxx);
3069 // printf("Left outside 2, Fac %d\n", fac);
3075 /* idle for this poor code */
3079 //printf("left because of timer (1 sec)\n");
3088 static void ui_set_ftf_font(uiBlock *block)
3091 #ifdef INTERNATIONAL
3092 if(block->aspect<1.15) {
3093 FTF_SetFontSize('l');
3095 else if(block->aspect<1.59) {
3096 FTF_SetFontSize('m');
3099 FTF_SetFontSize('s');
3106 * UI_NOTHING pass event to other ui's
3107 * UI_CONT don't pass event to other ui's
3108 * UI_RETURN something happened, return, swallow event
3110 static int ui_do_block(uiBlock *block, uiEvent *uevent)
3113 int butevent, event, retval=UI_NOTHING, count, act=0;
3114 int inside= 0, active=0;
3116 if(block->win != mywinget()) return UI_NOTHING;
3118 /* filter some unwanted events */
3119 /* btw: we allow event==0 for first time in menus, draws the hilited item */
3120 if(uevent==0 || uevent->event==LEFTSHIFTKEY || uevent->event==RIGHTSHIFTKEY) return UI_NOTHING;
3122 if(block->flag & UI_BLOCK_ENTER_OK) {
3123 if((uevent->event==RETKEY || uevent->event==PADENTER) && uevent->val) {
3124 // printf("qual: %d %d %d\n", uevent->qual, get_qual(), G.qual);
3125 if ((G.qual & LR_SHIFTKEY) == 0) {
3126 return UI_RETURN_OK;
3131 ui_set_ftf_font(block); // sets just a pointer in ftf lib... the button dont have ftf handles
3133 // added this for panels in windows with buttons...
3134 // maybe speed optimize should require test
3135 if((block->flag & UI_BLOCK_LOOP)==0) {
3136 glMatrixMode(GL_PROJECTION);
3137 bwin_load_winmatrix(block->win, block->winmat);
3138 glMatrixMode(GL_MODELVIEW);
3142 Mat4CpyMat4(UIwinmat, block->winmat);
3143 uiPanelPush(block); // push matrix; no return without pop!
3145 uiGetMouse(mywinget(), uevent->mval); /* transformed mouseco */
3147 /* check boundbox and panel events */
3148 if( block->minx <= uevent->mval[0] && block->maxx >= uevent->mval[0] ) {
3150 if( block->miny <= uevent->mval[1] && block->maxy >= uevent->mval[1] ) inside= 1;
3152 if(block->panel && block->panel->paneltab==NULL) {
3154 /* clicked at panel header? */
3155 if( block->panel->flag & PNL_CLOSEDX) {
3156 if(block->minx <= uevent->mval[0] && block->minx+PNL_HEADER >= uevent->mval[0])
3159 else if( (block->maxy <= uevent->mval[1]) && (block->maxy+PNL_HEADER >= uevent->mval[1]) )
3162 if(inside) { // this stuff should move to do_panel
3164 if(uevent->event==LEFTMOUSE) {
3166 uiPanelPop(block); // pop matrix; no return without pop!
3167 ui_do_panel(block, uevent);
3168 return UI_EXIT_LOOP; // exit loops because of moving panels
3171 else if(uevent->event==ESCKEY) {
3172 if(block->handler) {
3173 rem_blockhandler(curarea, block->handler);
3174 addqueue(curarea->win, REDRAW, 1);
3177 else if(uevent->event==PADPLUSKEY || uevent->event==PADMINUS) {
3178 SpaceLink *sl= curarea->spacedata.first;
3179 if(curarea->spacetype!=SPACE_BUTS) {
3180 if(uevent->event==PADPLUSKEY) sl->blockscale+= 0.1;
3181 else sl->blockscale-= 0.1;
3182 CLAMP(sl->blockscale, 0.6, 1.0);
3183 addqueue(block->winq, REDRAW, 1);
3191 switch(uevent->event) {
3192 case LEFTARROWKEY: // later on implement opening/closing sublevels of pupmenus
3196 case PAD8: case PAD2:
3199 if(inside || (block->flag & UI_BLOCK_LOOP)) {
3200 /* arrowkeys: only handle for block_loop blocks */
3202 if(block->flag & UI_BLOCK_LOOP) {
3203 event= uevent->event;
3204 if(event==PAD8) event= UPARROWKEY;
3205 if(event==PAD2) event= DOWNARROWKEY;
3208 if(uevent->event==PAD8) event= UPARROWKEY;
3209 if(uevent->event==PAD2) event= DOWNARROWKEY;
3211 if(event && uevent->val) {
3213 for(but= block->buttons.first; but; but= but->next) {
3214 but->flag &= ~UI_MOUSE_OVER;
3216 if(but->flag & UI_ACTIVE) {
3217 but->flag &= ~UI_ACTIVE;
3220 if(event==UPARROWKEY) {
3221 if(block->direction & UI_TOP) bt= ui_but_next(but);
3222 else bt= ui_but_prev(but);
3225 if(block->direction & UI_TOP) bt= ui_but_prev(but);
3226 else bt= ui_but_next(but);
3230 bt->flag |= UI_ACTIVE;
3240 if(event==UPARROWKEY) {
3241 if(block->direction & UI_TOP) but= ui_but_first(block);
3242 else but= ui_but_last(block);
3245 if(block->direction & UI_TOP) but= ui_but_last(block);
3246 else but= ui_but_first(block);
3249 but->flag |= UI_ACTIVE;
3258 case ONEKEY: act= 1;
3259 case TWOKEY: if(act==0) act= 2;
3260 case THREEKEY: if(act==0) act= 3;
3261 case FOURKEY: if(act==0) act= 4;
3262 case FIVEKEY: if(act==0) act= 5;
3263 case SIXKEY: if(act==0) act= 6;
3264 case SEVENKEY: if(act==0) act= 7;
3265 case EIGHTKEY: if(act==0) act= 8;
3266 case NINEKEY: if(act==0) act= 9;
3267 case ZEROKEY: if(act==0) act= 10;