Welcome to mirror list, hosted at ThFree Co, Russian Federation.

interface_layout.c « interface « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2bab0284a270f083a0b6f559c7e755574ac5d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "DNA_ID.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_windowmanager_types.h"

#include "BLI_listbase.h"

#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_idprop.h"
#include "BKE_screen.h"
#include "BKE_utildefines.h"

#include "RNA_access.h"

#include "UI_interface.h"
#include "UI_resources.h"
#include "UI_view2d.h"

#include "BIF_gl.h"

#include "ED_util.h"
#include "ED_types.h"
#include "ED_screen.h"

#include "WM_api.h"
#include "WM_types.h"

#include "interface_intern.h"

/************************ Structs and Defines *************************/

#define COLUMN_SPACE	5
#define TEMPLATE_SPACE	5
#define STACK_SPACE		5
#define BUTTON_SPACE_X	5
#define BUTTON_SPACE_Y	2

#define RNA_NO_INDEX	-1

/* Item */

typedef enum uiItemType {
	ITEM_OPERATOR,
	ITEM_RNA_PROPERTY,
	ITEM_MENU,
	ITEM_LABEL
} uiItemType;

enum uiItemFlag {
	ITEM_ICON,
	ITEM_TEXT
};

typedef struct uiItem {
	struct uiItem *next, *prev;
	uiItemType type;
	int slot;

	const char *name;
	int icon;
} uiItem;

typedef struct uiItemRNA {
	uiItem item;

	PointerRNA ptr;
	PropertyRNA *prop;
	int index;
} uiItemRNA;

typedef struct uiItemOp {
	uiItem item;

	wmOperatorType *ot;
	IDProperty *properties;
	int context;
} uiItemOp;

typedef struct uiItemLMenu {
	uiItem item;

	uiMenuCreateFunc func;
} uiItemLMenu;

/* Template */

typedef enum uiTemplateType {
	TEMPLATE_COLUMN,
	TEMPLATE_LR,
	TEMPLATE_STACK,

	TEMPLATE_HEADER_MENUS,
	TEMPLATE_HEADER_BUTTONS,
	TEMPLATE_HEADER_ID
} uiTemplateType;

typedef struct uiTemplate {
	struct uiTemplate *next, *prev;
	uiTemplateType type;

	ListBase items;
	int color;
} uiTemplate;

typedef struct uiTemplateStck {
	uiTemplate template;
	uiLayout *sublayout;
} uiTemplateStck;

typedef struct uiTemplateHeadID {
	uiTemplate template;

	PointerRNA ptr;
	char *propname;
	int flag;
	uiIDPoinFunc func;
} uiTemplateHeadID;

/* Layout */

struct uiLayout {
	ListBase templates;
	int opcontext;
	int dir;
	int x, y, w, h;
};

void ui_layout_free(uiLayout *layout);
void ui_layout_end(const bContext *C, uiBlock *block, uiLayout *layout, int *x, int *y);

/************************** Item ***************************/

static int ui_item_fit(int item, int all, int available)
{
	if(all > available)
		return (item*available)/all;
	
	return all;
}

