3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * Contributor(s): Blender Foundation 2009.
21 * ***** END GPL LICENSE BLOCK *****
24 /** \file blender/editors/interface/interface_templates.c
25 * \ingroup edinterface
33 #include "MEM_guardedalloc.h"
35 #include "DNA_dynamicpaint_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_object_force.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_string.h"
43 #include "BLI_ghash.h"
46 #include "BLI_listbase.h"
49 #include "BLF_translation.h"
51 #include "BKE_animsys.h"
52 #include "BKE_colortools.h"
53 #include "BKE_context.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_displist.h"
56 #include "BKE_dynamicpaint.h"
57 #include "BKE_global.h"
58 #include "BKE_library.h"
60 #include "BKE_material.h"
61 #include "BKE_modifier.h"
63 #include "BKE_object.h"
64 #include "BKE_packedFile.h"
65 #include "BKE_particle.h"
66 #include "BKE_report.h"
68 #include "BKE_scene.h"
69 #include "BKE_screen.h"
70 #include "BKE_texture.h"
72 #include "ED_screen.h"
73 #include "ED_object.h"
74 #include "ED_render.h"
77 #include "RNA_access.h"
78 #include "RNA_enum_types.h"
83 #include "UI_interface.h"
84 #include "UI_interface_icons.h"
85 #include "interface_intern.h"
87 void UI_template_fix_linking(void)
91 /********************** Header Template *************************/
93 void uiTemplateHeader(uiLayout *layout, bContext *C, int menus)
97 block = uiLayoutAbsoluteBlock(layout);
98 if (menus) ED_area_header_standardbuttons(C, block, 0);
99 else ED_area_header_switchbutton(C, block, 0);
102 /********************** Search Callbacks *************************/
104 typedef struct TemplateID {
109 int prv_rows, prv_cols;
113 /* Search browse menu, assign */
114 static void id_search_call_cb(bContext *C, void *arg_template, void *item)
116 TemplateID *template = (TemplateID *)arg_template;
122 RNA_id_pointer_create(item, &idptr);
123 RNA_property_pointer_set(&template->ptr, template->prop, idptr);
124 RNA_property_update(C, &template->ptr, template->prop);
128 /* ID Search browse menu, do the search */
129 static void id_search_cb(const bContext *C, void *arg_template, const char *str, uiSearchItems *items)
131 TemplateID *template = (TemplateID *)arg_template;
132 ListBase *lb = template->idlb;
133 ID *id, *id_from = template->ptr.id.data;
135 int flag = RNA_property_flag(template->prop);
138 for (id = lb->first; id; id = id->next) {
139 if (!((flag & PROP_ID_SELF_CHECK) && id == id_from)) {
142 if (RNA_property_type(template->prop) == PROP_POINTER) {
144 RNA_id_pointer_create(id, &ptr);
145 if (RNA_property_pointer_poll(&template->ptr, template->prop, &ptr) == 0)
149 /* hide dot-datablocks, but only if filter does not force it visible */
150 if (U.uiflag & USER_HIDE_DOT)
151 if ((id->name[2] == '.') && (str[0] != '.'))
154 if (*str == '\0' || BLI_strcasestr(id->name + 2, str)) {
155 /* +1 is needed because name_uiprefix_id used 3 letter prefix
156 * followed by ID_NAME-2 characters from id->name
158 char name_ui[MAX_ID_NAME + 1];
159 name_uiprefix_id(name_ui, id);
161 iconid = ui_id_icon_get((bContext *)C, id, template->preview);
163 if (false == uiSearchItemAdd(items, name_ui, id, iconid))
170 /* ID Search browse menu, open */
171 static uiBlock *id_search_menu(bContext *C, ARegion *ar, void *arg_litem)
173 static char search[256];
174 static TemplateID template;
176 wmWindow *win = CTX_wm_window(C);
180 /* clear initial search string, then all items show */
182 /* arg_litem is malloced, can be freed by parent button */
183 template = *((TemplateID *)arg_litem);
185 /* get active id for showing first item */
186 idptr = RNA_property_pointer_get(&template.ptr, template.prop);
188 block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
189 uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_REDRAW | UI_BLOCK_SEARCH_MENU);
191 /* preview thumbnails */
192 if (template.prv_rows > 0 && template.prv_cols > 0) {
193 int w = 4 * U.widget_unit * template.prv_cols;
194 int h = 4 * U.widget_unit * template.prv_rows + U.widget_unit;
196 /* fake button, it holds space for search items */
197 uiDefBut(block, LABEL, 0, "", 10, 15, w, h, NULL, 0, 0, 0, 0, NULL);
199 but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, w, UI_UNIT_Y,
200 template.prv_rows, template.prv_cols, "");
201 uiButSetSearchFunc(but, id_search_cb, &template, id_search_call_cb, idptr.data);
205 const int searchbox_width = uiSearchBoxWidth();
206 const int searchbox_height = uiSearchBoxHeight();
208 /* fake button, it holds space for search items */
209 uiDefBut(block, LABEL, 0, "", 10, 15, searchbox_width, searchbox_height, NULL, 0, 0, 0, 0, NULL);
210 but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, searchbox_width, UI_UNIT_Y - 1, 0, 0, "");
211 uiButSetSearchFunc(but, id_search_cb, &template, id_search_call_cb, idptr.data);
215 uiBoundsBlock(block, 0.3f * U.widget_unit);
216 uiBlockSetDirection(block, UI_DOWN);
217 uiEndBlock(C, block);
219 /* give search-field focus */
220 uiButSetFocusOnEnter(win, but);
221 /* this type of search menu requires undo */
222 but->flag |= UI_BUT_UNDO;
227 /************************ ID Template ***************************/
228 /* This is for browsing and editing the ID-blocks used */
230 /* for new/open operators */
231 void uiIDContextProperty(bContext *C, PointerRNA *ptr, PropertyRNA **prop)
233 TemplateID *template;
234 ARegion *ar = CTX_wm_region(C);
238 memset(ptr, 0, sizeof(*ptr));
244 for (block = ar->uiblocks.first; block; block = block->next) {
245 for (but = block->buttons.first; but; but = but->next) {
246 /* find the button before the active one */
247 if ((but->flag & (UI_BUT_LAST_ACTIVE | UI_ACTIVE))) {
248 if (but->func_argN) {
249 template = but->func_argN;
250 *ptr = template->ptr;
251 *prop = template->prop;
260 static void template_id_cb(bContext *C, void *arg_litem, void *arg_event)
262 TemplateID *template = (TemplateID *)arg_litem;
263 PointerRNA idptr = RNA_property_pointer_get(&template->ptr, template->prop);
265 int event = GET_INT_FROM_POINTER(arg_event);
270 RNA_warning("warning, id event %d shouldnt come here", event);
274 /* these call uiIDContextProperty */
277 memset(&idptr, 0, sizeof(idptr));
278 RNA_property_pointer_set(&template->ptr, template->prop, idptr);
279 RNA_property_update(C, &template->ptr, template->prop);
281 if (id && CTX_wm_window(C)->eventstate->shift) /* useful hidden functionality, */
285 case UI_ID_FAKE_USER:
287 if (id->flag & LIB_FAKEUSER) id_us_plus(id);
296 if (id_make_local(id, false)) {
297 /* reassign to get get proper updates/notifiers */
298 idptr = RNA_property_pointer_get(&template->ptr, template->prop);
299 RNA_property_pointer_set(&template->ptr, template->prop, idptr);
300 RNA_property_update(C, &template->ptr, template->prop);
306 const int do_scene_obj = (GS(id->name) == ID_OB) &&
307 (template->ptr.type == &RNA_SceneObjects);
311 Main *bmain = CTX_data_main(C);
312 Scene *scene = CTX_data_scene(C);
313 ED_object_single_user(bmain, scene, (struct Object *)id);
314 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
318 id_single_user(C, id, &template->ptr, template->prop);
324 case UI_ID_AUTO_NAME:
330 static const char *template_id_browse_tip(StructRNA *type)
333 switch (RNA_type_to_ID_code(type)) {
334 case ID_SCE: return N_("Browse Scene to be linked");
335 case ID_OB: return N_("Browse Object to be linked");
336 case ID_ME: return N_("Browse Mesh Data to be linked");
337 case ID_CU: return N_("Browse Curve Data to be linked");
338 case ID_MB: return N_("Browse Metaball Data to be linked");
339 case ID_MA: return N_("Browse Material to be linked");
340 case ID_TE: return N_("Browse Texture to be linked");
341 case ID_IM: return N_("Browse Image to be linked");
342 case ID_LS: return N_("Browse Line Style Data to be linked");
343 case ID_LT: return N_("Browse Lattice Data to be linked");
344 case ID_LA: return N_("Browse Lamp Data to be linked");
345 case ID_CA: return N_("Browse Camera Data to be linked");
346 case ID_WO: return N_("Browse World Settings to be linked");
347 case ID_SCR: return N_("Choose Screen lay-out");
348 case ID_TXT: return N_("Browse Text to be linked");
349 case ID_SPK: return N_("Browse Speaker Data to be linked");
350 case ID_SO: return N_("Browse Sound to be linked");
351 case ID_AR: return N_("Browse Armature data to be linked");
352 case ID_AC: return N_("Browse Action to be linked");
353 case ID_NT: return N_("Browse Node Tree to be linked");
354 case ID_BR: return N_("Browse Brush to be linked");
355 case ID_PA: return N_("Browse Particle System to be linked");
356 case ID_GD: return N_("Browse Grease Pencil Data to be linked");
359 return N_("Browse ID data to be linked");
362 /* Return a type-based i18n context, needed e.g. by "New" button.
363 * In most languages, this adjective takes different form based on gender of type name...
365 #ifdef WITH_INTERNATIONAL
366 static const char *template_id_context(StructRNA *type)
369 switch (RNA_type_to_ID_code(type)) {
370 case ID_SCE: return BLF_I18NCONTEXT_ID_SCENE;
371 case ID_OB: return BLF_I18NCONTEXT_ID_OBJECT;
372 case ID_ME: return BLF_I18NCONTEXT_ID_MESH;
373 case ID_CU: return BLF_I18NCONTEXT_ID_CURVE;
374 case ID_MB: return BLF_I18NCONTEXT_ID_METABALL;
375 case ID_MA: return BLF_I18NCONTEXT_ID_MATERIAL;
376 case ID_TE: return BLF_I18NCONTEXT_ID_TEXTURE;
377 case ID_IM: return BLF_I18NCONTEXT_ID_IMAGE;
378 case ID_LS: return BLF_I18NCONTEXT_ID_FREESTYLELINESTYLE;
379 case ID_LT: return BLF_I18NCONTEXT_ID_LATTICE;
380 case ID_LA: return BLF_I18NCONTEXT_ID_LAMP;
381 case ID_CA: return BLF_I18NCONTEXT_ID_CAMERA;
382 case ID_WO: return BLF_I18NCONTEXT_ID_WORLD;
383 case ID_SCR: return BLF_I18NCONTEXT_ID_SCREEN;
384 case ID_TXT: return BLF_I18NCONTEXT_ID_TEXT;
385 case ID_SPK: return BLF_I18NCONTEXT_ID_SPEAKER;
386 case ID_SO: return BLF_I18NCONTEXT_ID_SOUND;
387 case ID_AR: return BLF_I18NCONTEXT_ID_ARMATURE;
388 case ID_AC: return BLF_I18NCONTEXT_ID_ACTION;
389 case ID_NT: return BLF_I18NCONTEXT_ID_NODETREE;
390 case ID_BR: return BLF_I18NCONTEXT_ID_BRUSH;
391 case ID_PA: return BLF_I18NCONTEXT_ID_PARTICLESETTINGS;
392 case ID_GD: return BLF_I18NCONTEXT_ID_GPENCIL;
395 return BLF_I18NCONTEXT_DEFAULT;
399 static void template_ID(bContext *C, uiLayout *layout, TemplateID *template, StructRNA *type, short idcode, int flag,
400 const char *newop, const char *openop, const char *unlinkop)
405 // ListBase *lb; // UNUSED
407 int editable = RNA_property_editable(&template->ptr, template->prop);
409 idptr = RNA_property_pointer_get(&template->ptr, template->prop);
411 idfrom = template->ptr.id.data;
412 // lb = template->idlb;
414 block = uiLayoutGetBlock(layout);
415 uiBlockBeginAlign(block);
420 if (flag & UI_ID_PREVIEWS) {
421 template->preview = true;
423 but = uiDefBlockButN(block, id_search_menu, MEM_dupallocN(template), "", 0, 0, UI_UNIT_X * 6, UI_UNIT_Y * 6,
424 TIP_(template_id_browse_tip(type)));
426 but->icon = RNA_struct_ui_icon(type);
427 if (id) but->icon = ui_id_icon_get(C, id, true);
428 uiButSetFlag(but, UI_HAS_ICON | UI_ICON_PREVIEW);
430 if ((idfrom && idfrom->lib) || !editable)
431 uiButSetFlag(but, UI_BUT_DISABLED);
433 uiLayoutRow(layout, TRUE);
435 else if (flag & UI_ID_BROWSE) {
436 but = uiDefBlockButN(block, id_search_menu, MEM_dupallocN(template), "", 0, 0, UI_UNIT_X * 1.6, UI_UNIT_Y,
437 TIP_(template_id_browse_tip(type)));
439 uiButSetDrawFlag(but, UI_BUT_DRAW_ENUM_ARROWS);
442 but->icon = RNA_struct_ui_icon(type);
443 /* default dragging of icon for id browse buttons */
444 uiButSetDragID(but, id);
445 uiButSetFlag(but, UI_HAS_ICON | UI_ICON_LEFT);
448 if ((idfrom && idfrom->lib) || !editable)
449 uiButSetFlag(but, UI_BUT_DISABLED);
452 /* text button with name */
454 char name[UI_MAX_NAME_STR];
455 const short user_alert = (id->us <= 0);
457 //text_idbutton(id, name);
459 but = uiDefButR(block, TEX, 0, name, 0, 0, UI_UNIT_X * 6, UI_UNIT_Y,
460 &idptr, "name", -1, 0, 0, -1, -1, RNA_struct_ui_description(type));
461 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_RENAME));
462 if (user_alert) uiButSetFlag(but, UI_BUT_REDALERT);
465 if (id->flag & LIB_INDIRECT) {
466 but = uiDefIconBut(block, BUT, 0, ICON_LIBRARY_DATA_INDIRECT, 0, 0, UI_UNIT_X, UI_UNIT_Y,
467 NULL, 0, 0, 0, 0, TIP_("Indirect library datablock, cannot change"));
468 uiButSetFlag(but, UI_BUT_DISABLED);
471 but = uiDefIconBut(block, BUT, 0, ICON_LIBRARY_DATA_DIRECT, 0, 0, UI_UNIT_X, UI_UNIT_Y,
472 NULL, 0, 0, 0, 0, TIP_("Direct linked library datablock, click to make local"));
473 if (!id_make_local(id, true /* test */) || (idfrom && idfrom->lib))
474 uiButSetFlag(but, UI_BUT_DISABLED);
477 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_LOCAL));
483 BLI_snprintf(numstr, sizeof(numstr), "%d", id->us);
485 but = uiDefBut(block, BUT, 0, numstr, 0, 0, UI_UNIT_X + ((id->us < 10) ? 0 : 10), UI_UNIT_Y,
487 TIP_("Display number of users of this data (click to make a single-user copy)"));
489 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ALONE));
491 (id_copy(id, NULL, true) == false) ||
492 (idfrom && idfrom->lib) ||
493 (editable == FALSE) ||
494 /* object in editmode - don't change data */
495 (idfrom && GS(idfrom->name) == ID_OB && (((Object *)idfrom)->mode & OB_MODE_EDIT)))
497 uiButSetFlag(but, UI_BUT_DISABLED);
501 if (user_alert) uiButSetFlag(but, UI_BUT_REDALERT);
503 if (id->lib == NULL && !(ELEM5(GS(id->name), ID_GR, ID_SCE, ID_SCR, ID_TXT, ID_OB))) {
504 uiDefButR(block, TOG, 0, "F", 0, 0, UI_UNIT_X, UI_UNIT_Y, &idptr, "use_fake_user", -1, 0, 0, -1, -1, NULL);
508 if (flag & UI_ID_ADD_NEW) {
509 int w = id ? UI_UNIT_X : (flag & UI_ID_OPEN) ? UI_UNIT_X * 3 : UI_UNIT_X * 6;
511 /* i18n markup, does nothing! */
512 BLF_I18N_MSGID_MULTI_CTXT("New", BLF_I18NCONTEXT_DEFAULT,
513 BLF_I18NCONTEXT_ID_SCENE,
514 BLF_I18NCONTEXT_ID_OBJECT,
515 BLF_I18NCONTEXT_ID_MESH,
516 BLF_I18NCONTEXT_ID_CURVE,
517 BLF_I18NCONTEXT_ID_METABALL,
518 BLF_I18NCONTEXT_ID_MATERIAL,
519 BLF_I18NCONTEXT_ID_TEXTURE,
520 BLF_I18NCONTEXT_ID_IMAGE,
521 BLF_I18NCONTEXT_ID_LATTICE,
522 BLF_I18NCONTEXT_ID_LAMP,
523 BLF_I18NCONTEXT_ID_CAMERA,
524 BLF_I18NCONTEXT_ID_WORLD,
525 BLF_I18NCONTEXT_ID_SCREEN,
526 BLF_I18NCONTEXT_ID_TEXT,
528 BLF_I18N_MSGID_MULTI_CTXT("New", BLF_I18NCONTEXT_ID_SPEAKER,
529 BLF_I18NCONTEXT_ID_SOUND,
530 BLF_I18NCONTEXT_ID_ARMATURE,
531 BLF_I18NCONTEXT_ID_ACTION,
532 BLF_I18NCONTEXT_ID_NODETREE,
533 BLF_I18NCONTEXT_ID_BRUSH,
534 BLF_I18NCONTEXT_ID_PARTICLESETTINGS,
535 BLF_I18NCONTEXT_ID_GPENCIL,
536 BLF_I18NCONTEXT_ID_FREESTYLELINESTYLE,
540 but = uiDefIconTextButO(block, BUT, newop, WM_OP_INVOKE_DEFAULT, ICON_ZOOMIN,
541 (id) ? "" : CTX_IFACE_(template_id_context(type), "New"), 0, 0, w, UI_UNIT_Y, NULL);
542 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ADD_NEW));
545 but = uiDefIconTextBut(block, BUT, 0, ICON_ZOOMIN, (id) ? "" : CTX_IFACE_(template_id_context(type), "New"),
546 0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, NULL);
547 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_ADD_NEW));
550 if ((idfrom && idfrom->lib) || !editable)
551 uiButSetFlag(but, UI_BUT_DISABLED);
554 /* Due to space limit in UI - skip the "open" icon for packed data, and allow to unpack.
555 * Only for images, sound and fonts */
556 if (id && BKE_pack_check(id)) {
557 but = uiDefIconButO(block, BUT, "FILE_OT_unpack_item", WM_OP_INVOKE_REGION_WIN, ICON_PACKAGE, 0, 0,
558 UI_UNIT_X, UI_UNIT_Y, TIP_("Packed File, click to unpack"));
559 uiButGetOperatorPtrRNA(but);
561 RNA_string_set(but->opptr, "id_name", id->name + 2);
562 RNA_int_set(but->opptr, "id_type", GS(id->name));
565 else if (flag & UI_ID_OPEN) {
566 int w = id ? UI_UNIT_X : (flag & UI_ID_ADD_NEW) ? UI_UNIT_X * 3 : UI_UNIT_X * 6;
569 but = uiDefIconTextButO(block, BUT, openop, WM_OP_INVOKE_DEFAULT, ICON_FILESEL, (id) ? "" : IFACE_("Open"),
570 0, 0, w, UI_UNIT_Y, NULL);
571 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_OPEN));
574 but = uiDefIconTextBut(block, BUT, 0, ICON_FILESEL, (id) ? "" : IFACE_("Open"), 0, 0, w, UI_UNIT_Y,
575 NULL, 0, 0, 0, 0, NULL);
576 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_OPEN));
579 if ((idfrom && idfrom->lib) || !editable)
580 uiButSetFlag(but, UI_BUT_DISABLED);
584 /* don't use RNA_property_is_unlink here */
585 if (id && (flag & UI_ID_DELETE) && (RNA_property_flag(template->prop) & PROP_NEVER_UNLINK) == 0) {
587 but = uiDefIconButO(block, BUT, unlinkop, WM_OP_INVOKE_REGION_WIN, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL);
588 /* so we can access the template from operators, font unlinking needs this */
589 uiButSetNFunc(but, NULL, MEM_dupallocN(template), NULL);
592 but = uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0, 0, 0, 0,
593 TIP_("Unlink datablock "
594 "(Shift + Click to set users to zero, data will then not be saved)"));
595 uiButSetNFunc(but, template_id_cb, MEM_dupallocN(template), SET_INT_IN_POINTER(UI_ID_DELETE));
597 if (RNA_property_flag(template->prop) & PROP_NEVER_NULL)
598 uiButSetFlag(but, UI_BUT_DISABLED);
601 if ((idfrom && idfrom->lib) || !editable)
602 uiButSetFlag(but, UI_BUT_DISABLED);
606 uiTemplateTextureShow(layout, C, &template->ptr, template->prop);
608 uiBlockEndAlign(block);
611 static void ui_template_id(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
612 const char *openop, const char *unlinkop, int flag, int prv_rows, int prv_cols)
614 TemplateID *template;
619 prop = RNA_struct_find_property(ptr, propname);
621 if (!prop || RNA_property_type(prop) != PROP_POINTER) {
622 RNA_warning("pointer property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
626 template = MEM_callocN(sizeof(TemplateID), "TemplateID");
627 template->ptr = *ptr;
628 template->prop = prop;
629 template->prv_rows = prv_rows;
630 template->prv_cols = prv_cols;
633 flag |= UI_ID_ADD_NEW;
637 type = RNA_property_pointer_type(ptr, prop);
638 idcode = RNA_type_to_ID_code(type);
639 template->idlb = which_libbase(CTX_data_main(C), idcode);
641 /* create UI elements for this template
642 * - template_ID makes a copy of the template data and assigns it to the relevant buttons
644 if (template->idlb) {
645 uiLayoutRow(layout, TRUE);
646 template_ID(C, layout, template, type, idcode, flag, newop, openop, unlinkop);
652 void uiTemplateID(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
653 const char *openop, const char *unlinkop)
655 ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop,
656 UI_ID_BROWSE | UI_ID_RENAME | UI_ID_DELETE, 0, 0);
659 void uiTemplateIDBrowse(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
660 const char *openop, const char *unlinkop)
662 ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop, UI_ID_BROWSE | UI_ID_RENAME, 0, 0);
665 void uiTemplateIDPreview(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, const char *newop,
666 const char *openop, const char *unlinkop, int rows, int cols)
668 ui_template_id(layout, C, ptr, propname, newop, openop, unlinkop,
669 UI_ID_BROWSE | UI_ID_RENAME | UI_ID_DELETE | UI_ID_PREVIEWS, rows, cols);
672 /************************ ID Chooser Template ***************************/
674 /* This is for selecting the type of ID-block to use, and then from the relevant type choosing the block to use
676 * - propname: property identifier for property that ID-pointer gets stored to
677 * - proptypename: property identifier for property used to determine the type of ID-pointer that can be used
679 void uiTemplateAnyID(uiLayout *layout, PointerRNA *ptr, const char *propname, const char *proptypename,
682 PropertyRNA *propID, *propType;
683 uiLayout *split, *row, *sub;
685 /* get properties... */
686 propID = RNA_struct_find_property(ptr, propname);
687 propType = RNA_struct_find_property(ptr, proptypename);
689 if (!propID || RNA_property_type(propID) != PROP_POINTER) {
690 RNA_warning("pointer property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
693 if (!propType || RNA_property_type(propType) != PROP_ENUM) {
694 RNA_warning("pointer-type property not found: %s.%s", RNA_struct_identifier(ptr->type), proptypename);
698 /* Start drawing UI Elements using standard defines */
699 split = uiLayoutSplit(layout, 0.33f, FALSE); /* NOTE: split amount here needs to be synced with normal labels */
701 /* FIRST PART ................................................ */
702 row = uiLayoutRow(split, FALSE);
704 /* Label - either use the provided text, or will become "ID-Block:" */
707 uiItemL(row, text, ICON_NONE);
710 uiItemL(row, IFACE_("ID-Block:"), ICON_NONE);
713 /* SECOND PART ................................................ */
714 row = uiLayoutRow(split, TRUE);
716 /* ID-Type Selector - just have a menu of icons */
717 sub = uiLayoutRow(row, TRUE); /* HACK: special group just for the enum, otherwise we */
718 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT); /* we get ugly layout with text included too... */
720 uiItemFullR(sub, ptr, propType, 0, 0, UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
722 /* ID-Block Selector - just use pointer widget... */
723 sub = uiLayoutRow(row, TRUE); /* HACK: special group to counteract the effects of the previous */
724 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_EXPAND); /* enum, which now pushes everything too far right */
726 uiItemFullR(sub, ptr, propID, 0, 0, 0, "", ICON_NONE);
729 /********************* RNA Path Builder Template ********************/
733 /* This is creating/editing RNA-Paths
735 * - ptr: struct which holds the path property
736 * - propname: property identifier for property that path gets stored to
737 * - root_ptr: struct that path gets built from
739 void uiTemplatePathBuilder(uiLayout *layout, PointerRNA *ptr, const char *propname, PointerRNA *UNUSED(root_ptr),
742 PropertyRNA *propPath;
745 /* check that properties are valid */
746 propPath = RNA_struct_find_property(ptr, propname);
747 if (!propPath || RNA_property_type(propPath) != PROP_STRING) {
748 RNA_warning("path property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
752 /* Start drawing UI Elements using standard defines */
753 row = uiLayoutRow(layout, TRUE);
755 /* Path (existing string) Widget */
756 uiItemR(row, ptr, propname, 0, text, ICON_RNA);
758 /* TODO: attach something to this to make allow searching of nested properties to 'build' the path */
761 /************************ Modifier Template *************************/
763 #define ERROR_LIBDATA_MESSAGE IFACE_("Can't edit external libdata")
765 static void modifiers_setOnCage(bContext *C, void *ob_v, void *md_v)
767 Scene *scene = CTX_data_scene(C);
769 ModifierData *md = md_v;
770 int i, cageIndex = modifiers_getCageIndex(scene, ob, NULL, 0);
772 /* undo button operation */
773 md->mode ^= eModifierMode_OnCage;
775 for (i = 0, md = ob->modifiers.first; md; ++i, md = md->next) {
778 md->mode ^= eModifierMode_OnCage;
783 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
784 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
787 static void modifiers_convertToReal(bContext *C, void *ob_v, void *md_v)
790 ModifierData *md = md_v;
791 ModifierData *nmd = modifier_new(md->type);
793 modifier_copyData(md, nmd);
794 nmd->mode &= ~eModifierMode_Virtual;
796 BLI_addhead(&ob->modifiers, nmd);
798 modifier_unique_name(&ob->modifiers, nmd);
800 ob->partype = PAROBJECT;
802 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
803 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
805 ED_undo_push(C, "Modifier convert to real");
808 static int modifier_can_delete(ModifierData *md)
810 /* fluid particle modifier can't be deleted here */
811 if (md->type == eModifierType_ParticleSystem)
812 if (((ParticleSystemModifierData *)md)->psys->part->type == PART_FLUID)
818 /* Check whether Modifier is a simulation or not, this is used for switching to the physics/particles context tab */
819 static int modifier_is_simulation(ModifierData *md)
822 if (ELEM7(md->type, eModifierType_Cloth, eModifierType_Collision, eModifierType_Fluidsim, eModifierType_Smoke,
823 eModifierType_Softbody, eModifierType_Surface, eModifierType_DynamicPaint))
828 else if (md->type == eModifierType_ParticleSystem) {
836 static uiLayout *draw_modifier(uiLayout *layout, Scene *scene, Object *ob,
837 ModifierData *md, int index, int cageIndex, int lastCageIndex)
839 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
843 uiLayout *box, *column, *row;
844 uiLayout *result = NULL;
845 int isVirtual = (md->mode & eModifierMode_Virtual);
848 /* create RNA pointer */
849 RNA_pointer_create(&ob->id, &RNA_Modifier, md, &ptr);
851 column = uiLayoutColumn(layout, TRUE);
852 uiLayoutSetContextPointer(column, "modifier", &ptr);
854 /* rounded header ------------------------------------------------------------------- */
855 box = uiLayoutBox(column);
858 row = uiLayoutRow(box, FALSE);
859 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_EXPAND);
860 block = uiLayoutGetBlock(row);
861 /* VIRTUAL MODIFIER */
862 /* XXX this is not used now, since these cannot be accessed via RNA */
863 BLI_snprintf(str, sizeof(str), IFACE_("%s parent deform"), md->name);
864 uiDefBut(block, LABEL, 0, str, 0, 0, 185, UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Modifier name"));
866 but = uiDefBut(block, BUT, 0, IFACE_("Make Real"), 0, 0, 80, 16, NULL, 0.0, 0.0, 0.0, 0.0,
867 TIP_("Convert virtual modifier to a real modifier"));
868 uiButSetFunc(but, modifiers_convertToReal, ob, md);
872 row = uiLayoutRow(box, FALSE);
873 block = uiLayoutGetBlock(row);
875 uiBlockSetEmboss(block, UI_EMBOSSN);
876 /* Open/Close ................................. */
877 uiItemR(row, &ptr, "show_expanded", 0, "", ICON_NONE);
879 /* modifier-type icon */
880 uiItemL(row, "", RNA_struct_ui_icon(ptr.type));
881 uiBlockSetEmboss(block, UI_EMBOSS);
884 uiItemR(row, &ptr, "name", 0, "", ICON_NONE);
886 /* mode enabling buttons */
887 uiBlockBeginAlign(block);
888 /* Softbody not allowed in this situation, enforce! */
889 if (((md->type != eModifierType_Softbody && md->type != eModifierType_Collision) || !(ob->pd && ob->pd->deflect)) &&
890 (md->type != eModifierType_Surface) )
892 uiItemR(row, &ptr, "show_render", 0, "", ICON_NONE);
893 uiItemR(row, &ptr, "show_viewport", 0, "", ICON_NONE);
895 if (mti->flags & eModifierTypeFlag_SupportsEditmode)
896 uiItemR(row, &ptr, "show_in_editmode", 0, "", ICON_NONE);
899 if (ob->type == OB_MESH) {
900 if (modifier_couldBeCage(scene, md) && (index <= lastCageIndex)) {
901 /* -- convert to rna ? */
902 but = uiDefIconButBitI(block, TOG, eModifierMode_OnCage, 0, ICON_MESH_DATA, 0, 0,
903 UI_UNIT_X - 2, UI_UNIT_Y, &md->mode, 0.0, 0.0, 0.0, 0.0,
904 TIP_("Apply modifier to editing cage during Edit mode"));
905 if (index < cageIndex)
906 uiButSetFlag(but, UI_BUT_DISABLED);
907 uiButSetFunc(but, modifiers_setOnCage, ob, md);
910 uiBlockEndAlign(block);
912 /* place holder button */
913 uiBlockSetEmboss(block, UI_EMBOSSN);
914 but = uiDefIconBut(block, BUT, 0, ICON_NONE, 0, 0, UI_UNIT_X - 2, UI_UNIT_Y,
915 NULL, 0.0, 0.0, 0.0, 0.0, NULL);
916 uiButSetFlag(but, UI_BUT_DISABLED);
917 uiBlockSetEmboss(block, UI_EMBOSS);
919 } /* tessellation point for curve-typed objects */
920 else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
921 /* some modifiers could work with pre-tessellated curves only */
922 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
923 /* add disabled pre-tessellated button, so users could have
924 * message for this modifiers */
925 but = uiDefIconButBitI(block, TOG, eModifierMode_ApplyOnSpline, 0, ICON_SURFACE_DATA, 0, 0,
926 UI_UNIT_X - 2, UI_UNIT_Y, &md->mode, 0.0, 0.0, 0.0, 0.0,
927 TIP_("This modifier could be applied on splines' points only"));
928 uiButSetFlag(but, UI_BUT_DISABLED);
930 else if (mti->type != eModifierTypeType_Constructive) {
931 /* constructive modifiers tessellates curve before applying */
932 uiItemR(row, &ptr, "use_apply_on_spline", 0, "", ICON_NONE);
936 uiBlockEndAlign(block);
938 /* Up/Down + Delete ........................... */
939 uiBlockBeginAlign(block);
940 uiItemO(row, "", ICON_TRIA_UP, "OBJECT_OT_modifier_move_up");
941 uiItemO(row, "", ICON_TRIA_DOWN, "OBJECT_OT_modifier_move_down");
942 uiBlockEndAlign(block);
944 uiBlockSetEmboss(block, UI_EMBOSSN);
945 /* When Modifier is a simulation, show button to switch to context rather than the delete button. */
946 if (modifier_can_delete(md) && !modifier_is_simulation(md))
947 uiItemO(row, "", ICON_X, "OBJECT_OT_modifier_remove");
948 if (modifier_is_simulation(md) == 1)
949 uiItemStringO(row, "", ICON_BUTS, "WM_OT_properties_context_change", "context", "PHYSICS");
950 else if (modifier_is_simulation(md) == 2)
951 uiItemStringO(row, "", ICON_BUTS, "WM_OT_properties_context_change", "context", "PARTICLES");
952 uiBlockSetEmboss(block, UI_EMBOSS);
956 /* modifier settings (under the header) --------------------------------------------------- */
957 if (!isVirtual && (md->mode & eModifierMode_Expanded)) {
958 /* apply/convert/copy */
959 box = uiLayoutBox(column);
960 row = uiLayoutRow(box, FALSE);
962 if (!ELEM(md->type, eModifierType_Collision, eModifierType_Surface)) {
963 /* only here obdata, the rest of modifiers is ob level */
964 uiBlockSetButLock(block, BKE_object_obdata_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
966 if (md->type == eModifierType_ParticleSystem) {
967 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
969 if (!(ob->mode & OB_MODE_PARTICLE_EDIT) && psys->pathcache) {
970 if (ELEM(psys->part->ren_as, PART_DRAW_GR, PART_DRAW_OB))
971 uiItemO(row, CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Convert"), ICON_NONE,
972 "OBJECT_OT_duplicates_make_real");
973 else if (psys->part->ren_as == PART_DRAW_PATH)
974 uiItemO(row, CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Convert"), ICON_NONE,
975 "OBJECT_OT_modifier_convert");
979 uiLayoutSetOperatorContext(row, WM_OP_INVOKE_DEFAULT);
980 uiItemEnumO(row, "OBJECT_OT_modifier_apply", CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Apply"),
981 0, "apply_as", MODIFIER_APPLY_DATA);
983 if (modifier_isSameTopology(md) && !modifier_isNonGeometrical(md)) {
984 uiItemEnumO(row, "OBJECT_OT_modifier_apply",
985 CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Apply as Shape Key"),
986 0, "apply_as", MODIFIER_APPLY_SHAPE);
990 uiBlockClearButLock(block);
991 uiBlockSetButLock(block, ob && ob->id.lib, ERROR_LIBDATA_MESSAGE);
993 if (!ELEM5(md->type, eModifierType_Fluidsim, eModifierType_Softbody, eModifierType_ParticleSystem,
994 eModifierType_Cloth, eModifierType_Smoke))
996 uiItemO(row, CTX_IFACE_(BLF_I18NCONTEXT_OPERATOR_DEFAULT, "Copy"), ICON_NONE,
997 "OBJECT_OT_modifier_copy");
1001 /* result is the layout block inside the box, that we return so that modifier settings can be drawn */
1002 result = uiLayoutColumn(box, FALSE);
1003 block = uiLayoutAbsoluteBlock(box);
1006 /* error messages */
1008 box = uiLayoutBox(column);
1009 row = uiLayoutRow(box, FALSE);
1010 uiItemL(row, md->error, ICON_ERROR);
1016 uiLayout *uiTemplateModifier(uiLayout *layout, bContext *C, PointerRNA *ptr)
1018 Scene *scene = CTX_data_scene(C);
1020 ModifierData *md, *vmd;
1021 VirtualModifierData virtualModifierData;
1022 int i, lastCageIndex, cageIndex;
1024 /* verify we have valid data */
1025 if (!RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
1026 RNA_warning("Expected modifier on object");
1033 if (!ob || !(GS(ob->id.name) == ID_OB)) {
1034 RNA_warning("Expected modifier on object");
1038 uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1040 /* find modifier and draw it */
1041 cageIndex = modifiers_getCageIndex(scene, ob, &lastCageIndex, 0);
1043 /* XXX virtual modifiers are not accesible for python */
1044 vmd = modifiers_getVirtualModifierList(ob, &virtualModifierData);
1046 for (i = 0; vmd; i++, vmd = vmd->next) {
1048 return draw_modifier(layout, scene, ob, md, i, cageIndex, lastCageIndex);
1049 else if (vmd->mode & eModifierMode_Virtual)
1056 /************************ Constraint Template *************************/
1058 #include "DNA_constraint_types.h"
1060 #include "BKE_action.h"
1061 #include "BKE_constraint.h"
1063 #define B_CONSTRAINT_TEST 5
1064 // #define B_CONSTRAINT_CHANGETARGET 6
1066 static void do_constraint_panels(bContext *C, void *ob_pt, int event)
1068 Object *ob = (Object *)ob_pt;
1071 case B_CONSTRAINT_TEST:
1072 break; /* no handling */
1074 case B_CONSTRAINT_CHANGETARGET:
1076 Main *bmain = CTX_data_main(C);
1077 if (ob->pose) ob->pose->flag |= POSE_RECALC; /* checks & sorts pose channels */
1078 DAG_relations_tag_update(bmain);
1086 /* note: RNA updates now call this, commenting else it gets called twice.
1087 * if there are problems because of this, then rna needs changed update functions.
1089 * object_test_constraints(ob);
1090 * if (ob->pose) BKE_pose_update_constraint_flags(ob->pose); */
1092 if (ob->type == OB_ARMATURE) DAG_id_tag_update(&ob->id, OB_RECALC_DATA | OB_RECALC_OB);
1093 else DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1095 WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
1098 static void constraint_active_func(bContext *UNUSED(C), void *ob_v, void *con_v)
1100 ED_object_constraint_set_active(ob_v, con_v);
1103 /* draw panel showing settings for a constraint */
1104 static uiLayout *draw_constraint(uiLayout *layout, Object *ob, bConstraint *con)
1106 bPoseChannel *pchan = BKE_pose_channel_active(ob);
1107 bConstraintTypeInfo *cti;
1109 uiLayout *result = NULL, *col, *box, *row;
1112 short proxy_protected, xco = 0, yco = 0;
1113 // int rb_col; // UNUSED
1115 /* get constraint typeinfo */
1116 cti = BKE_constraint_get_typeinfo(con);
1118 /* exception for 'Null' constraint - it doesn't have constraint typeinfo! */
1119 BLI_strncpy(typestr, (con->type == CONSTRAINT_TYPE_NULL) ? IFACE_("Null") : IFACE_("Unknown"), sizeof(typestr));
1122 BLI_strncpy(typestr, IFACE_(cti->name), sizeof(typestr));
1124 /* determine whether constraint is proxy protected or not */
1125 if (BKE_proxylocked_constraints_owner(ob, pchan))
1126 proxy_protected = (con->flag & CONSTRAINT_PROXY_LOCAL) == 0;
1128 proxy_protected = 0;
1130 /* unless button has own callback, it adds this callback to button */
1131 block = uiLayoutGetBlock(layout);
1132 uiBlockSetHandleFunc(block, do_constraint_panels, ob);
1133 uiBlockSetFunc(block, constraint_active_func, ob, con);
1135 RNA_pointer_create(&ob->id, &RNA_Constraint, con, &ptr);
1137 col = uiLayoutColumn(layout, TRUE);
1138 uiLayoutSetContextPointer(col, "constraint", &ptr);
1140 box = uiLayoutBox(col);
1141 row = uiLayoutRow(box, FALSE);
1142 block = uiLayoutGetBlock(box);
1144 /* Draw constraint header */
1147 uiBlockSetEmboss(block, UI_EMBOSSN);
1148 uiItemR(row, &ptr, "show_expanded", UI_ITEM_R_ICON_ONLY, "", ICON_NONE);
1149 uiBlockSetEmboss(block, UI_EMBOSS);
1152 uiDefBut(block, LABEL, B_CONSTRAINT_TEST, typestr,
1153 xco + 0.5f * UI_UNIT_X, yco, 5 * UI_UNIT_X, 0.9f * UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, "");
1155 if (con->flag & CONSTRAINT_DISABLE)
1156 uiLayoutSetRedAlert(row, TRUE);
1158 if (proxy_protected == 0) {
1159 uiItemR(row, &ptr, "name", 0, "", ICON_NONE);
1162 uiItemL(row, con->name, ICON_NONE);
1164 uiLayoutSetRedAlert(row, FALSE);
1166 /* proxy-protected constraints cannot be edited, so hide up/down + close buttons */
1167 if (proxy_protected) {
1168 uiBlockSetEmboss(block, UI_EMBOSSN);
1170 /* draw a ghost icon (for proxy) and also a lock beside it, to show that constraint is "proxy locked" */
1171 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_GHOST, xco + 12.2f * UI_UNIT_X, yco, 0.95f * UI_UNIT_X, 0.95f * UI_UNIT_Y,
1172 NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Proxy Protected"));
1173 uiDefIconBut(block, BUT, B_CONSTRAINT_TEST, ICON_LOCKED, xco + 13.1f * UI_UNIT_X, yco, 0.95f * UI_UNIT_X, 0.95f * UI_UNIT_Y,
1174 NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Proxy Protected"));
1176 uiBlockSetEmboss(block, UI_EMBOSS);
1179 short prev_proxylock, show_upbut, show_downbut;
1182 * Proxy-constraints are not allowed to occur after local (non-proxy) constraints
1183 * as that poses problems when restoring them, so disable the "up" button where
1184 * it may cause this situation.
1186 * Up/Down buttons should only be shown (or not grayed - todo) if they serve some purpose.
1188 if (BKE_proxylocked_constraints_owner(ob, pchan)) {
1190 prev_proxylock = (con->prev->flag & CONSTRAINT_PROXY_LOCAL) ? 0 : 1;
1198 show_upbut = ((prev_proxylock == 0) && (con->prev));
1199 show_downbut = (con->next) ? 1 : 0;
1202 uiBlockSetEmboss(block, UI_EMBOSSN);
1203 uiItemR(row, &ptr, "mute", 0, "",
1204 (con->flag & CONSTRAINT_OFF) ? ICON_RESTRICT_VIEW_ON : ICON_RESTRICT_VIEW_OFF);
1205 uiBlockSetEmboss(block, UI_EMBOSS);
1207 uiLayoutSetOperatorContext(row, WM_OP_INVOKE_DEFAULT);
1210 if (show_upbut || show_downbut) {
1211 uiBlockBeginAlign(block);
1213 uiItemO(row, "", ICON_TRIA_UP, "CONSTRAINT_OT_move_up");
1216 uiItemO(row, "", ICON_TRIA_DOWN, "CONSTRAINT_OT_move_down");
1217 uiBlockEndAlign(block);
1220 /* Close 'button' - emboss calls here disable drawing of 'button' behind X */
1221 uiBlockSetEmboss(block, UI_EMBOSSN);
1222 uiItemO(row, "", ICON_X, "CONSTRAINT_OT_delete");
1223 uiBlockSetEmboss(block, UI_EMBOSS);
1226 /* Set but-locks for protected settings (magic numbers are used here!) */
1227 if (proxy_protected)
1228 uiBlockSetButLock(block, true, IFACE_("Cannot edit Proxy-Protected Constraint"));
1230 /* Draw constraint data */
1231 if ((con->flag & CONSTRAINT_EXPAND) == 0) {
1232 (yco) -= 10.5f * UI_UNIT_Y;
1235 box = uiLayoutBox(col);
1236 block = uiLayoutAbsoluteBlock(box);
1240 /* clear any locks set up for proxies/lib-linking */
1241 uiBlockClearButLock(block);
1246 uiLayout *uiTemplateConstraint(uiLayout *layout, PointerRNA *ptr)
1251 /* verify we have valid data */
1252 if (!RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
1253 RNA_warning("Expected constraint on object");
1260 if (!ob || !(GS(ob->id.name) == ID_OB)) {
1261 RNA_warning("Expected constraint on object");
1265 uiBlockSetButLock(uiLayoutGetBlock(layout), (ob && ob->id.lib), ERROR_LIBDATA_MESSAGE);
1267 /* hrms, the temporal constraint should not draw! */
1268 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
1269 bKinematicConstraint *data = con->data;
1270 if (data->flag & CONSTRAINT_IK_TEMP)
1274 return draw_constraint(layout, ob, con);
1278 /************************* Preview Template ***************************/
1280 #include "DNA_lamp_types.h"
1281 #include "DNA_material_types.h"
1282 #include "DNA_world_types.h"
1286 static void do_preview_buttons(bContext *C, void *arg, int event)
1290 WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_PREVIEW, arg);
1295 void uiTemplatePreview(uiLayout *layout, ID *id, int show_buttons, ID *parent, MTex *slot)
1297 uiLayout *row, *col;
1299 Material *ma = NULL;
1300 Tex *tex = (Tex *)id;
1302 short *pr_texture = NULL;
1303 PointerRNA material_ptr;
1304 PointerRNA texture_ptr;
1306 if (id && !ELEM4(GS(id->name), ID_MA, ID_TE, ID_WO, ID_LA)) {
1307 RNA_warning("Expected ID of type material, texture, lamp or world");
1311 /* decide what to render */
1315 if (id && (GS(id->name) == ID_TE)) {
1316 if (parent && (GS(parent->name) == ID_MA))
1317 pr_texture = &((Material *)parent)->pr_texture;
1318 else if (parent && (GS(parent->name) == ID_WO))
1319 pr_texture = &((World *)parent)->pr_texture;
1320 else if (parent && (GS(parent->name) == ID_LA))
1321 pr_texture = &((Lamp *)parent)->pr_texture;
1324 if (*pr_texture == TEX_PR_OTHER)
1326 else if (*pr_texture == TEX_PR_BOTH)
1332 block = uiLayoutGetBlock(layout);
1333 row = uiLayoutRow(layout, FALSE);
1334 col = uiLayoutColumn(row, FALSE);
1335 uiLayoutSetKeepAspect(col, TRUE);
1338 uiDefBut(block, BUT_EXTRA, 0, "", 0, 0, UI_UNIT_X * 6, UI_UNIT_Y * 6, pid, 0.0, 0.0, 0, 0, "");
1339 uiBlockSetDrawExtraFunc(block, ED_preview_draw, pparent, slot);
1340 uiBlockSetHandleFunc(block, do_preview_buttons, NULL);
1343 if (pid && show_buttons) {
1344 if (GS(pid->name) == ID_MA || (pparent && GS(pparent->name) == ID_MA)) {
1345 if (GS(pid->name) == ID_MA) ma = (Material *)pid;
1346 else ma = (Material *)pparent;
1348 /* Create RNA Pointer */
1349 RNA_pointer_create(&ma->id, &RNA_Material, ma, &material_ptr);
1351 col = uiLayoutColumn(row, TRUE);
1352 uiLayoutSetScaleX(col, 1.5);
1353 uiItemR(col, &material_ptr, "preview_render_type", UI_ITEM_R_EXPAND, "", ICON_NONE);
1357 /* Create RNA Pointer */
1358 RNA_pointer_create(id, &RNA_Texture, tex, &texture_ptr);
1360 uiLayoutRow(layout, TRUE);
1361 uiDefButS(block, ROW, B_MATPRV, IFACE_("Texture"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1362 pr_texture, 10, TEX_PR_TEXTURE, 0, 0, "");
1363 if (GS(parent->name) == ID_MA) {
1364 uiDefButS(block, ROW, B_MATPRV, IFACE_("Material"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1365 pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1367 else if (GS(parent->name) == ID_LA) {
1368 uiDefButS(block, ROW, B_MATPRV, IFACE_("Lamp"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1369 pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1371 else if (GS(parent->name) == ID_WO) {
1372 uiDefButS(block, ROW, B_MATPRV, IFACE_("World"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1373 pr_texture, 10, TEX_PR_OTHER, 0, 0, "");
1375 uiDefButS(block, ROW, B_MATPRV, IFACE_("Both"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
1376 pr_texture, 10, TEX_PR_BOTH, 0, 0, "");
1378 /* Alpha button for texture preview */
1379 if (*pr_texture != TEX_PR_OTHER) {
1380 row = uiLayoutRow(layout, FALSE);
1381 uiItemR(row, &texture_ptr, "use_preview_alpha", 0, NULL, ICON_NONE);
1387 /********************** ColorRamp Template **************************/
1390 typedef struct RNAUpdateCb {
1395 static void rna_update_cb(bContext *C, void *arg_cb, void *UNUSED(arg))
1397 RNAUpdateCb *cb = (RNAUpdateCb *)arg_cb;
1399 /* we call update here on the pointer property, this way the
1400 * owner of the curve mapping can still define it's own update
1401 * and notifier, even if the CurveMapping struct is shared. */
1402 RNA_property_update(C, &cb->ptr, cb->prop);
1405 static void colorband_add_cb(bContext *C, void *cb_v, void *coba_v)
1407 ColorBand *coba = coba_v;
1410 if (coba->tot > 1) {
1411 if (coba->cur > 0) pos = (coba->data[coba->cur - 1].pos + coba->data[coba->cur].pos) * 0.5f;
1412 else pos = (coba->data[coba->cur + 1].pos + coba->data[coba->cur].pos) * 0.5f;
1415 if (colorband_element_add(coba, pos)) {
1416 rna_update_cb(C, cb_v, NULL);
1417 ED_undo_push(C, "Add colorband");
1421 static void colorband_del_cb(bContext *C, void *cb_v, void *coba_v)
1423 ColorBand *coba = coba_v;
1425 if (colorband_element_remove(coba, coba->cur)) {
1426 ED_undo_push(C, "Delete colorband");
1427 rna_update_cb(C, cb_v, NULL);
1431 static void colorband_flip_cb(bContext *C, void *cb_v, void *coba_v)
1433 CBData data_tmp[MAXCOLORBAND];
1435 ColorBand *coba = coba_v;
1438 for (a = 0; a < coba->tot; a++) {
1439 data_tmp[a] = coba->data[coba->tot - (a + 1)];
1441 for (a = 0; a < coba->tot; a++) {
1442 data_tmp[a].pos = 1.0f - data_tmp[a].pos;
1443 coba->data[a] = data_tmp[a];
1446 /* may as well flip the cur*/
1447 coba->cur = coba->tot - (coba->cur + 1);
1449 ED_undo_push(C, "Flip colorband");
1451 rna_update_cb(C, cb_v, NULL);
1454 static void colorband_update_cb(bContext *UNUSED(C), void *bt_v, void *coba_v)
1457 ColorBand *coba = coba_v;
1459 /* sneaky update here, we need to sort the colorband points to be in order,
1460 * however the RNA pointer then is wrong, so we update it */
1461 colorband_update_sort(coba);
1462 bt->rnapoin.data = coba->data + coba->cur;
1465 /* offset aligns from bottom, standard width 300, height 115 */
1466 static void colorband_buttons_large(uiLayout *layout, uiBlock *block, ColorBand *coba,
1467 int xoffs, int yoffs, RNAUpdateCb *cb)
1471 const int line1_y = yoffs + 65 + UI_UNIT_Y + 2; /* 2 for some space between the buttons */
1472 const int line2_y = yoffs + 65;
1474 if (coba == NULL) return;
1476 bt = uiDefBut(block, BUT, 0, IFACE_("Add"), 0 + xoffs, line1_y, 40, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1477 TIP_("Add a new color stop to the colorband"));
1478 uiButSetNFunc(bt, colorband_add_cb, MEM_dupallocN(cb), coba);
1480 bt = uiDefBut(block, BUT, 0, IFACE_("Delete"), 45 + xoffs, line1_y, 45, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1481 TIP_("Delete the active position"));
1482 uiButSetNFunc(bt, colorband_del_cb, MEM_dupallocN(cb), coba);
1485 /* XXX, todo for later - convert to operator - campbell */
1486 bt = uiDefBut(block, BUT, 0, "F", 95 + xoffs, line1_y, 20, UI_UNIT_Y,
1487 NULL, 0, 0, 0, 0, TIP_("Flip colorband"));
1488 uiButSetNFunc(bt, colorband_flip_cb, MEM_dupallocN(cb), coba);
1490 uiDefButS(block, NUM, 0, "", 120 + xoffs, line1_y, 80, UI_UNIT_Y, &coba->cur, 0.0, (float)(MAX2(0, coba->tot - 1)),
1491 0, 0, TIP_("Choose active color stop"));
1493 bt = uiDefButS(block, MENU, 0, IFACE_("Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4"),
1494 210 + xoffs, line1_y, 90, UI_UNIT_Y, &coba->ipotype, 0.0, 0.0, 0, 0,
1495 TIP_("Set interpolation between color stops"));
1496 uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1497 uiBlockEndAlign(block);
1499 bt = uiDefBut(block, BUT_COLORBAND, 0, "", xoffs, line2_y, 300, UI_UNIT_Y, coba, 0, 0, 0, 0, "");
1500 uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1505 CBData *cbd = coba->data + coba->cur;
1507 /* better to use rna so we can animate them */
1509 RNA_pointer_create(cb->ptr.id.data, &RNA_ColorRampElement, cbd, &ptr);
1510 row = uiLayoutRow(layout, FALSE);
1512 uiItemR(row, &ptr, "position", 0, IFACE_("Pos"), ICON_NONE);
1513 bt = block->buttons.last;
1514 uiButSetFunc(bt, colorband_update_cb, bt, coba);
1516 uiItemR(row, &ptr, "color", 0, "", ICON_NONE);
1521 static void colorband_buttons_small(uiLayout *layout, uiBlock *block, ColorBand *coba, rctf *butr, RNAUpdateCb *cb)
1524 float unit = BLI_rctf_size_x(butr) / 14.0f;
1525 float xs = butr->xmin;
1527 uiBlockBeginAlign(block);
1528 bt = uiDefBut(block, BUT, 0, IFACE_("Add"), xs, butr->ymin + UI_UNIT_Y, 2.0f * unit, UI_UNIT_Y, NULL, 0, 0, 0, 0,
1529 TIP_("Add a new color stop to the colorband"));
1530 uiButSetNFunc(bt, colorband_add_cb, MEM_dupallocN(cb), coba);
1531 bt = uiDefBut(block, BUT, 0, IFACE_("Delete"), xs + 2.0f * unit, butr->ymin + UI_UNIT_Y, 1.5f * unit, UI_UNIT_Y,
1532 NULL, 0, 0, 0, 0, TIP_("Delete the active position"));
1533 uiButSetNFunc(bt, colorband_del_cb, MEM_dupallocN(cb), coba);
1534 bt = uiDefBut(block, BUT, 0, "F", xs + 3.5f * unit, butr->ymin + UI_UNIT_Y, 0.5f * unit, UI_UNIT_Y,
1535 NULL, 0, 0, 0, 0, TIP_("Flip the color ramp"));
1536 uiButSetNFunc(bt, colorband_flip_cb, MEM_dupallocN(cb), coba);
1537 uiBlockEndAlign(block);
1540 CBData *cbd = coba->data + coba->cur;
1542 RNA_pointer_create(cb->ptr.id.data, &RNA_ColorRampElement, cbd, &ptr);
1543 uiItemR(layout, &ptr, "color", 0, "", ICON_NONE);
1546 bt = uiDefButS(block, MENU, 0, IFACE_("Interpolation %t|Ease %x1|Cardinal %x3|Linear %x0|B-Spline %x2|Constant %x4"),
1547 xs + 10.0f * unit, butr->ymin + UI_UNIT_Y, unit * 4, UI_UNIT_Y, &coba->ipotype, 0.0, 0.0, 0, 0,
1548 TIP_("Set interpolation between color stops"));
1549 uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1551 bt = uiDefBut(block, BUT_COLORBAND, 0, "", xs, butr->ymin, BLI_rctf_size_x(butr), UI_UNIT_Y, coba, 0, 0, 0, 0, "");
1552 uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1554 uiBlockEndAlign(block);
1557 static void colorband_buttons_layout(uiLayout *layout, uiBlock *block, ColorBand *coba, rctf *butr,
1558 int small, RNAUpdateCb *cb)
1561 colorband_buttons_small(layout, block, coba, butr, cb);
1563 colorband_buttons_large(layout, block, coba, 0, 0, cb);
1566 void uiTemplateColorRamp(uiLayout *layout, PointerRNA *ptr, const char *propname, int expand)
1568 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1574 if (!prop || RNA_property_type(prop) != PROP_POINTER)
1577 cptr = RNA_property_pointer_get(ptr, prop);
1578 if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_ColorRamp))
1581 cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1585 rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1586 rect.ymin = 0; rect.ymax = 19.5f * UI_UNIT_X;
1588 block = uiLayoutAbsoluteBlock(layout);
1589 colorband_buttons_layout(layout, block, cptr.data, &rect, !expand, cb);
1595 /********************* Icon viewer Template ************************/
1597 /* ID Search browse menu, open */
1598 static uiBlock *icon_view_menu(bContext *C, ARegion *ar, void *arg_litem)
1600 static RNAUpdateCb cb;
1604 EnumPropertyItem *item;
1607 /* arg_litem is malloced, can be freed by parent button */
1608 cb = *((RNAUpdateCb *)arg_litem);
1611 // icon = RNA_property_enum_get(&cb.ptr, cb.prop);
1613 block = uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1614 uiBlockSetFlag(block, UI_BLOCK_LOOP | UI_BLOCK_REDRAW);
1617 RNA_property_enum_items(C, &cb.ptr, cb.prop, &item, NULL, &free);
1619 for (a = 0; item[a].identifier; a++) {
1622 /* XXX hardcoded size to 5 x unit */
1623 x = (a % 8) * UI_UNIT_X * 5;
1624 y = (a / 8) * UI_UNIT_X * 5;
1626 icon = item[a].icon;
1627 but = uiDefIconButR_prop(block, ROW, 0, icon, x, y, UI_UNIT_X * 5, UI_UNIT_Y * 5, &cb.ptr, cb.prop, -1, 0, icon, -1, -1, NULL);
1628 uiButSetFlag(but, UI_HAS_ICON | UI_ICON_PREVIEW);
1631 uiBoundsBlock(block, 0.3f * U.widget_unit);
1632 uiBlockSetDirection(block, UI_TOP);
1633 uiEndBlock(C, block);
1642 void uiTemplateIconView(uiLayout *layout, PointerRNA *ptr, const char *propname)
1644 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1648 // rctf rect; /* UNUSED */
1651 if (!prop || RNA_property_type(prop) != PROP_ENUM)
1654 icon = RNA_property_enum_get(ptr, prop);
1656 cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1660 // rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1661 // rect.ymin = 0; rect.ymax = 10.0f * UI_UNIT_X;
1663 block = uiLayoutAbsoluteBlock(layout);
1665 but = uiDefBlockButN(block, icon_view_menu, MEM_dupallocN(cb), "", 0, 0, UI_UNIT_X * 6, UI_UNIT_Y * 6, "");
1668 // but = uiDefIconButR_prop(block, ROW, 0, icon, 0, 0, BLI_rctf_size_x(&rect), BLI_rctf_size_y(&rect), ptr, prop, -1, 0, icon, -1, -1, NULL);
1671 uiButSetFlag(but, UI_HAS_ICON | UI_ICON_PREVIEW);
1673 uiButSetNFunc(but, rna_update_cb, MEM_dupallocN(cb), NULL);
1678 /********************* Histogram Template ************************/
1680 void uiTemplateHistogram(uiLayout *layout, PointerRNA *ptr, const char *propname)
1682 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1690 if (!prop || RNA_property_type(prop) != PROP_POINTER)
1693 cptr = RNA_property_pointer_get(ptr, prop);
1694 if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Histogram))
1697 cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1701 rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1702 rect.ymin = 0; rect.ymax = 9.5f * UI_UNIT_Y;
1704 block = uiLayoutAbsoluteBlock(layout);
1705 //colorband_buttons_layout(layout, block, cptr.data, &rect, !expand, cb);
1707 hist = (Histogram *)cptr.data;
1709 hist->height = (hist->height <= 20) ? 20 : hist->height;
1711 bt = uiDefBut(block, HISTOGRAM, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect), UI_DPI_FAC * hist->height,
1712 hist, 0, 0, 0, 0, "");
1714 uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1719 /********************* Waveform Template ************************/
1721 void uiTemplateWaveform(uiLayout *layout, PointerRNA *ptr, const char *propname)
1723 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1731 if (!prop || RNA_property_type(prop) != PROP_POINTER)
1734 cptr = RNA_property_pointer_get(ptr, prop);
1735 if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Scopes))
1737 scopes = (Scopes *)cptr.data;
1739 cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1743 rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1744 rect.ymin = 0; rect.ymax = 9.5f * UI_UNIT_Y;
1746 block = uiLayoutAbsoluteBlock(layout);
1748 scopes->wavefrm_height = (scopes->wavefrm_height <= 20) ? 20 : scopes->wavefrm_height;
1750 bt = uiDefBut(block, WAVEFORM, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect), UI_DPI_FAC * scopes->wavefrm_height,
1751 scopes, 0, 0, 0, 0, "");
1752 (void)bt; /* UNUSED */
1757 /********************* Vectorscope Template ************************/
1759 void uiTemplateVectorscope(uiLayout *layout, PointerRNA *ptr, const char *propname)
1761 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1769 if (!prop || RNA_property_type(prop) != PROP_POINTER)
1772 cptr = RNA_property_pointer_get(ptr, prop);
1773 if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Scopes))
1775 scopes = (Scopes *)cptr.data;
1777 cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
1781 rect.xmin = 0; rect.xmax = 10.0f * UI_UNIT_X;
1782 rect.ymin = 0; rect.ymax = 9.5f * UI_UNIT_Y;
1784 block = uiLayoutAbsoluteBlock(layout);
1786 scopes->vecscope_height = (scopes->vecscope_height <= 20) ? 20 : scopes->vecscope_height;
1788 bt = uiDefBut(block, VECTORSCOPE, 0, "", rect.xmin, rect.ymin, BLI_rctf_size_x(&rect),
1789 UI_DPI_FAC * scopes->vecscope_height, scopes, 0, 0, 0, 0, "");
1790 uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
1795 /********************* CurveMapping Template ************************/
1798 static void curvemap_buttons_zoom_in(bContext *C, void *cumap_v, void *UNUSED(arg))
1800 CurveMapping *cumap = cumap_v;
1803 /* we allow 20 times zoom */
1804 if (BLI_rctf_size_x(&cumap->curr) > 0.04f * BLI_rctf_size_x(&cumap->clipr)) {
1805 d = 0.1154f * BLI_rctf_size_x(&cumap->curr);
1806 cumap->curr.xmin += d;
1807 cumap->curr.xmax -= d;
1808 d = 0.1154f * BLI_rctf_size_y(&cumap->curr);
1809 cumap->curr.ymin += d;
1810 cumap->curr.ymax -= d;
1813 ED_region_tag_redraw(CTX_wm_region(C));
1816 static void curvemap_buttons_zoom_out(bContext *C, void *cumap_v, void *UNUSED(unused))
1818 CurveMapping *cumap = cumap_v;
1821 /* we allow 20 times zoom, but don't view outside clip */
1822 if (BLI_rctf_size_x(&cumap->curr) < 20.0f * BLI_rctf_size_x(&cumap->clipr)) {
1823 d = d1 = 0.15f * BLI_rctf_size_x(&cumap->curr);
1825 if (cumap->flag & CUMA_DO_CLIP)
1826 if (cumap->curr.xmin - d < cumap->clipr.xmin)
1827 d1 = cumap->curr.xmin - cumap->clipr.xmin;
1828 cumap->curr.xmin -= d1;
1831 if (cumap->flag & CUMA_DO_CLIP)
1832 if (cumap->curr.xmax + d > cumap->clipr.xmax)
1833 d1 = -cumap->curr.xmax + cumap->clipr.xmax;
1834 cumap->curr.xmax += d1;
1836 d = d1 = 0.15f * BLI_rctf_size_y(&cumap->curr);
1838 if (cumap->flag & CUMA_DO_CLIP)
1839 if (cumap->curr.ymin - d < cumap->clipr.ymin)
1840 d1 = cumap->curr.ymin - cumap->clipr.ymin;
1841 cumap->curr.ymin -= d1;
1844 if (cumap->flag & CUMA_DO_CLIP)
1845 if (cumap->curr.ymax + d > cumap->clipr.ymax)
1846 d1 = -cumap->curr.ymax + cumap->clipr.ymax;
1847 cumap->curr.ymax += d1;
1850 ED_region_tag_redraw(CTX_wm_region(C));
1853 static void curvemap_buttons_setclip(bContext *UNUSED(C), void *cumap_v, void *UNUSED(arg))
1855 CurveMapping *cumap = cumap_v;
1857 curvemapping_changed(cumap, FALSE);
1860 static void curvemap_buttons_delete(bContext *C, void *cb_v, void *cumap_v)
1862 CurveMapping *cumap = cumap_v;
1864 curvemap_remove(cumap->cm + cumap->cur, SELECT);
1865 curvemapping_changed(cumap, FALSE);
1867 rna_update_cb(C, cb_v, NULL);
1870 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
1871 static uiBlock *curvemap_clipping_func(bContext *C, ARegion *ar, void *cumap_v)
1873 CurveMapping *cumap = cumap_v;
1876 float width = 8 * UI_UNIT_X;
1878 block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1880 /* use this for a fake extra empy space around the buttons */
1881 uiDefBut(block, LABEL, 0, "", -4, 16, width + 8, 6 * UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
1883 bt = uiDefButBitI(block, TOG, CUMA_DO_CLIP, 1, IFACE_("Use Clipping"),
1884 0, 5 * UI_UNIT_Y, width, UI_UNIT_Y, &cumap->flag, 0.0, 0.0, 10, 0, "");
1885 uiButSetFunc(bt, curvemap_buttons_setclip, cumap, NULL);
1887 uiBlockBeginAlign(block);
1888 uiDefButF(block, NUM, 0, IFACE_("Min X "), 0, 4 * UI_UNIT_Y, width, UI_UNIT_Y,
1889 &cumap->clipr.xmin, -100.0, cumap->clipr.xmax, 10, 2, "");
1890 uiDefButF(block, NUM, 0, IFACE_("Min Y "), 0, 3 * UI_UNIT_Y, width, UI_UNIT_Y,
1891 &cumap->clipr.ymin, -100.0, cumap->clipr.ymax, 10, 2, "");
1892 uiDefButF(block, NUM, 0, IFACE_("Max X "), 0, 2 * UI_UNIT_Y, width, UI_UNIT_Y,
1893 &cumap->clipr.xmax, cumap->clipr.xmin, 100.0, 10, 2, "");
1894 uiDefButF(block, NUM, 0, IFACE_("Max Y "), 0, UI_UNIT_Y, width, UI_UNIT_Y,
1895 &cumap->clipr.ymax, cumap->clipr.ymin, 100.0, 10, 2, "");
1897 uiBlockSetDirection(block, UI_RIGHT);
1899 uiEndBlock(C, block);
1903 /* only for curvemap_tools_dofunc */
1905 UICURVE_FUNC_RESET_NEG,
1906 UICURVE_FUNC_RESET_POS,
1907 UICURVE_FUNC_RESET_VIEW,
1908 UICURVE_FUNC_HANDLE_VECTOR,
1909 UICURVE_FUNC_HANDLE_AUTO,
1910 UICURVE_FUNC_EXTEND_HOZ,
1911 UICURVE_FUNC_EXTEND_EXP,
1914 static void curvemap_tools_dofunc(bContext *C, void *cumap_v, int event)
1916 CurveMapping *cumap = cumap_v;
1917 CurveMap *cuma = cumap->cm + cumap->cur;
1920 case UICURVE_FUNC_RESET_NEG:
1921 case UICURVE_FUNC_RESET_POS: /* reset */
1922 curvemap_reset(cuma, &cumap->clipr, cumap->preset,
1923 (event == -1) ? CURVEMAP_SLOPE_NEGATIVE : CURVEMAP_SLOPE_POSITIVE);
1924 curvemapping_changed(cumap, FALSE);
1926 case UICURVE_FUNC_RESET_VIEW:
1927 cumap->curr = cumap->clipr;
1929 case UICURVE_FUNC_HANDLE_VECTOR: /* set vector */
1930 curvemap_sethandle(cuma, 1);
1931 curvemapping_changed(cumap, FALSE);
1933 case UICURVE_FUNC_HANDLE_AUTO: /* set auto */
1934 curvemap_sethandle(cuma, 0);
1935 curvemapping_changed(cumap, FALSE);
1937 case UICURVE_FUNC_EXTEND_HOZ: /* extend horiz */
1938 cuma->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1939 curvemapping_changed(cumap, FALSE);
1941 case UICURVE_FUNC_EXTEND_EXP: /* extend extrapolate */
1942 cuma->flag |= CUMA_EXTEND_EXTRAPOLATE;
1943 curvemapping_changed(cumap, FALSE);
1946 ED_region_tag_redraw(CTX_wm_region(C));
1949 static uiBlock *curvemap_tools_func(bContext *C, ARegion *ar, void *cumap_v)
1952 short yco = 0, menuwidth = 10 * UI_UNIT_X;
1954 block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1955 uiBlockSetButmFunc(block, curvemap_tools_dofunc, cumap_v);
1957 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset View"), 0, yco -= UI_UNIT_Y,
1958 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_RESET_VIEW, "");
1959 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Vector Handle"), 0, yco -= UI_UNIT_Y,
1960 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_HANDLE_VECTOR, "");
1961 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Auto Handle"), 0, yco -= UI_UNIT_Y,
1962 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_HANDLE_AUTO, "");
1963 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Extend Horizontal"), 0, yco -= UI_UNIT_Y,
1964 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_EXTEND_HOZ, "");
1965 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Extend Extrapolated"), 0, yco -= UI_UNIT_Y,
1966 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_EXTEND_EXP, "");
1967 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset Curve"), 0, yco -= UI_UNIT_Y,
1968 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_RESET_POS, "");
1970 uiBlockSetDirection(block, UI_RIGHT);
1971 uiTextBoundsBlock(block, 50);
1973 uiEndBlock(C, block);
1977 static uiBlock *curvemap_brush_tools_func(bContext *C, ARegion *ar, void *cumap_v)
1980 short yco = 0, menuwidth = 10 * UI_UNIT_X;
1982 block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
1983 uiBlockSetButmFunc(block, curvemap_tools_dofunc, cumap_v);
1985 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset View"), 0, yco -= UI_UNIT_Y,
1986 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_RESET_VIEW, "");
1987 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Vector Handle"), 0, yco -= UI_UNIT_Y,
1988 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_HANDLE_VECTOR, "");
1989 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Auto Handle"), 0, yco -= UI_UNIT_Y,
1990 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_HANDLE_AUTO, "");
1991 uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, IFACE_("Reset Curve"), 0, yco -= UI_UNIT_Y,
1992 menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, UICURVE_FUNC_RESET_NEG, "");
1994 uiBlockSetDirection(block, UI_RIGHT);
1995 uiTextBoundsBlock(block, 50);
1997 uiEndBlock(C, block);
2001 static void curvemap_buttons_redraw(bContext *C, void *UNUSED(arg1), void *UNUSED(arg2))
2003 ED_region_tag_redraw(CTX_wm_region(C));
2006 static void curvemap_buttons_update(bContext *C, void *arg1_v, void *cumap_v)
2008 CurveMapping *cumap = cumap_v;
2009 curvemapping_changed(cumap, TRUE);
2010 rna_update_cb(C, arg1_v, NULL);
2013 static void curvemap_buttons_reset(bContext *C, void *cb_v, void *cumap_v)
2015 CurveMapping *cumap = cumap_v;
2018 cumap->preset = CURVE_PRESET_LINE;
2019 for (a = 0; a < CM_TOT; a++)
2020 curvemap_reset(cumap->cm + a, &cumap->clipr, cumap->preset, CURVEMAP_SLOPE_POSITIVE);
2022 cumap->black[0] = cumap->black[1] = cumap->black[2] = 0.0f;
2023 cumap->white[0] = cumap->white[1] = cumap->white[2] = 1.0f;
2024 curvemapping_set_black_white(cumap, NULL, NULL);
2026 curvemapping_changed(cumap, FALSE);
2028 rna_update_cb(C, cb_v, NULL);
2031 /* still unsure how this call evolves... we use labeltype for defining what curve-channels to show */
2032 static void curvemap_buttons_layout(uiLayout *layout, PointerRNA *ptr, char labeltype, int levels,
2033 int brush, RNAUpdateCb *cb)
2035 CurveMapping *cumap = ptr->data;
2036 CurveMap *cm = &cumap->cm[cumap->cur];
2037 CurveMapPoint *cmp = NULL;
2038 uiLayout *row, *sub, *split;
2041 float dx = UI_UNIT_X;
2045 block = uiLayoutGetBlock(layout);
2048 row = uiLayoutRow(layout, FALSE);
2050 if (labeltype == 'v') {
2052 sub = uiLayoutRow(row, TRUE);
2053 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
2055 if (cumap->cm[0].curve) {
2056 bt = uiDefButI(block, ROW, 0, "X", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
2057 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2059 if (cumap->cm[1].curve) {
2060 bt = uiDefButI(block, ROW, 0, "Y", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
2061 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2063 if (cumap->cm[2].curve) {
2064 bt = uiDefButI(block, ROW, 0, "Z", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
2065 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2068 else if (labeltype == 'c') {
2070 sub = uiLayoutRow(row, TRUE);
2071 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
2073 if (cumap->cm[3].curve) {
2074 bt = uiDefButI(block, ROW, 0, "C", 0, 0, dx, dx, &cumap->cur, 0.0, 3.0, 0.0, 0.0, "");
2075 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2077 if (cumap->cm[0].curve) {
2078 bt = uiDefButI(block, ROW, 0, "R", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
2079 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2081 if (cumap->cm[1].curve) {
2082 bt = uiDefButI(block, ROW, 0, "G", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
2083 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2085 if (cumap->cm[2].curve) {
2086 bt = uiDefButI(block, ROW, 0, "B", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
2087 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2090 else if (labeltype == 'h') {
2092 sub = uiLayoutRow(row, TRUE);
2093 uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_LEFT);
2095 if (cumap->cm[0].curve) {
2096 bt = uiDefButI(block, ROW, 0, "H", 0, 0, dx, dx, &cumap->cur, 0.0, 0.0, 0.0, 0.0, "");
2097 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2099 if (cumap->cm[1].curve) {
2100 bt = uiDefButI(block, ROW, 0, "S", 0, 0, dx, dx, &cumap->cur, 0.0, 1.0, 0.0, 0.0, "");
2101 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2103 if (cumap->cm[2].curve) {
2104 bt = uiDefButI(block, ROW, 0, "V", 0, 0, dx, dx, &cumap->cur, 0.0, 2.0, 0.0, 0.0, "");
2105 uiButSetFunc(bt, curvemap_buttons_redraw, NULL, NULL);
2109 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_RIGHT);
2111 if (labeltype == 'h')
2114 /* operation buttons */
2115 sub = uiLayoutRow(row, TRUE);
2117 uiBlockSetEmboss(block, UI_EMBOSSN);
2119 bt = uiDefIconBut(block, BUT, 0, ICON_ZOOMIN, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Zoom in"));
2120 uiButSetFunc(bt, curvemap_buttons_zoom_in, cumap, NULL);
2122 bt = uiDefIconBut(block, BUT, 0, ICON_ZOOMOUT, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Zoom out"));
2123 uiButSetFunc(bt, curvemap_buttons_zoom_out, cumap, NULL);
2126 bt = uiDefIconBlockBut(block, curvemap_brush_tools_func, cumap, 0, ICON_MODIFIER, 0, 0, dx, dx, TIP_("Tools"));
2128 bt = uiDefIconBlockBut(block, curvemap_tools_func, cumap, 0, ICON_MODIFIER, 0, 0, dx, dx, TIP_("Tools"));
2130 uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
2132 icon = (cumap->flag & CUMA_DO_CLIP) ? ICON_CLIPUV_HLT : ICON_CLIPUV_DEHLT;
2133 bt = uiDefIconBlockBut(block, curvemap_clipping_func, cumap, 0, icon, 0, 0, dx, dx, TIP_("Clipping Options"));
2134 uiButSetNFunc(bt, rna_update_cb, MEM_dupallocN(cb), NULL);
2136 bt = uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, dx, dx, NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Delete points"));
2137 uiButSetNFunc(bt, curvemap_buttons_delete, MEM_dupallocN(cb), cumap);
2139 uiBlockSetEmboss(block, UI_EMBOSS);
2141 uiBlockSetNFunc(block, rna_update_cb, MEM_dupallocN(cb), NULL);
2144 size = uiLayoutGetWidth(layout);
2145 row = uiLayoutRow(layout, FALSE);
2146 uiDefBut(block, BUT_CURVE, 0, "", 0, 0, size, 8.0f * UI_UNIT_X, cumap, 0.0f, 1.0f, bg, 0, "");
2148 /* sliders for selected point */
2149 for (i = 0; i < cm->totpoint; i++) {
2150 if (cm->curve[i].flag & CUMA_SELECT) {
2151 cmp = &cm->curve[i];
2159 if (cumap->flag & CUMA_DO_CLIP) {
2160 bounds = cumap->clipr;
2163 bounds.xmin = bounds.ymin = -1000.0;
2164 bounds.xmax = bounds.ymax = 1000.0;
2167 uiLayoutRow(layout, TRUE);
2168 uiBlockSetNFunc(block, curvemap_buttons_update, MEM_dupallocN(cb), cumap);
2169 uiDefButF(block, NUM, 0, "X", 0, 2 * UI_UNIT_Y, UI_UNIT_X * 10, UI_UNIT_Y,
2170 &cmp->x, bounds.xmin, bounds.xmax, 1, 5, "");
2171 uiDefButF(block, NUM, 0, "Y", 0, 1 * UI_UNIT_Y, UI_UNIT_X * 10, UI_UNIT_Y,
2172 &cmp->y, bounds.ymin, bounds.ymax, 1, 5, "");
2175 /* black/white levels */
2177 split = uiLayoutSplit(layout, 0.0f, FALSE);
2178 uiItemR(uiLayoutColumn(split, FALSE), ptr, "black_level", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2179 uiItemR(uiLayoutColumn(split, FALSE), ptr, "white_level", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2181 uiLayoutRow(layout, FALSE);
2182 bt = uiDefBut(block, BUT, 0, IFACE_("Reset"), 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0,
2183 TIP_("Reset Black/White point and curves"));
2184 uiButSetNFunc(bt, curvemap_buttons_reset, MEM_dupallocN(cb), cumap);
2187 uiBlockSetNFunc(block, NULL, NULL, NULL);
2190 void uiTemplateCurveMapping(uiLayout *layout, PointerRNA *ptr, const char *propname, int type, int levels, int brush)
2193 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2197 RNA_warning("curve property not found: %s.%s",
2198 RNA_struct_identifier(ptr->type), propname);
2202 if (RNA_property_type(prop) != PROP_POINTER) {
2203 RNA_warning("curve is not a pointer: %s.%s",
2204 RNA_struct_identifier(ptr->type), propname);
2208 cptr = RNA_property_pointer_get(ptr, prop);
2209 if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_CurveMapping))
2212 cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
2216 curvemap_buttons_layout(layout, &cptr, type, levels, brush, cb);
2221 /********************* ColorPicker Template ************************/
2223 #define WHEEL_SIZE (5 * U.widget_unit)
2225 /* This template now follows User Preference for type - name is not correct anymore... */
2226 void uiTemplateColorPicker(uiLayout *layout, PointerRNA *ptr, const char *propname, int value_slider,
2227 int lock, int lock_luminosity, int cubic)
2229 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
2230 uiBlock *block = uiLayoutGetBlock(layout);
2231 uiLayout *col, *row;
2233 float softmin, softmax, step, precision;
2236 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2240 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
2242 col = uiLayoutColumn(layout, TRUE);
2243 row = uiLayoutRow(col, TRUE);
2245 switch (U.color_picker_type) {
2246 case USER_CP_CIRCLE:
2247 but = uiDefButR_prop(block, HSVCIRCLE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2248 -1, 0.0, 0.0, 0, 0, "");
2250 case USER_CP_SQUARE_SV:
2251 but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2252 -1, 0.0, 0.0, UI_GRAD_SV, 0, "");
2254 case USER_CP_SQUARE_HS:
2255 but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2256 -1, 0.0, 0.0, UI_GRAD_HS, 0, "");
2258 case USER_CP_SQUARE_HV:
2259 but = uiDefButR_prop(block, HSVCUBE, 0, "", 0, 0, WHEEL_SIZE, WHEEL_SIZE, ptr, prop,
2260 -1, 0.0, 0.0, UI_GRAD_HV, 0, "");
2265 but->flag |= UI_BUT_COLOR_LOCK;
2268 if (lock_luminosity) {
2269 float color[4]; /* in case of alpha */
2270 but->flag |= UI_BUT_VEC_SIZE_LOCK;
2271 RNA_property_float_get_array(ptr, prop, color);
2272 but->a2 = len_v3(color);
2276 but->flag |= UI_BUT_COLOR_CUBIC;
2280 switch (U.color_picker_type) {
2281 case USER_CP_CIRCLE:
2283 uiDefButR_prop(block, HSVCUBE, 0, "", WHEEL_SIZE + 6, 0, 14, WHEEL_SIZE, ptr, prop,
2284 -1, softmin, softmax, UI_GRAD_V_ALT, 0, "");
2286 case USER_CP_SQUARE_SV:
2288 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2289 -1, softmin, softmax, UI_GRAD_SV + 3, 0, "");
2291 case USER_CP_SQUARE_HS:
2293 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2294 -1, softmin, softmax, UI_GRAD_HS + 3, 0, "");
2296 case USER_CP_SQUARE_HV:
2298 uiDefButR_prop(block, HSVCUBE, 0, "", 0, 4, WHEEL_SIZE, 18, ptr, prop,
2299 -1, softmin, softmax, UI_GRAD_HV + 3, 0, "");
2305 /********************* Layer Buttons Template ************************/
2307 static void handle_layer_buttons(bContext *C, void *arg1, void *arg2)
2310 int cur = GET_INT_FROM_POINTER(arg2);
2311 wmWindow *win = CTX_wm_window(C);
2312 int i, tot, shift = win->eventstate->shift;
2315 tot = RNA_property_array_length(&but->rnapoin, but->rnaprop);
2317 /* Normally clicking only selects one layer */
2318 RNA_property_boolean_set_index(&but->rnapoin, but->rnaprop, cur, TRUE);
2319 for (i = 0; i < tot; ++i) {
2321 RNA_property_boolean_set_index(&but->rnapoin, but->rnaprop, i, FALSE);
2325 /* view3d layer change should update depsgraph (invisible object changed maybe) */
2326 /* see view3d_header.c */
2330 * - for now, grouping of layers is determined by dividing up the length of
2331 * the array of layer bitflags */
2333 void uiTemplateLayers(uiLayout *layout, PointerRNA *ptr, const char *propname,
2334 PointerRNA *used_ptr, const char *used_propname, int active_layer)
2336 uiLayout *uRow, *uCol;
2337 PropertyRNA *prop, *used_prop = NULL;
2338 int groups, cols, layers;
2339 int group, col, layer, row;
2340 int cols_per_group = 5;
2342 prop = RNA_struct_find_property(ptr, propname);
2344 RNA_warning("layers property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2348 /* the number of layers determines the way we group them
2349 * - we want 2 rows only (for now)
2350 * - the number of columns (cols) is the total number of buttons per row
2351 * the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
2352 * - for now, only split into groups if group will have at least 5 items
2354 layers = RNA_property_array_length(ptr, prop);
2355 cols = (layers / 2) + (layers % 2);
2356 groups = ((cols / 2) < cols_per_group) ? (1) : (cols / cols_per_group);
2358 if (used_ptr && used_propname) {
2359 used_prop = RNA_struct_find_property(used_ptr, used_propname);
2361 RNA_warning("used layers property not found: %s.%s", RNA_struct_identifier(ptr->type), used_propname);
2365 if (RNA_property_array_length(used_ptr, used_prop) < layers)
2369 /* layers are laid out going across rows, with the columns being divided into groups */
2371 for (group = 0; group < groups; group++) {
2372 uCol = uiLayoutColumn(layout, TRUE);
2374 for (row = 0; row < 2; row++) {
2378 uRow = uiLayoutRow(uCol, TRUE);
2379 block = uiLayoutGetBlock(uRow);
2380 layer = groups * cols_per_group * row + cols_per_group * group;
2382 /* add layers as toggle buts */
2383 for (col = 0; (col < cols_per_group) && (layer < layers); col++, layer++) {
2385 int butlay = 1 << layer;
2387 if (active_layer & butlay)
2388 icon = ICON_LAYER_ACTIVE;
2389 else if (used_prop && RNA_property_boolean_get_index(used_ptr, used_prop, layer))
2390 icon = ICON_LAYER_USED;
2392 but = uiDefAutoButR(block, ptr, prop, layer, "", icon, 0, 0, UI_UNIT_X / 2, UI_UNIT_Y / 2);
2393 uiButSetFunc(but, handle_layer_buttons, but, SET_INT_IN_POINTER(layer));
2400 void uiTemplateGameStates(uiLayout *layout, PointerRNA *ptr, const char *propname,
2401 PointerRNA *used_ptr, const char *used_propname, int active_state)
2403 uiLayout *uRow, *uCol;
2404 PropertyRNA *prop, *used_prop = NULL;
2405 int groups, cols, states;
2406 int group, col, state, row;
2407 int cols_per_group = 5;
2408 Object *ob = (Object *)ptr->id.data;
2410 prop = RNA_struct_find_property(ptr, propname);
2412 RNA_warning("states property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
2416 /* the number of states determines the way we group them
2417 * - we want 2 rows only (for now)
2418 * - the number of columns (cols) is the total number of buttons per row
2419 * the 'remainder' is added to this, as it will be ok to have first row slightly wider if need be
2420 * - for now, only split into groups if group will have at least 5 items
2422 states = RNA_property_array_length(ptr, prop);
2423 cols = (states / 2) + (states % 2);
2424 groups = ((cols / 2) < cols_per_group) ? (1) : (cols / cols_per_group);
2426 if (used_ptr && used_propname) {
2427 used_prop = RNA_struct_find_property(used_ptr, used_propname);
2429 RNA_warning("used layers property not found: %s.%s", RNA_struct_identifier(ptr->type), used_propname);
2433 if (RNA_property_array_length(used_ptr, used_prop) < states)
2437 /* layers are laid out going across rows, with the columns being divided into groups */
2439 for (group = 0; group < groups; group++) {
2440 uCol = uiLayoutColumn(layout, TRUE);
2442 for (row = 0; row < 2; row++) {
2446 uRow = uiLayoutRow(uCol, TRUE);
2447 block = uiLayoutGetBlock(uRow);
2448 state = groups * cols_per_group * row + cols_per_group * group;
2450 /* add layers as toggle buts */
2451 for (col = 0; (col < cols_per_group) && (state < states); col++, state++) {
2453 int butlay = 1 << state;
2455 if (active_state & butlay)
2456 icon = ICON_LAYER_ACTIVE;
2457 else if (used_prop && RNA_property_boolean_get_index(used_ptr, used_prop, state))
2458 icon = ICON_LAYER_USED;
2460 but = uiDefIconButR_prop(block, ICONTOG, 0, icon, 0, 0, UI_UNIT_X / 2, UI_UNIT_Y / 2, ptr, prop,
2461 state, 0, 0, -1, -1, sca_state_name_get(ob, state));
2462 uiButSetFunc(but, handle_layer_buttons, but, SET_INT_IN_POINTER(state));
2470 /************************* List Template **************************/
2471 static void uilist_draw_item_default(struct uiList *ui_list, struct bContext *UNUSED(C), struct uiLayout *layout,
2472 struct PointerRNA *UNUSED(dataptr), struct PointerRNA *itemptr, int icon,
2473 struct PointerRNA *UNUSED(active_dataptr), const char *UNUSED(active_propname),
2479 namebuf = RNA_struct_name_get_alloc(itemptr, NULL, 0, NULL);
2480 name = (namebuf) ? namebuf : "";
2483 switch (ui_list->layout_type) {
2484 case UILST_LAYOUT_GRID:
2485 uiItemL(layout, "", icon);
2487 case UILST_LAYOUT_DEFAULT:
2488 case UILST_LAYOUT_COMPACT:
2490 uiItemL(layout, name, icon);
2500 void uiTemplateList(uiLayout *layout, bContext *C, const char *listtype_name, const char *list_id,
2501 PointerRNA *dataptr, const char *propname, PointerRNA *active_dataptr,
2502 const char *active_propname, int rows, int maxrows, int layout_type)
2504 uiListType *ui_list_type;
2505 uiList *ui_list = NULL;
2507 uiListDrawItemFunc draw_item;
2509 PropertyRNA *prop = NULL, *activeprop;
2510 PropertyType type, activetype;
2512 uiLayout *box, *row, *col, *sub, *overlap;
2513 uiBlock *block, *subblock;
2516 char ui_list_id[UI_MAX_NAME_STR];
2518 int rnaicon = ICON_NONE, icon = ICON_NONE;
2519 int i = 0, activei = 0;
2525 /* validate arguments */
2526 /* Forbid default UI_UL_DEFAULT_CLASS_NAME list class without a custom list_id! */
2527 if (!strcmp(UI_UL_DEFAULT_CLASS_NAME, listtype_name) && !(list_id && list_id[0])) {
2528 RNA_warning("template_list using default '%s' UIList class must provide a custom list_id",
2529 UI_UL_DEFAULT_CLASS_NAME);
2533 block = uiLayoutGetBlock(layout);
2535 if (!active_dataptr->data) {
2536 RNA_warning("No active data");
2540 if (dataptr->data) {
2541 prop = RNA_struct_find_property(dataptr, propname);
2543 RNA_warning("Property not found: %s.%s", RNA_struct_identifier(dataptr->type), propname);
2548 activeprop = RNA_struct_find_property(active_dataptr, active_propname);
2550 RNA_warning("Property not found: %s.%s", RNA_struct_identifier(active_dataptr->type), active_propname);
2555 type = RNA_property_type(prop);
2556 if (type != PROP_COLLECTION) {
2557 RNA_warning("Expected a collection data property");
2562 activetype = RNA_property_type(activeprop);
2563 if (activetype != PROP_INT) {
2564 RNA_warning("Expected an integer active data property");
2569 if (dataptr->data && prop) {
2570 ptype = RNA_property_pointer_type(dataptr, prop);
2571 rnaicon = RNA_struct_ui_icon(ptype);
2574 /* get active data */
2575 activei = RNA_property_int_get(active_dataptr, activeprop);
2577 /* Find the uiList type. */
2578 ui_list_type = WM_uilisttype_find(listtype_name, FALSE);
2580 if (ui_list_type == NULL) {
2581 RNA_warning("List type %s not found", listtype_name);
2585 draw_item = ui_list_type->draw_item ? ui_list_type->draw_item : uilist_draw_item_default;
2587 /* Find or add the uiList to the current Region. */
2588 /* We tag the list id with the list type... */
2589 BLI_snprintf(ui_list_id, sizeof(ui_list_id), "%s_%s", ui_list_type->idname, list_id ? list_id : "");
2591 ar = CTX_wm_region(C);
2592 ui_list = BLI_findstring(&ar->ui_lists, ui_list_id, offsetof(uiList, list_id));
2595 ui_list = MEM_callocN(sizeof(uiList), __func__);
2596 BLI_strncpy(ui_list->list_id, ui_list_id, sizeof(ui_list->list_id));
2597 BLI_addtail(&ar->ui_lists, ui_list);
2600 /* Because we can't actually pass type across save&load... */
2601 ui_list->type = ui_list_type;
2602 ui_list->layout_type = layout_type;
2604 switch (layout_type) {
2605 case UILST_LAYOUT_DEFAULT:
2611 if (ui_list->list_grip_size != 0)
2612 rows = ui_list->list_grip_size;
2615 box = uiLayoutListBox(layout, ui_list, dataptr, prop, active_dataptr, activeprop);
2616 row = uiLayoutRow(box, FALSE);
2617 col = uiLayoutColumn(row, TRUE);
2620 RNA_property_int_range(active_dataptr, activeprop, &min, &max);
2623 len = RNA_property_collection_length(dataptr, prop);
2624 items = CLAMPIS(len, rows, MAX2(rows, maxrows));
2626 /* if list length changes and active is out of view, scroll to it */
2627 if ((ui_list->list_last_len != len) &&
2628 (activei < ui_list->list_scroll || activei >= ui_list->list_scroll + items))
2630 ui_list->list_scroll = activei;
2633 ui_list->list_scroll = min_ii(ui_list->list_scroll, len - items);
2634 ui_list->list_scroll = max_ii(ui_list->list_scroll, 0);
2635 ui_list->list_size = items;
2636 ui_list->list_last_len = len;
2638 if (dataptr->data && prop) {
2639 /* create list items */
2640 RNA_PROP_BEGIN (dataptr, itemptr, prop)
2642 if (i >= ui_list->list_scroll && i < ui_list->list_scroll + items) {
2643 subblock = uiLayoutGetBlock(col);
2644 overlap = uiLayoutOverlap(col);
2646 uiBlockSetFlag(subblock, UI_BLOCK_LIST_ITEM);
2648 /* list item behind label & other buttons */
2649 sub = uiLayoutRow(overlap, FALSE);
2651 but = uiDefButR_prop(subblock, LISTROW, 0, "", 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
2652 active_dataptr, activeprop, 0, 0, i, 0, 0, NULL);
2653 uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2655 sub = uiLayoutRow(overlap, FALSE);
2657 icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, false);
2658 if (icon == ICON_DOT)
2660 draw_item(ui_list, C, sub, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2662 /* If we are "drawing" active item, set all labels as active. */
2664 ui_layout_list_set_labels_active(sub);
2667 uiBlockClearFlag(subblock, UI_BLOCK_LIST_ITEM);
2674 /* add dummy buttons to fill space */
2675 while (i < ui_list->list_scroll + items) {
2676 if (i >= ui_list->list_scroll)
2677 uiItemL(col, "", ICON_NONE);
2683 col = uiLayoutColumn(row, FALSE);
2684 uiDefButI(block, SCROLL, 0, "", 0, 0, UI_UNIT_X * 0.75, UI_UNIT_Y * items, &ui_list->list_scroll,
2685 0, len - items, items, 0, "");
2688 case UILST_LAYOUT_COMPACT:
2689 row = uiLayoutRow(layout, TRUE);
2691 if (dataptr->data && prop) {
2692 /* create list items */
2693 RNA_PROP_BEGIN (dataptr, itemptr, prop)
2695 found = (activei == i);
2698 icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, false);
2699 if (icon == ICON_DOT)
2701 draw_item(ui_list, C, row, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2709 /* if list is empty, add in dummy button */
2711 uiItemL(row, "", ICON_NONE);
2713 /* next/prev button */
2714 BLI_snprintf(numstr, sizeof(numstr), "%d :", i);
2715 but = uiDefIconTextButR_prop(block, NUM, 0, 0, numstr, 0, 0, UI_UNIT_X * 5, UI_UNIT_Y,
2716 active_dataptr, activeprop, 0, 0, 0, 0, 0, "");
2718 uiButSetFlag(but, UI_BUT_DISABLED);
2720 case UILST_LAYOUT_GRID:
2721 box = uiLayoutListBox(layout, ui_list, dataptr, prop, active_dataptr, activeprop);
2722 col = uiLayoutColumn(box, TRUE);
2723 row = uiLayoutRow(col, FALSE);
2725 if (dataptr->data && prop) {
2726 /* create list items */
2727 RNA_PROP_BEGIN (dataptr, itemptr, prop)
2731 row = uiLayoutRow(col, FALSE);
2733 subblock = uiLayoutGetBlock(row);
2734 overlap = uiLayoutOverlap(row);
2736 uiBlockSetFlag(subblock, UI_BLOCK_LIST_ITEM);
2738 /* list item behind label & other buttons */
2739 sub = uiLayoutRow(overlap, FALSE);
2741 but = uiDefButR_prop(subblock, LISTROW, 0, "", 0, 0, UI_UNIT_X * 10, UI_UNIT_Y,
2742 active_dataptr, activeprop, 0, 0, i, 0, 0, NULL);
2743 uiButSetFlag(but, UI_BUT_NO_TOOLTIP);
2745 sub = uiLayoutRow(overlap, FALSE);
2747 icon = UI_rnaptr_icon_get(C, &itemptr, rnaicon, false);
2748 draw_item(ui_list, C, sub, dataptr, &itemptr, icon, active_dataptr, active_propname, i);
2750 /* If we are "drawing" active item, set all labels as active. */
2752 ui_layout_list_set_labels_active(sub);
2755 uiBlockClearFlag(subblock, UI_BLOCK_LIST_ITEM);
2765 /************************* Operator Search Template **************************/
2767 static void operator_call_cb(bContext *C, void *UNUSED(arg1), void *arg2)
2769 wmOperatorType *ot = arg2;
2772 WM_operator_name_call(C, ot->idname, WM_OP_INVOKE_DEFAULT, NULL);
2775 static void operator_search_cb(const bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
2777 GHashIterator *iter = WM_operatortype_iter();
2779 for (; !BLI_ghashIterator_done(iter); BLI_ghashIterator_step(iter)) {
2780 wmOperatorType *ot = BLI_ghashIterator_getValue(iter);
2782 if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0)
2785 if (BLI_strcasestr(ot->name, str)) {
2786 if (WM_operator_poll((bContext *)C, ot)) {
2788 int len = strlen(ot->name);
2790 /* display name for menu, can hold hotkey */
2791 BLI_strncpy(name, ot->name, sizeof(name));
2793 /* check for hotkey */
2794 if (len < sizeof(name) - 6) {
2795 if (WM_key_event_operator_string(C, ot->idname, WM_OP_EXEC_DEFAULT, NULL, true,
2796 &name[len + 1], sizeof(name) - len - 1))
2802 if (false == uiSearchItemAdd(items, name, ot, 0))
2807 BLI_ghashIterator_free(iter);
2810 void uiOperatorSearch_But(uiBut *but)
2812 uiButSetSearchFunc(but, operator_search_cb, NULL, operator_call_cb, NULL);
2815 void uiTemplateOperatorSearch(uiLayout *layout)
2819 static char search[256] = "";
2821 block = uiLayoutGetBlock(layout);
2822 uiBlockSetCurLayout(block, layout);
2824 but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, 0, 0, "");
2825 uiOperatorSearch_But(but);
2828 /************************* Running Jobs Template **************************/
2830 #define B_STOPRENDER 1
2831 #define B_STOPCAST 2
2832 #define B_STOPANIM 3
2833 #define B_STOPCOMPO 4
2835 #define B_STOPCLIP 6
2836 #define B_STOPOTHER 7
2838 static void do_running_jobs(bContext *C, void *UNUSED(arg), int event)
2845 WM_jobs_stop(CTX_wm_manager(C), CTX_wm_screen(C), NULL);
2848 WM_operator_name_call(C, "SCREEN_OT_animation_play", WM_OP_INVOKE_SCREEN, NULL);
2851 WM_jobs_stop(CTX_wm_manager(C), CTX_data_scene(C), NULL);
2854 WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2857 WM_jobs_stop(CTX_wm_manager(C), CTX_wm_area(C), NULL);
2865 void uiTemplateRunningJobs(uiLayout *layout, bContext *C)
2867 bScreen *screen = CTX_wm_screen(C);
2868 wmWindowManager *wm = CTX_wm_manager(C);
2869 ScrArea *sa = CTX_wm_area(C);
2874 block = uiLayoutGetBlock(layout);
2875 uiBlockSetCurLayout(block, layout);
2877 uiBlockSetHandleFunc(block, do_running_jobs, NULL);
2879 if (sa->spacetype == SPACE_SEQ) {
2880 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2882 handle_event = B_STOPSEQ;
2884 else if (sa->spacetype == SPACE_CLIP) {
2885 if (WM_jobs_test(wm, sa, WM_JOB_TYPE_ANY))
2887 handle_event = B_STOPCLIP;