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