3 * Copyright (C) 2001-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-preview.h"
27 #include "libglabels/db.h"
28 #include "cairo-label-path.h"
35 /*===========================================*/
36 /* Private macros and constants. */
37 /*===========================================*/
40 #define SHADOW_OFFSET 3
43 /*===========================================*/
45 /*===========================================*/
58 struct _glMiniPreviewPrivate {
62 lglTemplate *template;
63 gint labels_per_sheet;
76 /*===========================================*/
78 /*===========================================*/
80 static gint mini_preview_signals[LAST_SIGNAL] = { 0 };
83 /*===========================================*/
84 /* Local function prototypes */
85 /*===========================================*/
87 static void gl_mini_preview_finalize (GObject *object);
89 static void gl_mini_preview_construct (glMiniPreview *this,
93 static gboolean button_press_event_cb (GtkWidget *widget,
94 GdkEventButton *event);
95 static gboolean motion_notify_event_cb (GtkWidget *widget,
96 GdkEventMotion *event);
97 static gboolean button_release_event_cb (GtkWidget *widget,
98 GdkEventButton *event);
101 static gboolean expose_event_cb (GtkWidget *widget,
102 GdkEventExpose *event,
103 glMiniPreview *this);
104 static void style_set_cb (GtkWidget *widget,
105 GtkStyle *previous_style,
106 glMiniPreview *this);
108 static void redraw (glMiniPreview *this);
109 static void draw (glMiniPreview *this,
112 static void draw_shadow (glMiniPreview *this,
118 static void draw_paper (glMiniPreview *this,
123 static void draw_labels (glMiniPreview *this,
125 lglTemplate *template,
128 static gint find_closest_label (glMiniPreview *this,
132 static gdouble set_transform_and_get_scale (glMiniPreview *this,
136 /****************************************************************************/
137 /* Object infrastructure. */
138 /****************************************************************************/
139 G_DEFINE_TYPE (glMiniPreview, gl_mini_preview, GTK_TYPE_EVENT_BOX);
142 /*****************************************************************************/
143 /* Class Init Function. */
144 /*****************************************************************************/
146 gl_mini_preview_class_init (glMiniPreviewClass *class)
148 GObjectClass *object_class = G_OBJECT_CLASS (class);
149 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
151 gl_debug (DEBUG_MINI_PREVIEW, "START");
153 gl_mini_preview_parent_class = g_type_class_peek_parent (class);
155 object_class->finalize = gl_mini_preview_finalize;
157 widget_class->button_press_event = button_press_event_cb;
158 widget_class->motion_notify_event = motion_notify_event_cb;
159 widget_class->button_release_event = button_release_event_cb;
161 mini_preview_signals[CLICKED] =
162 g_signal_new ("clicked",
163 G_OBJECT_CLASS_TYPE(object_class),
165 G_STRUCT_OFFSET (glMiniPreviewClass, clicked),
167 gl_marshal_VOID__INT,
168 G_TYPE_NONE, 1, G_TYPE_INT);
170 mini_preview_signals[PRESSED] =
171 g_signal_new ("pressed",
172 G_OBJECT_CLASS_TYPE(object_class),
174 G_STRUCT_OFFSET (glMiniPreviewClass, pressed),
176 gl_marshal_VOID__INT_INT,
177 G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
179 gl_debug (DEBUG_MINI_PREVIEW, "END");
183 /*****************************************************************************/
184 /* Object Instance Init Function. */
185 /*****************************************************************************/
187 gl_mini_preview_init (glMiniPreview *this)
189 gl_debug (DEBUG_MINI_PREVIEW, "START");
191 this->priv = g_new0 (glMiniPreviewPrivate, 1);
193 gtk_widget_add_events (GTK_WIDGET (this),
194 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
195 GDK_POINTER_MOTION_MASK);
197 gtk_event_box_set_visible_window (GTK_EVENT_BOX (this), FALSE);
199 this->priv->canvas = gtk_drawing_area_new ();
200 GTK_WIDGET_SET_FLAGS (this->priv->canvas, GTK_NO_WINDOW);
201 gtk_container_add (GTK_CONTAINER (this), this->priv->canvas);
203 g_signal_connect (G_OBJECT (this->priv->canvas), "expose-event",
204 G_CALLBACK (expose_event_cb), this);
205 g_signal_connect (G_OBJECT (this->priv->canvas), "style-set",
206 G_CALLBACK (style_set_cb), this);
208 gl_debug (DEBUG_MINI_PREVIEW, "END");
212 /*****************************************************************************/
213 /* Finalize Method. */
214 /*****************************************************************************/
216 gl_mini_preview_finalize (GObject *object)
218 glMiniPreview *this = GL_MINI_PREVIEW (object);
220 gl_debug (DEBUG_MINI_PREVIEW, "START");
222 g_return_if_fail (object != NULL);
223 g_return_if_fail (GL_IS_MINI_PREVIEW (object));
225 lgl_template_free (this->priv->template);
226 g_free (this->priv->centers);
229 G_OBJECT_CLASS (gl_mini_preview_parent_class)->finalize (object);
231 gl_debug (DEBUG_MINI_PREVIEW, "END");
235 /*****************************************************************************/
236 /** New Object Generator. */
237 /*****************************************************************************/
239 gl_mini_preview_new (gint height,
244 gl_debug (DEBUG_MINI_PREVIEW, "START");
246 this = g_object_new (gl_mini_preview_get_type (), NULL);
248 gl_mini_preview_construct (this, height, width);
250 gl_debug (DEBUG_MINI_PREVIEW, "END");
252 return GTK_WIDGET (this);
256 /*--------------------------------------------------------------------------*/
257 /* Construct composite widget. */
258 /*--------------------------------------------------------------------------*/
260 gl_mini_preview_construct (glMiniPreview *this,
264 gl_debug (DEBUG_MINI_PREVIEW, "START");
266 gtk_widget_set_size_request (GTK_WIDGET (this->priv->canvas), width, height);
268 gl_debug (DEBUG_MINI_PREVIEW, "END");
272 /****************************************************************************/
273 /* Set label for mini-preview to determine geometry. */
274 /****************************************************************************/
276 gl_mini_preview_set_label_by_name (glMiniPreview *this,
279 lglTemplate *template;
281 gl_debug (DEBUG_MINI_PREVIEW, "START");
284 template = lgl_db_lookup_template_from_name (name);
286 gl_mini_preview_set_template (this, template);
288 lgl_template_free (template);
290 gl_debug (DEBUG_MINI_PREVIEW, "END");
294 /****************************************************************************/
295 /* Set label for mini-preview to determine geometry. */
296 /****************************************************************************/
298 gl_mini_preview_set_template (glMiniPreview *this,
299 const lglTemplate *template)
301 const lglTemplateFrame *frame;
302 lglTemplateOrigin *origins;
306 gl_debug (DEBUG_MINI_PREVIEW, "START");
308 frame = (lglTemplateFrame *)template->frames->data;
313 lgl_template_free (this->priv->template);
314 this->priv->template = lgl_template_dup (template);
317 * Set labels per sheet
319 this->priv->labels_per_sheet = lgl_template_frame_get_n_labels (frame);
324 g_free (this->priv->centers);
325 this->priv->centers = g_new0 (LabelCenter, this->priv->labels_per_sheet);
326 origins = lgl_template_frame_get_origins (frame);
327 lgl_template_frame_get_size (frame, &w, &h);
328 for ( i=0; i<this->priv->labels_per_sheet; i++ )
330 this->priv->centers[i].x = origins[i].x + w/2.0;
331 this->priv->centers[i].y = origins[i].y + h/2.0;
336 * Redraw modified preview
340 gl_debug (DEBUG_MINI_PREVIEW, "END");
344 /****************************************************************************/
345 /* Highlight given label outlines. */
346 /****************************************************************************/
348 gl_mini_preview_highlight_range (glMiniPreview *this,
352 gl_debug (DEBUG_MINI_PREVIEW, "START");
354 this->priv->highlight_first = first_label;
355 this->priv->highlight_last = last_label;
359 gl_debug (DEBUG_MINI_PREVIEW, "END");
363 /*--------------------------------------------------------------------------*/
364 /* Set transformation and return scale. */
365 /*--------------------------------------------------------------------------*/
367 set_transform_and_get_scale (glMiniPreview *this,
370 lglTemplate *template = this->priv->template;
373 gdouble offset_x, offset_y;
375 /* Establish scale and origin. */
376 w = GTK_WIDGET (this)->allocation.width;
377 h = GTK_WIDGET (this)->allocation.height;
379 /* establish scale. */
380 scale = MIN( (w - 2*MARGIN - 2*SHADOW_OFFSET)/template->page_width,
381 (h - 2*MARGIN - 2*SHADOW_OFFSET)/template->page_height );
383 /* Find offset to center preview. */
384 offset_x = (w/scale - template->page_width) / 2.0;
385 offset_y = (h/scale - template->page_height) / 2.0;
387 /* Set transformation. */
388 cairo_scale (cr, scale, scale);
389 cairo_translate (cr, offset_x, offset_y);
395 /*--------------------------------------------------------------------------*/
396 /* Button press event handler */
397 /*--------------------------------------------------------------------------*/
399 button_press_event_cb (GtkWidget *widget,
400 GdkEventButton *event)
402 glMiniPreview *this = GL_MINI_PREVIEW (widget);
408 gl_debug (DEBUG_MINI_PREVIEW, "START");
410 if ( event->button == 1 )
412 cr = gdk_cairo_create (GTK_WIDGET (this->priv->canvas)->window);
414 scale = set_transform_and_get_scale (this, cr);
418 cairo_device_to_user (cr, &x, &y);
420 i = find_closest_label (this, x, y);
422 g_signal_emit (G_OBJECT(this),
423 mini_preview_signals[CLICKED],
426 this->priv->first_i = i;
427 this->priv->last_i = i;
428 g_signal_emit (G_OBJECT(this),
429 mini_preview_signals[PRESSED],
430 0, this->priv->first_i, this->priv->last_i);
432 this->priv->dragging = TRUE;
433 this->priv->prev_i = i;
438 gl_debug (DEBUG_MINI_PREVIEW, "END");
443 /*--------------------------------------------------------------------------*/
444 /* Motion notify event handler */
445 /*--------------------------------------------------------------------------*/
447 motion_notify_event_cb (GtkWidget *widget,
448 GdkEventMotion *event)
450 glMiniPreview *this = GL_MINI_PREVIEW (widget);
456 gl_debug (DEBUG_MINI_PREVIEW, "START");
458 if (this->priv->dragging)
460 cr = gdk_cairo_create (GTK_WIDGET (this->priv->canvas)->window);
462 scale = set_transform_and_get_scale (this, cr);
466 cairo_device_to_user (cr, &x, &y);
468 i = find_closest_label (this, x, y);
470 if ( i != this->priv->prev_i )
472 this->priv->last_i = i;
474 g_signal_emit (G_OBJECT(this),
475 mini_preview_signals[PRESSED],
477 MIN (this->priv->first_i, this->priv->last_i),
478 MAX (this->priv->first_i, this->priv->last_i));
480 this->priv->prev_i = i;
485 gl_debug (DEBUG_MINI_PREVIEW, "END");
490 /*--------------------------------------------------------------------------*/
491 /* Button release event handler */
492 /*--------------------------------------------------------------------------*/
494 button_release_event_cb (GtkWidget *widget,
495 GdkEventButton *event)
497 glMiniPreview *this = GL_MINI_PREVIEW (widget);
499 gl_debug (DEBUG_MINI_PREVIEW, "START");
501 if ( event->button == 1 )
503 this->priv->dragging = FALSE;
507 gl_debug (DEBUG_MINI_PREVIEW, "END");
512 /*--------------------------------------------------------------------------*/
513 /* Find index+1 of label closest to given coordinates. */
514 /*--------------------------------------------------------------------------*/
516 find_closest_label (glMiniPreview *this,
522 gdouble dx, dy, d2, min_d2;
524 dx = x - this->priv->centers[0].x;
525 dy = y - this->priv->centers[0].y;
526 min_d2 = dx*dx + dy*dy;
529 for ( i=1; i<this->priv->labels_per_sheet; i++ )
531 dx = x - this->priv->centers[i].x;
532 dy = y - this->priv->centers[i].y;
546 /*--------------------------------------------------------------------------*/
547 /* Expose event handler. */
548 /*--------------------------------------------------------------------------*/
550 expose_event_cb (GtkWidget *widget,
551 GdkEventExpose *event,
556 gl_debug (DEBUG_MINI_PREVIEW, "START");
558 cr = gdk_cairo_create (widget->window);
561 event->area.x, event->area.y,
562 event->area.width, event->area.height);
564 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
570 gl_debug (DEBUG_MINI_PREVIEW, "END");
575 /*--------------------------------------------------------------------------*/
576 /* Style set handler (updates colors when style/theme changes). */
577 /*--------------------------------------------------------------------------*/
579 style_set_cb (GtkWidget *widget,
580 GtkStyle *previous_style,
583 gl_debug (DEBUG_MINI_PREVIEW, "START");
587 gl_debug (DEBUG_MINI_PREVIEW, "END");
591 /*--------------------------------------------------------------------------*/
593 /*--------------------------------------------------------------------------*/
595 redraw (glMiniPreview *this)
599 gl_debug (DEBUG_MINI_PREVIEW, "START");
601 if (GTK_WIDGET (this->priv->canvas)->window)
604 region = gdk_drawable_get_clip_region (GTK_WIDGET (this->priv->canvas)->window);
606 gdk_window_invalidate_region (GTK_WIDGET (this->priv->canvas)->window, region, TRUE);
607 gdk_window_process_updates (GTK_WIDGET (this->priv->canvas)->window, TRUE);
609 gdk_region_destroy (region);
612 gl_debug (DEBUG_MINI_PREVIEW, "END");
616 /*--------------------------------------------------------------------------*/
617 /* Draw mini preview. */
618 /*--------------------------------------------------------------------------*/
620 draw (glMiniPreview *this,
623 lglTemplate *template = this->priv->template;
625 gdouble shadow_x, shadow_y;
628 gl_debug (DEBUG_MINI_PREVIEW, "START");
633 scale = set_transform_and_get_scale (this, cr);
636 shadow_x = SHADOW_OFFSET/scale;
637 shadow_y = SHADOW_OFFSET/scale;
639 draw_shadow (this, cr,
641 template->page_width, template->page_height);
643 draw_paper (this, cr,
644 template->page_width, template->page_height,
647 draw_labels (this, cr, template, 1.0/scale);
651 gl_debug (DEBUG_MINI_PREVIEW, "END");
656 /*--------------------------------------------------------------------------*/
657 /* Draw page shadow */
658 /*--------------------------------------------------------------------------*/
660 draw_shadow (glMiniPreview *this,
670 gl_debug (DEBUG_MINI_PREVIEW, "START");
674 cairo_rectangle (cr, x, y, width, height);
676 style = gtk_widget_get_style (GTK_WIDGET(this));
677 shadow_color = gl_color_from_gdk_color (&style->dark[GTK_STATE_NORMAL]);
678 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (shadow_color));
684 gl_debug (DEBUG_MINI_PREVIEW, "END");
688 /*--------------------------------------------------------------------------*/
690 /*--------------------------------------------------------------------------*/
692 draw_paper (glMiniPreview *this,
699 guint paper_color, outline_color;
701 gl_debug (DEBUG_MINI_PREVIEW, "START");
705 style = gtk_widget_get_style (GTK_WIDGET(this));
706 paper_color = gl_color_from_gdk_color (&style->light[GTK_STATE_NORMAL]);
707 outline_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
709 cairo_rectangle (cr, 0.0, 0.0, width, height);
711 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (paper_color));
712 cairo_fill_preserve (cr);
714 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (outline_color));
715 cairo_set_line_width (cr, line_width);
720 gl_debug (DEBUG_MINI_PREVIEW, "END");
724 /*--------------------------------------------------------------------------*/
726 /*--------------------------------------------------------------------------*/
728 draw_labels (glMiniPreview *this,
730 lglTemplate *template,
733 const lglTemplateFrame *frame;
735 lglTemplateOrigin *origins;
737 guint highlight_color, paper_color, outline_color;
739 gl_debug (DEBUG_MINI_PREVIEW, "START");
741 frame = (lglTemplateFrame *)template->frames->data;
743 n_labels = lgl_template_frame_get_n_labels (frame);
744 origins = lgl_template_frame_get_origins (frame);
746 style = gtk_widget_get_style (GTK_WIDGET(this));
747 highlight_color = gl_color_from_gdk_color (&style->base[GTK_STATE_SELECTED]);
748 paper_color = gl_color_from_gdk_color (&style->light[GTK_STATE_NORMAL]);
749 outline_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
751 for ( i=0; i < n_labels; i++ ) {
755 cairo_translate (cr, origins[i].x, origins[i].y);
756 gl_cairo_label_path (cr, template, FALSE, FALSE);
758 if ( ((i+1) >= this->priv->highlight_first) &&
759 ((i+1) <= this->priv->highlight_last) )
761 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (highlight_color));
765 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (paper_color));
767 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
768 cairo_fill_preserve (cr);
770 cairo_set_line_width (cr, line_width);
771 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (outline_color));
780 gl_debug (DEBUG_MINI_PREVIEW, "END");
786 * Local Variables: -- emacs
788 * c-basic-offset: 8 -- emacs
789 * tab-width: 8 -- emacs
790 * indent-tabs-mode: nil -- emacs