/* create buttons for an item with an RNA array */
static void ui_item_array(uiBlock *block, uiItemRNA *rnaitem, int len, int x, int y, int w, int h)
{
	PropertyType type;
	PropertySubType subtype;
	char *name;
	int a;

	/* retrieve type and subtype */
	type= RNA_property_type(&rnaitem->ptr, rnaitem->prop);
	subtype= RNA_property_subtype(&rnaitem->ptr, rnaitem->prop);

	/* create label */
	if(rnaitem->item.name)
		name= (char*)rnaitem->item.name;
	else
		name= (char*)RNA_property_ui_name(&rnaitem->ptr, rnaitem->prop);

	if(strcmp(name, "") != 0)
		uiDefBut(block, LABEL, 0, name, x, y + h - YIC, w, YIC, NULL, 0.0, 0.0, 0, 0, "");

	/* create buttons */
	uiBlockBeginAlign(block);

	if(type == PROP_BOOLEAN && len == 20) {
		/* special check for layer layout */
		int butw, buth;

		butw= ui_item_fit(XIC, XIC*10 + BUTTON_SPACE_X, w);
		buth= MIN2(YIC, butw);

		y += 2*(YIC - buth);

		uiBlockBeginAlign(block);
		for(a=0; a<5; a++)
			uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, a, "", ICON_BLANK1, x + butw*a, y+buth, butw, buth);
		for(a=0; a<5; a++)
			uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, a+10, "", ICON_BLANK1, x + butw*a, y, butw, buth);
		uiBlockEndAlign(block);

		x += 5*butw + BUTTON_SPACE_X;

		uiBlockBeginAlign(block);
		for(a=0; a<5; a++)
			uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, a+5, "", ICON_BLANK1, x + butw*a, y+buth, butw, buth);
		for(a=0; a<5; a++)
			uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, a+15, "", ICON_BLANK1, x + butw*a, y, butw, buth);
		uiBlockEndAlign(block);
	}
	else if(subtype == PROP_MATRIX) {
		/* matrix layout */
		int row, col;

		len= ceil(sqrt(len));

		h /= len;
		w /= len;

		// XXX test
		for(a=0; a<len; a++) {
			col= a%len;
			row= a/len;

			uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, a, "", 0, x + w*col, y+(row-a-1)*YIC, w, YIC);
		}
	}
	else if(len <= 4 && ELEM3(subtype, PROP_ROTATION, PROP_VECTOR, PROP_COLOR)) {
		/* layout for known array subtypes */
		static char vectoritem[4]= {'X', 'Y', 'Z', 'W'};
		static char quatitem[4]= {'W', 'X', 'Y', 'Z'};
		static char coloritem[4]= {'R', 'G', 'B', 'A'};
		char str[3];

		for(a=0; a<len; a++) {
			if(len == 4 && subtype == PROP_ROTATION)
				str[0]= quatitem[a];
			else if(subtype == PROP_VECTOR || subtype == PROP_ROTATION)
				str[0]= vectoritem[a];
			else
				str[0]= coloritem[a];

			if(type == PROP_BOOLEAN) {
				str[1]= '\0';
			}
			else {
				str[1]= ':';
				str[2]= '\0';
			}

			uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, a, str, 0, x, y+(len-a-1)*YIC, w, YIC);
		}
	}
	else {
		/* default array layout */
		for(a=0; a<len; a++)
			uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, a, "", 0, x, y+(len-a-1)*YIC, w, YIC);
	}

	uiBlockEndAlign(block);
}

/* create lable + button for RNA property */
static void ui_item_with_label(uiBlock *block, uiItemRNA *rnaitem, int x, int y, int w, int h)
{
	char *name;
	int butw;

	if(rnaitem->item.name)
		name= (char*)rnaitem->item.name;
	else
		name= (char*)RNA_property_ui_name(&rnaitem->ptr, rnaitem->prop);
	
	if(strcmp(name, "") != 0) {
		butw= GetButStringLength(name);
		uiDefBut(block, LABEL, 0, name, x, y, butw, h, NULL, 0.0, 0.0, 0, 0, "");

		x += butw;
		w -= butw;
	}

	uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, rnaitem->index, "", rnaitem->item.icon, x, y, w, h);
}

