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