Merged changes in the trunk up to revision 52815.
[blender-staging.git] / source / blender / editors / interface / interface_templates.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Foundation 2009.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/interface/interface_templates.c
24  *  \ingroup edinterface
25  */
26
27
28 #include <stdlib.h>
29 #include <stddef.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_anim_types.h"
35 #include "DNA_dynamicpaint_types.h"
36 #include "DNA_key_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "BLI_utildefines.h"
42 #include "BLI_string.h"
43 #include "BLI_ghash.h"
44 #include "BLI_rect.h"
45
46 #include "BLF_translation.h"
47
48 #include "BKE_animsys.h"
49 #include "BKE_colortools.h"
50 #include "BKE_context.h"
51 #include "BKE_dynamicpaint.h"
52 #include "BKE_global.h"
53 #include "BKE_library.h"
54 #include "BKE_main.h"
55 #include "BKE_object.h"
56 #include "BKE_material.h"
57 #include "BKE_texture.h"
58 #include "BKE_report.h"
59 #include "BKE_displist.h"
60 #include "BKE_sca.h"
61 #include "BKE_scene.h"
62
63 #include "ED_screen.h"
64 #include "ED_object.h"
65 #include "ED_render.h"
66
67 #include "RNA_access.h"
68 #include "RNA_enum_types.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72
73 #include "UI_interface.h"
74 #include "interface_intern.h"
75
76 #include "BLF_api.h"
77 #include "BLF_translation.h"
78
79 void UI_template_fix_linking(void)
80 {
81 }
82
83 /********************** Header Template *************************/
84
85 void uiTemplateHeader(uiLayout *layout, bContext *C, int menus)
86 {
87         uiBlock *block;
88
89         block = uiLayoutAbsoluteBlock(layout);
90         if (menus) ED_area_header_standardbuttons(C, block, 0);
91         else ED_area_header_switchbutton(C, block, 0);
92 }
93
94 /********************** Search Callbacks *************************/
95
96 typedef struct TemplateID {
97         PointerRNA ptr;
98         PropertyRNA *prop;
99
100         ListBase *idlb;
101         int prv_rows, prv_cols;
102         int preview;
103 } TemplateID;
104
105 /* Search browse menu, assign  */
106 static void id_search_call_cb(bContext *C, void *arg_template, void *item)
107 {
108         TemplateID *template = (TemplateID *)arg_template;
109
110         /* ID */
111         if (item) {
112                 PointerRNA idptr;
113
114                 RNA_id_pointer_create(item, &idptr);
115                 RNA_property_pointer_set(&template->ptr, template->prop, idptr);
116                 RNA_property_update(C, &template->ptr, template->prop);
117         }
118 }
119
120 /* ID Search browse menu, do the search */
121 static void id_search_cb(const bContext *C, void *arg_template, const char *str, uiSearchItems *items)
122 {
123         TemplateID *template = (TemplateID *)arg_template;
124         ListBase *lb = template->idlb;
125         ID *id, *id_from = template->ptr.id.data;
126         int iconid;
127         int flag = RNA_property_flag(template->prop);
128
129         /* ID listbase */
130         for (id = lb->first; id; id = id->next) {
131                 if (!((flag & PROP_ID_SELF_CHECK) && id == id_from)) {
132
133                         /* use filter */
134                         if (RNA_property_type(template->prop) == PROP_POINTER) {
135                                 PointerRNA ptr;
136                                 RNA_id_pointer_create(id, &ptr);
137                                 if (RNA_property_pointer_poll(&template->ptr, template->prop, &ptr) == 0)
138                                         continue;
139                         }
140
141                         /* hide dot-datablocks, but only if filter does not force it visible */
142                         if (U.uiflag & USER_HIDE_DOT)
143                                 if ((id->name[2] == '.') && (str[0] != '.'))
144                                         continue;
145
146                         if (BLI_strcasestr(id->name + 2, str)) {
147                                 char name_ui[MAX_ID_NAME];
148                                 name_uiprefix_id(name_ui, id);
149
150                                 iconid = ui_id_icon_get((bContext *)C, id, template->preview);
151
152                                 if (!uiSearchItemAdd(items, name_ui, id, iconid))
153                                         break;
154                         }
155                 }
156         }
157 }
158
159 /* ID Search browse menu, open */
160 static uiBlock *id_search_menu(bContext *C, ARegion *ar, void *arg_litem)
161 {
162         static char search[256];
163         static TemplateID template;
164         PointerRNA idptr;
165         wmWindow *win = CTX_wm_window(C);
166         uiBlock *block;
167         uiBut *but;
168         
169         /* clear initial search string, then all items show */
170         search[0] = 0;
171         /* arg_litem is malloced, can be freed by parent button */
172         template = *((TemplateID *)arg_litem);
173         
174         /* get active id for showing first item */
175         idptr = RNA_property_pointer_get(&template.ptr, template.prop);
176
177         block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
178         uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_REDRAW | UI_BLOCK_SEARCH_MENU);
179         
180         /* preview thumbnails */
181         if (template.prv_rows > 0 && template.prv_cols > 0) {
182                 int w = 96 * template.prv_cols;
183                 int h = 96 * template.prv_rows + 20;
184                 
185                 /* fake button, it holds space for search items */
186                 uiDefBut(block, LABEL, 0, "", 10, 15, w, h, NULL, 0, 0, 0, 0, NULL);
187                 
188                 but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, w, 19,
189                                      template.prv_rows, template.prv_cols, "");
190                 uiButSetSearchFunc(but, id_search_cb, &template, id_search_call_cb, idptr.data);
191         }
192         /* list view */
193         else {
194                 const int searchbox_width  = uiSearchBoxWidth();
195                 const int searchbox_height = uiSearchBoxHeight();
196                 /* fake button, it holds space for search items */
197                 uiDefBut(block, LABEL, 0, "", 10, 15, searchbox_width, searchbox_height, NULL, 0, 0, 0, 0, NULL);
198                 
199                 but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, searchbox_width, UI_UNIT_Y - 1, 0, 0, "");
200                 uiButSetSearchFunc(but, id_search_cb, &template, id_search_call_cb, idptr.data);
201         }
202                 
203         
204         uiBoundsBlock(block, 6);
205         uiBlockSetDirection(block, UI_DOWN);
206         uiEndBlock(C, block);
207         
208         /* give search-field focus */
209         uiButSetFocusOnEnter(win, but);
210         /* this type of search menu requires undo */
211         but->flag |= UI_BUT_UNDO;
212         
213         return block;
214 }
215
216 /************************ ID Template ***************************/
217 /* This is for browsing and editing the ID-blocks used */
218
219 /* for new/open operators */
220 void uiIDContextProperty(bContext *C, PointerRNA *ptr, PropertyRNA **prop)
221 {
222         TemplateID *template;
223         ARegion *ar = CTX_wm_region(C);
224         uiBlock *block;
225         uiBut *but;
226
227         memset(ptr, 0, sizeof(*ptr));
228         *prop = NULL;
229
230         if (!ar)
231                 return;
232
233         for (block = ar->uiblocks.first; block; block = block->next) {
234                 for (but = block->buttons.first; but; but = but->next) {
235                         /* find the button before the active one */
236                         if ((but->flag & (UI_BUT_LAST_ACTIVE | UI_ACTIVE))) {
237                                 if (but->func_argN) {
238                                         template = but->func_argN;
239                                         *ptr = template->ptr;
240                                         *prop = template->prop;
241                                         return;
242                                 }
243                         }
244                 }
245         }
246 }
247
248
249 static void template_id_cb(bContext *C, void *arg_litem, void *arg_event)
250 {
251         TemplateID *template = (TemplateID *)arg_litem;
252         PointerRNA idptr = RNA_property_pointer_get(&template->ptr, template->prop);
253         ID *id = idptr.data;
254         int event = GET_INT_FROM_POINTER(arg_event);
255         
256         switch (event) {
257                 case UI_ID_BROWSE:
258                 case UI_ID_PIN:
259                         RNA_warning("warning, id event %d shouldnt come here", event);
260                         break;
261                 case UI_ID_OPEN:
262                 case UI_ID_ADD_NEW:
263                         /* these call uiIDContextProperty */
264                         break;
265                 case UI_ID_DELETE:
266                         memset(&idptr, 0, sizeof(idptr));
267                         RNA_property_pointer_set(&template->ptr, template->prop, idptr);
268                         RNA_property_update(C, &template->ptr, template->prop);
269
270                         if (id && CTX_wm_window(C)->eventstate->shift) /* useful hidden functionality, */
271                                 id->us = 0;
272
273                         break;
274                 case UI_ID_FAKE_USER:
275                         if (id) {
276                                 if (id->flag & LIB_FAKEUSER) id_us_plus(id);
277                                 else id_us_min(id);
278                         }
279                         else return;
280                         break;
281                 case UI_ID_LOCAL:
282                         if (id) {
283                                 if (id_make_local(id, 0)) {
284                                         /* reassign to get get proper updates/notifiers */
285                                         idptr = RNA_property_pointer_get(&template->ptr, template->prop);
286                                         RNA_property_pointer_set(&template->ptr, template->prop, idptr);
287                                         RNA_property_update(C, &template->ptr, template->prop);
288                                 }
289                         }
290                         break;
291                 case UI_ID_ALONE:
292                         if (id) {
293                                 const int do_scene_obj = (GS(id->name) == ID_OB) &&
294                                                          (template->ptr.type == &RNA_SceneObjects);
295
296                                 /* make copy */
297                                 if (do_scene_obj) {
298                                         Scene *scene = CTX_data_scene(C);
299                                         ED_object_single_user(scene, (struct Object *)id);
300                                         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
301                                 }
302                                 else {
303                                         if (id) {
304                                                 id_single_user(C, id, &template->ptr, template->prop);
305                                         }
306                                 }
307                         }
308                         break;
309 #if 0
310                 case UI_ID_AUTO_NAME:
311                         break;
312 #endif
313         }
314 }
315
316 static const char *template_id_browse_tip(StructRNA *type)
317 {
318         if (type) {
319                 switch (RNA_type_to_ID_code(type)) {
320                         case ID_SCE: return N_("Browse Scene to be linked");
321                         case ID_OB:  return N_("Browse Object to be linked");
322                         case ID_ME:  return N_("Browse Mesh Data to be linked");
323                         case ID_CU:  return N_("Browse Curve Data to be linked");
324                         case ID_MB:  return N_("Browse Metaball Data to be linked");
325                         case ID_MA:  return N_("Browse Material to be linked");
326                         case ID_TE:  return N_("Browse Texture to be linked");
327                         case ID_IM:  return N_("Browse Image to be linked");
328                         case ID_LS:  return N_("Browse Line Style Data to be linked");
329                         case ID_LT:  return N_("Browse Lattice Data to be linked");
330                         case ID_LA:  return N_("Browse Lamp Data to be linked");
331                         case ID_CA:  return N_("Browse Camera Data to be linked");
332                         case ID_WO:  return N_("Browse World Settings to be linked");
333                         case ID_SCR: return N_("Choose Screen lay-out");
334                         case ID_TXT: return N_("Browse Text to be linked");
335                         case ID_SPK: return N_("Browse Speaker Data to be linked");
336                         case ID_SO:  return N_("Browse Sound to be linked");
337                         case ID_AR:  return N_("Browse Armature data to be linked");
338                         case ID_AC:  return N_("Browse Action to be linked");
339                         case ID_NT:  return N_("Browse Node Tree to be linked");
340                         case ID_BR:  return N_("Browse Brush to be linked");
341                         case ID_PA:  return N_("Browse Particle System to be linked");
342                         case ID_GD:  return N_("Browse Grease Pencil Data to be linked");
343                 }
344         }
345         return N_("Browse ID data to be linked");
346 }
347
348 /* Return a type-based i18n context, needed e.g. by "New" button.
349  * In most languages, this adjective takes different form based on gender of type name...
350  */
351 static const char *template_id_context(StructRNA *type)
352 {
353         if (type) {
354                 switch (RNA_type_to_ID_code(type)) {
355                         case ID_SCE: return BLF_I18NCONTEXT_ID_SCENE;
356                         case ID_OB:  return BLF_I18NCONTEXT_ID_OBJECT;
357                         case ID_ME:  return BLF_I18NCONTEXT_ID_MESH;
358                         case ID_CU:  return BLF_I18NCONTEXT_ID_CURVE;
359                         case ID_MB:  return BLF_I18NCONTEXT_ID_METABALL;
360                         case ID_MA:  return BLF_I18NCONTEXT_ID_MATERIAL;
361                         case ID_TE:  return BLF_I18NCONTEXT_ID_TEXTURE;
362                         case ID_IM:  return BLF_I18NCONTEXT_ID_IMAGE;
363                         case ID_LS:  return BLF_I18NCONTEXT_ID_FREESTYLELINESTYLE;
364                         case ID_LT:  return BLF_I18NCONTEXT_ID_LATTICE;
365                         case ID_LA:  return BLF_I18NCONTEXT_ID_LAMP;
366                         case ID_CA:  return BLF_I18NCONTEXT_ID_CAMERA;
367                         case ID_WO:  return BLF_I18NCONTEXT_ID_WORLD;
368                         case ID_SCR: return BLF_I18NCONTEXT_ID_SCREEN;
369                         case ID_TXT: return BLF_I18NCONTEXT_ID_TEXT;
370                         case ID_SPK: return BLF_I18NCONTEXT_ID_SPEAKER;
371                         case ID_SO:  return BLF_I18NCONTEXT_ID_SOUND;
372                         case ID_AR:  return BLF_I18NCONTEXT_ID_ARMATURE;
373                         case ID_AC:  return BLF_I18NCONTEXT_ID_ACTION;
374                         case ID_NT:  return BLF_I18NCONTEXT_ID_NODETREE;
375                         case ID_BR:  return BLF_I18NCONTEXT_ID_BRUSH;
376                         case ID_PA:  return BLF_I18NCONTEXT_ID_PARTICLESETTINGS;
377                         case ID_GD:  return BLF_I18NCONTEXT_ID_GPENCIL;
378                 }
379         }
380         return BLF_I18NCONTEXT_DEFAULT;
381 }
382
383 static void template_ID(bContext *C, uiLayout *layout, TemplateID *template, StructRNA *type, short idcode, int flag,
384                         const char *newop, const char *openop, const char *unlinkop)
385 {
386         uiBut *but;
387         uiBlock *block;
388         PointerRNA idptr;
389         // ListBase *lb; // UNUSED
390         ID *id, *idfrom;
391         int editable = RNA_property_editable(&template->ptr, template->prop);
392         const char *i18n_ctxt = template_id_context(type);
393
394         idptr = RNA_property_pointer_get(&template->ptr, template->prop);
395         id = idptr.data;
396         idfrom = template->ptr.id.data;
397         // lb = template->idlb;
398
399         block = uiLayoutGetBlock(layout);
400         uiBlockBeginAlign(block);
401
402         if (idptr.type)
403                 type = idptr.type;
404
405         if (flag & UI_ID_PREVIEWS) {
406                 template->preview = TRUE;
407
408                 but = uiDefBlockButN(block, id_search_menu, MEM_dupallocN(template), "", 0, 0, UI_UNIT_X * 6, UI_UNIT_Y * 6,
409                                      TIP_(template_id_browse_tip(type)));
410                 if (type) {
411                         but->icon = RNA_struct_ui_icon(type);
412                         if (id) but->icon = ui_id_icon_get(C, id, 1);
413                         uiButSetFlag(but, UI_HAS_ICON | UI_ICON_PREVIEW);
414                 }
415                 if ((idfrom && idfrom->lib) || !editable)
416                         uiButSetFlag(but, UI_BUT_DISABLED);
417                 
418                 uiLayoutRow(layout, TRUE);
419         }
420         else if (flag & UI_ID_BROWSE) {
421                 but = uiDefBlockButN(block, id_search_menu, MEM_dupallocN(template), "", 0, 0, UI_UNIT_X * 1.6, UI_UNIT_Y,
422                                      TIP_(template_id_browse_tip(type)));
423
424                 uiButSetDrawFlag(but, UI_BUT_DRAW_ENUM_ARROWS);
425
426                 if (type) {
427                         but->icon = RNA_struct_ui_icon(type);
428                         /* default dragging of icon for id browse buttons */
429                         uiButSetDragID(but, id);
430                         uiButSetFlag(but, UI_HAS_ICON | UI_ICON_LEFT);
431                 }
432
433                 if ((idfrom && idfrom->lib) || !editable)
434                         uiButSetFlag(but, UI_BUT_DISABLED);
435         }
436
437         /* text button with name */
438         if (id) {
439                 char name[UI_MAX_NAME_STR];
440                 const short user_alert = (id->us <= 0);
441
442                 //text_idbutton(id, name);
443                 name[0] = '\0';
444                 but = uiDefButR(block, TEX, 0, name, 0, 0, UI_UNIT_X * 6, UI_UNIT_Y,
445                                 &idptr, "name", -1, 0, 0, -1, -1, RNA_struct_ui_description(type));
446                 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_RENAME));
447                 if (user_alert) uiButSetFlag(but, UI_BUT_REDALERT);
448
449                 if (id->lib) {
450                         if (id->flag & LIB_INDIRECT) {
451                                 but = uiDefIconBut(block, BUT, 0, ICON_LIBRARY_DATA_INDIRECT, 0, 0, UI_UNIT_X, UI_UNIT_Y,
452                                                    NULL, 0, 0, 0, 0, TIP_("Indirect library datablock, cannot change"));
453                                 uiButSetFlag(but, UI_BUT_DISABLED);
454                         }
455                         else {
456                                 but = uiDefIconBut(block, BUT, 0, ICON_LIBRARY_DATA_DIRECT, 0, 0, UI_UNIT_X, UI_UNIT_Y,
457                                                    NULL, 0, 0, 0, 0, TIP_("Direct linked library datablock, click to make local"));
458                                 if (!id_make_local(id, 1 /* test */) || (idfrom && idfrom->lib))
459                                         uiButSetFlag(but, UI_BUT_DISABLED);
460                         }
461
462                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_LOCAL));
463                 }
464
465                 if (id->us > 1) {
466                         char numstr[32];
467
468                         BLI_snprintf(numstr, sizeof(numstr), "%d", id->us);
469
470                         but = uiDefBut(block, BUT, 0, numstr, 0, 0, UI_UNIT_X + ((id->us < 10) ? 0 : 10), UI_UNIT_Y,
471                                        NULL, 0, 0, 0, 0,
472                                        TIP_("Display number of users of this data (click to make a single-user copy)"));
473
474                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ALONE));
475                         if (/* test only */
476                             (id_copy(id, NULL, 1) == FALSE) ||
477                             (idfrom && idfrom->lib) ||
478                             (editable == FALSE) ||
479                             /* object in editmode - don't change data */
480                             (idfrom && GS(idfrom->name) == ID_OB && (((Object *)idfrom)->mode & OB_MODE_EDIT)))
481                         {
482                                 uiButSetFlag(but, UI_BUT_DISABLED);
483                         }
484                 }
485         
486                 if (user_alert) uiButSetFlag(but, UI_BUT_REDALERT);
487                 
488                 if (id->lib == NULL && !(ELEM5(GS(id->name), ID_GR, ID_SCE, ID_SCR, ID_TXT, ID_OB))) {
489                         uiDefButR(block, TOG, 0, "F", 0, 0, UI_UNIT_X, UI_UNIT_Y, &idptr, "use_fake_user", -1, 0, 0, -1, -1, NULL);
490                 }
491         }
492         
493         if (flag & UI_ID_ADD_NEW) {
494                 int w = id ? UI_UNIT_X : (flag & UI_ID_OPEN) ? UI_UNIT_X * 3 : UI_UNIT_X * 6;
495                 
496                 /* i18n markup, does nothing! */
497                 BLF_I18N_MSGID_MULTI_CTXT("New", BLF_I18NCONTEXT_DEFAULT,
498                                                  BLF_I18NCONTEXT_ID_SCENE,
499                                                  BLF_I18NCONTEXT_ID_OBJECT,
500                                                  BLF_I18NCONTEXT_ID_MESH,
501                                                  BLF_I18NCONTEXT_ID_CURVE,
502                                                  BLF_I18NCONTEXT_ID_METABALL,
503                                                  BLF_I18NCONTEXT_ID_MATERIAL,
504                                                  BLF_I18NCONTEXT_ID_TEXTURE,
505                                                  BLF_I18NCONTEXT_ID_IMAGE,
506                                                  BLF_I18NCONTEXT_ID_LATTICE,
507                                                  BLF_I18NCONTEXT_ID_LAMP,
508                                                  BLF_I18NCONTEXT_ID_CAMERA,
509                                                  BLF_I18NCONTEXT_ID_WORLD,
510                                                  BLF_I18NCONTEXT_ID_SCREEN,
511                                                  BLF_I18NCONTEXT_ID_TEXT);
512                 BLF_I18N_MSGID_MULTI_CTXT("New", BLF_I18NCONTEXT_ID_SPEAKER,
513                                                  BLF_I18NCONTEXT_ID_SOUND,
514                                                  BLF_I18NCONTEXT_ID_ARMATURE,
515                                                  BLF_I18NCONTEXT_ID_ACTION,
516                                                  BLF_I18NCONTEXT_ID_NODETREE,
517                                                  BLF_I18NCONTEXT_ID_BRUSH,
518                                                  BLF_I18NCONTEXT_ID_PARTICLESETTINGS,
519                                                  BLF_I18NCONTEXT_ID_GPENCIL,
520                                                                                  BLF_I18NCONTEXT_ID_FREESTYLELINESTYLE);
521                 
522                 if (newop) {
523                         but = uiDefIconTextButO(block, BUT, newop, WM_OP_INVOKE_DEFAULT, ICON_ZOOMIN,
524                                                 (id) ? "" : CTX_IFACE_(i18n_ctxt, "New"), 0, 0, w, UI_UNIT_Y, NULL);
525                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ADD_NEW));
526                 }
527                 else {
528                         but = uiDefIconTextBut(block, BUT, 0, ICON_ZOOMIN, (id) ? "" : CTX_IFACE_(i18n_ctxt, "New"),
529                                                0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
530                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ADD_NEW));
531                 }
532
533                 if ((idfrom && idfrom->lib) || !editable)
534                         uiButSetFlag(but, UI_BUT_DISABLED);
535         }
536
537         if (flag & UI_ID_OPEN) {
538                 int w = id ? UI_UNIT_X : (flag & UI_ID_ADD_NEW) ? UI_UNIT_X * 3 : UI_UNIT_X * 6;
539                 
540                 if (openop) {
541                         but = uiDefIconTextButO(block, BUT, openop, WM_OP_INVOKE_DEFAULT, ICON_FILESEL, (id) ? "" : IFACE_("Open"),
542                                                 0, 0, w, UI_UNIT_Y, NULL);
543                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_OPEN));
544                 }
545                 else {
546                         but = uiDefIconTextBut(block, BUT, 0, ICON_FILESEL, (id) ? "" : IFACE_("Open"), 0, 0, w, UI_UNIT_Y,
547                                                NULL, 0, 0, 0, 0, NULL);
548                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_OPEN));
549                 }
550
551                 if ((idfrom && idfrom->lib) || !editable)
552                         uiButSetFlag(but, UI_BUT_DISABLED);
553         }
554         
555         /* delete button */
556         if (id && (flag & UI_ID_DELETE) && (RNA_property_flag(template->prop) & PROP_NEVER_UNLINK) == 0) {
557                 if (unlinkop) {
558                         but = uiDefIconButO(block, BUT, unlinkop, WM_OP_INVOKE_REGION_WIN, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL);
559                         /* so we can access the template from operators, font unlinking needs this */
560                         uiButSetNFunc(but, NULL, MEM_dupallocN(template), NULL);
561                 }
562                 else {
563                         but = uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0, 0, 0, 0,
564                                            TIP_("Unlink datablock "
565                                                 "(Shift + Click to set users to zero, data will then not be saved)"));
566                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_DELETE));
567
568                         if (RNA_property_flag(template->prop) & PROP_NEVER_NULL)
569                                 uiButSetFlag(but, UI_BUT_DISABLED);
570                 }
571
572                 if ((idfrom && idfrom->lib) || !editable)
573                         uiButSetFlag(but, UI_BUT_DISABLED);
574         }
575
576         if (idcode == ID_TE)
577                 uiTemplateTextureShow(layout, C, &template->ptr, template->prop);
578         
579         uiBlockEndAlign(block);
580 }
581
582 static void ui_template_id(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
583                            const char *openop, const char *unlinkop, int flag, int prv_rows, int prv_cols)
584 {
585         TemplateID *template;
586         PropertyRNA *prop;
587         StructRNA *type;
588         short idcode;
589
590         prop = RNA_struct_find_property(ptr, propname);
591
592         if (!prop || RNA_property_type(prop) != PROP_POINTER) {
593                 RNA_warning("pointer property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
594                 return;
595         }
596
597         template = MEM_callocN(sizeof(TemplateID), "TemplateID");
598         template->ptr = *ptr;
599         template->prop = prop;
600         template->prv_rows = prv_rows;
601         template->prv_cols = prv_cols;
602
603         if (newop)
604                 flag |= UI_ID_ADD_NEW;
605         if (openop)
606                 flag |= UI_ID_OPEN;
607
608         type = RNA_property_pointer_type(ptr, prop);
609         idcode = RNA_type_to_ID_code(type);
610         template->idlb = which_libbase(CTX_data_main(C), idcode);
611         
612         /* create UI elements for this template
613          *      - template_ID makes a copy of the template data and assigns it to the relevant buttons
614          */
615         if (template->idlb) {
616                 uiLayoutRow(layout, TRUE);
617                 template_ID(C, layout, template, type, idcode, flag, newop, openop, unlinkop);
618         }
619
620         MEM_freeN(template);
621 }
622
623 void uiTemplateID(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
624                   const char *openop, const char *unlinkop)
625 {
626         ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop,
627                        UI_ID_BROWSE | UI_ID_RENAME | UI_ID_DELETE, 0, 0);
628 }
629
630 void uiTemplateIDBrowse(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
631                         const char *openop, const char *unlinkop)
632 {
633         ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop, UI_ID_BROWSE | UI_ID_RENAME, 0, 0);
634 }
635
636 void uiTemplateIDPreview(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
637                          const char *openop, const char *unlinkop, int rows, int cols)
638 {
639         ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop,
640                        UI_ID_BROWSE | UI_ID_RENAME | UI_ID_DELETE | UI_ID_PREVIEWS, rows, cols);
641 }
642
643 /************************ ID Chooser Template ***************************/
644
645 /* This is for selecting the type of ID-block to use, and then from the relevant type choosing the block to use 
646  *
647  * - propname: property identifier for property that ID-pointer gets stored to
648  * - proptypename: property identifier for property used to determine the type of ID-pointer that can be used
649  */
650 void uiTemplateAnyID(uiLayout *layout, PointerRNA *ptr, const char *propname, const char *proptypename,
651                      const char *text)
652 {
653         PropertyRNA *propID, *propType;
654         uiLayout *row;
655         
656         /* get properties... */
657         propID = RNA_struct_find_property(ptr, propname);
658         propType = RNA_struct_find_property(ptr, proptypename);
659
660         if (!propID || RNA_property_type(propID) != PROP_POINTER) {
661                 RNA_warning("pointer property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
662                 return;
663         }
664         if (!propType || RNA_property_type(propType) != PROP_ENUM) {
665                 RNA_warning("pointer-type property not found: %s.%s", RNA_struct_identifier(ptr->type), proptypename);
666                 return;
667         }
668         
669         /* Start drawing UI Elements using standard defines */
670         row = uiLayoutRow(layout, TRUE);
671         
672         /* Label - either use the provided text, or will become "ID-Block:" */
673         if (text) {
674                 if (text[0])
675                         uiItemL(row, text, ICON_NONE);
676         }
677         else
678                 uiItemL(row, "ID-Block:", ICON_NONE);
679         
680         /* ID-Type Selector - just have a menu of icons */
681         /* FIXME: the icon-only setting doesn't work when we supply a blank name */
682         uiItemFullR(row, ptr, propType, 0, 0, UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
683         
684         /* ID-Block Selector - just use pointer widget... */
685         uiItemFullR(row, ptr, propID, 0, 0, 0, "", ICON_NONE);
686 }
687
688 /********************* RNA Path Builder Template ********************/
689
690 /* ---------- */
691
692 /* This is creating/editing RNA-Paths 
693  *
694  * - ptr: struct which holds the path property
695  * - propname: property identifier for property that path gets stored to
696  * - root_ptr: struct that path gets built from
697  */
698 void uiTemplatePathBuilder(uiLayout *layout, PointerRNA *ptr, const char *propname, PointerRNA *UNUSED(root_ptr),
699                            const char *text)
700 {
701         PropertyRNA *propPath;
702         uiLayout *row;
703         
704         /* check that properties are valid */
705         propPath = RNA_struct_find_property(ptr, propname);
706         if (!propPath || RNA_property_type(propPath) != PROP_STRING) {
707                 RNA_warning("path property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
708                 return;
709         }
710         
711         /* Start drawing UI Elements using standard defines */
712         row = uiLayoutRow(layout, TRUE);
713         
714         /* Path (existing string) Widget */
715         uiItemR(row, ptr, propname, 0, text, ICON_RNA);
716         
717         /* TODO: attach something to this to make allow searching of nested properties to 'build' the path */
718 }
719
720 /************************ Modifier Template *************************/
721
722 #define ERROR_LIBDATA_MESSAGE "Can't edit external libdata"
723
724 #include <string.h>
725
726 #include "DNA_object_force.h"
727
728 #include "BKE_depsgraph.h"
729 #include "BKE_modifier.h"
730 #include "BKE_particle.h"
731
732 #include "ED_util.h"
733
734 #include "BLI_math.h"
735 #include "BLI_listbase.h"
736
737 #include "ED_object.h"
738
739 static void modifiers_setOnCage(bContext *C, void *ob_v, void *md_v)
740 {
741         Scene *scene = CTX_data_scene(C);
742         Object *ob = ob_v;
743         ModifierData *md = md_v;
744         int i, cageIndex = modifiers_getCageIndex(scene, ob, NULL, 0);
745
746         /* undo button operation */
747         md->mode ^= eModifierMode_OnCage;
748
749         for (i = 0, md = ob->modifiers.first; md; ++i, md = md->next) {
750                 if (md == md_v) {
751                         if (i >= cageIndex)
752                                 md->mode ^= eModifierMode_OnCage;
753                         break;
754                 }
755         }
756
757         WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
758         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
759 }
760
761 static void modifiers_convertToReal(bContext *C, void *ob_v, void *md_v)
762 {
763         Object *ob = ob_v;
764         ModifierData *md = md_v;
765         ModifierData *nmd = modifier_new(md->type);
766
767         modifier_copyData(md, nmd);
768         nmd->mode &= ~eModifierMode_Virtual;
769
770         BLI_addhead(&ob->modifiers, nmd);
771         
772         modifier_unique_name(&ob->modifiers, nmd);
773
774         ob->partype = PAROBJECT;
775
776         WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
777         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
778
779         ED_undo_push(C, "Modifier convert to real");
780 }
781
782 static int modifier_can_delete(ModifierData *md)
783 {
784         /* fluid particle modifier can't be deleted here */
785         if (md->type == eModifierType_ParticleSystem)
786                 if (((ParticleSystemModifierData *)md)->psys->part->type == PART_FLUID)
787                         return 0;
788
789         return 1;
790 }
791
792 /* Check whether Modifier is a simulation or not, this is used for switching to the physics/particles context tab */
793 static int modifier_is_simulation(ModifierData *md)
794 {
795         /* Physic Tab */
796         if (ELEM7(md->type, eModifierType_Cloth, eModifierType_Collision, eModifierType_Fluidsim, eModifierType_Smoke,
797                   eModifierType_Softbody, eModifierType_Surface, eModifierType_DynamicPaint))
798         {
799                 return 1;
800         }
801         /* Particle Tab */
802         else if (md->type == eModifierType_ParticleSystem) {
803                 return 2;
804         }
805         else {
806                 return 0;
807         }
808 }
809
810 static uiLayout *draw_modifier(uiLayout *layout, Scene *scene, Object *ob,
811                                ModifierData *md, int index, int cageIndex, int lastCageIndex)
812 {
813         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
814         PointerRNA ptr;
815         uiBut *but;
816         uiBlock *block;
817         uiLayout *box, *column, *row;
818         uiLayout *result = NULL;
819         int isVirtual = (md->mode & eModifierMode_Virtual);
820         char str[128];
821
822         /* create RNA pointer */
823         RNA_pointer_create(&ob->id, &RNA_Modifier, md, &ptr);
824
825         column = uiLayoutColumn(layout, TRUE);
826         uiLayoutSetContextPointer(column, "modifier", &ptr);
827
828         /* rounded header ------------------------------------------------------------------- */
829         box = uiLayoutBox(column);
830         
831         if (isVirtual) {
832                 row = uiLayoutRow(box, FALSE);
833                 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_EXPAND);
834                 block = uiLayoutGetBlock(row);
835                 /* VIRTUAL MODIFIER */
836                 /* XXX this is not used now, since these cannot be accessed via RNA */
837                 BLI_snprintf(str, sizeof(str), "%s parent deform", md->name);
838                 uiDefBut(block, LABEL, 0, str, 0, 0, 185, UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, "Modifier name"); 
839                 
840                 but = uiDefBut(block, BUT, 0, IFACE_("Make Real"), 0, 0, 80, 16, NULL, 0.0, 0.0, 0.0, 0.0,
841                                TIP_("Convert virtual modifier to a real modifier"));
842                 uiButSetFunc(but, modifiers_convertToReal, ob, md);
843         }
844         else {
845                 /* REAL MODIFIER */
846                 row = uiLayoutRow(box, FALSE);
847                 block = uiLayoutGetBlock(row);
848                 
849                 uiBlockSetEmboss(block, UI_EMBOSSN);
850                 /* Open/Close .................................  */
851                 uiItemR(row, &ptr, "show_expanded", 0, "", ICON_NONE);
852                 
853                 /* modifier-type icon */
854                 uiItemL(row, "", RNA_struct_ui_icon(ptr.type));
855                 uiBlockSetEmboss(block, UI_EMBOSS);
856                 
857                 /* modifier name */
858                 uiItemR(row, &ptr, "name", 0, "", ICON_NONE);
859                 
860                 /* mode enabling buttons */
861                 uiBlockBeginAlign(block);
862                 /* Softbody not allowed in this situation, enforce! */
863                 if (((md->type != eModifierType_Softbody && md->type != eModifierType_Collision) || !(ob->pd && ob->pd->deflect)) &&
864                     (md->type != eModifierType_Surface) )
865                 {
866                         uiItemR(row, &ptr, "show_render", 0, "", ICON_NONE);
867                         uiItemR(row, &ptr, "show_viewport", 0, "", ICON_NONE);
868                         
869                         if (mti->flags & eModifierTypeFlag_SupportsEditmode)
870                                 uiItemR(row, &ptr, "show_in_editmode", 0, "", ICON_NONE);
871                 }
872
873                 if (ob->type == OB_MESH) {
874                         if (modifier_couldBeCage(scene, md) && (index <= lastCageIndex)) {
875                                 /* -- convert to rna ? */
876                                 but = uiDefIconButBitI(block, TOG, eModifierMode_OnCage, 0, ICON_MESH_DATA, 0, 0,
877                                                        UI_UNIT_X - 2, UI_UNIT_Y, &md->mode, 0.0, 0.0, 0.0, 0.0,
878                                                        TIP_("Apply modifier to editing cage during Edit mode"));
879                                 if (index < cageIndex)
880                                         uiButSetFlag(but, UI_BUT_DISABLED);
881                                 uiButSetFunc(but, modifiers_setOnCage, ob, md);
882                         }
883                         else {
884                                 uiBlockEndAlign(block);
885
886                                 /* place holder button */
887                                 uiBlockSetEmboss(block, UI_EMBOSSN);
888                                 but = uiDefIconBut(block, BUT, 0, ICON_NONE, 0, 0, UI_UNIT_X - 2, UI_UNIT_Y,
889                                                    NULL, 0.0, 0.0, 0.0, 0.0, NULL);
890                                 uiButSetFlag(but, UI_BUT_DISABLED);
891                                 uiBlockSetEmboss(block, UI_EMBOSS);
892                         }
893                 } /* tessellation point for curve-typed objects */
894                 else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
895                         /* some modifiers could work with pre-tessellated curves only */
896                         if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
897                                 /* add disabled pre-tessellated button, so users could have
898                                  * message for this modifiers */
899                                 but = uiDefIconButBitI(block, TOG, eModifierMode_ApplyOnSpline, 0, ICON_SURFACE_DATA, 0, 0,
900                                                        UI_UNIT_X - 2, UI_UNIT_Y, &md->mode, 0.0, 0.0, 0.0, 0.0,
901                                                        TIP_("This modifier could be applied on splines' points only"));
902                                 uiButSetFlag(but, UI_BUT_DISABLED);
903                         }
904                         else if (mti->type != eModifierTypeType_Constructive) {
905                                 /* constructive modifiers tessellates curve before applying */
906                                 uiItemR(row, &ptr, "use_apply_on_spline", 0, "", ICON_NONE);
907                         }
908                 }
909
910                 uiBlockEndAlign(block);
911                 
912                 /* Up/Down + Delete ........................... */
913                 uiBlockBeginAlign(block);
914                 uiItemO(row, "", ICON_TRIA_UP, "OBJECT_OT_modifier_move_up");
915                 uiItemO(row, "", ICON_TRIA_DOWN, "OBJECT_OT_modifier_move_down");
916                 uiBlockEndAlign(block);
917                 
918                 uiBlockSetEmboss(block, UI_EMBOSSN);
919                 /* When Modifier is a simulation, show button to switch to context rather than the delete button. */
920                 if (modifier_can_delete(md) && !modifier_is_simulation(md))
921                         uiItemO(row, "", ICON_X, "OBJECT_OT_modifier_remove");
922                 if (modifier_is_simulation(md) == 1)
923                         uiItemStringO(row, "", ICON_BUTS, "WM_OT_properties_context_change", "context", "PHYSICS");
924                 else if (modifier_is_simulation(md) == 2)
925                         uiItemStringO(row, "", ICON_BUTS, "WM_OT_properties_context_change", "context", "PARTICLES");
926                 uiBlockSetEmboss(block, UI_EMBOSS);
927         }
928
929         
930         /* modifier settings (under the header) --------------------------------------------------- */
931         if (!isVirtual && (md->mode & eModifierMode_Expanded)) {
932                 /* apply/convert/copy */
933                 box = uiLayoutBox(column);
934                 row = uiLayoutRow(box, FALSE);
935                 
936                 if (!ELEM(md->type, eModifierType_Collision, eModifierType_Surface)) {
937                         /* only here obdata, the rest of modifiers is ob level */
938                         uiBlockSetButLock(block, BKE_object_obdata_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
939                         
940                         if (md->type == eModifierType_ParticleSystem) {
941                                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
942                                 
943                                 if (!(ob->mode & OB_MODE_PARTICLE_EDIT) && psys->pathcache) {
944                                         if (ELEM(psys->part->ren_as, PART_DRAW_GR, PART_DRAW_OB))
945                                                 uiItemO(row, "Convert", ICON_NONE, "OBJECT_OT_duplicates_make_real");
946                                         else if (psys->part->ren_as == PART_DRAW_PATH)
947                                                 uiItemO(row, "Convert", ICON_NONE, "OBJECT_OT_modifier_convert");
948                                 }
949                         }
950                         else {
951                                 uiLayoutSetOperatorContext(row, WM_OP_INVOKE_DEFAULT);
952                                 uiItemEnumO(row, "OBJECT_OT_modifier_apply", IFACE_("Apply"), 0, "apply_as", MODIFIER_APPLY_DATA);
953                                 
954                                 if (modifier_isSameTopology(md) && !modifier_isNonGeometrical(md)) {
955                                         uiItemEnumO(row, "OBJECT_OT_modifier_apply", IFACE_("Apply as Shape Key"), 0,
956                                                     "apply_as", MODIFIER_APPLY_SHAPE);
957                                 }
958                         }
959                         
960                         uiBlockClearButLock(block);
961                         uiBlockSetButLock(block, ob && ob->id.lib, ERROR_LIBDATA_MESSAGE);
962                         
963                         if (!ELEM5(md->type, eModifierType_Fluidsim, eModifierType_Softbody, eModifierType_ParticleSystem,
964                                    eModifierType_Cloth, eModifierType_Smoke))
965                         {
966                                 uiItemO(row, IFACE_("Copy"), ICON_NONE, "OBJECT_OT_modifier_copy");
967                         }
968                 }
969                 
970                 /* result is the layout block inside the box, that we return so that modifier settings can be drawn */
971                 result = uiLayoutColumn(box, FALSE);
972                 block = uiLayoutAbsoluteBlock(box);
973         }
974         
975         /* error messages */
976         if (md->error) {
977                 box = uiLayoutBox(column);
978                 row = uiLayoutRow(box, FALSE);
979                 uiItemL(row, md->error, ICON_ERROR);
980         }
981         
982         return result;
983 }
984
985 uiLayout *uiTemplateModifier(uiLayout *layout, bContext *C, PointerRNA *ptr)
986 {
987         Scene *scene = CTX_data_scene(C);
988         Object *ob;
989         ModifierData *md, *vmd;
990         int i, lastCageIndex, cageIndex;
991
992         /* verify we have valid data */
993         if (!RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
994                 RNA_warning("Expected modifier on object");
995                 return NULL;
996         }
997
998         ob = ptr->id.data;
999         md = ptr->data;
1000
1001         if (!ob || !(GS(ob->id.name) == ID_OB)) {
1002                 RNA_warning("Expected modifier on object");
1003                 return NULL;
1004         }
1005         
1006         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1007         
1008         /* find modifier and draw it */
1009         cageIndex = modifiers_getCageIndex(scene, ob, &lastCageIndex, 0);
1010
1011         /* XXX virtual modifiers are not accesible for python */
1012         vmd = modifiers_getVirtualModifierList(ob);
1013
1014         for (i = 0; vmd; i++, vmd = vmd->next) {
1015                 if (md == vmd)
1016                         return draw_modifier(layout, scene, ob, md, i, cageIndex, lastCageIndex);
1017                 else if (vmd->mode & eModifierMode_Virtual)
1018                         i--;
1019         }
1020
1021         return NULL;
1022 }
1023
1024 /************************ Constraint Template *************************/
1025
1026 #include "DNA_constraint_types.h"
1027
1028 #include "BKE_action.h"
1029 #include "BKE_constraint.h"
1030
1031 #define B_CONSTRAINT_TEST           5
1032 // #define B_CONSTRAINT_CHANGETARGET   6
1033
1034 static void do_constraint_panels(bContext *C, void *ob_pt, int event)
1035 {
1036         Object *ob = (Object *)ob_pt;
1037
1038         switch (event) {
1039                 case B_CONSTRAINT_TEST:
1040                         break; /* no handling */
1041 #if 0   /* UNUSED */
1042                 case B_CONSTRAINT_CHANGETARGET:
1043                 {
1044                         Main *bmain = CTX_data_main(C);
1045                         Scene *scene = CTX_data_scene(C);
1046                         if (ob->pose) ob->pose->flag |= POSE_RECALC;  /* checks & sorts pose channels */
1047                         DAG_scene_sort(bmain, scene);
1048                         break;
1049                 }
1050 #endif
1051                 default:
1052                         break;
1053         }
1054
1055         /* note: RNA updates now call this, commenting else it gets called twice.
1056          * if there are problems because of this, then rna needs changed update functions.
1057          *
1058          * object_test_constraints(ob);
1059          * if (ob->pose) BKE_pose_update_constraint_flags(ob->pose); */
1060         
1061         if (ob->type == OB_ARMATURE) DAG_id_tag_update(&ob->id, OB_RECALC_DATA | OB_RECALC_OB);
1062         else DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1063
1064         WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
1065 }
1066
1067 static void constraint_active_func(bContext *UNUSED(C), void *ob_v, void *con_v)
1068 {
1069         ED_object_constraint_set_active(ob_v, con_v);
1070 }
1071
1072 /* draw panel showing settings for a constraint */
1073 static uiLayout *draw_constraint(uiLayout *layout, Object *ob, bConstraint *con)
1074 {
1075         bPoseChannel *pchan = BKE_pose_channel_active(ob);
1076         bConstraintTypeInfo *cti;
1077         uiBlock *block;
1078         uiLayout *result = NULL, *col, *box, *row;
1079         PointerRNA ptr;
1080         char typestr[32];
1081         short proxy_protected, xco = 0, yco = 0;
1082         // int rb_col; // UNUSED
1083
1084         /* get constraint typeinfo */
1085         cti = constraint_get_typeinfo(con);
1086         if (cti == NULL) {
1087                 /* exception for 'Null' constraint - it doesn't have constraint typeinfo! */
1088                 BLI_strncpy(typestr, (con->type == CONSTRAINT_TYPE_NULL) ? "Null" : "Unknown", sizeof(typestr));
1089         }
1090         else
1091                 BLI_strncpy(typestr, cti->name, sizeof(typestr));
1092                 
1093         /* determine whether constraint is proxy protected or not */
1094         if (proxylocked_constraints_owner(ob, pchan))
1095                 proxy_protected = (con->flag & CONSTRAINT_PROXY_LOCAL) == 0;
1096         else
1097                 proxy_protected = 0;
1098
1099         /* unless button has own callback, it adds this callback to button */
1100         block = uiLayoutGetBlock(layout);
1101         uiBlockSetHandleFunc(block, do_constraint_panels, ob);
1102         uiBlockSetFunc(block, constraint_active_func, ob, con);
1103
1104         RNA_pointer_create(&ob->id, &RNA_Constraint, con, &ptr);
1105
1106         col = uiLayoutColumn(layout, TRUE);
1107         uiLayoutSetContextPointer(col, "constraint", &ptr);
1108
1109         box = uiLayoutBox(col);
1110         row = uiLayoutRow(box, FALSE);
1111         block = uiLayoutGetBlock(box);
1112
1113         /* Draw constraint header */
1114
1115         /* open/close */
1116         uiBlockSetEmboss(block, UI_EMBOSSN);
1117         uiItemR(row, &ptr, "show_expanded", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
1118         uiBlockSetEmboss(block, UI_EMBOSS);
1119         
1120         /* name */
1121         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr, xco + 10, yco, 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, "");
1122
1123         if (con->flag & CONSTRAINT_DISABLE)
1124                 uiLayoutSetRedAlert(row, TRUE);
1125         
1126         if (proxy_protected == 0) {
1127                 uiItemR(row, &ptr, "name", 0, "", ICON_NONE);
1128         }
1129         else
1130                 uiItemL(row, con->name, ICON_NONE);
1131         
1132         uiLayoutSetRedAlert(row, FALSE);
1133         
1134         /* proxy-protected constraints cannot be edited, so hide up/down + close buttons */
1135         if (proxy_protected) {
1136                 uiBlockSetEmboss(block, UI_EMBOSSN);
1137                 
1138                 /* draw a ghost icon (for proxy) and also a lock beside it, to show that constraint is "proxy locked" */
1139                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_GHOST, xco + 244, yco, 19, 19,
1140                              NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Proxy Protected"));
1141                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_LOCKED, xco + 262, yco, 19, 19,
1142                              NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Proxy Protected"));
1143                 
1144                 uiBlockSetEmboss(block, UI_EMBOSS);
1145         }
1146         else {
1147                 short prev_proxylock, show_upbut, show_downbut;
1148                 
1149                 /* Up/Down buttons: 
1150                  *      Proxy-constraints are not allowed to occur after local (non-proxy) constraints
1151                  *      as that poses problems when restoring them, so disable the "up" button where
1152                  *      it may cause this situation. 
1153                  *
1154                  *  Up/Down buttons should only be shown (or not grayed - todo) if they serve some purpose.
1155                  */
1156                 if (proxylocked_constraints_owner(ob, pchan)) {
1157                         if (con->prev) {
1158                                 prev_proxylock = (con->prev->flag & CONSTRAINT_PROXY_LOCAL) ? 0 : 1;
1159                         }
1160                         else
1161                                 prev_proxylock = 0;
1162                 }
1163                 else
1164                         prev_proxylock = 0;
1165                         
1166                 show_upbut = ((prev_proxylock == 0) && (con->prev));
1167                 show_downbut = (con->next) ? 1 : 0;
1168                 
1169                 /* enabled */
1170                 uiBlockSetEmboss(block, UI_EMBOSSN);
1171                 uiItemR(row, &ptr, "mute", 0, "",
1172                         (con->flag & CONSTRAINT_OFF) ? ICON_RESTRICT_VIEW_ON : ICON_RESTRICT_VIEW_OFF);
1173                 uiBlockSetEmboss(block, UI_EMBOSS);
1174                 
1175                 uiLayoutSetOperatorContext(row, WM_OP_INVOKE_DEFAULT);
1176                 
1177                 /* up/down */
1178                 if (show_upbut || show_downbut) {
1179                         uiBlockBeginAlign(block);
1180                         if (show_upbut)
1181                                 uiItemO(row, "", ICON_TRIA_UP, "CONSTRAINT_OT_move_up");
1182                                 
1183                         if (show_downbut)
1184                                 uiItemO(row, "", ICON_TRIA_DOWN, "CONSTRAINT_OT_move_down");
1185                         uiBlockEndAlign(block);
1186                 }
1187                 
1188                 /* Close 'button' - emboss calls here disable drawing of 'button' behind X */
1189                 uiBlockSetEmboss(block, UI_EMBOSSN);
1190                 uiItemO(row, "", ICON_X, "CONSTRAINT_OT_delete");
1191                 uiBlockSetEmboss(block, UI_EMBOSS);
1192         }
1193
1194         /* Set but-locks for protected settings (magic numbers are used here!) */
1195         if (proxy_protected)
1196                 uiBlockSetButLock(block, 1, "Cannot edit Proxy-Protected Constraint");
1197
1198         /* Draw constraint data */
1199         if ((con->flag & CONSTRAINT_EXPAND) == 0) {
1200                 (yco) -= 21;
1201         }
1202         else {
1203                 box = uiLayoutBox(col);
1204                 block = uiLayoutAbsoluteBlock(box);
1205                 result = box;
1206         }
1207
1208         /* clear any locks set up for proxies/lib-linking */
1209         uiBlockClearButLock(block);
1210
1211         return result;
1212 }
1213
1214 uiLayout *uiTemplateConstraint(uiLayout *layout, PointerRNA *ptr)
1215 {
1216         Object *ob;
1217         bConstraint *con;
1218
1219         /* verify we have valid data */
1220         if (!RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
1221                 RNA_warning("Expected constraint on object");
1222                 return NULL;
1223         }
1224
1225         ob = ptr->id.data;
1226         con = ptr->data;
1227
1228         if (!ob || !(GS(ob->id.name) == ID_OB)) {
1229                 RNA_warning("Expected constraint on object");
1230                 return NULL;
1231         }
1232         
1233         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1234
1235         /* hrms, the temporal constraint should not draw! */
1236         if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
1237                 bKinematicConstraint *data = con->data;
1238                 if (data->flag & CONSTRAINT_IK_TEMP)
1239                         return NULL;
1240         }
1241
1242         return draw_constraint(layout, ob, con);
1243 }
1244
1245
1246 /************************* Preview Template ***************************/
1247
1248 #include "DNA_lamp_types.h"
1249 #include "DNA_material_types.h"
1250 #include "DNA_world_types.h"
1251
1252 #define B_MATPRV 1
1253
1254 static void do_preview_buttons(bContext *C, void *arg, int event)
1255 {
1256         switch (event) {
1257                 case B_MATPRV:
1258                         WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING, arg);
1259                         break;
1260         }
1261 }
1262
1263 void uiTemplatePreview(uiLayout *layout, ID *id, int show_buttons, ID *parent, MTex *slot)
1264 {
1265         uiLayout *row, *col;
1266         uiBlock *block;
1267         Material *ma = NULL;
1268         Tex *tex = (Tex *)id;
1269         ID *pid, *pparent;
1270         short *pr_texture = NULL;
1271         PointerRNA material_ptr;
1272         PointerRNA texture_ptr;
1273
1274         if (id && !ELEM4(GS(id->name), ID_MA, ID_TE, ID_WO, ID_LA)) {
1275                 RNA_warning("Expected ID of type material, texture, lamp or world");
1276                 return;
1277         }
1278
1279         /* decide what to render */
1280         pid = id;
1281         pparent = NULL;
1282
1283         if (id && (GS(id->name) == ID_TE)) {
1284                 if (parent && (GS(parent->name) == ID_MA))
1285                         pr_texture = &((Material *)parent)->pr_texture;
1286                 else if (parent && (GS(parent->name) == ID_WO))
1287                         pr_texture = &((World *)parent)->pr_texture;
1288                 else if (parent && (GS(parent->name) == ID_LA))
1289                         pr_texture = &((Lamp *)parent)->pr_texture;
1290
1291                 if (pr_texture) {
1292                         if (*pr_texture == TEX_PR_OTHER)
1293                                 pid = parent;
1294                         else if (*pr_texture == TEX_PR_BOTH)
1295                                 pparent = parent;
1296                 }
1297         }
1298
1299         /* layout */
1300         block = uiLayoutGetBlock(layout);
1301         row = uiLayoutRow(layout, FALSE);
1302         col = uiLayoutColumn(row, FALSE);
1303         uiLayoutSetKeepAspect(col, TRUE);
1304         
1305         /* add preview */
1306         uiDefBut(block, BUT_EXTRA, 0, "", 0, 0, UI_UNIT_X * 6, UI_UNIT_Y * 6, pid, 0.0, 0.0, 0, 0, "");
1307         uiBlockSetDrawExtraFunc(block, ED_preview_draw, pparent, slot);
1308         uiBlockSetHandleFunc(block, do_preview_buttons, NULL);
1309         
1310         /* add buttons */
1311         if (pid && show_buttons) {
1312                 if (GS(pid->name) == ID_MA || (pparent && GS(pparent->name) == ID_MA)) {
1313                         if (GS(pid->name) == ID_MA) ma = (Material *)pid;
1314                         else ma = (Material *)pparent;
1315                         
1316                         /* Create RNA Pointer */
1317                         RNA_pointer_create(&ma->id, &RNA_Material, ma, &material_ptr);
1318
1319                         col = uiLayoutColumn(row, TRUE);
1320                         uiLayoutSetScaleX(col, 1.5);
1321                         uiItemR(col, &material_ptr, "preview_render_type", UI_ITEM_R_EXPAND, "", ICON_NONE);
1322                 }
1323
1324                 if (pr_texture) {
1325                         /* Create RNA Pointer */
1326                         RNA_pointer_create(id, &RNA_Texture, tex, &texture_ptr);
1327                         
1328                         uiLayoutRow(layout, TRUE);
1329                         uiDefButS(block, ROW, B_MATPRV, IFACE_("Texture"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1330                                   pr_texture, 10, TEX_PR_TEXTURE, 0, 0, "");
1331                         if (GS(parent->name) == ID_MA) {
1332                                 uiDefButS(block, ROW, B_MATPRV, IFACE_("Material"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1333                                           pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1334                         }
1335                         else if (GS(parent->name) == ID_LA) {
1336                                 uiDefButS(block, ROW, B_MATPRV, IFACE_("Lamp"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1337                                           pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1338                         }
1339                         else if (GS(parent->name) == ID_WO) {
1340                                 uiDefButS(block, ROW, B_MATPRV, IFACE_("World"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1341                                           pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1342                         }
1343                         uiDefButS(block, ROW, B_MATPRV, IFACE_("Both"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1344                                   pr_texture, 10, TEX_PR_BOTH, 0, 0, "");
1345                         
1346                         /* Alpha button for texture preview */
1347                         if (*pr_texture != TEX_PR_OTHER) {
1348                                 row = uiLayoutRow(layout, FALSE);
1349                                 uiItemR(row, &texture_ptr, "use_preview_alpha", 0, NULL, ICON_NONE);
1350                         }
1351                 }
1352         }
1353 }
1354
1355 /********************** ColorRamp Template **************************/
1356
1357
1358 typedef struct RNAUpdateCb {
1359         PointerRNA ptr;
1360         PropertyRNA *prop;
1361 } RNAUpdateCb;
1362
1363 static void rna_update_cb(bContext *C, void *arg_cb, void *UNUSED(arg))
1364 {
1365         RNAUpdateCb *cb = (RNAUpdateCb *)arg_cb;
1366
1367         /* we call update here on the pointer property, this way the
1368          * owner of the curve mapping can still define it's own update
1369          * and notifier, even if the CurveMapping struct is shared. */
1370         RNA_property_update(C, &cb->ptr, cb->prop);
1371 }
1372
1373 static void colorband_add_cb(bContext *C, void *cb_v, void *coba_v)
1374 {
1375         ColorBand *coba = coba_v;
1376         float pos = 0.5f;
1377
1378         if (coba->tot > 1) {
1379                 if (coba->cur > 0) pos = (coba->data[coba->cur - 1].pos + coba->data[coba->cur].pos) * 0.5f;
1380                 else pos = (coba->data[coba->cur + 1].pos + coba->data[coba->cur].pos) * 0.5f;
1381         }
1382
1383         if (colorband_element_add(coba, pos)) {
1384                 rna_update_cb(C, cb_v, NULL);
1385                 ED_undo_push(C, "Add colorband");
1386         }
1387 }
1388
1389 static void colorband_del_cb(bContext *C, void *cb_v, void *coba_v)
1390 {
1391         ColorBand *coba = coba_v;
1392
1393         if (colorband_element_remove(coba, coba->cur)) {
1394                 ED_undo_push(C, "Delete colorband");
1395                 rna_update_cb(C, cb_v, NULL);
1396         }
1397 }
1398
1399 static void colorband_flip_cb(bContext *C, void *cb_v, void *coba_v)
1400 {
1401         CBData data_tmp[MAXCOLORBAND];
1402
1403         ColorBand *coba = coba_v;
1404         int a;
1405
1406         for (a = 0; a < coba->tot; a++) {
1407                 data_tmp[a] = coba->data[coba->tot - (a + 1)];
1408         }
1409         for (a = 0; a < coba->tot; a++) {
1410                 data_tmp[a].pos = 1.0f - data_tmp[a].pos;
1411                 coba->data[a] = data_tmp[a];
1412         }
1413
1414         /* may as well flip the cur*/
1415         coba->cur = coba->tot - (coba->cur + 1);
1416
1417         ED_undo_push(C, "Flip colorband");
1418
1419         rna_update_cb(C, cb_v, NULL);
1420 }
1421
1422 static void colorband_update_cb(bContext *UNUSED(C), void *bt_v, void *coba_v)
1423 {
1424         uiBut *bt = bt_v;
1425         ColorBand *coba = coba_v;
1426
1427         /* sneaky update here, we need to sort the colorband points to be in order,
1428          * however the RNA pointer then is wrong, so we update it */
1429         colorband_update_sort(coba);
1430         bt->rnapoin.data = coba->data + coba->cur;
1431 }
1432
1433 /* offset aligns from bottom, standard width 300, height 115 */
1434 static void colorband_buttons_large(uiLayout *layout, uiBlock *block, ColorBand *coba,
1435                                     int xoffs, int yoffs, RNAUpdateCb *cb)
1436 {
1437         uiBut *bt;
1438         uiLayout *row;
1439         const int line1_y = yoffs + 65 + UI_UNIT_Y + 2; /* 2 for some space between the buttons */
1440         const int line2_y = yoffs + 65;
1441
1442         if (coba == NULL) return;
1443
1444         bt = uiDefBut(block, BUT, 0, IFACE_("Add"), 0 + xoffs, line1_y, 40, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1445                       TIP_("Add a new color stop to the colorband"));
1446         uiButSetNFunc(bt, colorband_add_cb, MEM_dupallocN(cb), coba);
1447
1448         bt = uiDefBut(block, BUT, 0, IFACE_("Delete"), 45 + xoffs, line1_y, 45, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1449                       TIP_("Delete the active position"));
1450         uiButSetNFunc(bt, colorband_del_cb, MEM_dupallocN(cb), coba);
1451
1452
1453         /* XXX, todo for later - convert to operator - campbell */
1454         bt = uiDefBut(block, BUT, 0, "F",        95 + xoffs, line1_y, 20, UI_UNIT_Y,
1455                       NULL, 0, 0, 0, 0, TIP_("Flip colorband"));
1456         uiButSetNFunc(bt, colorband_flip_cb, MEM_dupallocN(cb), coba);
1457
1458         uiDefButS(block, NUM, 0, "", 120 + xoffs, line1_y, 80, UI_UNIT_Y, &coba->cur, 0.0, (float)(MAX2(0, coba->tot - 1)),
1459                   0, 0, TIP_("Choose active color stop"));
1460
1461         bt = uiDefButS(block, MENU, 0, IFACE_("Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4"),
1462                        210 + xoffs, line1_y, 90, UI_UNIT_Y, &coba->ipotype, 0.0, 0.0, 0, 0,
1463                        TIP_("Set interpolation between color stops"));
1464         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1465         uiBlockEndAlign(block);
1466
1467         bt = uiDefBut(block, BUT_COLORBAND, 0, "",   xoffs, line2_y, 300, UI_UNIT_Y, coba, 0, 0, 0, 0, "");
1468         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1469
1470
1471
1472         if (coba->tot) {
1473                 CBData *cbd = coba->data + coba->cur;
1474
1475                 /* better to use rna so we can animate them */
1476                 PointerRNA ptr;
1477                 RNA_pointer_create(cb->ptr.id.data, &RNA_ColorRampElement, cbd, &ptr);
1478                 row = uiLayoutRow(layout, FALSE);
1479
1480                 uiItemR(row, &ptr, "position", 0, "Pos", ICON_NONE);
1481                 bt = block->buttons.last;
1482                 uiButSetFunc(bt, colorband_update_cb, bt, coba);
1483
1484                 uiItemR(row, &ptr, "color", 0, "", ICON_NONE);
1485         }
1486
1487 }
1488
1489 static void colorband_buttons_small(uiLayout *layout, uiBlock *block, ColorBand *coba, rctf *butr, RNAUpdateCb *cb)
1490 {
1491         uiBut *bt;
1492         float unit = BLI_rctf_size_x(butr) / 14.0f;
1493         float xs = butr->xmin;
1494
1495         uiBlockBeginAlign(block);
1496         bt = uiDefBut(block, BUT, 0, IFACE_("Add"), xs, butr->ymin + UI_UNIT_Y, 2.0f * unit, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1497                       TIP_("Add a new color stop to the colorband"));
1498         uiButSetNFunc(bt, colorband_add_cb, MEM_dupallocN(cb), coba);
1499         bt = uiDefBut(block, BUT, 0, IFACE_("Delete"), xs + 2.0f * unit, butr->ymin + UI_UNIT_Y, 1.5f * unit, UI_UNIT_Y,
1500                       NULL, 0, 0, 0, 0, TIP_("Delete the active position"));
1501         uiButSetNFunc(bt, colorband_del_cb, MEM_dupallocN(cb), coba);
1502         bt = uiDefBut(block, BUT, 0, "F", xs + 3.5f * unit, butr->ymin + UI_UNIT_Y, 0.5f * unit, UI_UNIT_Y,
1503                       NULL, 0, 0, 0, 0, TIP_("Flip the color ramp"));
1504         uiButSetNFunc(bt, colorband_flip_cb, MEM_dupallocN(cb), coba);
1505         uiBlockEndAlign(block);
1506
1507         if (coba->tot) {
1508                 CBData *cbd = coba->data + coba->cur;
1509                 PointerRNA ptr;
1510                 RNA_pointer_create(cb->ptr.id.data, &RNA_ColorRampElement, cbd, &ptr);
1511                 uiItemR(layout, &ptr, "color", 0, "", ICON_NONE);
1512         }
1513
1514         bt = uiDefButS(block, MENU, 0, IFACE_("Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4"),
1515                        xs + 10.0f * unit, butr->ymin + UI_UNIT_Y, unit * 4, UI_UNIT_Y, &coba->ipotype, 0.0, 0.0, 0, 0,
1516                        TIP_("Set interpolation between color stops"));
1517         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1518
1519         bt = uiDefBut(block, BUT_COLORBAND, 0, "", xs, butr->ymin, BLI_rctf_size_x(butr), UI_UNIT_Y, coba, 0, 0, 0, 0, "");
1520         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1521
1522         uiBlockEndAlign(block);
1523 }
1524
1525 static void colorband_buttons_layout(uiLayout *layout, uiBlock *block, ColorBand *coba, rctf *butr,
1526                                      int small, RNAUpdateCb *cb)
1527 {
1528         if (small)
1529                 colorband_buttons_small(layout, block, coba, butr, cb);
1530         else
1531                 colorband_buttons_large(layout, block, coba, 0, 0, cb);
1532 }
1533
1534 void uiTemplateColorRamp(uiLayout *layout, PointerRNA *ptr, const char *propname, int expand)
1535 {
1536         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1537         PointerRNA cptr;
1538         RNAUpdateCb *cb;
1539         uiBlock *block;
1540         rctf rect;
1541
1542         if (!prop || RNA_property_type(prop) != PROP_POINTER)
1543                 return;
1544
1545         cptr = RNA_property_pointer_get(ptr, prop);
1546         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_ColorRamp))
1547                 return;
1548
1549         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1550         cb->ptr = *ptr;
1551         cb->prop = prop;
1552
1553         rect.xmin = 0; rect.xmax = 200;
1554         rect.ymin = 0; rect.ymax = 190;
1555
1556         block = uiLayoutAbsoluteBlock(layout);
1557         colorband_buttons_layout(layout, block, cptr.data, &rect, !expand, cb);
1558
1559         MEM_freeN(cb);
1560 }
1561
1562 /********************* Histogram Template ************************/
1563
1564 void uiTemplateHistogram(uiLayout *layout, PointerRNA *ptr, const char *propname)
1565 {
1566         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1567         PointerRNA cptr;
1568         RNAUpdateCb *cb;
1569         uiBlock *block;
1570         uiBut *bt;
1571         Histogram *hist;
1572         rctf rect;
1573         
1574         if (!prop || RNA_property_type(prop) != PROP_POINTER)
1575                 return;
1576         
1577         cptr = RNA_property_pointer_get(ptr, prop);
1578         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Histogram))
1579                 return;
1580         
1581         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1582         cb->ptr = *ptr;
1583         cb->prop = prop;
1584         
1585         rect.xmin = 0; rect.xmax = 200;
1586         rect.ymin = 0; rect.ymax = 190;
1587         
1588         block = uiLayoutAbsoluteBlock(layout);
1589         //colorband_buttons_layout(layout, block, cptr.data, &rect, !expand, cb);
1590
1591         hist = (Histogram *)cptr.data;
1592
1593         hist->height = (hist->height <= UI_UNIT_Y) ? UI_UNIT_Y : hist->height;
1594
1595         bt = uiDefBut(block, HISTOGRAM, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect), hist->height, hist,
1596                       0, 0, 0, 0, "");
1597         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1598
1599         MEM_freeN(cb);
1600 }
1601
1602 /********************* Waveform Template ************************/
1603
1604 void uiTemplateWaveform(uiLayout *layout, PointerRNA *ptr, const char *propname)
1605 {
1606         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1607         PointerRNA cptr;
1608         RNAUpdateCb *cb;
1609         uiBlock *block;
1610         uiBut *bt;
1611         Scopes *scopes;
1612         rctf rect;
1613         
1614         if (!prop || RNA_property_type(prop) != PROP_POINTER)
1615                 return;
1616         
1617         cptr = RNA_property_pointer_get(ptr, prop);
1618         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Scopes))
1619                 return;
1620         scopes = (Scopes *)cptr.data;
1621         
1622         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1623         cb->ptr = *ptr;
1624         cb->prop = prop;
1625         
1626         rect.xmin = 0; rect.xmax = 200;
1627         rect.ymin = 0; rect.ymax = 190;
1628         
1629         block = uiLayoutAbsoluteBlock(layout);
1630         
1631         scopes->wavefrm_height = (scopes->wavefrm_height <= UI_UNIT_Y) ? UI_UNIT_Y : scopes->wavefrm_height;
1632
1633         bt = uiDefBut(block, WAVEFORM, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect), scopes->wavefrm_height, scopes,
1634                       0, 0, 0, 0, "");
1635         (void)bt;  /* UNUSED */
1636         
1637         MEM_freeN(cb);
1638 }
1639
1640 /********************* Vectorscope Template ************************/
1641
1642 void uiTemplateVectorscope(uiLayout *layout, PointerRNA *ptr, const char *propname)
1643 {
1644         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1645         PointerRNA cptr;
1646         RNAUpdateCb *cb;
1647         uiBlock *block;
1648         uiBut *bt;
1649         Scopes *scopes;
1650         rctf rect;
1651         
1652         if (!prop || RNA_property_type(prop) != PROP_POINTER)
1653                 return;
1654         
1655         cptr = RNA_property_pointer_get(ptr, prop);
1656         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Scopes))
1657                 return;
1658         scopes = (Scopes *)cptr.data;
1659
1660         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1661         cb->ptr = *ptr;
1662         cb->prop = prop;
1663         
1664         rect.xmin = 0; rect.xmax = 200;
1665         rect.ymin = 0; rect.ymax = 190;
1666         
1667         block = uiLayoutAbsoluteBlock(layout);
1668
1669         scopes->vecscope_height = (scopes->vecscope_height <= UI_UNIT_Y) ? UI_UNIT_Y : scopes->vecscope_height;
1670         
1671         bt = uiDefBut(block, VECTORSCOPE, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect),
1672                       scopes->vecscope_height, scopes, 0, 0, 0, 0, "");
1673         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1674         
1675         MEM_freeN(cb);
1676 }
1677
1678 /********************* CurveMapping Template ************************/
1679
1680
1681 static void curvemap_buttons_zoom_in(bContext *C, void *cumap_v, void *UNUSED(arg))
1682 {
1683         CurveMapping *cumap = cumap_v;
1684         float d;
1685
1686         /* we allow 20 times zoom */
1687         if (BLI_rctf_size_x(&cumap->curr) > 0.04f * BLI_rctf_size_x(&cumap->clipr)) {
1688                 d = 0.1154f * BLI_rctf_size_x(&cumap->curr);
1689                 cumap->curr.xmin += d;
1690                 cumap->curr.xmax -= d;
1691                 d = 0.1154f * BLI_rctf_size_y(&cumap->curr);
1692                 cumap->curr.ymin += d;
1693                 cumap->curr.ymax -= d;
1694         }
1695
1696         ED_region_tag_redraw(CTX_wm_region(C));
1697 }
1698
1699 static void curvemap_buttons_zoom_out(bContext *C, void *cumap_v, void *UNUSED(unused))
1700 {
1701         CurveMapping *cumap = cumap_v;
1702         float d, d1;
1703
1704         /* we allow 20 times zoom, but don't view outside clip */
1705         if (BLI_rctf_size_x(&cumap->curr) < 20.0f * BLI_rctf_size_x(&cumap->clipr)) {
1706                 d = d1 = 0.15f * BLI_rctf_size_x(&cumap->curr);
1707
1708                 if (cumap->flag & CUMA_DO_CLIP) 
1709                         if (cumap->curr.xmin - d < cumap->clipr.xmin)
1710                                 d1 = cumap->curr.xmin - cumap->clipr.xmin;
1711                 cumap->curr.xmin -= d1;
1712
1713                 d1 = d;
1714                 if (cumap->flag & CUMA_DO_CLIP) 
1715                         if (cumap->curr.xmax + d > cumap->clipr.xmax)
1716                                 d1 = -cumap->curr.xmax + cumap->clipr.xmax;
1717                 cumap->curr.xmax += d1;
1718
1719                 d = d1 = 0.15f * BLI_rctf_size_y(&cumap->curr);
1720
1721                 if (cumap->flag & CUMA_DO_CLIP) 
1722                         if (cumap->curr.ymin - d < cumap->clipr.ymin)
1723                                 d1 = cumap->curr.ymin - cumap->clipr.ymin;
1724                 cumap->curr.ymin -= d1;
1725
1726                 d1 = d;
1727                 if (cumap->flag & CUMA_DO_CLIP) 
1728                         if (cumap->curr.ymax + d > cumap->clipr.ymax)
1729                                 d1 = -cumap->curr.ymax + cumap->clipr.ymax;
1730                 cumap->curr.ymax += d1;
1731         }
1732
1733         ED_region_tag_redraw(CTX_wm_region(C));
1734 }
1735
1736 static void curvemap_buttons_setclip(bContext *UNUSED(C), void *cumap_v, void *UNUSED(arg))
1737 {
1738         CurveMapping *cumap = cumap_v;
1739
1740         curvemapping_changed(cumap, FALSE);
1741 }       
1742
1743 static void curvemap_buttons_delete(bContext *C, void *cb_v, void *cumap_v)
1744 {
1745         CurveMapping *cumap = cumap_v;
1746
1747         curvemap_remove(cumap->cm + cumap->cur, SELECT);
1748         curvemapping_changed(cumap, FALSE);
1749
1750         rna_update_cb(C, cb_v, NULL);
1751 }
1752
1753 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
1754 static uiBlock *curvemap_clipping_func(bContext *C, ARegion *ar, void *cumap_v)
1755 {
1756         CurveMapping *cumap = cumap_v;
1757         uiBlock *block;
1758         uiBut *bt;
1759         float width = 8 * UI_UNIT_X;
1760
1761         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1762
1763         /* use this for a fake extra empy space around the buttons */
1764         uiDefBut(block, LABEL, 0, "",           -4, 16, width + 8, 6 * UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
1765
1766         bt = uiDefButBitI(block, TOG, CUMA_DO_CLIP, 1, "Use Clipping",
1767                           0, 5 * UI_UNIT_Y, width, UI_UNIT_Y, &cumap->flag, 0.0, 0.0, 10, 0, "");
1768         uiButSetFunc(bt, curvemap_buttons_setclip, cumap, NULL);
1769
1770         uiBlockBeginAlign(block);
1771         uiDefButF(block, NUM, 0, IFACE_("Min X "),   0, 4 * UI_UNIT_Y, width, UI_UNIT_Y,
1772                   &cumap->clipr.xmin, -100.0, cumap->clipr.xmax, 10, 0, "");
1773         uiDefButF(block, NUM, 0, IFACE_("Min Y "),   0, 3 * UI_UNIT_Y, width, UI_UNIT_Y,
1774                   &cumap->clipr.ymin, -100.0, cumap->clipr.ymax, 10, 0, "");
1775         uiDefButF(block, NUM, 0, IFACE_("Max X "),   0, 2 * UI_UNIT_Y, width, UI_UNIT_Y,
1776                   &cumap->clipr.xmax, cumap->clipr.xmin, 100.0, 10, 0, "");
1777         uiDefButF(block, NUM, 0, IFACE_("Max Y "),   0, UI_UNIT_Y, width, UI_UNIT_Y,
1778                   &cumap->clipr.ymax, cumap->clipr.ymin, 100.0, 10, 0, "");
1779
1780         uiBlockSetDirection(block, UI_RIGHT);
1781
1782         uiEndBlock(C, block);
1783         return block;
1784 }
1785
1786 static void curvemap_tools_dofunc(bContext *C, void *cumap_v, int event)
1787 {
1788         CurveMapping *cumap = cumap_v;
1789         CurveMap *cuma = cumap->cm + cumap->cur;
1790
1791         switch (event) {
1792                 case 0: /* reset */
1793                         curvemap_reset(cuma, &cumap->clipr, cumap->preset, CURVEMAP_SLOPE_POSITIVE);
1794                         curvemapping_changed(cumap, FALSE);
1795                         break;
1796                 case 1:
1797                         cumap->curr = cumap->clipr;
1798                         break;
1799                 case 2: /* set vector */
1800                         curvemap_sethandle(cuma, 1);
1801                         curvemapping_changed(cumap, FALSE);
1802                         break;
1803                 case 3: /* set auto */
1804                         curvemap_sethandle(cuma, 0);
1805                         curvemapping_changed(cumap, FALSE);
1806                         break;
1807                 case 4: /* extend horiz */
1808                         cuma->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1809                         curvemapping_changed(cumap, FALSE);
1810                         break;
1811                 case 5: /* extend extrapolate */
1812                         cuma->flag |= CUMA_EXTEND_EXTRAPOLATE;
1813                         curvemapping_changed(cumap, FALSE);
1814                         break;
1815         }
1816         ED_region_tag_redraw(CTX_wm_region(C));
1817 }
1818
1819 static uiBlock *curvemap_tools_func(bContext *C, ARegion *ar, void *cumap_v)
1820 {
1821         uiBlock *block;
1822         short yco = 0, menuwidth = 10 * UI_UNIT_X;
1823
1824         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1825         uiBlockSetButmFunc(block, curvemap_tools_dofunc, cumap_v);
1826
1827         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset View"),          0, yco -= UI_UNIT_Y,
1828                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 1, "");
1829         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Vector Handle"),       0, yco -= UI_UNIT_Y,
1830                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 2, "");
1831         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Auto Handle"),         0, yco -= UI_UNIT_Y,
1832                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 3, "");
1833         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Extend Horizontal"),   0, yco -= UI_UNIT_Y,
1834                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 4, "");
1835         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Extend Extrapolated"), 0, yco -= UI_UNIT_Y,
1836                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 5, "");
1837         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset Curve"),         0, yco -= UI_UNIT_Y,
1838                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1839
1840         uiBlockSetDirection(block, UI_RIGHT);
1841         uiTextBoundsBlock(block, 50);
1842
1843         uiEndBlock(C, block);
1844         return block;
1845 }
1846
1847 static uiBlock *curvemap_brush_tools_func(bContext *C, ARegion *ar, void *cumap_v)
1848 {
1849         uiBlock *block;
1850         short yco = 0, menuwidth = 10 * UI_UNIT_X;
1851
1852         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1853         uiBlockSetButmFunc(block, curvemap_tools_dofunc, cumap_v);
1854
1855         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset View"),    0, yco -= UI_UNIT_Y,
1856                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 1, "");
1857         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Vector Handle"), 0, yco -= UI_UNIT_Y,
1858                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 2, "");
1859         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Auto Handle"),   0, yco -= UI_UNIT_Y,
1860                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 3, "");
1861         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset Curve"),   0, yco -= UI_UNIT_Y,
1862                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1863
1864         uiBlockSetDirection(block, UI_RIGHT);
1865         uiTextBoundsBlock(block, 50);
1866
1867         uiEndBlock(C, block);
1868         return block;
1869 }
1870
1871 static void curvemap_buttons_redraw(bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
1872 {
1873         ED_region_tag_redraw(CTX_wm_region(C));
1874 }
1875
1876 static void curvemap_buttons_update(bContext *C, void *arg1_v, void *cumap_v)
1877 {
1878         CurveMapping *cumap = cumap_v;
1879         curvemapping_changed(cumap, TRUE);
1880         rna_update_cb(C, arg1_v, NULL);
1881 }
1882
1883 static void curvemap_buttons_reset(bContext *C, void *cb_v, void *cumap_v)
1884 {
1885         CurveMapping *cumap = cumap_v;
1886         int a;
1887         
1888         cumap->preset = CURVE_PRESET_LINE;
1889         for (a = 0; a < CM_TOT; a++)
1890                 curvemap_reset(cumap->cm + a, &cumap->clipr, cumap->preset, CURVEMAP_SLOPE_POSITIVE);
1891         
1892         cumap->black[0] = cumap->black[1] = cumap->black[2] = 0.0f;
1893         cumap->white[0] = cumap->white[1] = cumap->white[2] = 1.0f;
1894         curvemapping_set_black_white(cumap, NULL, NULL);
1895         
1896         curvemapping_changed(cumap, FALSE);
1897
1898         rna_update_cb(C, cb_v, NULL);
1899 }
1900
1901 /* still unsure how this call evolves... we use labeltype for defining what curve-channels to show */
1902 static void curvemap_buttons_layout(uiLayout *layout, PointerRNA *ptr, char labeltype, int levels,
1903                                     int brush, RNAUpdateCb *cb)
1904 {
1905         CurveMapping *cumap = ptr->data;
1906         CurveMap *cm = &cumap->cm[cumap->cur];
1907         CurveMapPoint *cmp = NULL;
1908         uiLayout *row, *sub, *split;
1909         uiBlock *block;
1910         uiBut *bt;
1911         float dx = UI_UNIT_X;
1912         int icon, size;
1913         int bg = -1, i;
1914
1915         block = uiLayoutGetBlock(layout);
1916
1917         /* curve chooser */
1918         row = uiLayoutRow(layout, FALSE);
1919
1920         if (labeltype == 'v') {
1921                 /* vector */
1922                 sub = uiLayoutRow(row, TRUE);
1923                 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
1924
1925                 if (cumap->cm[0].curve) {
1926                         bt = uiDefButI(block, ROW, 0, "X", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
1927                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1928                 }
1929                 if (cumap->cm[1].curve) {
1930                         bt = uiDefButI(block, ROW, 0, "Y", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
1931                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1932                 }
1933                 if (cumap->cm[2].curve) {
1934                         bt = uiDefButI(block, ROW, 0, "Z", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
1935                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1936                 }
1937         }
1938         else if (labeltype == 'c') {
1939                 /* color */
1940                 sub = uiLayoutRow(row, TRUE);
1941                 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
1942
1943                 if (cumap->cm[3].curve) {
1944                         bt = uiDefButI(block, ROW, 0, "C", 0, 0, dx, dx, &cumap->cur, 0.0, 3.0, 0.0, 0.0, "");
1945                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1946                 }
1947                 if (cumap->cm[0].curve) {
1948                         bt = uiDefButI(block, ROW, 0, "R", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
1949                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1950                 }
1951                 if (cumap->cm[1].curve) {
1952                         bt = uiDefButI(block, ROW, 0, "G", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
1953                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1954                 }
1955                 if (cumap->cm[2].curve) {
1956                         bt = uiDefButI(block, ROW, 0, "B", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
1957                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1958                 }
1959         }
1960         else if (labeltype == 'h') {
1961                 /* HSV */
1962                 sub = uiLayoutRow(row, TRUE);
1963                 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
1964                 
1965                 if (cumap->cm[0].curve) {
1966                         bt = uiDefButI(block, ROW, 0, "H", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
1967                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1968                 }
1969                 if (cumap->cm[1].curve) {
1970                         bt = uiDefButI(block, ROW, 0, "S", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
1971                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1972                 }
1973                 if (cumap->cm[2].curve) {
1974                         bt = uiDefButI(block, ROW, 0, "V", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
1975                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
1976                 }
1977         }
1978         else
1979                 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_RIGHT);
1980         
1981         if (labeltype == 'h')
1982                 bg = UI_GRAD_H;
1983
1984         /* operation buttons */
1985         sub = uiLayoutRow(row, TRUE);
1986
1987         uiBlockSetEmboss(block, UI_EMBOSSN);
1988
1989         bt = uiDefIconBut(block, BUT, 0, ICON_ZOOMIN, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Zoom in"));
1990         uiButSetFunc(bt, curvemap_buttons_zoom_in, cumap, NULL);
1991
1992         bt = uiDefIconBut(block, BUT, 0, ICON_ZOOMOUT, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Zoom out"));
1993         uiButSetFunc(bt, curvemap_buttons_zoom_out, cumap, NULL);
1994
1995         if (brush)
1996                 bt = uiDefIconBlockBut(block, curvemap_brush_tools_func, cumap, 0, ICON_MODIFIER, 0, 0, dx, dx, TIP_("Tools"));
1997         else
1998                 bt = uiDefIconBlockBut(block, curvemap_tools_func, cumap, 0, ICON_MODIFIER, 0, 0, dx, dx, TIP_("Tools"));
1999
2000         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
2001
2002         icon = (cumap->flag & CUMA_DO_CLIP) ? ICON_CLIPUV_HLT : ICON_CLIPUV_DEHLT;
2003         bt = uiDefIconBlockBut(block, curvemap_clipping_func, cumap, 0, icon, 0, 0, dx, dx, TIP_("Clipping Options"));
2004         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
2005
2006         bt = uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Delete points"));
2007         uiButSetNFunc(bt, curvemap_buttons_delete, MEM_dupallocN(cb), cumap);
2008
2009         uiBlockSetEmboss(block, UI_EMBOSS);
2010
2011         uiBlockSetNFunc(block, rna_update_cb, MEM_dupallocN(cb), NULL);
2012
2013         /* curve itself */
2014         size = uiLayoutGetWidth(layout);
2015         row = uiLayoutRow(layout, FALSE);
2016         uiDefBut(block, BUT_CURVE, 0, "", 0, 0, size, MIN2(size, 200), cumap, 0.0f, 1.0f, bg, 0, "");
2017
2018         /* sliders for selected point */
2019         for (i = 0; i < cm->totpoint; i++) {
2020                 if (cm->curve[i].flag & CUMA_SELECT) {
2021                         cmp = &cm->curve[i];
2022                         break;
2023                 }
2024         }
2025
2026         if (cmp) {
2027                 const float range_clamp[2]   = {0.0f, 1.0f};
2028                 const float range_unclamp[2] = {-1000.0f, 1000.0f};  /* arbitrary limits here */
2029                 const float *range = (cumap->flag & CUMA_DO_CLIP) ? range_clamp : range_unclamp;
2030
2031                 uiLayoutRow(layout, TRUE);
2032                 uiBlockSetNFunc(block, curvemap_buttons_update, MEM_dupallocN(cb), cumap);
2033                 bt = uiDefButF(block, NUM, 0, "X", 0, 2 * UI_UNIT_Y, UI_UNIT_X * 10, UI_UNIT_Y,
2034                                &cmp->x, range[0], range[1], 1, 5, "");
2035                 bt = uiDefButF(block, NUM, 0, "Y", 0, 1 * UI_UNIT_Y, UI_UNIT_X * 10, UI_UNIT_Y,
2036                                &cmp->y, range[0], range[1], 1, 5, "");
2037         }
2038
2039         /* black/white levels */
2040         if (levels) {
2041                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2042                 uiItemR(uiLayoutColumn(split, FALSE), ptr, "black_level", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2043                 uiItemR(uiLayoutColumn(split, FALSE), ptr, "white_level", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2044
2045                 uiLayoutRow(layout, FALSE);
2046                 bt = uiDefBut(block, BUT, 0, IFACE_("Reset"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0,
2047                               TIP_("Reset Black/White point and curves"));
2048                 uiButSetNFunc(bt, curvemap_buttons_reset, MEM_dupallocN(cb), cumap);
2049         }
2050
2051         uiBlockSetNFunc(block, NULL, NULL, NULL);
2052 }
2053
2054 void uiTemplateCurveMapping(uiLayout *layout, PointerRNA *ptr, const char *propname, int type, int levels, int brush)
2055 {
2056         RNAUpdateCb *cb;
2057         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2058         PointerRNA cptr;
2059
2060         if (!prop) {
2061                 RNA_warning("curve property not found: %s.%s",
2062                             RNA_struct_identifier(ptr->type), propname);
2063                 return;
2064         }
2065
2066         if (RNA_property_type(prop) != PROP_POINTER) {
2067                 RNA_warning("curve is not a pointer: %s.%s",
2068                             RNA_struct_identifier(ptr->type), propname);
2069                 return;
2070         }
2071
2072         cptr = RNA_property_pointer_get(ptr, prop);
2073         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_CurveMapping))
2074                 return;
2075
2076         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
2077         cb->ptr = *ptr;
2078         cb->prop = prop;
2079
2080         curvemap_buttons_layout(layout, &cptr, type, levels, brush, cb);
2081
2082         MEM_freeN(cb);
2083 }
2084
2085 /********************* ColorPicker Template ************************/
2086
2087 #define WHEEL_SIZE  100
2088
2089 /* This template now follows User Preference for type - name is not correct anymore... */
2090 void uiTemplateColorPicker(uiLayout *layout, PointerRNA *ptr, const char *propname, int value_slider,
2091                            int lock, int lock_luminosity, int cubic)
2092 {
2093         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2094         uiBlock *block = uiLayoutGetBlock(layout);
2095         uiLayout *col, *row;
2096         uiBut *but = NULL;
2097         float softmin, softmax, step, precision;
2098
2099         if (!prop) {
2100                 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2101                 return;
2102         }
2103
2104         RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2105
2106         col = uiLayoutColumn(layout, TRUE);
2107         row = uiLayoutRow(col, TRUE);
2108
2109         switch (U.color_picker_type) {
2110                 case USER_CP_CIRCLE:
2111                         but = uiDefButR_prop(block, HSVCIRCLE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2112                                              -1, 0.0, 0.0, 0, 0, "");
2113                         break;
2114                 case USER_CP_SQUARE_SV:
2115                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2116                                              -1, 0.0, 0.0, UI_GRAD_SV, 0, "");
2117                         break;
2118                 case USER_CP_SQUARE_HS:
2119                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2120                                              -1, 0.0, 0.0, UI_GRAD_HS, 0, "");
2121                         break;
2122                 case USER_CP_SQUARE_HV:
2123                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2124                                              -1, 0.0, 0.0, UI_GRAD_HV, 0, "");
2125                         break;
2126         }
2127
2128         if (lock) {
2129                 but->flag |= UI_BUT_COLOR_LOCK;
2130         }
2131
2132         if (lock_luminosity) {
2133                 float color[4]; /* in case of alpha */
2134                 but->flag |= UI_BUT_VEC_SIZE_LOCK;
2135                 RNA_property_float_get_array(ptr, prop, color);
2136                 but->a2 = len_v3(color);
2137         }
2138
2139         if (cubic)
2140                 but->flag |= UI_BUT_COLOR_CUBIC;
2141
2142         
2143         if (value_slider) {
2144                 switch (U.color_picker_type) {
2145                         case USER_CP_CIRCLE:
2146                                 uiItemS(row);
2147                                 uiDefButR_prop(block, HSVCUBE, 0, "", WHEEL_SIZE + 6, 0, 14, WHEEL_SIZE, ptr, prop,
2148                                                -1, softmin, softmax, UI_GRAD_V_ALT, 0, "");
2149                                 break;
2150                         case USER_CP_SQUARE_SV:
2151                                 uiItemS(col);
2152                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2153                                                -1, softmin, softmax, UI_GRAD_SV + 3, 0, "");
2154                                 break;
2155                         case USER_CP_SQUARE_HS:
2156                                 uiItemS(col);
2157                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2158                                                -1, softmin, softmax, UI_GRAD_HS + 3, 0, "");
2159                                 break;
2160                         case USER_CP_SQUARE_HV:
2161                                 uiItemS(col);
2162                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2163                                                -1, softmin, softmax, UI_GRAD_HV + 3, 0, "");
2164                                 break;
2165                 }
2166         }
2167 }
2168
2169 /********************* Layer Buttons Template ************************/
2170
2171 static void handle_layer_buttons(bContext *C, void *arg1, void *arg2)
2172 {
2173         uiBut *but = arg1;
2174         int cur = GET_INT_FROM_POINTER(arg2);
2175         wmWindow *win = CTX_wm_window(C);
2176         int i, tot, shift = win->eventstate->shift;
2177
2178         if (!shift) {
2179                 tot = RNA_property_array_length(&but->rnapoin, but->rnaprop);
2180                 
2181                 /* Normally clicking only selects one layer */
2182                 RNA_property_boolean_set_index(&but->rnapoin, but->rnaprop, cur, TRUE);
2183                 for (i = 0; i < tot; ++i) {
2184                         if (i != cur)
2185                                 RNA_property_boolean_set_index(&but->rnapoin, but->rnaprop, i, FALSE);
2186                 }
2187         }
2188
2189         /* view3d layer change should update depsgraph (invisible object changed maybe) */
2190         /* see view3d_header.c */
2191 }
2192
2193 /* TODO:
2194  * - for now, grouping of layers is determined by dividing up the length of
2195  *   the array of layer bitflags */
2196
2197 void uiTemplateLayers(uiLayout *layout, PointerRNA *ptr, const char *propname,
2198                       PointerRNA *used_ptr, const char *used_propname, int active_layer)
2199 {
2200         uiLayout *uRow, *uCol;
2201         PropertyRNA *prop, *used_prop = NULL;
2202         int groups, cols, layers;
2203         int group, col, layer, row;
2204         int cols_per_group = 5;
2205
2206         prop = RNA_struct_find_property(ptr, propname);
2207         if (!prop) {
2208                 RNA_warning("layers property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2209                 return;
2210         }
2211         
2212         /* the number of layers determines the way we group them 
2213          *      - we want 2 rows only (for now)
2214          *      - the number of columns (cols) is the total number of buttons per row
2215          *        the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
2216          *      - for now, only split into groups if group will have at least 5 items
2217          */
2218         layers = RNA_property_array_length(ptr, prop);
2219         cols = (layers / 2) + (layers % 2);
2220         groups = ((cols / 2) < cols_per_group) ? (1) : (cols / cols_per_group);
2221
2222         if (used_ptr && used_propname) {
2223                 used_prop = RNA_struct_find_property(used_ptr, used_propname);
2224                 if (!used_prop) {
2225                         RNA_warning("used layers property not found: %s.%s", RNA_struct_identifier(ptr->type), used_propname);
2226                         return;
2227                 }
2228
2229                 if (RNA_property_array_length(used_ptr, used_prop) < layers)
2230                         used_prop = NULL;
2231         }
2232         
2233         /* layers are laid out going across rows, with the columns being divided into groups */
2234         
2235         for (group = 0; group < groups; group++) {
2236                 uCol = uiLayoutColumn(layout, TRUE);
2237                 
2238                 for (row = 0; row < 2; row++) {
2239                         uiBlock *block;
2240                         uiBut *but;
2241
2242                         uRow = uiLayoutRow(uCol, TRUE);
2243                         block = uiLayoutGetBlock(uRow);
2244                         layer = groups * cols_per_group * row + cols_per_group * group;
2245                         
2246                         /* add layers as toggle buts */
2247                         for (col = 0; (col < cols_per_group) && (layer < layers); col++, layer++) {
2248                                 int icon = 0;
2249                                 int butlay = 1 << layer;
2250
2251                                 if (active_layer & butlay)
2252                                         icon = ICON_LAYER_ACTIVE;
2253                                 else if (used_prop && RNA_property_boolean_get_index(used_ptr, used_prop, layer))
2254                                         icon = ICON_LAYER_USED;
2255                                 
2256                                 but = uiDefAutoButR(block, ptr, prop, layer, "", icon, 0, 0, UI_UNIT_X / 2, UI_UNIT_Y / 2);
2257                                 uiButSetFunc(but, handle_layer_buttons, but, SET_INT_IN_POINTER(layer));
2258                                 but->type = TOG;
2259                         }
2260                 }
2261         }
2262 }
2263
2264 void uiTemplateGameStates(uiLayout *layout, PointerRNA *ptr, const char *propname,
2265                       PointerRNA *used_ptr, const char *used_propname, int active_state)
2266 {
2267         uiLayout *uRow, *uCol;
2268         PropertyRNA *prop, *used_prop = NULL;
2269         int groups, cols, states;
2270         int group, col, state, row;
2271         int cols_per_group = 5;
2272         Object *ob = (Object *)ptr->id.data;
2273
2274         prop = RNA_struct_find_property(ptr, propname);
2275         if (!prop) {
2276                 RNA_warning("states property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2277                 return;
2278         }
2279         
2280         /* the number of states determines the way we group them 
2281          *      - we want 2 rows only (for now)
2282          *      - the number of columns (cols) is the total number of buttons per row
2283          *        the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
2284          *      - for now, only split into groups if group will have at least 5 items
2285          */
2286         states = RNA_property_array_length(ptr, prop);
2287         cols = (states / 2) + (states % 2);
2288         groups = ((cols / 2) < cols_per_group) ? (1) : (cols / cols_per_group);
2289
2290         if (used_ptr && used_propname) {
2291                 used_prop = RNA_struct_find_property(used_ptr, used_propname);
2292                 if (!used_prop) {
2293                         RNA_warning("used layers property not found: %s.%s", RNA_struct_identifier(ptr->type), used_propname);
2294                         return;
2295                 }
2296
2297                 if (RNA_property_array_length(used_ptr, used_prop) < states)
2298                         used_prop = NULL;
2299         }
2300         
2301         /* layers are laid out going across rows, with the columns being divided into groups */
2302         
2303         for (group = 0; group < groups; group++) {
2304                 uCol = uiLayoutColumn(layout, TRUE);
2305                 
2306                 for (row = 0; row < 2; row++) {
2307                         uiBlock *block;
2308                         uiBut *but;
2309
2310                         uRow = uiLayoutRow(uCol, TRUE);
2311                         block = uiLayoutGetBlock(uRow);
2312                         state = groups * cols_per_group * row + cols_per_group * group;
2313                         
2314                         /* add layers as toggle buts */
2315                         for (col = 0; (col < cols_per_group) && (state < states); col++, state++) {
2316                                 int icon = 0;
2317                                 int butlay = 1 << state;
2318
2319                                 if (active_state & butlay)
2320                                         icon = ICON_LAYER_ACTIVE;
2321                                 else if (used_prop && RNA_property_boolean_get_index(used_ptr, used_prop, state))
2322                                         icon = ICON_LAYER_USED;
2323                                 
2324                                 but = uiDefIconButR_prop(block, ICONTOG, 0, icon, 0, 0, UI_UNIT_X / 2, UI_UNIT_Y / 2, ptr, prop,
2325                                                          state, 0, 0, -1, -1, sca_state_name_get(ob, state));
2326                                 uiButSetFunc(but, handle_layer_buttons, but, SET_INT_IN_POINTER(state));
2327                                 but->type = TOG;
2328                         }
2329                 }
2330         }
2331 }
2332
2333
2334 /************************* List Template **************************/
2335
2336 static int list_item_icon_get(bContext *C, PointerRNA *itemptr, int rnaicon, int big)
2337 {
2338         ID *id = NULL;
2339         int icon;
2340
2341         if (!itemptr->data)
2342                 return rnaicon;
2343
2344         /* try ID, material or texture slot */
2345         if (RNA_struct_is_ID(itemptr->type)) {
2346                 id = itemptr->id.data;
2347         }
2348         else if (RNA_struct_is_a(itemptr->type, &RNA_MaterialSlot)) {
2349                 id = RNA_pointer_get(itemptr, "material").data;
2350         }
2351         else if (RNA_struct_is_a(itemptr->type, &RNA_TextureSlot)) {
2352                 id = RNA_pointer_get(itemptr, "texture").data;
2353         }
2354         else if (RNA_struct_is_a(itemptr->type, &RNA_DynamicPaintSurface)) {
2355                 DynamicPaintSurface *surface = (DynamicPaintSurface *)itemptr->data;
2356
2357                 if (surface->format == MOD_DPAINT_SURFACE_F_PTEX) return ICON_TEXTURE_SHADED;
2358                 else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX) return ICON_OUTLINER_DATA_MESH;
2359                 else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ) return ICON_FILE_IMAGE;
2360         }
2361
2362         /* get icon from ID */
2363         if (id) {
2364                 icon = ui_id_icon_get(C, id, big);
2365
2366                 if (icon)
2367                         return icon;
2368         }
2369
2370         return rnaicon;
2371 }
2372
2373 static void list_item_row(bContext *C, uiLayout *layout, PointerRNA *ptr, PointerRNA *itemptr, int i,
2374                           int rnaicon, PointerRNA *activeptr, PropertyRNA *activeprop, const char *prop_list_id)
2375 {
2376         uiBlock *block = uiLayoutGetBlock(layout);
2377         uiBut *but;
2378         uiLayout *split, *overlap, *sub, *row;
2379         char *namebuf;
2380         const char *name;
2381         int icon;
2382
2383         overlap = uiLayoutOverlap(layout);
2384
2385         /* list item behind label & other buttons */
2386         sub = uiLayoutRow(overlap, FALSE);
2387
2388         but = uiDefButR_prop(block, LISTROW, 0, "", 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, activeptr, activeprop,
2389                              0, 0, i, 0, 0, "");
2390         uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2391
2392         sub = uiLayoutRow(overlap, FALSE);
2393
2394         /* retrieve icon and name */
2395         icon = list_item_icon_get(C, itemptr, rnaicon, 0);
2396         if (icon == ICON_NONE || icon == ICON_DOT)
2397                 icon = 0;
2398
2399         namebuf = RNA_struct_name_get_alloc(itemptr, NULL, 0, NULL);
2400         name = (namebuf) ? namebuf : "";
2401
2402         /* hardcoded types */
2403         if (itemptr->type == &RNA_MeshTexturePolyLayer || itemptr->type == &RNA_MeshLoopColorLayer) {
2404                 uiItemL(sub, name, icon);
2405                 uiBlockSetEmboss(block, UI_EMBOSSN);
2406                 uiDefIconButR(block, TOG, 0, ICON_SCENE, 0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "active_render",
2407                               0, 0, 0, 0, 0, NULL);
2408                 uiBlockSetEmboss(block, UI_EMBOSS);
2409         }
2410         else if (RNA_struct_is_a(itemptr->type, &RNA_MaterialTextureSlot)) {
2411                 uiItemL(sub, name, icon);
2412                 uiBlockSetEmboss(block, UI_EMBOSS);
2413                 uiDefButR(block, OPTION, 0, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, ptr, "use_textures", i, 0, 0, 0, 0,  NULL);
2414         }
2415         else if (RNA_struct_is_a(itemptr->type, &RNA_SceneRenderLayer) || RNA_struct_is_a(itemptr->type, &RNA_FreestyleLineSet)) {
2416                 uiItemL(sub, name, icon);
2417                 uiBlockSetEmboss(block, UI_EMBOSS);
2418                 uiDefButR(block, OPTION, 0, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "use", 0, 0, 0, 0, 0,  NULL);
2419         }
2420         else if (RNA_struct_is_a(itemptr->type, &RNA_MaterialSlot)) {
2421                 /* provision to draw active node name */
2422                 Material *ma, *manode;
2423                 Scene *scene = CTX_data_scene(C);
2424                 Object *ob = (Object *)ptr->id.data;
2425                 int index = (Material **)itemptr->data - ob->mat;
2426                 
2427                 /* default item with material base name */
2428                 uiItemL(sub, name, icon);
2429                 
2430                 ma = give_current_material(ob, index + 1);
2431                 if (ma && !BKE_scene_use_new_shading_nodes(scene)) {
2432                         manode = give_node_material(ma);
2433                         if (manode) {
2434                                 char str[MAX_ID_NAME + 12];
2435                                 BLI_snprintf(str, sizeof(str), IFACE_("Node %s"), manode->id.name + 2);
2436                                 uiItemL(sub, str, ui_id_icon_get(C, &manode->id, 1));
2437                         }
2438                         else if (ma->use_nodes) {
2439                                 uiItemL(sub, IFACE_("Node <none>"), ICON_NONE);
2440                         }
2441                 }
2442         }
2443         else if (itemptr->type == &RNA_ShapeKey) {
2444                 Object *ob = (Object *)activeptr->data;
2445                 Key *key = (Key *)itemptr->id.data;
2446                 KeyBlock *kb = (KeyBlock *)itemptr->data;
2447
2448                 split = uiLayoutSplit(sub, 0.66f, FALSE);
2449
2450                 uiItemL(split, name, icon);
2451
2452                 uiBlockSetEmboss(block, UI_EMBOSSN);
2453                 row = uiLayoutRow(split, TRUE);
2454                 if (i == 0 || (key->type != KEY_RELATIVE)) uiItemL(row, "", ICON_NONE);
2455                 else uiItemR(row, itemptr, "value", 0, "", ICON_NONE);
2456                 uiItemR(row, itemptr, "mute", 0, "", ICON_NONE);
2457
2458                 if ((kb->flag & KEYBLOCK_MUTE) ||
2459                     (ob->mode == OB_MODE_EDIT && !((ob->shapeflag & OB_SHAPE_EDIT_MODE) && ob->type == OB_MESH)))
2460                 {
2461                         uiLayoutSetActive(row, FALSE);
2462                 }
2463                 uiBlockSetEmboss(block, UI_EMBOSS);
2464         }
2465         else if (itemptr->type == &RNA_VertexGroup) {
2466                 bDeformGroup *dg = (bDeformGroup *)itemptr->data;
2467                 uiItemL(sub, name, icon);
2468                 /* RNA does not allow nice lock icons, use lower level buttons */
2469 #if 0
2470                 uiDefButR(block, OPTION, 0, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "lock_weight", 0, 0, 0, 0, 0,  NULL);
2471 #else
2472                 uiBlockSetEmboss(block, UI_EMBOSSN);
2473                 uiDefIconButBitC(block, TOG, DG_LOCK_WEIGHT, 0, (dg->flag & DG_LOCK_WEIGHT) ? ICON_LOCKED : ICON_UNLOCKED,
2474                                  0, 0, UI_UNIT_X, UI_UNIT_Y, &dg->flag, 0, 0, 0, 0,
2475                                  TIP_("Maintain relative weights while painting"));
2476                 uiBlockSetEmboss(block, UI_EMBOSS);
2477 #endif
2478         }
2479         else if (itemptr->type == &RNA_KeyingSetPath) {
2480                 KS_Path *ksp = (KS_Path *)itemptr->data;
2481                 
2482                 /* icon needs to be the type of ID which is currently active */
2483                 RNA_enum_icon_from_value(id_type_items, ksp->idtype, &icon);
2484                 
2485                 /* nothing else special to do... */
2486                 uiItemL(sub, name, icon); /* fails, backdrop LISTROW... */
2487         }
2488         else if (itemptr->type == &RNA_DynamicPaintSurface) {
2489                 char name_final[96];
2490                 const char *enum_name;
2491                 PropertyRNA *prop = RNA_struct_find_property(itemptr, "surface_type");
2492                 DynamicPaintSurface *surface = (DynamicPaintSurface *)itemptr->data;
2493
2494                 RNA_property_enum_name(C, itemptr, prop, RNA_property_enum_get(itemptr, prop), &enum_name);
2495
2496                 BLI_snprintf(name_final, sizeof(name_final), "%s (%s)", name, enum_name);
2497                 uiItemL(sub, name_final, icon);
2498                 if (dynamicPaint_surfaceHasColorPreview(surface)) {
2499                         uiBlockSetEmboss(block, UI_EMBOSSN);
2500                         uiDefIconButR(block, OPTION, 0,
2501                                       (surface->flags & MOD_DPAINT_PREVIEW) ? ICON_RESTRICT_VIEW_OFF : ICON_RESTRICT_VIEW_ON,
2502                                       0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "show_preview", 0, 0, 0, 0, 0, NULL);
2503                         uiBlockSetEmboss(block, UI_EMBOSS);
2504                 }
2505                 uiDefButR(block, OPTION, 0, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "is_active", i, 0, 0, 0, 0,  NULL);
2506         }
2507         else if (itemptr->type == &RNA_MovieTrackingObject) {
2508                 MovieTrackingObject *tracking_object = (MovieTrackingObject *)itemptr->data;
2509
2510                 split = uiLayoutSplit(sub, 0.75f, FALSE);
2511                 if (tracking_object->flag & TRACKING_OBJECT_CAMERA) {
2512                         uiItemL(split, name, ICON_CAMERA_DATA);
2513                 }
2514                 else {
2515                         uiItemL(split, name, ICON_OBJECT_DATA);
2516                 }
2517         }
2518         else if (itemptr->type == &RNA_MaskLayer) {
2519                 split = uiLayoutRow(sub, FALSE);
2520
2521                 uiItemL(split, name, icon);
2522
2523                 uiBlockSetEmboss(block, UI_EMBOSSN);
2524                 row = uiLayoutRow(split, TRUE);
2525                 uiItemR(row, itemptr, "alpha", 0, "", ICON_NONE);
2526                 uiItemR(row, itemptr, "hide", 0, "", ICON_NONE);
2527                 uiItemR(row, itemptr, "hide_select", 0, "", ICON_NONE);
2528                 uiItemR(row, itemptr, "hide_render", 0, "", ICON_NONE);
2529
2530                 uiBlockSetEmboss(block, UI_EMBOSS);
2531         }
2532
2533         /* There is a last chance to display custom controls (in addition to the name/label):
2534          * If the given item property group features a string property named as prop_list,
2535          * this tries to add controls for all properties of the item listed in that string property.
2536          * (colon-separated names).
2537          *
2538          * This is especially useful for python. E.g., if you list a collection of this property
2539          * group:
2540          *
2541          * class TestPropertyGroup(bpy.types.PropertyGroup):
2542          *     bool    = BoolProperty(default=False)
2543          *     integer = IntProperty()
2544          *     string  = StringProperty()
2545          * 
2546          *     # A string of all identifiers (colon-separated) which property's controls should be
2547          *     # displayed in a template_list.
2548          *     template_list_controls = StringProperty(default="integer:bool:string", options={"HIDDEN"})
2549          *
2550          * ... you'll get a numfield for the integer prop, a check box for the bool prop, and a textfield
2551          * for the string prop, after the name of each item of the collection.
2552          */
2553         else if (prop_list_id) {
2554                 row = uiLayoutRow(sub, TRUE);
2555                 uiItemL(row, name, icon);
2556
2557                 /* XXX: Check, as sometimes we get an itemptr looking like
2558                  *      {id = {data = 0x0}, type = 0x0, data = 0x0}
2559                  *      which would obviously produce a sigsev... */
2560                 if (itemptr->type) {
2561                         /* If the special property is set for the item, and it is a collection... */
2562                         PropertyRNA *prop_list = RNA_struct_find_property(itemptr, prop_list_id);
2563
2564                         if (prop_list && RNA_property_type(prop_list) == PROP_STRING) {
2565                                 int prop_names_len;
2566                                 char *prop_names = RNA_property_string_get_alloc(itemptr, prop_list, NULL, 0, &prop_names_len);
2567                                 char *prop_names_end = prop_names + prop_names_len;
2568                                 char *id = prop_names;
2569                                 char *id_next;
2570                                 while (id < prop_names_end) {
2571                                         if ((id_next = strchr(id, ':'))) *id_next++ = '\0';
2572                                         else id_next = prop_names_end;
2573                                         uiItemR(row, itemptr, id, 0, NULL, ICON_NONE);
2574                                         id = id_next;
2575                                 }
2576                                 MEM_freeN(prop_names);
2577                         }
2578                 }
2579         }
2580
2581         else
2582                 uiItemL(sub, name, icon);  /* fails, backdrop LISTROW... */
2583
2584         /* free name */
2585         if (namebuf) {
2586                 MEM_freeN(namebuf);
2587         }
2588 }
2589
2590 void uiTemplateList(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, PointerRNA *activeptr,
2591                     const char *activepropname, const char *prop_list, int rows, int maxrows, int listtype)
2592 {
2593         PropertyRNA *prop = NULL, *activeprop;
2594         PropertyType type, activetype;
2595         StructRNA *ptype;
2596         uiLayout *box, *row, *col;
2597         uiBlock *block;
2598         uiBut *but;
2599         Panel *pa;
2600         const char *name;
2601         char numstr[32];
2602         int rnaicon = 0, icon = 0, i = 0, activei = 0, len = 0, items, found, min, max;
2603
2604         /* validate arguments */
2605         block = uiLayoutGetBlock(layout);
2606         pa = block->panel;
2607
2608         if (!pa) {
2609                 RNA_warning("Only works inside a panel");
2610                 return;
2611         }
2612
2613         if (!activeptr->data)
2614                 return;
2615         
2616         if (ptr->data) {
2617                 prop = RNA_struct_find_property(ptr, propname);
2618                 if (!prop) {
2619                         RNA_warning("Property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2620                         return;
2621                 }
2622         }
2623
2624         activeprop = RNA_struct_find_property(activeptr, activepropname);
2625         if (!activeprop) {
2626                 RNA_warning("Property not found: %s.%s", RNA_struct_identifier(ptr->type), activepropname);
2627                 return;
2628         }
2629
2630         if (prop) {
2631                 type = RNA_property_type(prop);
2632                 if (type != PROP_COLLECTION) {
2633                         RNA_warning("uiExpected collection property");
2634                         return;
2635                 }
2636         }
2637
2638         activetype = RNA_property_type(activeprop);
2639         if (activetype != PROP_INT) {
2640                 RNA_warning("Expected integer property");
2641                 return;
2642         }
2643
2644         /* get icon */
2645         if (ptr->data && prop) {
2646                 ptype = RNA_property_pointer_type(ptr, prop);
2647                 rnaicon = RNA_struct_ui_icon(ptype);
2648         }
2649
2650         /* get active data */
2651         activei = RNA_property_int_get(activeptr, activeprop);
2652
2653         if (listtype == 'i') {
2654                 box = uiLayoutListBox(layout, ptr, prop, activeptr, activeprop);
2655                 col = uiLayoutColumn(box, TRUE);
2656                 row = uiLayoutRow(col, FALSE);
2657
2658                 if (ptr->data && prop) {
2659                         /* create list items */
2660                         RNA_PROP_BEGIN (ptr, itemptr, prop)
2661                         {
2662                                 /* create button */
2663                                 if (!(i % 9))
2664                                         row = uiLayoutRow(col, FALSE);
2665
2666                                 icon = list_item_icon_get(C, &itemptr, rnaicon, 1);
2667                                 but = uiDefIconButR_prop(block, LISTROW, 0, icon, 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, activeptr,
2668                                                          activeprop, 0, 0, i, 0, 0, "");
2669                                 uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2670                                 
2671
2672                                 i++;
2673                         }
2674                         RNA_PROP_END;
2675                 }
2676         }
2677         else if (listtype == 'c') {
2678                 /* compact layout */
2679
2680                 row = uiLayoutRow(layout, TRUE);
2681
2682                 if (ptr->data && prop) {
2683                         /* create list items */
2684                         RNA_PROP_BEGIN (ptr, itemptr, prop)
2685                         {
2686                                 found = (activei == i);
2687
2688                                 if (found) {
2689                                         /* create button */
2690                                         name = RNA_struct_name_get_alloc(&itemptr, NULL, 0, NULL);
2691                                         icon = list_item_icon_get(C, &itemptr, rnaicon, 0);
2692                                         uiItemL(row, (name) ? name : "", icon);
2693
2694                                         if (name) {
2695                                                 MEM_freeN((void *)name);
2696                                         }
2697                                 }
2698
2699                                 i++;
2700                         }
2701                         RNA_PROP_END;
2702                 }
2703
2704                 /* if not found, add in dummy button */
2705                 if (i == 0)
2706                         uiItemL(row, "", ICON_NONE);
2707
2708                 /* next/prev button */
2709                 BLI_snprintf(numstr, sizeof(numstr), "%d :", i);
2710                 but = uiDefIconTextButR_prop(block, NUM, 0, 0, numstr, 0, 0, UI_UNIT_X * 5, UI_UNIT_Y, activeptr,
2711                                              activeprop, 0, 0, 0, 0, 0, "");
2712                 if (i == 0)
2713                         uiButSetFlag(but, UI_BUT_DISABLED);
2714         }
2715         else {
2716                 /* default rows */
2717                 if (rows == 0)
2718                         rows = 5;
2719                 if (maxrows == 0)
2720                         maxrows = 5;
2721                 if (pa->list_grip_size != 0)
2722                         rows = pa->list_grip_size;
2723
2724                 /* layout */
2725                 box = uiLayoutListBox(layout, ptr, prop, activeptr, activeprop);
2726                 row = uiLayoutRow(box, FALSE);
2727                 col = uiLayoutColumn(row, TRUE);
2728
2729                 /* init numbers */
2730                 RNA_property_int_range(activeptr, activeprop, &min, &max);
2731
2732                 if (prop)
2733                         len = RNA_property_collection_length(ptr, prop);
2734                 items = CLAMPIS(len, rows, MAX2(rows, maxrows));
2735
2736                 /* if list length changes and active is out of view, scroll to it */
2737                 if (pa->list_last_len != len)
2738                         if ((activei < pa->list_scroll || activei >= pa->list_scroll + items))
2739                                 pa->list_scroll = activei;
2740
2741                 pa->list_scroll = MIN2(pa->list_scroll, len - items);
2742                 pa->list_scroll = MAX2(pa->list_scroll, 0);
2743                 pa->list_size = items;
2744                 pa->list_last_len = len;
2745
2746                 if (ptr->data && prop) {
2747                         /* create list items */
2748                         RNA_PROP_BEGIN (ptr, itemptr, prop)
2749                         {
2750                                 if (i >= pa->list_scroll && i < pa->list_scroll + items)
2751                                         list_item_row(C, col, ptr, &itemptr, i, rnaicon, activeptr, activeprop, prop_list);
2752
2753                                 i++;
2754                         }
2755                         RNA_PROP_END;
2756                 }
2757
2758                 /* add dummy buttons to fill space */
2759                 while (i < pa->list_scroll + items) {
2760                         if (i >= pa->list_scroll)
2761                                 uiItemL(col, "", ICON_NONE);
2762                         i++;
2763                 }
2764
2765                 /* add scrollbar */
2766                 if (len > items) {
2767                         col = uiLayoutColumn(row, FALSE);
2768                         uiDefButI(block, SCROLL, 0, "", 0, 0, UI_UNIT_X * 0.75, UI_UNIT_Y * items, &pa->list_scroll,
2769                                   0, len - items, items, 0, "");
2770                 }
2771         }
2772 }
2773
2774 /************************* Operator Search Template **************************/
2775
2776 static void operator_call_cb(bContext *C, void *UNUSED(arg1), void *arg2)
2777 {
2778         wmOperatorType *ot = arg2;
2779         
2780         if (ot)
2781                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
2782 }
2783
2784 static void operator_search_cb(const bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
2785 {
2786         GHashIterator *iter = WM_operatortype_iter();
2787
2788         for (; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
2789                 wmOperatorType *ot = BLI_ghashIterator_getValue(iter);
2790
2791                 if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0)
2792                         continue;
2793
2794                 if (BLI_strcasestr(ot->name, str)) {
2795                         if (WM_operator_poll((bContext *)C, ot)) {
2796                                 char name[256];
2797                                 int len = strlen(ot->name);
2798                                 
2799                                 /* display name for menu, can hold hotkey */
2800                                 BLI_strncpy(name, ot->name, sizeof(name));
2801                                 
2802                                 /* check for hotkey */
2803                                 if (len < sizeof(name) - 6) {
2804                                         if (WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, TRUE,
2805                                                                          &name[len + 1], sizeof(name) - len - 1))
2806                                         {
2807                                                 name[len] = '|';
2808                                         }
2809                                 }
2810                                 
2811                                 if (0 == uiSearchItemAdd(items, name, ot, 0))
2812                                         break;
2813                         }
2814                 }
2815         }
2816         BLI_ghashIterator_free(iter);
2817 }
2818
2819 void uiOperatorSearch_But(uiBut *but)
2820 {
2821         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
2822 }
2823
2824 void uiTemplateOperatorSearch(uiLayout *layout)
2825 {
2826         uiBlock *block;
2827         uiBut *but;
2828         static char search[256] = "";
2829                 
2830         block = uiLayoutGetBlock(layout);
2831         uiBlockSetCurLayout(block, layout);
2832
2833         but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, 0, 0, "");
2834         uiOperatorSearch_But(but);
2835 }
2836
2837 /************************* Running Jobs Template **************************/
2838
2839 #define B_STOPRENDER    1
2840 #define B_STOPCAST      2
2841 #define B_STOPANIM      3
2842 #define B_STOPCOMPO     4
2843 #define B_STOPSEQ       5
2844 #define B_STOPCLIP      6
2845 #define B_STOPOTHER     7
2846
2847 static void do_running_jobs(bContext *C, void *UNUSED(arg), int event)
2848 {
2849         switch (event) {
2850                 case B_STOPRENDER:
2851                         G.is_break = TRUE;
2852                         break;
2853                 case B_STOPCAST:
2854                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_screen(C), NULL);
2855                         break;
2856                 case B_STOPANIM:
2857                         WM_operator_name_call(C, "SCREEN_OT_animation_play", WM_OP_INVOKE_SCREEN, NULL);
2858                         break;
2859                 case B_STOPCOMPO:
2860                         WM_jobs_stop(CTX_wm_manager(C), CTX_data_scene(C), NULL);
2861                         break;
2862                 case B_STOPSEQ:
2863                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2864                         break;
2865                 case B_STOPCLIP:
2866                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2867                         break;
2868                 case B_STOPOTHER:
2869                         G.is_break = TRUE;
2870                         break;
2871         }
2872 }
2873
2874 void uiTemplateRunningJobs(uiLayout *layout, bContext *C)
2875 {
2876         bScreen *screen = CTX_wm_screen(C);
2877         wmWindowManager *wm = CTX_wm_manager(C);
2878         ScrArea *sa = CTX_wm_area(C);
2879         uiBlock *block;
2880         void *owner = NULL;
2881         int handle_event;
2882         
2883         block = uiLayoutGetBlock(layout);
2884         uiBlockSetCurLayout(block, layout);
2885
2886         uiBlockSetHandleFunc(block, do_running_jobs, NULL);
2887
2888         if (sa->spacetype == SPACE_SEQ) {
2889                 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2890                         owner = sa;
2891                 handle_event = B_STOPSEQ;
2892         }
2893         else if (sa->spacetype == SPACE_CLIP) {
2894                 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2895                         owner = sa;
2896                 handle_event = B_STOPCLIP;
2897         }
2898         else {
2899                 Scene *scene;
2900                 /* another scene can be rendering too, for example via compositor */
2901                 for (scene = CTX_data_main(C)->scene.first; scene; scene = scene->id.next) {
2902                         if (WM_jobs_test(wm, scene, WM_JOB_TYPE_RENDER)) {
2903                                 handle_event = B_STOPRENDER;
2904                                 break;
2905                         }
2906            &nbs