2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation
23 * ***** END GPL LICENSE BLOCK *****
31 #include "MEM_guardedalloc.h"
34 #include "DNA_screen_types.h"
35 #include "DNA_userdef_types.h"
36 #include "DNA_windowmanager_types.h"
38 #include "BLI_arithb.h"
39 #include "BLI_listbase.h"
41 #include "BLI_string.h"
43 #include "BKE_context.h"
44 #include "BKE_global.h"
45 #include "BKE_utildefines.h"
48 #include "BIF_glutil.h"
52 #include "UI_interface.h"
53 #include "UI_interface_icons.h"
54 #include "UI_resources.h"
55 #include "UI_view2d.h"
57 #include "ED_datafiles.h"
61 #include "interface_intern.h"
64 /* style + theme + layout-engine = UI */
67 This is a complete set of layout rules, the 'state' of the Layout
68 Engine. Multiple styles are possible, defined via C or Python. Styles
69 get a name, and will typically get activated per region type, like
70 "Header", or "Listview" or "Toolbar". Properties of Style definitions
73 - default collumn properties, internal spacing, aligning, min/max width
74 - button alignment rules (for groups)
75 - label placement rules
76 - internal labeling or external labeling default
77 - default minimum widths for buttons/labels (in amount of characters)
78 - font types, styles and relative sizes for Panel titles, labels, etc.
83 /* ********************************************** */
85 static uiStyle *ui_style_new(ListBase *styles, const char *name)
87 uiStyle *style= MEM_callocN(sizeof(uiStyle), "new style");
89 BLI_addtail(styles, style);
90 BLI_strncpy(style->name, name, MAX_STYLE_NAME);
92 style->paneltitle.uifont_id= UIFONT_DEFAULT;
93 style->paneltitle.points= 13;
94 style->paneltitle.shadow= 5;
95 style->paneltitle.shadx= 2;
96 style->paneltitle.shady= -2;
97 style->paneltitle.shadowalpha= 0.25f;
98 style->paneltitle.shadowcolor= 0.0f;
100 style->grouplabel.uifont_id= UIFONT_DEFAULT;
101 style->grouplabel.points= 12;
102 style->grouplabel.shadow= 3;
103 style->grouplabel.shadx= 1;
104 style->grouplabel.shady= -1;
105 style->grouplabel.shadowalpha= 0.25f;
107 style->widgetlabel.uifont_id= UIFONT_DEFAULT;
108 style->widgetlabel.points= 11;
109 style->widgetlabel.shadow= 3;
110 style->widgetlabel.shadx= 1;
111 style->widgetlabel.shady= -1;
112 style->widgetlabel.shadowalpha= 0.3f;
113 style->widgetlabel.shadowcolor= 1.0f;
115 style->widget.uifont_id= UIFONT_DEFAULT;
116 style->widget.points= 11;
117 style->widget.shadowalpha= 0.25f;
119 style->columnspace= 5;
120 style->templatespace= 5;
122 style->buttonspacex= 5;
123 style->buttonspacey= 2;
124 style->panelspace= 8;
125 style->panelouter= 4;
130 static uiFont *uifont_to_blfont(int id)
132 uiFont *font= U.uifonts.first;
134 for(; font; font= font->next) {
135 if(font->uifont_id==id) {
139 return U.uifonts.first;
142 /* *************** draw ************************ */
144 static void ui_font_shadow_draw(uiFontStyle *fs, int x, int y, char *str)
148 glGetFloatv(GL_CURRENT_COLOR, color);
150 glColor4f(fs->shadowcolor, fs->shadowcolor, fs->shadowcolor, fs->shadowalpha);
152 BLF_blur(fs->shadow);
153 BLF_position(x+fs->shadx, y+fs->shady, 0.0f);
160 void uiStyleFontDraw(uiFontStyle *fs, rcti *rect, char *str)
167 height= BLF_height("2"); /* correct offset is on baseline, the j is below that */
168 yofs= floor( 0.5f*(rect->ymax - rect->ymin - height));
170 if(fs->align==UI_STYLE_TEXT_CENTER)
171 xofs= floor( 0.5f*(rect->xmax - rect->xmin - BLF_width(str)));
172 else if(fs->align==UI_STYLE_TEXT_RIGHT)
173 xofs= rect->xmax - rect->xmin - BLF_width(str) - 1;
175 /* clip is very strict, so we give it some space */
176 BLF_clipping(rect->xmin-1, rect->ymin-4, rect->xmax+1, rect->ymax+4);
177 BLF_enable(BLF_CLIPPING);
180 ui_font_shadow_draw(fs, rect->xmin+xofs, rect->ymin+yofs, str);
182 BLF_position(rect->xmin+xofs, rect->ymin+yofs, 0.0f);
185 BLF_disable(BLF_CLIPPING);
188 /* ************** helpers ************************ */
190 /* temporarily, does widget font */
191 int UI_GetStringWidth(char *str)
193 uiStyle *style= U.uistyles.first;
195 uiStyleFontSet(&style->widget);
196 return BLF_width(str);
199 /* temporarily, does widget font */
200 void UI_DrawString(float x, float y, char *str)
202 uiStyle *style= U.uistyles.first;
204 uiStyleFontSet(&style->widget);
205 BLF_position(x, y, 0.0f);
209 /* ************** init exit ************************ */
211 /* called on each .B.blend read */
212 /* reading without uifont will create one */
213 void uiStyleInit(void)
215 uiFont *font= U.uifonts.first;
216 uiStyle *style= U.uistyles.first;
218 /* recover from uninitialized dpi */
219 CLAMP(U.dpi, 72, 240);
221 /* default builtin */
223 font= MEM_callocN(sizeof(uiFont), "ui font");
224 BLI_addtail(&U.uifonts, font);
226 strcpy(font->filename, "default");
227 font->uifont_id= UIFONT_DEFAULT;
230 for(font= U.uifonts.first; font; font= font->next) {
232 if(font->uifont_id==UIFONT_DEFAULT) {
233 font->blf_id= BLF_load_mem("default", (unsigned char*)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
236 font->blf_id= BLF_load(font->filename);
237 if(font->blf_id == -1)
238 font->blf_id= BLF_load_mem("default", (unsigned char*)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
241 if (font->blf_id == -1)
242 printf("uiStyleInit error, no fonts available\n");
244 BLF_set(font->blf_id);
245 /* ? just for speed to initialize?
246 * Yes, this build the glyph cache and create
256 ui_style_new(&U.uistyles, "Default Style");
260 void uiStyleFontSet(uiFontStyle *fs)
262 uiFont *font= uifont_to_blfont(fs->uifont_id);
264 BLF_set(font->blf_id);
265 BLF_size(fs->points, U.dpi);