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