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