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