2.5
[blender-staging.git] / source / blender / editors / interface / interface.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation 2002-2008, full recode.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <float.h>
29 #include <math.h>
30 #include <string.h>
31  
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_ID.h"
35 #include "DNA_listBase.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_texture_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "BLI_arithb.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_dynstr.h"
44
45 #include "BKE_context.h"
46 #include "BKE_idprop.h"
47 #include "BKE_library.h"
48 #include "BKE_screen.h"
49 #include "BKE_texture.h"
50 #include "BKE_utildefines.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54
55 #include "BLF_api.h"
56
57 #include "UI_interface.h"
58
59 #include "ED_screen.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63 #include "wm_subwindow.h"
64 #include "wm_window.h"
65
66 #include "RNA_access.h"
67 #include "RNA_types.h"
68
69 #include "interface_intern.h"
70
71 #define MENU_WIDTH                      120
72 #define MENU_ITEM_HEIGHT        20
73 #define MENU_SEP_HEIGHT         6
74
75 /* 
76  * a full doc with API notes can be found in bf-blender/blender/doc/interface_API.txt
77  * 
78  * uiBlahBlah()         external function
79  * ui_blah_blah()       internal function
80  */
81
82 static void ui_free_but(const bContext *C, uiBut *but);
83
84 /* ************* translation ************** */
85
86 int ui_translate_buttons()
87 {
88         return (U.transopts & USER_TR_BUTTONS);
89 }
90
91 int ui_translate_menus()
92 {
93         return (U.transopts & USER_TR_MENUS);
94 }
95
96 int ui_translate_tooltips()
97 {
98         return (U.transopts & USER_TR_TOOLTIPS);
99 }
100
101 /* ************* window matrix ************** */
102
103 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
104 {
105         float gx, gy;
106         int sx, sy, getsizex, getsizey;
107
108         getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
109         getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
110         sx= ar->winrct.xmin;
111         sy= ar->winrct.ymin;
112
113         gx= *x;
114         gy= *y;
115
116         if(block->panel) {
117                 gx += block->panel->ofsx;
118                 gy += block->panel->ofsy;
119         }
120
121         *x= ((float)sx) + ((float)getsizex)*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0]));
122         *y= ((float)sy) + ((float)getsizey)*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1]));
123 }
124
125 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
126 {
127         float fx, fy;
128
129         fx= *x;
130         fy= *y;
131
132         ui_block_to_window_fl(ar, block, &fx, &fy);
133
134         *x= (int)(fx+0.5f);
135         *y= (int)(fy+0.5f);
136 }
137
138 void ui_block_to_window_rct(const ARegion *ar, uiBlock *block, rctf *graph, rcti *winr)
139 {
140         rctf tmpr;
141
142         tmpr= *graph;
143         ui_block_to_window_fl(ar, block, &tmpr.xmin, &tmpr.ymin);
144         ui_block_to_window_fl(ar, block, &tmpr.xmax, &tmpr.ymax);
145
146         winr->xmin= tmpr.xmin;
147         winr->ymin= tmpr.ymin;
148         winr->xmax= tmpr.xmax;
149         winr->ymax= tmpr.ymax;
150 }
151
152 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y)       /* for mouse cursor */
153 {
154         float a, b, c, d, e, f, px, py;
155         int sx, sy, getsizex, getsizey;
156
157         getsizex= ar->winrct.xmax-ar->winrct.xmin+1;
158         getsizey= ar->winrct.ymax-ar->winrct.ymin+1;
159         sx= ar->winrct.xmin;
160         sy= ar->winrct.ymin;
161
162         a= .5*((float)getsizex)*block->winmat[0][0];
163         b= .5*((float)getsizex)*block->winmat[1][0];
164         c= .5*((float)getsizex)*(1.0+block->winmat[3][0]);
165
166         d= .5*((float)getsizey)*block->winmat[0][1];
167         e= .5*((float)getsizey)*block->winmat[1][1];
168         f= .5*((float)getsizey)*(1.0+block->winmat[3][1]);
169
170         px= *x - sx;
171         py= *y - sy;
172
173         *y=  (a*(py-f) + d*(c-px))/(a*e-d*b);
174         *x= (px- b*(*y)- c)/a;
175
176         if(block->panel) {
177                 *x -= block->panel->ofsx;
178                 *y -= block->panel->ofsy;
179         }
180 }
181
182 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
183 {
184         float fx, fy;
185
186         fx= *x;
187         fy= *y;
188
189         ui_window_to_block_fl(ar, block, &fx, &fy);
190
191         *x= (int)(fx+0.5f);
192         *y= (int)(fy+0.5f);
193 }
194
195 void ui_window_to_region(const ARegion *ar, int *x, int *y)
196 {
197         *x-= ar->winrct.xmin;
198         *y-= ar->winrct.ymin;
199 }
200
201 /* ******************* block calc ************************* */
202
203 void ui_block_translate(uiBlock *block, int x, int y)
204 {
205         uiBut *bt;
206
207         for(bt= block->buttons.first; bt; bt=bt->next) {
208                 bt->x1 += x;
209                 bt->y1 += y;
210                 bt->x2 += x;
211                 bt->y2 += y;
212         }
213
214         block->minx += x;
215         block->miny += y;
216         block->maxx += x;
217         block->maxy += y;
218 }
219
220 static void ui_text_bounds_block(uiBlock *block, float offset)
221 {
222         uiStyle *style= U.uistyles.first;       // XXX pass on as arg
223         uiBut *bt;
224         int i = 0, j, x1addval= offset, nextcol;
225         
226         uiStyleFontSet(&style->widget);
227         
228         for(bt= block->buttons.first; bt; bt= bt->next) {
229                 if(bt->type!=SEPR) {
230                         //int transopts= ui_translate_buttons();
231                         //if(bt->type==TEX || bt->type==IDPOIN) transopts= 0;
232                         
233                         j= BLF_width(bt->drawstr);
234
235                         if(j > i) i = j;
236                 }
237         }
238
239         /* cope with multi collumns */
240         bt= block->buttons.first;
241         while(bt) {
242                 if(bt->next && bt->x1 < bt->next->x1)
243                         nextcol= 1;
244                 else nextcol= 0;
245                 
246                 bt->x1 = x1addval;
247                 bt->x2 = bt->x1 + i + block->bounds;
248                 
249                 ui_check_but(bt);       // clips text again
250                 
251                 if(nextcol)
252                         x1addval+= i + block->bounds;
253                 
254                 bt= bt->next;
255         }
256 }
257
258 void ui_bounds_block(uiBlock *block)
259 {
260         uiBut *bt;
261         int xof;
262         
263         if(block->buttons.first==NULL) {
264                 if(block->panel) {
265                         block->minx= 0.0; block->maxx= block->panel->sizex;
266                         block->miny= 0.0; block->maxy= block->panel->sizey;
267                 }
268         }
269         else {
270         
271                 block->minx= block->miny= 10000;
272                 block->maxx= block->maxy= -10000;
273                 
274                 bt= block->buttons.first;
275                 while(bt) {
276                         if(bt->x1 < block->minx) block->minx= bt->x1;
277                         if(bt->y1 < block->miny) block->miny= bt->y1;
278         
279                         if(bt->x2 > block->maxx) block->maxx= bt->x2;
280                         if(bt->y2 > block->maxy) block->maxy= bt->y2;
281                         
282                         bt= bt->next;
283                 }
284                 
285                 block->minx -= block->bounds;
286                 block->miny -= block->bounds;
287                 block->maxx += block->bounds;
288                 block->maxy += block->bounds;
289         }
290
291         /* hardcoded exception... but that one is annoying with larger safety */ 
292         bt= block->buttons.first;
293         if(bt && strncmp(bt->str, "ERROR", 5)==0) xof= 10;
294         else xof= 40;
295         
296         block->safety.xmin= block->minx-xof;
297         block->safety.ymin= block->miny-xof;
298         block->safety.xmax= block->maxx+xof;
299         block->safety.ymax= block->maxy+xof;
300 }
301
302 static void ui_popup_bounds_block(const bContext *C, uiBlock *block, int menu)
303 {
304         wmWindow *window= CTX_wm_window(C);
305         int startx, starty, endx, endy, width, height;
306         int oldbounds, mx, my, xmax, ymax;
307
308         oldbounds= block->bounds;
309
310         /* compute mouse position with user defined offset */
311         ui_bounds_block(block);
312         mx= window->eventstate->x + block->minx + block->mx;
313         my= window->eventstate->y + block->miny + block->my;
314
315         wm_window_get_size(window, &xmax, &ymax);
316
317         /* first we ensure wide enough text bounds */
318         if(menu) {
319                 if(block->flag & UI_BLOCK_LOOP) {
320                         block->bounds= 50;
321                         ui_text_bounds_block(block, block->minx);
322                 }
323         }
324
325         /* next we recompute bounds */
326         block->bounds= oldbounds;
327         ui_bounds_block(block);
328
329         /* and we adjust the position to fit within window */
330         width= block->maxx - block->minx;
331         height= block->maxy - block->miny;
332
333         startx= mx-(0.8*(width));
334         starty= my;
335         
336         if(startx<10)
337                 startx= 10;
338         if(starty<10)
339                 starty= 10;
340         
341         endx= startx+width;
342         endy= starty+height;
343         
344         if(endx>xmax) {
345                 endx= xmax-10;
346                 startx= endx-width;
347         }
348         if(endy>ymax-20) {
349                 endy= ymax-20;
350                 starty= endy-height;
351         }
352
353         ui_block_translate(block, startx - block->minx, starty - block->miny);
354
355         /* now recompute bounds and safety */
356         ui_bounds_block(block);
357 }
358
359 /* used for various cases */
360 void uiBoundsBlock(uiBlock *block, int addval)
361 {
362         if(block==NULL)
363                 return;
364         
365         block->bounds= addval;
366         block->dobounds= 1;
367 }
368
369 /* used for pulldowns */
370 void uiTextBoundsBlock(uiBlock *block, int addval)
371 {
372         block->bounds= addval;
373         block->dobounds= 2;
374 }
375
376 /* used for block popups */
377 void uiPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
378 {
379         block->bounds= addval;
380         block->dobounds= 3;
381         block->mx= mx;
382         block->my= my;
383 }
384
385 /* used for menu popups */
386 void uiMenuPopupBoundsBlock(uiBlock *block, int addval, int mx, int my)
387 {
388         block->bounds= addval;
389         block->dobounds= 4;
390         block->mx= mx;
391         block->my= my;
392 }
393
394 /* ************** LINK LINE DRAWING  ************* */
395
396 /* link line drawing is not part of buttons or theme.. so we stick with it here */
397
398 static void ui_draw_linkline(uiBut *but, uiLinkLine *line)
399 {
400         rcti rect;
401
402         if(line->from==NULL || line->to==NULL) return;
403         
404         rect.xmin= (line->from->x1+line->from->x2)/2.0;
405         rect.ymin= (line->from->y1+line->from->y2)/2.0;
406         rect.xmax= (line->to->x1+line->to->x2)/2.0;
407         rect.ymax= (line->to->y1+line->to->y2)/2.0;
408         
409         if(line->flag & UI_SELECT) 
410                 glColor3ub(100,100,100);
411         else 
412                 glColor3ub(0,0,0);
413
414         ui_draw_link_bezier(&rect);
415 }
416
417 static void ui_draw_links(uiBlock *block)
418 {
419         uiBut *but;
420         uiLinkLine *line;
421         
422         but= block->buttons.first;
423         while(but) {
424                 if(but->type==LINK && but->link) {
425                         line= but->link->lines.first;
426                         while(line) {
427                                 ui_draw_linkline(but, line);
428                                 line= line->next;
429                         }
430                 }
431                 but= but->next;
432         }       
433 }
434
435 /* ************** BLOCK ENDING FUNCTION ************* */
436
437 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
438 static int ui_but_equals_old(uiBut *but, uiBut *oldbut)
439 {
440         /* various properties are being compared here, hopfully sufficient
441          * to catch all cases, but it is simple to add more checks later */
442         if(but->retval != oldbut->retval) return 0;
443         if(but->rnapoin.data != oldbut->rnapoin.data) return 0;
444         if(but->rnaprop != oldbut->rnaprop)
445                 if(but->rnaindex != oldbut->rnaindex) return 0;
446         if(but->func != oldbut->func) return 0;
447         if(but->funcN != oldbut->funcN) return 0;
448         if(oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return 0;
449         if(oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return 0;
450         if(!but->funcN && ((but->poin != oldbut->poin && (uiBut*)oldbut->poin != oldbut) || but->pointype != oldbut->pointype)) return 0;
451
452         return 1;
453 }
454
455 static int ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut *but)
456 {
457         uiBlock *oldblock;
458         uiBut *oldbut;
459         int found= 0;
460
461         oldblock= block->oldblock;
462         if(!oldblock)
463                 return found;
464
465         for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
466                 if(ui_but_equals_old(oldbut, but)) {
467                         if(oldbut->active) {
468                                 but->flag= oldbut->flag;
469                                 but->active= oldbut->active;
470                                 but->pos= oldbut->pos;
471                                 but->editstr= oldbut->editstr;
472                                 but->editval= oldbut->editval;
473                                 but->editvec= oldbut->editvec;
474                                 but->editcoba= oldbut->editcoba;
475                                 but->editcumap= oldbut->editcumap;
476                                 but->selsta= oldbut->selsta;
477                                 but->selend= oldbut->selend;
478                                 but->softmin= oldbut->softmin;
479                                 but->softmax= oldbut->softmax;
480                                 but->linkto[0]= oldbut->linkto[0];
481                                 but->linkto[1]= oldbut->linkto[1];
482                                 found= 1;
483
484                                 oldbut->active= NULL;
485                         }
486
487                         /* ensures one button can get activated, and in case the buttons
488                          * draw are the same this gives O(1) lookup for each button */
489                         BLI_remlink(&oldblock->buttons, oldbut);
490                         ui_free_but(C, oldbut);
491                         
492                         break;
493                 }
494         }
495         
496         return found;
497 }
498
499 /* needed for temporarily rename buttons, such as in outliner or fileselect,
500    they should keep calling uiDefButs to keep them alive */
501 /* returns 0 when button removed */
502 int uiButActiveOnly(const bContext *C, uiBlock *block, uiBut *but)
503 {
504         uiBlock *oldblock;
505         uiBut *oldbut;
506         int activate= 0, found= 0, isactive= 0;
507         
508         oldblock= block->oldblock;
509         if(!oldblock)
510                 activate= 1;
511         else {
512                 for(oldbut=oldblock->buttons.first; oldbut; oldbut=oldbut->next) {
513                         if(ui_but_equals_old(oldbut, but)) {
514                                 found= 1;
515                                 
516                                 if(oldbut->active)
517                                         isactive= 1;
518                                 
519                                 break;
520                         }
521                 }
522         }
523         if(activate || found==0) {
524                 ui_button_activate_do( (bContext *)C, CTX_wm_region(C), but);
525         }
526         else if(found && isactive==0) {
527                 
528                 BLI_remlink(&block->buttons, but);
529                 ui_free_but(C, but);
530                 return 0;
531         }
532         
533         return 1;
534 }
535
536 void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
537 {
538         uiBut *but;
539         IDProperty *prop;
540         char buf[512], *butstr;
541
542         /* only do it before bounding */
543         if(block->minx != block->maxx)
544                 return;
545
546         for(but=block->buttons.first; but; but=but->next) {
547                 if(but->optype) {
548                         prop= (but->opptr)? but->opptr->data: NULL;
549
550                         if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, buf, sizeof(buf))) {
551                                 butstr= MEM_mallocN(strlen(but->str)+strlen(buf)+2, "menu_block_set_keymaps");
552                                 strcpy(butstr, but->str);
553                                 strcat(butstr, "|");
554                                 strcat(butstr, buf);
555
556                                 but->str= but->strdata;
557                                 BLI_strncpy(but->str, butstr, sizeof(but->strdata));
558                                 MEM_freeN(butstr);
559
560                                 ui_check_but(but);
561                         }
562                 }
563         }
564 }
565
566 void uiEndBlock(const bContext *C, uiBlock *block)
567 {
568         uiBut *but;
569         Scene *scene= CTX_data_scene(C);
570
571         /* inherit flags from 'old' buttons that was drawn here previous, based
572          * on matching buttons, we need this to make button event handling non
573          * blocking, while still alowing buttons to be remade each redraw as it
574          * is expected by blender code */
575         for(but=block->buttons.first; but; but=but->next) {
576                 if(ui_but_update_from_old_block(C, block, but))
577                         ui_check_but(but);
578                 
579                 /* temp? Proper check for greying out */
580                 if(but->optype) {
581                         wmOperatorType *ot= but->optype;
582
583                         if(but->context)
584                                 CTX_store_set((bContext*)C, but->context);
585
586                         if(ot==NULL || (ot->poll && ot->poll((bContext *)C)==0)) {
587                                 but->flag |= UI_BUT_DISABLED;
588                                 but->lock = 1;
589                         }
590
591                         if(but->context)
592                                 CTX_store_set((bContext*)C, NULL);
593                 }
594
595                 /* only update soft range while not editing */
596                 if(but->rnaprop && !(but->editval || but->editstr || but->editvec))
597                         ui_set_but_soft_range(but, ui_get_but_val(but));
598
599                 ui_but_anim_flag(but, (scene)? scene->r.cfra: 0.0f);
600         }
601
602         if(block->oldblock) {
603                 block->auto_open= block->oldblock->auto_open;
604                 block->auto_open_last= block->oldblock->auto_open_last;
605                 block->tooltipdisabled= block->oldblock->tooltipdisabled;
606
607                 block->oldblock= NULL;
608         }
609
610         /* handle pending stuff */
611         if(block->layouts.first) uiBlockLayoutResolve(C, block, NULL, NULL);
612         ui_block_do_align(block);
613         if(block->flag & UI_BLOCK_LOOP) ui_menu_block_set_keymaps(C, block);
614         
615         /* after keymaps! */
616         if(block->dobounds == 1) ui_bounds_block(block);
617         else if(block->dobounds == 2) ui_text_bounds_block(block, 0.0f);
618         else if(block->dobounds) ui_popup_bounds_block(C, block, (block->dobounds == 4));
619
620         if(block->minx==0.0 && block->maxx==0.0) uiBoundsBlock(block, 0);
621         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
622
623         block->endblock= 1;
624 }
625
626 /* ************** BLOCK DRAWING FUNCTION ************* */
627
628 void ui_fontscale(short *points, float aspect)
629 {
630         if(aspect < 0.9f || aspect > 1.1f) {
631                 float pointsf= *points;
632                 
633                 /* for some reason scaling fonts goes too fast compared to widget size */
634                 aspect= sqrt(aspect);
635                 pointsf /= aspect;
636                 
637                 if(aspect > 1.0)
638                         *points= ceil(pointsf);
639                 else
640                         *points= floor(pointsf);
641         }
642 }
643
644 /* project button or block (but==NULL) to pixels in regionspace */
645 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
646 {
647         float gx, gy;
648         float getsizex, getsizey;
649         
650         getsizex= ar->winx;
651         getsizey= ar->winy;
652
653         gx= (but?but->x1:block->minx) + (block->panel?block->panel->ofsx:0.0f);
654         gy= (but?but->y1:block->miny) + (block->panel?block->panel->ofsy:0.0f);
655         
656         rect->xmin= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
657         rect->ymin= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
658         
659         gx= (but?but->x2:block->maxx) + (block->panel?block->panel->ofsx:0.0f);
660         gy= (but?but->y2:block->maxy) + (block->panel?block->panel->ofsy:0.0f);
661         
662         rect->xmax= floor(getsizex*(0.5+ 0.5*(gx*block->winmat[0][0]+ gy*block->winmat[1][0]+ block->winmat[3][0])));
663         rect->ymax= floor(getsizey*(0.5+ 0.5*(gx*block->winmat[0][1]+ gy*block->winmat[1][1]+ block->winmat[3][1])));
664
665 }
666
667 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
668 void uiDrawBlock(const bContext *C, uiBlock *block)
669 {
670         uiStyle style= *((uiStyle *)U.uistyles.first);  // XXX pass on as arg
671         ARegion *ar;
672         uiBut *but;
673         rcti rect;
674         
675         /* get menu region or area region */
676         ar= CTX_wm_menu(C);
677         if(!ar)
678                 ar= CTX_wm_region(C);
679
680         if(!block->endblock)
681                 uiEndBlock(C, block);
682
683         /* we set this only once */
684         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
685         
686         /* scale fonts */
687         ui_fontscale(&style.paneltitle.points, block->aspect);
688         ui_fontscale(&style.grouplabel.points, block->aspect);
689         ui_fontscale(&style.widgetlabel.points, block->aspect);
690         ui_fontscale(&style.widget.points, block->aspect);
691         
692         /* scale block min/max to rect */
693         ui_but_to_pixelrect(&rect, ar, block, NULL);
694         
695         /* pixel space for AA widgets */
696         wmPushMatrix();
697         wmLoadIdentity();
698         
699         wmOrtho2(-0.01f, ar->winx-0.01f, -0.01f, ar->winy-0.01f);
700         
701         /* back */
702         if(block->flag & UI_BLOCK_LOOP)
703                 ui_draw_menu_back(&style, block, &rect);
704         else if(block->panel)
705                 ui_draw_aligned_panel(ar, &style, block, &rect);
706
707         /* widgets */
708         for(but= block->buttons.first; but; but= but->next) {
709                 ui_but_to_pixelrect(&rect, ar, block, but);
710                 if(!(but->flag & UI_HIDDEN))
711                         ui_draw_but(C, ar, &style, but, &rect);
712         }
713         
714         /* restore matrix */
715         wmPopMatrix();
716         
717         ui_draw_links(block);
718 }
719
720 /* ************* EVENTS ************* */
721
722 static void ui_is_but_sel(uiBut *but)
723 {
724         double value;
725         int lvalue;
726         short push=0, true=1;
727
728         value= ui_get_but_val(but);
729
730         if(ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) true= 0;
731
732         if( but->bit ) {
733                 lvalue= (int)value;
734                 if( BTST(lvalue, (but->bitnr)) ) push= true;
735                 else push= !true;
736         }
737         else {
738                 switch(but->type) {
739                 case BUT:
740                         push= 2;
741                         break;
742                 case HOTKEYEVT:
743                 case KEYEVT:
744                         push= 2;
745                         break;
746                 case TOGBUT:
747                 case TOG:
748                 case TOGR:
749                 case TOG3:
750                 case BUT_TOGDUAL:
751                 case ICONTOG:
752                 case OPTION:
753                         if(value!=but->hardmin) push= 1;
754                         break;
755                 case ICONTOGN:
756                 case TOGN:
757                 case OPTIONN:
758                         if(value==0.0) push= 1;
759                         break;
760                 case ROW:
761                 case LISTROW:
762                         if(value == but->hardmax) push= 1;
763                         break;
764                 case COL:
765                         push= 1;
766                         break;
767                 default:
768                         push= 2;
769                         break;
770                 }
771         }
772         
773         if(push==2);
774         else if(push==1) but->flag |= UI_SELECT;
775         else but->flag &= ~UI_SELECT;
776 }
777
778 /* XXX 2.50 no links supported yet */
779
780 static int uibut_contains_pt(uiBut *but, short *mval)
781 {
782         return 0;
783
784 }
785
786 uiBut *ui_get_valid_link_button(uiBlock *block, uiBut *but, short *mval)
787 {
788         uiBut *bt;
789         
790                 /* find button to link to */
791         for (bt= block->buttons.first; bt; bt= bt->next)
792                 if(bt!=but && uibut_contains_pt(bt, mval))
793                         break;
794
795         if (bt) {
796                 if (but->type==LINK && bt->type==INLINK) {
797                         if( but->link->tocode == (int)bt->hardmin ) {
798                                 return bt;
799                         }
800                 }
801                 else if(but->type==INLINK && bt->type==LINK) {
802                         if( bt->link->tocode == (int)but->hardmin ) {
803                                 return bt;
804                         }
805                 }
806         }
807
808         return NULL;
809 }
810
811
812 static uiBut *ui_find_inlink(uiBlock *block, void *poin)
813 {
814         uiBut *but;
815         
816         but= block->buttons.first;
817         while(but) {
818                 if(but->type==INLINK) {
819                         if(but->poin == poin) return but;
820                 }
821                 but= but->next;
822         }
823         return NULL;
824 }
825
826 static void ui_add_link_line(ListBase *listb, uiBut *but, uiBut *bt)
827 {
828         uiLinkLine *line;
829         
830         line= MEM_callocN(sizeof(uiLinkLine), "linkline");
831         BLI_addtail(listb, line);
832         line->from= but;
833         line->to= bt;
834 }
835
836 uiBut *uiFindInlink(uiBlock *block, void *poin)
837 {
838         return ui_find_inlink(block, poin);
839 }
840
841 void uiComposeLinks(uiBlock *block)
842 {
843         uiBut *but, *bt;
844         uiLink *link;
845         void ***ppoin;
846         int a;
847         
848         but= block->buttons.first;
849         while(but) {
850                 if(but->type==LINK) {
851                         link= but->link;
852                         
853                         /* for all pointers in the array */
854                         if(link) {
855                                 if(link->ppoin) {
856                                         ppoin= link->ppoin;
857                                         for(a=0; a < *(link->totlink); a++) {
858                                                 bt= ui_find_inlink(block, (*ppoin)[a] );
859                                                 if(bt) {
860                                                         ui_add_link_line(&link->lines, but, bt);
861                                                 }
862                                         }
863                                 }
864                                 else if(link->poin) {
865                                         bt= ui_find_inlink(block, *(link->poin) );
866                                         if(bt) {
867                                                 ui_add_link_line(&link->lines, but, bt);
868                                         }
869                                 }
870                         }
871                 }
872                 but= but->next;
873         }
874 }
875
876
877 /* ************************************************ */
878
879 void uiBlockSetButLock(uiBlock *block, int val, char *lockstr)
880 {
881         if(val) {
882                 block->lock |= val;
883                 block->lockstr= lockstr;
884         }
885 }
886
887 void uiBlockClearButLock(uiBlock *block)
888 {
889         block->lock= 0;
890         block->lockstr= NULL;
891 }
892
893 /* *************************************************************** */
894
895
896 /* XXX 2.50 no links supported yet */
897 #if 0
898 static void ui_delete_active_linkline(uiBlock *block)
899 {
900         uiBut *but;
901         uiLink *link;
902         uiLinkLine *line, *nline;
903         int a, b;
904         
905         but= block->buttons.first;
906         while(but) {
907                 if(but->type==LINK && but->link) {
908                         line= but->link->lines.first;
909                         while(line) {
910                                 
911                                 nline= line->next;
912                                 
913                                 if(line->flag & UI_SELECT) {
914                                         BLI_remlink(&but->link->lines, line);
915
916                                         link= line->from->link;
917
918                                         /* are there more pointers allowed? */
919                                         if(link->ppoin) {
920                                                 
921                                                 if(*(link->totlink)==1) {
922                                                         *(link->totlink)= 0;
923                                                         MEM_freeN(*(link->ppoin));
924                                                         *(link->ppoin)= NULL;
925                                                 }
926                                                 else {
927                                                         b= 0;
928                                                         for(a=0; a< (*(link->totlink)); a++) {
929                                                                 
930                                                                 if( (*(link->ppoin))[a] != line->to->poin ) {
931                                                                         (*(link->ppoin))[b]= (*(link->ppoin))[a];
932                                                                         b++;
933                                                                 }
934                                                         }       
935                                                         (*(link->totlink))--;
936                                                 }
937                                         }
938                                         else {
939                                                 *(link->poin)= NULL;
940                                         }
941
942                                         MEM_freeN(line);
943                                 }
944                                 line= nline;
945                         }
946                 }
947                 but= but->next;
948         }
949         
950         /* temporal! these buttons can be everywhere... */
951         allqueue(REDRAWBUTSLOGIC, 0);
952 }
953
954 static void ui_do_active_linklines(uiBlock *block, short *mval)
955 {
956         uiBut *but;
957         uiLinkLine *line, *act= NULL;
958         float mindist= 12.0, fac, v1[2], v2[2], v3[3];
959         int foundone= 0; 
960         
961         if(mval) {
962                 v1[0]= mval[0];
963                 v1[1]= mval[1];
964                 
965                 /* find a line close to the mouse */
966                 but= block->buttons.first;
967                 while(but) {
968                         if(but->type==LINK && but->link) {
969                                 foundone= 1;
970                                 line= but->link->lines.first;
971                                 while(line) {
972                                         v2[0]= line->from->x2;
973                                         v2[1]= (line->from->y1+line->from->y2)/2.0;
974                                         v3[0]= line->to->x1;
975                                         v3[1]= (line->to->y1+line->to->y2)/2.0;
976                                         
977                                         fac= PdistVL2Dfl(v1, v2, v3);
978                                         if(fac < mindist) {
979                                                 mindist= fac;
980                                                 act= line;
981                                         }
982                                         line= line->next;
983                                 }
984                         }
985                         but= but->next;
986                 }
987         }
988
989         /* check for a 'found one' to prevent going to 'frontbuffer' mode.
990                 this slows done gfx quite some, and at OSX the 'finish' forces a swapbuffer */
991         if(foundone) {
992                 glDrawBuffer(GL_FRONT);
993                 
994                 /* draw */
995                 but= block->buttons.first;
996                 while(but) {
997                         if(but->type==LINK && but->link) {
998                                 line= but->link->lines.first;
999                                 while(line) {
1000                                         if(line==act) {
1001                                                 if((line->flag & UI_SELECT)==0) {
1002                                                         line->flag |= UI_SELECT;
1003                                                         ui_draw_linkline(but, line);
1004                                                 }
1005                                         }
1006                                         else if(line->flag & UI_SELECT) {
1007                                                 line->flag &= ~UI_SELECT;
1008                                                 ui_draw_linkline(but, line);
1009                                         }
1010                                         line= line->next;
1011                                 }
1012                         }
1013                         but= but->next;
1014                 }
1015                 bglFlush();
1016                 glDrawBuffer(GL_BACK);
1017         }
1018 }
1019 #endif
1020
1021 /* ******************************************************* */
1022
1023 /* XXX 2.50 no screendump supported yet */
1024
1025 #if 0
1026 /* nasty but safe way to store screendump rect */
1027 static int scr_x=0, scr_y=0, scr_sizex=0, scr_sizey=0;
1028
1029 static void ui_set_screendump_bbox(uiBlock *block)
1030 {
1031         if(block) {
1032                 scr_x= block->minx;
1033                 scr_y= block->miny;
1034                 scr_sizex= block->maxx - block->minx;
1035                 scr_sizey= block->maxy - block->miny;
1036         }
1037         else {
1038                 scr_sizex= scr_sizey= 0;
1039         }
1040 }
1041
1042 /* used for making screenshots for menus, called in screendump.c */
1043 int uiIsMenu(int *x, int *y, int *sizex, int *sizey)
1044 {
1045         if(scr_sizex!=0 && scr_sizey!=0) {
1046                 *x= scr_x;
1047                 *y= scr_y;
1048                 *sizex= scr_sizex;
1049                 *sizey= scr_sizey;
1050                 return 1;
1051         }
1052         
1053         return 0;
1054 }
1055 #endif
1056
1057 /* *********************** data get/set ***********************
1058  * this either works with the pointed to data, or can work with
1059  * an edit override pointer while dragging for example */
1060
1061 /* for buttons pointing to color for example */
1062 void ui_get_but_vectorf(uiBut *but, float *vec)
1063 {
1064         PropertyRNA *prop;
1065         int a, tot;
1066
1067         if(but->editvec) {
1068                 VECCOPY(vec, but->editvec);
1069                 return;
1070         }
1071
1072         if(but->rnaprop) {
1073                 prop= but->rnaprop;
1074
1075                 vec[0]= vec[1]= vec[2]= 0.0f;
1076
1077                 if(RNA_property_type(prop) == PROP_FLOAT) {
1078                         tot= RNA_property_array_length(prop);
1079                         tot= MIN2(tot, 3);
1080
1081                         for(a=0; a<tot; a++)
1082                                 vec[a]= RNA_property_float_get_index(&but->rnapoin, prop, a);
1083                 }
1084         }
1085         else if(but->pointype == CHA) {
1086                 char *cp= (char *)but->poin;
1087                 vec[0]= ((float)cp[0])/255.0;
1088                 vec[1]= ((float)cp[1])/255.0;
1089                 vec[2]= ((float)cp[2])/255.0;
1090         }
1091         else if(but->pointype == FLO) {
1092                 float *fp= (float *)but->poin;
1093                 VECCOPY(vec, fp);
1094         }
1095 }
1096
1097 /* for buttons pointing to color for example */
1098 void ui_set_but_vectorf(uiBut *but, float *vec)
1099 {
1100         PropertyRNA *prop;
1101         int a, tot;
1102
1103         if(but->editvec) {
1104                 VECCOPY(but->editvec, vec);
1105                 return;
1106         }
1107
1108         if(but->rnaprop) {
1109                 prop= but->rnaprop;
1110
1111                 if(RNA_property_type(prop) == PROP_FLOAT) {
1112                         tot= RNA_property_array_length(prop);
1113                         tot= MIN2(tot, 3);
1114
1115                         for(a=0; a<tot; a++)
1116                                 RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1117                 }
1118         }
1119         else if(but->pointype == CHA) {
1120                 char *cp= (char *)but->poin;
1121                 cp[0]= (char)(0.5 +vec[0]*255.0);
1122                 cp[1]= (char)(0.5 +vec[1]*255.0);
1123                 cp[2]= (char)(0.5 +vec[2]*255.0);
1124         }
1125         else if(but->pointype == FLO) {
1126                 float *fp= (float *)but->poin;
1127                 VECCOPY(fp, vec);
1128         }
1129 }
1130
1131 int ui_is_but_float(uiBut *but)
1132 {
1133         if(but->pointype==FLO && but->poin)
1134                 return 1;
1135         
1136         if(but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1137                 return 1;
1138         
1139         return 0;
1140 }
1141
1142 double ui_get_but_val(uiBut *but)
1143 {
1144         PropertyRNA *prop;
1145         double value = 0.0;
1146
1147         if(but->editval) { return *(but->editval); }
1148         if(but->poin==NULL && but->rnapoin.data==NULL) return 0.0;
1149
1150         if(but->rnaprop) {
1151                 prop= but->rnaprop;
1152
1153                 switch(RNA_property_type(prop)) {
1154                         case PROP_BOOLEAN:
1155                                 if(RNA_property_array_length(prop))
1156                                         value= RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1157                                 else
1158                                         value= RNA_property_boolean_get(&but->rnapoin, prop);
1159                                 break;
1160                         case PROP_INT:
1161                                 if(RNA_property_array_length(prop))
1162                                         value= RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1163                                 else
1164                                         value= RNA_property_int_get(&but->rnapoin, prop);
1165                                 break;
1166                         case PROP_FLOAT:
1167                                 if(RNA_property_array_length(prop))
1168                                         value= RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1169                                 else
1170                                         value= RNA_property_float_get(&but->rnapoin, prop);
1171                                 break;
1172                         case PROP_ENUM:
1173                                 value= RNA_property_enum_get(&but->rnapoin, prop);
1174                                 break;
1175                         default:
1176                                 value= 0.0;
1177                                 break;
1178                 }
1179         }
1180         else if(but->type== HSVSLI) {
1181                 float h, s, v, *fp;
1182                 
1183                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1184                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1185
1186                 switch(but->str[0]) {
1187                         case 'H': value= h; break;
1188                         case 'S': value= s; break;
1189                         case 'V': value= v; break;
1190                 }
1191         } 
1192         else if( but->pointype == CHA ) {
1193                 value= *(char *)but->poin;
1194         }
1195         else if( but->pointype == SHO ) {
1196                 value= *(short *)but->poin;
1197         } 
1198         else if( but->pointype == INT ) {
1199                 value= *(int *)but->poin;
1200         } 
1201         else if( but->pointype == FLO ) {
1202                 value= *(float *)but->poin;
1203         }
1204     
1205         return value;
1206 }
1207
1208 void ui_set_but_val(uiBut *but, double value)
1209 {
1210         PropertyRNA *prop;
1211
1212         /* value is a hsv value: convert to rgb */
1213         if(but->rnaprop) {
1214                 prop= but->rnaprop;
1215
1216                 if(RNA_property_editable(&but->rnapoin, prop)) {
1217                         switch(RNA_property_type(prop)) {
1218                                 case PROP_BOOLEAN:
1219                                         if(RNA_property_array_length(prop))
1220                                                 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1221                                         else
1222                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1223                                         break;
1224                                 case PROP_INT:
1225                                         if(RNA_property_array_length(prop))
1226                                                 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, value);
1227                                         else
1228                                                 RNA_property_int_set(&but->rnapoin, prop, value);
1229                                         break;
1230                                 case PROP_FLOAT:
1231                                         if(RNA_property_array_length(prop))
1232                                                 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1233                                         else
1234                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1235                                         break;
1236                                 case PROP_ENUM:
1237                                         RNA_property_enum_set(&but->rnapoin, prop, value);
1238                                         break;
1239                                 default:
1240                                         break;
1241                         }
1242                 }
1243         }
1244         else if(but->pointype==0);
1245         else if(but->type==HSVSLI ) {
1246                 float h, s, v, *fp;
1247                 
1248                 fp= (but->editvec)? but->editvec: (float *)but->poin;
1249                 rgb_to_hsv(fp[0], fp[1], fp[2], &h, &s, &v);
1250                 
1251                 switch(but->str[0]) {
1252                 case 'H': h= value; break;
1253                 case 'S': s= value; break;
1254                 case 'V': v= value; break;
1255                 }
1256                 
1257                 hsv_to_rgb(h, s, v, fp, fp+1, fp+2);
1258                 
1259         }
1260         else {
1261                 /* first do rounding */
1262                 if(but->pointype==CHA)
1263                         value= (char)floor(value+0.5);
1264                 else if(but->pointype==SHO ) {
1265                         /* gcc 3.2.1 seems to have problems 
1266                          * casting a double like 32772.0 to
1267                          * a short so we cast to an int, then 
1268                          to a short */
1269                         int gcckludge;
1270                         gcckludge = (int) floor(value+0.5);
1271                         value= (short)gcckludge;
1272                 }
1273                 else if(but->pointype==INT )
1274                         value= (int)floor(value+0.5);
1275                 else if(but->pointype==FLO ) {
1276                         float fval= (float)value;
1277                         if(fval>= -0.00001f && fval<= 0.00001f) fval= 0.0f;     /* prevent negative zero */
1278                         value= fval;
1279                 }
1280                 
1281                 /* then set value with possible edit override */
1282                 if(but->editval)
1283                         *but->editval= value;
1284                 else if(but->pointype==CHA)
1285                         *((char *)but->poin)= (char)value;
1286                 else if(but->pointype==SHO)
1287                         *((short *)but->poin)= (short)value;
1288                 else if(but->pointype==INT)
1289                         *((int *)but->poin)= (int)value;
1290                 else if(but->pointype==FLO)
1291                         *((float *)but->poin)= (float)value;
1292         }
1293
1294         /* update select flag */
1295         ui_is_but_sel(but);
1296 }
1297
1298 int ui_get_but_string_max_length(uiBut *but)
1299 {
1300         if(but->type == TEX)
1301                 return but->hardmax;
1302         else if(but->type == IDPOIN)
1303                 return sizeof(((ID*)NULL)->name)-2;
1304         else
1305                 return UI_MAX_DRAW_STR;
1306 }
1307
1308 void ui_get_but_string(uiBut *but, char *str, int maxlen)
1309 {
1310         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1311                 PropertyType type;
1312                 char *buf= NULL;
1313
1314                 type= RNA_property_type(but->rnaprop);
1315
1316                 if(type == PROP_STRING) {
1317                         /* RNA string */
1318                         buf= RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen);
1319                 }
1320                 else if(type == PROP_POINTER) {
1321                         /* RNA pointer */
1322                         PointerRNA ptr= RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
1323                         buf= RNA_struct_name_get_alloc(&ptr, str, maxlen);
1324                 }
1325
1326                 if(!buf) {
1327                         BLI_strncpy(str, "", maxlen);
1328                 }
1329                 else if(buf && buf != str) {
1330                         /* string was too long, we have to truncate */
1331                         BLI_strncpy(str, buf, maxlen);
1332                         MEM_freeN(buf);
1333                 }
1334         }
1335         else if(but->type == IDPOIN) {
1336                 /* ID pointer */
1337                 ID *id= *(but->idpoin_idpp);
1338
1339                 if(id) BLI_strncpy(str, id->name+2, maxlen);
1340                 else BLI_strncpy(str, "", maxlen);
1341
1342                 return;
1343         }
1344         else if(but->type == TEX) {
1345                 /* string */
1346                 BLI_strncpy(str, but->poin, maxlen);
1347                 return;
1348         }
1349         else if(but->type == SEARCH_MENU) {
1350                 /* string */
1351                 BLI_strncpy(str, but->poin, maxlen);
1352                 return;
1353         }
1354         else if(ui_but_anim_expression_get(but, str, maxlen))
1355                 ; /* driver expression */
1356         else {
1357                 /* number editing */
1358                 double value;
1359
1360                 value= ui_get_but_val(but);
1361
1362                 if(ui_is_but_float(but)) {
1363                         if(but->a2) { /* amount of digits defined */
1364                                 if(but->a2==1) BLI_snprintf(str, maxlen, "%.1f", value);
1365                                 else if(but->a2==2) BLI_snprintf(str, maxlen, "%.2f", value);
1366                                 else if(but->a2==3) BLI_snprintf(str, maxlen, "%.3f", value);
1367                                 else BLI_snprintf(str, maxlen, "%.4f", value);
1368                         }
1369                         else
1370                                 BLI_snprintf(str, maxlen, "%.3f", value);
1371                 }
1372                 else
1373                         BLI_snprintf(str, maxlen, "%d", (int)value);
1374         }
1375 }
1376
1377 int ui_set_but_string(bContext *C, uiBut *but, const char *str)
1378 {
1379         if(but->rnaprop && ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1380                 if(RNA_property_editable(&but->rnapoin, but->rnaprop)) {
1381                         PropertyType type;
1382
1383                         type= RNA_property_type(but->rnaprop);
1384
1385                         if(type == PROP_STRING) {
1386                                 /* RNA string */
1387                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
1388                                 return 1;
1389                         }
1390                         else if(type == PROP_POINTER) {
1391                                 /* RNA pointer */
1392                                 PointerRNA ptr, rptr;
1393                                 PropertyRNA *prop;
1394
1395                                 if(str == NULL || str[0] == '\0') {
1396                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
1397                                         return 1;
1398                                 }
1399                                 else {
1400                                         ptr= but->rnasearchpoin;
1401                                         prop= but->rnasearchprop;
1402                                         
1403                                         if(prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
1404                                                 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
1405
1406                                         return 1;
1407                                 }
1408
1409                                 return 0;
1410                         }
1411                 }
1412         }
1413         else if(but->type == IDPOIN) {
1414                 /* ID pointer */
1415         but->idpoin_func(C, (char*)str, but->idpoin_idpp);
1416                 return 1;
1417         }
1418         else if(but->type == TEX) {
1419                 /* string */
1420                 BLI_strncpy(but->poin, str, but->hardmax);
1421                 return 1;
1422         }
1423         else if(but->type == SEARCH_MENU) {
1424                 /* string */
1425                 BLI_strncpy(but->poin, str, but->hardmax);
1426                 return 1;
1427         }
1428         else if(ui_but_anim_expression_set(but, str)) {
1429                 /* driver expression */
1430                 return 1;
1431         }
1432         else {
1433                 /* number editing */
1434                 double value;
1435
1436                 /* XXX 2.50 missing python api */
1437 #if 0
1438                 if(BPY_button_eval(str, &value)) {
1439                         BKE_report(CTX_reports(C), RPT_WARNING, "Invalid Python expression, check console");
1440                         value = 0.0f; /* Zero out value on error */
1441                         
1442                         if(str[0])
1443                                 return 0;
1444                 }
1445 #else
1446                 value= atof(str);
1447 #endif
1448
1449                 if(!ui_is_but_float(but)) value= (int)value;
1450                 if(but->type==NUMABS) value= fabs(value);
1451
1452                 /* not that we use hard limits here */
1453                 if(value<but->hardmin) value= but->hardmin;
1454                 if(value>but->hardmax) value= but->hardmax;
1455
1456                 ui_set_but_val(but, value);
1457                 return 1;
1458         }
1459
1460         return 0;
1461 }
1462
1463 static double soft_range_round_up(double value, double max)
1464 {
1465         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1466         double newmax= pow(10.0, ceil(log(value)/log(10.0)));
1467
1468         if(newmax*0.2 >= max && newmax*0.2 >= value)
1469                 return newmax*0.2;
1470         else if(newmax*0.5 >= max && newmax*0.5 >= value)
1471                 return newmax*0.5;
1472         else
1473                 return newmax;
1474 }
1475
1476 static double soft_range_round_down(double value, double max)
1477 {
1478         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, .. */
1479         double newmax= pow(10.0, floor(log(value)/log(10.0)));
1480
1481         if(newmax*5.0 <= max && newmax*5.0 <= value)
1482                 return newmax*5.0;
1483         else if(newmax*2.0 <= max && newmax*2.0 <= value)
1484                 return newmax*2.0;
1485         else
1486                 return newmax;
1487 }
1488
1489 void ui_set_but_soft_range(uiBut *but, double value)
1490 {
1491         PropertyType type;
1492         double softmin, softmax, step, precision;
1493         
1494         if(but->rnaprop) {
1495                 type= RNA_property_type(but->rnaprop);
1496
1497                 if(type == PROP_INT) {
1498                         int imin, imax, istep;
1499
1500                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
1501                         softmin= imin;
1502                         softmax= imax;
1503                         step= istep;
1504                         precision= 1;
1505                 }
1506                 else if(type == PROP_FLOAT) {
1507                         float fmin, fmax, fstep, fprecision;
1508
1509                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
1510                         softmin= fmin;
1511                         softmax= fmax;
1512                         step= fstep;
1513                         precision= fprecision;
1514                 }
1515                 else
1516                         return;
1517
1518                 /* clamp button range to something reasonable in case
1519                  * we get -inf/inf from RNA properties */
1520                 softmin= MAX2(softmin, -1e4);
1521                 softmax= MIN2(softmax, 1e4);
1522
1523                 /* if the value goes out of the soft/max range, adapt the range */
1524                 if(value+1e-10 < softmin) {
1525                         if(value < 0.0)
1526                                 softmin= -soft_range_round_up(-value, -softmin);
1527                         else
1528                                 softmin= soft_range_round_down(value, softmin);
1529
1530                         if(softmin < but->hardmin)
1531                                 softmin= but->hardmin;
1532                 }
1533                 else if(value-1e-10 > softmax) {
1534                         if(value < 0.0)
1535                                 softmax= -soft_range_round_down(-value, -softmax);
1536                         else
1537                                 softmax= soft_range_round_up(value, softmax);
1538
1539                         if(softmax > but->hardmax)
1540                                 softmax= but->hardmax;
1541                 }
1542
1543                 but->softmin= softmin;
1544                 but->softmax= softmax;
1545         }
1546 }
1547
1548 /* ******************* Free ********************/
1549
1550 static void ui_free_link(uiLink *link)
1551 {
1552         if(link) {      
1553                 BLI_freelistN(&link->lines);
1554                 MEM_freeN(link);
1555         }
1556 }
1557
1558 /* can be called with C==NULL */
1559 static void ui_free_but(const bContext *C, uiBut *but)
1560 {
1561         if(but->opptr) {
1562                 WM_operator_properties_free(but->opptr);
1563                 MEM_freeN(but->opptr);
1564         }
1565         if(but->func_argN) MEM_freeN(but->func_argN);
1566         if(but->active) {
1567                 /* XXX solve later, buttons should be free-able without context? */
1568                 if(C) 
1569                         ui_button_active_cancel(C, but);
1570                 else
1571                         if(but->active) 
1572                                 MEM_freeN(but->active);
1573         }
1574         if(but->str && but->str != but->strdata) MEM_freeN(but->str);
1575         ui_free_link(but->link);
1576
1577         MEM_freeN(but);
1578 }
1579
1580 /* can be called with C==NULL */
1581 void uiFreeBlock(const bContext *C, uiBlock *block)
1582 {
1583         uiBut *but;
1584
1585         while( (but= block->buttons.first) ) {
1586                 BLI_remlink(&block->buttons, but);      
1587                 ui_free_but(C, but);
1588         }
1589
1590         CTX_store_free_list(&block->contexts);
1591
1592         BLI_freelistN(&block->saferct);
1593         
1594         MEM_freeN(block);
1595 }
1596
1597 /* can be called with C==NULL */
1598 void uiFreeBlocks(const bContext *C, ListBase *lb)
1599 {
1600         uiBlock *block;
1601         
1602         while( (block= lb->first) ) {
1603                 BLI_remlink(lb, block);
1604                 uiFreeBlock(C, block);
1605         }
1606 }
1607
1608 void uiFreeInactiveBlocks(const bContext *C, ListBase *lb)
1609 {
1610         uiBlock *block, *nextblock;
1611
1612         for(block=lb->first; block; block=nextblock) {
1613                 nextblock= block->next;
1614         
1615                 if(!block->handle) {
1616                         if(!block->active) {
1617                                 BLI_remlink(lb, block);
1618                                 uiFreeBlock(C, block);
1619                         }
1620                         else
1621                                 block->active= 0;
1622                 }
1623         }
1624 }
1625
1626 void uiBlockSetRegion(uiBlock *block, ARegion *region)
1627 {
1628         ListBase *lb;
1629         uiBlock *oldblock= NULL;
1630
1631         lb= &region->uiblocks;
1632         
1633         /* each listbase only has one block with this name, free block
1634          * if is already there so it can be rebuilt from scratch */
1635         if(lb) {
1636                 for (oldblock= lb->first; oldblock; oldblock= oldblock->next)
1637                         if (BLI_streq(oldblock->name, block->name))
1638                                 break;
1639
1640                 if (oldblock) {
1641                         oldblock->active= 0;
1642                         oldblock->panel= NULL;
1643                 }
1644         }
1645
1646         block->oldblock= oldblock;
1647
1648         /* at the beginning of the list! for dynamical menus/blocks */
1649         if(lb)
1650                 BLI_addhead(lb, block);
1651 }
1652
1653 uiBlock *uiBeginBlock(const bContext *C, ARegion *region, const char *name, short dt)
1654 {
1655         uiBlock *block;
1656         wmWindow *window;
1657         int getsizex, getsizey;
1658
1659         window= CTX_wm_window(C);
1660
1661         block= MEM_callocN(sizeof(uiBlock), "uiBlock");
1662         block->active= 1;
1663         block->dt= dt;
1664         block->evil_C= (void*)C; // XXX
1665         BLI_strncpy(block->name, name, sizeof(block->name));
1666
1667         if(region)
1668                 uiBlockSetRegion(block, region);
1669
1670         /* window matrix and aspect */
1671         if(region && region->swinid) {
1672                 wm_subwindow_getmatrix(window, region->swinid, block->winmat);
1673                 wm_subwindow_getsize(window, region->swinid, &getsizex, &getsizey);
1674
1675                 /* TODO - investigate why block->winmat[0][0] is negative
1676                  * in the image view when viewRedrawForce is called */
1677                 block->aspect= 2.0/fabs( (getsizex)*block->winmat[0][0]);
1678         }
1679         else {
1680                 /* no subwindow created yet, for menus for example, so we
1681                  * use the main window instead, since buttons are created
1682                  * there anyway */
1683                 wm_subwindow_getmatrix(window, window->screen->mainwin, block->winmat);
1684                 wm_subwindow_getsize(window, window->screen->mainwin, &getsizex, &getsizey);
1685
1686                 block->aspect= 2.0/fabs(getsizex*block->winmat[0][0]);
1687                 block->auto_open= 2;
1688                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
1689         }
1690
1691         return block;
1692 }
1693
1694 uiBlock *uiGetBlock(char *name, ARegion *ar)
1695 {
1696         uiBlock *block= ar->uiblocks.first;
1697         
1698         while(block) {
1699                 if( strcmp(name, block->name)==0 ) return block;
1700                 block= block->next;
1701         }
1702         
1703         return NULL;
1704 }
1705
1706 void uiBlockSetEmboss(uiBlock *block, short dt)
1707 {
1708         block->dt= dt;
1709 }
1710
1711 void ui_check_but(uiBut *but)
1712 {
1713         /* if something changed in the button */
1714         double value;
1715         float okwidth;
1716 //      int transopts= ui_translate_buttons();
1717         
1718         ui_is_but_sel(but);
1719         
1720 //      if(but->type==TEX || but->type==IDPOIN) transopts= 0;
1721
1722         /* test for min and max, icon sliders, etc */
1723         switch( but->type ) {
1724                 case NUM:
1725                 case SLI:
1726                 case SCROLL:
1727                 case NUMSLI:
1728                 case HSVSLI:
1729                         value= ui_get_but_val(but);
1730                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1731                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1732                         break;
1733                         
1734                 case NUMABS:
1735                         value= fabs( ui_get_but_val(but) );
1736                         if(value < but->hardmin) ui_set_but_val(but, but->hardmin);
1737                         else if(value > but->hardmax) ui_set_but_val(but, but->hardmax);
1738                         break;
1739                         
1740                 case ICONTOG: 
1741                 case ICONTOGN:
1742                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1743                                 if(but->flag & UI_SELECT) but->iconadd= 1;
1744                                 else but->iconadd= 0;
1745                         }
1746                         break;
1747                         
1748                 case ICONROW:
1749                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1750                                 value= ui_get_but_val(but);
1751                                 but->iconadd= (int)value- (int)(but->hardmin);
1752                         }
1753                         break;
1754                         
1755                 case ICONTEXTROW:
1756                         if(!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
1757                                 value= ui_get_but_val(but);
1758                                 but->iconadd= (int)value- (int)(but->hardmin);
1759                         }
1760                         break;
1761         }
1762         
1763         
1764         /* safety is 4 to enable small number buttons (like 'users') */
1765         okwidth= -4 + (but->x2 - but->x1); 
1766         
1767         /* name: */
1768         switch( but->type ) {
1769         
1770         case MENU:
1771         case ICONTEXTROW:
1772                 
1773                 if(but->x2 - but->x1 > 24) {
1774                         value= ui_get_but_val(but);
1775                         ui_set_name_menu(but, (int)value);
1776                 }
1777                 break;
1778         
1779         case NUM:
1780         case NUMSLI:
1781         case HSVSLI:
1782         case NUMABS:
1783
1784                 value= ui_get_but_val(but);
1785
1786                 if(ui_is_but_float(but)) {
1787                         if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str);
1788                         else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str);
1789                         else if(but->a2) { /* amount of digits defined */
1790                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1791                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1792                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1793                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1794                         }
1795                         else {
1796                                 if(but->hardmax<10.001) sprintf(but->drawstr, "%s%.3f", but->str, value);
1797                                 else sprintf(but->drawstr, "%s%.2f", but->str, value);
1798                         }
1799                 }
1800                 else {
1801                         sprintf(but->drawstr, "%s%d", but->str, (int)value);
1802                 }
1803                         
1804                 if(but->rnaprop) {
1805                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
1806                         
1807                         if (pstype == PROP_PERCENTAGE)
1808                                 strcat(but->drawstr, "%");
1809                 }
1810                 break;
1811
1812         case LABEL:
1813                 if(ui_is_but_float(but)) {
1814                         value= ui_get_but_val(but);
1815                         if(but->a2) { /* amount of digits defined */
1816                                 if(but->a2==1) sprintf(but->drawstr, "%s%.1f", but->str, value);
1817                                 else if(but->a2==2) sprintf(but->drawstr, "%s%.2f", but->str, value);
1818                                 else if(but->a2==3) sprintf(but->drawstr, "%s%.3f", but->str, value);
1819                                 else sprintf(but->drawstr, "%s%.4f", but->str, value);
1820                         }
1821                         else {
1822                                 sprintf(but->drawstr, "%s%.2f", but->str, value);
1823                         }
1824                 }
1825                 else strcpy(but->drawstr, but->str);
1826                 
1827                 break;
1828
1829         case IDPOIN:
1830         case TEX:
1831         case SEARCH_MENU:
1832                 if(!but->editstr) {
1833                         char str[UI_MAX_DRAW_STR];
1834
1835                         ui_get_but_string(but, str, UI_MAX_DRAW_STR-strlen(but->str));
1836
1837                         strcpy(but->drawstr, but->str);
1838                         strcat(but->drawstr, str);
1839                 }
1840                 break;
1841         
1842         case KEYEVT:
1843                 strcpy(but->drawstr, but->str);
1844                 if (but->flag & UI_SELECT) {
1845                         strcat(but->drawstr, "Press a key");
1846                 } else {
1847                         strcat(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1848                 }
1849                 break;
1850                 
1851         case HOTKEYEVT:
1852                 if (but->flag & UI_SELECT) {
1853                         short *sp= (short *)but->func_arg3;
1854                         
1855                         strcpy(but->drawstr, but->str);
1856                         
1857                         if(*sp) {
1858                                 char *str= but->drawstr;
1859                                 
1860                                 if(*sp & KM_SHIFT)
1861                                         str= strcat(str, "Shift ");
1862                                 if(*sp & KM_CTRL)
1863                                         str= strcat(str, "Ctrl ");
1864                                 if(*sp & KM_ALT)
1865                                         str= strcat(str, "Alt ");
1866                                 if(*sp & KM_OSKEY)
1867                                         str= strcat(str, "Cmd ");
1868                         }
1869                         else
1870                                 strcat(but->drawstr, "Press a key  ");
1871                 } else {
1872                         /* XXX todo, button currently only used temporarily */
1873                         strcpy(but->drawstr, WM_key_event_string((short) ui_get_but_val(but)));
1874                 }
1875                 break;
1876                 
1877         case BUT_TOGDUAL:
1878                 /* trying to get the dual-icon to left of text... not very nice */
1879                 if(but->str[0]) {
1880                         strcpy(but->drawstr, "  ");
1881                         strcpy(but->drawstr+2, but->str);
1882                 }
1883                 break;
1884         default:
1885                 strcpy(but->drawstr, but->str);
1886                 
1887         }
1888
1889         /* if we are doing text editing, this will override the drawstr */
1890         if(but->editstr) {
1891                 strcpy(but->drawstr, but->str);
1892                 strcat(but->drawstr, but->editstr);
1893         }
1894         
1895         /* text clipping moved to widget drawing code itself */
1896 }
1897
1898
1899 void uiBlockBeginAlign(uiBlock *block)
1900 {
1901         /* if other align was active, end it */
1902         if(block->flag & UI_BUT_ALIGN) uiBlockEndAlign(block);
1903
1904         block->flag |= UI_BUT_ALIGN_DOWN;       
1905         block->alignnr++;
1906
1907         /* buttons declared after this call will get this align nr */ // XXX flag?
1908 }
1909
1910 static int buts_are_horiz(uiBut *but1, uiBut *but2)
1911 {
1912         float dx, dy;
1913         
1914         dx= fabs( but1->x2 - but2->x1);
1915         dy= fabs( but1->y1 - but2->y2);
1916         
1917         if(dx > dy) return 0;
1918         return 1;
1919 }
1920
1921 void uiBlockEndAlign(uiBlock *block)
1922 {
1923         block->flag &= ~UI_BUT_ALIGN;   // all 4 flags
1924 }
1925
1926 int ui_but_can_align(uiBut *but)
1927 {
1928         return !ELEM3(but->type, LABEL, OPTION, OPTIONN);
1929 }
1930
1931 static void ui_block_do_align_but(uiBlock *block, uiBut *first, int nr)
1932 {
1933         uiBut *prev, *but=NULL, *next;
1934         int flag= 0, cols=0, rows=0;
1935         
1936         /* auto align */
1937
1938         for(but=first; but && but->alignnr == nr; but=but->next) {
1939                 if(but->next && but->next->alignnr == nr) {
1940                         if(buts_are_horiz(but, but->next)) cols++;
1941                         else rows++;
1942                 }
1943         }
1944
1945         /* rows==0: 1 row, cols==0: 1 collumn */
1946         
1947         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
1948         for(but=first, prev=NULL; but && but->alignnr == nr; prev=but, but=but->next) {
1949                 next= but->next;
1950                 if(next && next->alignnr != nr)
1951                         next= NULL;
1952
1953                 /* clear old flag */
1954                 but->flag &= ~UI_BUT_ALIGN;
1955                 
1956                 if(flag==0) {   /* first case */
1957                         if(next) {
1958                                 if(buts_are_horiz(but, next)) {
1959                                         if(rows==0)
1960                                                 flag= UI_BUT_ALIGN_RIGHT;
1961                                         else 
1962                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT;
1963                                 }
1964                                 else {
1965                                         flag= UI_BUT_ALIGN_DOWN;
1966                                 }
1967                         }
1968                 }
1969                 else if(next==NULL) {   /* last case */
1970                         if(prev) {
1971                                 if(buts_are_horiz(prev, but)) {
1972                                         if(rows==0) 
1973                                                 flag= UI_BUT_ALIGN_LEFT;
1974                                         else
1975                                                 flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT;
1976                                 }
1977                                 else flag= UI_BUT_ALIGN_TOP;
1978                         }
1979                 }
1980                 else if(buts_are_horiz(but, next)) {
1981                         /* check if this is already second row */
1982                         if( prev && buts_are_horiz(prev, but)==0) {
1983                                 flag &= ~UI_BUT_ALIGN_LEFT;
1984                                 flag |= UI_BUT_ALIGN_TOP;
1985                                 /* exception case: bottom row */
1986                                 if(rows>0) {
1987                                         uiBut *bt= but;
1988                                         while(bt && bt->alignnr == nr) {
1989                                                 if(bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next)==0 ) break; 
1990                                                 bt= bt->next;
1991                                         }
1992                                         if(bt==0 || bt->alignnr != nr) flag= UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT;
1993                                 }
1994                         }
1995                         else flag |= UI_BUT_ALIGN_LEFT;
1996                 }
1997                 else {
1998                         if(cols==0) {
1999                                 flag |= UI_BUT_ALIGN_TOP;
2000                         }
2001                         else {  /* next button switches to new row */
2002                                 
2003                                 if(prev && buts_are_horiz(prev, but))
2004                                    flag |= UI_BUT_ALIGN_LEFT;
2005                                 
2006                                 if( (flag & UI_BUT_ALIGN_TOP)==0) {     /* stil top row */
2007                                         if(prev)
2008                                                 flag= UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT;
2009                                         else 
2010                                                 flag |= UI_BUT_ALIGN_DOWN;
2011                                 }
2012                                 else 
2013                                         flag |= UI_BUT_ALIGN_TOP;
2014                         }
2015                 }
2016                 
2017                 but->flag |= flag;
2018                 
2019                 /* merge coordinates */
2020                 if(prev) {
2021                         // simple cases 
2022                         if(rows==0) {
2023                                 but->x1= (prev->x2+but->x1)/2.0;
2024                                 prev->x2= but->x1;
2025                         }
2026                         else if(cols==0) {
2027                                 but->y2= (prev->y1+but->y2)/2.0;
2028                                 prev->y1= but->y2;
2029                         }
2030                         else {
2031                                 if(buts_are_horiz(prev, but)) {
2032                                         but->x1= (prev->x2+but->x1)/2.0;
2033                                         prev->x2= but->x1;
2034                                         /* copy height too */
2035                                         but->y2= prev->y2;
2036                                 }
2037                                 else if(prev->prev && buts_are_horiz(prev->prev, prev)==0) {
2038                                         /* the previous button is a single one in its row */
2039                                         but->y2= (prev->y1+but->y2)/2.0;
2040                                         prev->y1= but->y2;
2041                                 }
2042                                 else {
2043                                         /* the previous button is not a single one in its row */
2044                                         but->y2= prev->y1;
2045                                 }
2046                         }
2047                 }
2048         }
2049 }
2050
2051 void ui_block_do_align(uiBlock *block)
2052 {
2053         uiBut *but;
2054         int nr;
2055
2056         /* align buttons with same align nr */
2057         for(but=block->buttons.first; but;) {
2058                 if(but->alignnr) {
2059                         nr= but->alignnr;
2060                         ui_block_do_align_but(block, but, nr);
2061
2062                         /* skip with same number */
2063                         for(; but && but->alignnr == nr; but=but->next);
2064
2065                         if(!but)
2066                                 break;
2067                 }
2068                 else
2069                         but= but->next;
2070         }
2071 }
2072
2073 /*
2074 ui_def_but is the function that draws many button types
2075
2076 for float buttons:
2077         "a1" Click Step (how much to change the value each click)
2078         "a2" Number of decimal point values to display. 0 defaults to 3 (0.000) 1,2,3, and a maximum of 4,
2079        all greater values will be clamped to 4.
2080
2081 */
2082 static uiBut *ui_def_but(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2083 {
2084         uiBut *but;
2085         short slen;
2086         
2087         if(type & BUTPOIN) {            /* a pointer is required */
2088                 if(poin==NULL)
2089                         return NULL;
2090         }
2091
2092         but= MEM_callocN(sizeof(uiBut), "uiBut");
2093
2094         but->type= type & BUTTYPE;
2095         but->pointype= type & BUTPOIN;
2096         but->bit= type & BIT;
2097         but->bitnr= type & 31;
2098         but->icon = 0;
2099
2100         but->retval= retval;
2101         if( strlen(str)>=UI_MAX_NAME_STR-1 ) {
2102                 but->str= MEM_callocN( strlen(str)+2, "uiDefBut");
2103                 strcpy(but->str, str);
2104         }
2105         else {
2106                 but->str= but->strdata;
2107                 strcpy(but->str, str);
2108         }
2109         but->x1= x1; 
2110         but->y1= y1;
2111         but->x2= (x1+x2); 
2112         but->y2= (y1+y2);
2113         
2114         but->poin= poin;
2115         but->hardmin= but->softmin= min; 
2116         but->hardmax= but->softmax= max;
2117         but->a1= a1; 
2118         but->a2= a2;
2119         but->tip= tip;
2120         
2121         but->lock= block->lock;
2122         but->lockstr= block->lockstr;
2123         but->dt= block->dt;
2124
2125         but->aspect= 1.0f; //XXX block->aspect;
2126         but->block= block;              // pointer back, used for frontbuffer status, and picker
2127
2128         if((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but))
2129                 but->alignnr= block->alignnr;
2130
2131         but->func= block->func;
2132         but->func_arg1= block->func_arg1;
2133         but->func_arg2= block->func_arg2;
2134         
2135         but->pos= -1;   /* cursor invisible */
2136
2137         if(ELEM(but->type, NUM, NUMABS)) {      /* add a space to name */
2138                 slen= strlen(but->str);
2139                 if(slen>0 && slen<UI_MAX_NAME_STR-2) {
2140                         if(but->str[slen-1]!=' ') {
2141                                 but->str[slen]= ' ';
2142                                 but->str[slen+1]= 0;
2143                         }
2144                 }
2145         }
2146         
2147         if(ELEM(but->type, HSVCUBE, HSVCIRCLE)) { /* hsv buttons temp storage */
2148                 float rgb[3];
2149                 ui_get_but_vectorf(but, rgb);
2150                 rgb_to_hsv(rgb[0], rgb[1], rgb[2], but->hsv, but->hsv+1, but->hsv+2);
2151         }
2152
2153         if((block->flag & UI_BLOCK_LOOP) || ELEM7(but->type, MENU, TEX, LABEL, IDPOIN, BLOCK, BUTM, SEARCH_MENU))
2154                 but->flag |= (UI_TEXT_LEFT|UI_ICON_LEFT);
2155         else if(but->type==BUT_TOGDUAL)
2156                 but->flag |= UI_ICON_LEFT;
2157
2158         but->flag |= (block->flag & UI_BUT_ALIGN);
2159
2160         if (but->lock) {
2161                 if (but->lockstr) {
2162                         but->flag |= UI_BUT_DISABLED;
2163                 }
2164         }
2165
2166         BLI_addtail(&block->buttons, but);
2167         
2168         if(block->curlayout)
2169                 ui_layout_add_but(block->curlayout, but);
2170
2171         return but;
2172 }
2173
2174 uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2175 {
2176         uiBut *but;
2177         PropertyRNA *prop;
2178         PropertyType proptype;
2179         int freestr= 0, icon= 0;
2180
2181         prop= RNA_struct_find_property(ptr, propname);
2182
2183         if(prop) {
2184                 proptype= RNA_property_type(prop);
2185
2186                 /* use rna values if parameters are not specified */
2187                 if(!str) {
2188                         if(type == MENU && proptype == PROP_ENUM) {
2189                                 EnumPropertyItem *item;
2190                                 DynStr *dynstr;
2191                                 int i, totitem, value, free;
2192
2193                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2194                                 value= RNA_property_enum_get(ptr, prop);
2195
2196                                 dynstr= BLI_dynstr_new();
2197                                 BLI_dynstr_appendf(dynstr, "%s%%t", RNA_property_ui_name(prop));
2198                                 for(i=0; i<totitem; i++) {
2199                                         if(!item[i].identifier[0])
2200                                                 BLI_dynstr_append(dynstr, "|%l");
2201                                         else if(item[i].icon)
2202                                                 BLI_dynstr_appendf(dynstr, "|%s %%i%d %%x%d", item[i].name, item[i].icon, item[i].value);
2203                                         else
2204                                                 BLI_dynstr_appendf(dynstr, "|%s %%x%d", item[i].name, item[i].value);
2205
2206                                         if(value == item[i].value)
2207                                                 icon= item[i].icon;
2208                                 }
2209                                 str= BLI_dynstr_get_cstring(dynstr);
2210                                 BLI_dynstr_free(dynstr);
2211
2212                                 if(free)
2213                                         MEM_freeN(item);
2214
2215                                 freestr= 1;
2216                         }
2217                         else if(ELEM(type, ROW, LISTROW) && proptype == PROP_ENUM) {
2218                                 EnumPropertyItem *item;
2219                                 int i, totitem, free;
2220
2221                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2222                                 for(i=0; i<totitem; i++) {
2223                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2224                                                 str= (char*)item[i].name;
2225                                                 icon= item[i].icon;
2226                                         }
2227                                 }
2228
2229                                 if(!str)
2230                                         str= (char*)RNA_property_ui_name(prop);
2231                                 if(free)
2232                                         MEM_freeN(item);
2233                         }
2234                         else {
2235                                 str= (char*)RNA_property_ui_name(prop);
2236                                 icon= RNA_property_ui_icon(prop);
2237                         }
2238                 }
2239
2240                 if(!tip) {
2241                         if(type == ROW && proptype == PROP_ENUM) {
2242                                 EnumPropertyItem *item;
2243                                 int i, totitem, free;
2244
2245                                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
2246
2247                                 for(i=0; i<totitem; i++) {
2248                                         if(item[i].identifier[0] && item[i].value == (int)max) {
2249                                                 if(item[i].description[0])
2250                                                         tip= (char*)item[i].description;
2251                                                 break;
2252                                         }
2253                                 }
2254
2255                                 if(free)
2256                                         MEM_freeN(item);
2257                         }
2258                 }
2259                 
2260                 if(!tip)
2261                         tip= (char*)RNA_property_ui_description(prop);
2262
2263                 if(min == max || a1 == -1 || a2 == -1) {
2264                         if(proptype == PROP_INT) {
2265                                 int hardmin, hardmax, softmin, softmax, step;
2266
2267                                 RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
2268                                 RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
2269
2270                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2271                                         min= hardmin;
2272                                         max= hardmax;
2273                                 }
2274                                 if(a1 == -1)
2275                                         a1= step;
2276                                 if(a2 == -1)
2277                                         a2= 0;
2278                         }
2279                         else if(proptype == PROP_FLOAT) {
2280                                 float hardmin, hardmax, softmin, softmax, step, precision;
2281
2282                                 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
2283                                 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2284
2285                                 if(!ELEM(type, ROW, LISTROW) && min == max) {
2286                                         min= hardmin;
2287                                         max= hardmax;
2288                                 }
2289                                 if(a1 == -1)
2290                                         a1= step;
2291                                 if(a2 == -1)
2292                                         a2= precision;
2293                         }
2294                         else if(proptype == PROP_STRING) {
2295                                 min= 0;
2296                                 max= RNA_property_string_maxlength(prop);
2297                                 if(max == 0) /* interface code should ideally support unlimited length */
2298                                         max= UI_MAX_DRAW_STR; 
2299                         }
2300                 }
2301         }
2302         else
2303                 str= (char*)propname;
2304
2305         /* now create button */
2306         but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, NULL, min, max, a1, a2, tip);
2307
2308         if(prop) {
2309                 but->rnapoin= *ptr;
2310                 but->rnaprop= prop;
2311
2312                 if(RNA_property_array_length(but->rnaprop))
2313                         but->rnaindex= index;
2314                 else
2315                         but->rnaindex= 0;
2316         }
2317
2318         if(icon) {
2319                 but->icon= (BIFIconID)icon;
2320                 but->flag |= UI_HAS_ICON;
2321                 but->flag|= UI_ICON_LEFT;
2322         }
2323         
2324         if (!prop || !RNA_property_editable(&but->rnapoin, prop)) {
2325                 but->flag |= UI_BUT_DISABLED;
2326                 but->lock = 1;
2327                 but->lockstr = "";
2328         }
2329
2330         if(freestr)
2331                 MEM_freeN(str);
2332         
2333         return but;
2334 }
2335
2336 uiBut *ui_def_but_operator(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2337 {
2338         uiBut *but;
2339         wmOperatorType *ot;
2340         
2341         ot= WM_operatortype_find(opname, 0);
2342
2343         if(!str) {
2344                 if(ot) str= ot->name;
2345                 else str= opname;
2346         }
2347         
2348         if ((!tip || tip[0]=='\0') && ot && ot->description) {
2349                 tip= ot->description;
2350         }
2351
2352         but= ui_def_but(block, type, -1, str, x1, y1, x2, y2, NULL, 0, 0, 0, 0, tip);
2353         but->optype= ot;
2354         but->opcontext= opcontext;
2355
2356         if(!ot) {
2357                 but->flag |= UI_BUT_DISABLED;
2358                 but->lock = 1;
2359                 but->lockstr = "";
2360         }
2361
2362         return but;
2363 }
2364
2365 uiBut *uiDefBut(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2366 {
2367         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2368
2369         ui_check_but(but);
2370         
2371         return but;
2372 }
2373
2374         /* if _x_ is a power of two (only one bit) return the power,
2375          * otherwise return -1. 
2376          * (1<<findBitIndex(x))==x for powers of two.
2377          */
2378 static int findBitIndex(unsigned int x) {
2379         if (!x || (x&(x-1))!=0) {       /* x&(x-1) strips lowest bit */
2380                 return -1;
2381         } else {
2382                 int idx= 0;
2383
2384                 if (x&0xFFFF0000)       idx+=16, x>>=16;
2385                 if (x&0xFF00)           idx+=8, x>>=8;
2386                 if (x&0xF0)                     idx+=4, x>>=4;
2387                 if (x&0xC)                      idx+=2, x>>=2;
2388                 if (x&0x2)                      idx+=1;
2389
2390                 return idx;
2391         }
2392 }
2393
2394 /* autocomplete helper functions */
2395 struct AutoComplete {
2396         int maxlen;
2397         char *truncate;
2398         char *startname;
2399 };
2400
2401 AutoComplete *autocomplete_begin(char *startname, int maxlen)
2402 {
2403         AutoComplete *autocpl;
2404         
2405         autocpl= MEM_callocN(sizeof(AutoComplete), "AutoComplete");
2406         autocpl->maxlen= maxlen;
2407         autocpl->truncate= MEM_callocN(sizeof(char)*maxlen, "AutoCompleteTruncate");
2408         autocpl->startname= startname;
2409
2410         return autocpl;
2411 }
2412
2413 void autocomplete_do_name(AutoComplete *autocpl, const char *name)
2414 {
2415         char *truncate= autocpl->truncate;
2416         char *startname= autocpl->startname;
2417         int a;
2418
2419         for(a=0; a<autocpl->maxlen-1; a++) {
2420                 if(startname[a]==0 || startname[a]!=name[a])
2421                         break;
2422         }
2423         /* found a match */
2424         if(startname[a]==0) {
2425                 /* first match */
2426                 if(truncate[0]==0)
2427                         BLI_strncpy(truncate, name, autocpl->maxlen);
2428                 else {
2429                         /* remove from truncate what is not in bone->name */
2430                         for(a=0; a<autocpl->maxlen-1; a++) {
2431                                 if(name[a] == 0) {
2432                                         truncate[a]= 0;
2433                                         break;
2434                                 }
2435                                 else if(truncate[a]!=name[a])
2436                                         truncate[a]= 0;
2437                         }
2438                 }
2439         }
2440 }
2441
2442 void autocomplete_end(AutoComplete *autocpl, char *autoname)
2443 {       
2444         if(autocpl->truncate[0])
2445                 BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
2446         else {
2447                 if (autoname != autocpl->startname) /* dont copy a string over its self */
2448                         BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
2449         }
2450         MEM_freeN(autocpl->truncate);
2451         MEM_freeN(autocpl);
2452 }
2453
2454 /* autocomplete callback for ID buttons */
2455 static void autocomplete_id(bContext *C, char *str, void *arg_v)
2456 {
2457         int blocktype= (intptr_t)arg_v;
2458         ListBase *listb= wich_libbase(CTX_data_main(C), blocktype);
2459         
2460         if(listb==NULL) return;
2461         
2462         /* search if str matches the beginning of an ID struct */
2463         if(str[0]) {
2464                 AutoComplete *autocpl= autocomplete_begin(str, 22);
2465                 ID *id;
2466                 
2467                 for(id= listb->first; id; id= id->next)
2468                         autocomplete_do_name(autocpl, id->name+2);
2469
2470                 autocomplete_end(autocpl, str);
2471         }
2472 }
2473
2474 static uiBut *uiDefButBit(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2475 {
2476         int bitIdx= findBitIndex(bit);
2477         if (bitIdx==-1) {
2478                 return NULL;
2479         } else {
2480                 return uiDefBut(block, type|BIT|bitIdx, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2481         }
2482 }
2483 uiBut *uiDefButF(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2484 {
2485         return uiDefBut(block, type|FLO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2486 }
2487 uiBut *uiDefButBitF(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2488 {
2489         return uiDefButBit(block, type|FLO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2490 }
2491 uiBut *uiDefButI(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2492 {
2493         return uiDefBut(block, type|INT, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2494 }
2495 uiBut *uiDefButBitI(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2496 {
2497         return uiDefButBit(block, type|INT, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2498 }
2499 uiBut *uiDefButS(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2500 {
2501         return uiDefBut(block, type|SHO, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2502 }
2503 uiBut *uiDefButBitS(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2504 {
2505         return uiDefButBit(block, type|SHO, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2506 }
2507 uiBut *uiDefButC(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2508 {
2509         return uiDefBut(block, type|CHA, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2510 }
2511 uiBut *uiDefButBitC(uiBlock *block, int type, int bit, int retval, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2512 {
2513         return uiDefButBit(block, type|CHA, bit, retval, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2514 }
2515 uiBut *uiDefButR(uiBlock *block, int type, int retval, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2516 {
2517         uiBut *but;
2518
2519         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2520         if(but)
2521                 ui_check_but(but);
2522
2523         return but;
2524 }
2525 uiBut *uiDefButO(uiBlock *block, int type, char *opname, int opcontext, char *str, short x1, short y1, short x2, short y2, char *tip)
2526 {
2527         uiBut *but;
2528
2529         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2530         if(but)
2531                 ui_check_but(but);
2532
2533         return but;
2534 }
2535
2536 uiBut *uiDefIconBut(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2537 {
2538         uiBut *but= ui_def_but(block, type, retval, "", x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2539         
2540         but->icon= (BIFIconID) icon;
2541         but->flag|= UI_HAS_ICON;
2542
2543         ui_check_but(but);
2544         
2545         return but;
2546 }
2547 static uiBut *uiDefIconButBit(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2548 {
2549         int bitIdx= findBitIndex(bit);
2550         if (bitIdx==-1) {
2551                 return NULL;
2552         } else {
2553                 return uiDefIconBut(block, type|BIT|bitIdx, retval, icon, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2554         }
2555 }
2556
2557 uiBut *uiDefIconButF(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2558 {
2559         return uiDefIconBut(block, type|FLO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2560 }
2561 uiBut *uiDefIconButBitF(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2562 {
2563         return uiDefIconButBit(block, type|FLO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2564 }
2565 uiBut *uiDefIconButI(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2566 {
2567         return uiDefIconBut(block, type|INT, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2568 }
2569 uiBut *uiDefIconButBitI(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2570 {
2571         return uiDefIconButBit(block, type|INT, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2572 }
2573 uiBut *uiDefIconButS(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2574 {
2575         return uiDefIconBut(block, type|SHO, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2576 }
2577 uiBut *uiDefIconButBitS(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2578 {
2579         return uiDefIconButBit(block, type|SHO, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2580 }
2581 uiBut *uiDefIconButC(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2582 {
2583         return uiDefIconBut(block, type|CHA, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2584 }
2585 uiBut *uiDefIconButBitC(uiBlock *block, int type, int bit, int retval, int icon, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2586 {
2587         return uiDefIconButBit(block, type|CHA, bit, retval, icon, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2588 }
2589 uiBut *uiDefIconButR(uiBlock *block, int type, int retval, int icon, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2590 {
2591         uiBut *but;
2592
2593         but= ui_def_but_rna(block, type, retval, "", x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2594         if(but) {
2595                 if(icon) {
2596                         but->icon= (BIFIconID) icon;
2597                         but->flag|= UI_HAS_ICON;
2598                 }
2599                 ui_check_but(but);
2600         }
2601
2602         return but;
2603 }
2604 uiBut *uiDefIconButO(uiBlock *block, int type, char *opname, int opcontext, int icon, short x1, short y1, short x2, short y2, char *tip)
2605 {
2606         uiBut *but;
2607
2608         but= ui_def_but_operator(block, type, opname, opcontext, "", x1, y1, x2, y2, tip);
2609         if(but) {
2610                 but->icon= (BIFIconID) icon;
2611                 but->flag|= UI_HAS_ICON;
2612                 ui_check_but(but);
2613         }
2614
2615         return but;
2616 }
2617
2618 /* Button containing both string label and icon */
2619 uiBut *uiDefIconTextBut(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2620 {
2621         uiBut *but= ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2622
2623         but->icon= (BIFIconID) icon;
2624         but->flag|= UI_HAS_ICON;
2625
2626         but->flag|= UI_ICON_LEFT;
2627
2628         ui_check_but(but);
2629
2630         return but;
2631 }
2632 static uiBut *uiDefIconTextButBit(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, void *poin, float min, float max, float a1, float a2,  char *tip)
2633 {
2634         int bitIdx= findBitIndex(bit);
2635         if (bitIdx==-1) {
2636                 return NULL;
2637         } else {
2638                 return uiDefIconTextBut(block, type|BIT|bitIdx, retval, icon, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
2639         }
2640 }
2641
2642 uiBut *uiDefIconTextButF(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2643 {
2644         return uiDefIconTextBut(block, type|FLO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2645 }
2646 uiBut *uiDefIconTextButBitF(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, float *poin, float min, float max, float a1, float a2,  char *tip)
2647 {
2648         return uiDefIconTextButBit(block, type|FLO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2649 }
2650 uiBut *uiDefIconTextButI(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2651 {
2652         return uiDefIconTextBut(block, type|INT, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2653 }
2654 uiBut *uiDefIconTextButBitI(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, int *poin, float min, float max, float a1, float a2,  char *tip)
2655 {
2656         return uiDefIconTextButBit(block, type|INT, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2657 }
2658 uiBut *uiDefIconTextButS(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2659 {
2660         return uiDefIconTextBut(block, type|SHO, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2661 }
2662 uiBut *uiDefIconTextButBitS(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, short *poin, float min, float max, float a1, float a2,  char *tip)
2663 {
2664         return uiDefIconTextButBit(block, type|SHO, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2665 }
2666 uiBut *uiDefIconTextButC(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2667 {
2668         return uiDefIconTextBut(block, type|CHA, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2669 }
2670 uiBut *uiDefIconTextButBitC(uiBlock *block, int type, int bit, int retval, int icon, char *str, short x1, short y1, short x2, short y2, char *poin, float min, float max, float a1, float a2,  char *tip)
2671 {
2672         return uiDefIconTextButBit(block, type|CHA, bit, retval, icon, str, x1, y1, x2, y2, (void*) poin, min, max, a1, a2, tip);
2673 }
2674 uiBut *uiDefIconTextButR(uiBlock *block, int type, int retval, int icon, char *str, short x1, short y1, short x2, short y2, PointerRNA *ptr, const char *propname, int index, float min, float max, float a1, float a2,  char *tip)
2675 {
2676         uiBut *but;
2677
2678         but= ui_def_but_rna(block, type, retval, str, x1, y1, x2, y2, ptr, propname, index, min, max, a1, a2, tip);
2679         if(but) {
2680                 if(icon) {
2681                         but->icon= (BIFIconID) icon;
2682                         but->flag|= UI_HAS_ICON;
2683                 }
2684                 but->flag|= UI_ICON_LEFT;
2685                 ui_check_but(but);
2686         }
2687
2688         return but;
2689 }
2690 uiBut *uiDefIconTextButO(uiBlock *block, int type, char *opname, int opcontext, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2691 {
2692         uiBut *but;
2693
2694         but= ui_def_but_operator(block, type, opname, opcontext, str, x1, y1, x2, y2, tip);
2695         if(but) {
2696                 but->icon= (BIFIconID) icon;
2697                 but->flag|= UI_HAS_ICON;
2698                 but->flag|= UI_ICON_LEFT;
2699                 ui_check_but(but);
2700         }
2701
2702         return but;
2703 }
2704
2705 /* END Button containing both string label and icon */
2706
2707 void uiSetButLink(uiBut *but, void **poin, void ***ppoin, short *tot, int from, int to)
2708 {
2709         uiLink *link;
2710         
2711         link= but->link= MEM_callocN(sizeof(uiLink), "new uilink");
2712         
2713         link->poin= poin;
2714         link->ppoin= ppoin;
2715         link->totlink= tot;
2716         link->fromcode= from;
2717         link->tocode= to;
2718 }
2719
2720 /* cruft to make uiBlock and uiBut private */
2721
2722 int uiBlocksGetYMin(ListBase *lb)
2723 {
2724         uiBlock *block;
2725         int min= 0;
2726         
2727         for (block= lb->first; block; block= block->next)
2728                 if (block==lb->first || block->miny<min)
2729                         min= block->miny;
2730                         
2731         return min;
2732 }
2733
2734 void uiBlockSetDirection(uiBlock *block, int direction)
2735 {
2736         block->direction= direction;
2737 }
2738
2739 /* this call escapes if there's alignment flags */
2740 void uiBlockFlipOrder(uiBlock *block)
2741 {
2742         ListBase lb;
2743         uiBut *but, *next;
2744         float centy, miny=10000, maxy= -10000;
2745
2746         if(U.uiflag & USER_MENUFIXEDORDER)
2747                 return;
2748         
2749         for(but= block->buttons.first; but; but= but->next) {
2750                 if(but->flag & UI_BUT_ALIGN) return;
2751                 if(but->y1 < miny) miny= but->y1;
2752                 if(but->y2 > maxy) maxy= but->y2;
2753         }
2754         /* mirror trick */
2755         centy= (miny+maxy)/2.0;
2756         for(but= block->buttons.first; but; but= but->next) {
2757                 but->y1 = centy-(but->y1-centy);
2758                 but->y2 = centy-(but->y2-centy);
2759                 SWAP(float, but->y1, but->y2);
2760         }
2761         
2762         /* also flip order in block itself, for example for arrowkey */
2763         lb.first= lb.last= NULL;
2764         but= block->buttons.first;
2765         while(but) {
2766                 next= but->next;
2767                 BLI_remlink(&block->buttons, but);
2768                 BLI_addtail(&lb, but);
2769                 but= next;
2770         }
2771         block->buttons= lb;
2772 }
2773
2774
2775 void uiBlockSetFlag(uiBlock *block, int flag)
2776 {
2777         block->flag|= flag;
2778 }
2779
2780 void uiBlockClearFlag(uiBlock *block, int flag)
2781 {
2782         block->flag&= ~flag;
2783 }
2784
2785 void uiBlockSetXOfs(uiBlock *block, int xofs)
2786 {
2787         block->xofs= xofs;
2788 }
2789
2790 void uiButSetFlag(uiBut *but, int flag)
2791 {
2792         but->flag|= flag;
2793 }
2794
2795 void uiButClearFlag(uiBut *but, int flag)
2796 {
2797         but->flag&= ~flag;
2798 }
2799
2800 int uiButGetRetVal(uiBut *but)
2801 {
2802         return but->retval;
2803 }
2804
2805 PointerRNA *uiButGetOperatorPtrRNA(uiBut *but)
2806 {
2807         if(but->optype && !but->opptr) {
2808                 but->opptr= MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
2809                 WM_operator_properties_create(but->opptr, but->optype->idname);
2810         }
2811
2812         return but->opptr;
2813 }
2814
2815 void uiBlockSetHandleFunc(uiBlock *block, uiBlockHandleFunc func, void *arg)
2816 {
2817         block->handle_func= func;
2818         block->handle_func_arg= arg;
2819 }
2820
2821 void uiBlockSetButmFunc(uiBlock *block, uiMenuHandleFunc func, void *arg)
2822 {
2823         block->butm_func= func;
2824         block->butm_func_arg= arg;
2825 }
2826
2827 void uiBlockSetFunc(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
2828 {
2829         block->func= func;
2830         block->func_arg1= arg1;
2831         block->func_arg2= arg2;
2832 }
2833
2834 void uiButSetRenameFunc(uiBut *but, uiButHandleRenameFunc func, void *arg1)
2835 {
2836         but->rename_func= func;
2837         but->rename_arg1= arg1;
2838 }
2839
2840 void uiBlockSetDrawExtraFunc(uiBlock *block, void (*func)(const bContext *C, void *idv, void *argv, rcti *rect), void *arg)
2841 {
2842         block->drawextra= func;
2843         block->drawextra_arg= arg;
2844 }
2845
2846 void uiButSetFunc(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
2847 {
2848         but->func= func;
2849         but->func_arg1= arg1;
2850         but->func_arg2= arg2;
2851 }
2852
2853 void uiButSetNFunc(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
2854 {
2855         but->funcN= funcN;
2856         but->func_argN= argN;
2857         but->func_arg2= arg2;
2858 }
2859
2860 void uiButSetCompleteFunc(uiBut *but, uiButCompleteFunc func, void *arg)
2861 {
2862         but->autocomplete_func= func;
2863         but->autofunc_arg= arg;
2864 }
2865
2866 uiBut *uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, short blocktype, int retval, char *str, short x1, short y1, short x2, short y2, void *idpp, char *tip)
2867 {
2868         uiBut *but= ui_def_but(block, IDPOIN, retval, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2869         but->idpoin_func= func;
2870         but->idpoin_idpp= (ID**) idpp;
2871         ui_check_but(but);
2872         
2873         if(blocktype)
2874                 uiButSetCompleteFunc(but, autocomplete_id, (void *)(intptr_t)blocktype);
2875
2876         return but;
2877 }
2878
2879 uiBut *uiDefBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2880 {
2881         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2882         but->block_create_func= func;
2883         ui_check_but(but);
2884         return but;
2885 }
2886
2887 uiBut *uiDefBlockButN(uiBlock *block, uiBlockCreateFunc func, void *argN, char *str, short x1, short y1, short x2, short y2, char *tip)
2888 {
2889         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, NULL, 0.0, 0.0, 0.0, 0.0, tip);
2890         but->block_create_func= func;
2891         but->func_argN= argN;
2892         ui_check_but(but);
2893         return but;
2894 }
2895
2896
2897 uiBut *uiDefPulldownBut(uiBlock *block, uiBlockCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2898 {
2899         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2900         but->block_create_func= func;
2901         ui_check_but(but);
2902         return but;
2903 }
2904
2905 uiBut *uiDefMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, char *str, short x1, short y1, short x2, short y2, char *tip)
2906 {
2907         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2908         but->menu_create_func= func;
2909         ui_check_but(but);
2910         return but;
2911 }
2912
2913 uiBut *uiDefIconTextMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2914 {
2915         uiBut *but= ui_def_but(block, PULLDOWN, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2916
2917         but->icon= (BIFIconID) icon;
2918         but->flag|= UI_HAS_ICON;
2919
2920         but->flag|= UI_ICON_LEFT;
2921         but->flag|= UI_ICON_SUBMENU;
2922
2923         but->menu_create_func= func;
2924         ui_check_but(but);
2925
2926         return but;
2927 }
2928
2929 /* Block button containing both string label and icon */
2930 uiBut *uiDefIconTextBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int icon, char *str, short x1, short y1, short x2, short y2, char *tip)
2931 {
2932         uiBut *but= ui_def_but(block, BLOCK, 0, str, x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2933         
2934         /* XXX temp, old menu calls pass on icon arrow, which is now UI_ICON_SUBMENU flag */
2935         if(icon!=ICON_RIGHTARROW_THIN) {
2936                 but->icon= (BIFIconID) icon;
2937                 but->flag|= UI_ICON_LEFT;
2938         }
2939         but->flag|= UI_HAS_ICON;
2940         but->flag|= UI_ICON_SUBMENU;
2941
2942         but->block_create_func= func;
2943         ui_check_but(but);
2944         
2945         return but;
2946 }
2947
2948 /* Block button containing icon */
2949 uiBut *uiDefIconBlockBut(uiBlock *block, uiBlockCreateFunc func, void *arg, int retval, int icon, short x1, short y1, short x2, short y2, char *tip)
2950 {
2951         uiBut *but= ui_def_but(block, BLOCK, retval, "", x1, y1, x2, y2, arg, 0.0, 0.0, 0.0, 0.0, tip);
2952         
2953         but->icon= (BIFIconID) icon;
2954         but->flag|= UI_HAS_ICON;
2955         
2956         but->flag|= UI_ICON_LEFT;
2957         but->flag|= UI_ICON_SUBMENU;
2958         
2959         but->block_create_func= func;
2960         ui_check_but(but);
2961         
2962         return but;
2963 }
2964
2965 void uiDefKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *spoin, char *tip)
2966 {
2967         uiBut *but= ui_def_but(block, KEYEVT|SHO, retval, str, x1, y1, x2, y2, spoin, 0.0, 0.0, 0.0, 0.0, tip);
2968         ui_check_but(but);
2969 }
2970
2971 /* short pointers hardcoded */
2972 /* modkeypoin will be set to KM_SHIFT, KM_ALT, KM_CTRL, KM_OSKEY bits */
2973 uiBut *uiDefHotKeyevtButS(uiBlock *block, int retval, char *str, short x1, short y1, short x2, short y2, short *keypoin, short *modkeypoin, char *tip)
2974 {
2975         uiBut *but= ui_def_but(block, HOTKEYEVT|SHO, retval, str, x1, y1, x2, y2, keypoin, 0.0, 0.0, 0.0, 0.0, tip);
2976         but->func_arg3= modkeypoin; /* XXX hrmf, abuse! */
2977         ui_check_but(but);
2978         return but;
2979 }
2980
2981
2982 /* arg is pointer to string/name, use uiButSetSearchFunc() below to make this work */
2983 uiBut *uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxlen, short x1, short y1, short x2, short y2, char *tip)
2984 {
2985         uiBut *but= ui_def_but(block, SEARCH_MENU, retval, "", x1, y1, x2, y2, arg, 0.0, maxlen, 0.0, 0.0, tip);
2986         
2987         but->icon= (BIFIconID) icon;
2988         but->flag|= UI_HAS_ICON;
2989         
2990         but->flag|= UI_ICON_LEFT|UI_TEXT_LEFT;
2991         
2992         ui_check_but(but);
2993         
2994         return but;
2995 }
2996
2997 /* arg is user value, searchfunc and handlefunc both get it as arg */
2998 /* if active set, button opens with this item visible and selected */
2999 void uiButSetSearchFunc(uiBut *but, uiButSearchFunc sfunc, void *arg, uiButHandleFunc bfunc, void *active)
3000 {
3001         but->search_func= sfunc;
3002         but->search_arg= arg;
3003         
3004         uiButSetFunc(but, bfunc, arg, active);
3005 }
3006
3007 /* Program Init/Exit */
3008
3009 void UI_init(void)
3010 {
3011         ui_resources_init();
3012 }
3013
3014 /* after reading userdef file */
3015 void UI_init_userdef(void)
3016 {
3017         /* fix saved themes */
3018         init_userdef_do_versions();
3019         /* set default colors in default theme */
3020         ui_theme_init_userdef();
3021         
3022         uiStyleInit();
3023 }
3024
3025 void UI_exit(void)
3026 {
3027         ui_resources_free();
3028 }
3029