Merged changes in the trunk up to revision 54171.
[blender-staging.git] / source / blender / editors / interface / interface_templates.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Foundation 2009.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/interface/interface_templates.c
24  *  \ingroup edinterface
25  */
26
27
28 #include <stdlib.h>
29 #include <stddef.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_dynamicpaint_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_object_force.h"
38
39 #include "BLI_utildefines.h"
40 #include "BLI_string.h"
41 #include "BLI_ghash.h"
42 #include "BLI_rect.h"
43 #include "BLI_math.h"
44 #include "BLI_listbase.h"
45
46 #include "BLF_api.h"
47 #include "BLF_translation.h"
48
49 #include "BKE_animsys.h"
50 #include "BKE_colortools.h"
51 #include "BKE_context.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_displist.h"
54 #include "BKE_dynamicpaint.h"
55 #include "BKE_global.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_material.h"
59 #include "BKE_modifier.h"
60 #include "BKE_object.h"
61 #include "BKE_packedFile.h"
62 #include "BKE_particle.h"
63 #include "BKE_report.h"
64 #include "BKE_sca.h"
65 #include "BKE_scene.h"
66 #include "BKE_screen.h"
67 #include "BKE_texture.h"
68
69 #include "ED_screen.h"
70 #include "ED_object.h"
71 #include "ED_render.h"
72 #include "ED_util.h"
73
74 #include "RNA_access.h"
75 #include "RNA_enum_types.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "UI_interface.h"
81 #include "UI_interface_icons.h"
82 #include "interface_intern.h"
83
84 void UI_template_fix_linking(void)
85 {
86 }
87
88 /********************** Header Template *************************/
89
90 void uiTemplateHeader(uiLayout *layout, bContext *C, int menus)
91 {
92         uiBlock *block;
93
94         block = uiLayoutAbsoluteBlock(layout);
95         if (menus) ED_area_header_standardbuttons(C, block, 0);
96         else ED_area_header_switchbutton(C, block, 0);
97 }
98
99 /********************** Search Callbacks *************************/
100
101 typedef struct TemplateID {
102         PointerRNA ptr;
103         PropertyRNA *prop;
104
105         ListBase *idlb;
106         int prv_rows, prv_cols;
107         int preview;
108 } TemplateID;
109
110 /* Search browse menu, assign  */
111 static void id_search_call_cb(bContext *C, void *arg_template, void *item)
112 {
113         TemplateID *template = (TemplateID *)arg_template;
114
115         /* ID */
116         if (item) {
117                 PointerRNA idptr;
118
119                 RNA_id_pointer_create(item, &idptr);
120                 RNA_property_pointer_set(&template->ptr, template->prop, idptr);
121                 RNA_property_update(C, &template->ptr, template->prop);
122         }
123 }
124
125 /* ID Search browse menu, do the search */
126 static void id_search_cb(const bContext *C, void *arg_template, const char *str, uiSearchItems *items)
127 {
128         TemplateID *template = (TemplateID *)arg_template;
129         ListBase *lb = template->idlb;
130         ID *id, *id_from = template->ptr.id.data;
131         int iconid;
132         int flag = RNA_property_flag(template->prop);
133
134         /* ID listbase */
135         for (id = lb->first; id; id = id->next) {
136                 if (!((flag & PROP_ID_SELF_CHECK) && id == id_from)) {
137
138                         /* use filter */
139                         if (RNA_property_type(template->prop) == PROP_POINTER) {
140                                 PointerRNA ptr;
141                                 RNA_id_pointer_create(id, &ptr);
142                                 if (RNA_property_pointer_poll(&template->ptr, template->prop, &ptr) == 0)
143                                         continue;
144                         }
145
146                         /* hide dot-datablocks, but only if filter does not force it visible */
147                         if (U.uiflag & USER_HIDE_DOT)
148                                 if ((id->name[2] == '.') && (str[0] != '.'))
149                                         continue;
150
151                         if (BLI_strcasestr(id->name + 2, str)) {
152                                 char name_ui[MAX_ID_NAME];
153                                 name_uiprefix_id(name_ui, id);
154
155                                 iconid = ui_id_icon_get((bContext *)C, id, template->preview);
156
157                                 if (!uiSearchItemAdd(items, name_ui, id, iconid))
158                                         break;
159                         }
160                 }
161         }
162 }
163
164 /* ID Search browse menu, open */
165 static uiBlock *id_search_menu(bContext *C, ARegion *ar, void *arg_litem)
166 {
167         static char search[256];
168         static TemplateID template;
169         PointerRNA idptr;
170         wmWindow *win = CTX_wm_window(C);
171         uiBlock *block;
172         uiBut *but;
173         
174         /* clear initial search string, then all items show */
175         search[0] = 0;
176         /* arg_litem is malloced, can be freed by parent button */
177         template = *((TemplateID *)arg_litem);
178         
179         /* get active id for showing first item */
180         idptr = RNA_property_pointer_get(&template.ptr, template.prop);
181
182         block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
183         uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_REDRAW | UI_BLOCK_SEARCH_MENU);
184         
185         /* preview thumbnails */
186         if (template.prv_rows > 0 && template.prv_cols > 0) {
187                 int w = 4 * U.widget_unit * template.prv_cols;
188                 int h = 4 * U.widget_unit * template.prv_rows + U.widget_unit;
189                 
190                 /* fake button, it holds space for search items */
191                 uiDefBut(block, LABEL, 0, "", 10, 15, w, h, NULL, 0, 0, 0, 0, NULL);
192                 
193                 but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, w, UI_UNIT_Y,
194                                      template.prv_rows, template.prv_cols, "");
195                 uiButSetSearchFunc(but, id_search_cb, &template, id_search_call_cb, idptr.data);
196         }
197         /* list view */
198         else {
199                 const int searchbox_width  = uiSearchBoxWidth();
200                 const int searchbox_height = uiSearchBoxHeight();
201                 
202                 /* fake button, it holds space for search items */
203                 uiDefBut(block, LABEL, 0, "", 10, 15, searchbox_width, searchbox_height, NULL, 0, 0, 0, 0, NULL);
204                 but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, searchbox_width, UI_UNIT_Y - 1, 0, 0, "");
205                 uiButSetSearchFunc(but, id_search_cb, &template, id_search_call_cb, idptr.data);
206         }
207                 
208         
209         uiBoundsBlock(block, 0.3f * U.widget_unit);
210         uiBlockSetDirection(block, UI_DOWN);
211         uiEndBlock(C, block);
212         
213         /* give search-field focus */
214         uiButSetFocusOnEnter(win, but);
215         /* this type of search menu requires undo */
216         but->flag |= UI_BUT_UNDO;
217         
218         return block;
219 }
220
221 /************************ ID Template ***************************/
222 /* This is for browsing and editing the ID-blocks used */
223
224 /* for new/open operators */
225 void uiIDContextProperty(bContext *C, PointerRNA *ptr, PropertyRNA **prop)
226 {
227         TemplateID *template;
228         ARegion *ar = CTX_wm_region(C);
229         uiBlock *block;
230         uiBut *but;
231
232         memset(ptr, 0, sizeof(*ptr));
233         *prop = NULL;
234
235         if (!ar)
236                 return;
237
238         for (block = ar->uiblocks.first; block; block = block->next) {
239                 for (but = block->buttons.first; but; but = but->next) {
240                         /* find the button before the active one */
241                         if ((but->flag & (UI_BUT_LAST_ACTIVE | UI_ACTIVE))) {
242                                 if (but->func_argN) {
243                                         template = but->func_argN;
244                                         *ptr = template->ptr;
245                                         *prop = template->prop;
246                                         return;
247                                 }
248                         }
249                 }
250         }
251 }
252
253
254 static void template_id_cb(bContext *C, void *arg_litem, void *arg_event)
255 {
256         TemplateID *template = (TemplateID *)arg_litem;
257         PointerRNA idptr = RNA_property_pointer_get(&template->ptr, template->prop);
258         ID *id = idptr.data;
259         int event = GET_INT_FROM_POINTER(arg_event);
260         
261         switch (event) {
262                 case UI_ID_BROWSE:
263                 case UI_ID_PIN:
264                         RNA_warning("warning, id event %d shouldnt come here", event);
265                         break;
266                 case UI_ID_OPEN:
267                 case UI_ID_ADD_NEW:
268                         /* these call uiIDContextProperty */
269                         break;
270                 case UI_ID_DELETE:
271                         memset(&idptr, 0, sizeof(idptr));
272                         RNA_property_pointer_set(&template->ptr, template->prop, idptr);
273                         RNA_property_update(C, &template->ptr, template->prop);
274
275                         if (id && CTX_wm_window(C)->eventstate->shift) /* useful hidden functionality, */
276                                 id->us = 0;
277
278                         break;
279                 case UI_ID_FAKE_USER:
280                         if (id) {
281                                 if (id->flag & LIB_FAKEUSER) id_us_plus(id);
282                                 else id_us_min(id);
283                         }
284                         else return;
285                         break;
286                 case UI_ID_LOCAL:
287                         if (id) {
288                                 if (id_make_local(id, 0)) {
289                                         /* reassign to get get proper updates/notifiers */
290                                         idptr = RNA_property_pointer_get(&template->ptr, template->prop);
291                                         RNA_property_pointer_set(&template->ptr, template->prop, idptr);
292                                         RNA_property_update(C, &template->ptr, template->prop);
293                                 }
294                         }
295                         break;
296                 case UI_ID_ALONE:
297                         if (id) {
298                                 const int do_scene_obj = (GS(id->name) == ID_OB) &&
299                                                          (template->ptr.type == &RNA_SceneObjects);
300
301                                 /* make copy */
302                                 if (do_scene_obj) {
303                                         Scene *scene = CTX_data_scene(C);
304                                         ED_object_single_user(scene, (struct Object *)id);
305                                         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
306                                 }
307                                 else {
308                                         if (id) {
309                                                 id_single_user(C, id, &template->ptr, template->prop);
310                                         }
311                                 }
312                         }
313                         break;
314 #if 0
315                 case UI_ID_AUTO_NAME:
316                         break;
317 #endif
318         }
319 }
320
321 static const char *template_id_browse_tip(StructRNA *type)
322 {
323         if (type) {
324                 switch (RNA_type_to_ID_code(type)) {
325                         case ID_SCE: return N_("Browse Scene to be linked");
326                         case ID_OB:  return N_("Browse Object to be linked");
327                         case ID_ME:  return N_("Browse Mesh Data to be linked");
328                         case ID_CU:  return N_("Browse Curve Data to be linked");
329                         case ID_MB:  return N_("Browse Metaball Data to be linked");
330                         case ID_MA:  return N_("Browse Material to be linked");
331                         case ID_TE:  return N_("Browse Texture to be linked");
332                         case ID_IM:  return N_("Browse Image to be linked");
333 #ifdef WITH_FREESTYLE
334                         case ID_LS:  return N_("Browse Line Style Data to be linked");
335 #endif
336                         case ID_LT:  return N_("Browse Lattice Data to be linked");
337                         case ID_LA:  return N_("Browse Lamp Data to be linked");
338                         case ID_CA:  return N_("Browse Camera Data to be linked");
339                         case ID_WO:  return N_("Browse World Settings to be linked");
340                         case ID_SCR: return N_("Choose Screen lay-out");
341                         case ID_TXT: return N_("Browse Text to be linked");
342                         case ID_SPK: return N_("Browse Speaker Data to be linked");
343                         case ID_SO:  return N_("Browse Sound to be linked");
344                         case ID_AR:  return N_("Browse Armature data to be linked");
345                         case ID_AC:  return N_("Browse Action to be linked");
346                         case ID_NT:  return N_("Browse Node Tree to be linked");
347                         case ID_BR:  return N_("Browse Brush to be linked");
348                         case ID_PA:  return N_("Browse Particle System to be linked");
349                         case ID_GD:  return N_("Browse Grease Pencil Data to be linked");
350                 }
351         }
352         return N_("Browse ID data to be linked");
353 }
354
355 /* Return a type-based i18n context, needed e.g. by "New" button.
356  * In most languages, this adjective takes different form based on gender of type name...
357  */
358 #ifdef WITH_INTERNATIONAL
359 static const char *template_id_context(StructRNA *type)
360 {
361         if (type) {
362                 switch (RNA_type_to_ID_code(type)) {
363                         case ID_SCE: return BLF_I18NCONTEXT_ID_SCENE;
364                         case ID_OB:  return BLF_I18NCONTEXT_ID_OBJECT;
365                         case ID_ME:  return BLF_I18NCONTEXT_ID_MESH;
366                         case ID_CU:  return BLF_I18NCONTEXT_ID_CURVE;
367                         case ID_MB:  return BLF_I18NCONTEXT_ID_METABALL;
368                         case ID_MA:  return BLF_I18NCONTEXT_ID_MATERIAL;
369                         case ID_TE:  return BLF_I18NCONTEXT_ID_TEXTURE;
370                         case ID_IM:  return BLF_I18NCONTEXT_ID_IMAGE;
371 #ifdef WITH_FREESTYLE
372                         case ID_LS:  return BLF_I18NCONTEXT_ID_FREESTYLELINESTYLE;
373 #endif
374                         case ID_LT:  return BLF_I18NCONTEXT_ID_LATTICE;
375                         case ID_LA:  return BLF_I18NCONTEXT_ID_LAMP;
376                         case ID_CA:  return BLF_I18NCONTEXT_ID_CAMERA;
377                         case ID_WO:  return BLF_I18NCONTEXT_ID_WORLD;
378                         case ID_SCR: return BLF_I18NCONTEXT_ID_SCREEN;
379                         case ID_TXT: return BLF_I18NCONTEXT_ID_TEXT;
380                         case ID_SPK: return BLF_I18NCONTEXT_ID_SPEAKER;
381                         case ID_SO:  return BLF_I18NCONTEXT_ID_SOUND;
382                         case ID_AR:  return BLF_I18NCONTEXT_ID_ARMATURE;
383                         case ID_AC:  return BLF_I18NCONTEXT_ID_ACTION;
384                         case ID_NT:  return BLF_I18NCONTEXT_ID_NODETREE;
385                         case ID_BR:  return BLF_I18NCONTEXT_ID_BRUSH;
386                         case ID_PA:  return BLF_I18NCONTEXT_ID_PARTICLESETTINGS;
387                         case ID_GD:  return BLF_I18NCONTEXT_ID_GPENCIL;
388                 }
389         }
390         return BLF_I18NCONTEXT_DEFAULT;
391 }
392 #endif
393
394 static void template_ID(bContext *C, uiLayout *layout, TemplateID *template, StructRNA *type, short idcode, int flag,
395                         const char *newop, const char *openop, const char *unlinkop)
396 {
397         uiBut *but;
398         uiBlock *block;
399         PointerRNA idptr;
400         // ListBase *lb; // UNUSED
401         ID *id, *idfrom;
402         int editable = RNA_property_editable(&template->ptr, template->prop);
403
404         idptr = RNA_property_pointer_get(&template->ptr, template->prop);
405         id = idptr.data;
406         idfrom = template->ptr.id.data;
407         // lb = template->idlb;
408
409         block = uiLayoutGetBlock(layout);
410         uiBlockBeginAlign(block);
411
412         if (idptr.type)
413                 type = idptr.type;
414
415         if (flag & UI_ID_PREVIEWS) {
416                 template->preview = TRUE;
417
418                 but = uiDefBlockButN(block, id_search_menu, MEM_dupallocN(template), "", 0, 0, UI_UNIT_X * 6, UI_UNIT_Y * 6,
419                                      TIP_(template_id_browse_tip(type)));
420                 if (type) {
421                         but->icon = RNA_struct_ui_icon(type);
422                         if (id) but->icon = ui_id_icon_get(C, id, 1);
423                         uiButSetFlag(but, UI_HAS_ICON | UI_ICON_PREVIEW);
424                 }
425                 if ((idfrom && idfrom->lib) || !editable)
426                         uiButSetFlag(but, UI_BUT_DISABLED);
427                 
428                 uiLayoutRow(layout, TRUE);
429         }
430         else if (flag & UI_ID_BROWSE) {
431                 but = uiDefBlockButN(block, id_search_menu, MEM_dupallocN(template), "", 0, 0, UI_UNIT_X * 1.6, UI_UNIT_Y,
432                                      TIP_(template_id_browse_tip(type)));
433
434                 uiButSetDrawFlag(but, UI_BUT_DRAW_ENUM_ARROWS);
435
436                 if (type) {
437                         but->icon = RNA_struct_ui_icon(type);
438                         /* default dragging of icon for id browse buttons */
439                         uiButSetDragID(but, id);
440                         uiButSetFlag(but, UI_HAS_ICON | UI_ICON_LEFT);
441                 }
442
443                 if ((idfrom && idfrom->lib) || !editable)
444                         uiButSetFlag(but, UI_BUT_DISABLED);
445         }
446
447         /* text button with name */
448         if (id) {
449                 char name[UI_MAX_NAME_STR];
450                 const short user_alert = (id->us <= 0);
451
452                 //text_idbutton(id, name);
453                 name[0] = '\0';
454                 but = uiDefButR(block, TEX, 0, name, 0, 0, UI_UNIT_X * 6, UI_UNIT_Y,
455                                 &idptr, "name", -1, 0, 0, -1, -1, RNA_struct_ui_description(type));
456                 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_RENAME));
457                 if (user_alert) uiButSetFlag(but, UI_BUT_REDALERT);
458
459                 if (id->lib) {
460                         if (id->flag & LIB_INDIRECT) {
461                                 but = uiDefIconBut(block, BUT, 0, ICON_LIBRARY_DATA_INDIRECT, 0, 0, UI_UNIT_X, UI_UNIT_Y,
462                                                    NULL, 0, 0, 0, 0, TIP_("Indirect library datablock, cannot change"));
463                                 uiButSetFlag(but, UI_BUT_DISABLED);
464                         }
465                         else {
466                                 but = uiDefIconBut(block, BUT, 0, ICON_LIBRARY_DATA_DIRECT, 0, 0, UI_UNIT_X, UI_UNIT_Y,
467                                                    NULL, 0, 0, 0, 0, TIP_("Direct linked library datablock, click to make local"));
468                                 if (!id_make_local(id, 1 /* test */) || (idfrom && idfrom->lib))
469                                         uiButSetFlag(but, UI_BUT_DISABLED);
470                         }
471
472                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_LOCAL));
473                 }
474
475                 if (id->us > 1) {
476                         char numstr[32];
477
478                         BLI_snprintf(numstr, sizeof(numstr), "%d", id->us);
479
480                         but = uiDefBut(block, BUT, 0, numstr, 0, 0, UI_UNIT_X + ((id->us < 10) ? 0 : 10), UI_UNIT_Y,
481                                        NULL, 0, 0, 0, 0,
482                                        TIP_("Display number of users of this data (click to make a single-user copy)"));
483
484                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ALONE));
485                         if (/* test only */
486                             (id_copy(id, NULL, 1) == FALSE) ||
487                             (idfrom && idfrom->lib) ||
488                             (editable == FALSE) ||
489                             /* object in editmode - don't change data */
490                             (idfrom && GS(idfrom->name) == ID_OB && (((Object *)idfrom)->mode & OB_MODE_EDIT)))
491                         {
492                                 uiButSetFlag(but, UI_BUT_DISABLED);
493                         }
494                 }
495         
496                 if (user_alert) uiButSetFlag(but, UI_BUT_REDALERT);
497                 
498                 if (id->lib == NULL && !(ELEM5(GS(id->name), ID_GR, ID_SCE, ID_SCR, ID_TXT, ID_OB))) {
499                         uiDefButR(block, TOG, 0, "F", 0, 0, UI_UNIT_X, UI_UNIT_Y, &idptr, "use_fake_user", -1, 0, 0, -1, -1, NULL);
500                 }
501         }
502         
503         if (flag & UI_ID_ADD_NEW) {
504                 int w = id ? UI_UNIT_X : (flag & UI_ID_OPEN) ? UI_UNIT_X * 3 : UI_UNIT_X * 6;
505                 
506                 /* i18n markup, does nothing! */
507                 BLF_I18N_MSGID_MULTI_CTXT("New", BLF_I18NCONTEXT_DEFAULT,
508                                                  BLF_I18NCONTEXT_ID_SCENE,
509                                                  BLF_I18NCONTEXT_ID_OBJECT,
510                                                  BLF_I18NCONTEXT_ID_MESH,
511                                                  BLF_I18NCONTEXT_ID_CURVE,
512                                                  BLF_I18NCONTEXT_ID_METABALL,
513                                                  BLF_I18NCONTEXT_ID_MATERIAL,
514                                                  BLF_I18NCONTEXT_ID_TEXTURE,
515                                                  BLF_I18NCONTEXT_ID_IMAGE,
516                                                  BLF_I18NCONTEXT_ID_LATTICE,
517                                                  BLF_I18NCONTEXT_ID_LAMP,
518                                                  BLF_I18NCONTEXT_ID_CAMERA,
519                                                  BLF_I18NCONTEXT_ID_WORLD,
520                                                  BLF_I18NCONTEXT_ID_SCREEN,
521                                                  BLF_I18NCONTEXT_ID_TEXT);
522                 BLF_I18N_MSGID_MULTI_CTXT("New", BLF_I18NCONTEXT_ID_SPEAKER,
523                                                  BLF_I18NCONTEXT_ID_SOUND,
524                                                  BLF_I18NCONTEXT_ID_ARMATURE,
525                                                  BLF_I18NCONTEXT_ID_ACTION,
526                                                  BLF_I18NCONTEXT_ID_NODETREE,
527                                                  BLF_I18NCONTEXT_ID_BRUSH,
528                                                  BLF_I18NCONTEXT_ID_PARTICLESETTINGS,
529                                                  BLF_I18NCONTEXT_ID_GPENCIL,
530 #ifdef WITH_FREESTYLE
531                                                                                  BLF_I18NCONTEXT_ID_FREESTYLELINESTYLE
532 #endif
533                 );
534                 
535                 if (newop) {
536                         but = uiDefIconTextButO(block, BUT, newop, WM_OP_INVOKE_DEFAULT, ICON_ZOOMIN,
537                                                 (id) ? "" : CTX_IFACE_(template_id_context(type), "New"), 0, 0, w, UI_UNIT_Y, NULL);
538                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ADD_NEW));
539                 }
540                 else {
541                         but = uiDefIconTextBut(block, BUT, 0, ICON_ZOOMIN, (id) ? "" : CTX_IFACE_(template_id_context(type), "New"),
542                                                0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
543                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ADD_NEW));
544                 }
545
546                 if ((idfrom && idfrom->lib) || !editable)
547                         uiButSetFlag(but, UI_BUT_DISABLED);
548         }
549
550         /* Due to space limit in UI - skip the "open" icon for packed data, and allow to unpack.
551            Only for images, sound and fonts */
552         if (id && BKE_pack_check(id)) {
553
554                 but = uiDefIconButO(block, BUT, "FILE_OT_unpack_item", WM_OP_INVOKE_REGION_WIN, ICON_PACKAGE, 0, 0, UI_UNIT_X, UI_UNIT_Y, "Packed File");
555                 uiButGetOperatorPtrRNA(but);
556                 
557                 RNA_string_set(but->opptr, "id_name", id->name + 2);
558                 RNA_int_set(but->opptr, "id_type", GS(id->name));
559                 
560         }
561         else if (flag & UI_ID_OPEN) {
562                 int w = id ? UI_UNIT_X : (flag & UI_ID_ADD_NEW) ? UI_UNIT_X * 3 : UI_UNIT_X * 6;
563                 
564                 if (openop) {
565                         but = uiDefIconTextButO(block, BUT, openop, WM_OP_INVOKE_DEFAULT, ICON_FILESEL, (id) ? "" : IFACE_("Open"),
566                                                 0, 0, w, UI_UNIT_Y, NULL);
567                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_OPEN));
568                 }
569                 else {
570                         but = uiDefIconTextBut(block, BUT, 0, ICON_FILESEL, (id) ? "" : IFACE_("Open"), 0, 0, w, UI_UNIT_Y,
571                                                NULL, 0, 0, 0, 0, NULL);
572                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_OPEN));
573                 }
574
575                 if ((idfrom && idfrom->lib) || !editable)
576                         uiButSetFlag(but, UI_BUT_DISABLED);
577         }
578         
579         /* delete button */
580         if (id && (flag & UI_ID_DELETE) && (RNA_property_flag(template->prop) & PROP_NEVER_UNLINK) == 0) {
581                 if (unlinkop) {
582                         but = uiDefIconButO(block, BUT, unlinkop, WM_OP_INVOKE_REGION_WIN, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL);
583                         /* so we can access the template from operators, font unlinking needs this */
584                         uiButSetNFunc(but, NULL, MEM_dupallocN(template), NULL);
585                 }
586                 else {
587                         but = uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0, 0, 0, 0,
588                                            TIP_("Unlink datablock "
589                                                 "(Shift + Click to set users to zero, data will then not be saved)"));
590                         uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_DELETE));
591
592                         if (RNA_property_flag(template->prop) & PROP_NEVER_NULL)
593                                 uiButSetFlag(but, UI_BUT_DISABLED);
594                 }
595
596                 if ((idfrom && idfrom->lib) || !editable)
597                         uiButSetFlag(but, UI_BUT_DISABLED);
598         }
599
600         if (idcode == ID_TE)
601                 uiTemplateTextureShow(layout, C, &template->ptr, template->prop);
602         
603         uiBlockEndAlign(block);
604 }
605
606 static void ui_template_id(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
607                            const char *openop, const char *unlinkop, int flag, int prv_rows, int prv_cols)
608 {
609         TemplateID *template;
610         PropertyRNA *prop;
611         StructRNA *type;
612         short idcode;
613
614         prop = RNA_struct_find_property(ptr, propname);
615
616         if (!prop || RNA_property_type(prop) != PROP_POINTER) {
617                 RNA_warning("pointer property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
618                 return;
619         }
620
621         template = MEM_callocN(sizeof(TemplateID), "TemplateID");
622         template->ptr = *ptr;
623         template->prop = prop;
624         template->prv_rows = prv_rows;
625         template->prv_cols = prv_cols;
626
627         if (newop)
628                 flag |= UI_ID_ADD_NEW;
629         if (openop)
630                 flag |= UI_ID_OPEN;
631
632         type = RNA_property_pointer_type(ptr, prop);
633         idcode = RNA_type_to_ID_code(type);
634         template->idlb = which_libbase(CTX_data_main(C), idcode);
635         
636         /* create UI elements for this template
637          *      - template_ID makes a copy of the template data and assigns it to the relevant buttons
638          */
639         if (template->idlb) {
640                 uiLayoutRow(layout, TRUE);
641                 template_ID(C, layout, template, type, idcode, flag, newop, openop, unlinkop);
642         }
643
644         MEM_freeN(template);
645 }
646
647 void uiTemplateID(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
648                   const char *openop, const char *unlinkop)
649 {
650         ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop,
651                        UI_ID_BROWSE | UI_ID_RENAME | UI_ID_DELETE, 0, 0);
652 }
653
654 void uiTemplateIDBrowse(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
655                         const char *openop, const char *unlinkop)
656 {
657         ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop, UI_ID_BROWSE | UI_ID_RENAME, 0, 0);
658 }
659
660 void uiTemplateIDPreview(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
661                          const char *openop, const char *unlinkop, int rows, int cols)
662 {
663         ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop,
664                        UI_ID_BROWSE | UI_ID_RENAME | UI_ID_DELETE | UI_ID_PREVIEWS, rows, cols);
665 }
666
667 /************************ ID Chooser Template ***************************/
668
669 /* This is for selecting the type of ID-block to use, and then from the relevant type choosing the block to use 
670  *
671  * - propname: property identifier for property that ID-pointer gets stored to
672  * - proptypename: property identifier for property used to determine the type of ID-pointer that can be used
673  */
674 void uiTemplateAnyID(uiLayout *layout, PointerRNA *ptr, const char *propname, const char *proptypename,
675                      const char *text)
676 {
677         PropertyRNA *propID, *propType;
678         uiLayout *row;
679         
680         /* get properties... */
681         propID = RNA_struct_find_property(ptr, propname);
682         propType = RNA_struct_find_property(ptr, proptypename);
683
684         if (!propID || RNA_property_type(propID) != PROP_POINTER) {
685                 RNA_warning("pointer property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
686                 return;
687         }
688         if (!propType || RNA_property_type(propType) != PROP_ENUM) {
689                 RNA_warning("pointer-type property not found: %s.%s", RNA_struct_identifier(ptr->type), proptypename);
690                 return;
691         }
692         
693         /* Start drawing UI Elements using standard defines */
694         row = uiLayoutRow(layout, TRUE);
695         
696         /* Label - either use the provided text, or will become "ID-Block:" */
697         if (text) {
698                 if (text[0])
699                         uiItemL(row, text, ICON_NONE);
700         }
701         else
702                 uiItemL(row, "ID-Block:", ICON_NONE);
703         
704         /* ID-Type Selector - just have a menu of icons */
705         /* FIXME: the icon-only setting doesn't work when we supply a blank name */
706         uiItemFullR(row, ptr, propType, 0, 0, UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
707         
708         /* ID-Block Selector - just use pointer widget... */
709         uiItemFullR(row, ptr, propID, 0, 0, 0, "", ICON_NONE);
710 }
711
712 /********************* RNA Path Builder Template ********************/
713
714 /* ---------- */
715
716 /* This is creating/editing RNA-Paths 
717  *
718  * - ptr: struct which holds the path property
719  * - propname: property identifier for property that path gets stored to
720  * - root_ptr: struct that path gets built from
721  */
722 void uiTemplatePathBuilder(uiLayout *layout, PointerRNA *ptr, const char *propname, PointerRNA *UNUSED(root_ptr),
723                            const char *text)
724 {
725         PropertyRNA *propPath;
726         uiLayout *row;
727         
728         /* check that properties are valid */
729         propPath = RNA_struct_find_property(ptr, propname);
730         if (!propPath || RNA_property_type(propPath) != PROP_STRING) {
731                 RNA_warning("path property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
732                 return;
733         }
734         
735         /* Start drawing UI Elements using standard defines */
736         row = uiLayoutRow(layout, TRUE);
737         
738         /* Path (existing string) Widget */
739         uiItemR(row, ptr, propname, 0, text, ICON_RNA);
740         
741         /* TODO: attach something to this to make allow searching of nested properties to 'build' the path */
742 }
743
744 /************************ Modifier Template *************************/
745
746 #define ERROR_LIBDATA_MESSAGE "Can't edit external libdata"
747
748 static void modifiers_setOnCage(bContext *C, void *ob_v, void *md_v)
749 {
750         Scene *scene = CTX_data_scene(C);
751         Object *ob = ob_v;
752         ModifierData *md = md_v;
753         int i, cageIndex = modifiers_getCageIndex(scene, ob, NULL, 0);
754
755         /* undo button operation */
756         md->mode ^= eModifierMode_OnCage;
757
758         for (i = 0, md = ob->modifiers.first; md; ++i, md = md->next) {
759                 if (md == md_v) {
760                         if (i >= cageIndex)
761                                 md->mode ^= eModifierMode_OnCage;
762                         break;
763                 }
764         }
765
766         WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
767         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
768 }
769
770 static void modifiers_convertToReal(bContext *C, void *ob_v, void *md_v)
771 {
772         Object *ob = ob_v;
773         ModifierData *md = md_v;
774         ModifierData *nmd = modifier_new(md->type);
775
776         modifier_copyData(md, nmd);
777         nmd->mode &= ~eModifierMode_Virtual;
778
779         BLI_addhead(&ob->modifiers, nmd);
780         
781         modifier_unique_name(&ob->modifiers, nmd);
782
783         ob->partype = PAROBJECT;
784
785         WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
786         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
787
788         ED_undo_push(C, "Modifier convert to real");
789 }
790
791 static int modifier_can_delete(ModifierData *md)
792 {
793         /* fluid particle modifier can't be deleted here */
794         if (md->type == eModifierType_ParticleSystem)
795                 if (((ParticleSystemModifierData *)md)->psys->part->type == PART_FLUID)
796                         return 0;
797
798         return 1;
799 }
800
801 /* Check whether Modifier is a simulation or not, this is used for switching to the physics/particles context tab */
802 static int modifier_is_simulation(ModifierData *md)
803 {
804         /* Physic Tab */
805         if (ELEM7(md->type, eModifierType_Cloth, eModifierType_Collision, eModifierType_Fluidsim, eModifierType_Smoke,
806                   eModifierType_Softbody, eModifierType_Surface, eModifierType_DynamicPaint))
807         {
808                 return 1;
809         }
810         /* Particle Tab */
811         else if (md->type == eModifierType_ParticleSystem) {
812                 return 2;
813         }
814         else {
815                 return 0;
816         }
817 }
818
819 static uiLayout *draw_modifier(uiLayout *layout, Scene *scene, Object *ob,
820                                ModifierData *md, int index, int cageIndex, int lastCageIndex)
821 {
822         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
823         PointerRNA ptr;
824         uiBut *but;
825         uiBlock *block;
826         uiLayout *box, *column, *row;
827         uiLayout *result = NULL;
828         int isVirtual = (md->mode & eModifierMode_Virtual);
829         char str[128];
830
831         /* create RNA pointer */
832         RNA_pointer_create(&ob->id, &RNA_Modifier, md, &ptr);
833
834         column = uiLayoutColumn(layout, TRUE);
835         uiLayoutSetContextPointer(column, "modifier", &ptr);
836
837         /* rounded header ------------------------------------------------------------------- */
838         box = uiLayoutBox(column);
839         
840         if (isVirtual) {
841                 row = uiLayoutRow(box, FALSE);
842                 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_EXPAND);
843                 block = uiLayoutGetBlock(row);
844                 /* VIRTUAL MODIFIER */
845                 /* XXX this is not used now, since these cannot be accessed via RNA */
846                 BLI_snprintf(str, sizeof(str), "%s parent deform", md->name);
847                 uiDefBut(block, LABEL, 0, str, 0, 0, 185, UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, "Modifier name"); 
848                 
849                 but = uiDefBut(block, BUT, 0, IFACE_("Make Real"), 0, 0, 80, 16, NULL, 0.0, 0.0, 0.0, 0.0,
850                                TIP_("Convert virtual modifier to a real modifier"));
851                 uiButSetFunc(but, modifiers_convertToReal, ob, md);
852         }
853         else {
854                 /* REAL MODIFIER */
855                 row = uiLayoutRow(box, FALSE);
856                 block = uiLayoutGetBlock(row);
857                 
858                 uiBlockSetEmboss(block, UI_EMBOSSN);
859                 /* Open/Close .................................  */
860                 uiItemR(row, &ptr, "show_expanded", 0, "", ICON_NONE);
861                 
862                 /* modifier-type icon */
863                 uiItemL(row, "", RNA_struct_ui_icon(ptr.type));
864                 uiBlockSetEmboss(block, UI_EMBOSS);
865                 
866                 /* modifier name */
867                 uiItemR(row, &ptr, "name", 0, "", ICON_NONE);
868                 
869                 /* mode enabling buttons */
870                 uiBlockBeginAlign(block);
871                 /* Softbody not allowed in this situation, enforce! */
872                 if (((md->type != eModifierType_Softbody && md->type != eModifierType_Collision) || !(ob->pd && ob->pd->deflect)) &&
873                     (md->type != eModifierType_Surface) )
874                 {
875                         uiItemR(row, &ptr, "show_render", 0, "", ICON_NONE);
876                         uiItemR(row, &ptr, "show_viewport", 0, "", ICON_NONE);
877                         
878                         if (mti->flags & eModifierTypeFlag_SupportsEditmode)
879                                 uiItemR(row, &ptr, "show_in_editmode", 0, "", ICON_NONE);
880                 }
881
882                 if (ob->type == OB_MESH) {
883                         if (modifier_couldBeCage(scene, md) && (index <= lastCageIndex)) {
884                                 /* -- convert to rna ? */
885                                 but = uiDefIconButBitI(block, TOG, eModifierMode_OnCage, 0, ICON_MESH_DATA, 0, 0,
886                                                        UI_UNIT_X - 2, UI_UNIT_Y, &md->mode, 0.0, 0.0, 0.0, 0.0,
887                                                        TIP_("Apply modifier to editing cage during Edit mode"));
888                                 if (index < cageIndex)
889                                         uiButSetFlag(but, UI_BUT_DISABLED);
890                                 uiButSetFunc(but, modifiers_setOnCage, ob, md);
891                         }
892                         else {
893                                 uiBlockEndAlign(block);
894
895                                 /* place holder button */
896                                 uiBlockSetEmboss(block, UI_EMBOSSN);
897                                 but = uiDefIconBut(block, BUT, 0, ICON_NONE, 0, 0, UI_UNIT_X - 2, UI_UNIT_Y,
898                                                    NULL, 0.0, 0.0, 0.0, 0.0, NULL);
899                                 uiButSetFlag(but, UI_BUT_DISABLED);
900                                 uiBlockSetEmboss(block, UI_EMBOSS);
901                         }
902                 } /* tessellation point for curve-typed objects */
903                 else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
904                         /* some modifiers could work with pre-tessellated curves only */
905                         if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
906                                 /* add disabled pre-tessellated button, so users could have
907                                  * message for this modifiers */
908                                 but = uiDefIconButBitI(block, TOG, eModifierMode_ApplyOnSpline, 0, ICON_SURFACE_DATA, 0, 0,
909                                                        UI_UNIT_X - 2, UI_UNIT_Y, &md->mode, 0.0, 0.0, 0.0, 0.0,
910                                                        TIP_("This modifier could be applied on splines' points only"));
911                                 uiButSetFlag(but, UI_BUT_DISABLED);
912                         }
913                         else if (mti->type != eModifierTypeType_Constructive) {
914                                 /* constructive modifiers tessellates curve before applying */
915                                 uiItemR(row, &ptr, "use_apply_on_spline", 0, "", ICON_NONE);
916                         }
917                 }
918
919                 uiBlockEndAlign(block);
920                 
921                 /* Up/Down + Delete ........................... */
922                 uiBlockBeginAlign(block);
923                 uiItemO(row, "", ICON_TRIA_UP, "OBJECT_OT_modifier_move_up");
924                 uiItemO(row, "", ICON_TRIA_DOWN, "OBJECT_OT_modifier_move_down");
925                 uiBlockEndAlign(block);
926                 
927                 uiBlockSetEmboss(block, UI_EMBOSSN);
928                 /* When Modifier is a simulation, show button to switch to context rather than the delete button. */
929                 if (modifier_can_delete(md) && !modifier_is_simulation(md))
930                         uiItemO(row, "", ICON_X, "OBJECT_OT_modifier_remove");
931                 if (modifier_is_simulation(md) == 1)
932                         uiItemStringO(row, "", ICON_BUTS, "WM_OT_properties_context_change", "context", "PHYSICS");
933                 else if (modifier_is_simulation(md) == 2)
934                         uiItemStringO(row, "", ICON_BUTS, "WM_OT_properties_context_change", "context", "PARTICLES");
935                 uiBlockSetEmboss(block, UI_EMBOSS);
936         }
937
938         
939         /* modifier settings (under the header) --------------------------------------------------- */
940         if (!isVirtual && (md->mode & eModifierMode_Expanded)) {
941                 /* apply/convert/copy */
942                 box = uiLayoutBox(column);
943                 row = uiLayoutRow(box, FALSE);
944                 
945                 if (!ELEM(md->type, eModifierType_Collision, eModifierType_Surface)) {
946                         /* only here obdata, the rest of modifiers is ob level */
947                         uiBlockSetButLock(block, BKE_object_obdata_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
948                         
949                         if (md->type == eModifierType_ParticleSystem) {
950                                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
951                                 
952                                 if (!(ob->mode & OB_MODE_PARTICLE_EDIT) && psys->pathcache) {
953                                         if (ELEM(psys->part->ren_as, PART_DRAW_GR, PART_DRAW_OB))
954                                                 uiItemO(row, "Convert", ICON_NONE, "OBJECT_OT_duplicates_make_real");
955                                         else if (psys->part->ren_as == PART_DRAW_PATH)
956                                                 uiItemO(row, "Convert", ICON_NONE, "OBJECT_OT_modifier_convert");
957                                 }
958                         }
959                         else {
960                                 uiLayoutSetOperatorContext(row, WM_OP_INVOKE_DEFAULT);
961                                 uiItemEnumO(row, "OBJECT_OT_modifier_apply", IFACE_("Apply"), 0, "apply_as", MODIFIER_APPLY_DATA);
962                                 
963                                 if (modifier_isSameTopology(md) && !modifier_isNonGeometrical(md)) {
964                                         uiItemEnumO(row, "OBJECT_OT_modifier_apply", IFACE_("Apply as Shape Key"), 0,
965                                                     "apply_as", MODIFIER_APPLY_SHAPE);
966                                 }
967                         }
968                         
969                         uiBlockClearButLock(block);
970                         uiBlockSetButLock(block, ob && ob->id.lib, ERROR_LIBDATA_MESSAGE);
971                         
972                         if (!ELEM5(md->type, eModifierType_Fluidsim, eModifierType_Softbody, eModifierType_ParticleSystem,
973                                    eModifierType_Cloth, eModifierType_Smoke))
974                         {
975                                 uiItemO(row, IFACE_("Copy"), ICON_NONE, "OBJECT_OT_modifier_copy");
976                         }
977                 }
978                 
979                 /* result is the layout block inside the box, that we return so that modifier settings can be drawn */
980                 result = uiLayoutColumn(box, FALSE);
981                 block = uiLayoutAbsoluteBlock(box);
982         }
983         
984         /* error messages */
985         if (md->error) {
986                 box = uiLayoutBox(column);
987                 row = uiLayoutRow(box, FALSE);
988                 uiItemL(row, md->error, ICON_ERROR);
989         }
990         
991         return result;
992 }
993
994 uiLayout *uiTemplateModifier(uiLayout *layout, bContext *C, PointerRNA *ptr)
995 {
996         Scene *scene = CTX_data_scene(C);
997         Object *ob;
998         ModifierData *md, *vmd;
999         int i, lastCageIndex, cageIndex;
1000
1001         /* verify we have valid data */
1002         if (!RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
1003                 RNA_warning("Expected modifier on object");
1004                 return NULL;
1005         }
1006
1007         ob = ptr->id.data;
1008         md = ptr->data;
1009
1010         if (!ob || !(GS(ob->id.name) == ID_OB)) {
1011                 RNA_warning("Expected modifier on object");
1012                 return NULL;
1013         }
1014         
1015         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1016         
1017         /* find modifier and draw it */
1018         cageIndex = modifiers_getCageIndex(scene, ob, &lastCageIndex, 0);
1019
1020         /* XXX virtual modifiers are not accesible for python */
1021         vmd = modifiers_getVirtualModifierList(ob);
1022
1023         for (i = 0; vmd; i++, vmd = vmd->next) {
1024                 if (md == vmd)
1025                         return draw_modifier(layout, scene, ob, md, i, cageIndex, lastCageIndex);
1026                 else if (vmd->mode & eModifierMode_Virtual)
1027                         i--;
1028         }
1029
1030         return NULL;
1031 }
1032
1033 /************************ Constraint Template *************************/
1034
1035 #include "DNA_constraint_types.h"
1036
1037 #include "BKE_action.h"
1038 #include "BKE_constraint.h"
1039
1040 #define B_CONSTRAINT_TEST           5
1041 // #define B_CONSTRAINT_CHANGETARGET   6
1042
1043 static void do_constraint_panels(bContext *C, void *ob_pt, int event)
1044 {
1045         Object *ob = (Object *)ob_pt;
1046
1047         switch (event) {
1048                 case B_CONSTRAINT_TEST:
1049                         break; /* no handling */
1050 #if 0   /* UNUSED */
1051                 case B_CONSTRAINT_CHANGETARGET:
1052                 {
1053                         Main *bmain = CTX_data_main(C);
1054                         Scene *scene = CTX_data_scene(C);
1055                         if (ob->pose) ob->pose->flag |= POSE_RECALC;  /* checks & sorts pose channels */
1056                         DAG_scene_sort(bmain, scene);
1057                         break;
1058                 }
1059 #endif
1060                 default:
1061                         break;
1062         }
1063
1064         /* note: RNA updates now call this, commenting else it gets called twice.
1065          * if there are problems because of this, then rna needs changed update functions.
1066          *
1067          * object_test_constraints(ob);
1068          * if (ob->pose) BKE_pose_update_constraint_flags(ob->pose); */
1069         
1070         if (ob->type == OB_ARMATURE) DAG_id_tag_update(&ob->id, OB_RECALC_DATA | OB_RECALC_OB);
1071         else DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1072
1073         WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
1074 }
1075
1076 static void constraint_active_func(bContext *UNUSED(C), void *ob_v, void *con_v)
1077 {
1078         ED_object_constraint_set_active(ob_v, con_v);
1079 }
1080
1081 /* draw panel showing settings for a constraint */
1082 static uiLayout *draw_constraint(uiLayout *layout, Object *ob, bConstraint *con)
1083 {
1084         bPoseChannel *pchan = BKE_pose_channel_active(ob);
1085         bConstraintTypeInfo *cti;
1086         uiBlock *block;
1087         uiLayout *result = NULL, *col, *box, *row;
1088         PointerRNA ptr;
1089         char typestr[32];
1090         short proxy_protected, xco = 0, yco = 0;
1091         // int rb_col; // UNUSED
1092
1093         /* get constraint typeinfo */
1094         cti = BKE_constraint_get_typeinfo(con);
1095         if (cti == NULL) {
1096                 /* exception for 'Null' constraint - it doesn't have constraint typeinfo! */
1097                 BLI_strncpy(typestr, (con->type == CONSTRAINT_TYPE_NULL) ? "Null" : "Unknown", sizeof(typestr));
1098         }
1099         else
1100                 BLI_strncpy(typestr, cti->name, sizeof(typestr));
1101                 
1102         /* determine whether constraint is proxy protected or not */
1103         if (BKE_proxylocked_constraints_owner(ob, pchan))
1104                 proxy_protected = (con->flag & CONSTRAINT_PROXY_LOCAL) == 0;
1105         else
1106                 proxy_protected = 0;
1107
1108         /* unless button has own callback, it adds this callback to button */
1109         block = uiLayoutGetBlock(layout);
1110         uiBlockSetHandleFunc(block, do_constraint_panels, ob);
1111         uiBlockSetFunc(block, constraint_active_func, ob, con);
1112
1113         RNA_pointer_create(&ob->id, &RNA_Constraint, con, &ptr);
1114
1115         col = uiLayoutColumn(layout, TRUE);
1116         uiLayoutSetContextPointer(col, "constraint", &ptr);
1117
1118         box = uiLayoutBox(col);
1119         row = uiLayoutRow(box, FALSE);
1120         block = uiLayoutGetBlock(box);
1121
1122         /* Draw constraint header */
1123
1124         /* open/close */
1125         uiBlockSetEmboss(block, UI_EMBOSSN);
1126         uiItemR(row, &ptr, "show_expanded", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
1127         uiBlockSetEmboss(block, UI_EMBOSS);
1128         
1129         /* name */
1130         uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr, xco + 10, yco, 100, 18, NULL, 0.0, 0.0, 0.0, 0.0, "");
1131
1132         if (con->flag & CONSTRAINT_DISABLE)
1133                 uiLayoutSetRedAlert(row, TRUE);
1134         
1135         if (proxy_protected == 0) {
1136                 uiItemR(row, &ptr, "name", 0, "", ICON_NONE);
1137         }
1138         else
1139                 uiItemL(row, con->name, ICON_NONE);
1140         
1141         uiLayoutSetRedAlert(row, FALSE);
1142         
1143         /* proxy-protected constraints cannot be edited, so hide up/down + close buttons */
1144         if (proxy_protected) {
1145                 uiBlockSetEmboss(block, UI_EMBOSSN);
1146                 
1147                 /* draw a ghost icon (for proxy) and also a lock beside it, to show that constraint is "proxy locked" */
1148                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_GHOST, xco + 244, yco, 19, 19,
1149                              NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Proxy Protected"));
1150                 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_LOCKED, xco + 262, yco, 19, 19,
1151                              NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Proxy Protected"));
1152                 
1153                 uiBlockSetEmboss(block, UI_EMBOSS);
1154         }
1155         else {
1156                 short prev_proxylock, show_upbut, show_downbut;
1157                 
1158                 /* Up/Down buttons: 
1159                  *      Proxy-constraints are not allowed to occur after local (non-proxy) constraints
1160                  *      as that poses problems when restoring them, so disable the "up" button where
1161                  *      it may cause this situation. 
1162                  *
1163                  *  Up/Down buttons should only be shown (or not grayed - todo) if they serve some purpose.
1164                  */
1165                 if (BKE_proxylocked_constraints_owner(ob, pchan)) {
1166                         if (con->prev) {
1167                                 prev_proxylock = (con->prev->flag & CONSTRAINT_PROXY_LOCAL) ? 0 : 1;
1168                         }
1169                         else
1170                                 prev_proxylock = 0;
1171                 }
1172                 else
1173                         prev_proxylock = 0;
1174                         
1175                 show_upbut = ((prev_proxylock == 0) && (con->prev));
1176                 show_downbut = (con->next) ? 1 : 0;
1177                 
1178                 /* enabled */
1179                 uiBlockSetEmboss(block, UI_EMBOSSN);
1180                 uiItemR(row, &ptr, "mute", 0, "",
1181                         (con->flag & CONSTRAINT_OFF) ? ICON_RESTRICT_VIEW_ON : ICON_RESTRICT_VIEW_OFF);
1182                 uiBlockSetEmboss(block, UI_EMBOSS);
1183                 
1184                 uiLayoutSetOperatorContext(row, WM_OP_INVOKE_DEFAULT);
1185                 
1186                 /* up/down */
1187                 if (show_upbut || show_downbut) {
1188                         uiBlockBeginAlign(block);
1189                         if (show_upbut)
1190                                 uiItemO(row, "", ICON_TRIA_UP, "CONSTRAINT_OT_move_up");
1191                                 
1192                         if (show_downbut)
1193                                 uiItemO(row, "", ICON_TRIA_DOWN, "CONSTRAINT_OT_move_down");
1194                         uiBlockEndAlign(block);
1195                 }
1196                 
1197                 /* Close 'button' - emboss calls here disable drawing of 'button' behind X */
1198                 uiBlockSetEmboss(block, UI_EMBOSSN);
1199                 uiItemO(row, "", ICON_X, "CONSTRAINT_OT_delete");
1200                 uiBlockSetEmboss(block, UI_EMBOSS);
1201         }
1202
1203         /* Set but-locks for protected settings (magic numbers are used here!) */
1204         if (proxy_protected)
1205                 uiBlockSetButLock(block, 1, "Cannot edit Proxy-Protected Constraint");
1206
1207         /* Draw constraint data */
1208         if ((con->flag & CONSTRAINT_EXPAND) == 0) {
1209                 (yco) -= 21;
1210         }
1211         else {
1212                 box = uiLayoutBox(col);
1213                 block = uiLayoutAbsoluteBlock(box);
1214                 result = box;
1215         }
1216
1217         /* clear any locks set up for proxies/lib-linking */
1218         uiBlockClearButLock(block);
1219
1220         return result;
1221 }
1222
1223 uiLayout *uiTemplateConstraint(uiLayout *layout, PointerRNA *ptr)
1224 {
1225         Object *ob;
1226         bConstraint *con;
1227
1228         /* verify we have valid data */
1229         if (!RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
1230                 RNA_warning("Expected constraint on object");
1231                 return NULL;
1232         }
1233
1234         ob = ptr->id.data;
1235         con = ptr->data;
1236
1237         if (!ob || !(GS(ob->id.name) == ID_OB)) {
1238                 RNA_warning("Expected constraint on object");
1239                 return NULL;
1240         }
1241         
1242         uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1243
1244         /* hrms, the temporal constraint should not draw! */
1245         if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
1246                 bKinematicConstraint *data = con->data;
1247                 if (data->flag & CONSTRAINT_IK_TEMP)
1248                         return NULL;
1249         }
1250
1251         return draw_constraint(layout, ob, con);
1252 }
1253
1254
1255 /************************* Preview Template ***************************/
1256
1257 #include "DNA_lamp_types.h"
1258 #include "DNA_material_types.h"
1259 #include "DNA_world_types.h"
1260
1261 #define B_MATPRV 1
1262
1263 static void do_preview_buttons(bContext *C, void *arg, int event)
1264 {
1265         switch (event) {
1266                 case B_MATPRV:
1267                         WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING, arg);
1268                         break;
1269         }
1270 }
1271
1272 void uiTemplatePreview(uiLayout *layout, ID *id, int show_buttons, ID *parent, MTex *slot)
1273 {
1274         uiLayout *row, *col;
1275         uiBlock *block;
1276         Material *ma = NULL;
1277         Tex *tex = (Tex *)id;
1278         ID *pid, *pparent;
1279         short *pr_texture = NULL;
1280         PointerRNA material_ptr;
1281         PointerRNA texture_ptr;
1282
1283         if (id && !ELEM4(GS(id->name), ID_MA, ID_TE, ID_WO, ID_LA)) {
1284                 RNA_warning("Expected ID of type material, texture, lamp or world");
1285                 return;
1286         }
1287
1288         /* decide what to render */
1289         pid = id;
1290         pparent = NULL;
1291
1292         if (id && (GS(id->name) == ID_TE)) {
1293                 if (parent && (GS(parent->name) == ID_MA))
1294                         pr_texture = &((Material *)parent)->pr_texture;
1295                 else if (parent && (GS(parent->name) == ID_WO))
1296                         pr_texture = &((World *)parent)->pr_texture;
1297                 else if (parent && (GS(parent->name) == ID_LA))
1298                         pr_texture = &((Lamp *)parent)->pr_texture;
1299
1300                 if (pr_texture) {
1301                         if (*pr_texture == TEX_PR_OTHER)
1302                                 pid = parent;
1303                         else if (*pr_texture == TEX_PR_BOTH)
1304                                 pparent = parent;
1305                 }
1306         }
1307
1308         /* layout */
1309         block = uiLayoutGetBlock(layout);
1310         row = uiLayoutRow(layout, FALSE);
1311         col = uiLayoutColumn(row, FALSE);
1312         uiLayoutSetKeepAspect(col, TRUE);
1313         
1314         /* add preview */
1315         uiDefBut(block, BUT_EXTRA, 0, "", 0, 0, UI_UNIT_X * 6, UI_UNIT_Y * 6, pid, 0.0, 0.0, 0, 0, "");
1316         uiBlockSetDrawExtraFunc(block, ED_preview_draw, pparent, slot);
1317         uiBlockSetHandleFunc(block, do_preview_buttons, NULL);
1318         
1319         /* add buttons */
1320         if (pid && show_buttons) {
1321                 if (GS(pid->name) == ID_MA || (pparent && GS(pparent->name) == ID_MA)) {
1322                         if (GS(pid->name) == ID_MA) ma = (Material *)pid;
1323                         else ma = (Material *)pparent;
1324                         
1325                         /* Create RNA Pointer */
1326                         RNA_pointer_create(&ma->id, &RNA_Material, ma, &material_ptr);
1327
1328                         col = uiLayoutColumn(row, TRUE);
1329                         uiLayoutSetScaleX(col, 1.5);
1330                         uiItemR(col, &material_ptr, "preview_render_type", UI_ITEM_R_EXPAND, "", ICON_NONE);
1331                 }
1332
1333                 if (pr_texture) {
1334                         /* Create RNA Pointer */
1335                         RNA_pointer_create(id, &RNA_Texture, tex, &texture_ptr);
1336                         
1337                         uiLayoutRow(layout, TRUE);
1338                         uiDefButS(block, ROW, B_MATPRV, IFACE_("Texture"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1339                                   pr_texture, 10, TEX_PR_TEXTURE, 0, 0, "");
1340                         if (GS(parent->name) == ID_MA) {
1341                                 uiDefButS(block, ROW, B_MATPRV, IFACE_("Material"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1342                                           pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1343                         }
1344                         else if (GS(parent->name) == ID_LA) {
1345                                 uiDefButS(block, ROW, B_MATPRV, IFACE_("Lamp"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1346                                           pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1347                         }
1348                         else if (GS(parent->name) == ID_WO) {
1349                                 uiDefButS(block, ROW, B_MATPRV, IFACE_("World"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1350                                           pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1351                         }
1352                         uiDefButS(block, ROW, B_MATPRV, IFACE_("Both"),  0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1353                                   pr_texture, 10, TEX_PR_BOTH, 0, 0, "");
1354                         
1355                         /* Alpha button for texture preview */
1356                         if (*pr_texture != TEX_PR_OTHER) {
1357                                 row = uiLayoutRow(layout, FALSE);
1358                                 uiItemR(row, &texture_ptr, "use_preview_alpha", 0, NULL, ICON_NONE);
1359                         }
1360                 }
1361         }
1362 }
1363
1364 /********************** ColorRamp Template **************************/
1365
1366
1367 typedef struct RNAUpdateCb {
1368         PointerRNA ptr;
1369         PropertyRNA *prop;
1370 } RNAUpdateCb;
1371
1372 static void rna_update_cb(bContext *C, void *arg_cb, void *UNUSED(arg))
1373 {
1374         RNAUpdateCb *cb = (RNAUpdateCb *)arg_cb;
1375
1376         /* we call update here on the pointer property, this way the
1377          * owner of the curve mapping can still define it's own update
1378          * and notifier, even if the CurveMapping struct is shared. */
1379         RNA_property_update(C, &cb->ptr, cb->prop);
1380 }
1381
1382 static void colorband_add_cb(bContext *C, void *cb_v, void *coba_v)
1383 {
1384         ColorBand *coba = coba_v;
1385         float pos = 0.5f;
1386
1387         if (coba->tot > 1) {
1388                 if (coba->cur > 0) pos = (coba->data[coba->cur - 1].pos + coba->data[coba->cur].pos) * 0.5f;
1389                 else pos = (coba->data[coba->cur + 1].pos + coba->data[coba->cur].pos) * 0.5f;
1390         }
1391
1392         if (colorband_element_add(coba, pos)) {
1393                 rna_update_cb(C, cb_v, NULL);
1394                 ED_undo_push(C, "Add colorband");
1395         }
1396 }
1397
1398 static void colorband_del_cb(bContext *C, void *cb_v, void *coba_v)
1399 {
1400         ColorBand *coba = coba_v;
1401
1402         if (colorband_element_remove(coba, coba->cur)) {
1403                 ED_undo_push(C, "Delete colorband");
1404                 rna_update_cb(C, cb_v, NULL);
1405         }
1406 }
1407
1408 static void colorband_flip_cb(bContext *C, void *cb_v, void *coba_v)
1409 {
1410         CBData data_tmp[MAXCOLORBAND];
1411
1412         ColorBand *coba = coba_v;
1413         int a;
1414
1415         for (a = 0; a < coba->tot; a++) {
1416                 data_tmp[a] = coba->data[coba->tot - (a + 1)];
1417         }
1418         for (a = 0; a < coba->tot; a++) {
1419                 data_tmp[a].pos = 1.0f - data_tmp[a].pos;
1420                 coba->data[a] = data_tmp[a];
1421         }
1422
1423         /* may as well flip the cur*/
1424         coba->cur = coba->tot - (coba->cur + 1);
1425
1426         ED_undo_push(C, "Flip colorband");
1427
1428         rna_update_cb(C, cb_v, NULL);
1429 }
1430
1431 static void colorband_update_cb(bContext *UNUSED(C), void *bt_v, void *coba_v)
1432 {
1433         uiBut *bt = bt_v;
1434         ColorBand *coba = coba_v;
1435
1436         /* sneaky update here, we need to sort the colorband points to be in order,
1437          * however the RNA pointer then is wrong, so we update it */
1438         colorband_update_sort(coba);
1439         bt->rnapoin.data = coba->data + coba->cur;
1440 }
1441
1442 /* offset aligns from bottom, standard width 300, height 115 */
1443 static void colorband_buttons_large(uiLayout *layout, uiBlock *block, ColorBand *coba,
1444                                     int xoffs, int yoffs, RNAUpdateCb *cb)
1445 {
1446         uiBut *bt;
1447         uiLayout *row;
1448         const int line1_y = yoffs + 65 + UI_UNIT_Y + 2; /* 2 for some space between the buttons */
1449         const int line2_y = yoffs + 65;
1450
1451         if (coba == NULL) return;
1452         
1453         bt = uiDefBut(block, BUT, 0, IFACE_("Add"), 0 + xoffs, line1_y, 40, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1454                       TIP_("Add a new color stop to the colorband"));
1455         uiButSetNFunc(bt, colorband_add_cb, MEM_dupallocN(cb), coba);
1456
1457         bt = uiDefBut(block, BUT, 0, IFACE_("Delete"), 45 + xoffs, line1_y, 45, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1458                       TIP_("Delete the active position"));
1459         uiButSetNFunc(bt, colorband_del_cb, MEM_dupallocN(cb), coba);
1460
1461
1462         /* XXX, todo for later - convert to operator - campbell */
1463         bt = uiDefBut(block, BUT, 0, "F",        95 + xoffs, line1_y, 20, UI_UNIT_Y,
1464                       NULL, 0, 0, 0, 0, TIP_("Flip colorband"));
1465         uiButSetNFunc(bt, colorband_flip_cb, MEM_dupallocN(cb), coba);
1466
1467         uiDefButS(block, NUM, 0, "", 120 + xoffs, line1_y, 80, UI_UNIT_Y, &coba->cur, 0.0, (float)(MAX2(0, coba->tot - 1)),
1468                   0, 0, TIP_("Choose active color stop"));
1469
1470         bt = uiDefButS(block, MENU, 0, IFACE_("Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4"),
1471                        210 + xoffs, line1_y, 90, UI_UNIT_Y, &coba->ipotype, 0.0, 0.0, 0, 0,
1472                        TIP_("Set interpolation between color stops"));
1473         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1474         uiBlockEndAlign(block);
1475
1476         bt = uiDefBut(block, BUT_COLORBAND, 0, "",   xoffs, line2_y, 300, UI_UNIT_Y, coba, 0, 0, 0, 0, "");
1477         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1478
1479
1480
1481         if (coba->tot) {
1482                 CBData *cbd = coba->data + coba->cur;
1483
1484                 /* better to use rna so we can animate them */
1485                 PointerRNA ptr;
1486                 RNA_pointer_create(cb->ptr.id.data, &RNA_ColorRampElement, cbd, &ptr);
1487                 row = uiLayoutRow(layout, FALSE);
1488
1489                 uiItemR(row, &ptr, "position", 0, "Pos", ICON_NONE);
1490                 bt = block->buttons.last;
1491                 uiButSetFunc(bt, colorband_update_cb, bt, coba);
1492
1493                 uiItemR(row, &ptr, "color", 0, "", ICON_NONE);
1494         }
1495
1496 }
1497
1498 static void colorband_buttons_small(uiLayout *layout, uiBlock *block, ColorBand *coba, rctf *butr, RNAUpdateCb *cb)
1499 {
1500         uiBut *bt;
1501         float unit = BLI_rctf_size_x(butr) / 14.0f;
1502         float xs = butr->xmin;
1503
1504         uiBlockBeginAlign(block);
1505         bt = uiDefBut(block, BUT, 0, IFACE_("Add"), xs, butr->ymin + UI_UNIT_Y, 2.0f * unit, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1506                       TIP_("Add a new color stop to the colorband"));
1507         uiButSetNFunc(bt, colorband_add_cb, MEM_dupallocN(cb), coba);
1508         bt = uiDefBut(block, BUT, 0, IFACE_("Delete"), xs + 2.0f * unit, butr->ymin + UI_UNIT_Y, 1.5f * unit, UI_UNIT_Y,
1509                       NULL, 0, 0, 0, 0, TIP_("Delete the active position"));
1510         uiButSetNFunc(bt, colorband_del_cb, MEM_dupallocN(cb), coba);
1511         bt = uiDefBut(block, BUT, 0, "F", xs + 3.5f * unit, butr->ymin + UI_UNIT_Y, 0.5f * unit, UI_UNIT_Y,
1512                       NULL, 0, 0, 0, 0, TIP_("Flip the color ramp"));
1513         uiButSetNFunc(bt, colorband_flip_cb, MEM_dupallocN(cb), coba);
1514         uiBlockEndAlign(block);
1515
1516         if (coba->tot) {
1517                 CBData *cbd = coba->data + coba->cur;
1518                 PointerRNA ptr;
1519                 RNA_pointer_create(cb->ptr.id.data, &RNA_ColorRampElement, cbd, &ptr);
1520                 uiItemR(layout, &ptr, "color", 0, "", ICON_NONE);
1521         }
1522
1523         bt = uiDefButS(block, MENU, 0, IFACE_("Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4"),
1524                        xs + 10.0f * unit, butr->ymin + UI_UNIT_Y, unit * 4, UI_UNIT_Y, &coba->ipotype, 0.0, 0.0, 0, 0,
1525                        TIP_("Set interpolation between color stops"));
1526         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1527
1528         bt = uiDefBut(block, BUT_COLORBAND, 0, "", xs, butr->ymin, BLI_rctf_size_x(butr), UI_UNIT_Y, coba, 0, 0, 0, 0, "");
1529         uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1530
1531         uiBlockEndAlign(block);
1532 }
1533
1534 static void colorband_buttons_layout(uiLayout *layout, uiBlock *block, ColorBand *coba, rctf *butr,
1535                                      int small, RNAUpdateCb *cb)
1536 {
1537         if (small)
1538                 colorband_buttons_small(layout, block, coba, butr, cb);
1539         else
1540                 colorband_buttons_large(layout, block, coba, 0, 0, cb);
1541 }
1542
1543 void uiTemplateColorRamp(uiLayout *layout, PointerRNA *ptr, const char *propname, int expand)
1544 {
1545         PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1546         PointerRNA cptr;
1547         RNAUpdateCb *cb;
1548         uiBlock *block;
1549         rctf rect;
1550
1551         if (!prop || RNA_property_type(prop) != PROP_POINTER)
1552                 return;
1553
1554         cptr = RNA_property_pointer_get(ptr, prop);
1555         if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_ColorRamp))
1556                 return;
1557
1558         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1559         cb->ptr = *ptr;
1560         cb->prop = prop;
1561
1562         rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1563         rect.ymin = 0; rect.ymax = 19.5f * UI_UNIT_X;
1564
1565         block = uiLayoutAbsoluteBlock(layout);
1566         colorband_buttons_layout(layout, block, cptr.data, &rect, !expand, cb);
1567
1568         MEM_freeN(cb);
1569 }
1570
1571
1572 /********************* Icon viewer Template ************************/
1573
1574 /* ID Search browse menu, open */
1575 static uiBlock *icon_view_menu(bContext *C, ARegion *ar, void *arg_litem)
1576 {
1577         static RNAUpdateCb cb;
1578         uiBlock *block;
1579         uiBut *but;
1580         int icon;
1581         EnumPropertyItem *item;
1582         int a, free;
1583
1584         /* arg_litem is malloced, can be freed by parent button */
1585         cb = *((RNAUpdateCb *)arg_litem);
1586         
1587         icon = RNA_property_enum_get(&cb.ptr, cb.prop);
1588         
1589         block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1590         uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_REDRAW);
1591         
1592         
1593         RNA_property_enum_items(C, &cb.ptr, cb.prop, &item, NULL, &free);
1594         
1595         for (a = 0; item[a].identifier; a++) {
1596                 int x, y;
1597                 
1598                 x = (a % 8) * UI_UNIT_X * 6;
1599                 y = (a / 8) * UI_UNIT_X * 6;
1600                 
1601                 icon = item[a].icon;
1602                 but = uiDefIconButR_prop(block, ROW, 0, icon, x, y, UI_UNIT_X * 6, UI_UNIT_Y * 6, &cb.ptr, cb.prop, -1, 0, icon, -1, -1, NULL);
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 <= UI_UNIT_Y) ? UI_UNIT_Y : 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 <= UI_UNIT_Y) ? UI_UNIT_Y : 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 <= UI_UNIT_Y) ? UI_UNIT_Y : 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, "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         block = uiLayoutGetBlock(layout);
2489
2490         if (!active_dataptr->data) {
2491                 RNA_warning("No active data");
2492                 return;
2493         }
2494
2495         if (dataptr->data) {
2496                 prop = RNA_struct_find_property(dataptr, propname);
2497                 if (!prop) {
2498                         RNA_warning("Property not found: %s.%s", RNA_struct_identifier(dataptr->type), propname);
2499                         return;
2500                 }
2501         }
2502
2503         activeprop = RNA_struct_find_property(active_dataptr, active_propname);
2504         if (!activeprop) {
2505                 RNA_warning("Property not found: %s.%s", RNA_struct_identifier(active_dataptr->type), active_propname);
2506                 return;
2507         }
2508
2509         if (prop) {
2510                 type = RNA_property_type(prop);
2511                 if (type != PROP_COLLECTION) {
2512                         RNA_warning("Expected a collection data property");
2513                         return;
2514                 }
2515         }
2516
2517         activetype = RNA_property_type(activeprop);
2518         if (activetype != PROP_INT) {
2519                 RNA_warning("Expected an integer active data property");
2520                 return;
2521         }
2522
2523         /* get icon */
2524         if (dataptr->data && prop) {
2525                 ptype = RNA_property_pointer_type(dataptr, prop);
2526                 rnaicon = RNA_struct_ui_icon(ptype);
2527         }
2528
2529         /* get active data */
2530         activei = RNA_property_int_get(active_dataptr, activeprop);
2531
2532         /* Find the uiList type. */
2533         ui_list_type = WM_uilisttype_find(listtype_name, FALSE);
2534
2535         if (ui_list_type == NULL) {
2536                 RNA_warning("List type %s not found", listtype_name);
2537                 return;
2538                         }
2539
2540         draw_item = ui_list_type->draw_item ? ui_list_type->draw_item : uilist_draw_item_default;
2541
2542         /* Find or add the uiList to the current Region. */
2543         /* We tag the list id with the list type... */
2544         BLI_snprintf(ui_list_id, sizeof(ui_list_id), "%s_%s", ui_list_type->idname, list_id ? list_id : "");
2545
2546         ar = CTX_wm_region(C);
2547         ui_list = BLI_findstring(&ar->ui_lists, ui_list_id, offsetof(uiList, list_id));
2548
2549         if (!ui_list) {
2550                 ui_list = MEM_callocN(sizeof(uiList), __func__);
2551                 BLI_strncpy(ui_list->list_id, ui_list_id, sizeof(ui_list->list_id));
2552                 BLI_addtail(&ar->ui_lists, ui_list);
2553                                         }
2554
2555         /* Because we can't actually pass type across save&load... */
2556         ui_list->type = ui_list_type;
2557         ui_list->layout_type = layout_type;
2558
2559         switch (layout_type) {
2560         case UILST_LAYOUT_DEFAULT:
2561                 /* default rows */
2562                 if (rows == 0)
2563                         rows = 5;
2564                 if (maxrows == 0)
2565                         maxrows = 5;
2566                 if (ui_list->list_grip_size != 0)
2567                         rows = ui_list->list_grip_size;
2568
2569                 /* layout */
2570                 box = uiLayoutListBox(layout, ui_list, dataptr, prop, active_dataptr, activeprop);
2571                 row = uiLayoutRow(box, FALSE);
2572                 col = uiLayoutColumn(row, TRUE);
2573
2574                 /* init numbers */
2575                 RNA_property_int_range(active_dataptr, activeprop, &min, &max);
2576
2577                 if (prop)
2578                         len = RNA_property_collection_length(dataptr, prop);
2579                 items = CLAMPIS(len, rows, MAX2(rows, maxrows));
2580
2581                 /* if list length changes and active is out of view, scroll to it */
2582                 if ((ui_list->list_last_len != len) &&
2583                     (activei < ui_list->list_scroll || activei >= ui_list->list_scroll + items)) {
2584                         ui_list->list_scroll = activei;
2585                 }
2586
2587                 ui_list->list_scroll = min_ii(ui_list->list_scroll, len - items);
2588                 ui_list->list_scroll = max_ii(ui_list->list_scroll, 0);
2589                 ui_list->list_size = items;
2590                 ui_list->list_last_len = len;
2591
2592                 if (dataptr->data && prop) {
2593                         /* create list items */
2594                         RNA_PROP_BEGIN (dataptr, itemptr, prop)
2595                         {
2596                                 if (i >= ui_list->list_scroll && i < ui_list->list_scroll + items) {
2597                                         subblock = uiLayoutGetBlock(col);
2598                                         overlap = uiLayoutOverlap(col);
2599
2600                                         /* list item behind label & other buttons */
2601                                         sub = uiLayoutRow(overlap, FALSE);
2602
2603                                         but = uiDefButR_prop(subblock, LISTROW, 0, "", 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
2604                                                              active_dataptr, activeprop, 0, 0, i, 0, 0, "");
2605                                         uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2606
2607                                         sub = uiLayoutRow(overlap, FALSE);
2608
2609                                         icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, FALSE);
2610                                         if (icon == ICON_DOT)
2611                                                 icon = ICON_NONE;
2612                                         draw_item(ui_list, C, sub, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2613                                 }
2614                                 i++;
2615                         }
2616                         RNA_PROP_END;
2617                 }
2618
2619                 /* add dummy buttons to fill space */
2620                 while (i < ui_list->list_scroll + items) {
2621                         if (i >= ui_list->list_scroll)
2622                                 uiItemL(col, "", ICON_NONE);
2623                         i++;
2624                 }
2625
2626                 /* add scrollbar */
2627                 if (len > items) {
2628                         col = uiLayoutColumn(row, FALSE);
2629                         uiDefButI(block, SCROLL, 0, "", 0, 0, UI_UNIT_X * 0.75, UI_UNIT_Y * items, &ui_list->list_scroll,
2630                                   0, len - items, items, 0, "");
2631                 }
2632                 break;
2633         case UILST_LAYOUT_COMPACT:
2634                 row = uiLayoutRow(layout, TRUE);
2635
2636                 if (dataptr->data && prop) {
2637                         /* create list items */
2638                         RNA_PROP_BEGIN (dataptr, itemptr, prop)
2639                         {
2640                                 found = (activei == i);
2641
2642                                 if (found) {
2643                                         icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, FALSE);
2644                                         if (icon == ICON_DOT)
2645                                                 icon = ICON_NONE;
2646                                         draw_item(ui_list, C, row, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2647         }
2648
2649                                 i++;
2650 }
2651                         RNA_PROP_END;
2652                 }
2653
2654                 /* if list is empty, add in dummy button */
2655                 if (i == 0)
2656                         uiItemL(row, "", ICON_NONE);
2657
2658                 /* next/prev button */
2659                 BLI_snprintf(numstr, sizeof(numstr), "%d :", i);
2660                 but = uiDefIconTextButR_prop(block, NUM, 0, 0, numstr, 0, 0, UI_UNIT_X * 5, UI_UNIT_Y,
2661                                              active_dataptr, activeprop, 0, 0, 0, 0, 0, "");
2662                 if (i == 0)
2663                         uiButSetFlag(but, UI_BUT_DISABLED);
2664                 break;
2665         case UILST_LAYOUT_GRID:
2666                 box = uiLayoutListBox(layout, ui_list, dataptr, prop, active_dataptr, activeprop);
2667                 col = uiLayoutColumn(box, TRUE);
2668                 row = uiLayoutRow(col, FALSE);
2669
2670                 if (dataptr->data && prop) {
2671                         /* create list items */
2672                         RNA_PROP_BEGIN (dataptr, itemptr, prop)
2673                         {
2674                                 /* create button */
2675                                 if (!(i % 9))
2676                                         row = uiLayoutRow(col, FALSE);
2677
2678                                 subblock = uiLayoutGetBlock(row);
2679                                 overlap = uiLayoutOverlap(row);
2680
2681                                 /* list item behind label & other buttons */
2682                                 sub = uiLayoutRow(overlap, FALSE);
2683
2684                                 but = uiDefButR_prop(subblock, LISTROW, 0, "", 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
2685                                                      active_dataptr, activeprop, 0, 0, i, 0, 0, "");
2686                                 uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2687
2688                                 sub = uiLayoutRow(overlap, FALSE);
2689
2690                                 icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, FALSE);
2691                                 draw_item(ui_list, C, sub, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2692
2693                                 i++;
2694                         }
2695                         RNA_PROP_END;
2696                 }
2697                 break;
2698         }
2699 }
2700
2701 /************************* Operator Search Template **************************/
2702
2703 static void operator_call_cb(bContext *C, void *UNUSED(arg1), void *arg2)
2704 {
2705         wmOperatorType *ot = arg2;
2706         
2707         if (ot)
2708                 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
2709 }
2710
2711 static void operator_search_cb(const bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
2712 {
2713         GHashIterator *iter = WM_operatortype_iter();
2714
2715         for (; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
2716                 wmOperatorType *ot = BLI_ghashIterator_getValue(iter);
2717
2718                 if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0)
2719                         continue;
2720
2721                 if (BLI_strcasestr(ot->name, str)) {
2722                         if (WM_operator_poll((bContext *)C, ot)) {
2723                                 char name[256];
2724                                 int len = strlen(ot->name);
2725                                 
2726                                 /* display name for menu, can hold hotkey */
2727                                 BLI_strncpy(name, ot->name, sizeof(name));
2728                                 
2729                                 /* check for hotkey */
2730                                 if (len < sizeof(name) - 6) {
2731                                         if (WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, TRUE,
2732                                                                          &name[len + 1], sizeof(name) - len - 1))
2733                                         {
2734                                                 name[len] = '|';
2735                                         }
2736                                 }
2737                                 
2738                                 if (0 == uiSearchItemAdd(items, name, ot, 0))
2739                                         break;
2740                         }
2741                 }
2742         }
2743         BLI_ghashIterator_free(iter);
2744 }
2745
2746 void uiOperatorSearch_But(uiBut *but)
2747 {
2748         uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
2749 }
2750
2751 void uiTemplateOperatorSearch(uiLayout *layout)
2752 {
2753         uiBlock *block;
2754         uiBut *but;
2755         static char search[256] = "";
2756                 
2757         block = uiLayoutGetBlock(layout);
2758         uiBlockSetCurLayout(block, layout);
2759
2760         but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, 0, 0, "");
2761         uiOperatorSearch_But(but);
2762 }
2763
2764 /************************* Running Jobs Template **************************/
2765
2766 #define B_STOPRENDER    1
2767 #define B_STOPCAST      2
2768 #define B_STOPANIM      3
2769 #define B_STOPCOMPO     4
2770 #define B_STOPSEQ       5
2771 #define B_STOPCLIP      6
2772 #define B_STOPOTHER     7
2773
2774 static void do_running_jobs(bContext *C, void *UNUSED(arg), int event)
2775 {
2776         switch (event) {
2777                 case B_STOPRENDER:
2778                         G.is_break = TRUE;
2779                         break;
2780                 case B_STOPCAST:
2781                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_screen(C), NULL);
2782                         break;
2783                 case B_STOPANIM:
2784                         WM_operator_name_call(C, "SCREEN_OT_animation_play", WM_OP_INVOKE_SCREEN, NULL);
2785                         break;
2786                 case B_STOPCOMPO:
2787                         WM_jobs_stop(CTX_wm_manager(C), CTX_data_scene(C), NULL);
2788                         break;
2789                 case B_STOPSEQ:
2790                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2791                         break;
2792                 case B_STOPCLIP:
2793                         WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2794                         break;
2795                 case B_STOPOTHER:
2796                         G.is_break = TRUE;
2797                         break;
2798         }
2799 }
2800
2801 void uiTemplateRunningJobs(uiLayout *layout, bContext *C)
2802 {
2803         bScreen *screen = CTX_wm_screen(C);
2804         wmWindowManager *wm = CTX_wm_manager(C);
2805         ScrArea *sa = CTX_wm_area(C);
2806         uiBlock *block;
2807         void *owner = NULL;
2808         int handle_event;
2809         
2810         block = uiLayoutGetBlock(layout);
2811         uiBlockSetCurLayout(block, layout);
2812
2813         uiBlockSetHandleFunc(block, do_running_jobs, NULL);
2814
2815         if (sa->spacetype == SPACE_SEQ) {
2816                 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2817                         owner = sa;
2818                 handle_event = B_STOPSEQ;
2819         }
2820         else if (sa->spacetype == SPACE_CLIP) {
2821                 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2822                         owner = sa;
2823                 handle_event = B_STOPCLIP;
2824         }
2825         else {
2826                 Scene *scene;
2827                 /* another scene can be rendering too, for example via compositor */
2828                 for (scene = CTX_data_main(C)->scene.first; scene; scene = scene->id.next) {
2829                         if (WM_jobs_test(wm, scene, WM_JOB_TYPE_RENDER)) {
2830                                 handle_event = B_STOPRENDER;
2831                                 break;
2832                         }
2833                         else if (WM_jobs_test(wm, scene, WM_JOB_TYPE_COMPOSITE)) {
2834                                 handle_event = B_STOPCOMPO;
2835                                 break;
2836                         }
2837                         else if (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY)) {
2838                                 handle_event = B_STOPOTHER;
2839                                 break;
2840                         }
2841                 }
2842                 owner = scene;
2843         }
2844
2845         if (owner) {
2846                 uiLayout *ui_abs;
2847                 
2848                 ui_abs = uiLayoutAbsolute(layout, FALSE);
2849                 (void)ui_abs;  /* UNUSED */
2850                 
2851                 uiDefIconBut(block, BUT, handle_event, ICON_PANEL_CLOSE, 0, UI_UNIT_Y * 0.1, UI_UNIT_X * 0.8, UI_UNIT_Y * 0.8,
2852                              NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop this job"));
2853                 uiDefBut(block, PROGRESSBAR, 0, WM_jobs_name(wm, owner), 
2854                          UI_UNIT_X, 0, 100, UI_UNIT_Y, NULL, 0.0f, 0.0f, WM_jobs_progress(wm, owner), 0, TIP_("Progress"));
2855                 
2856                 uiLayoutRow(layout, FALSE);
2857         }
2858         if (WM_jobs_test(wm, screen, WM_JOB_TYPE_SCREENCAST))
2859                 uiDefIconTextBut(block, BUT, B_STOPCAST, ICON_CANCEL, IFACE_("Capture"), 0, 0, 85, UI_UNIT_Y,
2860                                  NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop screencast"));
2861         if (screen->animtimer)
2862                 uiDefIconTextBut(block, BUT, B_STOPANIM, ICON_CANCEL, IFACE_("Anim Player"), 0, 0, 100, UI_UNIT_Y,
2863                                  NULL, 0.0f, 0.0f, 0, 0, TIP_("Stop animation playback"));
2864 }
2865
2866 /************************* Reports for Last Operator Template **************************/
2867
2868 void uiTemplateReportsBanner(uiLayout *layout, bContext *C)
2869 {
2870         ReportList *reports = CTX_wm_reports(C);
2871         Report *report = BKE_reports_last_displayable(reports);
2872         ReportTimerInfo *rti;
2873         
2874         uiLayout *ui_abs;
2875         uiBlock *block;
2876         uiBut *but;
2877         uiStyle *style = UI_GetStyle();
2878         int width;
2879         int icon = 0;
2880         
2881         /* if the report display has timed out, don't show */
2882         if (!reports->reporttimer) return;
2883         
2884         rti = (ReportTimerInfo *)reports->reporttimer->customdata;
2885         
2886         if (!rti || rti->widthfac == 0.0f || !report) return;
2887         
2888         ui_abs = uiLayoutAbsolute(layout, FALSE);
2889         block = uiLayoutGetBlock(ui_abs);
2890         
2891         width = BLF_width(style->widget.uifont_id, report->message);
2892         width = min_ii((int)(rti->widthfac * width), width);
2893         width = max_ii(width, 10);
2894         
2895         /* make a box around the report to make it stand out */
2896         uiBlockBeginAlign(block);
2897         but = uiDefBut(block, ROUNDBOX, 0, "", 0, 0, UI_UNIT_X + 10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "");
2898         /* set the report's bg color in but->col - ROUNDBOX feature */
2899         rgb_float_to_uchar(but->col, rti->col);
2900         but->col[3] = 255;
2901
2902         but = uiDefBut(block, ROUNDBOX, 0, "", UI_UNIT_X + 10, 0, UI_UNIT_X + width, UI_UNIT_Y,
2903                        NULL, 0.0f, 0.0f, 0, 0, "");
2904         but->col[0] = but->col[1] = but->col[2] = FTOCHAR(rti->grayscale);
2905         but->col[3] = 255;
2906
2907         uiBlockEndAlign(block);
2908         
2909         
2910         /* icon and report message on top */
2911         if (report->type & RPT_ERROR_ALL)
2912                 icon = ICON_ERROR;
2913         else if (report->type & RPT_WARNING_ALL)
2914                 icon = ICON_ERROR;
2915         else if (report->type & RPT_INFO_ALL)
2916                 icon = ICON_INFO;
2917         
2918         /* XXX: temporary operator to dump all reports to a text block, but only if more than 1 report 
2919          * to be shown instead of icon when appropriate...
2920          */