/* create buttons for an arbitrary item */
static void ui_item_buts(uiBlock *block, uiItem *item, int x, int y, int w, int h)
{
	if(item->type == ITEM_RNA_PROPERTY) {
		/* RNA property */
		uiItemRNA *rnaitem= (uiItemRNA*)item;
		PropertyType type;
		int len;
		
		/* retrieve info */
		type= RNA_property_type(&rnaitem->ptr, rnaitem->prop);
		len= RNA_property_array_length(&rnaitem->ptr, rnaitem->prop);

		/* array property */
		if(rnaitem->index == RNA_NO_INDEX && len > 0)
			ui_item_array(block, rnaitem, len, x, y, w, h);
		/* property with separate label */
		else if(type == PROP_ENUM || type == PROP_STRING || type == PROP_POINTER)
			ui_item_with_label(block, rnaitem, x, y, w, h);
		/* single button */
		else
			uiDefAutoButR(block, &rnaitem->ptr, rnaitem->prop, rnaitem->index, (char*)item->name, item->icon, x, y, w, h);
	}
	else if(item->type == ITEM_OPERATOR) {
		/* operator */
		uiItemOp *opitem= (uiItemOp*)item;
		uiBut *but;

		if(item->icon && item->name)
			but= uiDefIconTextButO(block, BUT, opitem->ot->idname, opitem->context, item->icon, (char*)item->name, x, y, w, h, NULL);
		else if(item->icon)
			but= uiDefIconButO(block, BUT, opitem->ot->idname, opitem->context, item->icon, x, y, w, h, NULL);
		/* text only */
		else
			but= uiDefButO(block, BUT, opitem->ot->idname, opitem->context, (char*)item->name, x, y, w, h, NULL);

		if(but && opitem->properties) {
			/* assign properties */
			PointerRNA *opptr= uiButGetOperatorPtrRNA(but);
			opptr->data= opitem->properties;
			opitem->properties= NULL;
		}
	}
	else if(item->type == ITEM_MENU) {
		/* menu */
		uiItemLMenu *menuitem= (uiItemLMenu*)item;

		uiDefMenuBut(block, menuitem->func, NULL, (char*)item->name, x, y-2, w-3, h+4, "");
	}
	else if(item->type == ITEM_LABEL) {
		/* label */

		if(item->icon && item->name)
			uiDefIconTextBut(block, LABEL, 0, item->icon, (char*)item->name, x, y, w, h, NULL, 0.0, 0.0, 0, 0, "");
		else if(item->icon)
			uiDefIconBut(block, LABEL, 0, item->icon, x, y, w, h, NULL, 0.0, 0.0, 0, 0, "");
		else if((char*)item->name)
			uiDefBut(block, LABEL, 0, (char*)item->name, x, y, w, h, NULL, 0.0, 0.0, 0, 0, "");
	}
	else {
		/* separator */
		uiDefBut(block, SEPR, 0, "", x, y, w, h, NULL, 0.0, 0.0, 0, 0, "");
	}
}

/* estimated size of text + icon */
static int ui_text_icon_width(const char *name, int icon)
{
	if(icon && name && strcmp(name, "") == 0)
		return XIC; /* icon only */
	else if(icon && name)
		return XIC + GetButStringLength((char*)name); /* icon + text */
	else if(name)
		return GetButStringLength((char*)name); /* text only */
	else
		return 0;
}

/* estimated size of an item */
static void ui_item_size(uiItem *item, int *r_w, int *r_h)
{
	const char *name;
	int w, h;

	if(item->type == ITEM_RNA_PROPERTY) {
		/* RNA property */
		uiItemRNA *rnaitem= (uiItemRNA*)item;
		PropertyType type;
		PropertySubType subtype;
		int len;

		name= item->name;
		if(!name)
			name= RNA_property_ui_name(&rnaitem->ptr, rnaitem->prop);

		w= ui_text_icon_width(name, item->icon);
		h= YIC;

		/* arbitrary extended width by type */
		type= RNA_property_type(&rnaitem->ptr, rnaitem->prop);
		subtype= RNA_property_subtype(&rnaitem->ptr, rnaitem->prop);
		len= RNA_property_array_length(&rnaitem->ptr, rnaitem->prop);

		if(type == PROP_BOOLEAN && !item->icon)
			w += XIC;
		else if(type == PROP_INT || type == PROP_FLOAT)
			w += 2*XIC;
		else if(type == PROP_STRING)
			w += 8*XIC;

		/* increase height for arrays */
		if(rnaitem->index == RNA_NO_INDEX && len > 0) {
			if(name && strcmp(name, "") == 0 && item->icon == 0)
				h= 0;

			if(type == PROP_BOOLEAN && len == 20)
				h += 2*YIC;
			else if(subtype == PROP_MATRIX)
				h += ceil(sqrt(len))*YIC;
			else
				h += len*YIC;
		}
	}
	else if(item->type == ITEM_OPERATOR) {
		/* operator */
		uiItemOp *opitem= (uiItemOp*)item;

		name= item->name;
		if(!name)
			name= opitem->ot->name;

		w= ui_text_icon_width(name, item->icon);
		h= YIC;
	}
	else {
		/* other */
		w= ui_text_icon_width(item->name, item->icon);
		h= YIC;
	}

	if(r_w) *r_w= w;
	if(r_h) *r_h= h;
}

