2.5/Posemode:
[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_INVOKE_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_INVOKE_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(!(ob->mode & OB_MODE_PARTICLE_EDIT))
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->mode & OB_MODE_POSE))
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, int compact)
1186 {
1187         rctf rect;
1188
1189         if(cumap) {
1190                 if(compact) {
1191                         rect.xmin= 0; rect.xmax= 150;
1192                         rect.ymin= 0; rect.ymax= 140;
1193                 }
1194                 else {
1195                         rect.xmin= 0; rect.xmax= 200;
1196                         rect.ymin= 0; rect.ymax= 190;
1197                 }
1198                 
1199                 curvemap_layout(layout, cumap, type, 0, 0, &rect);
1200         }
1201 }
1202
1203 /********************* TriColor (ThemeWireColorSet) Template ************************/
1204
1205 void uiTemplateTriColorSet(uiLayout *layout, PointerRNA *ptr, char *propname)
1206 {
1207         uiLayout *row;
1208         PropertyRNA *prop;
1209         PointerRNA csPtr;
1210         
1211         if (!ptr->data)
1212                 return;
1213         
1214         prop= RNA_struct_find_property(ptr, propname);
1215         if (!prop) {
1216                 printf("uiTemplateTriColorSet: property not found: %s\n", propname);
1217                 return;
1218         }
1219         
1220         /* we lay out the data in a row as 3 color swatches */
1221         row= uiLayoutRow(layout, 1);
1222         
1223         /* nselected, selected, active color swatches */
1224         csPtr= RNA_property_pointer_get(ptr, prop);
1225         
1226         uiItemR(row, "", 0, &csPtr, "normal", 0, 0, 0);
1227         uiItemR(row, "", 0, &csPtr, "selected", 0, 0, 0);
1228         uiItemR(row, "", 0, &csPtr, "active", 0, 0, 0);
1229 }
1230
1231 /********************* Layer Buttons Template ************************/
1232
1233 // TODO:
1234 //      - option for showing extra info like whether layer has contents?
1235 //      - for now, grouping of layers is determined by dividing up the length of 
1236 //        the array of layer bitflags
1237
1238 void uiTemplateLayers(uiLayout *layout, PointerRNA *ptr, char *propname)
1239 {
1240         uiLayout *uRow, *uSplit, *uCol;
1241         PropertyRNA *prop;
1242         int groups, cols, layers;
1243         int group, col, layer, row;
1244         
1245         if (!ptr->data)
1246                 return;
1247         
1248         prop= RNA_struct_find_property(ptr, propname);
1249         if (!prop) {
1250                 printf("uiTemplateLayer: layers property not found: %s\n", propname);
1251                 return;
1252         }
1253         
1254         /* the number of layers determines the way we group them 
1255          *      - we want 2 rows only (for now)
1256          *      - the number of columns (cols) is the total number of buttons per row
1257          *        the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
1258          *      - for now, only split into groups if if group will have at least 5 items
1259          */
1260         layers= RNA_property_array_length(prop);
1261         cols= (layers / 2) + (layers % 2);
1262         groups= ((cols / 2) < 5) ? (1) : (cols / 2);
1263         
1264         /* layers are laid out going across rows, with the columns being divided into groups */
1265         if (groups > 1)
1266                 uSplit= uiLayoutSplit(layout, (1.0f/(float)groups));
1267         else    
1268                 uSplit= layout;
1269         
1270         for (group= 0; group < groups; group++) {
1271                 uCol= uiLayoutColumn(uSplit, 1);
1272                 
1273                 for (row= 0; row < 2; row++) {
1274                         uRow= uiLayoutRow(uCol, 1);
1275                         layer= groups*cols*row + cols*group;
1276                         
1277                         /* add layers as toggle buts */
1278                         for (col= 0; (col < cols) && (layer < layers); col++, layer++) {
1279                                 int icon=0; // XXX - add some way of setting this...
1280                                 uiItemFullR(uRow, "", icon, ptr, prop, layer, 0, 0, 0, 1);
1281                         }
1282                 }
1283         }
1284 }
1285
1286
1287 /************************* List Template **************************/
1288
1289 #if 0
1290 static void list_item_add(ListBase *lb, ListBase *itemlb, uiLayout *layout, PointerRNA *data)
1291 {
1292         CollectionPointerLink *link;
1293         uiListItem *item;
1294
1295         /* add to list to store in box */
1296         item= MEM_callocN(sizeof(uiListItem), "uiListItem");
1297         item->layout= layout;
1298         item->data= *data;
1299         BLI_addtail(itemlb, item);
1300
1301         /* add to list to return from function */
1302         link= MEM_callocN(sizeof(CollectionPointerLink), "uiTemplateList return");
1303         RNA_pointer_create(NULL, &RNA_UIListItem, item, &link->ptr);
1304         BLI_addtail(lb, link);
1305 }
1306 #endif
1307
1308 ListBase uiTemplateList(uiLayout *layout, bContext *C, PointerRNA *ptr, char *propname, PointerRNA *activeptr, char *activepropname, int rows, int listtype)
1309 {
1310         //Scene *scene= CTX_data_scene(C);
1311         PropertyRNA *prop= NULL, *activeprop;
1312         PropertyType type, activetype;
1313         StructRNA *ptype;
1314         uiLayout *box, *row, *col, *subrow;
1315         uiBlock *block;
1316         uiBut *but;
1317         Panel *pa;
1318         ListBase lb, *itemlb;
1319         char *name, str[32];
1320         int icon=0, i= 0, activei= 0, len, items, found, min, max;
1321
1322         lb.first= lb.last= NULL;
1323         
1324         /* validate arguments */
1325         block= uiLayoutGetBlock(layout);
1326         pa= block->panel;
1327
1328         if(!pa) {
1329                 printf("uiTemplateList: only works inside a panel.\n");
1330                 return lb;
1331         }
1332
1333         if(!activeptr->data)
1334                 return lb;
1335         
1336         if(ptr->data) {
1337                 prop= RNA_struct_find_property(ptr, propname);
1338                 if(!prop) {
1339                         printf("uiTemplateList: property not found: %s\n", propname);
1340                         return lb;
1341                 }
1342         }
1343
1344         activeprop= RNA_struct_find_property(activeptr, activepropname);
1345         if(!activeprop) {
1346                 printf("uiTemplateList: property not found: %s\n", activepropname);
1347                 return lb;
1348         }
1349
1350         if(prop) {
1351                 type= RNA_property_type(prop);
1352                 if(type != PROP_COLLECTION) {
1353                         printf("uiTemplateList: expected collection property.\n");
1354                         return lb;
1355                 }
1356         }
1357
1358         activetype= RNA_property_type(activeprop);
1359         if(activetype != PROP_INT) {
1360                 printf("uiTemplateList: expected integer property.\n");
1361                 return lb;
1362         }
1363
1364         /* get icon */
1365         if(ptr->data && prop) {
1366                 ptype= RNA_property_pointer_type(ptr, prop);
1367                 icon= RNA_struct_ui_icon(ptype);
1368         }
1369
1370         /* get active data */
1371         activei= RNA_property_int_get(activeptr, activeprop);
1372
1373         if(listtype == 'i') {
1374                 box= uiLayoutListBox(layout);
1375                 col= uiLayoutColumn(box, 1);
1376                 row= uiLayoutRow(col, 0);
1377
1378                 itemlb= uiLayoutBoxGetList(box);
1379
1380                 if(ptr->data && prop) {
1381                         /* create list items */
1382                         RNA_PROP_BEGIN(ptr, itemptr, prop) {
1383                                 /* create button */
1384                                 if(i == 9)
1385                                         row= uiLayoutRow(col, 0);
1386
1387                                 if(RNA_struct_is_a(itemptr.type, &RNA_TextureSlot)) {
1388 #if 0
1389                                         MTex *mtex= itemptr.data;
1390
1391                                         if(mtex && mtex->tex)
1392                                                 icon= ui_id_icon_get(scene, &mtex->tex->id);
1393 #endif
1394                                 }
1395
1396                                 uiDefIconButR(block, LISTROW, 0, icon, 0,0,UI_UNIT_X*10,UI_UNIT_Y, activeptr, activepropname, 0, 0, i, 0, 0, "");
1397
1398                                 //list_item_add(&lb, itemlb, uiLayoutRow(row, 1), &itemptr);
1399
1400                                 i++;
1401                         }
1402                         RNA_PROP_END;
1403                 }
1404         }
1405         else if(listtype == 'c') {
1406                 /* compact layout */
1407                 found= 0;
1408
1409                 row= uiLayoutRow(layout, 1);
1410
1411                 if(ptr->data && prop) {
1412                         /* create list items */
1413                         RNA_PROP_BEGIN(ptr, itemptr, prop) {
1414                                 found= (activei == i);
1415
1416                                 if(found) {
1417                                         /* create button */
1418                                         name= RNA_struct_name_get_alloc(&itemptr, NULL, 0);
1419                                         uiItemL(row, (name)? name: "", icon);
1420
1421                                         if(name)
1422                                                 MEM_freeN(name);
1423
1424                                         /* add to list to return */
1425                                         //list_item_add(&lb, itemlb, uiLayoutRow(row, 1), &itemptr);
1426                                 }
1427
1428                                 i++;
1429                         }
1430                         RNA_PROP_END;
1431                 }
1432
1433                 /* if not found, add in dummy button */
1434                 if(i == 0)
1435                         uiItemL(row, "", 0);
1436
1437                 /* next/prev button */
1438                 sprintf(str, "%d :", i);
1439                 but= uiDefIconTextButR(block, NUM, 0, 0, str, 0,0,UI_UNIT_X*5,UI_UNIT_Y, activeptr, activepropname, 0, 0, 0, 0, 0, "");
1440                 if(i == 0)
1441                         uiButSetFlag(but, UI_BUT_DISABLED);
1442         }
1443         else {
1444                 /* default rows */
1445                 if(rows == 0)
1446                         rows= 5;
1447
1448                 /* layout */
1449                 box= uiLayoutListBox(layout);
1450                 row= uiLayoutRow(box, 0);
1451                 col = uiLayoutColumn(row, 1);
1452
1453                 /* init numbers */
1454                 RNA_property_int_range(activeptr, activeprop, &min, &max);
1455
1456                 len= max - min + 1;
1457                 items= CLAMPIS(len, rows, 5);
1458
1459                 pa->list_scroll= MIN2(pa->list_scroll, len-items);
1460                 pa->list_scroll= MAX2(pa->list_scroll, 0);
1461
1462                 itemlb= uiLayoutBoxGetList(box);
1463
1464                 if(ptr->data && prop) {
1465                         /* create list items */
1466                         RNA_PROP_BEGIN(ptr, itemptr, prop) {
1467                                 if(i >= pa->list_scroll && i<pa->list_scroll+items) {
1468                                         name= RNA_struct_name_get_alloc(&itemptr, NULL, 0);
1469
1470                                         subrow= uiLayoutRow(col, 0);
1471
1472                                         /* create button */
1473                                         if(!icon || icon == ICON_DOT)
1474                                                 but= uiDefButR(block, LISTROW, 0, (name)? name: "", 0,0,UI_UNIT_X*10,UI_UNIT_Y, activeptr, activepropname, 0, 0, i, 0, 0, "");
1475                                         else
1476                                                 but= uiDefIconTextButR(block, LISTROW, 0, icon, (name)? name: "", 0,0,UI_UNIT_X*10,UI_UNIT_Y, activeptr, activepropname, 0, 0, i, 0, 0, "");
1477                                         uiButSetFlag(but, UI_ICON_LEFT|UI_TEXT_LEFT);
1478
1479                                         /* XXX hardcoded */
1480                                         if(itemptr.type == &RNA_MeshTextureFaceLayer || itemptr.type == &RNA_MeshColorLayer) {
1481                                                 uiBlockSetEmboss(block, UI_EMBOSSN);
1482                                                 uiDefIconButR(block, TOG, 0, ICON_SCENE, 0, 0, UI_UNIT_X, UI_UNIT_Y, &itemptr, "active_render", 0, 0, 0, 0, 0, NULL);
1483                                                 uiBlockSetEmboss(block, UI_EMBOSS);
1484                                         }
1485
1486                                         if(name)
1487                                                 MEM_freeN(name);
1488
1489                                         /* add to list to return */
1490                                         //list_item_add(&lb, itemlb, subrow, &itemptr);
1491                                 }
1492
1493                                 i++;
1494                         }
1495                         RNA_PROP_END;
1496                 }
1497
1498                 /* add dummy buttons to fill space */
1499                 while(i < pa->list_scroll+items) {
1500                         if(i >= pa->list_scroll)
1501                                 uiItemL(col, "", 0);
1502                         i++;
1503                 }
1504
1505                 /* add scrollbar */
1506                 if(len > items) {
1507                         col= uiLayoutColumn(row, 0);
1508                         uiDefButI(block, SCROLL, 0, "", 0,0,UI_UNIT_X*0.75,UI_UNIT_Y*items, &pa->list_scroll, 0, len-items, items, 0, "");
1509                 }
1510         }
1511
1512         /* return items in list */
1513         return lb;
1514 }
1515
1516 /************************* Operator Search Template **************************/
1517
1518 static void operator_call_cb(struct bContext *C, void *arg1, void *arg2)
1519 {
1520         wmOperatorType *ot= arg2;
1521         
1522         if(ot)
1523                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
1524 }
1525
1526 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
1527 {
1528         wmOperatorType *ot = WM_operatortype_first();
1529         
1530         for(; ot; ot= ot->next) {
1531                 
1532                 if(BLI_strcasestr(ot->name, str)) {
1533                         if(ot->poll==NULL || ot->poll((bContext *)C)) {
1534                                 char name[256];
1535                                 int len= strlen(ot->name);
1536                                 
1537                                 /* display name for menu, can hold hotkey */
1538                                 BLI_strncpy(name, ot->name, 256);
1539                                 
1540                                 /* check for hotkey */
1541                                 if(len < 256-6) {
1542                                         if(WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, &name[len+1], 256-len-1))
1543                                                 name[len]= '|';
1544                                 }
1545                                 
1546                                 if(0==uiSearchItemAdd(items, name, ot, 0))
1547                                         break;
1548                         }
1549                 }
1550         }
1551 }
1552
1553 void uiTemplateOperatorSearch(uiLayout *layout)
1554 {
1555         uiBlock *block;
1556         uiBut *but;
1557         static char search[256]= "";
1558                 
1559         block= uiLayoutGetBlock(layout);
1560         uiBlockSetCurLayout(block, layout);
1561
1562         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X*6, UI_UNIT_Y, "");
1563         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
1564 }
1565
1566 /************************* Running Jobs Template **************************/
1567
1568 #define B_STOPRENDER    1
1569 #define B_STOPCAST              2
1570 #define B_STOPANIM              3
1571
1572 static void do_running_jobs(bContext *C, void *arg, int event)
1573 {
1574         switch(event) {
1575                 case B_STOPRENDER:
1576                         G.afbreek= 1;
1577                         break;
1578                 case B_STOPCAST:
1579                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_screen(C));
1580                         break;
1581                 case B_STOPANIM:
1582                         ED_screen_animation_timer(C, 0, 0);
1583                         break;
1584         }
1585 }
1586
1587 void uiTemplateRunningJobs(uiLayout *layout, bContext *C)
1588 {
1589         bScreen *screen= CTX_wm_screen(C);
1590         Scene *scene= CTX_data_scene(C);
1591         wmWindowManager *wm= CTX_wm_manager(C);
1592         uiBlock *block;
1593
1594         block= uiLayoutGetBlock(layout);
1595         uiBlockSetCurLayout(block, layout);
1596
1597         uiBlockSetHandleFunc(block, do_running_jobs, NULL);
1598
1599         if(WM_jobs_test(wm, scene))
1600                 uiDefIconTextBut(block, BUT, B_STOPRENDER, ICON_REC, "Render", 0,0,75,UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "Stop rendering");
1601         if(WM_jobs_test(wm, screen))
1602                 uiDefIconTextBut(block, BUT, B_STOPCAST, ICON_REC, "Capture", 0,0,85,UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "Stop screencast");
1603         if(screen->animtimer)
1604                 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");
1605 }
1606
1607 /************************* Image Template **************************/
1608
1609 #include "ED_image.h"
1610
1611 void uiTemplateTextureImage(uiLayout *layout, bContext *C, Tex *tex)
1612 {
1613         uiBlock *block;
1614
1615         if(tex) {
1616                 block= uiLayoutFreeBlock(layout);
1617                 ED_image_uiblock_panel(C, block, &tex->ima, &tex->iuser, 0, 0);
1618         }
1619 }
1620