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