static void ui_item_free(uiItem *item)
{
	if(item->type == ITEM_OPERATOR) {
		uiItemOp *opitem= (uiItemOp*)item;

		if(opitem->properties) {
			IDP_FreeProperty(opitem->properties);
			MEM_freeN(opitem->properties);
		}
	}
}

/* operator items */
void uiItemFullO(uiLayout *layout, int slot, const char *name, int icon, char *idname, IDProperty *properties, int context)
{
	uiTemplate *template= layout->templates.last;
	wmOperatorType *ot= WM_operatortype_find(idname);
	uiItemOp *opitem;

	if(!ot)
		return;

	opitem= MEM_callocN(sizeof(uiItemOp), "uiItemOp");

	opitem->item.name= name;
	opitem->item.icon= icon;
	opitem->item.type= ITEM_OPERATOR;
	opitem->item.slot= slot;

	opitem->ot= ot;
	opitem->properties= properties;
	opitem->context= context;

	BLI_addtail(&template->items, opitem);
}

void uiItemEnumO(uiLayout *layout, int slot, const char *name, int icon, char *opname, char *propname, int value)
{
	PointerRNA ptr;

	WM_operator_properties_create(&ptr, opname);
	RNA_enum_set(&ptr, propname, value);

	uiItemFullO(layout, slot, name, icon, opname, ptr.data, layout->opcontext);
}

void uiItemsEnumO(uiLayout *layout, int slot, char *opname, char *propname)
{
	wmOperatorType *ot= WM_operatortype_find(opname);
	PointerRNA ptr;
	PropertyRNA *prop;

	if(!ot || !ot->srna)
		return;

	RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
	prop= RNA_struct_find_property(&ptr, propname);

	if(prop && RNA_property_type(&ptr, prop) == PROP_ENUM) {
		const EnumPropertyItem *item;
		int totitem, i;

		RNA_property_enum_items(&ptr, prop, &item, &totitem);

		for(i=0; i<totitem; i++)
			uiItemEnumO(layout, slot, "", 0, opname, propname, item[i].value);
	}
}

void uiItemBooleanO(uiLayout *layout, int slot, const char *name, int icon, char *opname, char *propname, int value)
{
	PointerRNA ptr;

	WM_operator_properties_create(&ptr, opname);
	RNA_boolean_set(&ptr, propname, value);

	uiItemFullO(layout, slot, name, icon, opname, ptr.data, layout->opcontext);
}

void uiItemIntO(uiLayout *layout, int slot, const char *name, int icon, char *opname, char *propname, int value)
{
	PointerRNA ptr;

	WM_operator_properties_create(&ptr, opname);
	RNA_int_set(&ptr, propname, value);

	uiItemFullO(layout, slot, name, icon, opname, ptr.data, layout->opcontext);
}

void uiItemFloatO(uiLayout *layout, int slot, const char *name, int icon, char *opname, char *propname, float value)
{
	PointerRNA ptr;

	WM_operator_properties_create(&ptr, opname);
	RNA_float_set(&ptr, propname, value);

	uiItemFullO(layout, slot, name, icon, opname, ptr.data, layout->opcontext);
}

void uiItemStringO(uiLayout *layout, int slot, const char *name, int icon, char *opname, char *propname, char *value)
{
	PointerRNA ptr;

	WM_operator_properties_create(&ptr, opname);
	RNA_string_set(&ptr, propname, value);

	uiItemFullO(layout, slot, name, icon, opname, ptr.data, layout->opcontext);
}

