This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/editors/interface/interface_utils.c

218 lines
6.2 KiB
C
Raw Normal View History

/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2009 Blender Foundation.
* All rights reserved.
*
* Contributor(s): Blender Foundation
*
* ***** END GPL LICENSE BLOCK *****
*/
2011-02-27 20:29:51 +00:00
/** \file blender/editors/interface/interface_utils.c
* \ingroup edinterface
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "DNA_object_types.h"
#include "BLI_utildefines.h"
#include "BLI_string.h"
#include "BLF_translation.h"
2011-06-15 11:41:15 +00:00
#include "BKE_context.h"
#include "RNA_access.h"
#include "UI_interface.h"
#include "UI_resources.h"
/*************************** RNA Utilities ******************************/
uiBut *uiDefAutoButR(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int index, const char *name, int icon, int x1, int y1, int x2, int y2)
{
2012-03-30 01:51:25 +00:00
uiBut *but = NULL;
2012-03-30 01:51:25 +00:00
switch (RNA_property_type(prop)) {
case PROP_BOOLEAN:
{
2012-03-30 01:51:25 +00:00
int arraylen = RNA_property_array_length(ptr, prop);
if (arraylen && index == -1)
return NULL;
if (icon && name && name[0] == '\0')
but = uiDefIconButR_prop(block, ICONTOG, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
else if (icon)
but = uiDefIconTextButR_prop(block, ICONTOG, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
else
but = uiDefButR_prop(block, OPTION, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
break;
}
case PROP_INT:
case PROP_FLOAT:
{
2012-03-30 01:51:25 +00:00
int arraylen = RNA_property_array_length(ptr, prop);
if (arraylen && index == -1) {
if (ELEM(RNA_property_subtype(prop), PROP_COLOR, PROP_COLOR_GAMMA))
but = uiDefButR_prop(block, COL, 0, name, x1, y1, x2, y2, ptr, prop, 0, 0, 0, -1, -1, NULL);
}
else if (RNA_property_subtype(prop) == PROP_PERCENTAGE || RNA_property_subtype(prop) == PROP_FACTOR)
but = uiDefButR_prop(block, NUMSLI, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
else
but = uiDefButR_prop(block, NUM, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
break;
}
case PROP_ENUM:
if (icon && name && name[0] == '\0')
but = uiDefIconButR_prop(block, MENU, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
else if (icon)
but = uiDefIconTextButR_prop(block, MENU, 0, icon, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
else
but = uiDefButR_prop(block, MENU, 0, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
break;
case PROP_STRING:
if (icon && name && name[0] == '\0')
but = uiDefIconButR_prop(block, TEX, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
else if (icon)
but = uiDefIconTextButR_prop(block, TEX, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
else
but = uiDefButR_prop(block, TEX, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
break;
case PROP_POINTER: {
PointerRNA pptr;
2012-03-30 01:51:25 +00:00
pptr = RNA_property_pointer_get(ptr, prop);
if (!pptr.type)
2012-03-30 01:51:25 +00:00
pptr.type = RNA_property_pointer_type(ptr, prop);
icon = RNA_struct_ui_icon(pptr.type);
if (icon == ICON_DOT)
2012-03-30 01:51:25 +00:00
icon = 0;
but = uiDefIconTextButR_prop(block, IDPOIN, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
break;
}
case PROP_COLLECTION: {
char text[256];
BLI_snprintf(text, sizeof(text), IFACE_("%d items"), RNA_property_collection_length(ptr, prop));
but = uiDefBut(block, LABEL, 0, text, x1, y1, x2, y2, NULL, 0, 0, 0, 0, NULL);
uiButSetFlag(but, UI_BUT_DISABLED);
break;
}
default:
2012-03-30 01:51:25 +00:00
but = NULL;
break;
}
return but;
}
int uiDefAutoButsRNA(uiLayout *layout, PointerRNA *ptr, int (*check_prop)(PointerRNA *, PropertyRNA *), const char label_align)
{
uiLayout *split, *col;
int flag;
const char *name;
2012-03-30 01:51:25 +00:00
int tot = 0;
assert(ELEM3(label_align, '\0', 'H', 'V'));
RNA_STRUCT_BEGIN (ptr, prop) {
2012-03-30 01:51:25 +00:00
flag = RNA_property_flag(prop);
if (flag & PROP_HIDDEN || (check_prop && check_prop(ptr, prop) == FALSE))
continue;
if (label_align != '\0') {
PropertyType type = RNA_property_type(prop);
int is_boolean = (type == PROP_BOOLEAN && !RNA_property_array_check(prop));
2012-03-30 01:51:25 +00:00
name = RNA_property_ui_name(prop);
UI: Layout Engine * Buttons are now created first, and after that the layout is computed. This means the layout engine now works at button level, and makes it easier to write templates. Otherwise you had to store all info and create the buttons later. * Added interface_templates.c as a separate file to put templates in. These can contain regular buttons, and can be put in a Free layout, which means you can specify manual coordinates, but still get nested correct inside other layouts. * API was changed to allow better nesting. Previously items were added in the last added layout specifier, i.e. one level up in the layout hierarchy. This doesn't work well in always, so now when creating things like rows or columns it always returns a layout which you have to add the items in. All py scripts were updated to follow this. * Computing the layout now goes in two passes, first estimating the required width/height of all nested layouts, and then in the second pass using the results of that to decide on the actual locations. * Enum and array buttons now follow the direction of the layout, i.e. they are vertical or horizontal depending if they are in a column or row. * Color properties now get a color picker, and only get the additional RGB sliders with Expand=True. * File/directory string properties now get a button next to them for opening the file browse, though this is not implemented yet. * Layout items can now be aligned, set align=True when creating a column, row, etc. * Buttons now get a minimum width of one icon (avoids squashing icon buttons). * Moved some more space variables into Style.
2009-05-15 11:19:59 +00:00
2012-03-30 01:51:25 +00:00
if (label_align == 'V') {
col = uiLayoutColumn(layout, 1);
if (!is_boolean)
uiItemL(col, name, ICON_NONE);
}
2012-03-30 01:51:25 +00:00
else if (label_align == 'H') {
split = uiLayoutSplit(layout, 0.5f, 0);
2012-03-30 01:51:25 +00:00
col = uiLayoutColumn(split, 0);
uiItemL(col, (is_boolean) ? "" : name, ICON_NONE);
col = uiLayoutColumn(split, 0);
}
else {
2012-03-30 01:51:25 +00:00
col = NULL;
}
/* may meed to add more cases here.
2012-04-22 11:54:53 +00:00
* don't override enum flag names */
/* name is shown above, empty name for button below */
2012-03-30 01:51:25 +00:00
name = (flag & PROP_ENUM_FLAG || is_boolean) ? NULL : "";
}
else {
2012-03-30 01:51:25 +00:00
col = layout;
name = NULL; /* no smart label alignment, show default name with button */
}
uiItemFullR(col, ptr, prop, -1, 0, 0, name, ICON_NONE);
tot++;
2009-04-27 13:44:11 +00:00
}
RNA_STRUCT_END;
return tot;
2009-04-27 13:44:11 +00:00
}
/***************************** ID Utilities *******************************/
int uiIconFromID(ID *id)
{
Object *ob;
PointerRNA ptr;
short idcode;
2012-03-30 01:51:25 +00:00
if (id == NULL)
return ICON_NONE;
2012-03-30 01:51:25 +00:00
idcode = GS(id->name);
/* exception for objects */
if (idcode == ID_OB) {
2012-03-30 01:51:25 +00:00
ob = (Object *)id;
if (ob->type == OB_EMPTY)
return ICON_EMPTY_DATA;
else
return uiIconFromID(ob->data);
}
/* otherwise get it through RNA, creating the pointer
* will set the right type, also with subclassing */
RNA_id_pointer_create(id, &ptr);
2012-03-30 01:51:25 +00:00
return (ptr.type) ? RNA_struct_ui_icon(ptr.type) : ICON_NONE;
}