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