Pack UI:
[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                 x = (a % 8) * UI_UNIT_X * 6;
1589                 y = (a / 8) * UI_UNIT_X * 6;
1590                 
1591                 icon = item[a].icon;
1592                 but = uiDefIconButR_prop(block, ROW, 0, icon, x, y, UI_UNIT_X * 6, UI_UNIT_Y * 6, &cb.ptr, cb.prop, -1, 0, icon, -1, -1, NULL);
1593                 uiButSetFlag(but, UI_HAS_ICON | UI_ICON_PREVIEW);
1594         }
1595
1596         uiBoundsBlock(block, 0.3f * U.widget_unit);
1597         uiBlockSetDirection(block, UI_TOP);
1598         uiEndBlock(C, block);
1599                 
1600         if (free) {
1601                 MEM_freeN(item);
1602         }
1603         
1604         return block;
1605 }
1606
1607 void uiTemplateIconView(uiLayout *layout, PointerRNA *ptr, const char *propname)
1608 {
1609         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1610         RNAUpdateCb *cb;
1611         uiBlock *block;
1612         uiBut *but;
1613 //      rctf rect;  /* UNUSED */
1614         int icon;
1615         
1616         if (!prop || RNA_property_type(prop) != PROP_ENUM)
1617                 return;
1618         
1619         icon = RNA_property_enum_get(ptr, prop);
1620         
1621         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1622         cb->ptr = *ptr;
1623         cb->prop = prop;
1624         
1625 //      rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1626 //      rect.ymin = 0; rect.ymax = 10.0f * UI_UNIT_X;
1627         
1628         block = uiLayoutAbsoluteBlock(layout);
1629
1630         but = uiDefBlockButN(block, icon_view_menu, MEM_dupallocN(cb), "", 0, 0, UI_UNIT_X * 6, UI_UNIT_Y * 6, "");
1631
1632         
1633 //      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);
1634         
1635         but->icon = icon;
1636         uiButSetFlag(but, UI_HAS_ICON | UI_ICON_PREVIEW);
1637         
1638         uiButSetNFunc(but, rna_update_cb, MEM_dupallocN(cb), NULL);
1639         
1640         MEM_freeN(cb);
1641 }
1642
1643 /********************* Histogram Template ************************/
1644
1645 void uiTemplateHistogram(uiLayout *layout, PointerRNA *ptr, const char *propname)
1646 {
1647         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1648         PointerRNA cptr;
1649         RNAUpdateCb *cb;
1650         uiBlock *block;
1651         uiBut *bt;
1652         Histogram *hist;
1653         rctf rect;
1654         
1655         if (!prop || RNA_property_type(prop) != PROP_POINTER)
1656                 return;
1657         
1658         cptr = RNA_property_pointer_get(ptr, prop);
1659         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Histogram))
1660                 return;
1661         
1662         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1663         cb->ptr = *ptr;
1664         cb->prop = prop;
1665         
1666         rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1667         rect.ymin = 0; rect.ymax = 9.5f * UI_UNIT_Y;
1668         
1669         block = uiLayoutAbsoluteBlock(layout);
1670         //colorband_buttons_layout(layout, block, cptr.data, &rect, !expand, cb);
1671
1672         hist = (Histogram *)cptr.data;
1673
1674         hist->height = (hist->height <= UI_UNIT_Y) ? UI_UNIT_Y : hist->height;
1675
1676         bt = uiDefBut(block, HISTOGRAM, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect), UI_DPI_FAC * hist->height,
1677                       hist, 0, 0, 0, 0, "");
1678
1679         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1680
1681         MEM_freeN(cb);
1682 }
1683
1684 /********************* Waveform Template ************************/
1685
1686 void uiTemplateWaveform(uiLayout *layout, PointerRNA *ptr, const char *propname)
1687 {
1688         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1689         PointerRNA cptr;
1690         RNAUpdateCb *cb;
1691         uiBlock *block;
1692         uiBut *bt;
1693         Scopes *scopes;
1694         rctf rect;
1695         
1696         if (!prop || RNA_property_type(prop) != PROP_POINTER)
1697                 return;
1698         
1699         cptr = RNA_property_pointer_get(ptr, prop);
1700         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Scopes))
1701                 return;
1702         scopes = (Scopes *)cptr.data;
1703         
1704         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1705         cb->ptr = *ptr;
1706         cb->prop = prop;
1707         
1708         rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1709         rect.ymin = 0; rect.ymax = 9.5f * UI_UNIT_Y;
1710         
1711         block = uiLayoutAbsoluteBlock(layout);
1712         
1713         scopes->wavefrm_height = (scopes->wavefrm_height <= UI_UNIT_Y) ? UI_UNIT_Y : scopes->wavefrm_height;
1714
1715         bt = uiDefBut(block, WAVEFORM, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect), UI_DPI_FAC * scopes->wavefrm_height,
1716                       scopes, 0, 0, 0, 0, "");
1717         (void)bt;  /* UNUSED */
1718         
1719         MEM_freeN(cb);
1720 }
1721
1722 /********************* Vectorscope Template ************************/
1723
1724 void uiTemplateVectorscope(uiLayout *layout, PointerRNA *ptr, const char *propname)
1725 {
1726         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1727         PointerRNA cptr;
1728         RNAUpdateCb *cb;
1729         uiBlock *block;
1730         uiBut *bt;
1731         Scopes *scopes;
1732         rctf rect;
1733         
1734         if (!prop || RNA_property_type(prop) != PROP_POINTER)
1735                 return;
1736         
1737         cptr = RNA_property_pointer_get(ptr, prop);
1738         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Scopes))
1739                 return;
1740         scopes = (Scopes *)cptr.data;
1741
1742         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1743         cb->ptr = *ptr;
1744         cb->prop = prop;
1745         
1746         rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1747         rect.ymin = 0; rect.ymax = 9.5f * UI_UNIT_Y;
1748         
1749         block = uiLayoutAbsoluteBlock(layout);
1750
1751         scopes->vecscope_height = (scopes->vecscope_height <= UI_UNIT_Y) ? UI_UNIT_Y : scopes->vecscope_height;
1752         
1753         bt = uiDefBut(block, VECTORSCOPE, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect),
1754                       UI_DPI_FAC * scopes->vecscope_height, scopes, 0, 0, 0, 0, "");
1755         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1756         
1757         MEM_freeN(cb);
1758 }
1759
1760 /********************* CurveMapping Template ************************/
1761
1762
1763 static void curvemap_buttons_zoom_in(bContext *C, void *cumap_v, void *UNUSED(arg))
1764 {
1765         CurveMapping *cumap = cumap_v;
1766         float d;
1767
1768         /* we allow 20 times zoom */
1769         if (BLI_rctf_size_x(&cumap->curr) > 0.04f * BLI_rctf_size_x(&cumap->clipr)) {
1770                 d = 0.1154f * BLI_rctf_size_x(&cumap->curr);
1771                 cumap->curr.xmin += d;
1772                 cumap->curr.xmax -= d;
1773                 d = 0.1154f * BLI_rctf_size_y(&cumap->curr);
1774                 cumap->curr.ymin += d;
1775                 cumap->curr.ymax -= d;
1776         }
1777
1778         ED_region_tag_redraw(CTX_wm_region(C));
1779 }
1780
1781 static void curvemap_buttons_zoom_out(bContext *C, void *cumap_v, void *UNUSED(unused))
1782 {
1783         CurveMapping *cumap = cumap_v;
1784         float d, d1;
1785
1786         /* we allow 20 times zoom, but don't view outside clip */
1787         if (BLI_rctf_size_x(&cumap->curr) < 20.0f * BLI_rctf_size_x(&cumap->clipr)) {
1788                 d = d1 = 0.15f * BLI_rctf_size_x(&cumap->curr);
1789
1790                 if (cumap->flag & CUMA_DO_CLIP) 
1791                         if (cumap->curr.xmin - d < cumap->clipr.xmin)
1792                                 d1 = cumap->curr.xmin - cumap->clipr.xmin;
1793                 cumap->curr.xmin -= d1;
1794
1795                 d1 = d;
1796                 if (cumap->flag & CUMA_DO_CLIP) 
1797                         if (cumap->curr.xmax + d > cumap->clipr.xmax)
1798                                 d1 = -cumap->curr.xmax + cumap->clipr.xmax;
1799                 cumap->curr.xmax += d1;
1800
1801                 d = d1 = 0.15f * BLI_rctf_size_y(&cumap->curr);
1802
1803                 if (cumap->flag & CUMA_DO_CLIP) 
1804                         if (cumap->curr.ymin - d < cumap->clipr.ymin)
1805                                 d1 = cumap->curr.ymin - cumap->clipr.ymin;
1806                 cumap->curr.ymin -= d1;
1807
1808                 d1 = d;
1809                 if (cumap->flag & CUMA_DO_CLIP) 
1810                         if (cumap->curr.ymax + d > cumap->clipr.ymax)
1811                                 d1 = -cumap->curr.ymax + cumap->clipr.ymax;
1812                 cumap->curr.ymax += d1;
1813         }
1814
1815         ED_region_tag_redraw(CTX_wm_region(C));
1816 }
1817
1818 static void curvemap_buttons_setclip(bContext *UNUSED(C), void *cumap_v, void *UNUSED(arg))
1819 {
1820         CurveMapping *cumap = cumap_v;
1821
1822         curvemapping_changed(cumap, FALSE);
1823 }       
1824
1825 static void curvemap_buttons_delete(bContext *C, void *cb_v, void *cumap_v)
1826 {
1827         CurveMapping *cumap = cumap_v;
1828
1829         curvemap_remove(cumap->cm + cumap->cur, SELECT);
1830         curvemapping_changed(cumap, FALSE);
1831
1832         rna_update_cb(C, cb_v, NULL);
1833 }
1834
1835 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
1836 static uiBlock *curvemap_clipping_func(bContext *C, ARegion *ar, void *cumap_v)
1837 {
1838         CurveMapping *cumap = cumap_v;
1839         uiBlock *block;
1840         uiBut *bt;
1841         float width = 8 * UI_UNIT_X;
1842
1843         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1844
1845         /* use this for a fake extra empy space around the buttons */
1846         uiDefBut(block, LABEL, 0, "",           -4, 16, width + 8, 6 * UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
1847
1848         bt = uiDefButBitI(block, TOG, CUMA_DO_CLIP, 1, "Use Clipping",
1849                           0, 5 * UI_UNIT_Y, width, UI_UNIT_Y, &cumap->flag, 0.0, 0.0, 10, 0, "");
1850         uiButSetFunc(bt, curvemap_buttons_setclip, cumap, NULL);
1851
1852         uiBlockBeginAlign(block);
1853         uiDefButF(block, NUM, 0, IFACE_("Min X "),   0, 4 * UI_UNIT_Y, width, UI_UNIT_Y,
1854                   &cumap->clipr.xmin, -100.0, cumap->clipr.xmax, 10, 0, "");
1855         uiDefButF(block, NUM, 0, IFACE_("Min Y "),   0, 3 * UI_UNIT_Y, width, UI_UNIT_Y,
1856                   &cumap->clipr.ymin, -100.0, cumap->clipr.ymax, 10, 0, "");
1857         uiDefButF(block, NUM, 0, IFACE_("Max X "),   0, 2 * UI_UNIT_Y, width, UI_UNIT_Y,
1858                   &cumap->clipr.xmax, cumap->clipr.xmin, 100.0, 10, 0, "");
1859         uiDefButF(block, NUM, 0, IFACE_("Max Y "),   0, UI_UNIT_Y, width, UI_UNIT_Y,
1860                   &cumap->clipr.ymax, cumap->clipr.ymin, 100.0, 10, 0, "");
1861
1862         uiBlockSetDirection(block, UI_RIGHT);
1863
1864         uiEndBlock(C, block);
1865         return block;
1866 }
1867
1868 static void curvemap_tools_dofunc(bContext *C, void *cumap_v, int event)
1869 {
1870         CurveMapping *cumap = cumap_v;
1871         CurveMap *cuma = cumap->cm + cumap->cur;
1872
1873         switch (event) {
1874                 case 0: /* reset */
1875                         curvemap_reset(cuma, &cumap->clipr, cumap->preset, CURVEMAP_SLOPE_POSITIVE);
1876                         curvemapping_changed(cumap, FALSE);
1877                         break;
1878                 case 1:
1879                         cumap->curr = cumap->clipr;
1880                         break;
1881                 case 2: /* set vector */
1882                         curvemap_sethandle(cuma, 1);
1883                         curvemapping_changed(cumap, FALSE);
1884                         break;
1885                 case 3: /* set auto */
1886                         curvemap_sethandle(cuma, 0);
1887                         curvemapping_changed(cumap, FALSE);
1888                         break;
1889                 case 4: /* extend horiz */
1890                         cuma->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1891                         curvemapping_changed(cumap, FALSE);
1892                         break;
1893                 case 5: /* extend extrapolate */
1894                         cuma->flag |= CUMA_EXTEND_EXTRAPOLATE;
1895                         curvemapping_changed(cumap, FALSE);
1896                         break;
1897         }
1898         ED_region_tag_redraw(CTX_wm_region(C));
1899 }
1900
1901 static uiBlock *curvemap_tools_func(bContext *C, ARegion *ar, void *cumap_v)
1902 {
1903         uiBlock *block;
1904         short yco = 0, menuwidth = 10 * UI_UNIT_X;
1905
1906         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1907         uiBlockSetButmFunc(block, curvemap_tools_dofunc, cumap_v);
1908
1909         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset View"),          0, yco -= UI_UNIT_Y,
1910                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 1, "");
1911         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Vector Handle"),       0, yco -= UI_UNIT_Y,
1912                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 2, "");
1913         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Auto Handle"),         0, yco -= UI_UNIT_Y,
1914                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 3, "");
1915         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Extend Horizontal"),   0, yco -= UI_UNIT_Y,
1916                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 4, "");
1917         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Extend Extrapolated"), 0, yco -= UI_UNIT_Y,
1918                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 5, "");
1919         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset Curve"),         0, yco -= UI_UNIT_Y,
1920                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1921
1922         uiBlockSetDirection(block, UI_RIGHT);
1923         uiTextBoundsBlock(block, 50);
1924
1925         uiEndBlock(C, block);
1926         return block;
1927 }
1928
1929 static uiBlock *curvemap_brush_tools_func(bContext *C, ARegion *ar, void *cumap_v)
1930 {
1931         uiBlock *block;
1932         short yco = 0, menuwidth = 10 * UI_UNIT_X;
1933
1934         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1935         uiBlockSetButmFunc(block, curvemap_tools_dofunc, cumap_v);
1936
1937         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset View"),    0, yco -= UI_UNIT_Y,
1938                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 1, "");
1939         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Vector Handle"), 0, yco -= UI_UNIT_Y,
1940                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 2, "");
1941         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Auto Handle"),   0, yco -= UI_UNIT_Y,
1942                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 3, "");
1943         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset Curve"),   0, yco -= UI_UNIT_Y,
1944                          menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
1945
1946         uiBlockSetDirection(block, UI_RIGHT);
1947         uiTextBoundsBlock(block, 50);
1948
1949         uiEndBlock(C, block);
1950         return block;
1951 }
1952
1953 static void curvemap_buttons_redraw(bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
1954 {
1955         ED_region_tag_redraw(CTX_wm_region(C));
1956 }
1957
1958 static void curvemap_buttons_update(bContext *C, void *arg1_v, void *cumap_v)
1959 {
1960         CurveMapping *cumap = cumap_v;
1961         curvemapping_changed(cumap, TRUE);
1962         rna_update_cb(C, arg1_v, NULL);
1963 }
1964
1965 static void curvemap_buttons_reset(bContext *C, void *cb_v, void *cumap_v)
1966 {
1967         CurveMapping *cumap = cumap_v;
1968         int a;
1969         
1970         cumap->preset = CURVE_PRESET_LINE;
1971         for (a = 0; a < CM_TOT; a++)
1972                 curvemap_reset(cumap->cm + a, &cumap->clipr, cumap->preset, CURVEMAP_SLOPE_POSITIVE);
1973         
1974         cumap->black[0] = cumap->black[1] = cumap->black[2] = 0.0f;
1975         cumap->white[0] = cumap->white[1] = cumap->white[2] = 1.0f;
1976         curvemapping_set_black_white(cumap, NULL, NULL);
1977         
1978         curvemapping_changed(cumap, FALSE);
1979
1980         rna_update_cb(C, cb_v, NULL);
1981 }
1982
1983 /* still unsure how this call evolves... we use labeltype for defining what curve-channels to show */
1984 static void curvemap_buttons_layout(uiLayout *layout, PointerRNA *ptr, char labeltype, int levels,
1985                                     int brush, RNAUpdateCb *cb)
1986 {
1987         CurveMapping *cumap = ptr->data;
1988         CurveMap *cm = &cumap->cm[cumap->cur];
1989         CurveMapPoint *cmp = NULL;
1990         uiLayout *row, *sub, *split;
1991         uiBlock *block;
1992         uiBut *bt;
1993         float dx = UI_UNIT_X;
1994         int icon, size;
1995         int bg = -1, i;
1996
1997         block = uiLayoutGetBlock(layout);
1998
1999         /* curve chooser */
2000         row = uiLayoutRow(layout, FALSE);
2001
2002         if (labeltype == 'v') {
2003                 /* vector */
2004                 sub = uiLayoutRow(row, TRUE);
2005                 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
2006
2007                 if (cumap->cm[0].curve) {
2008                         bt = uiDefButI(block, ROW, 0, "X", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
2009                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2010                 }
2011                 if (cumap->cm[1].curve) {
2012                         bt = uiDefButI(block, ROW, 0, "Y", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
2013                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2014                 }
2015                 if (cumap->cm[2].curve) {
2016                         bt = uiDefButI(block, ROW, 0, "Z", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
2017                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2018                 }
2019         }
2020         else if (labeltype == 'c') {
2021                 /* color */
2022                 sub = uiLayoutRow(row, TRUE);
2023                 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
2024
2025                 if (cumap->cm[3].curve) {
2026                         bt = uiDefButI(block, ROW, 0, "C", 0, 0, dx, dx, &cumap->cur, 0.0, 3.0, 0.0, 0.0, "");
2027                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2028                 }
2029                 if (cumap->cm[0].curve) {
2030                         bt = uiDefButI(block, ROW, 0, "R", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
2031                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2032                 }
2033                 if (cumap->cm[1].curve) {
2034                         bt = uiDefButI(block, ROW, 0, "G", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
2035                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2036                 }
2037                 if (cumap->cm[2].curve) {
2038                         bt = uiDefButI(block, ROW, 0, "B", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
2039                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2040                 }
2041         }
2042         else if (labeltype == 'h') {
2043                 /* HSV */
2044                 sub = uiLayoutRow(row, TRUE);
2045                 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
2046                 
2047                 if (cumap->cm[0].curve) {
2048                         bt = uiDefButI(block, ROW, 0, "H", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
2049                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2050                 }
2051                 if (cumap->cm[1].curve) {
2052                         bt = uiDefButI(block, ROW, 0, "S", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
2053                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2054                 }
2055                 if (cumap->cm[2].curve) {
2056                         bt = uiDefButI(block, ROW, 0, "V", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
2057                         uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2058                 }
2059         }
2060         else
2061                 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_RIGHT);
2062         
2063         if (labeltype == 'h')
2064                 bg = UI_GRAD_H;
2065
2066         /* operation buttons */
2067         sub = uiLayoutRow(row, TRUE);
2068
2069         uiBlockSetEmboss(block, UI_EMBOSSN);
2070
2071         bt = uiDefIconBut(block, BUT, 0, ICON_ZOOMIN, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Zoom in"));
2072         uiButSetFunc(bt, curvemap_buttons_zoom_in, cumap, NULL);
2073
2074         bt = uiDefIconBut(block, BUT, 0, ICON_ZOOMOUT, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Zoom out"));
2075         uiButSetFunc(bt, curvemap_buttons_zoom_out, cumap, NULL);
2076
2077         if (brush)
2078                 bt = uiDefIconBlockBut(block, curvemap_brush_tools_func, cumap, 0, ICON_MODIFIER, 0, 0, dx, dx, TIP_("Tools"));
2079         else
2080                 bt = uiDefIconBlockBut(block, curvemap_tools_func, cumap, 0, ICON_MODIFIER, 0, 0, dx, dx, TIP_("Tools"));
2081
2082         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
2083
2084         icon = (cumap->flag & CUMA_DO_CLIP) ? ICON_CLIPUV_HLT : ICON_CLIPUV_DEHLT;
2085         bt = uiDefIconBlockBut(block, curvemap_clipping_func, cumap, 0, icon, 0, 0, dx, dx, TIP_("Clipping Options"));
2086         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
2087
2088         bt = uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Delete points"));
2089         uiButSetNFunc(bt, curvemap_buttons_delete, MEM_dupallocN(cb), cumap);
2090
2091         uiBlockSetEmboss(block, UI_EMBOSS);
2092
2093         uiBlockSetNFunc(block, rna_update_cb, MEM_dupallocN(cb), NULL);
2094
2095         /* curve itself */
2096         size = uiLayoutGetWidth(layout);
2097         row = uiLayoutRow(layout, FALSE);
2098         uiDefBut(block, BUT_CURVE, 0, "", 0, 0, size, MIN2(size, 10.0f * UI_UNIT_X), cumap, 0.0f, 1.0f, bg, 0, "");
2099
2100         /* sliders for selected point */
2101         for (i = 0; i < cm->totpoint; i++) {
2102                 if (cm->curve[i].flag & CUMA_SELECT) {
2103                         cmp = &cm->curve[i];
2104                         break;
2105                 }
2106         }
2107
2108         if (cmp) {
2109                 rctf bounds;
2110
2111                 if (cumap->flag & CUMA_DO_CLIP) {
2112                         bounds = cumap->clipr;
2113                 }
2114                 else {
2115                         bounds.xmin = bounds.ymin = -1000.0;
2116                         bounds.xmax = bounds.ymax =  1000.0;
2117                 }
2118
2119                 uiLayoutRow(layout, TRUE);
2120                 uiBlockSetNFunc(block, curvemap_buttons_update, MEM_dupallocN(cb), cumap);
2121                 bt = uiDefButF(block, NUM, 0, "X", 0, 2 * UI_UNIT_Y, UI_UNIT_X * 10, UI_UNIT_Y,
2122                                &cmp->x, bounds.xmin, bounds.xmax, 1, 5, "");
2123                 bt = uiDefButF(block, NUM, 0, "Y", 0, 1 * UI_UNIT_Y, UI_UNIT_X * 10, UI_UNIT_Y,
2124                                &cmp->y, bounds.ymin, bounds.ymax, 1, 5, "");
2125         }
2126
2127         /* black/white levels */
2128         if (levels) {
2129                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2130                 uiItemR(uiLayoutColumn(split, FALSE), ptr, "black_level", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2131                 uiItemR(uiLayoutColumn(split, FALSE), ptr, "white_level", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2132
2133                 uiLayoutRow(layout, FALSE);
2134                 bt = uiDefBut(block, BUT, 0, IFACE_("Reset"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0,
2135                               TIP_("Reset Black/White point and curves"));
2136                 uiButSetNFunc(bt, curvemap_buttons_reset, MEM_dupallocN(cb), cumap);
2137         }
2138
2139         uiBlockSetNFunc(block, NULL, NULL, NULL);
2140 }
2141
2142 void uiTemplateCurveMapping(uiLayout *layout, PointerRNA *ptr, const char *propname, int type, int levels, int brush)
2143 {
2144         RNAUpdateCb *cb;
2145         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2146         PointerRNA cptr;
2147
2148         if (!prop) {
2149                 RNA_warning("curve property not found: %s.%s",
2150                             RNA_struct_identifier(ptr->type), propname);
2151                 return;
2152         }
2153
2154         if (RNA_property_type(prop) != PROP_POINTER) {
2155                 RNA_warning("curve is not a pointer: %s.%s",
2156                             RNA_struct_identifier(ptr->type), propname);
2157                 return;
2158         }
2159
2160         cptr = RNA_property_pointer_get(ptr, prop);
2161         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_CurveMapping))
2162                 return;
2163
2164         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
2165         cb->ptr = *ptr;
2166         cb->prop = prop;
2167
2168         curvemap_buttons_layout(layout, &cptr, type, levels, brush, cb);
2169
2170         MEM_freeN(cb);
2171 }
2172
2173 /********************* ColorPicker Template ************************/
2174
2175 #define WHEEL_SIZE  (5 * U.widget_unit)
2176
2177 /* This template now follows User Preference for type - name is not correct anymore... */
2178 void uiTemplateColorPicker(uiLayout *layout, PointerRNA *ptr, const char *propname, int value_slider,
2179                            int lock, int lock_luminosity, int cubic)
2180 {
2181         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2182         uiBlock *block = uiLayoutGetBlock(layout);
2183         uiLayout *col, *row;
2184         uiBut *but = NULL;
2185         float softmin, softmax, step, precision;
2186
2187         if (!prop) {
2188                 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2189                 return;
2190         }
2191
2192         RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2193
2194         col = uiLayoutColumn(layout, TRUE);
2195         row = uiLayoutRow(col, TRUE);
2196
2197         switch (U.color_picker_type) {
2198                 case USER_CP_CIRCLE:
2199                         but = uiDefButR_prop(block, HSVCIRCLE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2200                                              -1, 0.0, 0.0, 0, 0, "");
2201                         break;
2202                 case USER_CP_SQUARE_SV:
2203                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2204                                              -1, 0.0, 0.0, UI_GRAD_SV, 0, "");
2205                         break;
2206                 case USER_CP_SQUARE_HS:
2207                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2208                                              -1, 0.0, 0.0, UI_GRAD_HS, 0, "");
2209                         break;
2210                 case USER_CP_SQUARE_HV:
2211                         but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2212                                              -1, 0.0, 0.0, UI_GRAD_HV, 0, "");
2213                         break;
2214         }
2215
2216         if (lock) {
2217                 but->flag |= UI_BUT_COLOR_LOCK;
2218         }
2219
2220         if (lock_luminosity) {
2221                 float color[4]; /* in case of alpha */
2222                 but->flag |= UI_BUT_VEC_SIZE_LOCK;
2223                 RNA_property_float_get_array(ptr, prop, color);
2224                 but->a2 = len_v3(color);
2225         }
2226
2227         if (cubic)
2228                 but->flag |= UI_BUT_COLOR_CUBIC;
2229
2230         
2231         if (value_slider) {
2232                 switch (U.color_picker_type) {
2233                         case USER_CP_CIRCLE:
2234                                 uiItemS(row);
2235                                 uiDefButR_prop(block, HSVCUBE, 0, "", WHEEL_SIZE + 6, 0, 14, WHEEL_SIZE, ptr, prop,
2236                                                -1, softmin, softmax, UI_GRAD_V_ALT, 0, "");
2237                                 break;
2238                         case USER_CP_SQUARE_SV:
2239                                 uiItemS(col);
2240                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2241                                                -1, softmin, softmax, UI_GRAD_SV + 3, 0, "");
2242                                 break;
2243                         case USER_CP_SQUARE_HS:
2244                                 uiItemS(col);
2245                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2246                                                -1, softmin, softmax, UI_GRAD_HS + 3, 0, "");
2247                                 break;
2248                         case USER_CP_SQUARE_HV:
2249                                 uiItemS(col);
2250                                 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2251                                                -1, softmin, softmax, UI_GRAD_HV + 3, 0, "");
2252                                 break;
2253                 }
2254         }
2255 }
2256
2257 /********************* Layer Buttons Template ************************/
2258
2259 static void handle_layer_buttons(bContext *C, void *arg1, void *arg2)
2260 {
2261         uiBut *but = arg1;
2262         int cur = GET_INT_FROM_POINTER(arg2);
2263         wmWindow *win = CTX_wm_window(C);
2264         int i, tot, shift = win->eventstate->shift;
2265
2266         if (!shift) {
2267                 tot = RNA_property_array_length(&but->rnapoin, but->rnaprop);
2268                 
2269                 /* Normally clicking only selects one layer */
2270                 RNA_property_boolean_set_index(&but->rnapoin, but->rnaprop, cur, TRUE);
2271                 for (i = 0; i < tot; ++i) {
2272                         if (i != cur)
2273                                 RNA_property_boolean_set_index(&but->rnapoin, but->rnaprop, i, FALSE);
2274                 }
2275         }
2276
2277         /* view3d layer change should update depsgraph (invisible object changed maybe) */
2278         /* see view3d_header.c */
2279 }
2280
2281 /* TODO:
2282  * - for now, grouping of layers is determined by dividing up the length of
2283  *   the array of layer bitflags */
2284
2285 void uiTemplateLayers(uiLayout *layout, PointerRNA *ptr, const char *propname,
2286                       PointerRNA *used_ptr, const char *used_propname, int active_layer)
2287 {
2288         uiLayout *uRow, *uCol;
2289         PropertyRNA *prop, *used_prop = NULL;
2290         int groups, cols, layers;
2291         int group, col, layer, row;
2292         int cols_per_group = 5;
2293
2294         prop = RNA_struct_find_property(ptr, propname);
2295         if (!prop) {
2296                 RNA_warning("layers property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2297                 return;
2298         }
2299         
2300         /* the number of layers determines the way we group them 
2301          *      - we want 2 rows only (for now)
2302          *      - the number of columns (cols) is the total number of buttons per row
2303          *        the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
2304          *      - for now, only split into groups if group will have at least 5 items
2305          */
2306         layers = RNA_property_array_length(ptr, prop);
2307         cols = (layers / 2) + (layers % 2);
2308         groups = ((cols / 2) < cols_per_group) ? (1) : (cols / cols_per_group);
2309
2310         if (used_ptr && used_propname) {
2311                 used_prop = RNA_struct_find_property(used_ptr, used_propname);
2312                 if (!used_prop) {
2313                         RNA_warning("used layers property not found: %s.%s", RNA_struct_identifier(ptr->type), used_propname);
2314                         return;
2315                 }
2316
2317                 if (RNA_property_array_length(used_ptr, used_prop) < layers)
2318                         used_prop = NULL;
2319         }
2320         
2321         /* layers are laid out going across rows, with the columns being divided into groups */
2322         
2323         for (group = 0; group < groups; group++) {
2324                 uCol = uiLayoutColumn(layout, TRUE);
2325                 
2326                 for (row = 0; row < 2; row++) {
2327                         uiBlock *block;
2328                         uiBut *but;
2329
2330                         uRow = uiLayoutRow(uCol, TRUE);
2331                         block = uiLayoutGetBlock(uRow);
2332                         layer = groups * cols_per_group * row + cols_per_group * group;
2333                         
2334                         /* add layers as toggle buts */
2335                         for (col = 0; (col < cols_per_group) && (layer < layers); col++, layer++) {
2336                                 int icon = 0;
2337                                 int butlay = 1 << layer;
2338
2339                                 if (active_layer & butlay)
2340                                         icon = ICON_LAYER_ACTIVE;
2341                                 else if (used_prop && RNA_property_boolean_get_index(used_ptr, used_prop, layer))
2342                                         icon = ICON_LAYER_USED;
2343                                 
2344                                 but = uiDefAutoButR(block, ptr, prop, layer, "", icon, 0, 0, UI_UNIT_X / 2, UI_UNIT_Y / 2);
2345                                 uiButSetFunc(but, handle_layer_buttons, but, SET_INT_IN_POINTER(layer));
2346                                 but->type = TOG;
2347                         }
2348                 }
2349         }
2350 }
2351
2352 void uiTemplateGameStates(uiLayout *layout, PointerRNA *ptr, const char *propname,
2353                       PointerRNA *used_ptr, const char *used_propname, int active_state)
2354 {
2355         uiLayout *uRow, *uCol;
2356         PropertyRNA *prop, *used_prop = NULL;
2357         int groups, cols, states;
2358         int group, col, state, row;
2359         int cols_per_group = 5;
2360         Object *ob = (Object *)ptr->id.data;
2361
2362         prop = RNA_struct_find_property(ptr, propname);
2363         if (!prop) {
2364                 RNA_warning("states property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2365                 return;
2366         }
2367         
2368         /* the number of states determines the way we group them 
2369          *      - we want 2 rows only (for now)
2370          *      - the number of columns (cols) is the total number of buttons per row
2371          *        the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
2372          *      - for now, only split into groups if group will have at least 5 items
2373          */
2374         states = RNA_property_array_length(ptr, prop);
2375         cols = (states / 2) + (states % 2);
2376         groups = ((cols / 2) < cols_per_group) ? (1) : (cols / cols_per_group);
2377
2378         if (used_ptr && used_propname) {
2379                 used_prop = RNA_struct_find_property(used_ptr, used_propname);
2380                 if (!used_prop) {
2381                         RNA_warning("used layers property not found: %s.%s", RNA_struct_identifier(ptr->type), used_propname);
2382                         return;
2383                 }
2384
2385                 if (RNA_property_array_length(used_ptr, used_prop) < states)
2386                         used_prop = NULL;
2387         }
2388         
2389         /* layers are laid out going across rows, with the columns being divided into groups */
2390         
2391         for (group = 0; group < groups; group++) {
2392                 uCol = uiLayoutColumn(layout, TRUE);
2393                 
2394                 for (row = 0; row < 2; row++) {
2395                         uiBlock *block;
2396                         uiBut *but;
2397
2398                         uRow = uiLayoutRow(uCol, TRUE);
2399                         block = uiLayoutGetBlock(uRow);
2400                         state = groups * cols_per_group * row + cols_per_group * group;
2401                         
2402                         /* add layers as toggle buts */
2403                         for (col = 0; (col < cols_per_group) && (state < states); col++, state++) {
2404                                 int icon = 0;
2405                                 int butlay = 1 << state;
2406
2407                                 if (active_state & butlay)
2408                                         icon = ICON_LAYER_ACTIVE;
2409                                 else if (used_prop && RNA_property_boolean_get_index(used_ptr, used_prop, state))
2410                                         icon = ICON_LAYER_USED;
2411                                 
2412                                 but = uiDefIconButR_prop(block, ICONTOG, 0, icon, 0, 0, UI_UNIT_X / 2, UI_UNIT_Y / 2, ptr, prop,
2413                                                          state, 0, 0, -1, -1, sca_state_name_get(ob, state));
2414                                 uiButSetFunc(but, handle_layer_buttons, but, SET_INT_IN_POINTER(state));
2415                                 but->type = TOG;
2416                         }
2417                 }
2418         }
2419 }
2420
2421
2422 /************************* List Template **************************/
2423 static void uilist_draw_item_default(struct uiList *ui_list, struct bContext *UNUSED(C), struct uiLayout *layout,
2424                                      struct PointerRNA *UNUSED(dataptr), struct PointerRNA *itemptr, int icon,
2425                                      struct PointerRNA *UNUSED(active_dataptr), const char *UNUSED(active_propname),
2426                                      int UNUSED(index))
2427 {
2428         char *namebuf;
2429         const char *name;
2430
2431         namebuf = RNA_struct_name_get_alloc(itemptr, NULL, 0, NULL);
2432         name = (namebuf) ? namebuf : "";
2433
2434         /* Simplest one! */
2435         switch (ui_list->layout_type) {
2436         case UILST_LAYOUT_GRID:
2437                 uiItemL(layout, "", icon);
2438                 break;
2439         case UILST_LAYOUT_DEFAULT:
2440         case UILST_LAYOUT_COMPACT:
2441         default:
2442                 uiItemL(layout, name, icon);
2443                 break;
2444         }
2445
2446         /* free name */
2447         if (namebuf) {
2448                 MEM_freeN(namebuf);
2449         }
2450 }
2451
2452 void uiTemplateList(uiLayout *layout, bContext *C, const char *listtype_name, const char *list_id,
2453                     PointerRNA *dataptr, const char *propname, PointerRNA *active_dataptr,
2454                     const char *active_propname, int rows, int maxrows, int layout_type)
2455 {
2456         uiListType *ui_list_type;
2457         uiList *ui_list = NULL;
2458         ARegion *ar;
2459         uiListDrawItemFunc draw_item;
2460
2461         PropertyRNA *prop = NULL, *activeprop;
2462         PropertyType type, activetype;
2463         StructRNA *ptype;
2464         uiLayout *box, *row, *col, *sub, *overlap;
2465         uiBlock *block, *subblock;
2466         uiBut *but;
2467
2468         char ui_list_id[UI_MAX_NAME_STR];
2469         char numstr[32];
2470         int rnaicon = ICON_NONE, icon = ICON_NONE;
2471         int i = 0, activei = 0;
2472         int len = 0;
2473         int items;
2474         int found;
2475         int min, max;
2476
2477         /* validate arguments */
2478         block = uiLayoutGetBlock(layout);
2479
2480         if (!active_dataptr->data) {
2481                 RNA_warning("No active data");
2482                 return;
2483         }
2484
2485         if (dataptr->data) {
2486                 prop = RNA_struct_find_property(dataptr, propname);
2487                 if (!prop) {
2488                         RNA_warning("Property not found: %s.%s", RNA_struct_identifier(dataptr->type), propname);
2489                         return;
2490                 }
2491         }
2492
2493         activeprop = RNA_struct_find_property(active_dataptr, active_propname);
2494         if (!activeprop) {
2495                 RNA_warning("Property not found: %s.%s", RNA_struct_identifier(active_dataptr->type), active_propname);
2496                 return;
2497         }
2498
2499         if (prop) {
2500                 type = RNA_property_type(prop);
2501                 if (type != PROP_COLLECTION) {
2502                         RNA_warning("Expected a collection data property");
2503                         return;
2504                 }
2505         }
2506
2507         activetype = RNA_property_type(activeprop);
2508         if (activetype != PROP_INT) {
2509                 RNA_warning("Expected an integer active data property");
2510                 return;
2511         }
2512
2513         /* get icon */
2514         if (dataptr->data && prop) {
2515                 ptype = RNA_property_pointer_type(dataptr, prop);
2516                 rnaicon = RNA_struct_ui_icon(ptype);
2517         }
2518
2519         /* get active data */
2520         activei = RNA_property_int_get(active_dataptr, activeprop);
2521
2522         /* Find the uiList type. */
2523         ui_list_type = WM_uilisttype_find(listtype_name, FALSE);
2524
2525         if (ui_list_type == NULL) {
2526                 RNA_warning("List type %s not found", listtype_name);
2527                 return;
2528         }
2529
2530         draw_item = ui_list_type->draw_item ? ui_list_type->draw_item : uilist_draw_item_default;
2531
2532         /* Find or add the uiList to the current Region. */
2533         /* We tag the list id with the list type... */
2534         BLI_snprintf(ui_list_id, sizeof(ui_list_id), "%s_%s", ui_list_type->idname, list_id ? list_id : "");
2535
2536         ar = CTX_wm_region(C);
2537         ui_list = BLI_findstring(&ar->ui_lists, ui_list_id, offsetof(uiList, list_id));
2538
2539         if (!ui_list) {
2540                 ui_list = MEM_callocN(sizeof(uiList), __func__);
2541                 BLI_strncpy(ui_list->list_id, ui_list_id, sizeof(ui_list->list_id));
2542                 BLI_addtail(&ar->ui_lists, ui_list);
2543         }
2544
2545         /* Because we can't actually pass type across save&load... */
2546         ui_list->type = ui_list_type;
2547         ui_list->layout_type = layout_type;
2548
2549         switch (layout_type) {
2550         case UILST_LAYOUT_DEFAULT:
2551                 /* default rows */
2552                 if (rows == 0)
2553                         rows = 5;
2554                 if (maxrows == 0)
2555                         maxrows = 5;
2556                 if (ui_list->list_grip_size != 0)
2557                         rows = ui_list->list_grip_size;
2558
2559                 /* layout */
2560                 box = uiLayoutListBox(layout, ui_list, dataptr, prop, active_dataptr, activeprop);
2561                 row = uiLayoutRow(box, FALSE);
2562                 col = uiLayoutColumn(row, TRUE);
2563
2564                 /* init numbers */
2565                 RNA_property_int_range(active_dataptr, activeprop, &min, &max);
2566
2567                 if (prop)
2568                         len = RNA_property_collection_length(dataptr, prop);
2569                 items = CLAMPIS(len, rows, MAX2(rows, maxrows));
2570
2571                 /* if list length changes and active is out of view, scroll to it */
2572                 if ((ui_list->list_last_len != len) &&
2573                     (activei < ui_list->list_scroll || activei >= ui_list->list_scroll + items)) {
2574                         ui_list->list_scroll = activei;
2575                 }
2576
2577                 ui_list->list_scroll = min_ii(ui_list->list_scroll, len - items);
2578                 ui_list->list_scroll = max_ii(ui_list->list_scroll, 0);
2579                 ui_list->list_size = items;
2580                 ui_list->list_last_len = len;
2581
2582                 if (dataptr->data && prop) {
2583                         /* create list items */
2584                         RNA_PROP_BEGIN (dataptr, itemptr, prop)
2585                         {
2586                                 if (i >= ui_list->list_scroll && i < ui_list->list_scroll + items) {
2587                                         subblock = uiLayoutGetBlock(col);
2588                                         overlap = uiLayoutOverlap(col);
2589
2590                                         /* list item behind label & other buttons */
2591                                         sub = uiLayoutRow(overlap, FALSE);
2592
2593                                         but = uiDefButR_prop(subblock, LISTROW, 0, "", 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
2594                                                              active_dataptr, activeprop, 0, 0, i, 0, 0, "");
2595                                         uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2596
2597                                         sub = uiLayoutRow(overlap, FALSE);
2598
2599                                         icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, FALSE);
2600                                         if (icon == ICON_DOT)
2601                                                 icon = ICON_NONE;
2602                                         draw_item(ui_list, C, sub, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2603                                 }
2604                                 i++;
2605                         }
2606                         RNA_PROP_END;
2607                 }
2608
2609                 /* add dummy buttons to fill space */
2610                 while (i < ui_list->list_scroll + items) {
2611                         if (i >= ui_list->list_scroll)
2612                                 uiItemL(col, "", ICON_NONE);
2613                         i++;
2614                 }
2615
2616                 /* add scrollbar */
2617                 if (len > items) {
2618                         col = uiLayoutColumn(row, FALSE);
2619                         uiDefButI(block, SCROLL, 0, "", 0, 0, UI_UNIT_X * 0.75, UI_UNIT_Y * items, &ui_list->list_scroll,
2620                                   0, len - items, items, 0, "");
2621                 }
2622                 break;
2623         case UILST_LAYOUT_COMPACT:
2624                 row = uiLayoutRow(layout, TRUE);
2625
2626                 if (dataptr->data && prop) {
2627                         /* create list items */
2628                         RNA_PROP_BEGIN (dataptr, itemptr, prop)
2629                         {
2630                                 found = (activei == i);
2631
2632                                 if (found) {
2633                                         icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, FALSE);
2634                                         if (icon == ICON_DOT)
2635                                                 icon = ICON_NONE;
2636                                         draw_item(ui_list, C, row, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2637                                 }
2638
2639                                 i++;
2640                         }
2641                         RNA_PROP_END;
2642                 }
2643
2644                 /* if list is empty, add in dummy button */
2645                 if (i == 0)
2646                         uiItemL(row, "", ICON_NONE);
2647
2648                 /* next/prev button */
2649                 BLI_snprintf(numstr, sizeof(numstr), "%d :", i);
2650                 but = uiDefIconTextButR_prop(block, NUM, 0, 0, numstr, 0, 0, UI_UNIT_X * 5, UI_UNIT_Y,
2651                                              active_dataptr, activeprop, 0, 0, 0, 0, 0, "");
2652                 if (i == 0)
2653                         uiButSetFlag(but, UI_BUT_DISABLED);
2654                 break;
2655         case UILST_LAYOUT_GRID:
2656                 box = uiLayoutListBox(layout, ui_list, dataptr, prop, active_dataptr, activeprop);
2657                 col = uiLayoutColumn(box, TRUE);
2658                 row = uiLayoutRow(col, FALSE);
2659
2660                 if (dataptr->data && prop) {
2661                         /* create list items */
2662                         RNA_PROP_BEGIN (dataptr, itemptr, prop)
2663                         {
2664                                 /* create button */
2665                                 if (!(i % 9))
2666                                         row = uiLayoutRow(col, FALSE);
2667
2668                                 subblock = uiLayoutGetBlock(row);
2669                                 overlap = uiLayoutOverlap(row);
2670
2671                                 /* list item behind label & other buttons */
2672                                 sub = uiLayoutRow(overlap, FALSE);
2673
2674                                 but = uiDefButR_prop(subblock, LISTROW, 0, "", 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
2675                                                      active_dataptr, activeprop, 0, 0, i, 0, 0, "");
2676                                 uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2677
2678                                 sub = uiLayoutRow(overlap, FALSE);
2679
2680                                 icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, FALSE);
2681                                 draw_item(ui_list, C, sub, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2682
2683                                 i++;
2684                         }
2685                         RNA_PROP_END;
2686                 }
2687                 break;
2688         }
2689 }
2690
2691 /************************* Operator Search Template **************************/
2692
2693 static void operator_call_cb(bContext *C, void *UNUSED(arg1), void *arg2)
2694 {
2695         wmOperatorType *ot = arg2;
2696         
2697         if (ot)
2698                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
2699 }
2700
2701 static void operator_search_cb(const bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
2702 {
2703         GHashIterator *iter = WM_operatortype_iter();
2704
2705         for (; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
2706                 wmOperatorType *ot = BLI_ghashIterator_getValue(iter);
2707
2708                 if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0)
2709                         continue;
2710
2711                 if (BLI_strcasestr(ot->name, str)) {
2712                         if (WM_operator_poll((bContext *)C, ot)) {
2713                                 char name[256];
2714                                 int len = strlen(ot->name);
2715                                 
2716                                 /* display name for menu, can hold hotkey */
2717                                 BLI_strncpy(name, ot->name, sizeof(name));
2718                                 
2719                                 /* check for hotkey */
2720                                 if (len < sizeof(name) - 6) {
2721                                         if (WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, TRUE,
2722                                                                          &name[len + 1], sizeof(name) - len - 1))
2723                                         {
2724                                                 name[len] = '|';
2725                                         }
2726                                 }
2727                                 
2728                                 if (0 == uiSearchItemAdd(items, name, ot, 0))
2729                                         break;
2730                         }
2731                 }
2732         }
2733         BLI_ghashIterator_free(iter);
2734 }
2735
2736 void uiOperatorSearch_But(uiBut *but)
2737 {
2738         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
2739 }
2740
2741 void uiTemplateOperatorSearch(uiLayout *layout)
2742 {
2743         uiBlock *block;
2744         uiBut *but;
2745         static char search[256] = "";
2746                 
2747         block = uiLayoutGetBlock(layout);
2748         uiBlockSetCurLayout(block, layout);
2749
2750         but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, 0, 0, "");
2751         uiOperatorSearch_But(but);
2752 }
2753
2754 /************************* Running Jobs Template **************************/
2755
2756 #define B_STOPRENDER    1
2757 #define B_STOPCAST      2
2758 #define B_STOPANIM      3
2759 #define B_STOPCOMPO     4
2760 #define B_STOPSEQ       5
2761 #define B_STOPCLIP      6
2762 #define B_STOPOTHER     7
2763
2764 static void do_running_jobs(bContext *C, void *UNUSED(arg), int event)
2765 {
2766         switch (event) {
2767                 case B_STOPRENDER:
2768                         G.is_break = TRUE;
2769                         break;
2770                 case B_STOPCAST:
2771                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_screen(C), NULL);
2772                         break;
2773                 case B_STOPANIM:
2774                         WM_operator_name_call(C, "SCREEN_OT_animation_play", WM_OP_INVOKE_SCREEN, NULL);
2775                         break;
2776                 case B_STOPCOMPO:
2777                         WM_jobs_stop(CTX_wm_manager(C), CTX_data_scene(C), NULL);
2778                         break;
2779                 case B_STOPSEQ:
2780                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2781                         break;
2782                 case B_STOPCLIP:
2783                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2784                         break;
2785                 case B_STOPOTHER:
2786                         G.is_break = TRUE;
2787                         break;
2788         }
2789 }
2790
2791 void uiTemplateRunningJobs(uiLayout *layout, bContext *C)
2792 {
2793         bScreen *screen = CTX_wm_screen(C);
2794         wmWindowManager *wm = CTX_wm_manager(C);
2795         ScrArea *sa = CTX_wm_area(C);
2796         uiBlock *block;
2797         void *owner = NULL;
2798         int handle_event;
2799         
2800         block = uiLayoutGetBlock(layout);
2801         uiBlockSetCurLayout(block, layout);
2802
2803         uiBlockSetHandleFunc(block, do_running_jobs, NULL);
2804
2805         if (sa->spacetype == SPACE_SEQ) {
2806                 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2807                         owner = sa;
2808                 handle_event = B_STOPSEQ;
2809         }
2810         else if (sa->spacetype == SPACE_CLIP) {
2811                 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2812                         owner = sa;
2813                 handle_event = B_STOPCLIP;
2814         }
2815         else {
2816                 Scene *scene;
2817                 /* another scene can be rendering too, for example via compositor */
2818                 for (scene = CTX_data_main(C)->scene.first; scene; scene = scene->id.next) {
2819                         if (WM_jobs_test(wm, scene, WM_JOB_TYPE_RENDER)) {
2820                                 handle_event = B_STOPRENDER;
2821                                 break;
2822                         }
2823                         else if (WM_jobs_test(wm, scene, WM_JOB_TYPE_COMPOSITE)) {
2824                                 handle_event = B_STOPCOMPO;
2825                                 break;
2826                         }
2827                         else if (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY)) {
2828                                 handle_event = B_STOPOTHER;
2829                                 break;
2830                         }
2831                 }
2832                 owner = scene;
2833         }
2834
2835         if (owner) {
2836                 uiLayout *ui_abs;
2837                 
2838                 ui_abs = uiLayoutAbsolute(layout, FALSE);
2839                 (void)ui_abs;  /* UNUSED */
2840                 
2841                 uiDefIconBut(block, BUT, handle_event, ICON_PANEL_CLOSE, 0, UI_UNIT_Y * 0.1, UI_UNIT_X * 0.8, UI_UNIT_Y * 0.8,
2842                              NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop this job"));
2843                 uiDefBut(block, PROGRESSBAR, 0, WM_jobs_name(wm, owner), 
2844                          UI_UNIT_X, 0, 100, UI_UNIT_Y, NULL, 0.0f, 0.0f, WM_jobs_progress(wm, owner), 0, TIP_("Progress"));
2845                 
2846                 uiLayoutRow(layout, FALSE);
2847         }
2848         if (WM_jobs_test(wm, screen, WM_JOB_TYPE_SCREENCAST))
2849                 uiDefIconTextBut(block, BUT, B_STOPCAST, ICON_CANCEL, IFACE_("Capture"), 0, 0, 85, UI_UNIT_Y,
2850                                  NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop screencast"));
2851         if (screen->animtimer)
2852                 uiDefIconTextBut(block, BUT, B_STOPANIM, ICON_CANCEL, IFACE_("Anim Player"), 0, 0, 100, UI_UNIT_Y,
2853                                  NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop animation playback"));
2854 }
2855
2856 /************************* Reports for Last Operator Template **************************/
2857
2858 void uiTemplateReportsBanner(uiLayout *layout, bContext *C)
2859 {
2860         ReportList *reports = CTX_wm_reports(C);
2861         Report *report = BKE_reports_last_displayable(reports);
2862         ReportTimerInfo *rti;
2863         
2864         uiLayout *ui_abs;
2865         uiBlock *block;
2866         uiBut *but;
2867         uiStyle *style = UI_GetStyle();
2868         int width;
2869         int icon = 0;
2870         
2871         /* if the report display has timed out, don't show */
2872         if (!reports->reporttimer) return;
2873         
2874         rti = (ReportTimerInfo *)reports->reporttimer->customdata;
2875         
2876         if (!rti || rti->widthfac == 0.0f || !report) return;
2877         
2878         ui_abs = uiLayoutAbsolute(layout, FALSE);
2879         block = uiLayoutGetBlock(ui_abs);
2880         
2881         width = BLF_width(style->widget.uifont_id, report->message);
2882         width = min_ii((int)(rti->widthfac * width), width);
2883         width = max_ii(width, 10);
2884         
2885         /* make a box around the report to make it stand out */
2886         uiBlockBeginAlign(block);
2887         but = uiDefBut(block, ROUNDBOX, 0, "", 0, 0, UI_UNIT_X + 10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "");
2888         /* set the report's bg color in but->col - ROUNDBOX feature */
2889         rgb_float_to_uchar(but->col, rti->col);
2890         but->col[3] = 255;
2891
2892         but = uiDefBut(block, ROUNDBOX, 0, "", UI_UNIT_X + 10, 0, UI_UNIT_X + width, UI_UNIT_Y,
2893                        NULL, 0.0f, 0.0f, 0, 0, "");
2894         but->col[0] = but->col[1] = but->col[2] = FTOCHAR(rti->grayscale);
2895         but->col[3] = 255;
2896
2897         uiBlockEndAlign(block);
2898         
2899         
2900         /* icon and report message on top */
2901         if (report->type & RPT_ERROR_ALL)
2902                 icon = ICON_ERROR;
2903         else if (report->type & RPT_WARNING_ALL)
2904                 icon = ICON_ERROR;
2905         else if (report->type & RPT_INFO_ALL)
2906                 icon = ICON_INFO;
2907         
2908         /* XXX: temporary operator to dump all reports to a text block, but only if more than 1 report 
2909          * to be shown instead of icon when appropriate...
2910          */
2911         uiBlockSetEmboss(block, UI_EMBOSSN);
2912
2913         if (reports->list.first != reports->list.last)
2914                 uiDefIconButO(block, BUT, "UI_OT_reports_to_textblock", WM_OP_INVOKE_REGION_WIN, icon, 2, 0, UI_UNIT_X,
2915                               UI_UNIT_Y, TIP_("Click to see the remaining reports in text block: 'Recent Reports'"));
2916         else
2917                 uiDefIconBut(block, LABEL, 0, icon, 2, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "");
2918
2919         uiBlockSetEmboss(block, UI_EMBOSS);
2920         
2921         uiDefBut(block, LABEL, 0, report->message, UI_UNIT_X + 10, 0, UI_UNIT_X + width, UI_UNIT_Y,
2922                  NULL, 0.0f, 0.0f, 0, 0, "");
2923 }
2924
2925 /********************************* Keymap *************************************/
2926
2927 static void keymap_item_modified(bContext *UNUSED(C), void *kmi_p, void *UNUSED(unused))
2928 {
2929         wmKeyMapItem *kmi = (wmKeyMapItem *)kmi_p;
2930         WM_keyconfig_update_tag(NULL, kmi);
2931 }
2932
2933 static void template_keymap_item_properties(uiLayout *layout, const char *title, PointerRNA *ptr)
2934 {
2935         uiLayout *flow;
2936
2937         uiItemS(layout);
2938
2939         if (title)
2940                 uiItemL(layout, title, ICON_NONE);
2941         
2942         flow = uiLayoutColumnFlow(layout, 2, FALSE);
2943
2944         RNA_STRUCT_BEGIN (ptr, prop)
2945         {
2946                 int flag = RNA_property_flag(prop);
2947
2948                 if (flag & PROP_HIDDEN)
2949                         continue;
2950
2951                 /* recurse for nested properties */
2952                 if (RNA_property_type(prop) == PROP_POINTER) {
2953                         PointerRNA propptr = RNA_property_pointer_get(ptr, prop);
2954                         const char *name = RNA_property_ui_name(prop);
2955
2956                         if (propptr.data && RNA_struct_is_a(propptr.type, &RNA_OperatorProperties)) {
2957                                 template_keymap_item_properties(layout, name, &propptr);
2958                                 continue;
2959                         }
2960                 }
2961
2962                 /* add property */
2963                 uiItemR(flow, ptr, RNA_property_identifier(prop), 0, NULL, ICON_NONE);
2964         }
2965         RNA_STRUCT_END;
2966 }
2967
2968 void uiTemplateKeymapItemProperties(uiLayout *layout, PointerRNA *ptr)
2969 {
2970         PointerRNA propptr = RNA_pointer_get(ptr, "properties");
2971
2972         if (propptr.data) {
2973                 uiBut *but = uiLayoutGetBlock(layout)->buttons.last;
2974
2975                 template_keymap_item_properties(layout, NULL, &propptr);
2976
2977                 /* attach callbacks to compensate for missing properties update,
2978                  * we don't know which keymap (item) is being modified there */
2979                 for (; but; but = but->next)
2980                         uiButSetFunc(but, keymap_item_modified, ptr->data, NULL);
2981         }
2982 }
2983
2984 /********************************* Color management *************************************/
2985
2986 void uiTemplateColorspaceSettings(uiLayout *layout, PointerRNA *ptr, const char *propname)
2987 {
2988         PropertyRNA *prop;
2989         PointerRNA colorspace_settings_ptr;
2990
2991         prop = RNA_struct_find_property(ptr, propname);
2992
2993         if (!prop) {
2994                 printf("%s: property not found: %s.%s\n",
2995                        __func__, RNA_struct_iden