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