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