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