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