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