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