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