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