operator spacebar search menu wasn't ignoring internal operators,
[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                 const float range_clamp[2]   = {0.0f, 1.0f};
2025                 const float range_unclamp[2] = {-1000.0f, 1000.0f};  /* arbitrary limits here */
2026                 const float *range = (cumap->flag & CUMA_DO_CLIP) ? range_clamp : range_unclamp;
2027
2028                 uiLayoutRow(layout, TRUE);
2029                 uiBlockSetNFunc(block, curvemap_buttons_update, MEM_dupallocN(cb), cumap);
2030                 bt = uiDefButF(block, NUM, 0, "X", 0, 2 * UI_UNIT_Y, UI_UNIT_X * 10, UI_UNIT_Y,
2031                                &cmp->x, range[0], range[1], 1, 5, "");
2032                 bt = uiDefButF(block, NUM, 0, "Y", 0, 1 * UI_UNIT_Y, UI_UNIT_X * 10, UI_UNIT_Y,
2033                                &cmp->y, range[0], range[1], 1, 5, "");
2034         }
2035
2036         /* black/white levels */
2037         if (levels) {
2038                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2039                 uiItemR(uiLayoutColumn(split, FALSE), ptr, "black_level", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2040                 uiItemR(uiLayoutColumn(split, FALSE), ptr, "white_level", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2041
2042                 uiLayoutRow(layout, FALSE);
2043                 bt = uiDefBut(block, BUT, 0, IFACE_("Reset"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0,
2044                               TIP_("Reset Black/White point and curves"));
2045                 uiButSetNFunc(bt, curvemap_buttons_reset, MEM_dupallocN(cb), cumap);
2046         }
2047
2048         uiBlockSetNFunc(block, NULL, NULL, NULL);
2049 }
2050
2051 void uiTemplateCurveMapping(uiLayout *layout, PointerRNA *ptr, const char *propname, int type, int levels, int brush)
2052 {
2053         RNAUpdateCb *cb;
2054         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2055         PointerRNA cptr;
2056
2057         if (!prop) {
2058                 RNA_warning("curve property not found: %s.%s",
2059                             RNA_struct_identifier(ptr->type), propname);
2060                 return;
2061         }
2062
2063         if (RNA_property_type(prop) != PROP_POINTER) {
2064                 RNA_warning("curve is not a pointer: %s.%s",
2065                             RNA_struct_identifier(ptr->type), propname);
2066                 return;
2067         }
2068
2069         cptr = RNA_property_pointer_get(ptr, prop);
2070         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_CurveMapping))
2071                 return;
2072
2073         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
2074         cb->ptr = *ptr;
2075         cb->prop = prop;
2076
2077         curvemap_buttons_layout(layout, &cptr, type, levels, brush, cb);
2078
2079         MEM_freeN(cb);
2080 }
2081
2082 /********************* ColorPicker Template ************************/
2083
2084 #define WHEEL_SIZE  100
2085
2086 /* This template now follows User Preference for type - name is not correct anymore... */
2087 void uiTemplateColorPicker(uiLayout *layout, PointerRNA *ptr, const char *propname, int value_slider,
2088                            int lock, int lock_luminosity, int cubic)
2089 {
2090         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2091         uiBlock *block = uiLayoutGetBlock(layout);
2092         uiLayout *col, *row;
2093         uiBut *but = NULL;
2094         float softmin, softmax, step, precision;
2095
2096         if (!prop) {
2097                 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2098                 return;
2099         }
2100
2101         RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2102
2103         col = uiLayoutColumn(layout, TRUE);
2104         row = uiLayoutRow(col, TRUE);
2105
2106         switch (U.color_picker_type) {
2107                 case USER_CP_CIRCLE:
2108                         but = uiDefButR_prop(block, HSVCIRCLE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2109                                              -1, 0.0, 0.0, 0, 0, "");
2110                         break;
2111                 case USER_CP_SQUARE_SV:
2112                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2113                                              -1, 0.0, 0.0, UI_GRAD_SV, 0, "");
2114                         break;
2115                 case USER_CP_SQUARE_HS:
2116                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2117                                              -1, 0.0, 0.0, UI_GRAD_HS, 0, "");
2118                         break;
2119                 case USER_CP_SQUARE_HV:
2120                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2121                                              -1, 0.0, 0.0, UI_GRAD_HV, 0, "");
2122                         break;
2123         }
2124
2125         if (lock) {
2126                 but->flag |= UI_BUT_COLOR_LOCK;
2127         }
2128
2129         if (lock_luminosity) {
2130                 float color[4]; /* in case of alpha */
2131                 but->flag |= UI_BUT_VEC_SIZE_LOCK;
2132                 RNA_property_float_get_array(ptr, prop, color);
2133                 but->a2 = len_v3(color);
2134         }
2135
2136         if (cubic)
2137                 but->flag |= UI_BUT_COLOR_CUBIC;
2138
2139         
2140         if (value_slider) {
2141                 switch (U.color_picker_type) {
2142                         case USER_CP_CIRCLE:
2143                                 uiItemS(row);
2144                                 uiDefButR_prop(block, HSVCUBE, 0, "", WHEEL_SIZE + 6, 0, 14, WHEEL_SIZE, ptr, prop,
2145                                                -1, softmin, softmax, UI_GRAD_V_ALT, 0, "");
2146                                 break;
2147                         case USER_CP_SQUARE_SV:
2148                                 uiItemS(col);
2149                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2150                                                -1, softmin, softmax, UI_GRAD_SV + 3, 0, "");
2151                                 break;
2152                         case USER_CP_SQUARE_HS:
2153                                 uiItemS(col);
2154                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2155                                                -1, softmin, softmax, UI_GRAD_HS + 3, 0, "");
2156                                 break;
2157                         case USER_CP_SQUARE_HV:
2158                                 uiItemS(col);
2159                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2160                                                -1, softmin, softmax, UI_GRAD_HV + 3, 0, "");
2161                                 break;
2162                 }
2163         }
2164 }
2165
2166 /********************* Layer Buttons Template ************************/
2167
2168 static void handle_layer_buttons(bContext *C, void *arg1, void *arg2)
2169 {
2170         uiBut *but = arg1;
2171         int cur = GET_INT_FROM_POINTER(arg2);
2172         wmWindow *win = CTX_wm_window(C);
2173         int i, tot, shift = win->eventstate->shift;
2174
2175         if (!shift) {
2176                 tot = RNA_property_array_length(&but->rnapoin, but->rnaprop);
2177                 
2178                 /* Normally clicking only selects one layer */
2179                 RNA_property_boolean_set_index(&but->rnapoin, but->rnaprop, cur, TRUE);
2180                 for (i = 0; i < tot; ++i) {
2181                         if (i != cur)
2182                                 RNA_property_boolean_set_index(&but->rnapoin, but->rnaprop, i, FALSE);
2183                 }
2184         }
2185
2186         /* view3d layer change should update depsgraph (invisible object changed maybe) */
2187         /* see view3d_header.c */
2188 }
2189
2190 /* TODO:
2191  * - for now, grouping of layers is determined by dividing up the length of
2192  *   the array of layer bitflags */
2193
2194 void uiTemplateLayers(uiLayout *layout, PointerRNA *ptr, const char *propname,
2195                       PointerRNA *used_ptr, const char *used_propname, int active_layer)
2196 {
2197         uiLayout *uRow, *uCol;
2198         PropertyRNA *prop, *used_prop = NULL;
2199         int groups, cols, layers;
2200         int group, col, layer, row;
2201         int cols_per_group = 5;
2202
2203         prop = RNA_struct_find_property(ptr, propname);
2204         if (!prop) {
2205                 RNA_warning("layers property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2206                 return;
2207         }
2208         
2209         /* the number of layers determines the way we group them 
2210          *      - we want 2 rows only (for now)
2211          *      - the number of columns (cols) is the total number of buttons per row
2212          *        the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
2213          *      - for now, only split into groups if group will have at least 5 items
2214          */
2215         layers = RNA_property_array_length(ptr, prop);
2216         cols = (layers / 2) + (layers % 2);
2217         groups = ((cols / 2) < cols_per_group) ? (1) : (cols / cols_per_group);
2218
2219         if (used_ptr && used_propname) {
2220                 used_prop = RNA_struct_find_property(used_ptr, used_propname);
2221                 if (!used_prop) {
2222                         RNA_warning("used layers property not found: %s.%s", RNA_struct_identifier(ptr->type), used_propname);
2223                         return;
2224                 }
2225
2226                 if (RNA_property_array_length(used_ptr, used_prop) < layers)
2227                         used_prop = NULL;
2228         }
2229         
2230         /* layers are laid out going across rows, with the columns being divided into groups */
2231         
2232         for (group = 0; group < groups; group++) {
2233                 uCol = uiLayoutColumn(layout, TRUE);
2234                 
2235                 for (row = 0; row < 2; row++) {
2236                         uiBlock *block;
2237                         uiBut *but;
2238
2239                         uRow = uiLayoutRow(uCol, TRUE);
2240                         block = uiLayoutGetBlock(uRow);
2241                         layer = groups * cols_per_group * row + cols_per_group * group;
2242                         
2243                         /* add layers as toggle buts */
2244                         for (col = 0; (col < cols_per_group) && (layer < layers); col++, layer++) {
2245                                 int icon = 0;
2246                                 int butlay = 1 << layer;
2247
2248                                 if (active_layer & butlay)
2249                                         icon = ICON_LAYER_ACTIVE;
2250                                 else if (used_prop && RNA_property_boolean_get_index(used_ptr, used_prop, layer))
2251                                         icon = ICON_LAYER_USED;
2252                                 
2253                                 but = uiDefAutoButR(block, ptr, prop, layer, "", icon, 0, 0, UI_UNIT_X / 2, UI_UNIT_Y / 2);
2254                                 uiButSetFunc(but, handle_layer_buttons, but, SET_INT_IN_POINTER(layer));
2255                                 but->type = TOG;
2256                         }
2257                 }
2258         }
2259 }
2260
2261 void uiTemplateGameStates(uiLayout *layout, PointerRNA *ptr, const char *propname,
2262                       PointerRNA *used_ptr, const char *used_propname, int active_state)
2263 {
2264         uiLayout *uRow, *uCol;
2265         PropertyRNA *prop, *used_prop = NULL;
2266         int groups, cols, states;
2267         int group, col, state, row;
2268         int cols_per_group = 5;
2269         Object *ob = (Object *)ptr->id.data;
2270
2271         prop = RNA_struct_find_property(ptr, propname);
2272         if (!prop) {
2273                 RNA_warning("states property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2274                 return;
2275         }
2276         
2277         /* the number of states determines the way we group them 
2278          *      - we want 2 rows only (for now)
2279          *      - the number of columns (cols) is the total number of buttons per row
2280          *        the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
2281          *      - for now, only split into groups if group will have at least 5 items
2282          */
2283         states = RNA_property_array_length(ptr, prop);
2284         cols = (states / 2) + (states % 2);
2285         groups = ((cols / 2) < cols_per_group) ? (1) : (cols / cols_per_group);
2286
2287         if (used_ptr && used_propname) {
2288                 used_prop = RNA_struct_find_property(used_ptr, used_propname);
2289                 if (!used_prop) {
2290                         RNA_warning("used layers property not found: %s.%s", RNA_struct_identifier(ptr->type), used_propname);
2291                         return;
2292                 }
2293
2294                 if (RNA_property_array_length(used_ptr, used_prop) < states)
2295                         used_prop = NULL;
2296         }
2297         
2298         /* layers are laid out going across rows, with the columns being divided into groups */
2299         
2300         for (group = 0; group < groups; group++) {
2301                 uCol = uiLayoutColumn(layout, TRUE);
2302                 
2303                 for (row = 0; row < 2; row++) {
2304                         uiBlock *block;
2305                         uiBut *but;
2306
2307                         uRow = uiLayoutRow(uCol, TRUE);
2308                         block = uiLayoutGetBlock(uRow);
2309                         state = groups * cols_per_group * row + cols_per_group * group;
2310                         
2311                         /* add layers as toggle buts */
2312                         for (col = 0; (col < cols_per_group) && (state < states); col++, state++) {
2313                                 int icon = 0;
2314                                 int butlay = 1 << state;
2315
2316                                 if (active_state & butlay)
2317                                         icon = ICON_LAYER_ACTIVE;
2318                                 else if (used_prop && RNA_property_boolean_get_index(used_ptr, used_prop, state))
2319                                         icon = ICON_LAYER_USED;
2320                                 
2321                                 but = uiDefIconButR_prop(block, ICONTOG, 0, icon, 0, 0, UI_UNIT_X / 2, UI_UNIT_Y / 2, ptr, prop,
2322                                                          state, 0, 0, -1, -1, sca_state_name_get(ob, state));
2323                                 uiButSetFunc(but, handle_layer_buttons, but, SET_INT_IN_POINTER(state));
2324                                 but->type = TOG;
2325                         }
2326                 }
2327         }
2328 }
2329
2330
2331 /************************* List Template **************************/
2332
2333 static int list_item_icon_get(bContext *C, PointerRNA *itemptr, int rnaicon, int big)
2334 {
2335         ID *id = NULL;
2336         int icon;
2337
2338         if (!itemptr->data)
2339                 return rnaicon;
2340
2341         /* try ID, material or texture slot */
2342         if (RNA_struct_is_ID(itemptr->type)) {
2343                 id = itemptr->id.data;
2344         }
2345         else if (RNA_struct_is_a(itemptr->type, &RNA_MaterialSlot)) {
2346                 id = RNA_pointer_get(itemptr, "material").data;
2347         }
2348         else if (RNA_struct_is_a(itemptr->type, &RNA_TextureSlot)) {
2349                 id = RNA_pointer_get(itemptr, "texture").data;
2350         }
2351         else if (RNA_struct_is_a(itemptr->type, &RNA_DynamicPaintSurface)) {
2352                 DynamicPaintSurface *surface = (DynamicPaintSurface *)itemptr->data;
2353
2354                 if (surface->format == MOD_DPAINT_SURFACE_F_PTEX) return ICON_TEXTURE_SHADED;
2355                 else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX) return ICON_OUTLINER_DATA_MESH;
2356                 else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ) return ICON_FILE_IMAGE;
2357         }
2358
2359         /* get icon from ID */
2360         if (id) {
2361                 icon = ui_id_icon_get(C, id, big);
2362
2363                 if (icon)
2364                         return icon;
2365         }
2366
2367         return rnaicon;
2368 }
2369
2370 static void list_item_row(bContext *C, uiLayout *layout, PointerRNA *ptr, PointerRNA *itemptr, int i,
2371                           int rnaicon, PointerRNA *activeptr, PropertyRNA *activeprop, const char *prop_list_id)
2372 {
2373         uiBlock *block = uiLayoutGetBlock(layout);
2374         uiBut *but;
2375         uiLayout *split, *overlap, *sub, *row;
2376         char *namebuf;
2377         const char *name;
2378         int icon;
2379
2380         overlap = uiLayoutOverlap(layout);
2381
2382         /* list item behind label & other buttons */
2383         sub = uiLayoutRow(overlap, FALSE);
2384
2385         but = uiDefButR_prop(block, LISTROW, 0, "", 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, activeptr, activeprop,
2386                              0, 0, i, 0, 0, "");
2387         uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2388
2389         sub = uiLayoutRow(overlap, FALSE);
2390
2391         /* retrieve icon and name */
2392         icon = list_item_icon_get(C, itemptr, rnaicon, 0);
2393         if (icon == ICON_NONE || icon == ICON_DOT)
2394                 icon = 0;
2395
2396         namebuf = RNA_struct_name_get_alloc(itemptr, NULL, 0, NULL);
2397         name = (namebuf) ? namebuf : "";
2398
2399         /* hardcoded types */
2400         if (itemptr->type == &RNA_MeshTexturePolyLayer || itemptr->type == &RNA_MeshLoopColorLayer) {
2401                 uiItemL(sub, name, icon);
2402                 uiBlockSetEmboss(block, UI_EMBOSSN);
2403                 uiDefIconButR(block, TOG, 0, ICON_SCENE, 0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "active_render",
2404                               0, 0, 0, 0, 0, NULL);
2405                 uiBlockSetEmboss(block, UI_EMBOSS);
2406         }
2407         else if (RNA_struct_is_a(itemptr->type, &RNA_MaterialTextureSlot)) {
2408                 uiItemL(sub, name, icon);
2409                 uiBlockSetEmboss(block, UI_EMBOSS);
2410                 uiDefButR(block, OPTION, 0, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, ptr, "use_textures", i, 0, 0, 0, 0,  NULL);
2411         }
2412         else if (RNA_struct_is_a(itemptr->type, &RNA_SceneRenderLayer)) {
2413                 uiItemL(sub, name, icon);
2414                 uiBlockSetEmboss(block, UI_EMBOSS);
2415                 uiDefButR(block, OPTION, 0, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "use", 0, 0, 0, 0, 0,  NULL);
2416         }
2417         else if (RNA_struct_is_a(itemptr->type, &RNA_MaterialSlot)) {
2418                 /* provision to draw active node name */
2419                 Material *ma, *manode;
2420                 Scene *scene = CTX_data_scene(C);
2421                 Object *ob = (Object *)ptr->id.data;
2422                 int index = (Material **)itemptr->data - ob->mat;
2423                 
2424                 /* default item with material base name */
2425                 uiItemL(sub, name, icon);
2426                 
2427                 ma = give_current_material(ob, index + 1);
2428                 if (ma && !BKE_scene_use_new_shading_nodes(scene)) {
2429                         manode = give_node_material(ma);
2430                         if (manode) {
2431                                 char str[MAX_ID_NAME + 12];
2432                                 BLI_snprintf(str, sizeof(str), IFACE_("Node %s"), manode->id.name + 2);
2433                                 uiItemL(sub, str, ui_id_icon_get(C, &manode->id, 1));
2434                         }
2435                         else if (ma->use_nodes) {
2436                                 uiItemL(sub, IFACE_("Node <none>"), ICON_NONE);
2437                         }
2438                 }
2439         }
2440         else if (itemptr->type == &RNA_ShapeKey) {
2441                 Object *ob = (Object *)activeptr->data;
2442                 Key *key = (Key *)itemptr->id.data;
2443                 KeyBlock *kb = (KeyBlock *)itemptr->data;
2444
2445                 split = uiLayoutSplit(sub, 0.66f, FALSE);
2446
2447                 uiItemL(split, name, icon);
2448
2449                 uiBlockSetEmboss(block, UI_EMBOSSN);
2450                 row = uiLayoutRow(split, TRUE);
2451                 if (i == 0 || (key->type != KEY_RELATIVE)) uiItemL(row, "", ICON_NONE);
2452                 else uiItemR(row, itemptr, "value", 0, "", ICON_NONE);
2453                 uiItemR(row, itemptr, "mute", 0, "", ICON_NONE);
2454
2455                 if ((kb->flag & KEYBLOCK_MUTE) ||
2456                     (ob->mode == OB_MODE_EDIT && !((ob->shapeflag & OB_SHAPE_EDIT_MODE) && ob->type == OB_MESH)))
2457                 {
2458                         uiLayoutSetActive(row, FALSE);
2459                 }
2460                 uiBlockSetEmboss(block, UI_EMBOSS);
2461         }
2462         else if (itemptr->type == &RNA_VertexGroup) {
2463                 bDeformGroup *dg = (bDeformGroup *)itemptr->data;
2464                 uiItemL(sub, name, icon);
2465                 /* RNA does not allow nice lock icons, use lower level buttons */
2466 #if 0
2467                 uiDefButR(block, OPTION, 0, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "lock_weight", 0, 0, 0, 0, 0,  NULL);
2468 #else
2469                 uiBlockSetEmboss(block, UI_EMBOSSN);
2470                 uiDefIconButBitC(block, TOG, DG_LOCK_WEIGHT, 0, (dg->flag & DG_LOCK_WEIGHT) ? ICON_LOCKED : ICON_UNLOCKED,
2471                                  0, 0, UI_UNIT_X, UI_UNIT_Y, &dg->flag, 0, 0, 0, 0,
2472                                  TIP_("Maintain relative weights while painting"));
2473                 uiBlockSetEmboss(block, UI_EMBOSS);
2474 #endif
2475         }
2476         else if (itemptr->type == &RNA_KeyingSetPath) {
2477                 KS_Path *ksp = (KS_Path *)itemptr->data;
2478                 
2479                 /* icon needs to be the type of ID which is currently active */
2480                 RNA_enum_icon_from_value(id_type_items, ksp->idtype, &icon);
2481                 
2482                 /* nothing else special to do... */
2483                 uiItemL(sub, name, icon); /* fails, backdrop LISTROW... */
2484         }
2485         else if (itemptr->type == &RNA_DynamicPaintSurface) {
2486                 char name_final[96];
2487                 const char *enum_name;
2488                 PropertyRNA *prop = RNA_struct_find_property(itemptr, "surface_type");
2489                 DynamicPaintSurface *surface = (DynamicPaintSurface *)itemptr->data;
2490
2491                 RNA_property_enum_name(C, itemptr, prop, RNA_property_enum_get(itemptr, prop), &enum_name);
2492
2493                 BLI_snprintf(name_final, sizeof(name_final), "%s (%s)", name, enum_name);
2494                 uiItemL(sub, name_final, icon);
2495                 if (dynamicPaint_surfaceHasColorPreview(surface)) {
2496                         uiBlockSetEmboss(block, UI_EMBOSSN);
2497                         uiDefIconButR(block, OPTION, 0,
2498                                       (surface->flags & MOD_DPAINT_PREVIEW) ? ICON_RESTRICT_VIEW_OFF : ICON_RESTRICT_VIEW_ON,
2499                                       0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "show_preview", 0, 0, 0, 0, 0, NULL);
2500                         uiBlockSetEmboss(block, UI_EMBOSS);
2501                 }
2502                 uiDefButR(block, OPTION, 0, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, itemptr, "is_active", i, 0, 0, 0, 0,  NULL);
2503         }
2504         else if (itemptr->type == &RNA_MovieTrackingObject) {
2505                 MovieTrackingObject *tracking_object = (MovieTrackingObject *)itemptr->data;
2506
2507                 split = uiLayoutSplit(sub, 0.75f, FALSE);
2508                 if (tracking_object->flag & TRACKING_OBJECT_CAMERA) {
2509                         uiItemL(split, name, ICON_CAMERA_DATA);
2510                 }
2511                 else {
2512                         uiItemL(split, name, ICON_OBJECT_DATA);
2513                 }
2514         }
2515         else if (itemptr->type == &RNA_MaskLayer) {
2516                 split = uiLayoutRow(sub, FALSE);
2517
2518                 uiItemL(split, name, icon);
2519
2520                 uiBlockSetEmboss(block, UI_EMBOSSN);
2521                 row = uiLayoutRow(split, TRUE);
2522                 uiItemR(row, itemptr, "alpha", 0, "", ICON_NONE);
2523                 uiItemR(row, itemptr, "hide", 0, "", ICON_NONE);
2524                 uiItemR(row, itemptr, "hide_select", 0, "", ICON_NONE);
2525                 uiItemR(row, itemptr, "hide_render", 0, "", ICON_NONE);
2526
2527                 uiBlockSetEmboss(block, UI_EMBOSS);
2528         }
2529
2530         /* There is a last chance to display custom controls (in addition to the name/label):
2531          * If the given item property group features a string property named as prop_list,
2532          * this tries to add controls for all properties of the item listed in that string property.
2533          * (colon-separated names).
2534          *
2535          * This is especially useful for python. E.g., if you list a collection of this property
2536          * group:
2537          *
2538          * class TestPropertyGroup(bpy.types.PropertyGroup):
2539          *     bool    = BoolProperty(default=False)
2540          *     integer = IntProperty()
2541          *     string  = StringProperty()
2542          * 
2543          *     # A string of all identifiers (colon-separated) which property's controls should be
2544          *     # displayed in a template_list.
2545          *     template_list_controls = StringProperty(default="integer:bool:string", options={"HIDDEN"})
2546          *
2547          * ... you'll get a numfield for the integer prop, a check box for the bool prop, and a textfield
2548          * for the string prop, after the name of each item of the collection.
2549          */
2550         else if (prop_list_id) {
2551                 row = uiLayoutRow(sub, TRUE);
2552                 uiItemL(row, name, icon);
2553
2554                 /* XXX: Check, as sometimes we get an itemptr looking like
2555                  *      {id = {data = 0x0}, type = 0x0, data = 0x0}
2556                  *      which would obviously produce a sigsev... */
2557                 if (itemptr->type) {
2558                         /* If the special property is set for the item, and it is a collection... */
2559                         PropertyRNA *prop_list = RNA_struct_find_property(itemptr, prop_list_id);
2560
2561                         if (prop_list && RNA_property_type(prop_list) == PROP_STRING) {
2562                                 int prop_names_len;
2563                                 char *prop_names = RNA_property_string_get_alloc(itemptr, prop_list, NULL, 0, &prop_names_len);
2564                                 char *prop_names_end = prop_names + prop_names_len;
2565                                 char *id = prop_names;
2566                                 char *id_next;
2567                                 while (id < prop_names_end) {
2568                                         if ((id_next = strchr(id, ':'))) *id_next++ = '\0';
2569                                         else id_next = prop_names_end;
2570                                         uiItemR(row, itemptr, id, 0, NULL, ICON_NONE);
2571                                         id = id_next;
2572                                 }
2573                                 MEM_freeN(prop_names);
2574                         }
2575                 }
2576         }
2577
2578         else
2579                 uiItemL(sub, name, icon);  /* fails, backdrop LISTROW... */
2580
2581         /* free name */
2582         if (namebuf) {
2583                 MEM_freeN(namebuf);
2584         }
2585 }
2586
2587 void uiTemplateList(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, PointerRNA *activeptr,
2588                     const char *activepropname, const char *prop_list, int rows, int maxrows, int listtype)
2589 {
2590         PropertyRNA *prop = NULL, *activeprop;
2591         PropertyType type, activetype;
2592         StructRNA *ptype;
2593         uiLayout *box, *row, *col;
2594         uiBlock *block;
2595         uiBut *but;
2596         Panel *pa;
2597         const char *name;
2598         char numstr[32];
2599         int rnaicon = 0, icon = 0, i = 0, activei = 0, len = 0, items, found, min, max;
2600
2601         /* validate arguments */
2602         block = uiLayoutGetBlock(layout);
2603         pa = block->panel;
2604
2605         if (!pa) {
2606                 RNA_warning("Only works inside a panel");
2607                 return;
2608         }
2609
2610         if (!activeptr->data)
2611                 return;
2612         
2613         if (ptr->data) {
2614                 prop = RNA_struct_find_property(ptr, propname);
2615                 if (!prop) {
2616                         RNA_warning("Property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2617                         return;
2618                 }
2619         }
2620
2621         activeprop = RNA_struct_find_property(activeptr, activepropname);
2622         if (!activeprop) {
2623                 RNA_warning("Property not found: %s.%s", RNA_struct_identifier(ptr->type), activepropname);
2624                 return;
2625         }
2626
2627         if (prop) {
2628                 type = RNA_property_type(prop);
2629                 if (type != PROP_COLLECTION) {
2630                         RNA_warning("uiExpected collection property");
2631                         return;
2632                 }
2633         }
2634
2635         activetype = RNA_property_type(activeprop);
2636         if (activetype != PROP_INT) {
2637                 RNA_warning("Expected integer property");
2638                 return;
2639         }
2640
2641         /* get icon */
2642         if (ptr->data && prop) {
2643                 ptype = RNA_property_pointer_type(ptr, prop);
2644                 rnaicon = RNA_struct_ui_icon(ptype);
2645         }
2646
2647         /* get active data */
2648         activei = RNA_property_int_get(activeptr, activeprop);
2649
2650         if (listtype == 'i') {
2651                 box = uiLayoutListBox(layout, ptr, prop, activeptr, activeprop);
2652                 col = uiLayoutColumn(box, TRUE);
2653                 row = uiLayoutRow(col, FALSE);
2654
2655                 if (ptr->data && prop) {
2656                         /* create list items */
2657                         RNA_PROP_BEGIN (ptr, itemptr, prop)
2658                         {
2659                                 /* create button */
2660                                 if (!(i % 9))
2661                                         row = uiLayoutRow(col, FALSE);
2662
2663                                 icon = list_item_icon_get(C, &itemptr, rnaicon, 1);
2664                                 but = uiDefIconButR_prop(block, LISTROW, 0, icon, 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, activeptr,
2665                                                          activeprop, 0, 0, i, 0, 0, "");
2666                                 uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2667                                 
2668
2669                                 i++;
2670                         }
2671                         RNA_PROP_END;
2672                 }
2673         }
2674         else if (listtype == 'c') {
2675                 /* compact layout */
2676
2677                 row = uiLayoutRow(layout, TRUE);
2678
2679                 if (ptr->data && prop) {
2680                         /* create list items */
2681                         RNA_PROP_BEGIN (ptr, itemptr, prop)
2682                         {
2683                                 found = (activei == i);
2684
2685                                 if (found) {
2686                                         /* create button */
2687                                         name = RNA_struct_name_get_alloc(&itemptr, NULL, 0, NULL);
2688                                         icon = list_item_icon_get(C, &itemptr, rnaicon, 0);
2689                                         uiItemL(row, (name) ? name : "", icon);
2690
2691                                         if (name) {
2692                                                 MEM_freeN((void *)name);
2693                                         }
2694                                 }
2695
2696                                 i++;
2697                         }
2698                         RNA_PROP_END;
2699                 }
2700
2701                 /* if not found, add in dummy button */
2702                 if (i == 0)
2703                         uiItemL(row, "", ICON_NONE);
2704
2705                 /* next/prev button */
2706                 BLI_snprintf(numstr, sizeof(numstr), "%d :", i);
2707                 but = uiDefIconTextButR_prop(block, NUM, 0, 0, numstr, 0, 0, UI_UNIT_X * 5, UI_UNIT_Y, activeptr,
2708                                              activeprop, 0, 0, 0, 0, 0, "");
2709                 if (i == 0)
2710                         uiButSetFlag(but, UI_BUT_DISABLED);
2711         }
2712         else {
2713                 /* default rows */
2714                 if (rows == 0)
2715                         rows = 5;
2716                 if (maxrows == 0)
2717                         maxrows = 5;
2718                 if (pa->list_grip_size != 0)
2719                         rows = pa->list_grip_size;
2720
2721                 /* layout */
2722                 box = uiLayoutListBox(layout, ptr, prop, activeptr, activeprop);
2723                 row = uiLayoutRow(box, FALSE);
2724                 col = uiLayoutColumn(row, TRUE);
2725
2726                 /* init numbers */
2727                 RNA_property_int_range(activeptr, activeprop, &min, &max);
2728
2729                 if (prop)
2730                         len = RNA_property_collection_length(ptr, prop);
2731                 items = CLAMPIS(len, rows, MAX2(rows, maxrows));
2732
2733                 /* if list length changes and active is out of view, scroll to it */
2734                 if (pa->list_last_len != len)
2735                         if ((activei < pa->list_scroll || activei >= pa->list_scroll + items))
2736                                 pa->list_scroll = activei;
2737
2738                 pa->list_scroll = MIN2(pa->list_scroll, len - items);
2739                 pa->list_scroll = MAX2(pa->list_scroll, 0);
2740                 pa->list_size = items;
2741                 pa->list_last_len = len;
2742
2743                 if (ptr->data && prop) {
2744                         /* create list items */
2745                         RNA_PROP_BEGIN (ptr, itemptr, prop)
2746                         {
2747                                 if (i >= pa->list_scroll && i < pa->list_scroll + items)
2748                                         list_item_row(C, col, ptr, &itemptr, i, rnaicon, activeptr, activeprop, prop_list);
2749
2750                                 i++;
2751                         }
2752                         RNA_PROP_END;
2753                 }
2754
2755                 /* add dummy buttons to fill space */
2756                 while (i < pa->list_scroll + items) {
2757                         if (i >= pa->list_scroll)
2758                                 uiItemL(col, "", ICON_NONE);
2759                         i++;
2760                 }
2761
2762                 /* add scrollbar */
2763                 if (len > items) {
2764                         col = uiLayoutColumn(row, FALSE);
2765                         uiDefButI(block, SCROLL, 0, "", 0, 0, UI_UNIT_X * 0.75, UI_UNIT_Y * items, &pa->list_scroll,
2766                                   0, len - items, items, 0, "");
2767                 }
2768         }
2769 }
2770
2771 /************************* Operator Search Template **************************/
2772
2773 static void operator_call_cb(bContext *C, void *UNUSED(arg1), void *arg2)
2774 {
2775         wmOperatorType *ot = arg2;
2776         
2777         if (ot)
2778                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
2779 }
2780
2781 static void operator_search_cb(const bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
2782 {
2783         GHashIterator *iter = WM_operatortype_iter();
2784
2785         for (; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
2786                 wmOperatorType *ot = BLI_ghashIterator_getValue(iter);
2787
2788                 if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0)
2789                         continue;
2790
2791                 if (BLI_strcasestr(ot->name, str)) {
2792                         if (WM_operator_poll((bContext *)C, ot)) {
2793                                 char name[256];
2794                                 int len = strlen(ot->name);
2795                                 
2796                                 /* display name for menu, can hold hotkey */
2797                                 BLI_strncpy(name, ot->name, sizeof(name));
2798                                 
2799                                 /* check for hotkey */
2800                                 if (len < sizeof(name) - 6) {
2801                                         if (WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, TRUE,
2802                                                                          &name[len + 1], sizeof(name) - len - 1))
2803                                         {
2804                                                 name[len] = '|';
2805                                         }
2806                                 }
2807                                 
2808                                 if (0 == uiSearchItemAdd(items, name, ot, 0))
2809                                         break;
2810                         }
2811                 }
2812         }
2813         BLI_ghashIterator_free(iter);
2814 }
2815
2816 void uiOperatorSearch_But(uiBut *but)
2817 {
2818         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
2819 }
2820
2821 void uiTemplateOperatorSearch(uiLayout *layout)
2822 {
2823         uiBlock *block;
2824         uiBut *but;
2825         static char search[256] = "";
2826                 
2827         block = uiLayoutGetBlock(layout);
2828         uiBlockSetCurLayout(block, layout);
2829
2830         but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, 0, 0, "");
2831         uiOperatorSearch_But(but);
2832 }
2833
2834 /************************* Running Jobs Template **************************/
2835
2836 #define B_STOPRENDER    1
2837 #define B_STOPCAST      2
2838 #define B_STOPANIM      3
2839 #define B_STOPCOMPO     4
2840 #define B_STOPSEQ       5
2841 #define B_STOPCLIP      6
2842 #define B_STOPOTHER     7
2843
2844 static void do_running_jobs(bContext *C, void *UNUSED(arg), int event)
2845 {
2846         switch (event) {
2847                 case B_STOPRENDER:
2848                         G.is_break = TRUE;
2849                         break;
2850                 case B_STOPCAST:
2851                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_screen(C), NULL);
2852                         break;
2853                 case B_STOPANIM:
2854                         WM_operator_name_call(C, "SCREEN_OT_animation_play", WM_OP_INVOKE_SCREEN, NULL);
2855                         break;
2856                 case B_STOPCOMPO:
2857                         WM_jobs_stop(CTX_wm_manager(C), CTX_data_scene(C), NULL);
2858                         break;
2859                 case B_STOPSEQ:
2860                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2861                         break;
2862                 case B_STOPCLIP:
2863                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2864                         break;
2865                 case B_STOPOTHER:
2866                         G.is_break = TRUE;
2867                         break;
2868         }
2869 }
2870
2871 void uiTemplateRunningJobs(uiLayout *layout, bContext *C)
2872 {
2873         bScreen *screen = CTX_wm_screen(C);
2874         wmWindowManager *wm = CTX_wm_manager(C);
2875         ScrArea *sa = CTX_wm_area(C);
2876         uiBlock *block;
2877         void *owner = NULL;
2878         int handle_event;
2879         
2880         block = uiLayoutGetBlock(layout);
2881         uiBlockSetCurLayout(block, layout);
2882
2883         uiBlockSetHandleFunc(block, do_running_jobs, NULL);
2884
2885         if (sa->spacetype == SPACE_SEQ) {
2886                 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2887                         owner = sa;
2888                 handle_event = B_STOPSEQ;
2889         }
2890         else if (sa->spacetype == SPACE_CLIP) {
2891                 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2892                         owner = sa;
2893                 handle_event = B_STOPCLIP;
2894         }
2895         else {
2896                 Scene *scene;
2897                 /* another scene can be rendering too, for example via compositor */
2898                 for (scene = CTX_data_main(C)->scene.first; scene; scene = scene->id.next) {
2899                         if (WM_jobs_test(wm, scene, WM_JOB_TYPE_RENDER)) {
2900                                 handle_event = B_STOPRENDER;
2901                                 break;
2902                         }
2903                         else if (WM_jobs_test(wm, scene, WM_JOB_TYPE_COMPOSITE)) {
2904                                 handle_event = B_STOPCOMPO;
2905                                 break;
2906                         }
2907                         else if (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY)) {
2908                                 handle_event = B_STOPOTHER;
2909                                 break;
2910                         }
2911                 }
2912                 owner = scene;
2913         }
2914
2915         if (owner) {
2916                 uiLayout *ui_abs;
2917                 
2918                 ui_abs = uiLayoutAbsolute(layout, FALSE);
2919                 (void)ui_abs;  /* UNUSED */
2920                 
2921                 uiDefIconBut(block, BUT, handle_event, ICON_PANEL_CLOSE, 0, UI_UNIT_Y * 0.1, UI_UNIT_X * 0.8, UI_UNIT_Y * 0.8,
2922                              NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop this job"));
2923                 uiDefBut(block, PROGRESSBAR, 0, WM_jobs_name(wm, owner), 
2924                          UI_UNIT_X, 0, 100, UI_UNIT_Y, NULL, 0.0f, 0.0f, WM_jobs_progress(wm, owner), 0, TIP_("Progress"));
2925                 
2926                 uiLayoutRow(layout, FALSE);
2927         }
2928         if (WM_jobs_test(wm, screen, WM_JOB_TYPE_SCREENCAST))
2929                 uiDefIconTextBut(block, BUT, B_STOPCAST, ICON_CANCEL, IFACE_("Capture"), 0, 0, 85, UI_UNIT_Y,
2930                                  NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop screencast"));
2931         if (screen->animtimer)
2932                 uiDefIconTextBut(block, BUT, B_STOPANIM, ICON_CANCEL, IFACE_("Anim Player"), 0, 0, 100, UI_UNIT_Y,
2933                                  NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop animation playback"));
2934 }
2935
2936 /************************* Reports for Last Operator Template **************************/
2937
2938 void uiTemplateReportsBanner(uiLayout *layout, bContext *C)
2939 {
2940         ReportList *reports = CTX_wm_reports(C);
2941         Report *report = BKE_reports_last_displayable(reports);
2942         ReportTimerInfo *rti;
2943         
2944         uiLayout *ui_abs;
2945         uiBlock *block;
2946         uiBut *but;
2947         uiStyle *style = UI_GetStyle();
2948         int width;
2949         int icon = 0;
2950         
2951         /* if the report display has timed out, don't show */
2952         if (!reports->reporttimer) return;
2953         
2954         rti = (ReportTimerInfo *)reports->reporttimer->customdata;
2955         
2956         if (!rti || rti->widthfac == 0.0f || !report) return;
2957         
2958         ui_abs = uiLayoutAbsolute(layout, FALSE);
2959         block = uiLayoutGetBlock(ui_abs);
2960         
2961         width = BLF_width(style->widget.uifont_id, report->message);
2962         width = min_ii((int)(rti->widthfac * width), width);
2963         width = max_ii(width, 10);
2964         
2965         /* make a box around the report to make it stand out */
2966         uiBlockBeginAlign(block);
2967         but = uiDefBut(block, ROUNDBOX, 0, "", 0, 0, UI_UNIT_X + 10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "");
2968         /* set the report's bg color in but->col - ROUNDBOX feature */
2969         rgb_float_to_uchar(but->col, rti->col);
2970         but->col[3] = 255;
2971
2972         but = uiDefBut(block, ROUNDBOX, 0, "", UI_UNIT_X + 10, 0, UI_UNIT_X + width, UI_UNIT_Y,
2973                        NULL, 0.0f, 0.0f, 0, 0, "");
2974         but->col[0] = but->col[1] = but->col[2] = FTOCHAR(rti->grayscale);