void uiItemO(uiLayout *layout, int slot, const char *name, int icon, char *opname)
{
	uiItemFullO(layout, slot, name, icon, opname, NULL, layout->opcontext);
}

/* RNA property items */
void uiItemFullR(uiLayout *layout, int slot, const char *name, int icon, PointerRNA *ptr, char *propname, int index)
{
	uiTemplate *template= layout->templates.last;
	PropertyRNA *prop;
	uiItemRNA *rnaitem;
	
	prop= RNA_struct_find_property(ptr, propname);
	if(!prop){
		printf("Property not found : %s \n",propname);
		return;
	}
	
	rnaitem= MEM_callocN(sizeof(uiItemRNA), "uiItemRNA");

	rnaitem->item.name= name;
	rnaitem->item.icon= icon;
	rnaitem->item.type= ITEM_RNA_PROPERTY;
	rnaitem->item.slot= slot;

	rnaitem->ptr= *ptr;
	rnaitem->prop= prop;
	rnaitem->index= index;

	BLI_addtail(&template->items, rnaitem);
}

void uiItemR(uiLayout *layout, int slot, const char *name, int icon, PointerRNA *ptr, char *propname)
{
	uiItemFullR(layout, slot, name, icon, ptr, propname, RNA_NO_INDEX);
}

/* menu item */
void uiItemMenu(uiLayout *layout, int slot, const char *name, int icon, uiMenuCreateFunc func)
{
	uiTemplate *template= layout->templates.last;
	uiItemLMenu *menuitem= MEM_callocN(sizeof(uiItemLMenu), "uiItemLMenu");

	menuitem->item.name= name;
	menuitem->item.icon= icon;
	menuitem->item.type= ITEM_MENU;
	menuitem->item.slot= slot;

	menuitem->func= func;

	BLI_addtail(&template->items, menuitem);
}

/* label item */
void uiItemLabel(uiLayout *layout, int slot, const char *name, int icon)
{
	uiTemplate *template= layout->templates.last;
	uiItem *item= MEM_callocN(sizeof(uiItem), "uiItem");

	item->name= name;
	item->icon= icon;
	item->type= ITEM_LABEL;
	item->slot= slot;

	BLI_addtail(&template->items, item);
}

/**************************** Template ***************************/

/* multi-column layout */
static void ui_layout_column(uiLayout *layout, uiBlock *block, uiTemplate *template, int *x, int *y, int w, int h)
{
	uiItem *item;
	int col, totcol= 0, colx, coly, colw, miny, itemw, itemh;

	/* compute number of columns */
	for(item=template->items.first; item; item=item->next)
		totcol= MAX2(item->slot+1, totcol);
	
	if(totcol == 0)
		return;
	
	colx= *x;
	colw= (w - (totcol-1)*COLUMN_SPACE)/totcol;
	miny= *y;

	/* create column per column */
	for(col=0; col<totcol; col++) {
		coly= *y;

		for(item=template->items.first; item; item=item->next) {
			if(item->slot != col)
				continue;

			ui_item_size(item, &itemw, &itemh);

			coly -= itemh + BUTTON_SPACE_Y;
			ui_item_buts(block, item, colx, coly, colw, itemh);
		}

		colx += colw + COLUMN_SPACE;
		miny= MIN2(miny, coly);
	}

	*y= miny;
}

/* left-right layout, with buttons aligned on both sides */
static void ui_layout_lr(uiLayout *layout, uiBlock *block, uiTemplate *template, int *x, int *y, int w, int h)
{
	uiItem *item;
	int totw= 0, maxh= 0, itemw, itemh, leftx, rightx;

	/* estimate total width of buttons */
	for(item=template->items.first; item; item=item->next) {
		ui_item_size(item, &itemw, &itemh);
		totw += itemw;
		maxh= MAX2(maxh, itemh);
	}

	if(totw == 0)
		return;
	
	/* create buttons starting from left and right */
	leftx= *x;
	rightx= *x + w;

	for(item=template->items.first; item; item=item->next) {
		ui_item_size(item, &itemw, &itemh);
		itemw= ui_item_fit(itemw, totw+BUTTON_SPACE_X, w);

		if(item->slot == UI_TSLOT_LR_LEFT) {
			ui_item_buts(block, item, leftx, *y-itemh, itemw, itemh);
			leftx += itemw;
		}
		else {
			rightx -= itemw;
			ui_item_buts(block, item, rightx, *y-itemh, itemw, itemh);
		}
	}

	*y -= maxh;
}

