37b2a4af84e219e2e76fc7a9eadb1247b3f040fa
[blender.git] / source / blender / editors / interface / interface_templates.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 <stdlib.h>
26 #include <string.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_screen_types.h"
31
32 #include "BLI_string.h"
33
34 #include "BKE_context.h"
35 #include "BKE_icons.h"
36 #include "BKE_library.h"
37 #include "BKE_utildefines.h"
38
39 #include "ED_screen.h"
40 #include "ED_previewrender.h"
41
42 #include "RNA_access.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "UI_interface.h"
48 #include "UI_resources.h"
49 #include "interface_intern.h"
50
51 void ui_template_fix_linking()
52 {
53 }
54
55 /********************** Header Template *************************/
56
57 void uiTemplateHeader(uiLayout *layout, bContext *C)
58 {
59         uiBlock *block;
60         
61         block= uiLayoutFreeBlock(layout);
62         ED_area_header_standardbuttons(C, block, 0);
63 }
64
65 /******************* Header ID Template ************************/
66
67 typedef struct TemplateID {
68         PointerRNA ptr;
69         PropertyRNA *prop;
70
71         int flag;
72         short browse;
73
74         char newop[256];
75         char openop[256];
76         char unlinkop[256];
77         
78         short idtype;
79 } TemplateID;
80
81 static void template_id_cb(bContext *C, void *arg_litem, void *arg_event)
82 {
83         TemplateID *template= (TemplateID*)arg_litem;
84         PointerRNA idptr= RNA_property_pointer_get(&template->ptr, template->prop);
85         ID *id= idptr.data;
86         int event= GET_INT_FROM_POINTER(arg_event);
87         
88         if(event == UI_ID_BROWSE && template->browse == 32767)
89                 event= UI_ID_ADD_NEW;
90         else if(event == UI_ID_BROWSE && template->browse == 32766)
91                 event= UI_ID_OPEN;
92
93         switch(event) {
94                 case UI_ID_BROWSE:
95                         printf("warning, id browse shouldnt come here\n");
96                         break;
97                 case UI_ID_DELETE:
98                         memset(&idptr, 0, sizeof(idptr));
99                         RNA_property_pointer_set(&template->ptr, template->prop, idptr);
100                         RNA_property_update(C, &template->ptr, template->prop);
101                         break;
102                 case UI_ID_FAKE_USER:
103                         if(id) {
104                                 if(id->flag & LIB_FAKEUSER) id->us++;
105                                 else id->us--;
106                         }
107                         else return;
108                         break;
109                 case UI_ID_PIN:
110                         break;
111                 case UI_ID_ADD_NEW:
112                         WM_operator_name_call(C, template->newop, WM_OP_INVOKE_REGION_WIN, NULL);
113                         break;
114                 case UI_ID_OPEN:
115                         WM_operator_name_call(C, template->openop, WM_OP_INVOKE_REGION_WIN, NULL);
116                         break;
117 #if 0
118                 case UI_ID_ALONE:
119                         if(!id || id->us < 1)
120                                 return;
121                         break;
122                 case UI_ID_LOCAL:
123                         if(!id || id->us < 1)
124                                 return;
125                         break;
126                 case UI_ID_AUTO_NAME:
127                         break;
128 #endif
129         }
130 }
131
132 /* ID Search browse menu, assign  */
133 static void id_search_call_cb(struct bContext *C, void *arg_litem, void *item)
134 {
135         if(item) {
136                 TemplateID *template= (TemplateID*)arg_litem;
137                 PointerRNA idptr= RNA_property_pointer_get(&template->ptr, template->prop);
138
139                 RNA_id_pointer_create(item, &idptr);
140                 RNA_property_pointer_set(&template->ptr, template->prop, idptr);
141                 RNA_property_update(C, &template->ptr, template->prop);
142         }       
143 }
144
145 /* ID Search browse menu, do the search */
146 static void id_search_cb(const struct bContext *C, void *arg_litem, char *str, uiSearchItems *items)
147 {
148         TemplateID *template= (TemplateID*)arg_litem;
149         ListBase *lb= wich_libbase(CTX_data_main(C), template->idtype);
150         ID *id;
151         
152         for(id= lb->first; id; id= id->next) {
153                 int iconid= 0;
154                 
155                 /* icon */
156                 switch(GS(id->name))
157                 {
158                         case ID_MA: /* fall through */
159                         case ID_TE: /* fall through */
160                         case ID_IM: /* fall through */
161                         case ID_WO: /* fall through */
162                         case ID_LA: /* fall through */
163                                 iconid= BKE_icon_getid(id);
164                                 /* checks if not exists, or changed */
165                                 ui_id_icon_render(CTX_data_scene(C), id);
166                                 break;
167                         default:
168                                 break;
169                 }
170                 
171                 if(BLI_strcasestr(id->name+2, str)) {
172                         if(0==uiSearchItemAdd(items, id->name+2, id, iconid))
173                                 break;
174                 }
175         }
176 }
177
178 /* ID Search browse menu, open */
179 static uiBlock *id_search_menu(bContext *C, ARegion *ar, void *arg_litem)
180 {
181         static char search[256];
182         static TemplateID template;
183         wmEvent event;
184         wmWindow *win= CTX_wm_window(C);
185         uiBlock *block;
186         uiBut *but;
187         
188         /* clear initial search string, then all items show */
189         search[0]= 0;
190         /* arg_litem is malloced, can be freed by parent button */
191         template= *((TemplateID*)arg_litem);
192         
193         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
194         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
195         
196         /* fake button, it holds space for search items */
197         uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
198         
199         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, 256, 10, 0, 150, 19, "");
200         uiButSetSearchFunc(but, id_search_cb, &template, id_search_call_cb);
201         
202         uiBoundsBlock(block, 6);
203         uiBlockSetDirection(block, UI_DOWN);    
204         uiEndBlock(C, block);
205         
206         event= *(win->eventstate);      /* XXX huh huh? make api call */
207         event.type= EVT_BUT_OPEN;
208         event.val= KM_PRESS;
209         event.customdata= but;
210         event.customdatafree= FALSE;
211         wm_event_add(win, &event);
212         
213         return block;
214 }
215
216 /* ****************** */
217
218
219 static void template_header_ID(bContext *C, uiBlock *block, TemplateID *template, StructRNA *type)
220 {
221         uiBut *but;
222         PointerRNA idptr;
223         ListBase *lb;
224
225         idptr= RNA_property_pointer_get(&template->ptr, template->prop);
226         lb= wich_libbase(CTX_data_main(C), template->idtype);
227
228         if(idptr.type)
229                 type= idptr.type;
230         if(type)
231                 uiDefIconBut(block, LABEL, 0, RNA_struct_ui_icon(type), 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
232
233         uiBlockBeginAlign(block);
234         if(template->flag & UI_ID_BROWSE) {
235                 /*
236                 char *extrastr, *str;
237                 
238                 if((template->flag & UI_ID_ADD_NEW) && (template->flag & UI_ID_OPEN))
239                         extrastr= "OPEN NEW %x 32766 |ADD NEW %x 32767";
240                 else if(template->flag & UI_ID_ADD_NEW)
241                         extrastr= "ADD NEW %x 32767";
242                 else if(template->flag & UI_ID_OPEN)
243                         extrastr= "OPEN NEW %x 32766";
244                 else
245                         extrastr= NULL;
246
247                 duptemplate= MEM_dupallocN(template);
248                 IDnames_to_pupstring(&str, NULL, extrastr, lb, idptr.data, &duptemplate->browse);
249
250                 but= uiDefButS(block, MENU, 0, str, 0, 0, UI_UNIT_X, UI_UNIT_Y, &duptemplate->browse, 0, 0, 0, 0, "Browse existing choices, or add new");
251                 uiButSetNFunc(but, template_id_cb, duptemplate, SET_INT_IN_POINTER(UI_ID_BROWSE));
252         
253                 MEM_freeN(str);
254                 */
255                 uiDefBlockButN(block, id_search_menu, MEM_dupallocN(template), "", 0, 0, UI_UNIT_X, UI_UNIT_Y, "Browse ID data");
256         }
257
258         /* text button with name */
259         if(idptr.data) {
260                 char name[64];
261
262                 //text_idbutton(idptr.data, name);
263                 name[0]= '\0';
264                 but= uiDefButR(block, TEX, 0, name, 0, 0, UI_UNIT_X*6, UI_UNIT_Y, &idptr, "name", -1, 0, 0, -1, -1, NULL);
265                 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_RENAME));
266         }
267         
268         if(template->flag & UI_ID_ADD_NEW) {
269                 int w= idptr.data?UI_UNIT_X:UI_UNIT_X*6;
270                 
271                 if(template->newop[0]) {
272                         but= uiDefIconTextButO(block, BUT, template->newop, WM_OP_EXEC_REGION_WIN, ICON_ZOOMIN, "Add New", 0, 0, w, UI_UNIT_Y, NULL);
273                 }
274                 else {
275                         but= uiDefIconTextBut(block, BUT, 0, ICON_ZOOMIN, "Add New", 0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
276                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ADD_NEW));
277                 }
278         }
279         
280         /* delete button */
281         if(idptr.data && (template->flag & UI_ID_DELETE)) {
282                 if(template->unlinkop[0]) {
283                         but= uiDefIconButO(block, BUT, template->unlinkop, WM_OP_EXEC_REGION_WIN, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL);
284                 }
285                 else {
286                         but= uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
287                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_DELETE));
288                 }
289         }
290         
291         uiBlockEndAlign(block);
292 }
293
294 void uiTemplateID(uiLayout *layout, bContext *C, PointerRNA *ptr, char *propname, char *newop, char *openop, char *unlinkop)
295 {
296         TemplateID *template;
297         uiBlock *block;
298         PropertyRNA *prop;
299         StructRNA *type;
300
301         if(!ptr->data)
302                 return;
303
304         prop= RNA_struct_find_property(ptr, propname);
305
306         if(!prop || RNA_property_type(prop) != PROP_POINTER) {
307                 printf("uiTemplateID: pointer property not found: %s\n", propname);
308                 return;
309         }
310
311         template= MEM_callocN(sizeof(TemplateID), "TemplateID");
312         template->ptr= *ptr;
313         template->prop= prop;
314         template->flag= UI_ID_BROWSE|UI_ID_RENAME|UI_ID_DELETE;
315
316         if(newop) {
317                 template->flag |= UI_ID_ADD_NEW;
318                 BLI_strncpy(template->newop, newop, sizeof(template->newop));
319         }
320         if(openop) {
321                 template->flag |= UI_ID_OPEN;
322                 BLI_strncpy(template->openop, openop, sizeof(template->openop));
323         }
324         if(unlinkop)
325                 BLI_strncpy(template->unlinkop, unlinkop, sizeof(template->unlinkop));
326
327         type= RNA_property_pointer_type(ptr, prop);
328         template->idtype = RNA_type_to_ID_code(type);
329
330         if(template->idtype) {
331                 uiLayoutRow(layout, 1);
332                 block= uiLayoutGetBlock(layout);
333                 template_header_ID(C, block, template, type);
334         }
335
336         MEM_freeN(template);
337 }
338
339 /************************ Modifier Template *************************/
340
341 #define ERROR_LIBDATA_MESSAGE "Can't edit external libdata"
342
343 #include <string.h>
344
345 #include "DNA_object_force.h"
346 #include "DNA_object_types.h"
347 #include "DNA_modifier_types.h"
348 #include "DNA_scene_types.h"
349
350 #include "BKE_depsgraph.h"
351 #include "BKE_DerivedMesh.h"
352 #include "BKE_global.h"
353 #include "BKE_modifier.h"
354 #include "BKE_object.h"
355 #include "BKE_particle.h"
356 #include "BKE_report.h"
357
358 #include "UI_resources.h"
359 #include "ED_util.h"
360
361 #include "BLI_arithb.h"
362 #include "BLI_listbase.h"
363
364 #include "ED_object.h"
365
366 static void modifiers_del(bContext *C, void *ob_v, void *md_v)
367 {
368         Scene *scene= CTX_data_scene(C);
369         Object *ob= ob_v;
370         ReportList reports;
371
372         BKE_reports_init(&reports, RPT_STORE);
373
374         if(ED_object_modifier_delete(&reports, ob_v, md_v)) {
375                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
376                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
377
378                 ED_undo_push(C, "Delete modifier");
379         }
380         else
381                 uiPupMenuReports(C, &reports);
382
383         BKE_reports_clear(&reports);
384 }
385
386 static void modifiers_activate(bContext *C, void *ob_v, void *md_v)
387 {
388         Scene *scene= CTX_data_scene(C);
389         Object *ob= ob_v;
390
391         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
392         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
393 }
394
395 static void modifiers_moveUp(bContext *C, void *ob_v, void *md_v)
396 {
397         Scene *scene= CTX_data_scene(C);
398         Object *ob= ob_v;
399         ReportList reports;
400
401         BKE_reports_init(&reports, RPT_STORE);
402
403         if(ED_object_modifier_move_up(&reports, ob_v, md_v)) {
404                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
405                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
406
407                 ED_undo_push(C, "Move modifier");
408         }
409         else
410                 uiPupMenuReports(C, &reports);
411
412         BKE_reports_clear(&reports);
413 }
414
415 static void modifiers_moveDown(bContext *C, void *ob_v, void *md_v)
416 {
417         Scene *scene= CTX_data_scene(C);
418         Object *ob= ob_v;
419         ReportList reports;
420
421         BKE_reports_init(&reports, RPT_STORE);
422
423         if(ED_object_modifier_move_down(&reports, ob_v, md_v)) {
424                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
425                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
426
427                 ED_undo_push(C, "Move modifier");
428         }
429         else
430                 uiPupMenuReports(C, &reports);
431
432         BKE_reports_clear(&reports);
433 }
434
435 static void modifiers_convertParticles(bContext *C, void *obv, void *mdv)
436 {
437         Scene *scene= CTX_data_scene(C);
438         Object *ob= obv;
439         ReportList reports;
440
441         BKE_reports_init(&reports, RPT_STORE);
442
443         if(ED_object_modifier_convert(&reports, scene, obv, mdv)) {
444                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
445                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
446
447                 ED_undo_push(C, "Convert particles to mesh object(s).");
448         }
449         else
450                 uiPupMenuReports(C, &reports);
451
452         BKE_reports_clear(&reports);
453 }
454
455 static void modifiers_applyModifier(bContext *C, void *obv, void *mdv)
456 {
457         Scene *scene= CTX_data_scene(C);
458         Object *ob= obv;
459         ReportList reports;
460
461         BKE_reports_init(&reports, RPT_STORE);
462
463         if(ED_object_modifier_apply(&reports, scene, obv, mdv)) {
464                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
465                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
466
467                 ED_undo_push(C, "Apply modifier");
468         }
469         else
470                 uiPupMenuReports(C, &reports);
471
472         BKE_reports_clear(&reports);
473 }
474
475 static void modifiers_copyModifier(bContext *C, void *ob_v, void *md_v)
476 {
477         Scene *scene= CTX_data_scene(C);
478         Object *ob= ob_v;
479         ReportList reports;
480
481         BKE_reports_init(&reports, RPT_STORE);
482
483         if(ED_object_modifier_copy(&reports, ob_v, md_v)) {
484                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
485                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
486                 ED_undo_push(C, "Copy modifier");
487         }
488         else
489                 uiPupMenuReports(C, &reports);
490
491         BKE_reports_clear(&reports);
492 }
493
494 static void modifiers_setOnCage(bContext *C, void *ob_v, void *md_v)
495 {
496         Scene *scene= CTX_data_scene(C);
497         Object *ob = ob_v;
498         ModifierData *md;
499         
500         int i, cageIndex = modifiers_getCageIndex(ob, NULL );
501
502         for(i = 0, md=ob->modifiers.first; md; ++i, md=md->next) {
503                 if(md == md_v) {
504                         if(i >= cageIndex)
505                                 md->mode ^= eModifierMode_OnCage;
506                         break;
507                 }
508         }
509
510         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
511         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
512 }
513
514 static void modifiers_convertToReal(bContext *C, void *ob_v, void *md_v)
515 {
516         Scene *scene= CTX_data_scene(C);
517         Object *ob = ob_v;
518         ModifierData *md = md_v;
519         ModifierData *nmd = modifier_new(md->type);
520
521         modifier_copyData(md, nmd);
522         nmd->mode &= ~eModifierMode_Virtual;
523
524         BLI_addhead(&ob->modifiers, nmd);
525
526         ob->partype = PAROBJECT;
527
528         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
529         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
530
531         ED_undo_push(C, "Modifier convert to real");
532 }
533
534 static int modifier_can_delete(ModifierData *md)
535 {
536         // deletion over the deflection panel
537         // fluid particle modifier can't be deleted here
538
539         if(md->type==eModifierType_Fluidsim)
540                 return 0;
541         if(md->type==eModifierType_Collision)
542                 return 0;
543         if(md->type==eModifierType_Surface)
544                 return 0;
545         if(md->type == eModifierType_ParticleSystem)
546                 if(((ParticleSystemModifierData *)md)->psys->part->type == PART_FLUID)
547                         return 0;
548
549         return 1;
550 }
551
552 static uiLayout *draw_modifier(uiLayout *layout, Object *ob, ModifierData *md, int index, int cageIndex, int lastCageIndex)
553 {
554         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
555         PointerRNA ptr;
556         uiBut *but;
557         uiBlock *block;
558         uiLayout *column, *row, *subrow, *result= NULL;
559         int isVirtual = md->mode&eModifierMode_Virtual;
560         // XXX short color = md->error?TH_REDALERT:TH_BUT_NEUTRAL;
561         short width = 295, buttonWidth = width-120-10;
562         char str[128];
563
564         RNA_pointer_create(&ob->id, &RNA_Modifier, md, &ptr);
565
566         column= uiLayoutColumn(layout, 1);
567         uiLayoutSetContextPointer(column, "modifier", &ptr);
568
569         /* rounded header */
570         /* XXX uiBlockSetCol(block, color); */
571                 /* roundbox 4 free variables: corner-rounding, nop, roundbox type, shade */
572
573         row= uiLayoutRow(uiLayoutBox(column), 0);
574         block= uiLayoutGetBlock(row);
575
576         subrow= uiLayoutRow(row, 0);
577         uiLayoutSetAlignment(subrow, UI_LAYOUT_ALIGN_LEFT);
578
579         //uiDefBut(block, ROUNDBOX, 0, "", x-10, y-4, width, 25, NULL, 7.0, 0.0, 
580         //               (!isVirtual && (md->mode&eModifierMode_Expanded))?3:15, 20, ""); 
581         /* XXX uiBlockSetCol(block, TH_AUTO); */
582         
583         /* open/close icon */
584         if (!isVirtual) {
585                 uiBlockSetEmboss(block, UI_EMBOSSN);
586                 uiDefIconButBitI(block, ICONTOG, eModifierMode_Expanded, 0, ICON_TRIA_RIGHT, 0, 0, UI_UNIT_X, UI_UNIT_Y, &md->mode, 0.0, 0.0, 0.0, 0.0, "Collapse/Expand Modifier");
587         }
588         
589         /* modifier-type icon */
590         uiDefIconBut(block, BUT, 0, RNA_struct_ui_icon(ptr.type), 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, "Current Modifier Type");
591         
592         uiBlockSetEmboss(block, UI_EMBOSS);
593         
594         if (isVirtual) {
595                 sprintf(str, "%s parent deform", md->name);
596                 uiDefBut(block, LABEL, 0, str, 0, 0, 185, UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, "Modifier name"); 
597
598                 but = uiDefBut(block, BUT, 0, "Make Real", 0, 0, 80, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Convert virtual modifier to a real modifier");
599                 uiButSetFunc(but, modifiers_convertToReal, ob, md);
600         } else {
601                 uiBlockBeginAlign(block);
602                 uiDefBut(block, TEX, 0, "", 0, 0, buttonWidth-40, UI_UNIT_Y, md->name, 0.0, sizeof(md->name)-1, 0.0, 0.0, "Modifier name"); 
603
604                 /* Softbody not allowed in this situation, enforce! */
605                 if (((md->type!=eModifierType_Softbody && md->type!=eModifierType_Collision) || !(ob->pd && ob->pd->deflect)) && (md->type!=eModifierType_Surface)) {
606                         uiDefIconButBitI(block, TOG, eModifierMode_Render, 0, ICON_SCENE, 0, 0, 19, UI_UNIT_Y,&md->mode, 0, 0, 1, 0, "Enable modifier during rendering");
607                         but= uiDefIconButBitI(block, TOG, eModifierMode_Realtime, 0, ICON_VIEW3D, 0, 0, 19, UI_UNIT_Y,&md->mode, 0, 0, 1, 0, "Enable modifier during interactive display");
608                         uiButSetFunc(but, modifiers_activate, ob, md);
609                         if (mti->flags&eModifierTypeFlag_SupportsEditmode) {
610                                 but= uiDefIconButBitI(block, TOG, eModifierMode_Editmode, 0, ICON_EDITMODE_HLT, 0, 0, 19, UI_UNIT_Y,&md->mode, 0, 0, 1, 0, "Enable modifier during Editmode (only if enabled for display)");
611                                 uiButSetFunc(but, modifiers_activate, ob, md);
612                         }
613                 }
614                 uiBlockEndAlign(block);
615
616                 /* XXX uiBlockSetEmboss(block, UI_EMBOSSR); */
617
618                 if (ob->type==OB_MESH && modifier_couldBeCage(md) && index<=lastCageIndex) {
619                         int icon; //, color;
620
621                         if (index==cageIndex) {
622                                 // XXX color = TH_BUT_SETTING;
623                                 icon = VICON_EDITMODE_HLT;
624                         } else if (index<cageIndex) {
625                                 // XXX color = TH_BUT_NEUTRAL;
626                                 icon = VICON_EDITMODE_DEHLT;
627                         } else {
628                                 // XXX color = TH_BUT_NEUTRAL;
629                                 icon = ICON_BLANK1;
630                         }
631                         /* XXX uiBlockSetCol(block, color); */
632                         but = uiDefIconBut(block, BUT, 0, icon, 0, 0, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Apply modifier to editing cage during Editmode");
633                         uiButSetFunc(but, modifiers_setOnCage, ob, md);
634                         /* XXX uiBlockSetCol(block, TH_AUTO); */
635                 }
636         }
637
638         subrow= uiLayoutRow(row, 0);
639         uiLayoutSetAlignment(subrow, UI_LAYOUT_ALIGN_RIGHT);
640
641         if(!isVirtual) {
642                 /* XXX uiBlockSetCol(block, TH_BUT_ACTION); */
643
644                 but = uiDefIconBut(block, BUT, 0, VICON_MOVE_UP, 0, 0, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Move modifier up in stack");
645                 uiButSetFunc(but, modifiers_moveUp, ob, md);
646
647                 but = uiDefIconBut(block, BUT, 0, VICON_MOVE_DOWN, 0, 0, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Move modifier down in stack");
648                 uiButSetFunc(but, modifiers_moveDown, ob, md);
649                 
650                 uiBlockSetEmboss(block, UI_EMBOSSN);
651                 
652                 if(modifier_can_delete(md)) {
653                         but = uiDefIconBut(block, BUT, 0, VICON_X, 0, 0, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Delete modifier");
654                         uiButSetFunc(but, modifiers_del, ob, md);
655                 }
656                 /* XXX uiBlockSetCol(block, TH_AUTO); */
657         }
658
659         uiBlockSetEmboss(block, UI_EMBOSS);
660
661         if(!isVirtual && (md->mode&eModifierMode_Expanded)) {
662                 uiLayout *box;
663
664                 box= uiLayoutBox(column);
665                 row= uiLayoutRow(box, 1);
666
667                 if (!isVirtual && (md->type!=eModifierType_Collision) && (md->type!=eModifierType_Surface)) {
668                         uiBlockSetButLock(block, object_data_is_libdata(ob), ERROR_LIBDATA_MESSAGE); /* only here obdata, the rest of modifiers is ob level */
669
670                                                 if (md->type==eModifierType_ParticleSystem) {
671                         ParticleSystem *psys= ((ParticleSystemModifierData *)md)->psys;
672
673                         if(!(G.f & G_PARTICLEEDIT)) {
674                                         if(ELEM3(psys->part->draw_as, PART_DRAW_PATH, PART_DRAW_GR, PART_DRAW_OB) && psys->pathcache) {
675                                                 but = uiDefBut(block, BUT, 0, "Convert",        0,0,60,19, 0, 0, 0, 0, 0, "Convert the current particles to a mesh object");
676                                                 uiButSetFunc(but, modifiers_convertParticles, ob, md);
677                                         }
678                                 }
679                         }
680                         else{
681                                 but = uiDefBut(block, BUT, 0, "Apply",  0,0,60,19, 0, 0, 0, 0, 0, "Apply the current modifier and remove from the stack");
682                                 uiButSetFunc(but, modifiers_applyModifier, ob, md);
683                         }
684                         
685                         uiBlockClearButLock(block);
686                         uiBlockSetButLock(block, ob && ob->id.lib, ERROR_LIBDATA_MESSAGE);
687
688                         if (md->type!=eModifierType_Fluidsim && md->type!=eModifierType_Softbody && md->type!=eModifierType_ParticleSystem && (md->type!=eModifierType_Cloth)) {
689                                 but = uiDefBut(block, BUT, 0, "Copy",   0,0,60,19, 0, 0, 0, 0, 0, "Duplicate the current modifier at the same position in the stack");
690                                 uiButSetFunc(but, modifiers_copyModifier, ob, md);
691                         }
692                 }
693
694                 result= uiLayoutColumn(box, 0);
695                 block= uiLayoutFreeBlock(box);
696         }
697
698         if (md->error) {
699                 row = uiLayoutRow(uiLayoutBox(column), 0);
700
701                 /* XXX uiBlockSetCol(block, color); */
702                 uiItemL(row, md->error, ICON_ERROR);
703                 /* XXX uiBlockSetCol(block, TH_AUTO); */
704         }
705
706         return result;
707 }
708
709 uiLayout *uiTemplateModifier(uiLayout *layout, PointerRNA *ptr)
710 {
711         Object *ob;
712         ModifierData *md, *vmd;
713         int i, lastCageIndex, cageIndex;
714
715         /* verify we have valid data */
716         if(!RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
717                 printf("uiTemplateModifier: expected modifier on object.\n");
718                 return NULL;
719         }
720
721         ob= ptr->id.data;
722         md= ptr->data;
723
724         if(!ob || !(GS(ob->id.name) == ID_OB)) {
725                 printf("uiTemplateModifier: expected modifier on object.\n");
726                 return NULL;
727         }
728         
729         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
730         
731         /* find modifier and draw it */
732         cageIndex = modifiers_getCageIndex(ob, &lastCageIndex);
733
734         // XXX virtual modifiers are not accesible for python
735         vmd = modifiers_getVirtualModifierList(ob);
736
737         for(i=0; vmd; i++, vmd=vmd->next) {
738                 if(md == vmd)
739                         return draw_modifier(layout, ob, md, i, cageIndex, lastCageIndex);
740                 else if(vmd->mode&eModifierMode_Virtual)
741                         i--;
742         }
743
744         return NULL;
745 }
746
747 /************************ Constraint Template *************************/
748
749 #include "DNA_action_types.h"
750 #include "DNA_constraint_types.h"
751
752 #include "BKE_action.h"
753 #include "BKE_constraint.h"
754
755 #define REDRAWIPO                                       1
756 #define REDRAWNLA                                       2
757 #define REDRAWBUTSOBJECT                        3               
758 #define REDRAWACTION                            4
759 #define B_CONSTRAINT_TEST                       5
760 #define B_CONSTRAINT_CHANGETARGET       6
761 #define B_CONSTRAINT_INF                        7
762 #define REMAKEIPO                                       8
763 #define B_DIFF                                          9
764
765 void do_constraint_panels(bContext *C, void *arg, int event)
766 {
767         Scene *scene= CTX_data_scene(C);
768         Object *ob= CTX_data_active_object(C);
769         
770         switch(event) {
771         case B_CONSTRAINT_TEST:
772                 // XXX allqueue(REDRAWVIEW3D, 0);
773                 // XXX allqueue(REDRAWBUTSOBJECT, 0);
774                 // XXX allqueue(REDRAWBUTSEDIT, 0);
775                 break;  // no handling
776         case B_CONSTRAINT_INF:
777                 /* influence; do not execute actions for 1 dag_flush */
778                 if (ob->pose)
779                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
780                 break;
781         case B_CONSTRAINT_CHANGETARGET:
782                 if (ob->pose) ob->pose->flag |= POSE_RECALC;    // checks & sorts pose channels
783                 DAG_scene_sort(scene);
784                 break;
785         default:
786                 break;
787         }
788
789         object_test_constraints(ob);
790         
791         if(ob->pose) update_pose_constraint_flags(ob->pose);
792         
793         if(ob->type==OB_ARMATURE) DAG_object_flush_update(scene, ob, OB_RECALC_DATA|OB_RECALC_OB);
794         else DAG_object_flush_update(scene, ob, OB_RECALC_OB);
795         
796         // XXX allqueue(REDRAWVIEW3D, 0);
797         // XXX allqueue(REDRAWBUTSOBJECT, 0);
798         // XXX allqueue(REDRAWBUTSEDIT, 0);
799 }
800
801 static void constraint_active_func(bContext *C, void *ob_v, void *con_v)
802 {
803         ED_object_constraint_set_active(ob_v, con_v);
804 }
805
806 static void del_constraint_func (bContext *C, void *ob_v, void *con_v)
807 {
808         if(ED_object_constraint_delete(NULL, ob_v, con_v))
809                 ED_undo_push(C, "Delete Constraint");
810 }
811
812 static void verify_constraint_name_func (bContext *C, void *con_v, void *name_v)
813 {
814         Object *ob= CTX_data_active_object(C);
815         bConstraint *con= con_v;
816         char oldname[32];       
817         
818         if (!con)
819                 return;
820         
821         /* put on the stack */
822         BLI_strncpy(oldname, (char *)name_v, 32);
823         
824         ED_object_constraint_rename(ob, con, oldname);
825         ED_object_constraint_set_active(ob, con);
826         // XXX allqueue(REDRAWACTION, 0); 
827 }
828
829 static void constraint_moveUp(bContext *C, void *ob_v, void *con_v)
830 {
831         if(ED_object_constraint_move_up(NULL, ob_v, con_v))
832                 ED_undo_push(C, "Move Constraint");
833 }
834
835 static void constraint_moveDown(bContext *C, void *ob_v, void *con_v)
836 {
837         if(ED_object_constraint_move_down(NULL, ob_v, con_v))
838                 ED_undo_push(C, "Move Constraint");
839 }
840
841 /* some commonly used macros in the constraints drawing code */
842 #define is_armature_target(target) (target && target->type==OB_ARMATURE)
843 #define is_armature_owner(ob) ((ob->type == OB_ARMATURE) && (ob->flag & OB_POSEMODE))
844 #define is_geom_target(target) (target && (ELEM(target->type, OB_MESH, OB_LATTICE)) )
845
846 /* Helper function for draw constraint - draws constraint space stuff 
847  * This function should not be called if no menus are required 
848  * owner/target: -1 = don't draw menu; 0= not posemode, 1 = posemode 
849  */
850 static void draw_constraint_spaceselect (uiBlock *block, bConstraint *con, short xco, short yco, short owner, short target)
851 {
852         short tarx, ownx, iconx;
853         short bwidth;
854         short iconwidth = 20;
855         
856         /* calculate sizes and placement of menus */
857         if (owner == -1) {
858                 bwidth = 125;
859                 tarx = 120;
860                 ownx = 0;
861         }
862         else if (target == -1) {
863                 bwidth = 125;
864                 tarx = 0;
865                 ownx = 120;
866         }
867         else {
868                 bwidth = 100;
869                 tarx = 85;
870                 iconx = tarx + bwidth + 5;
871                 ownx = tarx + bwidth + iconwidth + 10;
872         }
873         
874         
875         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Convert:", xco, yco, 80,18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
876
877         /* Target-Space */
878         if (target == 1) {
879                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Pose Space %x2|Local with Parent %x3|Local Space %x1", 
880                                                                                                 tarx, yco, bwidth, 18, &con->tarspace, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
881         }
882         else if (target == 0) {
883                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Local (Without Parent) Space %x1", 
884                                                                                 tarx, yco, bwidth, 18, &con->tarspace, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
885         }
886         
887         if ((target != -1) && (owner != -1))
888                 uiDefIconBut(block, LABEL, 0, ICON_ARROW_LEFTRIGHT,
889                         iconx, yco, 20, 20, NULL, 0.0, 0.0, 0.0, 0.0, "");
890         
891         /* Owner-Space */
892         if (owner == 1) {
893                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Owner Space %t|World Space %x0|Pose Space %x2|Local with Parent %x3|Local Space %x1", 
894                                                                                                 ownx, yco, bwidth, 18, &con->ownspace, 0, 0, 0, 0, "Choose space that owner is evaluated in");  
895         }
896         else if (owner == 0) {
897                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Owner Space %t|World Space %x0|Local (Without Parent) Space %x1", 
898                                                                                 ownx, yco, bwidth, 18, &con->ownspace, 0, 0, 0, 0, "Choose space that owner is evaluated in");  
899         }
900 }
901
902 /* draw panel showing settings for a constraint */
903 static uiLayout *draw_constraint(uiLayout *layout, Object *ob, bConstraint *con)
904 {
905         bPoseChannel *pchan= get_active_posechannel(ob);
906         bConstraintTypeInfo *cti;
907         uiBlock *block;
908         uiLayout *result= NULL, *col, *box, *row, *subrow;
909         uiBut *but;
910         PointerRNA ptr;
911         char typestr[32];
912         short width = 265;
913         short proxy_protected, xco=0, yco=0;
914         int rb_col;
915
916         /* get constraint typeinfo */
917         cti= constraint_get_typeinfo(con);
918         if (cti == NULL) {
919                 /* exception for 'Null' constraint - it doesn't have constraint typeinfo! */
920                 if (con->type == CONSTRAINT_TYPE_NULL)
921                         strcpy(typestr, "Null");
922                 else
923                         strcpy(typestr, "Unknown");
924         }
925         else
926                 strcpy(typestr, cti->name);
927                 
928         /* determine whether constraint is proxy protected or not */
929         if (proxylocked_constraints_owner(ob, pchan))
930                 proxy_protected= (con->flag & CONSTRAINT_PROXY_LOCAL)==0;
931         else
932                 proxy_protected= 0;
933
934         /* unless button has own callback, it adds this callback to button */
935         block= uiLayoutGetBlock(layout);
936         uiBlockSetHandleFunc(block, do_constraint_panels, NULL);
937         uiBlockSetFunc(block, constraint_active_func, ob, con);
938
939         RNA_pointer_create(&ob->id, &RNA_Constraint, con, &ptr);
940
941         col= uiLayoutColumn(layout, 1);
942         uiLayoutSetContextPointer(col, "constraint", &ptr);
943
944         box= uiLayoutBox(col);
945         row= uiLayoutRow(box, 0);
946
947         block= uiLayoutFreeBlock(box);
948
949         subrow= uiLayoutRow(row, 0);
950         uiLayoutSetAlignment(subrow, UI_LAYOUT_ALIGN_LEFT);
951
952         /* Draw constraint header */
953         uiBlockSetEmboss(block, UI_EMBOSSN);
954         
955         /* rounded header */
956         rb_col= (con->flag & CONSTRAINT_ACTIVE)?50:20;
957         
958         /* open/close */
959         uiDefIconButBitS(block, ICONTOG, CONSTRAINT_EXPAND, B_CONSTRAINT_TEST, ICON_TRIA_RIGHT, xco-10, yco, 20, 20, &con->flag, 0.0, 0.0, 0.0, 0.0, "Collapse/Expand Constraint");
960         
961         /* name */      
962         if ((con->flag & CONSTRAINT_EXPAND) && (proxy_protected==0)) {
963                 /* XXX if (con->flag & CONSTRAINT_DISABLE)
964                         uiBlockSetCol(block, TH_REDALERT);*/
965                 
966                 uiBlockSetEmboss(block, UI_EMBOSS);
967                 
968                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr, xco+10, yco, 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
969                 
970                 but = uiDefBut(block, TEX, B_CONSTRAINT_TEST, "", xco+120, yco, 85, 18, con->name, 0.0, 29.0, 0.0, 0.0, "Constraint name"); 
971                 uiButSetFunc(but, verify_constraint_name_func, con, NULL);
972         }       
973         else {
974                 uiBlockSetEmboss(block, UI_EMBOSSN);
975                 
976                 /* XXX if (con->flag & CONSTRAINT_DISABLE)
977                         uiBlockSetCol(block, TH_REDALERT);*/
978                 
979                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr, xco+10, yco, 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
980                 
981                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, con->name, xco+120, yco-1, 135, 19, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
982         }
983
984         // XXX uiBlockSetCol(block, TH_AUTO);   
985
986         subrow= uiLayoutRow(row, 0);
987         uiLayoutSetAlignment(subrow, UI_LAYOUT_ALIGN_RIGHT);
988         
989         /* proxy-protected constraints cannot be edited, so hide up/down + close buttons */
990         if (proxy_protected) {
991                 uiBlockSetEmboss(block, UI_EMBOSSN);
992                 
993                 /* draw a ghost icon (for proxy) and also a lock beside it, to show that constraint is "proxy locked" */
994                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_GHOST, xco+244, yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Proxy Protected");
995                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_LOCKED, xco+262, yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Proxy Protected");
996                 
997                 uiBlockSetEmboss(block, UI_EMBOSS);
998         }
999         else {
1000                 short prev_proxylock, show_upbut, show_downbut;
1001                 
1002                 /* Up/Down buttons: 
1003                  *      Proxy-constraints are not allowed to occur after local (non-proxy) constraints
1004                  *      as that poses problems when restoring them, so disable the "up" button where
1005                  *      it may cause this situation. 
1006                  *
1007                  *      Up/Down buttons should only be shown (or not greyed - todo) if they serve some purpose. 
1008                  */
1009                 if (proxylocked_constraints_owner(ob, pchan)) {
1010                         if (con->prev) {
1011                                 prev_proxylock= (con->prev->flag & CONSTRAINT_PROXY_LOCAL) ? 0 : 1;
1012                         }
1013                         else
1014                                 prev_proxylock= 0;
1015                 }
1016                 else
1017                         prev_proxylock= 0;
1018                         
1019                 show_upbut= ((prev_proxylock == 0) && (con->prev));
1020                 show_downbut= (con->next) ? 1 : 0;
1021                 
1022                 if (show_upbut || show_downbut) {
1023                         uiBlockBeginAlign(block);
1024                                 uiBlockSetEmboss(block, UI_EMBOSS);
1025                                 
1026                                 if (show_upbut) {
1027                                         but = uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, VICON_MOVE_UP, xco+width-50, yco, 16, 18, NULL, 0.0, 0.0, 0.0, 0.0, "Move constraint up in constraint stack");
1028                                         uiButSetFunc(but, constraint_moveUp, ob, con);
1029                                 }
1030                                 
1031                                 if (show_downbut) {
1032                                         but = uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, VICON_MOVE_DOWN, xco+width-50+18, yco, 16, 18, NULL, 0.0, 0.0, 0.0, 0.0, "Move constraint down in constraint stack");
1033                                         uiButSetFunc(but, constraint_moveDown, ob, con);
1034                                 }
1035                         uiBlockEndAlign(block);
1036                 }
1037                 
1038                 
1039                 /* Close 'button' - emboss calls here disable drawing of 'button' behind X */
1040                 uiBlockSetEmboss(block, UI_EMBOSSN);
1041                 
1042                 but = uiDefIconBut(block, BUT, B_CONSTRAINT_CHANGETARGET, ICON_X, xco+262, yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Delete constraint");
1043                 uiButSetFunc(but, del_constraint_func, ob, con);
1044                 
1045                 uiBlockSetEmboss(block, UI_EMBOSS);
1046         }
1047         
1048         /* Set but-locks for protected settings (magic numbers are used here!) */
1049         if (proxy_protected)
1050                 uiBlockSetButLock(block, 1, "Cannot edit Proxy-Protected Constraint");
1051         
1052         /* Draw constraint data */
1053         if ((con->flag & CONSTRAINT_EXPAND) == 0) {
1054                 (yco) -= 21;
1055         }
1056         else {
1057                 box= uiLayoutBox(col);
1058                 block= uiLayoutFreeBlock(box);
1059
1060                 switch (con->type) {
1061 #ifndef DISABLE_PYTHON
1062                 case CONSTRAINT_TYPE_PYTHON:
1063                         {
1064                                 bPythonConstraint *data = con->data;
1065                                 bConstraintTarget *ct;
1066                                 // uiBut *but2;
1067                                 int tarnum, theight;
1068                                 // static int pyconindex=0;
1069                                 // char *menustr;
1070                                 
1071                                 theight = (data->tarnum)? (data->tarnum * 38) : (38);
1072                                 
1073                                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Script:", xco+60, yco-24, 55, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1074                                 
1075                                 /* do the scripts menu */
1076                                 /* XXX menustr = buildmenu_pyconstraints(data->text, &pyconindex);
1077                                 but2 = uiDefButI(block, MENU, B_CONSTRAINT_TEST, menustr,
1078                                       xco+120, yco-24, 150, 20, &pyconindex,
1079                                       0, 0, 0, 0, "Set the Script Constraint to use");
1080                                 uiButSetFunc(but2, validate_pyconstraint_cb, data, &pyconindex);
1081                                 MEM_freeN(menustr);      */
1082                                 
1083                                 /* draw target(s) */
1084                                 if (data->flag & PYCON_USETARGETS) {
1085                                         /* Draw target parameters */ 
1086                                         for (ct=data->targets.first, tarnum=1; ct; ct=ct->next, tarnum++) {
1087                                                 char tarstr[32];
1088                                                 short yoffset= ((tarnum-1) * 38);
1089         
1090                                                 /* target label */
1091                                                 sprintf(tarstr, "Target %d:", tarnum);
1092                                                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, tarstr, xco+45, yco-(48+yoffset), 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1093                                                 
1094                                                 /* target space-selector - per target */
1095                                                 if (is_armature_target(ct->tar)) {
1096                                                         uiDefButS(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Pose Space %x3|Local with Parent %x4|Local Space %x1", 
1097                                                                                                                         xco+10, yco-(66+yoffset), 100, 18, &ct->space, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
1098                                                 }
1099                                                 else {
1100                                                         uiDefButS(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Local (Without Parent) Space %x1", 
1101                                                                                                                         xco+10, yco-(66+yoffset), 100, 18, &ct->space, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
1102                                                 }
1103                                                 
1104                                                 uiBlockBeginAlign(block);
1105                                                         /* target object */
1106                                                         uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_CONSTRAINT_CHANGETARGET, "OB:", xco+120, yco-(48+yoffset), 150, 18, &ct->tar, "Target Object"); 
1107                                                         
1108                                                         /* subtarget */
1109                                                         if (is_armature_target(ct->tar)) {
1110                                                                 but= uiDefBut(block, TEX, B_CONSTRAINT_CHANGETARGET, "BO:", xco+120, yco-(66+yoffset),150,18, &ct->subtarget, 0, 24, 0, 0, "Subtarget Bone");
1111                                                                 uiButSetCompleteFunc(but, autocomplete_bone, (void *)ct->tar);
1112                                                         }
1113                                                         else if (is_geom_target(ct->tar)) {
1114                                                                 but= uiDefBut(block, TEX, B_CONSTRAINT_CHANGETARGET, "VG:", xco+120, yco-(66+yoffset),150,18, &ct->subtarget, 0, 24, 0, 0, "Name of Vertex Group defining 'target' points");
1115                                                                 uiButSetCompleteFunc(but, autocomplete_vgroup, (void *)ct->tar);
1116                                                         }
1117                                                         else {
1118                                                                 strcpy(ct->subtarget, "");
1119                                                         }
1120                                                 uiBlockEndAlign(block);
1121                                         }
1122                                 }
1123                                 else {
1124                                         /* Draw indication that no target needed */
1125                                         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Target:", xco+60, yco-48, 55, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1126                                         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Not Applicable", xco+120, yco-48, 150, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1127                                 }
1128                                 
1129                                 /* settings */
1130                                 uiBlockBeginAlign(block);
1131                                         but=uiDefBut(block, BUT, B_CONSTRAINT_TEST, "Options", xco, yco-(52+theight), (width/2),18, NULL, 0, 24, 0, 0, "Change some of the constraint's settings.");
1132                                         // XXX uiButSetFunc(but, BPY_pyconstraint_settings, data, NULL);
1133                                         
1134                                         but=uiDefBut(block, BUT, B_CONSTRAINT_TEST, "Refresh", xco+((width/2)+10), yco-(52+theight), (width/2),18, NULL, 0, 24, 0, 0, "Force constraint to refresh it's settings");
1135                                 uiBlockEndAlign(block);
1136                                 
1137                                 /* constraint space settings */
1138                                 draw_constraint_spaceselect(block, con, xco, yco-(73+theight), is_armature_owner(ob), -1);
1139                         }
1140                         break;
1141 #endif /* DISABLE_PYTHON */
1142                 /*case CONSTRAINT_TYPE_CHILDOF:
1143                         {
1144                                 // Inverse options 
1145                                 uiBlockBeginAlign(block);
1146                                         but=uiDefBut(block, BUT, B_CONSTRAINT_TEST, "Set Offset", xco, yco-151, (width/2),18, NULL, 0, 24, 0, 0, "Calculate current Parent-Inverse Matrix (i.e. restore offset from parent)");
1147                                         // XXX uiButSetFunc(but, childof_const_setinv, con, NULL);
1148                                         
1149                                         but=uiDefBut(block, BUT, B_CONSTRAINT_TEST, "Clear Offset", xco+((width/2)+10), yco-151, (width/2),18, NULL, 0, 24, 0, 0, "Clear Parent-Inverse Matrix (i.e. clear offset from parent)");
1150                                         // XXX uiButSetFunc(but, childof_const_clearinv, con, NULL);
1151                                 uiBlockEndAlign(block);
1152                         }
1153                         break; 
1154                 */
1155                 
1156                 /*case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1157                         {
1158                                 if (data->type==CONSTRAINT_RB_GENERIC6DOF) {
1159                                         // Draw Pairs of LimitToggle+LimitValue 
1160                                         uiBlockBeginAlign(block); 
1161                                                 uiDefButBitS(block, TOG, 1, B_CONSTRAINT_TEST, "LinMinX", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum x limit"); 
1162                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+togButWidth, yco-offsetY, (textButWidth-5), 18, &(data->minLimit[0]), -extremeLin, extremeLin, 0.1,0.5,"min x limit"); 
1163                                         uiBlockEndAlign(block);
1164                                         
1165                                         uiBlockBeginAlign(block); 
1166                                                 uiDefButBitS(block, TOG, 1, B_CONSTRAINT_TEST, "LinMaxX", xco+(width-(textButWidth-5)-togButWidth), yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use maximum x limit"); 
1167                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+(width-textButWidth-5), yco-offsetY, (textButWidth), 18, &(data->maxLimit[0]), -extremeLin, extremeLin, 0.1,0.5,"max x limit"); 
1168                                         uiBlockEndAlign(block);
1169                                         
1170                                         offsetY += 20;
1171                                         uiBlockBeginAlign(block); 
1172                                                 uiDefButBitS(block, TOG, 2, B_CONSTRAINT_TEST, "LinMinY", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum y limit"); 
1173                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+togButWidth, yco-offsetY, (textButWidth-5), 18, &(data->minLimit[1]), -extremeLin, extremeLin, 0.1,0.5,"min y limit"); 
1174                                         uiBlockEndAlign(block);
1175                                         
1176                                         uiBlockBeginAlign(block); 
1177                                                 uiDefButBitS(block, TOG, 2, B_CONSTRAINT_TEST, "LinMaxY", xco+(width-(textButWidth-5)-togButWidth), yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use maximum y limit"); 
1178                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+(width-textButWidth-5), yco-offsetY, (textButWidth), 18, &(data->maxLimit[1]), -extremeLin, extremeLin, 0.1,0.5,"max y limit"); 
1179                                         uiBlockEndAlign(block);
1180                                         
1181                                         offsetY += 20;
1182                                         uiBlockBeginAlign(block); 
1183                                                 uiDefButBitS(block, TOG, 4, B_CONSTRAINT_TEST, "LinMinZ", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum z limit"); 
1184                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+togButWidth, yco-offsetY, (textButWidth-5), 18, &(data->minLimit[2]), -extremeLin, extremeLin, 0.1,0.5,"min z limit"); 
1185                                         uiBlockEndAlign(block);
1186                                         
1187                                         uiBlockBeginAlign(block); 
1188                                                 uiDefButBitS(block, TOG, 4, B_CONSTRAINT_TEST, "LinMaxZ", xco+(width-(textButWidth-5)-togButWidth), yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use maximum z limit"); 
1189                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+(width-textButWidth-5), yco-offsetY, (textButWidth), 18, &(data->maxLimit[2]), -extremeLin, extremeLin, 0.1,0.5,"max z limit"); 
1190                                         uiBlockEndAlign(block);
1191                                         offsetY += 20;
1192                                 }
1193                                 if ((data->type==CONSTRAINT_RB_GENERIC6DOF) || (data->type==CONSTRAINT_RB_CONETWIST)) {
1194                                         // Draw Pairs of LimitToggle+LimitValue /
1195                                         uiBlockBeginAlign(block); 
1196                                                 uiDefButBitS(block, TOG, 8, B_CONSTRAINT_TEST, "AngMinX", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum x limit"); 
1197                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+togButWidth, yco-offsetY, (textButWidth-5), 18, &(data->minLimit[3]), -extremeAngX, extremeAngX, 0.1,0.5,"min x limit"); 
1198                                         uiBlockEndAlign(block);
1199                                         uiBlockBeginAlign(block); 
1200                                                 uiDefButBitS(block, TOG, 8, B_CONSTRAINT_TEST, "AngMaxX", xco+(width-(textButWidth-5)-togButWidth), yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use maximum x limit"); 
1201                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+(width-textButWidth-5), yco-offsetY, (textButWidth), 18, &(data->maxLimit[3]), -extremeAngX, extremeAngX, 0.1,0.5,"max x limit"); 
1202                                         uiBlockEndAlign(block);
1203                                         
1204                                         offsetY += 20;
1205                                         uiBlockBeginAlign(block); 
1206                                                 uiDefButBitS(block, TOG, 16, B_CONSTRAINT_TEST, "AngMinY", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum y limit"); 
1207                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+togButWidth, yco-offsetY, (textButWidth-5), 18, &(data->minLimit[4]), -extremeAngY, extremeAngY, 0.1,0.5,"min y limit"); 
1208                                         uiBlockEndAlign(block);
1209                                         
1210                                         uiBlockBeginAlign(block); 
1211                                                 uiDefButBitS(block, TOG, 16, B_CONSTRAINT_TEST, "AngMaxY", xco+(width-(textButWidth-5)-togButWidth), yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use maximum y limit"); 
1212                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+(width-textButWidth-5), yco-offsetY, (textButWidth), 18, &(data->maxLimit[4]), -extremeAngY, extremeAngY, 0.1,0.5,"max y limit"); 
1213                                         uiBlockEndAlign(block);
1214                                         
1215                                         offsetY += 20;
1216                                         uiBlockBeginAlign(block); 
1217                                                 uiDefButBitS(block, TOG, 32, B_CONSTRAINT_TEST, "AngMinZ", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum z limit"); 
1218                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+togButWidth, yco-offsetY, (textButWidth-5), 18, &(data->minLimit[5]), -extremeAngZ, extremeAngZ, 0.1,0.5,"min z limit"); 
1219                                         uiBlockEndAlign(block);
1220                                         
1221                                         uiBlockBeginAlign(block); 
1222                                                 uiDefButBitS(block, TOG, 32, B_CONSTRAINT_TEST, "AngMaxZ", xco+(width-(textButWidth-5)-togButWidth), yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use maximum z limit"); 
1223                                                 uiDefButF(block, NUM, B_CONSTRAINT_TEST, "", xco+(width-textButWidth-5), yco-offsetY, (textButWidth), 18, &(data->maxLimit[5]), -extremeAngZ, extremeAngZ, 0.1,0.5,"max z limit"); 
1224                                         uiBlockEndAlign(block);
1225                                 }
1226                                 
1227                         }
1228                         break;
1229                         */
1230
1231                 case CONSTRAINT_TYPE_NULL:
1232                         {
1233                                 uiItemL(box, "", 0);
1234                         }
1235                         break;
1236                 default:
1237                         result= box;
1238                         break;
1239                 }
1240         }
1241         
1242         /* clear any locks set up for proxies/lib-linking */
1243         uiBlockClearButLock(block);
1244
1245         return result;
1246 }
1247
1248 uiLayout *uiTemplateConstraint(uiLayout *layout, PointerRNA *ptr)
1249 {
1250         Object *ob;
1251         bConstraint *con;
1252
1253         /* verify we have valid data */
1254         if(!RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
1255                 printf("uiTemplateConstraint: expected constraint on object.\n");
1256                 return NULL;
1257         }
1258
1259         ob= ptr->id.data;
1260         con= ptr->data;
1261
1262         if(!ob || !(GS(ob->id.name) == ID_OB)) {
1263                 printf("uiTemplateConstraint: expected constraint on object.\n");
1264                 return NULL;
1265         }
1266         
1267         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1268
1269         /* hrms, the temporal constraint should not draw! */
1270         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
1271                 bKinematicConstraint *data= con->data;
1272                 if(data->flag & CONSTRAINT_IK_TEMP)
1273                         return NULL;
1274         }
1275
1276         return draw_constraint(layout, ob, con);
1277 }
1278
1279 /************************* Group Template ***************************/
1280
1281 #if 0
1282 static void do_add_groupmenu(void *arg, int event)
1283 {
1284         Object *ob= OBACT;
1285         
1286         if(ob) {
1287                 
1288                 if(event== -1) {
1289                         Group *group= add_group( "Group" );
1290                         add_to_group(group, ob);
1291                 }
1292                 else
1293                         add_to_group(BLI_findlink(&G.main->group, event), ob);
1294                         
1295                 ob->flag |= OB_FROMGROUP;
1296                 BASACT->flag |= OB_FROMGROUP;
1297                 allqueue(REDRAWBUTSOBJECT, 0);
1298                 allqueue(REDRAWVIEW3D, 0);
1299         }               
1300 }
1301
1302 static uiBlock *add_groupmenu(void *arg_unused)
1303 {
1304         uiBlock *block;
1305         Group *group;
1306         short xco=0, yco= 0, index=0;
1307         char str[32];
1308         
1309         block= uiNewBlock(&curarea->uiblocks, "add_constraintmenu", UI_EMBOSSP, UI_HELV, curarea->win);
1310         uiBlockSetButmFunc(block, do_add_groupmenu, NULL);
1311
1312         uiDefBut(block, BUTM, B_NOP, "ADD NEW",         0, 20, 160, 19, NULL, 0.0, 0.0, 1, -1, "");
1313         for(group= G.main->group.first; group; group= group->id.next, index++) {
1314                 
1315                 /*if(group->id.lib) strcpy(str, "L  ");*/ /* we cant allow adding objects inside linked groups, it wont be saved anyway */
1316                 if(group->id.lib==0) {
1317                         strcpy(str, "   ");
1318                         strcat(str, group->id.name+2);
1319                         uiDefBut(block, BUTM, B_NOP, str,       xco*160, -20*yco, 160, 19, NULL, 0.0, 0.0, 1, index, "");
1320                         
1321                         yco++;
1322                         if(yco>24) {
1323                                 yco= 0;
1324                                 xco++;
1325                         }
1326                 }
1327         }
1328         
1329         uiTextBoundsBlock(block, 50);
1330         uiBlockSetDirection(block, UI_DOWN);    
1331         
1332         return block;
1333 }
1334
1335 static void group_ob_rem(void *gr_v, void *ob_v)
1336 {
1337         Object *ob= OBACT;
1338         
1339         if(rem_from_group(gr_v, ob) && find_group(ob, NULL)==NULL) {
1340                 ob->flag &= ~OB_FROMGROUP;
1341                 BASACT->flag &= ~OB_FROMGROUP;
1342         }
1343         allqueue(REDRAWBUTSOBJECT, 0);
1344         allqueue(REDRAWVIEW3D, 0);
1345
1346 }
1347
1348 static void group_local(void *gr_v, void *unused)
1349 {
1350         Group *group= gr_v;
1351         
1352         group->id.lib= NULL;
1353         
1354         allqueue(REDRAWBUTSOBJECT, 0);
1355         allqueue(REDRAWVIEW3D, 0);
1356         
1357 }
1358
1359 uiLayout *uiTemplateGroup(uiLayout *layout, Object *ob, Group *group)
1360 {
1361         uiSetButLock(1, NULL);
1362         uiDefBlockBut(block, add_groupmenu, NULL, "Add to Group", 10,150,150,20, "Add Object to a new Group");
1363
1364         /* all groups */
1365         if(group->id.lib) {
1366                 uiLayoutRow()
1367                 uiBlockBeginAlign(block);
1368                 uiSetButLock(GET_INT_FROM_POINTER(group->id.lib), ERROR_LIBDATA_MESSAGE); /* We cant actually use this button */
1369                 uiDefBut(block, TEX, B_IDNAME, "GR:",   10, 120-yco, 100, 20, group->id.name+2, 0.0, 21.0, 0, 0, "Displays Group name. Click to change.");
1370                 uiClearButLock();
1371                 
1372                 but= uiDefIconBut(block, BUT, B_NOP, ICON_PARLIB, 110, 120-yco, 20, 20, NULL, 0.0, 0.0, 0.0, 0.0, "Make Group local");
1373                 uiButSetFunc(but, group_local, group, NULL);
1374                 uiBlockEndAlign(block);
1375         } else {
1376                 but = uiDefBut(block, TEX, B_IDNAME, "GR:",     10, 120-yco, 120, 20, group->id.name+2, 0.0, 21.0, 0, 0, "Displays Group name. Click to change.");
1377                 uiButSetFunc(but, test_idbutton_cb, group->id.name, NULL);
1378         }
1379         
1380         xco = 290;
1381         if(group->id.lib==0) { /* cant remove objects from linked groups */
1382                 but = uiDefIconBut(block, BUT, B_NOP, VICON_X, xco, 120-yco, 20, 20, NULL, 0.0, 0.0, 0.0, 0.0, "Remove Group membership");
1383                 uiButSetFunc(but, group_ob_rem, group, ob);
1384         }
1385 }
1386 #endif
1387
1388 /************************* Preview Template ***************************/
1389
1390 #include "DNA_material_types.h"
1391
1392 #define B_MATPRV 1
1393
1394
1395 static void do_preview_buttons(bContext *C, void *arg, int event)
1396 {
1397         switch(event) {
1398                 case B_MATPRV:
1399                         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING, arg);
1400                         break;
1401         }
1402 }
1403
1404 void uiTemplatePreview(uiLayout *layout, ID *id)
1405 {
1406         uiLayout *row, *col;
1407         uiBlock *block;
1408         Material *ma;
1409
1410         if(id && !ELEM4(GS(id->name), ID_MA, ID_TE, ID_WO, ID_LA)) {
1411                 printf("uiTemplatePreview: expected ID of type material, texture, lamp or world.\n");
1412                 return;
1413         }
1414
1415         block= uiLayoutGetBlock(layout);
1416
1417         row= uiLayoutRow(layout, 0);
1418
1419         col= uiLayoutColumn(row, 0);
1420         uiLayoutSetKeepAspect(col, 1);
1421         
1422         uiDefBut(block, BUT_EXTRA, 0, "", 0, 0, UI_UNIT_X*6, UI_UNIT_Y*6, id, 0.0, 0.0, 0, 0, "");
1423         uiBlockSetDrawExtraFunc(block, ED_preview_draw);
1424         
1425         uiBlockSetHandleFunc(block, do_preview_buttons, NULL);
1426         
1427         if(id) {
1428                 if(GS(id->name) == ID_MA) {
1429                         ma= (Material*)id;
1430
1431                         uiLayoutColumn(row, 1);
1432
1433                         uiDefIconButC(block, ROW, B_MATPRV, ICON_MATPLANE,  0, 0,UI_UNIT_X*1.5,UI_UNIT_Y, &(ma->pr_type), 10, MA_FLAT, 0, 0, "Preview type: Flat XY plane");
1434                         uiDefIconButC(block, ROW, B_MATPRV, ICON_MATSPHERE, 0, 0,UI_UNIT_X*1.5,UI_UNIT_Y, &(ma->pr_type), 10, MA_SPHERE, 0, 0, "Preview type: Sphere");
1435                         uiDefIconButC(block, ROW, B_MATPRV, ICON_MATCUBE,   0, 0,UI_UNIT_X*1.5,UI_UNIT_Y, &(ma->pr_type), 10, MA_CUBE, 0, 0, "Preview type: Cube");
1436                         uiDefIconButC(block, ROW, B_MATPRV, ICON_MONKEY,    0, 0,UI_UNIT_X*1.5,UI_UNIT_Y, &(ma->pr_type), 10, MA_MONKEY, 0, 0, "Preview type: Monkey");
1437                         uiDefIconButC(block, ROW, B_MATPRV, ICON_HAIR,      0, 0,UI_UNIT_X*1.5,UI_UNIT_Y, &(ma->pr_type), 10, MA_HAIR, 0, 0, "Preview type: Hair strands");
1438                         uiDefIconButC(block, ROW, B_MATPRV, ICON_MATSPHERE, 0, 0,UI_UNIT_X*1.5,UI_UNIT_Y, &(ma->pr_type), 10, MA_SPHERE_A, 0, 0, "Preview type: Large sphere with sky");
1439                 }
1440         }
1441 }
1442
1443 /********************** ColorRamp Template **************************/
1444
1445 void uiTemplateColorRamp(uiLayout *layout, ColorBand *coba, int expand)
1446 {
1447         uiBlock *block;
1448         rctf rect;
1449
1450         if(coba) {
1451                 rect.xmin= 0; rect.xmax= 200;
1452                 rect.ymin= 0; rect.ymax= 190;
1453                 
1454                 block= uiLayoutFreeBlock(layout);
1455                 colorband_buttons(block, coba, &rect, !expand);
1456         }
1457 }
1458
1459 /********************* CurveMapping Template ************************/
1460
1461 #include "DNA_color_types.h"
1462
1463 void uiTemplateCurveMapping(uiLayout *layout, CurveMapping *cumap, int type)
1464 {
1465         uiBlock *block;
1466         rctf rect;
1467
1468         if(cumap) {
1469                 rect.xmin= 0; rect.xmax= 200;
1470                 rect.ymin= 0; rect.ymax= 190;
1471                 
1472                 block= uiLayoutFreeBlock(layout);
1473                 curvemap_buttons(block, cumap, type, 0, 0, &rect);
1474         }
1475 }
1476
1477 /********************* Layer Buttons Template ************************/
1478
1479 // TODO:
1480 //      - option for showing extra info like whether layer has contents?
1481 //      - for now, grouping of layers is determined by dividing up the length of 
1482 //        the array of layer bitflags
1483
1484 void uiTemplateLayers(uiLayout *layout, PointerRNA *ptr, char *propname)
1485 {
1486         uiLayout *uRow, *uSplit, *uCol;
1487         PropertyRNA *prop;
1488         int groups, cols, layers;
1489         int group, col, layer, row;
1490         
1491         if (!ptr->data)
1492                 return;
1493         
1494         prop= RNA_struct_find_property(ptr, propname);
1495         if (!prop) {
1496                 printf("uiTemplateLayer: layers property not found: %s\n", propname);
1497                 return;
1498         }
1499         
1500         /* the number of layers determines the way we group them 
1501          *      - we want 2 rows only (for now)
1502          *      - the number of columns (cols) is the total number of buttons per row
1503          *        the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
1504          *      - for now, only split into groups if if group will have at least 5 items
1505          */
1506         layers= RNA_property_array_length(prop);
1507         cols= (layers / 2) + (layers % 2);
1508         groups= ((cols / 2) < 5) ? (1) : (cols / 2);
1509         
1510         /* layers are laid out going across rows, with the columns being divided into groups */
1511         uSplit= uiLayoutSplit(layout, (1.0f/(float)groups));
1512         
1513         for (group= 0; group < groups; group++) {
1514                 uCol= uiLayoutColumn(uSplit, 1);
1515                 
1516                 for (row= 0; row < 2; row++) {
1517                         uRow= uiLayoutRow(uCol, 1);
1518                         layer= groups*cols*row + cols*group;
1519                         
1520                         /* add layers as toggle buts */
1521                         for (col= 0; (col < cols) && (layer < layers); col++, layer++) {
1522                                 int icon=0; // XXX - add some way of setting this...
1523                                 uiItemFullR(uRow, "", icon, ptr, prop, layer, 0, 0, 0, 1);
1524                         }
1525                 }
1526         }
1527 }
1528
1529
1530 /************************* List Template **************************/
1531
1532 typedef struct ListItem {
1533         PointerRNA ptr;
1534         PropertyRNA *prop;
1535         PropertyRNA *activeprop;
1536
1537         PointerRNA activeptr;
1538         int activei;
1539
1540         int selected;
1541 } ListItem;
1542
1543 static void list_item_cb(bContext *C, void *arg_item, void *arg_unused)
1544 {
1545         ListItem *item= (ListItem*)arg_item;
1546         PropertyType activetype;
1547         char *activename;
1548
1549         if(item->selected) {
1550                 activetype= RNA_property_type(item->activeprop);
1551
1552                 if(activetype == PROP_POINTER)
1553                         RNA_property_pointer_set(&item->ptr, item->activeprop, item->activeptr);
1554                 else if(activetype == PROP_INT)
1555                         RNA_property_int_set(&item->ptr, item->activeprop, item->activei);
1556                 else if(activetype == PROP_STRING) {
1557                         activename= RNA_struct_name_get_alloc(&item->activeptr, NULL, 0);
1558                         RNA_property_string_set(&item->ptr, item->activeprop, activename);
1559                         MEM_freeN(activename);
1560                 }
1561         }
1562 }
1563
1564 void uiTemplateList(uiLayout *layout, PointerRNA *ptr, char *propname, char *activepropname, int items)
1565 {
1566         PropertyRNA *prop, *activeprop;
1567         PropertyType type, activetype;
1568         PointerRNA activeptr;
1569         uiLayout *box, *row, *col;
1570         uiBlock *block;
1571         uiBut *but;
1572         char *name, *activename= NULL;
1573         int i= 1, activei= 0, len;
1574         static int scroll = 1;
1575         
1576         /* validate arguments */
1577         if(!ptr->data)
1578                 return;
1579         
1580         prop= RNA_struct_find_property(ptr, propname);
1581         if(!prop) {
1582                 printf("uiTemplateList: property not found: %s\n", propname);
1583                 return;
1584         }
1585
1586         activeprop= RNA_struct_find_property(ptr, activepropname);
1587         if(!activeprop) {
1588                 printf("uiTemplateList: property not found: %s\n", activepropname);
1589                 return;
1590         }
1591
1592         type= RNA_property_type(prop);
1593         if(type != PROP_COLLECTION) {
1594                 printf("uiTemplateList: expected collection property.\n");
1595                 return;
1596         }
1597
1598         activetype= RNA_property_type(activeprop);
1599         if(!ELEM3(activetype, PROP_POINTER, PROP_INT, PROP_STRING)) {
1600                 printf("uiTemplateList: expected pointer, integer or string property.\n");
1601                 return;
1602         }
1603
1604         if(items == 0)
1605                 items= 5;
1606
1607         /* get active data */
1608         if(activetype == PROP_POINTER)
1609                 activeptr= RNA_property_pointer_get(ptr, activeprop);
1610         else if(activetype == PROP_INT)
1611                 activei= RNA_property_int_get(ptr, activeprop);
1612         else if(activetype == PROP_STRING)
1613                 activename= RNA_property_string_get_alloc(ptr, activeprop, NULL, 0);
1614
1615         box= uiLayoutBox(layout);
1616         row= uiLayoutRow(box, 0);
1617         col = uiLayoutColumn(row, 1);
1618
1619         block= uiLayoutGetBlock(col);
1620         uiBlockSetEmboss(block, UI_EMBOSSN);
1621
1622         len= RNA_property_collection_length(ptr, prop);
1623         scroll= MIN2(scroll, len-items+1);
1624         scroll= MAX2(scroll, 1);
1625
1626         RNA_PROP_BEGIN(ptr, itemptr, prop) {
1627                 if(i >= scroll && i<scroll+items) {
1628                         name= RNA_struct_name_get_alloc(&itemptr, NULL, 0);
1629
1630                         if(name) {
1631                                 ListItem *item= MEM_callocN(sizeof(ListItem), "uiTemplateList ListItem");
1632
1633                                 item->ptr= *ptr;
1634                                 item->prop= prop;
1635                                 item->activeprop= activeprop;
1636                                 item->activeptr= itemptr;
1637                                 item->activei= i;
1638
1639                                 if(activetype == PROP_POINTER)
1640                                         item->selected= (activeptr.data == itemptr.data)? i: -1;
1641                                 else if(activetype == PROP_INT)
1642                                         item->selected= (activei == i)? i: -1;
1643                                 else if(activetype == PROP_STRING)
1644                                         item->selected= (strcmp(activename, name) == 0)? i: -1;
1645
1646                                 but= uiDefIconTextButI(block, ROW, 0, RNA_struct_ui_icon(itemptr.type), name, 0,0,UI_UNIT_X*10,UI_UNIT_Y, &item->selected, 0, i, 0, 0, "");
1647                                 uiButSetFlag(but, UI_ICON_LEFT|UI_TEXT_LEFT);
1648                                 uiButSetNFunc(but, list_item_cb, item, NULL);
1649
1650                                 MEM_freeN(name);
1651                         }
1652                 }
1653
1654                 i++;
1655         }
1656         RNA_PROP_END;
1657
1658         while(i < scroll+items) {
1659                 if(i >= scroll)
1660                         uiItemL(col, "", 0);
1661                 i++;
1662         }
1663
1664         uiBlockSetEmboss(block, UI_EMBOSS);
1665
1666         if(len > items) {
1667                 col= uiLayoutColumn(row, 0);
1668                 uiDefButI(block, SCROLL, 0, "", 0,0,UI_UNIT_X*0.75,UI_UNIT_Y*items, &scroll, 1, len-items+1, items, 0, "");
1669         }
1670
1671         //uiDefButI(block, SCROLL, 0, "", 0,0,UI_UNIT_X*15,UI_UNIT_Y*0.75, &scroll, 1, 16-5, 5, 0, "");
1672 }
1673