1 /* robwidget - gtk2 & GL wrapper
2  *
3  * Copyright (C) 2013 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
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.
14  *
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 #ifndef COMMON_CAIRO_H
21 #define COMMON_CAIRO_H
22 
get_font_from_gtk()23 static PangoFontDescription * get_font_from_gtk () {
24   PangoFontDescription * rv;
25   GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
26 	GtkWidget *foobar = gtk_label_new("Foobar");
27 	gtk_container_add(GTK_CONTAINER(window), foobar);
28 	gtk_widget_ensure_style(foobar);
29 
30 	PangoContext* pc = gtk_widget_get_pango_context(foobar);
31 	PangoFontDescription const * pfd = pango_context_get_font_description (pc);
32 	rv = pango_font_description_copy (pfd);
33 	gtk_widget_destroy(foobar);
34 	gtk_widget_destroy(window);
35 	assert(rv);
36 	return rv;
37 }
38 
get_color_from_gtk(GdkColor * c,int which)39 static void get_color_from_gtk (GdkColor *c, int which) {
40   GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
41 	GtkWidget *foobar = gtk_label_new("Foobar");
42 	gtk_container_add(GTK_CONTAINER(window), foobar);
43 	gtk_widget_ensure_style(foobar);
44 
45 	GtkStyle *style = gtk_widget_get_style(foobar);
46 	switch (which) {
47 		default:
48 			memcpy((void*) c, (void*) &style->fg[GTK_STATE_NORMAL], sizeof(GdkColor));
49 			break;
50 		case 1:
51 			memcpy((void*) c, (void*) &style->bg[GTK_STATE_NORMAL], sizeof(GdkColor));
52 			break;
53 		case 2:
54 			memcpy((void*) c, (void*) &style->fg[GTK_STATE_ACTIVE], sizeof(GdkColor));
55 			break;
56 	}
57 	gtk_widget_destroy(foobar);
58 	gtk_widget_destroy(window);
59 }
60 
61 static float   robtk_colorcache[3][4];
62 static bool    robtk_colorcached[3] = { false, false, false };
63 
get_color_from_theme(int which,float * col)64 static void get_color_from_theme (int which, float *col) {
65 	GdkColor color;
66 	assert(which >= 0 && which <= 2);
67 
68 	if (robtk_colorcached[which]) {
69 		memcpy(col, robtk_colorcache[which], 4 * sizeof(float));
70 	} else {
71 		get_color_from_gtk(&color, which);
72 		col[0] = color.red/65536.0;
73 		col[1] = color.green/65536.0;
74 		col[2] = color.blue/65536.0;
75 		col[3] = 1.0;
76 		memcpy(robtk_colorcache[which], col, 4 * sizeof(float));
77 		robtk_colorcached[which] = true;
78 	}
79 }
80 
get_font_from_theme()81 static PangoFontDescription * get_font_from_theme () {
82 	return get_font_from_gtk(); // TODO cache this -- but how to free on exit?
83 }
84 
85 #endif
86