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