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