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