/* element in a stack layout */
static void ui_layout_stack(const bContext *C, uiLayout *layout, uiBlock *block, uiTemplate *template, int *x, int *y, int w, int h)
{
	uiTemplateStck *stack= (uiTemplateStck*)template;
	int starty, startx;

	startx= *x;
	starty= *y;

	/* some extra padding */
	stack->sublayout->x= *x + STACK_SPACE;
	stack->sublayout->w= w - 2*STACK_SPACE;
	stack->sublayout->y= *y - STACK_SPACE;
	stack->sublayout->h= h;

	/* do layout for elements in sublayout */
	ui_layout_end(C, block, stack->sublayout, NULL, y);

	/* roundbox around the sublayout */
	uiDefBut(block, ROUNDBOX, 0, "", startx, *y, w, starty - *y, NULL, 7.0, 0.0, 3, 20, "");
}

static void ui_layout_header_buttons(uiLayout *layout, uiBlock *block, uiTemplate *template, int *x, int *y, int w, int h)
{
	uiItem *item;
	int itemw, itemh;
	
	uiBlockBeginAlign(block);

	for(item=template->items.first; item; item=item->next) {
		ui_item_size(item, &itemw, &itemh);
		ui_item_buts(block, item, *x, *y, itemw, itemh);
		*x += itemw;
	}

	uiBlockEndAlign(block);
}

static void ui_layout_header_menus(const bContext *C, uiLayout *layout, uiBlock *block, uiTemplate *template, int *x, int *y, int w, int h)
{
	ScrArea *sa= CTX_wm_area(C);

	*x= ED_area_header_standardbuttons(C, block, *y);

	if((sa->flag & HEADER_NO_PULLDOWN)==0) {
		uiBlockSetEmboss(block, UI_EMBOSSP);
		ui_layout_header_buttons(layout, block, template, x, y, w, h);
	}

	uiBlockSetEmboss(block, UI_EMBOSS);
}

static void ui_layout_header_id(const bContext *C, uiLayout *layout, uiBlock *block, uiTemplate *template, int *x, int *y, int w, int h)
{
	uiTemplateHeadID *idtemplate= (uiTemplateHeadID*)template;
	PointerRNA idptr;

	idptr= RNA_pointer_get(&idtemplate->ptr, idtemplate->propname);

	*x= uiDefIDPoinButs(block, CTX_data_main(C), NULL, (ID*)idptr.data, ID_TXT, NULL, *x, *y,
		idtemplate->func, UI_ID_BROWSE|UI_ID_RENAME|UI_ID_ADD_NEW|UI_ID_OPEN|UI_ID_DELETE);
}

void ui_template_free(uiTemplate *template)
{
	uiItem *item;

	if(template->type == TEMPLATE_STACK) {
		uiTemplateStck *stack= (uiTemplateStck*)template;
		ui_layout_free(stack->sublayout);
	}

	for(item=template->items.first; item; item=item->next)
		ui_item_free(item);

	BLI_freelistN(&template->items);
}

/* template create functions */
void uiTemplateColumn(uiLayout *layout)
{
	uiTemplate *template;

	template= MEM_callocN(sizeof(uiTemplate), "uiTemplate");
	template->type= TEMPLATE_COLUMN;

	BLI_addtail(&layout->templates, template);
}


void uiTemplateLeftRight(uiLayout *layout)
{
	uiTemplate *template;

	template= MEM_callocN(sizeof(uiTemplate), "uiTemplate");
	template->type= TEMPLATE_LR;

	BLI_addtail(&layout->templates, template);
}

