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