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