]> git.sur5r.net Git - glabels/blob - src/font-sample.c
Imported Upstream version 3.0.0
[glabels] / src / font-sample.c
1 /*
2  *  font-sample.c
3  *  Copyright (C) 2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "font-sample.h"
24
25 #include "marshal.h"
26 #include "color.h"
27
28
29 /*===========================================*/
30 /* Private macros and constants.             */
31 /*===========================================*/
32
33 #define MARGIN 2
34
35
36 /*===========================================*/
37 /* Private types                             */
38 /*===========================================*/
39
40 struct _glFontSamplePrivate {
41
42         gchar *sample_text;
43
44         gchar *font_family;
45 };
46
47
48 /*===========================================*/
49 /* Private globals                           */
50 /*===========================================*/
51
52
53 /*===========================================*/
54 /* Local function prototypes                 */
55 /*===========================================*/
56
57 static void       gl_font_sample_finalize     (GObject        *object);
58
59 static void       style_set_cb                (GtkWidget      *widget,
60                                                GtkStyle       *previous_style);
61
62 static void       redraw                      (glFontSample   *this);
63
64 static gboolean   draw_cb                     (GtkWidget      *widget,
65                                                cairo_t        *cr);
66
67 static void       draw_sample                 (glFontSample   *this,
68                                                cairo_t        *cr);
69
70
71
72 /****************************************************************************/
73 /* Boilerplate Object stuff.                                                */
74 /****************************************************************************/
75 G_DEFINE_TYPE (glFontSample, gl_font_sample, GTK_TYPE_DRAWING_AREA)
76
77
78 /*****************************************************************************/
79 /* Class Init Function.                                                      */
80 /*****************************************************************************/
81 static void
82 gl_font_sample_class_init (glFontSampleClass *class)
83 {
84         GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
85         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
86
87         gl_font_sample_parent_class = g_type_class_peek_parent (class);
88
89         gobject_class->finalize    = gl_font_sample_finalize;
90
91         widget_class->style_set    = style_set_cb;
92         widget_class->draw         = draw_cb;
93 }
94
95
96 /*****************************************************************************/
97 /* Object Instance Init Function.                                            */
98 /*****************************************************************************/
99 static void
100 gl_font_sample_init (glFontSample *this)
101 {
102         gtk_widget_set_has_window (GTK_WIDGET (this), FALSE);
103
104         this->priv = g_new0 (glFontSamplePrivate, 1);
105 }
106
107
108 /*****************************************************************************/
109 /* Finalize Method.                                                          */
110 /*****************************************************************************/
111 static void
112 gl_font_sample_finalize (GObject *object)
113 {
114         glFontSample *this = GL_FONT_SAMPLE (object);
115
116         g_return_if_fail (object != NULL);
117         g_return_if_fail (GL_IS_FONT_SAMPLE (object));
118
119         g_free (this->priv->sample_text);
120         g_free (this->priv->font_family);
121         g_free (this->priv);
122
123         G_OBJECT_CLASS (gl_font_sample_parent_class)->finalize (object);
124 }
125
126
127 /*****************************************************************************/
128 /** New Object Generator.                                                    */
129 /*****************************************************************************/
130 GtkWidget *
131 gl_font_sample_new (gint         w,
132                     gint         h,
133                     const gchar *sample_text,
134                     const gchar *font_family)
135 {
136         glFontSample *this;
137
138         this = g_object_new (GL_TYPE_FONT_SAMPLE, NULL);
139
140         this->priv->sample_text = g_strdup (sample_text);
141         this->priv->font_family = g_strdup (font_family);
142
143         gtk_widget_set_size_request (GTK_WIDGET (this), w, h);
144
145         return GTK_WIDGET (this);
146 }
147
148
149 /*--------------------------------------------------------------------------*/
150 /* Style set handler (updates colors when style/theme changes).             */
151 /*--------------------------------------------------------------------------*/
152 static void
153 style_set_cb (GtkWidget        *widget,
154               GtkStyle         *previous_style)
155 {
156         redraw (GL_FONT_SAMPLE (widget));
157 }
158
159
160 /*****************************************************************************/
161 /* Request redraw.                                                           */
162 /*****************************************************************************/
163 static void
164 redraw (glFontSample  *this)
165 {
166         GdkWindow     *window;
167         GtkAllocation  allocation;
168
169         window = gtk_widget_get_window (GTK_WIDGET (this));
170
171         if (window)
172         {
173                 gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
174                 gdk_window_invalidate_rect (window, &allocation, FALSE);
175         }
176 }
177
178
179 /*****************************************************************************/
180 /* "Expose event" callback.                                                  */
181 /*****************************************************************************/
182 static gboolean
183 draw_cb (GtkWidget      *widget,
184          cairo_t        *cr)
185 {
186         draw_sample (GL_FONT_SAMPLE (widget), cr);
187
188         return FALSE;
189 }
190
191
192 /*****************************************************************************/
193 /* Draw sample.                                                              */
194 /*****************************************************************************/
195 static void
196 draw_sample (glFontSample *this,
197              cairo_t       *cr)
198 {
199         GtkAllocation         allocation;
200         GtkStyle             *style;
201         gdouble               w, h;
202         guint                 fill_color, line_color;
203         PangoLayout          *layout;
204         PangoFontDescription *desc;
205         PangoRectangle        ink_rect, logical_rect;
206         gdouble               layout_x, layout_y, layout_width, layout_height;
207
208
209         gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
210         w = allocation.width;
211         h = allocation.height;
212
213
214         style = gtk_widget_get_style (GTK_WIDGET (this));
215         if ( gtk_widget_is_sensitive (GTK_WIDGET (this)) )
216         {
217                 fill_color = gl_color_from_gdk_color (&style->light[GTK_STATE_NORMAL]);
218                 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
219         }
220         else
221         {
222                 fill_color = GL_COLOR_NONE;
223                 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]);
224         }
225
226
227         cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
228
229         cairo_rectangle( cr, 1, 1, w-2, h-2 );
230
231         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
232         cairo_fill_preserve( cr );
233
234         cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (line_color));
235         cairo_set_line_width (cr, 1.0);
236         cairo_stroke (cr);
237
238
239         cairo_set_antialias (cr, CAIRO_ANTIALIAS_DEFAULT);
240
241
242         layout = pango_cairo_create_layout (cr);
243
244         desc = pango_font_description_new ();
245         pango_font_description_set_family (desc, this->priv->font_family);
246         pango_font_description_set_weight (desc, PANGO_WEIGHT_NORMAL);
247         pango_font_description_set_style  (desc, PANGO_STYLE_NORMAL);
248         pango_font_description_set_size   (desc, 0.6 * (h-1) * PANGO_SCALE);
249         
250         pango_layout_set_font_description (layout, desc);
251         pango_font_description_free       (desc);
252
253         pango_layout_set_text (layout, this->priv->sample_text, -1);
254         pango_layout_set_width (layout, -1);
255         pango_layout_get_pixel_extents (layout, &ink_rect, &logical_rect);
256         layout_width  = MAX (logical_rect.width, ink_rect.width);
257         layout_height = MAX (logical_rect.height, ink_rect.height);
258
259         layout_x = (w - layout_width) / 2.0;
260         layout_y = (h - layout_height) / 2.0;
261
262         if (ink_rect.x < logical_rect.x)
263         {
264                 layout_x += logical_rect.x - ink_rect.x;
265         }
266
267         if (ink_rect.y < logical_rect.y)
268         {
269                 layout_y += logical_rect.y - ink_rect.y;
270         }
271
272         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (line_color));
273         cairo_move_to (cr, layout_x, layout_y);
274         pango_cairo_show_layout (cr, layout);
275
276         g_object_unref (layout);
277 }
278
279
280
281 /*
282  * Local Variables:       -- emacs
283  * mode: C                -- emacs
284  * c-basic-offset: 8      -- emacs
285  * tab-width: 8           -- emacs
286  * indent-tabs-mode: nil  -- emacs
287  * End:                   -- emacs
288  */