uiLayout *uiTemplateStack(uiLayout *layout)
{
	uiTemplateStck *stack;

	stack= MEM_callocN(sizeof(uiTemplateStck), "uiTemplateStck");
	stack->template.type= TEMPLATE_STACK;
	stack->sublayout= uiLayoutBegin(layout->dir, 0, 0, 0, 0);
	BLI_addtail(&layout->templates, stack);

	return stack->sublayout;
}

void uiTemplateHeaderMenus(uiLayout *layout)
{
	uiTemplate *template;

	template= MEM_callocN(sizeof(uiTemplate), "uiTemplate");
	template->type= TEMPLATE_HEADER_MENUS;

	BLI_addtail(&layout->templates, template);
}

void uiTemplateHeaderButtons(uiLayout *layout)
{
	uiTemplate *template;

	template= MEM_callocN(sizeof(uiTemplate), "uiTemplate");
	template->type= TEMPLATE_HEADER_BUTTONS;

	BLI_addtail(&layout->templates, template);
}

void uiTemplateHeaderID(uiLayout *layout, PointerRNA *ptr, char *propname, int flag, uiIDPoinFunc func)
{
	uiTemplateHeadID *idtemplate;

	idtemplate= MEM_callocN(sizeof(uiTemplateHeadID), "uiTemplateHeadID");
	idtemplate->template.type= TEMPLATE_HEADER_ID;
	idtemplate->ptr= *ptr;
	idtemplate->propname= propname;
	idtemplate->flag= flag;
	idtemplate->func= func;

	BLI_addtail(&layout->templates, idtemplate);
}

void uiTemplateSetColor(uiLayout *layout, int color)
{
	uiTemplate *template= layout->templates.last;

	template->color= color;
}

/********************** Layout *******************/

static void ui_layout_templates(const bContext *C, uiBlock *block, uiLayout *layout)
{
	uiTemplate *template;
	int oldcolor= 0;

	for(template=layout->templates.first; template; template=template->next) {
		if(template->color) {
			oldcolor= uiBlockGetCol(block);
			uiBlockSetCol(block, template->color);
		}

		switch(template->type) {
			case TEMPLATE_COLUMN:
				ui_layout_column(layout, block, template, &layout->x, &layout->y, layout->w, layout->h);
				break;
			case TEMPLATE_LR:
				ui_layout_lr(layout, block, template, &layout->x, &layout->y, layout->w, layout->h);
				break;
			case TEMPLATE_STACK:
				ui_layout_stack(C, layout, block, template, &layout->x, &layout->y, layout->w, layout->h);
				break;
			case TEMPLATE_HEADER_MENUS:
				ui_layout_header_menus(C, layout, block, template, &layout->x, &layout->y, layout->w, layout->h);
				break;
			case TEMPLATE_HEADER_BUTTONS:
				ui_layout_header_buttons(layout, block, template, &layout->x, &layout->y, layout->w, layout->h);
				break;
			case TEMPLATE_HEADER_ID:
				ui_layout_header_id(C, layout, block, template, &layout->x, &layout->y, layout->w, layout->h);
				break;
		}

		if(template->color)
			uiBlockSetCol(block, oldcolor);

		if(layout->dir == UI_LAYOUT_HORIZONTAL)
			layout->x += TEMPLATE_SPACE;
		else
			layout->y -= TEMPLATE_SPACE;
	}
}

void ui_layout_end(const bContext *C, uiBlock *block, uiLayout *layout, int *x, int *y)
{
	ui_layout_templates(C, block, layout);

	if(x) *x= layout->x;
	if(y) *y= layout->y;
	
}

void ui_layout_free(uiLayout *layout)
{
	uiTemplate *template;

	for(template=layout->templates.first; template; template=template->next)
		ui_template_free(template);

	BLI_freelistN(&layout->templates);
	MEM_freeN(layout);
}

uiLayout *uiLayoutBegin(int dir, int x, int y, int w, int h)
{
	uiLayout *layout;

	layout= MEM_callocN(sizeof(uiLayout), "uiLayout");
	layout->opcontext= WM_OP_INVOKE_REGION_WIN;
	layout->dir= dir;
	layout->x= x;
	layout->y= y;
	layout->w= w;
	layout->h= h;

	return layout;
}

