Merge from 2.5 rev 20515 through 20647.
[blender.git] / source / blender / editors / interface / interface_templates.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): Blender Foundation 2009.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <stdlib.h>
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_string.h"
30
31 #include "BKE_context.h"
32 #include "BKE_library.h"
33 #include "BKE_utildefines.h"
34
35 #include "ED_screen.h"
36
37 #include "RNA_access.h"
38
39 #include "WM_api.h"
40 #include "WM_types.h"
41
42 #include "UI_interface.h"
43 #include "UI_resources.h"
44
45 void ui_template_fix_linking()
46 {
47 }
48
49 /********************** Header Template *************************/
50
51 void uiTemplateHeader(uiLayout *layout, bContext *C)
52 {
53         uiBlock *block;
54         
55         block= uiLayoutFreeBlock(layout);
56         ED_area_header_standardbuttons(C, block, 0);
57 }
58
59 /******************* Header ID Template ************************/
60
61 typedef struct TemplateHeaderID {
62         PointerRNA ptr;
63         PropertyRNA *prop;
64
65         int flag;
66         short browse;
67
68         char newop[256];
69         char openop[256];
70         char unlinkop[256];
71 } TemplateHeaderID;
72
73 static void template_header_id_cb(bContext *C, void *arg_litem, void *arg_event)
74 {
75         TemplateHeaderID *template= (TemplateHeaderID*)arg_litem;
76         PointerRNA idptr= RNA_property_pointer_get(&template->ptr, template->prop);
77         ID *idtest, *id= idptr.data;
78         ListBase *lb= wich_libbase(CTX_data_main(C), ID_TXT);
79         int nr, event= GET_INT_FROM_POINTER(arg_event);
80         
81         if(event == UI_ID_BROWSE && template->browse == 32767)
82                 event= UI_ID_ADD_NEW;
83         else if(event == UI_ID_BROWSE && template->browse == 32766)
84                 event= UI_ID_OPEN;
85
86         switch(event) {
87                 case UI_ID_BROWSE: {
88                         if(template->browse== -2) {
89                                 /* XXX implement or find a replacement
90                                  * activate_databrowse((ID *)G.buts->lockpoin, GS(id->name), 0, B_MESHBROWSE, &template->browse, do_global_buttons); */
91                                 return;
92                         }
93                         if(template->browse < 0)
94                                 return;
95
96                         for(idtest=lb->first, nr=1; idtest; idtest=idtest->next, nr++) {
97                                 if(nr==template->browse) {
98                                         if(id == idtest)
99                                                 return;
100
101                                         id= idtest;
102                                         RNA_id_pointer_create(id, &idptr);
103                                         RNA_property_pointer_set(&template->ptr, template->prop, idptr);
104                                         RNA_property_update(C, &template->ptr, template->prop);
105                                         /* XXX */
106
107                                         break;
108                                 }
109                         }
110                         break;
111                 }
112 #if 0
113                 case UI_ID_DELETE:
114                         id= NULL;
115                         break;
116                 case UI_ID_FAKE_USER:
117                         if(id) {
118                                 if(id->flag & LIB_FAKEUSER) id->us++;
119                                 else id->us--;
120                         }
121                         else return;
122                         break;
123 #endif
124                 case UI_ID_PIN:
125                         break;
126                 case UI_ID_ADD_NEW:
127                         WM_operator_name_call(C, template->newop, WM_OP_INVOKE_REGION_WIN, NULL);
128                         break;
129                 case UI_ID_OPEN:
130                         WM_operator_name_call(C, template->openop, WM_OP_INVOKE_REGION_WIN, NULL);
131                         break;
132 #if 0
133                 case UI_ID_ALONE:
134                         if(!id || id->us < 1)
135                                 return;
136                         break;
137                 case UI_ID_LOCAL:
138                         if(!id || id->us < 1)
139                                 return;
140                         break;
141                 case UI_ID_AUTO_NAME:
142                         break;
143 #endif
144         }
145 }
146
147 static void template_header_ID(bContext *C, uiBlock *block, TemplateHeaderID *template)
148 {
149         uiBut *but;
150         TemplateHeaderID *duptemplate;
151         PointerRNA idptr;
152         ListBase *lb;
153         int x= 0, y= 0;
154
155         idptr= RNA_property_pointer_get(&template->ptr, template->prop);
156         lb= wich_libbase(CTX_data_main(C), ID_TXT);
157
158         uiBlockBeginAlign(block);
159         if(template->flag & UI_ID_BROWSE) {
160                 char *extrastr, *str;
161                 
162                 if((template->flag & UI_ID_ADD_NEW) && (template->flag && UI_ID_OPEN))
163                         extrastr= "OPEN NEW %x 32766 |ADD NEW %x 32767";
164                 else if(template->flag & UI_ID_ADD_NEW)
165                         extrastr= "ADD NEW %x 32767";
166                 else if(template->flag & UI_ID_OPEN)
167                         extrastr= "OPEN NEW %x 32766";
168                 else
169                         extrastr= NULL;
170
171                 duptemplate= MEM_dupallocN(template);
172                 IDnames_to_pupstring(&str, NULL, extrastr, lb, idptr.data, &duptemplate->browse);
173
174                 but= uiDefButS(block, MENU, 0, str, x, y, UI_UNIT_X, UI_UNIT_Y, &duptemplate->browse, 0, 0, 0, 0, "Browse existing choices, or add new");
175                 uiButSetNFunc(but, template_header_id_cb, duptemplate, SET_INT_IN_POINTER(UI_ID_BROWSE));
176                 x+= UI_UNIT_X;
177         
178                 MEM_freeN(str);
179         }
180
181         /* text button with name */
182         if(idptr.data) {
183                 char name[64];
184
185                 text_idbutton(idptr.data, name);
186                 but= uiDefButR(block, TEX, 0, name, x, y, UI_UNIT_X*6, UI_UNIT_Y, &idptr, "name", -1, 0, 0, -1, -1, NULL);
187                 uiButSetNFunc(but, template_header_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_RENAME));
188                 x += UI_UNIT_X*6;
189
190                 /* delete button */
191                 if(template->flag & UI_ID_DELETE) {
192                         but= uiDefIconButO(block, BUT, template->unlinkop, WM_OP_EXEC_REGION_WIN, ICON_X, x, y, UI_UNIT_X, UI_UNIT_Y, NULL);
193                         x += UI_UNIT_X;
194                 }
195         }
196         uiBlockEndAlign(block);
197 }
198
199 void uiTemplateHeaderID(uiLayout *layout, bContext *C, PointerRNA *ptr, char *propname, char *newop, char *openop, char *unlinkop)
200 {
201         TemplateHeaderID *template;
202         uiBlock *block;
203         PropertyRNA *prop;
204
205         if(!ptr->data)
206                 return;
207
208         prop= RNA_struct_find_property(ptr, propname);
209
210         if(!prop) {
211                 printf("uiTemplateHeaderID: property not found: %s\n", propname);
212                 return;
213         }
214
215         template= MEM_callocN(sizeof(TemplateHeaderID), "TemplateHeaderID");
216         template->ptr= *ptr;
217         template->prop= prop;
218         template->flag= UI_ID_BROWSE|UI_ID_RENAME;
219
220         if(newop) {
221                 template->flag |= UI_ID_ADD_NEW;
222                 BLI_strncpy(template->newop, newop, sizeof(template->newop));
223         }
224         if(openop) {
225                 template->flag |= UI_ID_OPEN;
226                 BLI_strncpy(template->openop, openop, sizeof(template->openop));
227         }
228         if(unlinkop) {
229                 template->flag |= UI_ID_DELETE;
230                 BLI_strncpy(template->unlinkop, unlinkop, sizeof(template->unlinkop));
231         }
232
233         block= uiLayoutFreeBlock(layout);
234         template_header_ID(C, block, template);
235
236         MEM_freeN(template);
237 }
238
239 /************************ Modifier Template *************************/
240
241 #define ERROR_LIBDATA_MESSAGE "Can't edit external libdata"
242
243 #include <string.h>
244
245 #include "DNA_object_force.h"
246 #include "DNA_object_types.h"
247 #include "DNA_modifier_types.h"
248 #include "DNA_scene_types.h"
249
250 #include "BKE_depsgraph.h"
251 #include "BKE_DerivedMesh.h"
252 #include "BKE_global.h"
253 #include "BKE_modifier.h"
254 #include "BKE_object.h"
255 #include "BKE_particle.h"
256 #include "BKE_report.h"
257
258 #include "UI_resources.h"
259 #include "ED_util.h"
260
261 #include "BLI_arithb.h"
262 #include "BLI_listbase.h"
263
264 #include "ED_object.h"
265
266 static void modifiers_del(bContext *C, void *ob_v, void *md_v)
267 {
268         Scene *scene= CTX_data_scene(C);
269         Object *ob= ob_v;
270         ReportList reports;
271
272         BKE_reports_init(&reports, RPT_STORE);
273
274         if(ED_object_modifier_delete(&reports, ob_v, md_v)) {
275                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
276                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
277
278                 ED_undo_push(C, "Delete modifier");
279         }
280         else
281                 uiPupMenuReports(C, &reports);
282
283         BKE_reports_clear(&reports);
284 }
285
286 static void modifiers_moveUp(bContext *C, void *ob_v, void *md_v)
287 {
288         Scene *scene= CTX_data_scene(C);
289         Object *ob= ob_v;
290         ReportList reports;
291
292         BKE_reports_init(&reports, RPT_STORE);
293
294         if(ED_object_modifier_move_up(&reports, ob_v, md_v)) {
295                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
296                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
297
298                 ED_undo_push(C, "Move modifier");
299         }
300         else
301                 uiPupMenuReports(C, &reports);
302
303         BKE_reports_clear(&reports);
304 }
305
306 static void modifiers_moveDown(bContext *C, void *ob_v, void *md_v)
307 {
308         Scene *scene= CTX_data_scene(C);
309         Object *ob= ob_v;
310         ReportList reports;
311
312         BKE_reports_init(&reports, RPT_STORE);
313
314         if(ED_object_modifier_move_down(&reports, ob_v, md_v)) {
315                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
316                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
317
318                 ED_undo_push(C, "Move modifier");
319         }
320         else
321                 uiPupMenuReports(C, &reports);
322
323         BKE_reports_clear(&reports);
324 }
325
326 static void modifiers_convertParticles(bContext *C, void *obv, void *mdv)
327 {
328         Scene *scene= CTX_data_scene(C);
329         Object *ob= obv;
330         ReportList reports;
331
332         BKE_reports_init(&reports, RPT_STORE);
333
334         if(ED_object_modifier_convert(&reports, scene, obv, mdv)) {
335                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
336                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
337
338                 ED_undo_push(C, "Convert particles to mesh object(s).");
339         }
340         else
341                 uiPupMenuReports(C, &reports);
342
343         BKE_reports_clear(&reports);
344 }
345
346 static void modifiers_applyModifier(bContext *C, void *obv, void *mdv)
347 {
348         Scene *scene= CTX_data_scene(C);
349         Object *ob= obv;
350         ReportList reports;
351
352         BKE_reports_init(&reports, RPT_STORE);
353
354         if(ED_object_modifier_apply(&reports, scene, obv, mdv)) {
355                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
356                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
357
358                 ED_undo_push(C, "Apply modifier");
359         }
360         else
361                 uiPupMenuReports(C, &reports);
362
363         BKE_reports_clear(&reports);
364 }
365
366 static void modifiers_copyModifier(bContext *C, void *ob_v, void *md_v)
367 {
368         Scene *scene= CTX_data_scene(C);
369         Object *ob= ob_v;
370         ReportList reports;
371
372         BKE_reports_init(&reports, RPT_STORE);
373
374         if(ED_object_modifier_copy(&reports, ob_v, md_v)) {
375                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
376                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
377                 ED_undo_push(C, "Copy modifier");
378         }
379         else
380                 uiPupMenuReports(C, &reports);
381
382         BKE_reports_clear(&reports);
383 }
384
385 static void modifiers_setOnCage(bContext *C, void *ob_v, void *md_v)
386 {
387         Scene *scene= CTX_data_scene(C);
388         Object *ob = ob_v;
389         ModifierData *md;
390         
391         int i, cageIndex = modifiers_getCageIndex(ob, NULL );
392
393         for(i = 0, md=ob->modifiers.first; md; ++i, md=md->next) {
394                 if(md == md_v) {
395                         if(i >= cageIndex)
396                                 md->mode ^= eModifierMode_OnCage;
397                         break;
398                 }
399         }
400
401         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
402         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
403 }
404
405 static void modifiers_convertToReal(bContext *C, void *ob_v, void *md_v)
406 {
407         Scene *scene= CTX_data_scene(C);
408         Object *ob = ob_v;
409         ModifierData *md = md_v;
410         ModifierData *nmd = modifier_new(md->type);
411
412         modifier_copyData(md, nmd);
413         nmd->mode &= ~eModifierMode_Virtual;
414
415         BLI_addhead(&ob->modifiers, nmd);
416
417         ob->partype = PAROBJECT;
418
419         WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
420         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
421
422         ED_undo_push(C, "Modifier convert to real");
423 }
424
425 static int modifier_can_delete(ModifierData *md)
426 {
427         // deletion over the deflection panel
428         // fluid particle modifier can't be deleted here
429
430         if(md->type==eModifierType_Fluidsim)
431                 return 0;
432         if(md->type==eModifierType_Collision)
433                 return 0;
434         if(md->type==eModifierType_Surface)
435                 return 0;
436         if(md->type == eModifierType_ParticleSystem)
437                 if(((ParticleSystemModifierData *)md)->psys->part->type == PART_FLUID)
438                         return 0;
439
440         return 1;
441 }
442
443 static uiLayout *draw_modifier(uiLayout *layout, Object *ob, ModifierData *md, int index, int cageIndex, int lastCageIndex)
444 {
445         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
446         PointerRNA ptr;
447         uiBut *but;
448         uiBlock *block;
449         uiLayout *column, *row, *subrow, *result= NULL;
450         int isVirtual = md->mode&eModifierMode_Virtual;
451         // XXX short color = md->error?TH_REDALERT:TH_BUT_NEUTRAL;
452         short width = 295, buttonWidth = width-120-10;
453         char str[128];
454
455         RNA_pointer_create(&ob->id, &RNA_Modifier, md, &ptr);
456
457         column= uiLayoutColumn(layout, 1);
458         uiLayoutSetContextPointer(column, "modifier", &ptr);
459
460         /* rounded header */
461         /* XXX uiBlockSetCol(block, color); */
462                 /* roundbox 4 free variables: corner-rounding, nop, roundbox type, shade */
463
464         row= uiLayoutRow(uiLayoutBox(column), 0);
465         block= uiLayoutGetBlock(row);
466
467         subrow= uiLayoutRow(row, 0);
468         uiLayoutSetAlignment(subrow, UI_LAYOUT_ALIGN_LEFT);
469
470         //uiDefBut(block, ROUNDBOX, 0, "", x-10, y-4, width, 25, NULL, 7.0, 0.0, 
471         //               (!isVirtual && (md->mode&eModifierMode_Expanded))?3:15, 20, ""); 
472         /* XXX uiBlockSetCol(block, TH_AUTO); */
473         
474         /* open/close icon */
475         if (!isVirtual) {
476                 uiBlockSetEmboss(block, UI_EMBOSSN);
477                 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");
478         }
479
480         uiBlockSetEmboss(block, UI_EMBOSS);
481         
482         if (isVirtual) {
483                 sprintf(str, "%s parent deform", md->name);
484                 uiDefBut(block, LABEL, 0, str, 0, 0, 185, UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, "Modifier name"); 
485
486                 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");
487                 uiButSetFunc(but, modifiers_convertToReal, ob, md);
488         } else {
489                 uiBlockBeginAlign(block);
490                 uiDefBut(block, TEX, 0, "", 0, 0, buttonWidth-60, UI_UNIT_Y, md->name, 0.0, sizeof(md->name)-1, 0.0, 0.0, "Modifier name"); 
491
492                 /* Softbody not allowed in this situation, enforce! */
493                 if (((md->type!=eModifierType_Softbody && md->type!=eModifierType_Collision) || !(ob->pd && ob->pd->deflect)) && (md->type!=eModifierType_Surface)) {
494                         uiDefIconButBitI(block, TOG, eModifierMode_Render, 0, ICON_SCENE, 0, 0, 19, UI_UNIT_Y,&md->mode, 0, 0, 1, 0, "Enable modifier during rendering");
495                         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");
496                         if (mti->flags&eModifierTypeFlag_SupportsEditmode) {
497                                 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)");
498                         }
499                 }
500                 uiBlockEndAlign(block);
501
502                 /* XXX uiBlockSetEmboss(block, UI_EMBOSSR); */
503
504                 if (ob->type==OB_MESH && modifier_couldBeCage(md) && index<=lastCageIndex) {
505                         int icon; //, color;
506
507                         if (index==cageIndex) {
508                                 // XXX color = TH_BUT_SETTING;
509                                 icon = VICON_EDITMODE_HLT;
510                         } else if (index<cageIndex) {
511                                 // XXX color = TH_BUT_NEUTRAL;
512                                 icon = VICON_EDITMODE_DEHLT;
513                         } else {
514                                 // XXX color = TH_BUT_NEUTRAL;
515                                 icon = ICON_BLANK1;
516                         }
517                         /* XXX uiBlockSetCol(block, color); */
518                         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");
519                         uiButSetFunc(but, modifiers_setOnCage, ob, md);
520                         /* XXX uiBlockSetCol(block, TH_AUTO); */
521                 }
522         }
523
524         subrow= uiLayoutRow(row, 0);
525         uiLayoutSetAlignment(subrow, UI_LAYOUT_ALIGN_RIGHT);
526
527         if(!isVirtual) {
528                 /* XXX uiBlockSetCol(block, TH_BUT_ACTION); */
529
530                 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");
531                 uiButSetFunc(but, modifiers_moveUp, ob, md);
532
533                 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");
534                 uiButSetFunc(but, modifiers_moveDown, ob, md);
535                 
536                 uiBlockSetEmboss(block, UI_EMBOSSN);
537                 
538                 if(modifier_can_delete(md)) {
539                         but = uiDefIconBut(block, BUT, 0, VICON_X, 0, 0, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Delete modifier");
540                         uiButSetFunc(but, modifiers_del, ob, md);
541                 }
542                 /* XXX uiBlockSetCol(block, TH_AUTO); */
543         }
544
545         uiBlockSetEmboss(block, UI_EMBOSS);
546
547         if(!isVirtual && (md->mode&eModifierMode_Expanded)) {
548                 uiLayout *box;
549
550                 box= uiLayoutBox(column);
551                 row= uiLayoutRow(box, 1);
552
553                 if (!isVirtual && (md->type!=eModifierType_Collision) && (md->type!=eModifierType_Surface)) {
554                         uiBlockSetButLock(block, object_data_is_libdata(ob), ERROR_LIBDATA_MESSAGE); /* only here obdata, the rest of modifiers is ob level */
555
556                                                 if (md->type==eModifierType_ParticleSystem) {
557                         ParticleSystem *psys= ((ParticleSystemModifierData *)md)->psys;
558
559                         if(!(G.f & G_PARTICLEEDIT)) {
560                                         if(ELEM3(psys->part->draw_as, PART_DRAW_PATH, PART_DRAW_GR, PART_DRAW_OB) && psys->pathcache) {
561                                                 but = uiDefBut(block, BUT, 0, "Convert",        0,0,60,19, 0, 0, 0, 0, 0, "Convert the current particles to a mesh object");
562                                                 uiButSetFunc(but, modifiers_convertParticles, ob, md);
563                                         }
564                                 }
565                         }
566                         else{
567                                 but = uiDefBut(block, BUT, 0, "Apply",  0,0,60,19, 0, 0, 0, 0, 0, "Apply the current modifier and remove from the stack");
568                                 uiButSetFunc(but, modifiers_applyModifier, ob, md);
569                         }
570                         
571                         uiBlockClearButLock(block);
572                         uiBlockSetButLock(block, ob && ob->id.lib, ERROR_LIBDATA_MESSAGE);
573
574                         if (md->type!=eModifierType_Fluidsim && md->type!=eModifierType_Softbody && md->type!=eModifierType_ParticleSystem && (md->type!=eModifierType_Cloth)) {
575                                 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");
576                                 uiButSetFunc(but, modifiers_copyModifier, ob, md);
577                         }
578                 }
579
580                 result= uiLayoutColumn(box, 0);
581                 block= uiLayoutFreeBlock(box);
582         }
583
584         if (md->error) {
585                 row = uiLayoutRow(uiLayoutBox(column), 0);
586
587                 /* XXX uiBlockSetCol(block, color); */
588                 uiItemL(row, md->error, ICON_ERROR);
589                 /* XXX uiBlockSetCol(block, TH_AUTO); */
590         }
591
592         return result;
593 }
594
595 uiLayout *uiTemplateModifier(uiLayout *layout, PointerRNA *ptr)
596 {
597         Object *ob;
598         ModifierData *md, *vmd;
599         int i, lastCageIndex, cageIndex;
600
601         /* verify we have valid data */
602         if(!RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
603                 printf("uiTemplateModifier: expected modifier on object.\n");
604                 return NULL;
605         }
606
607         ob= ptr->id.data;
608         md= ptr->data;
609
610         if(!ob || !(GS(ob->id.name) == ID_OB)) {
611                 printf("uiTemplateModifier: expected modifier on object.\n");
612                 return NULL;
613         }
614         
615         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
616         
617         /* find modifier and draw it */
618         cageIndex = modifiers_getCageIndex(ob, &lastCageIndex);
619
620         // XXX virtual modifiers are not accesible for python
621         vmd = modifiers_getVirtualModifierList(ob);
622
623         for(i=0; vmd; i++, vmd=vmd->next) {
624                 if(md == vmd)
625                         return draw_modifier(layout, ob, md, i, cageIndex, lastCageIndex);
626                 else if(vmd->mode&eModifierMode_Virtual)
627                         i--;
628         }
629
630         return NULL;
631 }
632
633 /************************ Constraint Template *************************/
634
635 #include "DNA_action_types.h"
636 #include "DNA_constraint_types.h"
637
638 #include "BKE_action.h"
639 #include "BKE_constraint.h"
640
641 #define REDRAWIPO                                       1
642 #define REDRAWNLA                                       2
643 #define REDRAWBUTSOBJECT                        3               
644 #define REDRAWACTION                            4
645 #define B_CONSTRAINT_TEST                       5
646 #define B_CONSTRAINT_CHANGETARGET       6
647 #define B_CONSTRAINT_INF                        7
648 #define REMAKEIPO                                       8
649 #define B_DIFF                                          9
650
651 void do_constraint_panels(bContext *C, void *arg, int event)
652 {
653         Scene *scene= CTX_data_scene(C);
654         Object *ob= CTX_data_active_object(C);
655         
656         switch(event) {
657         case B_CONSTRAINT_TEST:
658                 // XXX allqueue(REDRAWVIEW3D, 0);
659                 // XXX allqueue(REDRAWBUTSOBJECT, 0);
660                 // XXX allqueue(REDRAWBUTSEDIT, 0);
661                 break;  // no handling
662         case B_CONSTRAINT_INF:
663                 /* influence; do not execute actions for 1 dag_flush */
664                 if (ob->pose)
665                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
666                 break;
667         case B_CONSTRAINT_CHANGETARGET:
668                 if (ob->pose) ob->pose->flag |= POSE_RECALC;    // checks & sorts pose channels
669                 DAG_scene_sort(scene);
670                 break;
671         default:
672                 break;
673         }
674
675         object_test_constraints(ob);
676         
677         if(ob->pose) update_pose_constraint_flags(ob->pose);
678         
679         if(ob->type==OB_ARMATURE) DAG_object_flush_update(scene, ob, OB_RECALC_DATA|OB_RECALC_OB);
680         else DAG_object_flush_update(scene, ob, OB_RECALC_OB);
681         
682         // XXX allqueue(REDRAWVIEW3D, 0);
683         // XXX allqueue(REDRAWBUTSOBJECT, 0);
684         // XXX allqueue(REDRAWBUTSEDIT, 0);
685 }
686
687 static void constraint_active_func(bContext *C, void *ob_v, void *con_v)
688 {
689         ED_object_constraint_set_active(ob_v, con_v);
690 }
691
692 static void del_constraint_func (bContext *C, void *ob_v, void *con_v)
693 {
694         if(ED_object_constraint_delete(NULL, ob_v, con_v))
695                 ED_undo_push(C, "Delete Constraint");
696 }
697
698 static void verify_constraint_name_func (bContext *C, void *con_v, void *name_v)
699 {
700         Object *ob= CTX_data_active_object(C);
701         bConstraint *con= con_v;
702         char oldname[32];       
703         
704         if (!con)
705                 return;
706         
707         /* put on the stack */
708         BLI_strncpy(oldname, (char *)name_v, 32);
709         
710         ED_object_constraint_rename(ob, con, oldname);
711         ED_object_constraint_set_active(ob, con);
712         // XXX allqueue(REDRAWACTION, 0); 
713 }
714
715 static void constraint_moveUp(bContext *C, void *ob_v, void *con_v)
716 {
717         if(ED_object_constraint_move_up(NULL, ob_v, con_v))
718                 ED_undo_push(C, "Move Constraint");
719 }
720
721 static void constraint_moveDown(bContext *C, void *ob_v, void *con_v)
722 {
723         if(ED_object_constraint_move_down(NULL, ob_v, con_v))
724                 ED_undo_push(C, "Move Constraint");
725 }
726
727 /* some commonly used macros in the constraints drawing code */
728 #define is_armature_target(target) (target && target->type==OB_ARMATURE)
729 #define is_armature_owner(ob) ((ob->type == OB_ARMATURE) && (ob->flag & OB_POSEMODE))
730 #define is_geom_target(target) (target && (ELEM(target->type, OB_MESH, OB_LATTICE)) )
731
732 /* Helper function for draw constraint - draws constraint space stuff 
733  * This function should not be called if no menus are required 
734  * owner/target: -1 = don't draw menu; 0= not posemode, 1 = posemode 
735  */
736 static void draw_constraint_spaceselect (uiBlock *block, bConstraint *con, short xco, short yco, short owner, short target)
737 {
738         short tarx, ownx, iconx;
739         short bwidth;
740         short iconwidth = 20;
741         
742         /* calculate sizes and placement of menus */
743         if (owner == -1) {
744                 bwidth = 125;
745                 tarx = 120;
746                 ownx = 0;
747         }
748         else if (target == -1) {
749                 bwidth = 125;
750                 tarx = 0;
751                 ownx = 120;
752         }
753         else {
754                 bwidth = 100;
755                 tarx = 85;
756                 iconx = tarx + bwidth + 5;
757                 ownx = tarx + bwidth + iconwidth + 10;
758         }
759         
760         
761         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Convert:", xco, yco, 80,18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
762
763         /* Target-Space */
764         if (target == 1) {
765                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Pose Space %x2|Local with Parent %x3|Local Space %x1", 
766                                                                                                 tarx, yco, bwidth, 18, &con->tarspace, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
767         }
768         else if (target == 0) {
769                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Local (Without Parent) Space %x1", 
770                                                                                 tarx, yco, bwidth, 18, &con->tarspace, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
771         }
772         
773         if ((target != -1) && (owner != -1))
774                 uiDefIconBut(block, LABEL, 0, ICON_ARROW_LEFTRIGHT,
775                         iconx, yco, 20, 20, NULL, 0.0, 0.0, 0.0, 0.0, "");
776         
777         /* Owner-Space */
778         if (owner == 1) {
779                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Owner Space %t|World Space %x0|Pose Space %x2|Local with Parent %x3|Local Space %x1", 
780                                                                                                 ownx, yco, bwidth, 18, &con->ownspace, 0, 0, 0, 0, "Choose space that owner is evaluated in");  
781         }
782         else if (owner == 0) {
783                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Owner Space %t|World Space %x0|Local (Without Parent) Space %x1", 
784                                                                                 ownx, yco, bwidth, 18, &con->ownspace, 0, 0, 0, 0, "Choose space that owner is evaluated in");  
785         }
786 }
787
788 /* draw panel showing settings for a constraint */
789 static uiLayout *draw_constraint(uiLayout *layout, Object *ob, bConstraint *con)
790 {
791         bPoseChannel *pchan= get_active_posechannel(ob);
792         bConstraintTypeInfo *cti;
793         uiBlock *block;
794         uiLayout *result= NULL, *col, *box, *row, *subrow;
795         uiBut *but;
796         PointerRNA ptr;
797         char typestr[32];
798         short width = 265;
799         short proxy_protected, xco=0, yco=0;
800         int rb_col;
801
802         /* get constraint typeinfo */
803         cti= constraint_get_typeinfo(con);
804         if (cti == NULL) {
805                 /* exception for 'Null' constraint - it doesn't have constraint typeinfo! */
806                 if (con->type == CONSTRAINT_TYPE_NULL)
807                         strcpy(typestr, "Null");
808                 else
809                         strcpy(typestr, "Unknown");
810         }
811         else
812                 strcpy(typestr, cti->name);
813                 
814         /* determine whether constraint is proxy protected or not */
815         if (proxylocked_constraints_owner(ob, pchan))
816                 proxy_protected= (con->flag & CONSTRAINT_PROXY_LOCAL)==0;
817         else
818                 proxy_protected= 0;
819
820         /* unless button has own callback, it adds this callback to button */
821         block= uiLayoutGetBlock(layout);
822         uiBlockSetHandleFunc(block, do_constraint_panels, NULL);
823         uiBlockSetFunc(block, constraint_active_func, ob, con);
824
825         RNA_pointer_create(&ob->id, &RNA_Constraint, con, &ptr);
826
827         col= uiLayoutColumn(layout, 1);
828         uiLayoutSetContextPointer(col, "constraint", &ptr);
829
830         box= uiLayoutBox(col);
831         row= uiLayoutRow(box, 0);
832
833         block= uiLayoutFreeBlock(box);
834
835         subrow= uiLayoutRow(row, 0);
836         uiLayoutSetAlignment(subrow, UI_LAYOUT_ALIGN_LEFT);
837
838         /* Draw constraint header */
839         uiBlockSetEmboss(block, UI_EMBOSSN);
840         
841         /* rounded header */
842         rb_col= (con->flag & CONSTRAINT_ACTIVE)?50:20;
843         
844         /* open/close */
845         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");
846         
847         /* name */      
848         if ((con->flag & CONSTRAINT_EXPAND) && (proxy_protected==0)) {
849                 /* XXX if (con->flag & CONSTRAINT_DISABLE)
850                         uiBlockSetCol(block, TH_REDALERT);*/
851                 
852                 uiBlockSetEmboss(block, UI_EMBOSS);
853                 
854                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr, xco+10, yco, 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
855                 
856                 but = uiDefBut(block, TEX, B_CONSTRAINT_TEST, "", xco+120, yco, 85, 18, con->name, 0.0, 29.0, 0.0, 0.0, "Constraint name"); 
857                 uiButSetFunc(but, verify_constraint_name_func, con, NULL);
858         }       
859         else {
860                 uiBlockSetEmboss(block, UI_EMBOSSN);
861                 
862                 /* XXX if (con->flag & CONSTRAINT_DISABLE)
863                         uiBlockSetCol(block, TH_REDALERT);*/
864                 
865                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr, xco+10, yco, 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
866                 
867                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, con->name, xco+120, yco-1, 135, 19, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
868         }
869
870         // XXX uiBlockSetCol(block, TH_AUTO);   
871
872         subrow= uiLayoutRow(row, 0);
873         uiLayoutSetAlignment(subrow, UI_LAYOUT_ALIGN_RIGHT);
874         
875         /* proxy-protected constraints cannot be edited, so hide up/down + close buttons */
876         if (proxy_protected) {
877                 uiBlockSetEmboss(block, UI_EMBOSSN);
878                 
879                 /* draw a ghost icon (for proxy) and also a lock beside it, to show that constraint is "proxy locked" */
880                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_GHOST, xco+244, yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Proxy Protected");
881                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_LOCKED, xco+262, yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Proxy Protected");
882                 
883                 uiBlockSetEmboss(block, UI_EMBOSS);
884         }
885         else {
886                 short prev_proxylock, show_upbut, show_downbut;
887                 
888                 /* Up/Down buttons: 
889                  *      Proxy-constraints are not allowed to occur after local (non-proxy) constraints
890                  *      as that poses problems when restoring them, so disable the "up" button where
891                  *      it may cause this situation. 
892                  *
893                  *      Up/Down buttons should only be shown (or not greyed - todo) if they serve some purpose. 
894                  */
895                 if (proxylocked_constraints_owner(ob, pchan)) {
896                         if (con->prev) {
897                                 prev_proxylock= (con->prev->flag & CONSTRAINT_PROXY_LOCAL) ? 0 : 1;
898                         }
899                         else
900                                 prev_proxylock= 0;
901                 }
902                 else
903                         prev_proxylock= 0;
904                         
905                 show_upbut= ((prev_proxylock == 0) && (con->prev));
906                 show_downbut= (con->next) ? 1 : 0;
907                 
908                 if (show_upbut || show_downbut) {
909                         uiBlockBeginAlign(block);
910                                 uiBlockSetEmboss(block, UI_EMBOSS);
911                                 
912                                 if (show_upbut) {
913                                         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");
914                                         uiButSetFunc(but, constraint_moveUp, ob, con);
915                                 }
916                                 
917                                 if (show_downbut) {
918                                         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");
919                                         uiButSetFunc(but, constraint_moveDown, ob, con);
920                                 }
921                         uiBlockEndAlign(block);
922                 }
923                 
924                 
925                 /* Close 'button' - emboss calls here disable drawing of 'button' behind X */
926                 uiBlockSetEmboss(block, UI_EMBOSSN);
927                 
928                 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");
929                 uiButSetFunc(but, del_constraint_func, ob, con);
930                 
931                 uiBlockSetEmboss(block, UI_EMBOSS);
932         }
933         
934         /* Set but-locks for protected settings (magic numbers are used here!) */
935         if (proxy_protected)
936                 uiBlockSetButLock(block, 1, "Cannot edit Proxy-Protected Constraint");
937         
938         /* Draw constraint data */
939         if ((con->flag & CONSTRAINT_EXPAND) == 0) {
940                 (yco) -= 21;
941         }
942         else {
943                 box= uiLayoutBox(col);
944                 block= uiLayoutFreeBlock(box);
945
946                 switch (con->type) {
947 #ifndef DISABLE_PYTHON
948                 case CONSTRAINT_TYPE_PYTHON:
949                         {
950                                 bPythonConstraint *data = con->data;
951                                 bConstraintTarget *ct;
952                                 // uiBut *but2;
953                                 int tarnum, theight;
954                                 // static int pyconindex=0;
955                                 // char *menustr;
956                                 
957                                 theight = (data->tarnum)? (data->tarnum * 38) : (38);
958                                 
959                                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Script:", xco+60, yco-24, 55, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
960                                 
961                                 /* do the scripts menu */
962                                 /* XXX menustr = buildmenu_pyconstraints(data->text, &pyconindex);
963                                 but2 = uiDefButI(block, MENU, B_CONSTRAINT_TEST, menustr,
964                                       xco+120, yco-24, 150, 20, &pyconindex,
965                                       0, 0, 0, 0, "Set the Script Constraint to use");
966                                 uiButSetFunc(but2, validate_pyconstraint_cb, data, &pyconindex);
967                                 MEM_freeN(menustr);      */
968                                 
969                                 /* draw target(s) */
970                                 if (data->flag & PYCON_USETARGETS) {
971                                         /* Draw target parameters */ 
972                                         for (ct=data->targets.first, tarnum=1; ct; ct=ct->next, tarnum++) {
973                                                 char tarstr[32];
974                                                 short yoffset= ((tarnum-1) * 38);
975         
976                                                 /* target label */
977                                                 sprintf(tarstr, "Target %d:", tarnum);
978                                                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, tarstr, xco+45, yco-(48+yoffset), 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
979                                                 
980                                                 /* target space-selector - per target */
981                                                 if (is_armature_target(ct->tar)) {
982                                                         uiDefButS(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Pose Space %x3|Local with Parent %x4|Local Space %x1", 
983                                                                                                                         xco+10, yco-(66+yoffset), 100, 18, &ct->space, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
984                                                 }
985                                                 else {
986                                                         uiDefButS(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Local (Without Parent) Space %x1", 
987                                                                                                                         xco+10, yco-(66+yoffset), 100, 18, &ct->space, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
988                                                 }
989                                                 
990                                                 uiBlockBeginAlign(block);
991                                                         /* target object */
992                                                         uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_CONSTRAINT_CHANGETARGET, "OB:", xco+120, yco-(48+yoffset), 150, 18, &ct->tar, "Target Object"); 
993                                                         
994                                                         /* subtarget */
995                                                         if (is_armature_target(ct->tar)) {
996                                                                 but= uiDefBut(block, TEX, B_CONSTRAINT_CHANGETARGET, "BO:", xco+120, yco-(66+yoffset),150,18, &ct->subtarget, 0, 24, 0, 0, "Subtarget Bone");
997                                                                 uiButSetCompleteFunc(but, autocomplete_bone, (void *)ct->tar);
998                                                         }
999                                                         else if (is_geom_target(ct->tar)) {
1000                                                                 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");
1001                                                                 uiButSetCompleteFunc(but, autocomplete_vgroup, (void *)ct->tar);
1002                                                         }
1003                                                         else {
1004                                                                 strcpy(ct->subtarget, "");
1005                                                         }
1006                                                 uiBlockEndAlign(block);
1007                                         }
1008                                 }
1009                                 else {
1010                                         /* Draw indication that no target needed */
1011                                         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Target:", xco+60, yco-48, 55, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1012                                         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Not Applicable", xco+120, yco-48, 150, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1013                                 }
1014                                 
1015                                 /* settings */
1016                                 uiBlockBeginAlign(block);
1017                                         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.");
1018                                         // XXX uiButSetFunc(but, BPY_pyconstraint_settings, data, NULL);
1019                                         
1020                                         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");
1021                                 uiBlockEndAlign(block);
1022                                 
1023                                 /* constraint space settings */
1024                                 draw_constraint_spaceselect(block, con, xco, yco-(73+theight), is_armature_owner(ob), -1);
1025                         }
1026                         break;
1027 #endif /* DISABLE_PYTHON */
1028                 /*case CONSTRAINT_TYPE_CHILDOF:
1029                         {
1030                                 // Inverse options 
1031                                 uiBlockBeginAlign(block);
1032                                         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)");
1033                                         // XXX uiButSetFunc(but, childof_const_setinv, con, NULL);
1034                                         
1035                                         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)");
1036                                         // XXX uiButSetFunc(but, childof_const_clearinv, con, NULL);
1037                                 uiBlockEndAlign(block);
1038                         }
1039                         break; 
1040                 */
1041                 
1042                 /*case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1043                         {
1044                                 if (data->type==CONSTRAINT_RB_GENERIC6DOF) {
1045                                         // Draw Pairs of LimitToggle+LimitValue 
1046                                         uiBlockBeginAlign(block); 
1047                                                 uiDefButBitS(block, TOG, 1, B_CONSTRAINT_TEST, "LinMinX", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum x limit"); 
1048                                                 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"); 
1049                                         uiBlockEndAlign(block);
1050                                         
1051                                         uiBlockBeginAlign(block); 
1052                                                 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"); 
1053                                                 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"); 
1054                                         uiBlockEndAlign(block);
1055                                         
1056                                         offsetY += 20;
1057                                         uiBlockBeginAlign(block); 
1058                                                 uiDefButBitS(block, TOG, 2, B_CONSTRAINT_TEST, "LinMinY", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum y limit"); 
1059                                                 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"); 
1060                                         uiBlockEndAlign(block);
1061                                         
1062                                         uiBlockBeginAlign(block); 
1063                                                 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"); 
1064                                                 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"); 
1065                                         uiBlockEndAlign(block);
1066                                         
1067                                         offsetY += 20;
1068                                         uiBlockBeginAlign(block); 
1069                                                 uiDefButBitS(block, TOG, 4, B_CONSTRAINT_TEST, "LinMinZ", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum z limit"); 
1070                                                 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"); 
1071                                         uiBlockEndAlign(block);
1072                                         
1073                                         uiBlockBeginAlign(block); 
1074                                                 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"); 
1075                                                 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"); 
1076                                         uiBlockEndAlign(block);
1077                                         offsetY += 20;
1078                                 }
1079                                 if ((data->type==CONSTRAINT_RB_GENERIC6DOF) || (data->type==CONSTRAINT_RB_CONETWIST)) {
1080                                         // Draw Pairs of LimitToggle+LimitValue /
1081                                         uiBlockBeginAlign(block); 
1082                                                 uiDefButBitS(block, TOG, 8, B_CONSTRAINT_TEST, "AngMinX", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum x limit"); 
1083                                                 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"); 
1084                                         uiBlockEndAlign(block);
1085                                         uiBlockBeginAlign(block); 
1086                                                 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"); 
1087                                                 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"); 
1088                                         uiBlockEndAlign(block);
1089                                         
1090                                         offsetY += 20;
1091                                         uiBlockBeginAlign(block); 
1092                                                 uiDefButBitS(block, TOG, 16, B_CONSTRAINT_TEST, "AngMinY", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum y limit"); 
1093                                                 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"); 
1094                                         uiBlockEndAlign(block);
1095                                         
1096                                         uiBlockBeginAlign(block); 
1097                                                 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"); 
1098                                                 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"); 
1099                                         uiBlockEndAlign(block);
1100                                         
1101                                         offsetY += 20;
1102                                         uiBlockBeginAlign(block); 
1103                                                 uiDefButBitS(block, TOG, 32, B_CONSTRAINT_TEST, "AngMinZ", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum z limit"); 
1104                                                 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"); 
1105                                         uiBlockEndAlign(block);
1106                                         
1107                                         uiBlockBeginAlign(block); 
1108                                                 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"); 
1109                                                 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"); 
1110                                         uiBlockEndAlign(block);
1111                                 }
1112                                 
1113                         }
1114                         break;
1115                         */
1116
1117                 case CONSTRAINT_TYPE_NULL:
1118                         {
1119                                 uiItemL(box, "", 0);
1120                         }
1121                         break;
1122                 default:
1123                         result= box;
1124                         break;
1125                 }
1126         }
1127         
1128         /* clear any locks set up for proxies/lib-linking */
1129         uiBlockClearButLock(block);
1130
1131         return result;
1132 }
1133
1134 uiLayout *uiTemplateConstraint(uiLayout *layout, PointerRNA *ptr)
1135 {
1136         Object *ob;
1137         bConstraint *con;
1138
1139         /* verify we have valid data */
1140         if(!RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
1141                 printf("uiTemplateConstraint: expected constraint on object.\n");
1142                 return NULL;
1143         }
1144
1145         ob= ptr->id.data;
1146         con= ptr->data;
1147
1148         if(!ob || !(GS(ob->id.name) == ID_OB)) {
1149                 printf("uiTemplateConstraint: expected constraint on object.\n");
1150                 return NULL;
1151         }
1152         
1153         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1154
1155         /* hrms, the temporal constraint should not draw! */
1156         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
1157                 bKinematicConstraint *data= con->data;
1158                 if(data->flag & CONSTRAINT_IK_TEMP)
1159                         return NULL;
1160         }
1161
1162         return draw_constraint(layout, ob, con);
1163 }
1164
1165 /************************* Group Template ***************************/
1166
1167 #if 0
1168 static void do_add_groupmenu(void *arg, int event)
1169 {
1170         Object *ob= OBACT;
1171         
1172         if(ob) {
1173                 
1174                 if(event== -1) {
1175                         Group *group= add_group( "Group" );
1176                         add_to_group(group, ob);
1177                 }
1178                 else
1179                         add_to_group(BLI_findlink(&G.main->group, event), ob);
1180                         
1181                 ob->flag |= OB_FROMGROUP;
1182                 BASACT->flag |= OB_FROMGROUP;
1183                 allqueue(REDRAWBUTSOBJECT, 0);
1184                 allqueue(REDRAWVIEW3D, 0);
1185         }               
1186 }
1187
1188 static uiBlock *add_groupmenu(void *arg_unused)
1189 {
1190         uiBlock *block;
1191         Group *group;
1192         short xco=0, yco= 0, index=0;
1193         char str[32];
1194         
1195         block= uiNewBlock(&curarea->uiblocks, "add_constraintmenu", UI_EMBOSSP, UI_HELV, curarea->win);
1196         uiBlockSetButmFunc(block, do_add_groupmenu, NULL);
1197
1198         uiDefBut(block, BUTM, B_NOP, "ADD NEW",         0, 20, 160, 19, NULL, 0.0, 0.0, 1, -1, "");
1199         for(group= G.main->group.first; group; group= group->id.next, index++) {
1200                 
1201                 /*if(group->id.lib) strcpy(str, "L  ");*/ /* we cant allow adding objects inside linked groups, it wont be saved anyway */
1202                 if(group->id.lib==0) {
1203                         strcpy(str, "   ");
1204                         strcat(str, group->id.name+2);
1205                         uiDefBut(block, BUTM, B_NOP, str,       xco*160, -20*yco, 160, 19, NULL, 0.0, 0.0, 1, index, "");
1206                         
1207                         yco++;
1208                         if(yco>24) {
1209                                 yco= 0;
1210                                 xco++;
1211                         }
1212                 }
1213         }
1214         
1215         uiTextBoundsBlock(block, 50);
1216         uiBlockSetDirection(block, UI_DOWN);    
1217         
1218         return block;
1219 }
1220
1221 static void group_ob_rem(void *gr_v, void *ob_v)
1222 {
1223         Object *ob= OBACT;
1224         
1225         if(rem_from_group(gr_v, ob) && find_group(ob, NULL)==NULL) {
1226                 ob->flag &= ~OB_FROMGROUP;
1227                 BASACT->flag &= ~OB_FROMGROUP;
1228         }
1229         allqueue(REDRAWBUTSOBJECT, 0);
1230         allqueue(REDRAWVIEW3D, 0);
1231
1232 }
1233
1234 static void group_local(void *gr_v, void *unused)
1235 {
1236         Group *group= gr_v;
1237         
1238         group->id.lib= NULL;
1239         
1240         allqueue(REDRAWBUTSOBJECT, 0);
1241         allqueue(REDRAWVIEW3D, 0);
1242         
1243 }
1244
1245 uiLayout *uiTemplateGroup(uiLayout *layout, Object *ob, Group *group)
1246 {
1247         uiSetButLock(1, NULL);
1248         uiDefBlockBut(block, add_groupmenu, NULL, "Add to Group", 10,150,150,20, "Add Object to a new Group");
1249
1250         /* all groups */
1251         if(group->id.lib) {
1252                 uiLayoutRow()
1253                 uiBlockBeginAlign(block);
1254                 uiSetButLock(GET_INT_FROM_POINTER(group->id.lib), ERROR_LIBDATA_MESSAGE); /* We cant actually use this button */
1255                 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.");
1256                 uiClearButLock();
1257                 
1258                 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");
1259                 uiButSetFunc(but, group_local, group, NULL);
1260                 uiBlockEndAlign(block);
1261         } else {
1262                 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.");
1263                 uiButSetFunc(but, test_idbutton_cb, group->id.name, NULL);
1264         }
1265         
1266         xco = 290;
1267         if(group->id.lib==0) { /* cant remove objects from linked groups */
1268                 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");
1269                 uiButSetFunc(but, group_ob_rem, group, ob);
1270         }
1271 }
1272 #endif
1273
1274 /************************* Preview Template ***************************/
1275
1276 #include "DNA_material_types.h"
1277
1278 #define B_MATPRV 1
1279
1280 void uiTemplatePreview(uiLayout *layout, ID *id)
1281 {
1282         uiLayout *row, *col;
1283         uiBlock *block;
1284         Material *ma;
1285
1286         if(!id || !ELEM3(GS(id->name), ID_MA, ID_TE, ID_WO)) {
1287                 printf("uiTemplatePreview: expected ID of type material, texture or world.\n");
1288                 return;
1289         }
1290
1291         block= uiLayoutGetBlock(layout);
1292
1293         row= uiLayoutRow(layout, 0);
1294
1295         col= uiLayoutColumn(row, 0);
1296         uiLayoutSetKeepAspect(col, 1);
1297         uiDefBut(block, ROUNDBOX, 0, "", 0, 0, UI_UNIT_X*6, UI_UNIT_Y*6, NULL, 0.0, 0.0, 0, 0, "");
1298
1299         if(GS(id->name) == ID_MA) {
1300                 ma= (Material*)id;
1301
1302                 uiLayoutColumn(row, 1);
1303
1304                 uiDefIconButC(block, ROW, B_MATPRV, ICON_MATPLANE,  0, 0,UI_UNIT_X,UI_UNIT_Y, &(ma->pr_type), 10, MA_FLAT, 0, 0, "Preview type: Flat XY plane");
1305                 uiDefIconButC(block, ROW, B_MATPRV, ICON_MATSPHERE, 0, 0,UI_UNIT_X,UI_UNIT_Y, &(ma->pr_type), 10, MA_SPHERE, 0, 0, "Preview type: Sphere");
1306                 uiDefIconButC(block, ROW, B_MATPRV, ICON_MATCUBE,   0, 0,UI_UNIT_X,UI_UNIT_Y, &(ma->pr_type), 10, MA_CUBE, 0, 0, "Preview type: Cube");
1307                 uiDefIconButC(block, ROW, B_MATPRV, ICON_MONKEY,    0, 0,UI_UNIT_X,UI_UNIT_Y, &(ma->pr_type), 10, MA_MONKEY, 0, 0, "Preview type: Monkey");
1308                 uiDefIconButC(block, ROW, B_MATPRV, ICON_HAIR,      0, 0,UI_UNIT_X,UI_UNIT_Y, &(ma->pr_type), 10, MA_HAIR, 0, 0, "Preview type: Hair strands");
1309                 uiDefIconButC(block, ROW, B_MATPRV, ICON_MATSPHERE, 0, 0,UI_UNIT_X,UI_UNIT_Y, &(ma->pr_type), 10, MA_SPHERE_A, 0, 0, "Preview type: Large sphere with sky");
1310         }
1311
1312 }
1313
1314 /********************** ColorRamp Template **************************/
1315
1316 void uiTemplateColorRamp(uiLayout *layout, ColorBand *coba, int expand)
1317 {
1318         uiBlock *block;
1319         rctf rect;
1320
1321         if(coba) {
1322                 rect.xmin= 0; rect.xmax= 200;
1323                 rect.ymin= 0; rect.ymax= 190;
1324                 
1325                 block= uiLayoutFreeBlock(layout);
1326                 colorband_buttons(block, coba, &rect, !expand);
1327         }
1328 }
1329
1330 /********************* CurveMapping Template ************************/
1331
1332 #include "DNA_color_types.h"
1333
1334 void uiTemplateCurveMapping(uiLayout *layout, CurveMapping *cumap, int type)
1335 {
1336         uiBlock *block;
1337         rctf rect;
1338
1339         if(cumap) {
1340                 rect.xmin= 0; rect.xmax= 200;
1341                 rect.ymin= 0; rect.ymax= 190;
1342                 
1343                 block= uiLayoutFreeBlock(layout);
1344                 curvemap_buttons(block, cumap, type, 0, 0, &rect);
1345         }
1346 }
1347