3 * Copyright (C) 2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
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.
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.
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/>.
23 #include "mini-label-preview.h"
25 #include <libglabels.h>
26 #include "cairo-label-path.h"
31 /*===========================================*/
32 /* Private macros and constants. */
33 /*===========================================*/
36 #define SHADOW_OFFSET 3
39 /*===========================================*/
41 /*===========================================*/
43 struct _glMiniLabelPreviewPrivate {
45 lglTemplate *template;
50 /*===========================================*/
52 /*===========================================*/
55 /*===========================================*/
56 /* Local function prototypes */
57 /*===========================================*/
59 static void gl_mini_label_preview_finalize (GObject *object);
61 static void style_set_cb (GtkWidget *widget,
62 GtkStyle *previous_style);
64 static void redraw (glMiniLabelPreview *this);
66 static gboolean draw_cb (GtkWidget *widget,
69 static void draw_preview (glMiniLabelPreview *this,
73 /****************************************************************************/
74 /* Boilerplate Object stuff. */
75 /****************************************************************************/
76 G_DEFINE_TYPE (glMiniLabelPreview, gl_mini_label_preview, GTK_TYPE_DRAWING_AREA)
79 /*****************************************************************************/
80 /* Class Init Function. */
81 /*****************************************************************************/
83 gl_mini_label_preview_class_init (glMiniLabelPreviewClass *class)
85 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
86 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
88 gl_mini_label_preview_parent_class = g_type_class_peek_parent (class);
90 gobject_class->finalize = gl_mini_label_preview_finalize;
92 widget_class->style_set = style_set_cb;
93 widget_class->draw = draw_cb;
97 /*****************************************************************************/
98 /* Object Instance Init Function. */
99 /*****************************************************************************/
101 gl_mini_label_preview_init (glMiniLabelPreview *this)
103 gtk_widget_set_has_window (GTK_WIDGET (this), FALSE);
105 this->priv = g_new0 (glMiniLabelPreviewPrivate, 1);
109 /*****************************************************************************/
110 /* Finalize Method. */
111 /*****************************************************************************/
113 gl_mini_label_preview_finalize (GObject *object)
115 glMiniLabelPreview *this = GL_MINI_LABEL_PREVIEW (object);
117 g_return_if_fail (object != NULL);
118 g_return_if_fail (GL_IS_MINI_LABEL_PREVIEW (object));
120 lgl_template_free (this->priv->template);
123 G_OBJECT_CLASS (gl_mini_label_preview_parent_class)->finalize (object);
127 /*****************************************************************************/
128 /** New Object Generator. */
129 /*****************************************************************************/
131 gl_mini_label_preview_new (gint w,
134 glMiniLabelPreview *this;
136 this = g_object_new (GL_TYPE_MINI_LABEL_PREVIEW, NULL);
138 gtk_widget_set_size_request (GTK_WIDGET (this), w, h);
140 return GTK_WIDGET (this);
144 /*****************************************************************************/
145 /* Set label by name. */
146 /*****************************************************************************/
148 gl_mini_label_preview_set_by_name (glMiniLabelPreview *this,
150 gboolean rotate_flag)
154 lgl_template_free (this->priv->template);
155 this->priv->template = NULL;
159 this->priv->template = lgl_db_lookup_template_from_name (name);
160 this->priv->rotate_flag = rotate_flag;
167 /*--------------------------------------------------------------------------*/
168 /* Style set handler (updates colors when style/theme changes). */
169 /*--------------------------------------------------------------------------*/
171 style_set_cb (GtkWidget *widget,
172 GtkStyle *previous_style)
174 redraw (GL_MINI_LABEL_PREVIEW (widget));
178 /*****************************************************************************/
179 /* Request redraw. */
180 /*****************************************************************************/
182 redraw (glMiniLabelPreview *this)
185 GtkAllocation allocation;
187 window = gtk_widget_get_window (GTK_WIDGET (this));
191 gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
192 gdk_window_invalidate_rect (window, &allocation, FALSE);
197 /*****************************************************************************/
198 /* "Expose event" callback. */
199 /*****************************************************************************/
201 draw_cb (GtkWidget *widget,
205 draw_preview (GL_MINI_LABEL_PREVIEW (widget), cr);
211 /*****************************************************************************/
213 /*****************************************************************************/
215 draw_preview (glMiniLabelPreview *this,
218 GtkAllocation allocation;
221 guint fill_color, line_color, shadow_color;
222 const lglTemplateFrame *frame;
223 gdouble w_label, h_label;
227 gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
228 w = allocation.width;
229 h = allocation.height;
232 style = gtk_widget_get_style (GTK_WIDGET (this));
233 if ( gtk_widget_is_sensitive (GTK_WIDGET (this)) )
235 fill_color = gl_color_from_gdk_color (&style->light[GTK_STATE_NORMAL]);
236 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
237 shadow_color = gl_color_from_gdk_color (&style->dark[GTK_STATE_NORMAL]);
241 fill_color = GL_COLOR_NONE;
242 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]);
243 shadow_color = GL_COLOR_NONE;
247 if (this->priv->template == NULL)
252 frame = (lglTemplateFrame *)this->priv->template->frames->data;
254 if (this->priv->rotate_flag)
256 lgl_template_frame_get_size (frame, &h_label, &w_label);
260 lgl_template_frame_get_size (frame, &w_label, &h_label);
263 scale = MIN( (w - 2*MARGIN - 2*SHADOW_OFFSET)/w_label,
264 (h - 2*MARGIN - 2*SHADOW_OFFSET)/h_label );
267 cairo_translate (cr, w/2.0, h/2.0);
268 cairo_scale (cr, scale, scale);
269 cairo_translate (cr, -w_label/2.0, -h_label/2.0);
276 cairo_translate (cr, SHADOW_OFFSET/scale, SHADOW_OFFSET/scale);
277 gl_cairo_label_path (cr, this->priv->template, this->priv->rotate_flag, FALSE);
279 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (shadow_color));
286 gl_cairo_label_path (cr, this->priv->template, this->priv->rotate_flag, FALSE);
288 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
289 cairo_fill_preserve (cr);
291 cairo_set_line_width (cr, 1.0/scale);
292 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (line_color));
300 * Local Variables: -- emacs
302 * c-basic-offset: 8 -- emacs
303 * tab-width: 8 -- emacs
304 * indent-tabs-mode: nil -- emacs