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