void uiLayoutContext(uiLayout *layout, int opcontext)
{
	layout->opcontext= opcontext;
}

void uiLayoutEnd(const bContext *C, uiBlock *block, uiLayout *layout, int *x, int *y)
{
	ui_layout_end(C, block, layout, x, y);
	ui_layout_free(layout);
}

/************************ Utilities ************************/

void uiRegionPanelLayout(const bContext *C, ARegion *ar, int vertical, char *context)
{
	uiBlock *block;
	PanelType *pt;
	Panel *panel;
	float col[3];
	int xco, yco, x=PNL_DIST, y=-PNL_HEADER-PNL_DIST, w;

	// XXX this only hides cruft

	/* clear */
	UI_GetThemeColor3fv(TH_HEADER, col);
	glClearColor(col[0], col[1], col[2], 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	
	/* set view2d view matrix for scrolling (without scrollers) */
	UI_view2d_view_ortho(C, &ar->v2d);
	
	uiBeginPanels(C, ar);

	for(pt= ar->type->paneltypes.first; pt; pt= pt->next) {
		if(context)
			if(!pt->context || strcmp(context, pt->context) != 0)
				continue;

		if(pt->draw && (!pt->poll || pt->poll(C))) {
			block= uiBeginBlock(C, ar, pt->idname, UI_EMBOSS, UI_HELV);
			
			if(vertical)
				w= (ar->type->minsizex)? ar->type->minsizex-12: ar->winx-12;
			else
				w= (ar->type->minsizex)? ar->type->minsizex-12: UI_PANEL_WIDTH-12;

			if(uiNewPanel(C, ar, block, pt->name, pt->name, x, y, w, 0)) {
				panel= uiPanelFromBlock(block);
				panel->type= pt;
				panel->layout= uiLayoutBegin(UI_LAYOUT_VERTICAL, x, y, w, 0);

				pt->draw(C, panel);

				uiLayoutEnd(C, block, panel->layout, &xco, &yco);
				panel->layout= NULL;
				uiNewPanelHeight(block, y - yco + 6);
			}
			else {
				w= PNL_HEADER;
				yco= PNL_HEADER;
			}

			uiEndBlock(C, block);

			if(vertical)
				y += yco+PNL_DIST;
			else
				x += w+PNL_DIST;
		}
	}

	uiEndPanels(C, ar);
	
	/* restore view matrix? */
	UI_view2d_view_restore(C);
}

void uiRegionHeaderLayout(const bContext *C, ARegion *ar)
{
	uiBlock *block;
	uiLayout *layout;
	HeaderType *ht;
	float col[3];
	int xco, yco;

	// XXX this only hides cruft
	
	/* clear */
	if(ED_screen_area_active(C))
		UI_GetThemeColor3fv(TH_HEADER, col);
	else
		UI_GetThemeColor3fv(TH_HEADERDESEL, col);
	
	glClearColor(col[0], col[1], col[2], 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	
	/* set view2d view matrix for scrolling (without scrollers) */
	UI_view2d_view_ortho(C, &ar->v2d);

	xco= 8;
	yco= 3;

	/* draw all headers types */
	for(ht= ar->type->headertypes.first; ht; ht= ht->next) {
		block= uiBeginBlock(C, ar, "header buttons", UI_EMBOSS, UI_HELV);
		layout= uiLayoutBegin(UI_LAYOUT_HORIZONTAL, xco, yco, 0, 24);

		if(ht->draw)
			ht->draw(C, layout);

		uiLayoutEnd(C, block, layout, &xco, &yco);
		uiEndBlock(C, block);
		uiDrawBlock(C, block);
	}

	/* always as last  */
	UI_view2d_totRect_set(&ar->v2d, xco+XIC+80, ar->v2d.tot.ymax-ar->v2d.tot.ymin);

	/* restore view matrix? */
	UI_view2d_view_restore(C);
}