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