Merge with -r 22620:23107.
[blender.git] / source / blender / editors / interface / interface_layout.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Blender Foundation 2009.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <limits.h>
26 #include <math.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_ID.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
35 #include "DNA_userdef_types.h"
36 #include "DNA_windowmanager_types.h"
37
38 #include "BLI_listbase.h"
39 #include "BLI_string.h"
40
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_idprop.h"
44 #include "BKE_library.h"
45 #include "BKE_screen.h"
46 #include "BKE_utildefines.h"
47
48 #include "RNA_access.h"
49
50 #include "UI_interface.h"
51 #include "UI_resources.h"
52 #include "UI_view2d.h"
53
54 #include "ED_util.h"
55 #include "ED_types.h"
56 #include "ED_screen.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "interface_intern.h"
62
63 /************************ Structs and Defines *************************/
64
65 #define RNA_NO_INDEX    -1
66 #define RNA_ENUM_VALUE  -2
67
68 #define EM_SEPR_X               6
69 #define EM_SEPR_Y               6
70
71 /* uiLayoutRoot */
72
73 typedef struct uiLayoutRoot {
74         struct uiLayoutRoot *next, *prev;
75
76         int type;
77         int opcontext;
78
79         int emw, emh;
80
81         uiMenuHandleFunc handlefunc;
82         void *argv;
83
84         uiStyle *style;
85         uiBlock *block;
86         uiLayout *layout;
87 } uiLayoutRoot;
88
89 /* Item */
90
91 typedef enum uiItemType {
92         ITEM_BUTTON,
93
94         ITEM_LAYOUT_ROW,
95         ITEM_LAYOUT_COLUMN,
96         ITEM_LAYOUT_COLUMN_FLOW,
97         ITEM_LAYOUT_ROW_FLOW,
98         ITEM_LAYOUT_BOX,
99         ITEM_LAYOUT_FREE,
100         ITEM_LAYOUT_SPLIT,
101
102         ITEM_LAYOUT_ROOT
103 #if 0
104         TEMPLATE_COLUMN_FLOW,
105         TEMPLATE_SPLIT,
106         TEMPLATE_BOX,
107
108         TEMPLATE_HEADER,
109         TEMPLATE_HEADER_ID
110 #endif
111 } uiItemType;
112
113 typedef struct uiItem {
114         void *next, *prev;
115         uiItemType type;
116         int flag;
117 } uiItem;
118
119 typedef struct uiButtonItem {
120         uiItem item;
121         uiBut *but;
122 } uiButtonItem;
123
124 struct uiLayout {
125         uiItem item;
126
127         uiLayoutRoot *root;
128         bContextStore *context;
129         ListBase items;
130
131         int x, y, w, h;
132         float scale[2];
133         short space;
134         char align;
135         char active;
136         char enabled;
137         char redalert;
138         char keepaspect;
139         char alignment;
140 };
141
142 typedef struct uiLayoutItemFlow {
143         uiLayout litem;
144         int number;
145         int totcol;
146 } uiLayoutItemFlow;
147
148 typedef struct uiLayoutItemBx {
149         uiLayout litem;
150         uiBut *roundbox;
151         ListBase items;
152 } uiLayoutItemBx;
153
154 typedef struct uiLayoutItemSplt {
155         uiLayout litem;
156         float percentage;
157 } uiLayoutItemSplt;
158
159 typedef struct uiLayoutItemRoot {
160         uiLayout litem;
161 } uiLayoutItemRoot;
162
163 /************************** Item ***************************/
164
165 static char *ui_item_name_add_colon(char *name, char namestr[UI_MAX_NAME_STR])
166 {
167         int len= strlen(name);
168
169         if(len != 0 && len+1 < UI_MAX_NAME_STR) {
170                 BLI_strncpy(namestr, name, UI_MAX_NAME_STR);
171                 namestr[len]= ':';
172                 namestr[len+1]= '\0';
173                 return namestr;
174         }
175
176         return name;
177 }
178
179 static int ui_item_fit(int item, int pos, int all, int available, int last, int alignment, int *offset)
180 {
181         /* available == 0 is unlimited */
182         if(available == 0)
183                 return item;
184         
185         if(offset)
186                 *offset= 0;
187         
188         if(all > available) {
189                 /* contents is bigger than available space */
190                 if(last)
191                         return available-pos;
192                 else
193                         return (item*available)/all;
194         }
195         else {
196                 /* contents is smaller or equal to available space */
197                 if(alignment == UI_LAYOUT_ALIGN_EXPAND) {
198                         if(last)
199                                 return available-pos;
200                         else
201                                 return (item*available)/all;
202                 }
203                 else
204                         return item;
205         }
206 }
207
208 /* variable button size in which direction? */
209 #define UI_ITEM_VARY_X  1
210 #define UI_ITEM_VARY_Y  2
211
212 static int ui_layout_vary_direction(uiLayout *layout)
213 {
214         return (layout->root->type == UI_LAYOUT_HEADER || layout->alignment != UI_LAYOUT_ALIGN_EXPAND)? UI_ITEM_VARY_X: UI_ITEM_VARY_Y;
215 }
216
217 /* estimated size of text + icon */
218 static int ui_text_icon_width(uiLayout *layout, char *name, int icon, int compact)
219 {
220         int variable = ui_layout_vary_direction(layout) == UI_ITEM_VARY_X;
221
222         if(icon && !name[0])
223                 return UI_UNIT_X; /* icon only */
224         else if(icon)
225                 return (variable)? UI_GetStringWidth(name) + (compact? 5: 10) + UI_UNIT_X: 10*UI_UNIT_X; /* icon + text */
226         else
227                 return (variable)? UI_GetStringWidth(name) + (compact? 5: 10) + UI_UNIT_X: 10*UI_UNIT_X; /* text only */
228 }
229
230 static void ui_item_size(uiItem *item, int *r_w, int *r_h)
231 {
232         if(item->type == ITEM_BUTTON) {
233                 uiButtonItem *bitem= (uiButtonItem*)item;
234
235                 if(r_w) *r_w= bitem->but->x2 - bitem->but->x1;
236                 if(r_h) *r_h= bitem->but->y2 - bitem->but->y1;
237         }
238         else {
239                 uiLayout *litem= (uiLayout*)item;
240
241                 if(r_w) *r_w= litem->w;
242                 if(r_h) *r_h= litem->h;
243         }
244 }
245
246 static void ui_item_offset(uiItem *item, int *r_x, int *r_y)
247 {
248         if(item->type == ITEM_BUTTON) {
249                 uiButtonItem *bitem= (uiButtonItem*)item;
250
251                 if(r_x) *r_x= bitem->but->x1;
252                 if(r_y) *r_y= bitem->but->y1;
253         }
254         else {
255                 if(r_x) *r_x= 0;
256                 if(r_y) *r_y= 0;
257         }
258 }
259
260 static void ui_item_position(uiItem *item, int x, int y, int w, int h)
261 {
262         if(item->type == ITEM_BUTTON) {
263                 uiButtonItem *bitem= (uiButtonItem*)item;
264
265                 bitem->but->x1= x;
266                 bitem->but->y1= y;
267                 bitem->but->x2= x+w;
268                 bitem->but->y2= y+h;
269                 
270                 ui_check_but(bitem->but); /* for strlen */
271         }
272         else {
273                 uiLayout *litem= (uiLayout*)item;
274
275                 litem->x= x;
276                 litem->y= y+h;
277                 litem->w= w;
278                 litem->h= h;
279         }
280 }
281
282 /******************** Special RNA Items *********************/
283
284 static int ui_layout_local_dir(uiLayout *layout)
285 {
286         switch(layout->item.type) {
287                 case ITEM_LAYOUT_ROW:
288                 case ITEM_LAYOUT_ROOT:
289                         return UI_LAYOUT_HORIZONTAL;
290                 case ITEM_LAYOUT_COLUMN:
291                 case ITEM_LAYOUT_COLUMN_FLOW:
292                 case ITEM_LAYOUT_SPLIT:
293                 case ITEM_LAYOUT_FREE:
294                 case ITEM_LAYOUT_BOX:
295                 default:
296                         return UI_LAYOUT_VERTICAL;
297         }
298 }
299
300 static uiLayout *ui_item_local_sublayout(uiLayout *test, uiLayout *layout, int align)
301 {
302         uiLayout *sub;
303
304         if(ui_layout_local_dir(test) == UI_LAYOUT_HORIZONTAL)
305                 sub= uiLayoutRow(layout, align);
306         else
307                 sub= uiLayoutColumn(layout, align);
308         
309         sub->space= 0;
310         return sub;
311 }
312
313 static void ui_layer_but_cb(bContext *C, void *arg_but, void *arg_index)
314 {
315         wmWindow *win= CTX_wm_window(C);
316         uiBut *but= arg_but, *cbut;
317         PointerRNA *ptr= &but->rnapoin;
318         PropertyRNA *prop= but->rnaprop;
319         int i, index= GET_INT_FROM_POINTER(arg_index);
320         int shift= win->eventstate->shift;
321         int len= RNA_property_array_length(ptr, prop);
322
323         if(!shift) {
324                 RNA_property_boolean_set_index(ptr, prop, index, 1);
325
326                 for(i=0; i<len; i++)
327                         if(i != index)
328                                 RNA_property_boolean_set_index(ptr, prop, i, 0);
329
330                 RNA_property_update(C, ptr, prop);
331
332                 for(cbut=but->block->buttons.first; cbut; cbut=cbut->next)
333                         ui_check_but(cbut);
334         }
335 }
336
337 /* create buttons for an item with an RNA array */
338 static void ui_item_array(uiLayout *layout, uiBlock *block, char *name, int icon, PointerRNA *ptr, PropertyRNA *prop, int len, int x, int y, int w, int h, int expand, int slider)
339 {
340         uiStyle *style= layout->root->style;
341         uiBut *but;
342         PropertyType type;
343         PropertySubType subtype;
344         uiLayout *sub;
345         int a, b;
346
347         /* retrieve type and subtype */
348         type= RNA_property_type(prop);
349         subtype= RNA_property_subtype(prop);
350
351         sub= ui_item_local_sublayout(layout, layout, 1);
352         uiBlockSetCurLayout(block, sub);
353
354         /* create label */
355         if(strcmp(name, "") != 0)
356                 uiDefBut(block, LABEL, 0, name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
357
358         /* create buttons */
359         if(type == PROP_BOOLEAN && ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER)) {
360                 /* special check for layer layout */
361                 int butw, buth, unit;
362                 int cols= (len >= 20)? 2: 1;
363                 int colbuts= len/(2*cols);
364
365                 uiBlockSetCurLayout(block, uiLayoutFree(layout, 0));
366
367                 unit= UI_UNIT_X*0.75;
368                 butw= unit;
369                 buth= unit;
370
371                 for(b=0; b<cols; b++) {
372                         uiBlockBeginAlign(block);
373
374                         for(a=0; a<colbuts; a++) {
375                                 but= uiDefAutoButR(block, ptr, prop, a+b*colbuts, "", ICON_BLANK1, x + butw*a, y+buth, butw, buth);
376                                 if(subtype == PROP_LAYER_MEMBER)
377                                         uiButSetFunc(but, ui_layer_but_cb, but, SET_INT_IN_POINTER(a+b*colbuts));
378                         }
379                         for(a=0; a<colbuts; a++) {
380                                 but= uiDefAutoButR(block, ptr, prop, a+len/2+b*colbuts, "", ICON_BLANK1, x + butw*a, y, butw, buth);
381                                 if(subtype == PROP_LAYER_MEMBER)
382                                         uiButSetFunc(but, ui_layer_but_cb, but, SET_INT_IN_POINTER(a+len/2+b*colbuts));
383                         }
384                         uiBlockEndAlign(block);
385
386                         x += colbuts*butw + style->buttonspacex;
387                 }
388         }
389         else if(subtype == PROP_MATRIX) {
390                 /* matrix layout */
391                 int row, col;
392
393                 uiBlockSetCurLayout(block, uiLayoutFree(layout, 1));
394
395                 len= ceil(sqrt(len));
396
397                 h /= len;
398                 w /= len;
399
400                 // XXX test
401                 for(a=0; a<len; a++) {
402                         col= a%len;
403                         row= a/len;
404
405                         but= uiDefAutoButR(block, ptr, prop, a, "", 0, x + w*col, y+(row-a-1)*UI_UNIT_Y, w, UI_UNIT_Y);
406                         if(slider && but->type==NUM)
407                                 but->type= NUMSLI;
408                 }
409         }
410         else {
411                 if(ELEM(subtype, PROP_COLOR, PROP_RGB))
412                         uiDefAutoButR(block, ptr, prop, -1, "", 0, 0, 0, w, UI_UNIT_Y);
413
414                 if(!ELEM(subtype, PROP_COLOR, PROP_RGB) || expand) {
415                         /* layout for known array subtypes */
416                         char str[3];
417
418                         for(a=0; a<len; a++) {
419                                 str[0]= RNA_property_array_item_char(prop, a);
420
421                                 if(str[0]) {
422                                         if(type == PROP_BOOLEAN) {
423                                                 str[1]= '\0';
424                                         }
425                                         else {
426                                                 str[1]= ':';
427                                                 str[2]= '\0';
428                                         }
429                                 }
430
431                                 but= uiDefAutoButR(block, ptr, prop, a, str, 0, 0, 0, w, UI_UNIT_Y);
432                                 if(slider && but->type==NUM)
433                                         but->type= NUMSLI;
434                         }
435                 }
436                 else if(ELEM(subtype, PROP_COLOR, PROP_RGB) && len == 4) {
437                         but= uiDefAutoButR(block, ptr, prop, 3, "A:", 0, 0, 0, w, UI_UNIT_Y);
438                         if(slider && but->type==NUM)
439                                 but->type= NUMSLI;
440                 }
441         }
442
443         uiBlockSetCurLayout(block, layout);
444 }
445
446 static void ui_item_enum_row(uiLayout *layout, uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, char *uiname, int x, int y, int w, int h)
447 {
448         EnumPropertyItem *item;
449         const char *identifier;
450         char *name;
451         int a, totitem, itemw, icon, value, free;
452
453         identifier= RNA_property_identifier(prop);
454         RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
455
456         uiBlockSetCurLayout(block, ui_item_local_sublayout(layout, layout, 1));
457         for(a=0; a<totitem; a++) {
458                 if(!item[a].identifier[0])
459                         continue;
460
461                 name= (!uiname || uiname[0])? (char*)item[a].name: "";
462                 icon= item[a].icon;
463                 value= item[a].value;
464                 itemw= ui_text_icon_width(block->curlayout, name, icon, 0);
465
466                 if(icon && strcmp(name, "") != 0)
467                         uiDefIconTextButR(block, ROW, 0, icon, name, 0, 0, itemw, h, ptr, identifier, -1, 0, value, -1, -1, NULL);
468                 else if(icon)
469                         uiDefIconButR(block, ROW, 0, icon, 0, 0, itemw, h, ptr, identifier, -1, 0, value, -1, -1, NULL);
470                 else
471                         uiDefButR(block, ROW, 0, name, 0, 0, itemw, h, ptr, identifier, -1, 0, value, -1, -1, NULL);
472         }
473         uiBlockSetCurLayout(block, layout);
474
475         if(free)
476                 MEM_freeN(item);
477 }
478
479 /* create label + button for RNA property */
480 static uiBut *ui_item_with_label(uiLayout *layout, uiBlock *block, char *name, int icon, PointerRNA *ptr, PropertyRNA *prop, int index, int x, int y, int w, int h)
481 {
482         uiLayout *sub;
483         uiBut *but;
484         PropertySubType subtype;
485         int labelw;
486
487         sub= uiLayoutRow(layout, 0);
488         uiBlockSetCurLayout(block, sub);
489
490         if(strcmp(name, "") != 0) {
491                 /* XXX UI_GetStringWidth is not accurate
492                 labelw= UI_GetStringWidth(name);
493                 CLAMP(labelw, w/4, 3*w/4);*/
494                 labelw= w/2;
495                 uiDefBut(block, LABEL, 0, name, x, y, labelw, h, NULL, 0.0, 0.0, 0, 0, "");
496                 w= w-labelw;
497         }
498
499         subtype= RNA_property_subtype(prop);
500
501         if(subtype == PROP_FILEPATH || subtype == PROP_DIRPATH) {
502                 uiBlockSetCurLayout(block, uiLayoutRow(sub, 1));
503                 uiDefAutoButR(block, ptr, prop, index, "", icon, x, y, w-UI_UNIT_X, h);
504
505                 /* BUTTONS_OT_file_browse calls uiFileBrowseContextProperty */
506                 but= uiDefIconButO(block, BUT, "BUTTONS_OT_file_browse", WM_OP_INVOKE_DEFAULT, ICON_FILESEL, x, y, UI_UNIT_X, h, "Browse for file or directory.");
507         }
508         else
509                 but= uiDefAutoButR(block, ptr, prop, index, "", icon, x, y, w, h);
510
511         uiBlockSetCurLayout(block, layout);
512         return but;
513 }
514
515 void uiFileBrowseContextProperty(const bContext *C, PointerRNA *ptr, PropertyRNA **prop)
516 {
517         ARegion *ar= CTX_wm_region(C);
518         uiBlock *block;
519         uiBut *but, *prevbut;
520
521         memset(ptr, 0, sizeof(*ptr));
522         *prop= NULL;
523
524         if(!ar)
525                 return;
526
527         for(block=ar->uiblocks.first; block; block=block->next) {
528                 for(but=block->buttons.first; but; but= but->next) {
529                         prevbut= but->prev;
530
531                         /* find the button before the active one */
532                         if((but->flag & UI_BUT_LAST_ACTIVE) && prevbut && prevbut->rnapoin.id.data) {
533                                 if(RNA_property_type(prevbut->rnaprop) == PROP_STRING) {
534                                         *ptr= prevbut->rnapoin;
535                                         *prop= prevbut->rnaprop;
536                                         return;
537                                 }
538                         }
539                 }
540         }
541 }
542
543 /********************* Button Items *************************/
544
545 /* disabled item */
546 static void ui_item_disabled(uiLayout *layout, char *name)
547 {
548         uiBlock *block= layout->root->block;
549         uiBut *but;
550         int w;
551
552         uiBlockSetCurLayout(block, layout);
553
554         if(!name)
555                 name= "";
556
557         w= ui_text_icon_width(layout, name, 0, 0);
558
559         but= uiDefBut(block, LABEL, 0, (char*)name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
560         but->flag |= UI_BUT_DISABLED;
561         but->lock = 1;
562         but->lockstr = "";
563 }
564
565 /* operator items */
566 PointerRNA uiItemFullO(uiLayout *layout, char *name, int icon, char *idname, IDProperty *properties, int context, int flag)
567 {
568         uiBlock *block= layout->root->block;
569         wmOperatorType *ot= WM_operatortype_find(idname, 0);
570         uiBut *but;
571         int w;
572
573         if(!ot) {
574                 ui_item_disabled(layout, idname);
575                 return PointerRNA_NULL;
576         }
577
578         if(!name)
579                 name= ot->name;
580         if(layout->root->type == UI_LAYOUT_MENU && !icon)
581                 icon= ICON_BLANK1;
582
583         /* create button */
584         uiBlockSetCurLayout(block, layout);
585
586         w= ui_text_icon_width(layout, name, icon, 0);
587
588         if(icon && strcmp(name, "") != 0)
589                 but= uiDefIconTextButO(block, BUT, ot->idname, context, icon, (char*)name, 0, 0, w, UI_UNIT_Y, NULL);
590         else if(icon)
591                 but= uiDefIconButO(block, BUT, ot->idname, context, icon, 0, 0, w, UI_UNIT_Y, NULL);
592         else
593                 but= uiDefButO(block, BUT, ot->idname, context, (char*)name, 0, 0, w, UI_UNIT_Y, NULL);
594
595         /* assign properties */
596         if(properties || (flag & UI_ITEM_O_RETURN_PROPS)) {
597                 PointerRNA *opptr= uiButGetOperatorPtrRNA(but);
598
599                 if(properties) {
600                         opptr->data= properties;
601                 }
602                 else {
603                         IDPropertyTemplate val = {0};
604                         opptr->data= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
605                 }
606
607                 return *opptr;
608         }
609
610         return PointerRNA_NULL;
611 }
612
613 static char *ui_menu_enumpropname(uiLayout *layout, char *opname, char *propname, int retval)
614 {
615         wmOperatorType *ot= WM_operatortype_find(opname, 0);
616         PointerRNA ptr;
617         PropertyRNA *prop;
618
619         if(!ot || !ot->srna)
620                 return "";
621
622         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
623         prop= RNA_struct_find_property(&ptr, propname);
624
625         if(prop) {
626                 EnumPropertyItem *item;
627                 int totitem, free;
628                 const char *name;
629
630                 RNA_property_enum_items(layout->root->block->evil_C, &ptr, prop, &item, &totitem, &free);
631                 if(RNA_enum_name(item, retval, &name)) {
632                         if(free) MEM_freeN(item);
633                         return (char*)name;
634                 }
635                 
636                 if(free)
637                         MEM_freeN(item);
638         }
639
640         return "";
641 }
642
643 void uiItemEnumO(uiLayout *layout, char *name, int icon, char *opname, char *propname, int value)
644 {
645         PointerRNA ptr;
646
647         WM_operator_properties_create(&ptr, opname);
648         RNA_enum_set(&ptr, propname, value);
649
650         if(!name)
651                 name= ui_menu_enumpropname(layout, opname, propname, value);
652
653         uiItemFullO(layout, name, icon, opname, ptr.data, layout->root->opcontext, 0);
654 }
655
656 void uiItemsEnumO(uiLayout *layout, char *opname, char *propname)
657 {
658         wmOperatorType *ot= WM_operatortype_find(opname, 0);
659         PointerRNA ptr;
660         PropertyRNA *prop;
661         uiBut *bt;
662         uiBlock *block= layout->root->block;
663
664         if(!ot || !ot->srna) {
665                 ui_item_disabled(layout, opname);
666                 return;
667         }
668
669         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
670         prop= RNA_struct_find_property(&ptr, propname);
671
672         if(prop && RNA_property_type(prop) == PROP_ENUM) {
673                 EnumPropertyItem *item;
674                 int totitem, i, free;
675                 uiLayout *split= uiLayoutSplit(layout, 0);
676                 uiLayout *column= uiLayoutColumn(split, 0);
677
678                 RNA_property_enum_items(block->evil_C, &ptr, prop, &item, &totitem, &free);
679
680                 for(i=0; i<totitem; i++) {
681                         if(item[i].identifier[0]) {
682                                 uiItemEnumO(column, (char*)item[i].name, item[i].icon, opname, propname, item[i].value);
683                         }
684                         else {
685                                 if(item[i].name) {
686                                         if(i != 0) {
687                                                 column= uiLayoutColumn(split, 0);
688                                                 /* inconsistent, but menus with labels do not look good flipped */
689                                                 block->flag |= UI_BLOCK_NO_FLIP;
690                                         }
691
692                                         uiItemL(column, (char*)item[i].name, 0);
693                                         bt= block->buttons.last;
694                                         bt->flag= UI_TEXT_LEFT;
695                                 }
696                                 else
697                                         uiItemS(column);
698                         }
699                 }
700
701                 if(free)
702                         MEM_freeN(item);
703         }
704 }
705
706 /* for use in cases where we have */
707 void uiItemEnumO_string(uiLayout *layout, char *name, int icon, char *opname, char *propname, char *value_str)
708 {
709         PointerRNA ptr;
710         
711         /* for getting the enum */
712         PropertyRNA *prop;
713         EnumPropertyItem *item;
714         int value, free;
715
716         WM_operator_properties_create(&ptr, opname);
717         
718         /* enum lookup */
719         if((prop= RNA_struct_find_property(&ptr, propname))) {
720                 RNA_property_enum_items(layout->root->block->evil_C, &ptr, prop, &item, NULL, &free);
721                 if(RNA_enum_value_from_id(item, value_str, &value)==0) {
722                         if(free) MEM_freeN(item);
723                         printf("uiItemEnumO_string: %s.%s, enum %s not found.\n", RNA_struct_identifier(ptr.type), propname, value_str);
724                         return;
725                 }
726
727                 if(free)
728                         MEM_freeN(item);
729         }
730         else {
731                 printf("uiItemEnumO_string: %s.%s not found.\n", RNA_struct_identifier(ptr.type), propname);
732                 return;
733         }
734         
735         RNA_property_enum_set(&ptr, prop, value);
736         
737         /* same as uiItemEnumO */
738         if(!name)
739                 name= ui_menu_enumpropname(layout, opname, propname, value);
740
741         uiItemFullO(layout, name, icon, opname, ptr.data, layout->root->opcontext, 0);
742 }
743
744 void uiItemBooleanO(uiLayout *layout, char *name, int icon, char *opname, char *propname, int value)
745 {
746         PointerRNA ptr;
747
748         WM_operator_properties_create(&ptr, opname);
749         RNA_boolean_set(&ptr, propname, value);
750
751         uiItemFullO(layout, name, icon, opname, ptr.data, layout->root->opcontext, 0);
752 }
753
754 void uiItemIntO(uiLayout *layout, char *name, int icon, char *opname, char *propname, int value)
755 {
756         PointerRNA ptr;
757
758         WM_operator_properties_create(&ptr, opname);
759         RNA_int_set(&ptr, propname, value);
760
761         uiItemFullO(layout, name, icon, opname, ptr.data, layout->root->opcontext, 0);
762 }
763
764 void uiItemFloatO(uiLayout *layout, char *name, int icon, char *opname, char *propname, float value)
765 {
766         PointerRNA ptr;
767
768         WM_operator_properties_create(&ptr, opname);
769         RNA_float_set(&ptr, propname, value);
770
771         uiItemFullO(layout, name, icon, opname, ptr.data, layout->root->opcontext, 0);
772 }
773
774 void uiItemStringO(uiLayout *layout, char *name, int icon, char *opname, char *propname, char *value)
775 {
776         PointerRNA ptr;
777
778         WM_operator_properties_create(&ptr, opname);
779         RNA_string_set(&ptr, propname, value);
780
781         uiItemFullO(layout, name, icon, opname, ptr.data, layout->root->opcontext, 0);
782 }
783
784 void uiItemO(uiLayout *layout, char *name, int icon, char *opname)
785 {
786         uiItemFullO(layout, name, icon, opname, NULL, layout->root->opcontext, 0);
787 }
788
789 /* RNA property items */
790
791 static void ui_item_rna_size(uiLayout *layout, char *name, int icon, PointerRNA *ptr, PropertyRNA *prop, int index, int *r_w, int *r_h)
792 {
793         PropertyType type;
794         PropertySubType subtype;
795         int len, w, h;
796
797         /* arbitrary extended width by type */
798         type= RNA_property_type(prop);
799         subtype= RNA_property_subtype(prop);
800         len= RNA_property_array_length(ptr, prop);
801
802         if(ELEM3(type, PROP_STRING, PROP_POINTER, PROP_ENUM) && !name[0])
803                 name= "non-empty text";
804         else if(type == PROP_BOOLEAN && !name[0])
805                 icon= ICON_DOT;
806
807         w= ui_text_icon_width(layout, name, icon, 0);
808         h= UI_UNIT_Y;
809
810         /* increase height for arrays */
811         if(index == RNA_NO_INDEX && len > 0) {
812                 if(!name[0] && icon == 0)
813                         h= 0;
814
815                 if(ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER))
816                         h += 2*UI_UNIT_Y;
817                 else if(subtype == PROP_MATRIX)
818                         h += ceil(sqrt(len))*UI_UNIT_Y;
819                 else
820                         h += len*UI_UNIT_Y;
821         }
822         else if(ui_layout_vary_direction(layout) == UI_ITEM_VARY_X) {
823                 if(type == PROP_BOOLEAN && strcmp(name, "") != 0)
824                         w += UI_UNIT_X/5;
825                 else if(type == PROP_ENUM)
826                         w += UI_UNIT_X/2;
827                 else if(type == PROP_FLOAT || type == PROP_INT)
828                         w += UI_UNIT_X*3;
829         }
830
831         *r_w= w;
832         *r_h= h;
833 }
834
835 void uiItemFullR(uiLayout *layout, char *name, int icon, PointerRNA *ptr, PropertyRNA *prop, int index, int value, int flag)
836 {
837         uiBlock *block= layout->root->block;
838         uiBut *but;
839         PropertyType type;
840         char namestr[UI_MAX_NAME_STR];
841         int len, w, h, slider, toggle, expand;
842
843         if(!ptr->data || !prop)
844                 return;
845
846         uiBlockSetCurLayout(block, layout);
847
848         /* retrieve info */
849         type= RNA_property_type(prop);
850         len= RNA_property_array_length(ptr, prop);
851
852         /* set name and icon */
853         if(!name)
854                 name= (char*)RNA_property_ui_name(prop);
855         if(!icon)
856                 icon= RNA_property_ui_icon(prop);
857
858         if(ELEM4(type, PROP_INT, PROP_FLOAT, PROP_STRING, PROP_POINTER))
859                 name= ui_item_name_add_colon(name, namestr);
860         else if(type == PROP_BOOLEAN && len)
861                 name= ui_item_name_add_colon(name, namestr);
862         else if(type == PROP_ENUM && index != RNA_ENUM_VALUE)
863                 name= ui_item_name_add_colon(name, namestr);
864
865         if(layout->root->type == UI_LAYOUT_MENU) {
866                 if(type == PROP_BOOLEAN)
867                         icon= (RNA_property_boolean_get(ptr, prop))? ICON_CHECKBOX_HLT: ICON_CHECKBOX_DEHLT;
868                 else if(type == PROP_ENUM && index == RNA_ENUM_VALUE)
869                         icon= (RNA_property_enum_get(ptr, prop) == value)? ICON_CHECKBOX_HLT: ICON_CHECKBOX_DEHLT; 
870         }
871
872         slider= (flag & UI_ITEM_R_SLIDER);
873         toggle= (flag & UI_ITEM_R_TOGGLE);
874         expand= (flag & UI_ITEM_R_EXPAND);
875
876         /* get size */
877         ui_item_rna_size(layout, name, icon, ptr, prop, index, &w, &h);
878
879         /* array property */
880         if(index == RNA_NO_INDEX && len > 0)
881                 ui_item_array(layout, block, name, icon, ptr, prop, len, 0, 0, w, h, expand, slider);
882         /* enum item */
883         else if(type == PROP_ENUM && index == RNA_ENUM_VALUE) {
884                 char *identifier= (char*)RNA_property_identifier(prop);
885
886                 if(icon && strcmp(name, "") != 0)
887                         uiDefIconTextButR(block, ROW, 0, icon, name, 0, 0, w, h, ptr, identifier, -1, 0, value, -1, -1, NULL);
888                 else if(icon)
889                         uiDefIconButR(block, ROW, 0, icon, 0, 0, w, h, ptr, identifier, -1, 0, value, -1, -1, NULL);
890                 else
891                         uiDefButR(block, ROW, 0, name, 0, 0, w, h, ptr, identifier, -1, 0, value, -1, -1, NULL);
892         }
893         /* expanded enum */
894         else if(type == PROP_ENUM && expand)
895                 ui_item_enum_row(layout, block, ptr, prop, name, 0, 0, w, h);
896         /* property with separate label */
897         else if(type == PROP_ENUM || type == PROP_STRING || type == PROP_POINTER) {
898                 but= ui_item_with_label(layout, block, name, icon, ptr, prop, index, 0, 0, w, h);
899                 ui_but_add_search(but, ptr, prop, NULL, NULL);
900         }
901         /* single button */
902         else {
903                 but= uiDefAutoButR(block, ptr, prop, index, (char*)name, icon, 0, 0, w, h);
904
905                 if(slider && but->type==NUM)
906                         but->type= NUMSLI;
907
908                 if(toggle && but->type==OPTION)
909                         but->type= TOG;
910         }
911 }
912
913 void uiItemR(uiLayout *layout, char *name, int icon, PointerRNA *ptr, char *propname, int flag)
914 {
915         PropertyRNA *prop;
916
917         if(!ptr->data || !propname)
918                 return;
919
920         prop= RNA_struct_find_property(ptr, propname);
921
922         if(!prop) {
923                 ui_item_disabled(layout, propname);
924                 printf("uiItemR: property not found: %s\n", propname);
925                 return;
926         }
927
928         uiItemFullR(layout, name, icon, ptr, prop, RNA_NO_INDEX, 0, flag);
929 }
930
931 void uiItemEnumR(uiLayout *layout, char *name, int icon, struct PointerRNA *ptr, char *propname, int value)
932 {
933         PropertyRNA *prop;
934
935         if(!ptr->data || !propname)
936                 return;
937
938         prop= RNA_struct_find_property(ptr, propname);
939
940         if(!prop || RNA_property_type(prop) != PROP_ENUM) {
941                 ui_item_disabled(layout, propname);
942                 printf("uiItemEnumR: enum property not found: %s\n", propname);
943                 return;
944         }
945
946         uiItemFullR(layout, name, icon, ptr, prop, RNA_ENUM_VALUE, value, 0);
947 }
948
949 void uiItemEnumR_string(uiLayout *layout, char *name, int icon, struct PointerRNA *ptr, char *propname, char *value)
950 {
951         PropertyRNA *prop;
952         EnumPropertyItem *item;
953         int ivalue, a, free;
954
955         if(!ptr->data || !propname)
956                 return;
957
958         prop= RNA_struct_find_property(ptr, propname);
959
960         if(!prop || RNA_property_type(prop) != PROP_ENUM) {
961                 ui_item_disabled(layout, propname);
962                 printf("uiItemEnumR: enum property not found: %s\n", propname);
963                 return;
964         }
965
966         RNA_property_enum_items(layout->root->block->evil_C, ptr, prop, &item, NULL, &free);
967
968         if(!RNA_enum_value_from_id(item, value, &ivalue)) {
969                 if(free) MEM_freeN(item);
970                 ui_item_disabled(layout, propname);
971                 printf("uiItemEnumR: enum property value not found: %s\n", value);
972                 return;
973         }
974
975         for(a=0; item[a].identifier; a++) {
976                 if(item[a].value == ivalue) {
977                         uiItemFullR(layout, (char*)item[a].name, item[a].icon, ptr, prop, RNA_ENUM_VALUE, ivalue, 0);
978                         break;
979                 }
980         }
981
982         if(free)
983                 MEM_freeN(item);
984 }
985
986 void uiItemsEnumR(uiLayout *layout, struct PointerRNA *ptr, char *propname)
987 {
988         PropertyRNA *prop;
989         uiBlock *block= layout->root->block;
990         uiBut *bt;
991
992         prop= RNA_struct_find_property(ptr, propname);
993
994         if(!prop) {
995                 ui_item_disabled(layout, propname);
996                 return;
997         }
998
999         if(RNA_property_type(prop) == PROP_ENUM) {
1000                 EnumPropertyItem *item;
1001                 int totitem, i, free;
1002                 uiLayout *split= uiLayoutSplit(layout, 0);
1003                 uiLayout *column= uiLayoutColumn(split, 0);
1004
1005                 RNA_property_enum_items(block->evil_C, ptr, prop, &item, &totitem, &free);
1006
1007                 for(i=0; i<totitem; i++) {
1008                         if(item[i].identifier[0]) {
1009                                 uiItemEnumR(column, (char*)item[i].name, 0, ptr, propname, item[i].value);
1010                         }
1011                         else {
1012                                 if(item[i].name) {
1013                                         if(i != 0) {
1014                                                 column= uiLayoutColumn(split, 0);
1015                                                 /* inconsistent, but menus with labels do not look good flipped */
1016                                                 block->flag |= UI_BLOCK_NO_FLIP;
1017                                         }
1018
1019                                         uiItemL(column, (char*)item[i].name, 0);
1020                                         bt= block->buttons.last;
1021                                         bt->flag= UI_TEXT_LEFT;
1022                                 }
1023                                 else
1024                                         uiItemS(column);
1025                         }
1026                 }
1027
1028                 if(free)
1029                         MEM_freeN(item);
1030         }
1031 }
1032
1033 /* Pointer RNA button with search */
1034
1035 static void rna_search_cb(const struct bContext *C, void *arg_but, char *str, uiSearchItems *items)
1036 {
1037         Scene *scene= CTX_data_scene(C);
1038         uiBut *but= arg_but;
1039         char *name;
1040         int i, iconid;
1041
1042         i = 0;
1043         RNA_PROP_BEGIN(&but->rnasearchpoin, itemptr, but->rnasearchprop) {
1044                 iconid= 0;
1045                 if(RNA_struct_is_ID(itemptr.type))
1046                         iconid= ui_id_icon_get(scene, itemptr.data);
1047
1048                 name= RNA_struct_name_get_alloc(&itemptr, NULL, 0);
1049
1050                 if(name) {
1051                         if(BLI_strcasestr(name, str)) {
1052                                 if(!uiSearchItemAdd(items, name, SET_INT_IN_POINTER(i), iconid)) {
1053                                         MEM_freeN(name);
1054                                         break;
1055                                 }
1056                         }
1057
1058                         MEM_freeN(name);
1059                 }
1060
1061                 i++;
1062         }
1063         RNA_PROP_END;
1064 }
1065
1066 static void search_id_collection(StructRNA *ptype, PointerRNA *ptr, PropertyRNA **prop)
1067 {
1068         StructRNA *srna;
1069
1070         /* look for collection property in Main */
1071         RNA_main_pointer_create(G.main, ptr);
1072
1073         *prop= NULL;
1074
1075         RNA_STRUCT_BEGIN(ptr, iprop) {
1076                 /* if it's a collection and has same pointer type, we've got it */
1077                 if(RNA_property_type(iprop) == PROP_COLLECTION) {
1078                         srna= RNA_property_pointer_type(ptr, iprop);
1079
1080                         if(ptype == srna) {
1081                                 *prop= iprop;
1082                                 break;
1083                         }
1084                 }
1085         }
1086         RNA_STRUCT_END;
1087 }
1088
1089 void ui_but_add_search(uiBut *but, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop)
1090 {
1091         StructRNA *ptype;
1092         PointerRNA sptr;
1093
1094         /* for ID's we do automatic lookup */
1095         if(!searchprop) {
1096                 if(RNA_property_type(prop) == PROP_POINTER) {
1097                         ptype= RNA_property_pointer_type(ptr, prop);
1098                         search_id_collection(ptype, &sptr, &searchprop);
1099                         searchptr= &sptr;
1100                 }
1101         }
1102
1103         /* turn button into search button */
1104         if(searchprop) {
1105                 but->type= SEARCH_MENU;
1106                 but->hardmax= MAX2(but->hardmax, 256);
1107                 but->rnasearchpoin= *searchptr;
1108                 but->rnasearchprop= searchprop;
1109                 but->flag |= UI_ICON_LEFT|UI_TEXT_LEFT;
1110
1111                 uiButSetSearchFunc(but, rna_search_cb, but, NULL, NULL);
1112         }
1113 }
1114
1115 void uiItemPointerR(uiLayout *layout, char *name, int icon, struct PointerRNA *ptr, char *propname, struct PointerRNA *searchptr, char *searchpropname)
1116 {
1117         PropertyRNA *prop, *searchprop;
1118         PropertyType type;
1119         uiBut *but;
1120         uiBlock *block;
1121         StructRNA *icontype;
1122         int w, h;
1123         
1124         /* validate arguments */
1125         if(!ptr->data || !searchptr->data)
1126                 return;
1127
1128         prop= RNA_struct_find_property(ptr, propname);
1129
1130         if(!prop) {
1131                 printf("uiItemPointerR: property not found: %s\n", propname);
1132                 return;
1133         }
1134         
1135         type= RNA_property_type(prop);
1136         if(!ELEM(type, PROP_POINTER, PROP_STRING)) {
1137                 printf("uiItemPointerR: property %s must be a pointer or string.\n", propname);
1138                 return;
1139         }
1140
1141         searchprop= RNA_struct_find_property(searchptr, searchpropname);
1142
1143         if(!searchprop || RNA_property_type(searchprop) != PROP_COLLECTION) {
1144                 printf("uiItemPointerR: search collection property not found: %s\n", searchpropname);
1145                 return;
1146         }
1147
1148         /* get icon & name */
1149         if(!icon) {
1150                 if(type == PROP_POINTER)
1151                         icontype= RNA_property_pointer_type(ptr, prop);
1152                 else
1153                         icontype= RNA_property_pointer_type(searchptr, searchprop);
1154
1155                 icon= RNA_struct_ui_icon(icontype);
1156         }
1157         if(!name)
1158                 name= (char*)RNA_property_ui_name(prop);
1159
1160         /* create button */
1161         block= uiLayoutGetBlock(layout);
1162
1163         ui_item_rna_size(layout, name, icon, ptr, prop, 0, &w, &h);
1164         but= ui_item_with_label(layout, block, name, icon, ptr, prop, 0, 0, 0, w, h);
1165
1166         ui_but_add_search(but, ptr, prop, searchptr, searchprop);
1167 }
1168
1169 /* menu item */
1170 static void ui_item_menutype_func(bContext *C, uiLayout *layout, void *arg_mt)
1171 {
1172         MenuType *mt= (MenuType*)arg_mt;
1173         Menu menu = {0};
1174
1175         menu.type= mt;
1176         menu.layout= layout;
1177         mt->draw(C, &menu);
1178 }
1179
1180 static void ui_item_menu(uiLayout *layout, char *name, int icon, uiMenuCreateFunc func, void *arg, void *argN)
1181 {
1182         uiBlock *block= layout->root->block;
1183         uiBut *but;
1184         int w, h;
1185
1186         uiBlockSetCurLayout(block, layout);
1187
1188         if(layout->root->type == UI_LAYOUT_HEADER)
1189                 uiBlockSetEmboss(block, UI_EMBOSS);
1190
1191         if(!name)
1192                 name= "";
1193         if(layout->root->type == UI_LAYOUT_MENU && !icon)
1194                 icon= ICON_BLANK1;
1195
1196         w= ui_text_icon_width(layout, name, icon, 1);
1197         h= UI_UNIT_Y;
1198
1199         if(layout->root->type == UI_LAYOUT_HEADER) /* ugly .. */
1200                 w -= 10;
1201
1202         if(icon)
1203                 but= uiDefIconTextMenuBut(block, func, arg, icon, (char*)name, 0, 0, w, h, "");
1204         else
1205                 but= uiDefMenuBut(block, func, arg, (char*)name, 0, 0, w, h, "");
1206
1207         if(argN) { /* ugly .. */
1208                 but->poin= (char*)but;
1209                 but->func_argN= argN;
1210         }
1211
1212         if(layout->root->type == UI_LAYOUT_HEADER)
1213                 uiBlockSetEmboss(block, UI_EMBOSS);
1214         else if(layout->root->type == UI_LAYOUT_PANEL)
1215                 but->type= MENU;
1216 }
1217
1218 void uiItemM(uiLayout *layout, bContext *C, char *name, int icon, char *menuname)
1219 {
1220         ARegion *ar= CTX_wm_region(C);
1221         MenuType *mt;
1222
1223         if(!menuname)
1224                 return;
1225
1226         for(mt=ar->type->menutypes.first; mt; mt=mt->next) {
1227                 if(strcmp(menuname, mt->idname) == 0) {
1228                         if(!name)
1229                                 name= mt->label;
1230                         if(layout->root->type == UI_LAYOUT_MENU && !icon)
1231                                 icon= ICON_BLANK1;
1232                         ui_item_menu(layout, name, icon, ui_item_menutype_func, mt, NULL);
1233                         return;
1234                 }
1235         }
1236
1237         printf("uiItemM: not found %s\n", menuname);
1238 }
1239
1240 /* label item */
1241 void uiItemL(uiLayout *layout, char *name, int icon)
1242 {
1243         uiBlock *block= layout->root->block;
1244         uiBut *but;
1245         int w;
1246
1247         uiBlockSetCurLayout(block, layout);
1248
1249         if(!name)
1250                 name= "";
1251         if(layout->root->type == UI_LAYOUT_MENU && !icon)
1252                 icon= ICON_BLANK1;
1253
1254         w= ui_text_icon_width(layout, name, icon, 0);
1255
1256         if(icon && strcmp(name, "") != 0)
1257                 but= uiDefIconTextBut(block, LABEL, 0, icon, (char*)name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1258         else if(icon)
1259                 but= uiDefIconBut(block, LABEL, 0, icon, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1260         else
1261                 but= uiDefBut(block, LABEL, 0, (char*)name, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1262 }
1263
1264 /* value item */
1265 void uiItemV(uiLayout *layout, char *name, int icon, int argval)
1266 {
1267         /* label */
1268         uiBlock *block= layout->root->block;
1269         float *retvalue= (block->handle)? &block->handle->retvalue: NULL;
1270         int w;
1271
1272         uiBlockSetCurLayout(block, layout);
1273
1274         if(!name)
1275                 name= "";
1276         if(layout->root->type == UI_LAYOUT_MENU && !icon)
1277                 icon= ICON_BLANK1;
1278
1279         w= ui_text_icon_width(layout, name, icon, 0);
1280
1281         if(icon && strcmp(name, "") != 0)
1282                 uiDefIconTextButF(block, BUTM, 0, icon, (char*)name, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, argval, "");
1283         else if(icon)
1284                 uiDefIconButF(block, BUTM, 0, icon, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, argval, "");
1285         else
1286                 uiDefButF(block, BUTM, 0, (char*)name, 0, 0, w, UI_UNIT_Y, retvalue, 0.0, 0.0, 0, argval, "");
1287 }
1288
1289 /* separator item */
1290 void uiItemS(uiLayout *layout)
1291 {
1292         uiBlock *block= layout->root->block;
1293
1294         uiBlockSetCurLayout(block, layout);
1295         uiDefBut(block, SEPR, 0, "", 0, 0, EM_SEPR_X, EM_SEPR_Y, NULL, 0.0, 0.0, 0, 0, "");
1296 }
1297
1298 /* level items */
1299 void uiItemMenuF(uiLayout *layout, char *name, int icon, uiMenuCreateFunc func, void *arg)
1300 {
1301         if(!func)
1302                 return;
1303
1304         ui_item_menu(layout, name, icon, func, arg, NULL);
1305 }
1306
1307 typedef struct MenuItemLevel {
1308         int opcontext;
1309         char *opname;
1310         char *propname;
1311         PointerRNA rnapoin;
1312 } MenuItemLevel;
1313
1314 static void menu_item_enum_opname_menu(bContext *C, uiLayout *layout, void *arg)
1315 {
1316         MenuItemLevel *lvl= (MenuItemLevel*)(((uiBut*)arg)->func_argN);
1317
1318         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_REGION_WIN);
1319         uiItemsEnumO(layout, lvl->opname, lvl->propname);
1320 }
1321
1322 void uiItemMenuEnumO(uiLayout *layout, char *name, int icon, char *opname, char *propname)
1323 {
1324         wmOperatorType *ot= WM_operatortype_find(opname, 0);
1325         MenuItemLevel *lvl;
1326
1327         if(!ot || !ot->srna) {
1328                 ui_item_disabled(layout, opname);
1329                 return;
1330         }
1331
1332         if(!name)
1333                 name= ot->name;
1334         if(layout->root->type == UI_LAYOUT_MENU && !icon)
1335                 icon= ICON_BLANK1;
1336
1337         lvl= MEM_callocN(sizeof(MenuItemLevel), "MenuItemLevel");
1338         lvl->opname= opname;
1339         lvl->propname= propname;
1340         lvl->opcontext= layout->root->opcontext;
1341
1342         ui_item_menu(layout, name, icon, menu_item_enum_opname_menu, NULL, lvl);
1343 }
1344
1345 static void menu_item_enum_rna_menu(bContext *C, uiLayout *layout, void *arg)
1346 {
1347         MenuItemLevel *lvl= (MenuItemLevel*)(((uiBut*)arg)->func_argN);
1348
1349         uiLayoutSetOperatorContext(layout, lvl->opcontext);
1350         uiItemsEnumR(layout, &lvl->rnapoin, lvl->propname);
1351 }
1352
1353 void uiItemMenuEnumR(uiLayout *layout, char *name, int icon, struct PointerRNA *ptr, char *propname)
1354 {
1355         MenuItemLevel *lvl;
1356         PropertyRNA *prop;
1357
1358         prop= RNA_struct_find_property(ptr, propname);
1359         if(!prop) {
1360                 ui_item_disabled(layout, propname);
1361                 return;
1362         }
1363
1364         if(!name)
1365                 name= (char*)RNA_property_ui_name(prop);
1366         if(layout->root->type == UI_LAYOUT_MENU && !icon)
1367                 icon= ICON_BLANK1;
1368
1369         lvl= MEM_callocN(sizeof(MenuItemLevel), "MenuItemLevel");
1370         lvl->rnapoin= *ptr;
1371         lvl->propname= propname;
1372         lvl->opcontext= layout->root->opcontext;
1373
1374         ui_item_menu(layout, name, icon, menu_item_enum_rna_menu, NULL, lvl);
1375 }
1376
1377 /**************************** Layout Items ***************************/
1378
1379 /* single-row layout */
1380 static void ui_litem_estimate_row(uiLayout *litem)
1381 {
1382         uiItem *item;
1383         int itemw, itemh;
1384
1385         litem->w= 0;
1386         litem->h= 0;
1387
1388         for(item=litem->items.first; item; item=item->next) {
1389                 ui_item_size(item, &itemw, &itemh);
1390
1391                 litem->w += itemw;
1392                 litem->h= MAX2(itemh, litem->h);
1393
1394                 if(item->next)
1395                         litem->w += litem->space;
1396         }
1397 }
1398
1399 static int ui_litem_min_width(int itemw)
1400 {
1401         return MIN2(2*UI_UNIT_X, itemw);
1402 }
1403
1404 static void ui_litem_layout_row(uiLayout *litem)
1405 {
1406         uiItem *item;
1407         int x, y, w, tot, totw, neww, itemw, minw, itemh, offset;
1408         int fixedw, freew, fixedx, freex, flag= 0, lastw= 0;
1409
1410         x= litem->x;
1411         y= litem->y;
1412         w= litem->w;
1413         totw= 0;
1414         tot= 0;
1415
1416         for(item=litem->items.first; item; item=item->next) {
1417                 ui_item_size(item, &itemw, &itemh);
1418                 totw += itemw;
1419                 tot++;
1420         }
1421
1422         if(totw == 0)
1423                 return;
1424         
1425         if(w != 0)
1426                 w -= (tot-1)*litem->space;
1427         fixedw= 0;
1428
1429         /* keep clamping items to fixed minimum size until all are done */
1430         do {
1431                 freew= 0;
1432                 x= 0;
1433                 flag= 0;
1434
1435                 for(item=litem->items.first; item; item=item->next) {
1436                         if(item->flag)
1437                                 continue;
1438
1439                         ui_item_size(item, &itemw, &itemh);
1440                         minw= ui_litem_min_width(itemw);
1441
1442                         if(w - lastw > 0)
1443                                 neww= ui_item_fit(itemw, x, totw, w-lastw, !item->next, litem->alignment, NULL);
1444                         else
1445                                 neww= 0; /* no space left, all will need clamping to minimum size */
1446
1447                         x += neww;
1448
1449                         if((neww < minw || itemw == minw) && w != 0) {
1450                                 /* fixed size */
1451                                 item->flag= 1;
1452                                 fixedw += minw;
1453                                 flag= 1;
1454                                 totw -= itemw;
1455                         }
1456                         else {
1457                                 /* keep free size */
1458                                 item->flag= 0;
1459                                 freew += itemw;
1460                         }
1461                 }
1462
1463                 lastw= fixedw;
1464         } while(flag);
1465
1466         freex= 0;
1467         fixedx= 0;
1468         x= litem->x;
1469
1470         for(item=litem->items.first; item; item=item->next) {
1471                 ui_item_size(item, &itemw, &itemh);
1472                 minw= ui_litem_min_width(itemw);
1473
1474                 if(item->flag) {
1475                         /* fixed minimum size items */
1476                         itemw= ui_item_fit(minw, fixedx, fixedw, MIN2(w, fixedw), !item->next, litem->alignment, NULL);
1477                         fixedx += itemw;
1478                 }
1479                 else {
1480                         /* free size item */
1481                         itemw= ui_item_fit(itemw, freex, freew, w-fixedw, !item->next, litem->alignment, NULL);
1482                         freex += itemw;
1483                 }
1484
1485                 /* align right/center */
1486                 offset= 0;
1487                 if(litem->alignment == UI_LAYOUT_ALIGN_RIGHT) {
1488                         if(fixedw == 0 && freew < w-fixedw)
1489                                 offset= (w - fixedw) - freew;
1490                 }
1491                 else if(litem->alignment == UI_LAYOUT_ALIGN_CENTER) {
1492                         if(fixedw == 0 && freew < w-fixedw)
1493                                 offset= ((w - fixedw) - freew)/2;
1494                 }
1495
1496                 /* position item */
1497                 ui_item_position(item, x+offset, y-itemh, itemw, itemh);
1498
1499                 x += itemw;
1500                 if(item->next)
1501                         x += litem->space;
1502         }
1503
1504         litem->w= x - litem->x;
1505         litem->h= litem->y - y;
1506         litem->x= x;
1507         litem->y= y;
1508 }
1509
1510 /* single-column layout */
1511 static void ui_litem_estimate_column(uiLayout *litem)
1512 {
1513         uiItem *item;
1514         int itemw, itemh;
1515
1516         litem->w= 0;
1517         litem->h= 0;
1518
1519         for(item=litem->items.first; item; item=item->next) {
1520                 ui_item_size(item, &itemw, &itemh);
1521
1522                 litem->w= MAX2(litem->w, itemw);
1523                 litem->h += itemh;
1524
1525                 if(item->next)
1526                         litem->h += litem->space;
1527         }
1528 }
1529
1530 static void ui_litem_layout_column(uiLayout *litem)
1531 {
1532         uiItem *item;
1533         int itemh, x, y;
1534
1535         x= litem->x;
1536         y= litem->y;
1537
1538         for(item=litem->items.first; item; item=item->next) {
1539                 ui_item_size(item, NULL, &itemh);
1540
1541                 y -= itemh;
1542                 ui_item_position(item, x, y, litem->w, itemh);
1543
1544                 if(item->next)
1545                         y -= litem->space;
1546         }
1547
1548         litem->h= litem->y - y;
1549         litem->x= x;
1550         litem->y= y;
1551 }
1552
1553 /* root layout */
1554 static void ui_litem_estimate_root(uiLayout *litem)
1555 {
1556         /* nothing to do */
1557 }
1558
1559 static void ui_litem_layout_root(uiLayout *litem)
1560 {
1561         if(litem->root->type == UI_LAYOUT_HEADER)
1562                 ui_litem_layout_row(litem);
1563         else
1564                 ui_litem_layout_column(litem);
1565 }
1566
1567 /* box layout */
1568 static void ui_litem_estimate_box(uiLayout *litem)
1569 {
1570         uiStyle *style= litem->root->style;
1571
1572         ui_litem_estimate_column(litem);
1573         litem->w += 2*style->boxspace;
1574         litem->h += style->boxspace;
1575 }
1576
1577 static void ui_litem_layout_box(uiLayout *litem)
1578 {
1579         uiLayoutItemBx *box= (uiLayoutItemBx*)litem;
1580         uiStyle *style= litem->root->style;
1581         uiBut *but;
1582         int w, h;
1583
1584         w= litem->w;
1585         h= litem->h;
1586
1587         litem->x += style->boxspace;
1588
1589         if(w != 0) litem->w -= 2*style->boxspace;
1590         if(h != 0) litem->h -= 2*style->boxspace;
1591
1592         ui_litem_layout_column(litem);
1593
1594         litem->x -= style->boxspace;
1595         litem->y -= style->boxspace;
1596
1597         if(w != 0) litem->w += 2*style->boxspace;
1598         if(h != 0) litem->h += style->boxspace;
1599
1600         /* roundbox around the sublayout */
1601         but= box->roundbox;
1602         but->x1= litem->x;
1603         but->y1= litem->y;
1604         but->x2= litem->x+litem->w;
1605         but->y2= litem->y+litem->h;
1606 }
1607
1608 /* multi-column layout, automatically flowing to the next */
1609 static void ui_litem_estimate_column_flow(uiLayout *litem)
1610 {
1611         uiStyle *style= litem->root->style;
1612         uiLayoutItemFlow *flow= (uiLayoutItemFlow*)litem;
1613         uiItem *item;
1614         int col, x, y, emh, emy, miny, itemw, itemh, maxw=0;
1615         int toth, totitem;
1616
1617         /* compute max needed width and total height */
1618         toth= 0;
1619         totitem= 0;
1620         for(item=litem->items.first; item; item=item->next) {
1621                 ui_item_size(item, &itemw, &itemh);
1622                 maxw= MAX2(maxw, itemw);
1623                 toth += itemh;
1624                 totitem++;
1625         }
1626
1627         if(flow->number <= 0) {
1628                 /* auto compute number of columns, not very good */
1629                 if(maxw == 0) {
1630                         flow->totcol= 1;
1631                         return;
1632                 }
1633
1634                 flow->totcol= MAX2(litem->root->emw/maxw, 1);
1635                 flow->totcol= MIN2(flow->totcol, totitem);
1636         }
1637         else
1638                 flow->totcol= flow->number;
1639
1640         /* compute sizes */
1641         x= 0;
1642         y= 0;
1643         emy= 0;
1644         miny= 0;
1645
1646         maxw= 0;
1647         emh= toth/flow->totcol;
1648
1649         /* create column per column */
1650         col= 0;
1651         for(item=litem->items.first; item; item=item->next) {
1652                 ui_item_size(item, &itemw, &itemh);
1653
1654                 y -= itemh + style->buttonspacey;
1655                 miny= MIN2(miny, y);
1656                 emy -= itemh;
1657                 maxw= MAX2(itemw, maxw);
1658
1659                 /* decide to go to next one */
1660                 if(col < flow->totcol-1 && emy <= -emh) {
1661                         x += maxw + litem->space;
1662                         maxw= 0;
1663                         y= 0;
1664                         col++;
1665                 }
1666         }
1667
1668         litem->w= x;
1669         litem->h= litem->y - miny;
1670 }
1671
1672 static void ui_litem_layout_column_flow(uiLayout *litem)
1673 {
1674         uiStyle *style= litem->root->style;
1675         uiLayoutItemFlow *flow= (uiLayoutItemFlow*)litem;
1676         uiItem *item;
1677         int col, x, y, w, emh, emy, miny, itemw, itemh;
1678         int toth, totitem, offset;
1679
1680         /* compute max needed width and total height */
1681         toth= 0;
1682         totitem= 0;
1683         for(item=litem->items.first; item; item=item->next) {
1684                 ui_item_size(item, &itemw, &itemh);
1685                 toth += itemh;
1686                 totitem++;
1687         }
1688
1689         /* compute sizes */
1690         x= litem->x;
1691         y= litem->y;
1692         emy= 0;
1693         miny= 0;
1694
1695         w= litem->w - (flow->totcol-1)*style->columnspace;
1696         emh= toth/flow->totcol;
1697
1698         /* create column per column */
1699         col= 0;
1700         for(item=litem->items.first; item; item=item->next) {
1701                 ui_item_size(item, NULL, &itemh);
1702                 itemw= ui_item_fit(1, x-litem->x, flow->totcol, w, col == flow->totcol-1, litem->alignment, &offset);
1703         
1704                 y -= itemh;
1705                 emy -= itemh;
1706                 ui_item_position(item, x+offset, y, itemw, itemh);
1707                 y -= style->buttonspacey;
1708                 miny= MIN2(miny, y);
1709
1710                 /* decide to go to next one */
1711                 if(col < flow->totcol-1 && emy <= -emh) {
1712                         x += itemw + style->columnspace;
1713                         y= litem->y;
1714                         col++;
1715                 }
1716         }
1717
1718         litem->h= litem->y - miny;
1719         litem->x= x;
1720         litem->y= miny;
1721 }
1722
1723 /* free layout */
1724 static void ui_litem_estimate_free(uiLayout *litem)
1725 {
1726         uiItem *item;
1727         int itemx, itemy, itemw, itemh, minx, miny;
1728
1729         minx= 1e6;
1730         miny= 1e6;
1731         litem->w= 0;
1732         litem->h= 0;
1733
1734         for(item=litem->items.first; item; item=item->next) {
1735                 ui_item_offset(item, &itemx, &itemy);
1736                 ui_item_size(item, &itemw, &itemh);
1737
1738                 minx= MIN2(minx, itemx);
1739                 miny= MIN2(miny, itemy);
1740
1741                 litem->w= MAX2(litem->w, itemx+itemw);
1742                 litem->h= MAX2(litem->h, itemy+itemh);
1743         }
1744
1745         litem->w -= minx;
1746         litem->h -= miny;
1747 }
1748
1749 static void ui_litem_layout_free(uiLayout *litem)
1750 {
1751         uiItem *item;
1752         float scalex=1.0f, scaley=1.0f;
1753         int x, y, newx, newy, itemx, itemy, itemh, itemw, minx, miny, totw, toth;
1754
1755         minx= 1e6;
1756         miny= 1e6;
1757         totw= 0;
1758         toth= 0;
1759
1760         for(item=litem->items.first; item; item=item->next) {
1761                 ui_item_offset(item, &itemx, &itemy);
1762                 ui_item_size(item, &itemw, &itemh);
1763
1764                 minx= MIN2(minx, itemx);
1765                 miny= MIN2(miny, itemy);
1766
1767                 totw= MAX2(totw, itemx+itemw);
1768                 toth= MAX2(toth, itemy+itemh);
1769         }
1770
1771         totw -= minx;
1772         toth -= miny;
1773
1774         if(litem->w && totw > 0)
1775                 scalex= (float)litem->w/(float)totw;
1776         if(litem->h && toth > 0)
1777                 scaley= (float)litem->h/(float)toth;
1778         
1779         x= litem->x;
1780         y= litem->y - scaley*toth;
1781
1782         for(item=litem->items.first; item; item=item->next) {
1783                 ui_item_offset(item, &itemx, &itemy);
1784                 ui_item_size(item, &itemw, &itemh);
1785
1786                 if(scalex != 1.0f) {
1787                         newx= (itemx - minx)*scalex;
1788                         itemw= (itemx - minx + itemw)*scalex - newx;
1789                         itemx= minx + newx;
1790                 }
1791
1792                 if(scaley != 1.0f) {
1793                         newy= (itemy - miny)*scaley;
1794                         itemh= (itemy - miny + itemh)*scaley - newy;
1795                         itemy= miny + newy;
1796                 }
1797
1798                 ui_item_position(item, x+itemx-minx, y+itemy-miny, itemw, itemh);
1799         }
1800
1801         litem->w= scalex*totw;
1802         litem->h= litem->y - y;
1803         litem->x= x + litem->w;
1804         litem->y= y;
1805 }
1806
1807 /* split layout */
1808 static void ui_litem_estimate_split(uiLayout *litem)
1809 {
1810         ui_litem_estimate_row(litem);
1811 }
1812
1813 static void ui_litem_layout_split(uiLayout *litem)
1814 {
1815         uiLayoutItemSplt *split= (uiLayoutItemSplt*)litem;
1816         uiItem *item;
1817         float percentage;
1818         int itemh, x, y, w, tot=0, colw=0;
1819
1820         x= litem->x;
1821         y= litem->y;
1822
1823         for(item=litem->items.first; item; item=item->next)
1824                 tot++;
1825         
1826         if(tot == 0)
1827                 return;
1828         
1829         percentage= (split->percentage == 0.0f)? 1.0f/(float)tot: split->percentage;
1830         
1831         w= (litem->w - (tot-1)*litem->space);
1832         colw= w*percentage;
1833         colw= MAX2(colw, 0);
1834
1835         for(item=litem->items.first; item; item=item->next) {
1836                 ui_item_size(item, NULL, &itemh);
1837
1838                 ui_item_position(item, x, y-itemh, colw, itemh);
1839                 x += colw;
1840
1841                 if(item->next) {
1842                         colw= (w - (int)(w*percentage))/(tot-1);
1843                         colw= MAX2(colw, 0);
1844
1845                         x += litem->space;
1846                 }
1847         }
1848
1849         litem->w= x - litem->x;
1850         litem->h= litem->y - y;
1851         litem->x= x;
1852         litem->y= y;
1853 }
1854
1855 /* layout create functions */
1856 uiLayout *uiLayoutRow(uiLayout *layout, int align)
1857 {
1858         uiLayout *litem;
1859
1860         litem= MEM_callocN(sizeof(uiLayout), "uiLayoutRow");
1861         litem->item.type= ITEM_LAYOUT_ROW;
1862         litem->root= layout->root;
1863         litem->align= align;
1864         litem->active= 1;
1865         litem->enabled= 1;
1866         litem->context= layout->context;
1867         litem->space= (align)? 0: layout->root->style->buttonspacex;
1868         BLI_addtail(&layout->items, litem);
1869
1870         uiBlockSetCurLayout(layout->root->block, litem);
1871
1872         return litem;
1873 }
1874
1875 uiLayout *uiLayoutColumn(uiLayout *layout, int align)
1876 {
1877         uiLayout *litem;
1878
1879         litem= MEM_callocN(sizeof(uiLayout), "uiLayoutColumn");
1880         litem->item.type= ITEM_LAYOUT_COLUMN;
1881         litem->root= layout->root;
1882         litem->align= align;
1883         litem->active= 1;
1884         litem->enabled= 1;
1885         litem->context= layout->context;
1886         litem->space= (litem->align)? 0: layout->root->style->buttonspacey;
1887         BLI_addtail(&layout->items, litem);
1888
1889         uiBlockSetCurLayout(layout->root->block, litem);
1890
1891         return litem;
1892 }
1893
1894 uiLayout *uiLayoutColumnFlow(uiLayout *layout, int number, int align)
1895 {
1896         uiLayoutItemFlow *flow;
1897
1898         flow= MEM_callocN(sizeof(uiLayoutItemFlow), "uiLayoutItemFlow");
1899         flow->litem.item.type= ITEM_LAYOUT_COLUMN_FLOW;
1900         flow->litem.root= layout->root;
1901         flow->litem.align= align;
1902         flow->litem.active= 1;
1903         flow->litem.enabled= 1;
1904         flow->litem.context= layout->context;
1905         flow->litem.space= (flow->litem.align)? 0: layout->root->style->columnspace;
1906         flow->number= number;
1907         BLI_addtail(&layout->items, flow);
1908
1909         uiBlockSetCurLayout(layout->root->block, &flow->litem);
1910
1911         return &flow->litem;
1912 }
1913
1914 static uiLayout *ui_layout_box(uiLayout *layout, int type)
1915 {
1916         uiLayoutItemBx *box;
1917
1918         box= MEM_callocN(sizeof(uiLayoutItemBx), "uiLayoutItemBx");
1919         box->litem.item.type= ITEM_LAYOUT_BOX;
1920         box->litem.root= layout->root;
1921         box->litem.active= 1;
1922         box->litem.enabled= 1;
1923         box->litem.context= layout->context;
1924         box->litem.space= layout->root->style->columnspace;
1925         BLI_addtail(&layout->items, box);
1926
1927         uiBlockSetCurLayout(layout->root->block, &box->litem);
1928
1929         box->roundbox= uiDefBut(layout->root->block, type, 0, "", 0, 0, 0, 0, NULL, 0.0, 0.0, 0, 0, "");
1930
1931         return &box->litem;
1932 }
1933
1934 uiLayout *uiLayoutBox(uiLayout *layout)
1935 {
1936         return ui_layout_box(layout, ROUNDBOX);
1937 }
1938
1939 uiLayout *uiLayoutListBox(uiLayout *layout)
1940 {
1941         return ui_layout_box(layout, LISTBOX);
1942 }
1943
1944 ListBase *uiLayoutBoxGetList(uiLayout *layout)
1945 {
1946         uiLayoutItemBx *box= (uiLayoutItemBx*)layout;
1947         return &box->items;
1948 }
1949
1950 uiLayout *uiLayoutFree(uiLayout *layout, int align)
1951 {
1952         uiLayout *litem;
1953
1954         litem= MEM_callocN(sizeof(uiLayout), "uiLayoutFree");
1955         litem->item.type= ITEM_LAYOUT_FREE;
1956         litem->root= layout->root;
1957         litem->align= align;
1958         litem->active= 1;
1959         litem->enabled= 1;
1960         litem->context= layout->context;
1961         BLI_addtail(&layout->items, litem);
1962
1963         uiBlockSetCurLayout(layout->root->block, litem);
1964
1965         return litem;
1966 }
1967
1968 uiBlock *uiLayoutFreeBlock(uiLayout *layout)
1969 {
1970         uiBlock *block;
1971
1972         block= uiLayoutGetBlock(layout);
1973         uiLayoutFree(layout, 0);
1974
1975         return block;
1976 }
1977
1978 uiLayout *uiLayoutSplit(uiLayout *layout, float percentage)
1979 {
1980         uiLayoutItemSplt *split;
1981
1982         split= MEM_callocN(sizeof(uiLayoutItemSplt), "uiLayoutItemSplt");
1983         split->litem.item.type= ITEM_LAYOUT_SPLIT;
1984         split->litem.root= layout->root;
1985         split->litem.active= 1;
1986         split->litem.enabled= 1;
1987         split->litem.context= layout->context;
1988         split->litem.space= layout->root->style->columnspace;
1989         split->percentage= percentage;
1990         BLI_addtail(&layout->items, split);
1991
1992         uiBlockSetCurLayout(layout->root->block, &split->litem);
1993
1994         return &split->litem;
1995 }
1996
1997 void uiLayoutSetActive(uiLayout *layout, int active)
1998 {
1999         layout->active= active;
2000 }
2001
2002 void uiLayoutSetEnabled(uiLayout *layout, int enabled)
2003 {
2004         layout->enabled= enabled;
2005 }
2006
2007 void uiLayoutSetRedAlert(uiLayout *layout, int redalert)
2008 {
2009         layout->redalert= redalert;
2010 }
2011
2012 void uiLayoutSetKeepAspect(uiLayout *layout, int keepaspect)
2013 {
2014         layout->keepaspect= keepaspect;
2015 }
2016
2017 void uiLayoutSetAlignment(uiLayout *layout, int alignment)
2018 {
2019         layout->alignment= alignment;
2020 }
2021
2022 void uiLayoutSetScaleX(uiLayout *layout, float scale)
2023 {
2024         layout->scale[0]= scale;
2025 }
2026
2027 void uiLayoutSetScaleY(uiLayout *layout, float scale)
2028 {
2029         layout->scale[1]= scale;
2030 }
2031
2032 int uiLayoutGetActive(uiLayout *layout)
2033 {
2034         return layout->active;
2035 }
2036
2037 int uiLayoutGetEnabled(uiLayout *layout)
2038 {
2039         return layout->enabled;
2040 }
2041
2042 int uiLayoutGetRedAlert(uiLayout *layout)
2043 {
2044         return layout->redalert;
2045 }
2046
2047 int uiLayoutGetKeepAspect(uiLayout *layout)
2048 {
2049         return layout->keepaspect;
2050 }
2051
2052 int uiLayoutGetAlignment(uiLayout *layout)
2053 {
2054         return layout->alignment;
2055 }
2056
2057 int uiLayoutGetWidth(uiLayout *layout)
2058 {
2059         return layout->w;
2060 }
2061
2062 float uiLayoutGetScaleX(uiLayout *layout)
2063 {
2064         return layout->scale[0];
2065 }
2066
2067 float uiLayoutGetScaleY(uiLayout *layout)
2068 {
2069         return layout->scale[0];
2070 }
2071
2072 /********************** Layout *******************/
2073
2074 static void ui_item_scale(uiLayout *litem, float scale[2])
2075 {
2076         uiItem *item;
2077         int x, y, w, h;
2078
2079         for(item=litem->items.last; item; item=item->prev) {
2080                 ui_item_size(item, &w, &h);
2081                 ui_item_offset(item, &x, &y);
2082
2083                 if(scale[0] != 0.0f) {
2084                         x *= scale[0];
2085                         w *= scale[0];
2086                 }
2087
2088                 if(scale[1] != 0.0f) {
2089                         y *= scale[1];
2090                         h *= scale[1];
2091                 }
2092
2093                 ui_item_position(item, x, y, w, h);
2094         }
2095 }
2096
2097 static void ui_item_estimate(uiItem *item)
2098 {
2099         uiItem *subitem;
2100
2101         if(item->type != ITEM_BUTTON) {
2102                 uiLayout *litem= (uiLayout*)item;
2103
2104                 for(subitem=litem->items.first; subitem; subitem=subitem->next)
2105                         ui_item_estimate(subitem);
2106
2107                 if(litem->items.first == NULL)
2108                         return;
2109
2110                 if(litem->scale[0] != 0.0f || litem->scale[1] != 0.0f)
2111                         ui_item_scale(litem, litem->scale);
2112
2113                 switch(litem->item.type) {
2114                         case ITEM_LAYOUT_COLUMN:
2115                                 ui_litem_estimate_column(litem);
2116                                 break;
2117                         case ITEM_LAYOUT_COLUMN_FLOW:
2118                                 ui_litem_estimate_column_flow(litem);
2119                                 break;
2120                         case ITEM_LAYOUT_ROW:
2121                                 ui_litem_estimate_row(litem);
2122                                 break;
2123                         case ITEM_LAYOUT_BOX:
2124                                 ui_litem_estimate_box(litem);
2125                                 break;
2126                         case ITEM_LAYOUT_ROOT:
2127                                 ui_litem_estimate_root(litem);
2128                                 break;
2129                         case ITEM_LAYOUT_FREE:
2130                                 ui_litem_estimate_free(litem);
2131                                 break;
2132                         case ITEM_LAYOUT_SPLIT:
2133                                 ui_litem_estimate_split(litem);
2134                                 break;
2135                         default:
2136                                 break;
2137                 }
2138         }
2139 }
2140
2141 static void ui_item_align(uiLayout *litem, int nr)
2142 {
2143         uiItem *item;
2144         uiButtonItem *bitem;
2145         uiLayoutItemBx *box;
2146
2147         for(item=litem->items.last; item; item=item->prev) {
2148                 if(item->type == ITEM_BUTTON) {
2149                         bitem= (uiButtonItem*)item;
2150                         if(ui_but_can_align(bitem->but))
2151                                 if(!bitem->but->alignnr)
2152                                         bitem->but->alignnr= nr;
2153                 }
2154                 else if(item->type == ITEM_LAYOUT_FREE);
2155                 else if(item->type == ITEM_LAYOUT_BOX) {
2156                         box= (uiLayoutItemBx*)item;
2157                         box->roundbox->alignnr= nr;
2158                         BLI_remlink(&litem->root->block->buttons, box->roundbox);
2159                         BLI_addhead(&litem->root->block->buttons, box->roundbox);
2160                 }
2161                 else
2162                         ui_item_align((uiLayout*)item, nr);
2163         }
2164 }
2165
2166 static void ui_item_flag(uiLayout *litem, int flag)
2167 {
2168         uiItem *item;
2169         uiButtonItem *bitem;
2170
2171         for(item=litem->items.last; item; item=item->prev) {
2172                 if(item->type == ITEM_BUTTON) {
2173                         bitem= (uiButtonItem*)item;
2174                         bitem->but->flag |= flag;
2175                 }
2176                 else
2177                         ui_item_flag((uiLayout*)item, flag);
2178         }
2179 }
2180
2181 static void ui_item_layout(uiItem *item)
2182 {
2183         uiItem *subitem;
2184
2185         if(item->type != ITEM_BUTTON) {
2186                 uiLayout *litem= (uiLayout*)item;
2187
2188                 if(litem->items.first == NULL)
2189                         return;
2190
2191                 if(litem->align)
2192                         ui_item_align(litem, ++litem->root->block->alignnr);
2193                 if(!litem->active)
2194                         ui_item_flag(litem, UI_BUT_INACTIVE);
2195                 if(!litem->enabled)
2196                         ui_item_flag(litem, UI_BUT_DISABLED);
2197
2198                 switch(litem->item.type) {
2199                         case ITEM_LAYOUT_COLUMN:
2200                                 ui_litem_layout_column(litem);
2201                                 break;
2202                         case ITEM_LAYOUT_COLUMN_FLOW:
2203                                 ui_litem_layout_column_flow(litem);
2204                                 break;
2205                         case ITEM_LAYOUT_ROW:
2206                                 ui_litem_layout_row(litem);
2207                                 break;
2208                         case ITEM_LAYOUT_BOX:
2209                                 ui_litem_layout_box(litem);
2210                                 break;
2211                         case ITEM_LAYOUT_ROOT:
2212                                 ui_litem_layout_root(litem);
2213                                 break;
2214                         case ITEM_LAYOUT_FREE:
2215                                 ui_litem_layout_free(litem);
2216                                 break;
2217                         case ITEM_LAYOUT_SPLIT:
2218                                 ui_litem_layout_split(litem);
2219                                 break;
2220                         default:
2221                                 break;
2222                 }
2223
2224                 for(subitem=litem->items.first; subitem; subitem=subitem->next)
2225                         ui_item_layout(subitem);
2226         }
2227 }
2228
2229 static void ui_layout_items(const bContext *C, uiBlock *block, uiLayout *layout)
2230 {
2231         ui_item_estimate(&layout->item);
2232         ui_item_layout(&layout->item);
2233 }
2234
2235 static void ui_layout_end(const bContext *C, uiBlock *block, uiLayout *layout, int *x, int *y)
2236 {
2237         if(layout->root->handlefunc)
2238                 uiBlockSetButmFunc(block, layout->root->handlefunc, layout->root->argv);
2239
2240         ui_layout_items(C, block, layout);
2241
2242         if(x) *x= layout->x;
2243         if(y) *y= layout->y;
2244 }
2245
2246 static void ui_layout_free(uiLayout *layout)
2247 {
2248         uiItem *item, *next;
2249
2250         for(item=layout->items.first; item; item=next) {
2251                 next= item->next;
2252
2253                 if(item->type == ITEM_BUTTON)
2254                         MEM_freeN(item);
2255                 else
2256                         ui_layout_free((uiLayout*)item);
2257         }
2258
2259         if(layout->item.type == ITEM_LAYOUT_BOX)
2260                 BLI_freelistN(&((uiLayoutItemBx*)layout)->items);
2261
2262         MEM_freeN(layout);
2263 }
2264
2265 uiLayout *uiBlockLayout(uiBlock *block, int dir, int type, int x, int y, int size, int em, uiStyle *style)
2266 {
2267         uiLayout *layout;
2268         uiLayoutRoot *root;
2269
2270         root= MEM_callocN(sizeof(uiLayoutRoot), "uiLayoutRoot");
2271         root->type= type;
2272         root->style= style;
2273         root->block= block;
2274         root->opcontext= WM_OP_INVOKE_REGION_WIN;
2275
2276         layout= MEM_callocN(sizeof(uiLayout), "uiLayout");
2277         layout->item.type= ITEM_LAYOUT_ROOT;
2278
2279         layout->x= x;
2280         layout->y= y;
2281         layout->root= root;
2282         layout->space= style->templatespace;
2283         layout->active= 1;
2284         layout->enabled= 1;
2285         layout->context= NULL;
2286
2287         if(type == UI_LAYOUT_MENU)
2288                 layout->space= 0;
2289
2290         if(dir == UI_LAYOUT_HORIZONTAL) {
2291                 layout->h= size;
2292                 layout->root->emh= em*UI_UNIT_Y;
2293         }
2294         else {
2295                 layout->w= size;
2296                 layout->root->emw= em*UI_UNIT_X;
2297         }
2298
2299         block->curlayout= layout;
2300         root->layout= layout;
2301         BLI_addtail(&block->layouts, root);
2302         
2303         return layout;
2304 }
2305
2306 uiBlock *uiLayoutGetBlock(uiLayout *layout)
2307 {
2308         return layout->root->block;
2309 }
2310
2311 int uiLayoutGetOperatorContext(uiLayout *layout)
2312 {
2313         return layout->root->opcontext;
2314 }
2315
2316
2317 void uiBlockSetCurLayout(uiBlock *block, uiLayout *layout)
2318 {
2319         block->curlayout= layout;
2320 }
2321
2322 void ui_layout_add_but(uiLayout *layout, uiBut *but)
2323 {
2324         uiButtonItem *bitem;
2325         
2326         bitem= MEM_callocN(sizeof(uiButtonItem), "uiButtonItem");
2327         bitem->item.type= ITEM_BUTTON;
2328         bitem->but= but;
2329         BLI_addtail(&layout->items, bitem);
2330
2331         if(layout->context) {
2332                 but->context= layout->context;
2333                 but->context->used= 1;
2334         }
2335 }
2336
2337 void uiLayoutSetOperatorContext(uiLayout *layout, int opcontext)
2338 {
2339         layout->root->opcontext= opcontext;
2340 }
2341
2342 void uiLayoutSetFunc(uiLayout *layout, uiMenuHandleFunc handlefunc, void *argv)
2343 {
2344         layout->root->handlefunc= handlefunc;
2345         layout->root->argv= argv;
2346 }
2347
2348 void uiBlockLayoutResolve(const bContext *C, uiBlock *block, int *x, int *y)
2349 {
2350         uiLayoutRoot *root;
2351
2352         if(x) *x= 0;
2353         if(y) *y= 0;
2354
2355         block->curlayout= NULL;
2356
2357         for(root=block->layouts.first; root; root=root->next) {
2358                 /* NULL in advance so we don't interfere when adding button */
2359                 ui_layout_end(C, block, root->layout, x, y);
2360                 ui_layout_free(root->layout);
2361         }
2362
2363         BLI_freelistN(&block->layouts);
2364
2365         /* XXX silly trick, interface_templates.c doesn't get linked
2366          * because it's not used by other files in this module? */
2367         {
2368                 void ui_template_fix_linking();
2369                 ui_template_fix_linking();
2370         }
2371 }
2372
2373 void uiLayoutSetContextPointer(uiLayout *layout, char *name, PointerRNA *ptr)
2374 {
2375         uiBlock *block= layout->root->block;
2376         layout->context= CTX_store_add(&block->contexts, name, ptr);
2377 }
2378