2.5 Constraints:
[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
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 #define B_NOP                           0
244 #define B_MODIFIER_RECALC       1
245 #define B_MODIFIER_REDRAW       2
246 #define B_CHANGEDEP                     3
247 #define B_ARM_RECALCDATA        4
248
249 #include <string.h>
250
251 #include "DNA_armature_types.h"
252 #include "DNA_curve_types.h"
253 #include "DNA_object_force.h"
254 #include "DNA_object_types.h"
255 #include "DNA_mesh_types.h"
256 #include "DNA_meshdata_types.h"
257 #include "DNA_modifier_types.h"
258 #include "DNA_particle_types.h"
259 #include "DNA_scene_types.h"
260
261 #include "BKE_bmesh.h"
262 #include "BKE_curve.h"
263 #include "BKE_depsgraph.h"
264 #include "BKE_DerivedMesh.h"
265 #include "BKE_displist.h"
266 #include "BKE_global.h"
267 #include "BKE_lattice.h"
268 #include "BKE_main.h"
269 #include "BKE_mesh.h"
270 #include "BKE_modifier.h"
271 #include "BKE_object.h"
272 #include "BKE_particle.h"
273 #include "BKE_report.h"
274
275 #include "UI_resources.h"
276 #include "ED_util.h"
277
278 #include "BLI_arithb.h"
279 #include "BLI_listbase.h"
280
281 #include "ED_object.h"
282
283 void do_modifier_panels(bContext *C, void *arg, int event)
284 {
285         Scene *scene= CTX_data_scene(C);
286         Object *ob = CTX_data_active_object(C);
287
288         switch(event) {
289         case B_MODIFIER_REDRAW:
290                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
291                 break;
292
293         case B_MODIFIER_RECALC:
294                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
295                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
296                 object_handle_update(scene, ob);
297                 // XXX countall();
298                 break;
299         }
300 }
301
302 static void modifiers_del(bContext *C, void *ob_v, void *md_v)
303 {
304         ReportList reports;
305
306         BKE_reports_init(&reports, RPT_STORE);
307
308         if(ED_object_modifier_delete(&reports, ob_v, md_v))
309                 ED_undo_push(C, "Delete modifier");
310         else
311                 uiPupMenuReports(C, &reports);
312
313         BKE_reports_clear(&reports);
314 }
315
316 static void modifiers_moveUp(bContext *C, void *ob_v, void *md_v)
317 {
318         ReportList reports;
319
320         BKE_reports_init(&reports, RPT_STORE);
321
322         if(ED_object_modifier_move_up(&reports, ob_v, md_v))
323                 ED_undo_push(C, "Move modifier");
324         else
325                 uiPupMenuReports(C, &reports);
326
327         BKE_reports_clear(&reports);
328 }
329
330 static void modifiers_moveDown(bContext *C, void *ob_v, void *md_v)
331 {
332         ReportList reports;
333
334         BKE_reports_init(&reports, RPT_STORE);
335
336         if(ED_object_modifier_move_down(&reports, ob_v, md_v))
337                 ED_undo_push(C, "Move modifier");
338         else
339                 uiPupMenuReports(C, &reports);
340
341         BKE_reports_clear(&reports);
342 }
343
344 static void modifiers_convertParticles(bContext *C, void *obv, void *mdv)
345 {
346         Scene *scene= CTX_data_scene(C);
347         ReportList reports;
348
349         BKE_reports_init(&reports, RPT_STORE);
350
351         if(ED_object_modifier_convert(&reports, scene, obv, mdv))
352                 ED_undo_push(C, "Convert particles to mesh object(s).");
353         else
354                 uiPupMenuReports(C, &reports);
355
356         BKE_reports_clear(&reports);
357 }
358
359 static void modifiers_applyModifier(bContext *C, void *obv, void *mdv)
360 {
361         Scene *scene= CTX_data_scene(C);
362         ReportList reports;
363
364         BKE_reports_init(&reports, RPT_STORE);
365
366         if(ED_object_modifier_apply(&reports, scene, obv, mdv))
367                 ED_undo_push(C, "Apply modifier");
368         else
369                 uiPupMenuReports(C, &reports);
370
371         BKE_reports_clear(&reports);
372 }
373
374 static void modifiers_copyModifier(bContext *C, void *ob_v, void *md_v)
375 {
376         ReportList reports;
377
378         BKE_reports_init(&reports, RPT_STORE);
379
380         if(ED_object_modifier_copy(&reports, ob_v, md_v))
381                 ED_undo_push(C, "Copy modifier");
382         else
383                 uiPupMenuReports(C, &reports);
384
385         BKE_reports_clear(&reports);
386 }
387
388 static void modifiers_setOnCage(bContext *C, void *ob_v, void *md_v)
389 {
390         Object *ob = ob_v;
391         ModifierData *md;
392         
393         int i, cageIndex = modifiers_getCageIndex(ob, NULL );
394
395         for( i = 0, md=ob->modifiers.first; md; ++i, md=md->next )
396                 if( md == md_v ) {
397                         if( i >= cageIndex )
398                                 md->mode ^= eModifierMode_OnCage;
399                         break;
400                 }
401 }
402
403 static void modifiers_convertToReal(bContext *C, void *ob_v, void *md_v)
404 {
405         Object *ob = ob_v;
406         ModifierData *md = md_v;
407         ModifierData *nmd = modifier_new(md->type);
408
409         modifier_copyData(md, nmd);
410         nmd->mode &= ~eModifierMode_Virtual;
411
412         BLI_addhead(&ob->modifiers, nmd);
413
414         ob->partype = PAROBJECT;
415
416         ED_undo_push(C, "Modifier convert to real");
417 }
418
419 #if 0
420 static void modifiers_clearHookOffset(bContext *C, void *ob_v, void *md_v)
421 {
422         Object *ob = ob_v;
423         ModifierData *md = md_v;
424         HookModifierData *hmd = (HookModifierData*) md;
425         
426         if (hmd->object) {
427                 Mat4Invert(hmd->object->imat, hmd->object->obmat);
428                 Mat4MulSerie(hmd->parentinv, hmd->object->imat, ob->obmat, NULL, NULL, NULL, NULL, NULL, NULL);
429                 ED_undo_push(C, "Clear hook offset");
430         }
431 }
432
433 static void modifiers_cursorHookCenter(bContext *C, void *ob_v, void *md_v)
434 {
435         /* XXX 
436         Object *ob = ob_v;
437         ModifierData *md = md_v;
438         HookModifierData *hmd = (HookModifierData*) md;
439
440         if(G.vd) {
441                 float *curs = give_cursor();
442                 float bmat[3][3], imat[3][3];
443
444                 where_is_object(ob);
445         
446                 Mat3CpyMat4(bmat, ob->obmat);
447                 Mat3Inv(imat, bmat);
448
449                 curs= give_cursor();
450                 hmd->cent[0]= curs[0]-ob->obmat[3][0];
451                 hmd->cent[1]= curs[1]-ob->obmat[3][1];
452                 hmd->cent[2]= curs[2]-ob->obmat[3][2];
453                 Mat3MulVecfl(imat, hmd->cent);
454
455                 ED_undo_push(C, "Hook cursor center");
456         }*/
457 }
458
459 static void modifiers_selectHook(bContext *C, void *ob_v, void *md_v)
460 {
461         /* XXX ModifierData *md = md_v;
462         HookModifierData *hmd = (HookModifierData*) md;
463
464         hook_select(hmd);*/
465 }
466
467 static void modifiers_reassignHook(bContext *C, void *ob_v, void *md_v)
468 {
469         /* XXX ModifierData *md = md_v;
470         HookModifierData *hmd = (HookModifierData*) md;
471         float cent[3];
472         int *indexar, tot, ok;
473         char name[32];
474                 
475         ok= hook_getIndexArray(&tot, &indexar, name, cent);
476
477         if (!ok) {
478                 uiPupMenuError(C, "Requires selected vertices or active Vertex Group");
479         } else {
480                 if (hmd->indexar) {
481                         MEM_freeN(hmd->indexar);
482                 }
483
484                 VECCOPY(hmd->cent, cent);
485                 hmd->indexar = indexar;
486                 hmd->totindex = tot;
487         }*/
488 }
489
490 static void modifiers_bindMeshDeform(bContext *C, void *ob_v, void *md_v)
491 {
492         Scene *scene= CTX_data_scene(C);
493         MeshDeformModifierData *mmd = (MeshDeformModifierData*) md_v;
494         Object *ob = (Object*)ob_v;
495
496         if(mmd->bindcos) {
497                 if(mmd->bindweights) MEM_freeN(mmd->bindweights);
498                 if(mmd->bindcos) MEM_freeN(mmd->bindcos);
499                 if(mmd->dyngrid) MEM_freeN(mmd->dyngrid);
500                 if(mmd->dyninfluences) MEM_freeN(mmd->dyninfluences);
501                 if(mmd->dynverts) MEM_freeN(mmd->dynverts);
502                 mmd->bindweights= NULL;
503                 mmd->bindcos= NULL;
504                 mmd->dyngrid= NULL;
505                 mmd->dyninfluences= NULL;
506                 mmd->dynverts= NULL;
507                 mmd->totvert= 0;
508                 mmd->totcagevert= 0;
509                 mmd->totinfluence= 0;
510         }
511         else {
512                 DerivedMesh *dm;
513                 int mode= mmd->modifier.mode;
514
515                 /* force modifier to run, it will call binding routine */
516                 mmd->needbind= 1;
517                 mmd->modifier.mode |= eModifierMode_Realtime;
518
519                 if(ob->type == OB_MESH) {
520                         dm= mesh_create_derived_view(scene, ob, 0);
521                         dm->release(dm);
522                 }
523                 else if(ob->type == OB_LATTICE) {
524                         lattice_calc_modifiers(scene, ob);
525                 }
526                 else if(ob->type==OB_MBALL) {
527                         makeDispListMBall(scene, ob);
528                 }
529                 else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
530                         makeDispListCurveTypes(scene, ob, 0);
531                 }
532
533                 mmd->needbind= 0;
534                 mmd->modifier.mode= mode;
535         }
536 }
537
538 void modifiers_explodeFacepa(bContext *C, void *arg1, void *arg2)
539 {
540         ExplodeModifierData *emd=arg1;
541
542         emd->flag |= eExplodeFlag_CalcFaces;
543 }
544
545 void modifiers_explodeDelVg(bContext *C, void *arg1, void *arg2)
546 {
547         ExplodeModifierData *emd=arg1;
548         emd->vgroup = 0;
549 }
550 #endif
551
552 static int modifier_is_fluid_particles(ModifierData *md)
553 {
554         if(md->type == eModifierType_ParticleSystem) {
555                 if(((ParticleSystemModifierData *)md)->psys->part->type == PART_FLUID)
556                         return 1;
557         }
558         return 0;
559 }
560
561 static uiLayout *draw_modifier(uiLayout *layout, Object *ob, ModifierData *md, int index, int cageIndex, int lastCageIndex)
562 {
563         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
564         uiBut *but;
565         uiBlock *block;
566         uiLayout *column, *row, *result= NULL;
567         int isVirtual = md->mode&eModifierMode_Virtual;
568         int x = 0, y = 0; // XXX , color = md->error?TH_REDALERT:TH_BUT_NEUTRAL;
569         short width = 295, buttonWidth = width-120-10;
570         char str[128];
571
572         column= uiLayoutColumn(layout, 1);
573
574         /* rounded header */
575         /* XXX uiBlockSetCol(block, color); */
576                 /* roundbox 4 free variables: corner-rounding, nop, roundbox type, shade */
577         block= uiLayoutFreeBlock(uiLayoutBox(column));
578         uiBlockSetHandleFunc(block, do_modifier_panels, NULL);
579
580         //uiDefBut(block, ROUNDBOX, 0, "", x-10, y-4, width, 25, NULL, 7.0, 0.0, 
581         //               (!isVirtual && (md->mode&eModifierMode_Expanded))?3:15, 20, ""); 
582         /* XXX uiBlockSetCol(block, TH_AUTO); */
583         
584         /* open/close icon */
585         if (!isVirtual) {
586                 uiBlockSetEmboss(block, UI_EMBOSSN);
587                 uiDefIconButBitI(block, ICONTOG, eModifierMode_Expanded, B_MODIFIER_REDRAW, ICON_TRIA_RIGHT, x-10, y-2, 20, 20, &md->mode, 0.0, 0.0, 0.0, 0.0, "Collapse/Expand Modifier");
588         }
589
590         uiBlockSetEmboss(block, UI_EMBOSS);
591         
592         if (isVirtual) {
593                 sprintf(str, "%s parent deform", md->name);
594                 uiDefBut(block, LABEL, 0, str, x+10, y-1, width-110, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Modifier name"); 
595
596                 but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Make Real", x+width-100, y, 80, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Convert virtual modifier to a real modifier");
597                 uiButSetFunc(but, modifiers_convertToReal, ob, md);
598         } else {
599                 uiBlockBeginAlign(block);
600                 uiDefBut(block, TEX, B_MODIFIER_REDRAW, "", x+10, y-1, buttonWidth-60, 19, md->name, 0.0, sizeof(md->name)-1, 0.0, 0.0, "Modifier name"); 
601
602                 /* Softbody not allowed in this situation, enforce! */
603                 if (((md->type!=eModifierType_Softbody && md->type!=eModifierType_Collision) || !(ob->pd && ob->pd->deflect)) && (md->type!=eModifierType_Surface)) {
604                         uiDefIconButBitI(block, TOG, eModifierMode_Render, B_MODIFIER_RECALC, ICON_SCENE, x+10+buttonWidth-60, y-1, 19, 19,&md->mode, 0, 0, 1, 0, "Enable modifier during rendering");
605                         but= uiDefIconButBitI(block, TOG, eModifierMode_Realtime, B_MODIFIER_RECALC, ICON_VIEW3D, x+10+buttonWidth-40, y-1, 19, 19,&md->mode, 0, 0, 1, 0, "Enable modifier during interactive display");
606                         if (mti->flags&eModifierTypeFlag_SupportsEditmode) {
607                                 uiDefIconButBitI(block, TOG, eModifierMode_Editmode, B_MODIFIER_RECALC, ICON_EDITMODE_HLT, x+10+buttonWidth-20, y-1, 19, 19,&md->mode, 0, 0, 1, 0, "Enable modifier during Editmode (only if enabled for display)");
608                         }
609                 }
610                 uiBlockEndAlign(block);
611
612                 /* XXX uiBlockSetEmboss(block, UI_EMBOSSR); */
613
614                 if (ob->type==OB_MESH && modifier_couldBeCage(md) && index<=lastCageIndex) {
615                         int icon; //, color;
616
617                         if (index==cageIndex) {
618                                 // XXX color = TH_BUT_SETTING;
619                                 icon = VICON_EDITMODE_HLT;
620                         } else if (index<cageIndex) {
621                                 // XXX color = TH_BUT_NEUTRAL;
622                                 icon = VICON_EDITMODE_DEHLT;
623                         } else {
624                                 // XXX color = TH_BUT_NEUTRAL;
625                                 icon = ICON_BLANK1;
626                         }
627                         /* XXX uiBlockSetCol(block, color); */
628                         but = uiDefIconBut(block, BUT, B_MODIFIER_RECALC, icon, x+width-105, y, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Apply modifier to editing cage during Editmode");
629                         uiButSetFunc(but, modifiers_setOnCage, ob, md);
630                         /* XXX uiBlockSetCol(block, TH_AUTO); */
631                 }
632
633                 /* XXX uiBlockSetCol(block, TH_BUT_ACTION); */
634
635                 but = uiDefIconBut(block, BUT, B_MODIFIER_RECALC, VICON_MOVE_UP, x+width-75, y, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Move modifier up in stack");
636                 uiButSetFunc(but, modifiers_moveUp, ob, md);
637
638                 but = uiDefIconBut(block, BUT, B_MODIFIER_RECALC, VICON_MOVE_DOWN, x+width-75+20, y, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Move modifier down in stack");
639                 uiButSetFunc(but, modifiers_moveDown, ob, md);
640                 
641                 uiBlockSetEmboss(block, UI_EMBOSSN);
642                 
643                 // deletion over the deflection panel
644                 // fluid particle modifier can't be deleted here
645                 if(md->type!=eModifierType_Fluidsim && md->type!=eModifierType_Collision && md->type!=eModifierType_Surface && !modifier_is_fluid_particles(md))
646                 {
647                         but = uiDefIconBut(block, BUT, B_MODIFIER_RECALC, VICON_X, x+width-70+40, y, 16, 16, NULL, 0.0, 0.0, 0.0, 0.0, "Delete modifier");
648                         uiButSetFunc(but, modifiers_del, ob, md);
649                 }
650                 /* XXX uiBlockSetCol(block, TH_AUTO); */
651         }
652
653         uiBlockSetEmboss(block, UI_EMBOSS);
654
655         if(!isVirtual && (md->mode&eModifierMode_Expanded)) {
656                 int cy = y - 8;
657                 int lx = x + width - 60 - 15;
658                 uiLayout *box;
659
660                 box= uiLayoutBox(column);
661                 row= uiLayoutRow(box, 1);
662
663                 y -= 18;
664
665                 if (!isVirtual && (md->type!=eModifierType_Collision) && (md->type!=eModifierType_Surface)) {
666                         uiBlockSetButLock(block, object_data_is_libdata(ob), ERROR_LIBDATA_MESSAGE); /* only here obdata, the rest of modifiers is ob level */
667
668                                                 if (md->type==eModifierType_ParticleSystem) {
669                         ParticleSystem *psys= ((ParticleSystemModifierData *)md)->psys;
670
671                         if(!(G.f & G_PARTICLEEDIT)) {
672                                         if(ELEM3(psys->part->draw_as, PART_DRAW_PATH, PART_DRAW_GR, PART_DRAW_OB) && psys->pathcache) {
673                                                 but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Convert",        lx,(cy-=19),60,19, 0, 0, 0, 0, 0, "Convert the current particles to a mesh object");
674                                                 uiButSetFunc(but, modifiers_convertParticles, ob, md);
675                                         }
676                                 }
677                         }
678                         else{
679                                 but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Apply",  lx,(cy-=19),60,19, 0, 0, 0, 0, 0, "Apply the current modifier and remove from the stack");
680                                 uiButSetFunc(but, modifiers_applyModifier, ob, md);
681                         }
682                         
683                         uiBlockClearButLock(block);
684                         uiBlockSetButLock(block, ob && ob->id.lib, ERROR_LIBDATA_MESSAGE);
685
686                         if (md->type!=eModifierType_Fluidsim && md->type!=eModifierType_Softbody && md->type!=eModifierType_ParticleSystem && (md->type!=eModifierType_Cloth)) {
687                                 but = uiDefBut(block, BUT, B_MODIFIER_RECALC, "Copy",   lx,(cy-=19),60,19, 0, 0, 0, 0, 0, "Duplicate the current modifier at the same position in the stack");
688                                 uiButSetFunc(but, modifiers_copyModifier, ob, md);
689                         }
690                 }
691
692                 result= uiLayoutColumn(box, 0);
693                 block= uiLayoutFreeBlock(box);
694
695                 lx = x + 10;
696                 cy = y + 10 - 1;
697         }
698
699         if (md->error) {
700                 row = uiLayoutRow(uiLayoutBox(column), 0);
701
702                 /* XXX uiBlockSetCol(block, color); */
703                 uiItemL(row, md->error, ICON_ERROR);
704                 /* XXX uiBlockSetCol(block, TH_AUTO); */
705         }
706
707         return result;
708 }
709
710 uiLayout *uiTemplateModifier(uiLayout *layout, PointerRNA *ptr)
711 {
712         Object *ob;
713         ModifierData *md, *vmd;
714         int i, lastCageIndex, cageIndex;
715
716         /* verify we have valid data */
717         if(!RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
718                 printf("uiTemplateModifier: expected modifier on object.\n");
719                 return NULL;
720         }
721
722         ob= ptr->id.data;
723         md= ptr->data;
724
725         if(!ob || !(GS(ob->id.name) == ID_OB)) {
726                 printf("uiTemplateModifier: expected modifier on object.\n");
727                 return NULL;
728         }
729         
730         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
731         
732         /* find modifier and draw it */
733         cageIndex = modifiers_getCageIndex(ob, &lastCageIndex);
734
735         // XXX virtual modifiers are not accesible for python
736         vmd = modifiers_getVirtualModifierList(ob);
737
738         for(i=0; vmd; i++, vmd=vmd->next) {
739                 if(md == vmd)
740                         return draw_modifier(layout, ob, md, i, cageIndex, lastCageIndex);
741                 else if(vmd->mode&eModifierMode_Virtual)
742                         i--;
743         }
744
745         return NULL;
746 }
747
748 /************************ Constraint Template *************************/
749
750 #include "DNA_action_types.h"
751 #include "DNA_constraint_types.h"
752
753 #include "BKE_action.h"
754 #include "BKE_constraint.h"
755
756 #define REDRAWIPO                                       1
757 #define REDRAWNLA                                       2
758 #define REDRAWBUTSOBJECT                        3               
759 #define REDRAWACTION                            4
760 #define B_CONSTRAINT_TEST                       5
761 #define B_CONSTRAINT_CHANGETARGET       6
762 #define B_CONSTRAINT_INF                        7
763 #define REMAKEIPO                                       8
764 #define B_DIFF                                          9
765
766 void do_constraint_panels(bContext *C, void *arg, int event)
767 {
768         Scene *scene= CTX_data_scene(C);
769         Object *ob= CTX_data_active_object(C);
770         
771         switch(event) {
772         case B_CONSTRAINT_TEST:
773                 // XXX allqueue(REDRAWVIEW3D, 0);
774                 // XXX allqueue(REDRAWBUTSOBJECT, 0);
775                 // XXX allqueue(REDRAWBUTSEDIT, 0);
776                 break;  // no handling
777         case B_CONSTRAINT_INF:
778                 /* influence; do not execute actions for 1 dag_flush */
779                 if (ob->pose)
780                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
781                 break;
782         case B_CONSTRAINT_CHANGETARGET:
783                 if (ob->pose) ob->pose->flag |= POSE_RECALC;    // checks & sorts pose channels
784                 DAG_scene_sort(scene);
785                 break;
786         default:
787                 break;
788         }
789
790         object_test_constraints(ob);
791         
792         if(ob->pose) update_pose_constraint_flags(ob->pose);
793         
794         if(ob->type==OB_ARMATURE) DAG_object_flush_update(scene, ob, OB_RECALC_DATA|OB_RECALC_OB);
795         else DAG_object_flush_update(scene, ob, OB_RECALC_OB);
796         
797         // XXX allqueue(REDRAWVIEW3D, 0);
798         // XXX allqueue(REDRAWBUTSOBJECT, 0);
799         // XXX allqueue(REDRAWBUTSEDIT, 0);
800 }
801
802 static void constraint_active_func(bContext *C, void *ob_v, void *con_v)
803 {
804         ED_object_constraint_set_active(ob_v, con_v);
805 }
806
807 static void del_constraint_func (bContext *C, void *ob_v, void *con_v)
808 {
809         if(ED_object_constraint_delete(NULL, ob_v, con_v))
810                 ED_undo_push(C, "Delete Constraint");
811 }
812
813 static void verify_constraint_name_func (bContext *C, void *con_v, void *name_v)
814 {
815         Object *ob= CTX_data_active_object(C);
816         bConstraint *con= con_v;
817         char oldname[32];       
818         
819         if (!con)
820                 return;
821         
822         /* put on the stack */
823         BLI_strncpy(oldname, (char *)name_v, 32);
824         
825         ED_object_constraint_rename(ob, con, oldname);
826         ED_object_constraint_set_active(ob, con);
827         // XXX allqueue(REDRAWACTION, 0); 
828 }
829
830 static void constraint_moveUp(bContext *C, void *ob_v, void *con_v)
831 {
832         if(ED_object_constraint_move_up(NULL, ob_v, con_v))
833                 ED_undo_push(C, "Move Constraint");
834 }
835
836 static void constraint_moveDown(bContext *C, void *ob_v, void *con_v)
837 {
838         if(ED_object_constraint_move_down(NULL, ob_v, con_v))
839                 ED_undo_push(C, "Move Constraint");
840 }
841
842 /* some commonly used macros in the constraints drawing code */
843 #define is_armature_target(target) (target && target->type==OB_ARMATURE)
844 #define is_armature_owner(ob) ((ob->type == OB_ARMATURE) && (ob->flag & OB_POSEMODE))
845 #define is_geom_target(target) (target && (ELEM(target->type, OB_MESH, OB_LATTICE)) )
846
847 /* Helper function for draw constraint - draws constraint space stuff 
848  * This function should not be called if no menus are required 
849  * owner/target: -1 = don't draw menu; 0= not posemode, 1 = posemode 
850  */
851 static void draw_constraint_spaceselect (uiBlock *block, bConstraint *con, short xco, short yco, short owner, short target)
852 {
853         short tarx, ownx, iconx;
854         short bwidth;
855         short iconwidth = 20;
856         
857         /* calculate sizes and placement of menus */
858         if (owner == -1) {
859                 bwidth = 125;
860                 tarx = 120;
861                 ownx = 0;
862         }
863         else if (target == -1) {
864                 bwidth = 125;
865                 tarx = 0;
866                 ownx = 120;
867         }
868         else {
869                 bwidth = 100;
870                 tarx = 85;
871                 iconx = tarx + bwidth + 5;
872                 ownx = tarx + bwidth + iconwidth + 10;
873         }
874         
875         
876         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Convert:", xco, yco, 80,18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
877
878         /* Target-Space */
879         if (target == 1) {
880                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Pose Space %x2|Local with Parent %x3|Local Space %x1", 
881                                                                                                 tarx, yco, bwidth, 18, &con->tarspace, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
882         }
883         else if (target == 0) {
884                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Local (Without Parent) Space %x1", 
885                                                                                 tarx, yco, bwidth, 18, &con->tarspace, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
886         }
887         
888         if ((target != -1) && (owner != -1))
889                 uiDefIconBut(block, LABEL, B_NOP, ICON_ARROW_LEFTRIGHT,
890                         iconx, yco, 20, 20, NULL, 0.0, 0.0, 0.0, 0.0, "");
891         
892         /* Owner-Space */
893         if (owner == 1) {
894                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Owner Space %t|World Space %x0|Pose Space %x2|Local with Parent %x3|Local Space %x1", 
895                                                                                                 ownx, yco, bwidth, 18, &con->ownspace, 0, 0, 0, 0, "Choose space that owner is evaluated in");  
896         }
897         else if (owner == 0) {
898                 uiDefButC(block, MENU, B_CONSTRAINT_TEST, "Owner Space %t|World Space %x0|Local (Without Parent) Space %x1", 
899                                                                                 ownx, yco, bwidth, 18, &con->ownspace, 0, 0, 0, 0, "Choose space that owner is evaluated in");  
900         }
901 }
902
903 /* draw panel showing settings for a constraint */
904 static uiLayout *draw_constraint(uiLayout *layout, Object *ob, bConstraint *con)
905 {
906         bPoseChannel *pchan= get_active_posechannel(ob);
907         bConstraintTypeInfo *cti;
908         uiBlock *block;
909         uiLayout *result= NULL, *col, *box;
910         uiBut *but;
911         char typestr[32];
912         short width = 265;
913         short proxy_protected, xco=0, yco=0;
914         int rb_col;
915
916         /* get constraint typeinfo */
917         cti= constraint_get_typeinfo(con);
918         if (cti == NULL) {
919                 /* exception for 'Null' constraint - it doesn't have constraint typeinfo! */
920                 if (con->type == CONSTRAINT_TYPE_NULL)
921                         strcpy(typestr, "Null");
922                 else
923                         strcpy(typestr, "Unknown");
924         }
925         else
926                 strcpy(typestr, cti->name);
927                 
928         /* determine whether constraint is proxy protected or not */
929         if (proxylocked_constraints_owner(ob, pchan))
930                 proxy_protected= (con->flag & CONSTRAINT_PROXY_LOCAL)==0;
931         else
932                 proxy_protected= 0;
933
934         /* unless button has own callback, it adds this callback to button */
935         block= uiLayoutGetBlock(layout);
936         uiBlockSetHandleFunc(block, do_constraint_panels, NULL);
937         uiBlockSetFunc(block, constraint_active_func, ob, con);
938
939         col= uiLayoutColumn(layout, 1);
940         box= uiLayoutBox(col);
941
942         block= uiLayoutFreeBlock(box);
943                 
944         /* Draw constraint header */
945         uiBlockSetEmboss(block, UI_EMBOSSN);
946         
947         /* rounded header */
948         rb_col= (con->flag & CONSTRAINT_ACTIVE)?50:20;
949         
950         /* open/close */
951         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");
952         
953         /* name */      
954         if ((con->flag & CONSTRAINT_EXPAND) && (proxy_protected==0)) {
955                 /* XXX if (con->flag & CONSTRAINT_DISABLE)
956                         uiBlockSetCol(block, TH_REDALERT);*/
957                 
958                 uiBlockSetEmboss(block, UI_EMBOSS);
959                 
960                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr, xco+10, yco, 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
961                 
962                 but = uiDefBut(block, TEX, B_CONSTRAINT_TEST, "", xco+120, yco, 85, 18, con->name, 0.0, 29.0, 0.0, 0.0, "Constraint name"); 
963                 uiButSetFunc(but, verify_constraint_name_func, con, NULL);
964         }       
965         else {
966                 uiBlockSetEmboss(block, UI_EMBOSSN);
967                 
968                 /* XXX if (con->flag & CONSTRAINT_DISABLE)
969                         uiBlockSetCol(block, TH_REDALERT);*/
970                 
971                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr, xco+10, yco, 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
972                 
973                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, con->name, xco+120, yco-1, 135, 19, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
974         }
975
976         // XXX uiBlockSetCol(block, TH_AUTO);   
977         
978         /* proxy-protected constraints cannot be edited, so hide up/down + close buttons */
979         if (proxy_protected) {
980                 uiBlockSetEmboss(block, UI_EMBOSSN);
981                 
982                 /* draw a ghost icon (for proxy) and also a lock beside it, to show that constraint is "proxy locked" */
983                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_GHOST, xco+244, yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Proxy Protected");
984                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_LOCKED, xco+262, yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Proxy Protected");
985                 
986                 uiBlockSetEmboss(block, UI_EMBOSS);
987         }
988         else {
989                 short prev_proxylock, show_upbut, show_downbut;
990                 
991                 /* Up/Down buttons: 
992                  *      Proxy-constraints are not allowed to occur after local (non-proxy) constraints
993                  *      as that poses problems when restoring them, so disable the "up" button where
994                  *      it may cause this situation. 
995                  *
996                  *      Up/Down buttons should only be shown (or not greyed - todo) if they serve some purpose. 
997                  */
998                 if (proxylocked_constraints_owner(ob, pchan)) {
999                         if (con->prev) {
1000                                 prev_proxylock= (con->prev->flag & CONSTRAINT_PROXY_LOCAL) ? 0 : 1;
1001                         }
1002                         else
1003                                 prev_proxylock= 0;
1004                 }
1005                 else
1006                         prev_proxylock= 0;
1007                         
1008                 show_upbut= ((prev_proxylock == 0) && (con->prev));
1009                 show_downbut= (con->next) ? 1 : 0;
1010                 
1011                 if (show_upbut || show_downbut) {
1012                         uiBlockBeginAlign(block);
1013                                 uiBlockSetEmboss(block, UI_EMBOSS);
1014                                 
1015                                 if (show_upbut) {
1016                                         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");
1017                                         uiButSetFunc(but, constraint_moveUp, ob, con);
1018                                 }
1019                                 
1020                                 if (show_downbut) {
1021                                         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");
1022                                         uiButSetFunc(but, constraint_moveDown, ob, con);
1023                                 }
1024                         uiBlockEndAlign(block);
1025                 }
1026                 
1027                 
1028                 /* Close 'button' - emboss calls here disable drawing of 'button' behind X */
1029                 uiBlockSetEmboss(block, UI_EMBOSSN);
1030                 
1031                 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");
1032                 uiButSetFunc(but, del_constraint_func, ob, con);
1033                 
1034                 uiBlockSetEmboss(block, UI_EMBOSS);
1035         }
1036         
1037         /* Set but-locks for protected settings (magic numbers are used here!) */
1038         if (proxy_protected)
1039                 uiBlockSetButLock(block, 1, "Cannot edit Proxy-Protected Constraint");
1040         
1041         /* Draw constraint data */
1042         if ((con->flag & CONSTRAINT_EXPAND) == 0) {
1043                 (yco) -= 21;
1044         }
1045         else {
1046                 box= uiLayoutBox(col);
1047                 block= uiLayoutFreeBlock(box);
1048
1049                 switch (con->type) {
1050 #ifndef DISABLE_PYTHON
1051                 case CONSTRAINT_TYPE_PYTHON:
1052                         {
1053                                 bPythonConstraint *data = con->data;
1054                                 bConstraintTarget *ct;
1055                                 // uiBut *but2;
1056                                 int tarnum, theight;
1057                                 // static int pyconindex=0;
1058                                 // char *menustr;
1059                                 
1060                                 theight = (data->tarnum)? (data->tarnum * 38) : (38);
1061                                 
1062                                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Script:", xco+60, yco-24, 55, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1063                                 
1064                                 /* do the scripts menu */
1065                                 /* XXX menustr = buildmenu_pyconstraints(data->text, &pyconindex);
1066                                 but2 = uiDefButI(block, MENU, B_CONSTRAINT_TEST, menustr,
1067                                       xco+120, yco-24, 150, 20, &pyconindex,
1068                                       0, 0, 0, 0, "Set the Script Constraint to use");
1069                                 uiButSetFunc(but2, validate_pyconstraint_cb, data, &pyconindex);
1070                                 MEM_freeN(menustr);      */
1071                                 
1072                                 /* draw target(s) */
1073                                 if (data->flag & PYCON_USETARGETS) {
1074                                         /* Draw target parameters */ 
1075                                         for (ct=data->targets.first, tarnum=1; ct; ct=ct->next, tarnum++) {
1076                                                 char tarstr[32];
1077                                                 short yoffset= ((tarnum-1) * 38);
1078         
1079                                                 /* target label */
1080                                                 sprintf(tarstr, "Target %d:", tarnum);
1081                                                 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, tarstr, xco+45, yco-(48+yoffset), 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1082                                                 
1083                                                 /* target space-selector - per target */
1084                                                 if (is_armature_target(ct->tar)) {
1085                                                         uiDefButS(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Pose Space %x3|Local with Parent %x4|Local Space %x1", 
1086                                                                                                                         xco+10, yco-(66+yoffset), 100, 18, &ct->space, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
1087                                                 }
1088                                                 else {
1089                                                         uiDefButS(block, MENU, B_CONSTRAINT_TEST, "Target Space %t|World Space %x0|Local (Without Parent) Space %x1", 
1090                                                                                                                         xco+10, yco-(66+yoffset), 100, 18, &ct->space, 0, 0, 0, 0, "Choose space that target is evaluated in"); 
1091                                                 }
1092                                                 
1093                                                 uiBlockBeginAlign(block);
1094                                                         /* target object */
1095                                                         uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_CONSTRAINT_CHANGETARGET, "OB:", xco+120, yco-(48+yoffset), 150, 18, &ct->tar, "Target Object"); 
1096                                                         
1097                                                         /* subtarget */
1098                                                         if (is_armature_target(ct->tar)) {
1099                                                                 but= uiDefBut(block, TEX, B_CONSTRAINT_CHANGETARGET, "BO:", xco+120, yco-(66+yoffset),150,18, &ct->subtarget, 0, 24, 0, 0, "Subtarget Bone");
1100                                                                 uiButSetCompleteFunc(but, autocomplete_bone, (void *)ct->tar);
1101                                                         }
1102                                                         else if (is_geom_target(ct->tar)) {
1103                                                                 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");
1104                                                                 uiButSetCompleteFunc(but, autocomplete_vgroup, (void *)ct->tar);
1105                                                         }
1106                                                         else {
1107                                                                 strcpy(ct->subtarget, "");
1108                                                         }
1109                                                 uiBlockEndAlign(block);
1110                                         }
1111                                 }
1112                                 else {
1113                                         /* Draw indication that no target needed */
1114                                         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Target:", xco+60, yco-48, 55, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1115                                         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, "Not Applicable", xco+120, yco-48, 150, 18, NULL, 0.0, 0.0, 0.0, 0.0, ""); 
1116                                 }
1117                                 
1118                                 /* settings */
1119                                 uiBlockBeginAlign(block);
1120                                         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.");
1121                                         // XXX uiButSetFunc(but, BPY_pyconstraint_settings, data, NULL);
1122                                         
1123                                         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");
1124                                 uiBlockEndAlign(block);
1125                                 
1126                                 /* constraint space settings */
1127                                 draw_constraint_spaceselect(block, con, xco, yco-(73+theight), is_armature_owner(ob), -1);
1128                         }
1129                         break;
1130 #endif /* DISABLE_PYTHON */
1131                 /*case CONSTRAINT_TYPE_CHILDOF:
1132                         {
1133                                 // Inverse options 
1134                                 uiBlockBeginAlign(block);
1135                                         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)");
1136                                         // XXX uiButSetFunc(but, childof_const_setinv, con, NULL);
1137                                         
1138                                         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)");
1139                                         // XXX uiButSetFunc(but, childof_const_clearinv, con, NULL);
1140                                 uiBlockEndAlign(block);
1141                         }
1142                         break; 
1143                 */
1144                 
1145                 /*case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1146                         {
1147                                 if (data->type==CONSTRAINT_RB_GENERIC6DOF) {
1148                                         // Draw Pairs of LimitToggle+LimitValue 
1149                                         uiBlockBeginAlign(block); 
1150                                                 uiDefButBitS(block, TOG, 1, B_CONSTRAINT_TEST, "LinMinX", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum x limit"); 
1151                                                 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"); 
1152                                         uiBlockEndAlign(block);
1153                                         
1154                                         uiBlockBeginAlign(block); 
1155                                                 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"); 
1156                                                 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"); 
1157                                         uiBlockEndAlign(block);
1158                                         
1159                                         offsetY += 20;
1160                                         uiBlockBeginAlign(block); 
1161                                                 uiDefButBitS(block, TOG, 2, B_CONSTRAINT_TEST, "LinMinY", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum y limit"); 
1162                                                 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"); 
1163                                         uiBlockEndAlign(block);
1164                                         
1165                                         uiBlockBeginAlign(block); 
1166                                                 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"); 
1167                                                 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"); 
1168                                         uiBlockEndAlign(block);
1169                                         
1170                                         offsetY += 20;
1171                                         uiBlockBeginAlign(block); 
1172                                                 uiDefButBitS(block, TOG, 4, B_CONSTRAINT_TEST, "LinMinZ", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum z limit"); 
1173                                                 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"); 
1174                                         uiBlockEndAlign(block);
1175                                         
1176                                         uiBlockBeginAlign(block); 
1177                                                 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"); 
1178                                                 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"); 
1179                                         uiBlockEndAlign(block);
1180                                         offsetY += 20;
1181                                 }
1182                                 if ((data->type==CONSTRAINT_RB_GENERIC6DOF) || (data->type==CONSTRAINT_RB_CONETWIST)) {
1183                                         // Draw Pairs of LimitToggle+LimitValue /
1184                                         uiBlockBeginAlign(block); 
1185                                                 uiDefButBitS(block, TOG, 8, B_CONSTRAINT_TEST, "AngMinX", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum x limit"); 
1186                                                 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"); 
1187                                         uiBlockEndAlign(block);
1188                                         uiBlockBeginAlign(block); 
1189                                                 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"); 
1190                                                 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"); 
1191                                         uiBlockEndAlign(block);
1192                                         
1193                                         offsetY += 20;
1194                                         uiBlockBeginAlign(block); 
1195                                                 uiDefButBitS(block, TOG, 16, B_CONSTRAINT_TEST, "AngMinY", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum y limit"); 
1196                                                 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"); 
1197                                         uiBlockEndAlign(block);
1198                                         
1199                                         uiBlockBeginAlign(block); 
1200                                                 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"); 
1201                                                 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"); 
1202                                         uiBlockEndAlign(block);
1203                                         
1204                                         offsetY += 20;
1205                                         uiBlockBeginAlign(block); 
1206                                                 uiDefButBitS(block, TOG, 32, B_CONSTRAINT_TEST, "AngMinZ", xco, yco-offsetY, togButWidth, 18, &data->flag, 0, 24, 0, 0, "Use minimum z limit"); 
1207                                                 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"); 
1208                                         uiBlockEndAlign(block);
1209                                         
1210                                         uiBlockBeginAlign(block); 
1211                                                 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"); 
1212                                                 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"); 
1213                                         uiBlockEndAlign(block);
1214                                 }
1215                                 
1216                         }
1217                         break;
1218                         */
1219
1220                 case CONSTRAINT_TYPE_NULL:
1221                         {
1222                                 uiItemL(box, "", 0);
1223                         }
1224                         break;
1225                 default:
1226                         result= box;
1227                         break;
1228                 }
1229         }
1230         
1231         /* clear any locks set up for proxies/lib-linking */
1232         uiBlockClearButLock(block);
1233
1234         return result;
1235 }
1236
1237 uiLayout *uiTemplateConstraint(uiLayout *layout, PointerRNA *ptr)
1238 {
1239         Object *ob;
1240         bConstraint *con;
1241
1242         /* verify we have valid data */
1243         if(!RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
1244                 printf("uiTemplateConstraint: expected constraint on object.\n");
1245                 return NULL;
1246         }
1247
1248         ob= ptr->id.data;
1249         con= ptr->data;
1250
1251         if(!ob || !(GS(ob->id.name) == ID_OB)) {
1252                 printf("uiTemplateConstraint: expected constraint on object.\n");
1253                 return NULL;
1254         }
1255         
1256         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1257
1258         /* hrms, the temporal constraint should not draw! */
1259         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
1260                 bKinematicConstraint *data= con->data;
1261                 if(data->flag & CONSTRAINT_IK_TEMP)
1262                         return NULL;
1263         }
1264
1265         return draw_constraint(layout, ob, con);
1266 }
1267
1268 /************************* Group Template ***************************/
1269
1270 #if 0
1271 static void do_add_groupmenu(void *arg, int event)
1272 {
1273         Object *ob= OBACT;
1274         
1275         if(ob) {
1276                 
1277                 if(event== -1) {
1278                         Group *group= add_group( "Group" );
1279                         add_to_group(group, ob);
1280                 }
1281                 else
1282                         add_to_group(BLI_findlink(&G.main->group, event), ob);
1283                         
1284                 ob->flag |= OB_FROMGROUP;
1285                 BASACT->flag |= OB_FROMGROUP;
1286                 allqueue(REDRAWBUTSOBJECT, 0);
1287                 allqueue(REDRAWVIEW3D, 0);
1288         }               
1289 }
1290
1291 static uiBlock *add_groupmenu(void *arg_unused)
1292 {
1293         uiBlock *block;
1294         Group *group;
1295         short xco=0, yco= 0, index=0;
1296         char str[32];
1297         
1298         block= uiNewBlock(&curarea->uiblocks, "add_constraintmenu", UI_EMBOSSP, UI_HELV, curarea->win);
1299         uiBlockSetButmFunc(block, do_add_groupmenu, NULL);
1300
1301         uiDefBut(block, BUTM, B_NOP, "ADD NEW",         0, 20, 160, 19, NULL, 0.0, 0.0, 1, -1, "");
1302         for(group= G.main->group.first; group; group= group->id.next, index++) {
1303                 
1304                 /*if(group->id.lib) strcpy(str, "L  ");*/ /* we cant allow adding objects inside linked groups, it wont be saved anyway */
1305                 if(group->id.lib==0) {
1306                         strcpy(str, "   ");
1307                         strcat(str, group->id.name+2);
1308                         uiDefBut(block, BUTM, B_NOP, str,       xco*160, -20*yco, 160, 19, NULL, 0.0, 0.0, 1, index, "");
1309                         
1310                         yco++;
1311                         if(yco>24) {
1312                                 yco= 0;
1313                                 xco++;
1314                         }
1315                 }
1316         }
1317         
1318         uiTextBoundsBlock(block, 50);
1319         uiBlockSetDirection(block, UI_DOWN);    
1320         
1321         return block;
1322 }
1323
1324 static void group_ob_rem(void *gr_v, void *ob_v)
1325 {
1326         Object *ob= OBACT;
1327         
1328         if(rem_from_group(gr_v, ob) && find_group(ob, NULL)==NULL) {
1329                 ob->flag &= ~OB_FROMGROUP;
1330                 BASACT->flag &= ~OB_FROMGROUP;
1331         }
1332         allqueue(REDRAWBUTSOBJECT, 0);
1333         allqueue(REDRAWVIEW3D, 0);
1334
1335 }
1336
1337 static void group_local(void *gr_v, void *unused)
1338 {
1339         Group *group= gr_v;
1340         
1341         group->id.lib= NULL;
1342         
1343         allqueue(REDRAWBUTSOBJECT, 0);
1344         allqueue(REDRAWVIEW3D, 0);
1345         
1346 }
1347
1348 uiLayout *uiTemplateGroup(uiLayout *layout, Object *ob, Group *group)
1349 {
1350         uiSetButLock(1, NULL);
1351         uiDefBlockBut(block, add_groupmenu, NULL, "Add to Group", 10,150,150,20, "Add Object to a new Group");
1352
1353         /* all groups */
1354         if(group->id.lib) {
1355                 uiLayoutRow()
1356                 uiBlockBeginAlign(block);
1357                 uiSetButLock(GET_INT_FROM_POINTER(group->id.lib), ERROR_LIBDATA_MESSAGE); /* We cant actually use this button */
1358                 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.");
1359                 uiClearButLock();
1360                 
1361                 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");
1362                 uiButSetFunc(but, group_local, group, NULL);
1363                 uiBlockEndAlign(block);
1364         } else {
1365                 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.");
1366                 uiButSetFunc(but, test_idbutton_cb, group->id.name, NULL);
1367         }
1368         
1369         xco = 290;
1370         if(group->id.lib==0) { /* cant remove objects from linked groups */
1371                 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");
1372                 uiButSetFunc(but, group_ob_rem, group, ob);
1373         }
1374 }
1375 #endif
1376
1377 /************************* Preview Template ***************************/
1378
1379 #include "DNA_material_types.h"
1380
1381 #define B_MATPRV 1
1382
1383 void uiTemplatePreview(uiLayout *layout, ID *id)
1384 {
1385         uiLayout *row, *col;
1386         uiBlock *block;
1387         Material *ma;
1388
1389         if(!id || !ELEM3(GS(id->name), ID_MA, ID_TE, ID_WO)) {
1390                 printf("uiTemplatePreview: expected ID of type material, texture or world.\n");
1391                 return;
1392         }
1393
1394         block= uiLayoutGetBlock(layout);
1395
1396         row= uiLayoutRow(layout, 0);
1397
1398         col= uiLayoutColumn(row, 0);
1399         uiLayoutSetKeepAspect(col, 1);
1400         uiDefBut(block, ROUNDBOX, 0, "", 0, 0, UI_UNIT_X*6, UI_UNIT_Y*6, NULL, 0.0, 0.0, 0, 0, "");
1401
1402         if(GS(id->name) == ID_MA) {
1403                 ma= (Material*)id;
1404
1405                 uiLayoutColumn(row, 1);
1406
1407                 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");
1408                 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");
1409                 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");
1410                 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");
1411                 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");
1412                 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");
1413         }
1414
1415 }
1416