Blender V2.61 - r43446

interface_utils.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2009 Blender Foundation.
00019  * All rights reserved.
00020  * 
00021  * Contributor(s): Blender Foundation
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <assert.h>
00035 
00036 #include "DNA_object_types.h"
00037 
00038 #include "BLI_utildefines.h"
00039 #include "BLI_string.h"
00040 
00041 #include "BLF_translation.h"
00042 
00043 #include "BKE_context.h"
00044 
00045 
00046 #include "RNA_access.h"
00047 
00048 #include "UI_interface.h"
00049 #include "UI_resources.h"
00050 
00051 
00052 /*************************** RNA Utilities ******************************/
00053 
00054 uiBut *uiDefAutoButR(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int index, const char *name, int icon, int x1, int y1, int x2, int y2)
00055 {
00056     uiBut *but=NULL;
00057 
00058     switch(RNA_property_type(prop)) {
00059         case PROP_BOOLEAN:
00060         {
00061             int arraylen= RNA_property_array_length(ptr, prop);
00062 
00063             if(arraylen && index == -1)
00064                 return NULL;
00065             
00066             if(icon && name && name[0] == '\0')
00067                 but= uiDefIconButR_prop(block, ICONTOG, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00068             else if(icon)
00069                 but= uiDefIconTextButR_prop(block, ICONTOG, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00070             else
00071                 but= uiDefButR_prop(block, OPTION, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00072             break;
00073         }
00074         case PROP_INT:
00075         case PROP_FLOAT:
00076         {
00077             int arraylen= RNA_property_array_length(ptr, prop);
00078 
00079             if(arraylen && index == -1) {
00080                 if(ELEM(RNA_property_subtype(prop), PROP_COLOR, PROP_COLOR_GAMMA))
00081                     but= uiDefButR_prop(block, COL, 0, name, x1, y1, x2, y2, ptr, prop, 0, 0, 0, -1, -1, NULL);
00082             }
00083             else if(RNA_property_subtype(prop) == PROP_PERCENTAGE || RNA_property_subtype(prop) == PROP_FACTOR)
00084                 but= uiDefButR_prop(block, NUMSLI, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00085             else
00086                 but= uiDefButR_prop(block, NUM, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00087             break;
00088         }
00089         case PROP_ENUM:
00090             if(icon && name && name[0] == '\0')
00091                 but= uiDefIconButR_prop(block, MENU, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00092             else if(icon)
00093                 but= uiDefIconTextButR_prop(block, MENU, 0, icon, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00094             else
00095                 but= uiDefButR_prop(block, MENU, 0, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00096             break;
00097         case PROP_STRING:
00098             if(icon && name && name[0] == '\0')
00099                 but= uiDefIconButR_prop(block, TEX, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00100             else if(icon)
00101                 but= uiDefIconTextButR_prop(block, TEX, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00102             else
00103                 but= uiDefButR_prop(block, TEX, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00104             break;
00105         case PROP_POINTER: {
00106             PointerRNA pptr;
00107 
00108             pptr= RNA_property_pointer_get(ptr, prop);
00109             if(!pptr.type)
00110                 pptr.type= RNA_property_pointer_type(ptr, prop);
00111             icon= RNA_struct_ui_icon(pptr.type);
00112             if(icon == ICON_DOT)
00113                 icon= 0;
00114 
00115             but= uiDefIconTextButR_prop(block, IDPOIN, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00116             break;
00117         }
00118         case PROP_COLLECTION: {
00119             char text[256];
00120             BLI_snprintf(text, sizeof(text), IFACE_("%d items"), RNA_property_collection_length(ptr, prop));
00121             but= uiDefBut(block, LABEL, 0, text, x1, y1, x2, y2, NULL, 0, 0, 0, 0, NULL);
00122             uiButSetFlag(but, UI_BUT_DISABLED);
00123             break;
00124         }
00125         default:
00126             but= NULL;
00127             break;
00128     }
00129 
00130     return but;
00131 }
00132 
00133 int uiDefAutoButsRNA(uiLayout *layout, PointerRNA *ptr, int (*check_prop)(PointerRNA *, PropertyRNA *), const char label_align)
00134 {
00135     uiLayout *split, *col;
00136     int flag;
00137     const char *name;
00138     int tot= 0;
00139 
00140     assert(ELEM3(label_align, '\0', 'H', 'V'));
00141 
00142     RNA_STRUCT_BEGIN(ptr, prop) {
00143         flag= RNA_property_flag(prop);
00144         if(flag & PROP_HIDDEN || (check_prop && check_prop(ptr, prop)==FALSE))
00145             continue;
00146 
00147         if(label_align != '\0') {
00148             PropertyType type = RNA_property_type(prop);
00149             int is_boolean = (type == PROP_BOOLEAN && !RNA_property_array_check(prop));
00150 
00151             name= RNA_property_ui_name(prop);
00152 
00153             if(label_align=='V') {
00154                 col= uiLayoutColumn(layout, 1);
00155 
00156                 if(!is_boolean)
00157                     uiItemL(col, name, ICON_NONE);
00158             }
00159             else if(label_align=='H') {
00160                 split = uiLayoutSplit(layout, 0.5f, 0);
00161 
00162                 col= uiLayoutColumn(split, 0);
00163                 uiItemL(col, (is_boolean)? "": name, ICON_NONE);
00164                 col= uiLayoutColumn(split, 0);
00165             }
00166             else {
00167                 col= NULL;
00168             }
00169 
00170             /* may meed to add more cases here.
00171              * don't override enum flag names */
00172 
00173             /* name is shown above, empty name for button below */
00174             name= (flag & PROP_ENUM_FLAG || is_boolean)? NULL: "";
00175         }
00176         else {
00177             col= layout;
00178             name= NULL; /* no smart label alignment, show default name with button */
00179         }
00180 
00181         uiItemFullR(col, ptr, prop, -1, 0, 0, name, ICON_NONE);
00182         tot++;
00183     }
00184     RNA_STRUCT_END;
00185 
00186     return tot;
00187 }
00188 
00189 /***************************** ID Utilities *******************************/
00190 
00191 int uiIconFromID(ID *id)
00192 {
00193     Object *ob;
00194     PointerRNA ptr;
00195     short idcode;
00196 
00197     if(id==NULL)
00198         return ICON_NONE;
00199     
00200     idcode= GS(id->name);
00201 
00202     /* exception for objects */
00203     if(idcode == ID_OB) {
00204         ob= (Object*)id;
00205 
00206         if(ob->type == OB_EMPTY)
00207             return ICON_EMPTY_DATA;
00208         else
00209             return uiIconFromID(ob->data);
00210     }
00211 
00212     /* otherwise get it through RNA, creating the pointer
00213        will set the right type, also with subclassing */
00214     RNA_id_pointer_create(id, &ptr);
00215 
00216     return (ptr.type)? RNA_struct_ui_icon(ptr.type) : ICON_NONE;
00217 }