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