Blender  V2.59
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 
00040 #include "BKE_context.h"
00041 
00042 
00043 #include "RNA_access.h"
00044 
00045 #include "UI_interface.h"
00046 #include "UI_resources.h"
00047 
00048 
00049 /*************************** RNA Utilities ******************************/
00050 
00051 uiBut *uiDefAutoButR(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int index, const char *name, int icon, int x1, int y1, int x2, int y2)
00052 {
00053         uiBut *but=NULL;
00054 
00055         switch(RNA_property_type(prop)) {
00056                 case PROP_BOOLEAN:
00057                 {
00058                         int arraylen= RNA_property_array_length(ptr, prop);
00059 
00060                         if(arraylen && index == -1)
00061                                 return NULL;
00062                         
00063                         if(icon && name && name[0] == '\0')
00064                                 but= uiDefIconButR_prop(block, ICONTOG, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00065                         else if(icon)
00066                                 but= uiDefIconTextButR_prop(block, ICONTOG, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00067                         else
00068                                 but= uiDefButR_prop(block, OPTION, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00069                         break;
00070                 }
00071                 case PROP_INT:
00072                 case PROP_FLOAT:
00073                 {
00074                         int arraylen= RNA_property_array_length(ptr, prop);
00075 
00076                         if(arraylen && index == -1) {
00077                                 if(ELEM(RNA_property_subtype(prop), PROP_COLOR, PROP_COLOR_GAMMA))
00078                                         but= uiDefButR_prop(block, COL, 0, name, x1, y1, x2, y2, ptr, prop, 0, 0, 0, -1, -1, NULL);
00079                         }
00080                         else if(RNA_property_subtype(prop) == PROP_PERCENTAGE || RNA_property_subtype(prop) == PROP_FACTOR)
00081                                 but= uiDefButR_prop(block, NUMSLI, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00082                         else
00083                                 but= uiDefButR_prop(block, NUM, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00084                         break;
00085                 }
00086                 case PROP_ENUM:
00087                         if(icon && name && name[0] == '\0')
00088                                 but= uiDefIconButR_prop(block, MENU, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00089                         else if(icon)
00090                                 but= uiDefIconTextButR_prop(block, MENU, 0, icon, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00091                         else
00092                                 but= uiDefButR_prop(block, MENU, 0, NULL, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00093                         break;
00094                 case PROP_STRING:
00095                         if(icon && name && name[0] == '\0')
00096                                 but= uiDefIconButR_prop(block, TEX, 0, icon, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00097                         else if(icon)
00098                                 but= uiDefIconTextButR_prop(block, TEX, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00099                         else
00100                                 but= uiDefButR_prop(block, TEX, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00101                         break;
00102                 case PROP_POINTER: {
00103                         PointerRNA pptr;
00104 
00105                         pptr= RNA_property_pointer_get(ptr, prop);
00106                         if(!pptr.type)
00107                                 pptr.type= RNA_property_pointer_type(ptr, prop);
00108                         icon= RNA_struct_ui_icon(pptr.type);
00109                         if(icon == ICON_DOT)
00110                                 icon= 0;
00111 
00112                         but= uiDefIconTextButR_prop(block, IDPOIN, 0, icon, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL);
00113                         break;
00114                 }
00115                 case PROP_COLLECTION: {
00116                         char text[256];
00117                         sprintf(text, "%d items", RNA_property_collection_length(ptr, prop));
00118                         but= uiDefBut(block, LABEL, 0, text, x1, y1, x2, y2, NULL, 0, 0, 0, 0, NULL);
00119                         uiButSetFlag(but, UI_BUT_DISABLED);
00120                         break;
00121                 }
00122                 default:
00123                         but= NULL;
00124                         break;
00125         }
00126 
00127         return but;
00128 }
00129 
00130 int uiDefAutoButsRNA(uiLayout *layout, PointerRNA *ptr, int (*check_prop)(PropertyRNA *), const char label_align)
00131 {
00132         uiLayout *split, *col;
00133         int flag;
00134         const char *name;
00135         int tot= 0;
00136 
00137         assert(ELEM3(label_align, '\0', 'H', 'V'));
00138 
00139         RNA_STRUCT_BEGIN(ptr, prop) {
00140                 flag= RNA_property_flag(prop);
00141                 if(flag & PROP_HIDDEN || (check_prop && check_prop(prop)==FALSE))
00142                         continue;
00143 
00144                 if(label_align != '\0') {
00145                         PropertyType type = RNA_property_type(prop);
00146                         int is_boolean = (type == PROP_BOOLEAN && !RNA_property_array_check(ptr, prop));
00147 
00148                         name= RNA_property_ui_name(prop);
00149 
00150                         if(label_align=='V') {
00151                                 col= uiLayoutColumn(layout, 1);
00152 
00153                                 if(!is_boolean)
00154                                         uiItemL(col, name, ICON_NONE);
00155                         }
00156                         else if(label_align=='H') {
00157                                 split = uiLayoutSplit(layout, 0.5f, 0);
00158 
00159                                 col= uiLayoutColumn(split, 0);
00160                                 uiItemL(col, (is_boolean)? "": name, ICON_NONE);
00161                                 col= uiLayoutColumn(split, 0);
00162                         }
00163                         else {
00164                                 col= NULL;
00165                         }
00166 
00167                         /* may meed to add more cases here.
00168                          * don't override enum flag names */
00169 
00170                         /* name is shown above, empty name for button below */
00171                         name= (flag & PROP_ENUM_FLAG || is_boolean)? NULL: "";
00172                 }
00173                 else {
00174                         col= layout;
00175                         name= NULL; /* no smart label alignment, show default name with button */
00176                 }
00177 
00178                 uiItemFullR(col, ptr, prop, -1, 0, 0, name, ICON_NONE);
00179                 tot++;
00180         }
00181         RNA_STRUCT_END;
00182 
00183         return tot;
00184 }
00185 
00186 /***************************** ID Utilities *******************************/
00187 
00188 int uiIconFromID(ID *id)
00189 {
00190         Object *ob;
00191         PointerRNA ptr;
00192         short idcode;
00193 
00194         if(id==NULL)
00195                 return ICON_NONE;
00196         
00197         idcode= GS(id->name);
00198 
00199         /* exception for objects */
00200         if(idcode == ID_OB) {
00201                 ob= (Object*)id;
00202 
00203                 if(ob->type == OB_EMPTY)
00204                         return ICON_EMPTY_DATA;
00205                 else
00206                         return uiIconFromID(ob->data);
00207         }
00208 
00209         /* otherwise get it through RNA, creating the pointer
00210            will set the right type, also with subclassing */
00211         RNA_id_pointer_create(id, &ptr);
00212 
00213         return (ptr.type)? RNA_struct_ui_icon(ptr.type) : ICON_NONE;
00214 }