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