]> git.sur5r.net Git - glabels/blob - src/new-label-dialog.c
Added decorations to "new label" dialog
[glabels] / src / new-label-dialog.c
1 /*
2  *  new-label-dialog.c
3  *  Copyright (C) 2006-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 "new-label-dialog.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include "marshal.h"
29 #include "builder-util.h"
30 #include "prefs.h"
31 #include "media-select.h"
32 #include "mini-label-preview.h"
33 #include "mini-preview.h"
34
35 #include "debug.h"
36
37
38 #define LABEL_PREVIEW_WIDTH  96
39 #define LABEL_PREVIEW_HEIGHT 96
40
41 #define MINI_PREVIEW_MIN_WIDTH  300
42 #define MINI_PREVIEW_MIN_HEIGHT 360
43
44
45 /*===========================================*/
46 /* Private data types                        */
47 /*===========================================*/
48
49 struct _glNewLabelDialogPrivate {
50
51         GtkBuilder *builder;
52
53         GtkWidget  *template_page_vbox;
54         GtkWidget  *combo_hbox;
55         GtkWidget  *combo;
56
57         GtkWidget  *rotate_page_vbox;
58         GtkWidget  *normal_radio;
59         GtkWidget  *rotated_radio;
60         GtkWidget  *normal_preview_hbox;
61         GtkWidget  *rotated_preview_hbox;
62         GtkWidget  *normal_preview;
63         GtkWidget  *rotated_preview;
64
65         GtkWidget  *confirm_page_vbox;
66         GtkWidget  *preview_vbox;
67         GtkWidget  *preview;
68         GtkWidget  *desc_label;
69         GtkWidget  *page_size_label;
70         GtkWidget  *label_size_label;
71         GtkWidget  *layout_label;
72         GtkWidget  *vendor_label;
73         GtkWidget  *part_label;
74         GtkWidget  *similar_label;
75 };
76
77 /* Page numbers for traversing GtkAssistant */
78 enum {
79         TEMPLATE_PAGE_NUM = 0,
80         ROTATE_PAGE_NUM,
81         CONFIRM_PAGE_NUM
82 };
83
84 enum {
85         COMPLETE,
86         LAST_SIGNAL
87 };
88
89
90 /*===========================================*/
91 /* Private globals                           */
92 /*===========================================*/
93
94 static gint signals[LAST_SIGNAL] = { 0 };
95
96
97 /*===========================================*/
98 /* Local function prototypes                 */
99 /*===========================================*/
100
101 static void       gl_new_label_dialog_finalize        (GObject           *object);
102
103 static void       cancel_cb                           (glNewLabelDialog  *this);
104 static void       apply_cb                            (glNewLabelDialog  *this);
105 static void       close_cb                            (glNewLabelDialog  *this);
106
107 static gint       forward_page_function               (gint               current_page,
108                                                        glNewLabelDialog  *this);
109
110 static void       combo_changed_cb                    (glNewLabelDialog  *this);
111 static void       rotate_toggled_cb                   (glNewLabelDialog  *this);
112
113 static gchar     *get_default_name                    (void);
114
115 static void       set_info                            (glNewLabelDialog  *this,
116                                                        const gchar       *name);
117
118
119 /*****************************************************************************/
120 /* Boilerplate object stuff.                                                 */
121 /*****************************************************************************/
122 G_DEFINE_TYPE (glNewLabelDialog, gl_new_label_dialog, GTK_TYPE_ASSISTANT);
123
124
125 /*****************************************************************************/
126 /* Class Init Function.                                                      */
127 /*****************************************************************************/
128 static void
129 gl_new_label_dialog_class_init (glNewLabelDialogClass *class)
130 {
131         GObjectClass *object_class = G_OBJECT_CLASS (class);
132
133         gl_debug (DEBUG_FILE, "");
134
135         gl_new_label_dialog_parent_class = g_type_class_peek_parent (class);
136
137         object_class->finalize = gl_new_label_dialog_finalize;
138
139         signals[COMPLETE] =
140             g_signal_new ("complete",
141                           G_OBJECT_CLASS_TYPE(object_class),
142                           G_SIGNAL_RUN_LAST,
143                           G_STRUCT_OFFSET (glNewLabelDialogClass, complete),
144                           NULL, NULL,
145                           gl_marshal_VOID__VOID,
146                           G_TYPE_NONE, 0);
147
148 }
149
150
151 /*****************************************************************************/
152 /* Object Instance Init Function.                                            */
153 /*****************************************************************************/
154 static void
155 gl_new_label_dialog_init (glNewLabelDialog *this)
156 {
157         GtkWidget    *vbox;
158         gchar        *logo_filename;
159         GdkPixbuf    *logo;
160         GtkBuilder   *builder;
161         gchar        *builder_filename;
162         static gchar *object_ids[] = { "template_page_vbox",
163                                        "rotate_page_vbox",
164                                        "confirm_page_vbox",
165                                        NULL };
166         GError       *error = NULL;
167         GtkWidget    *new_label_dialog_hbox;
168
169         gl_debug (DEBUG_FILE, "START");
170
171         g_return_if_fail (GL_IS_NEW_LABEL_DIALOG (this));
172
173         this->priv = g_new0 (glNewLabelDialogPrivate, 1);
174
175         builder = gtk_builder_new ();
176         builder_filename = g_build_filename (GLABELS_DATA_DIR, "builder", "new-label-dialog.builder", NULL);
177         gtk_builder_add_objects_from_file (builder, builder_filename, object_ids, &error);
178         g_free (builder_filename);
179         if (error) {
180                 g_critical ("%s\n\ngLabels may not be installed correctly!", error->message);
181                 g_error_free (error);
182                 return;
183         }
184
185         gl_builder_util_get_widgets (builder,
186                                      "template_page_vbox",     &this->priv->template_page_vbox,
187                                      "combo_hbox",             &this->priv->combo_hbox,
188                                      "rotate_page_vbox",       &this->priv->rotate_page_vbox,
189                                      "normal_radio",           &this->priv->normal_radio,
190                                      "rotated_radio",          &this->priv->rotated_radio,
191                                      "normal_preview_hbox",    &this->priv->normal_preview_hbox,
192                                      "rotated_preview_hbox",   &this->priv->rotated_preview_hbox,
193                                      "confirm_page_vbox",      &this->priv->confirm_page_vbox,
194                                      "preview_vbox",           &this->priv->preview_vbox,
195                                      "desc_label",             &this->priv->desc_label,
196                                      "page_size_label",        &this->priv->page_size_label,
197                                      "label_size_label",       &this->priv->label_size_label,
198                                      "layout_label",           &this->priv->layout_label,
199                                      "vendor_label",           &this->priv->vendor_label,
200                                      "part_label",             &this->priv->part_label,
201                                      "similar_label",          &this->priv->similar_label,
202                                      NULL);
203
204         this->priv->builder = builder;
205
206         gtk_assistant_append_page (GTK_ASSISTANT (this), this->priv->template_page_vbox);
207         gtk_assistant_set_page_title (GTK_ASSISTANT (this), this->priv->template_page_vbox, _("Select Product"));
208         gtk_assistant_set_page_type (GTK_ASSISTANT (this), this->priv->template_page_vbox, GTK_ASSISTANT_PAGE_INTRO);
209         logo_filename = g_build_filename (GLABELS_DATA_DIR, "pixmaps", "new-select.png", NULL);
210         logo = gdk_pixbuf_new_from_file (logo_filename, NULL);
211         gtk_assistant_set_page_header_image (GTK_ASSISTANT (this), this->priv->template_page_vbox, logo);
212         g_free (logo_filename);
213         g_object_unref (logo);
214
215         this->priv->combo = gl_media_select_new ();
216         gtk_container_add (GTK_CONTAINER (this->priv->combo_hbox), this->priv->combo);
217
218         gtk_assistant_append_page (GTK_ASSISTANT (this), this->priv->rotate_page_vbox);
219         gtk_assistant_set_page_title (GTK_ASSISTANT (this), this->priv->rotate_page_vbox, _("Choose Orientation"));
220         gtk_assistant_set_page_complete (GTK_ASSISTANT (this), this->priv->rotate_page_vbox, TRUE);
221         logo_filename = g_build_filename (GLABELS_DATA_DIR, "pixmaps", "new-rotate.png", NULL);
222         logo = gdk_pixbuf_new_from_file (logo_filename, NULL);
223         gtk_assistant_set_page_header_image (GTK_ASSISTANT (this), this->priv->rotate_page_vbox, logo);
224         g_free (logo_filename);
225         g_object_unref (logo);
226
227         this->priv->normal_preview = gl_mini_label_preview_new (LABEL_PREVIEW_WIDTH, LABEL_PREVIEW_HEIGHT);
228         gtk_container_add (GTK_CONTAINER (this->priv->normal_preview_hbox), this->priv->normal_preview);
229         this->priv->rotated_preview = gl_mini_label_preview_new (LABEL_PREVIEW_WIDTH, LABEL_PREVIEW_HEIGHT);
230         gtk_container_add (GTK_CONTAINER (this->priv->rotated_preview_hbox), this->priv->rotated_preview);
231
232         gtk_assistant_append_page (GTK_ASSISTANT (this), this->priv->confirm_page_vbox);
233         gtk_assistant_set_page_title (GTK_ASSISTANT (this), this->priv->confirm_page_vbox, _("Review"));
234         gtk_assistant_set_page_type (GTK_ASSISTANT (this), this->priv->confirm_page_vbox, GTK_ASSISTANT_PAGE_CONFIRM);
235         gtk_assistant_set_page_complete (GTK_ASSISTANT (this), this->priv->confirm_page_vbox, TRUE);
236         logo_filename = g_build_filename (GLABELS_DATA_DIR, "pixmaps", "new-confirm.png", NULL);
237         logo = gdk_pixbuf_new_from_file (logo_filename, NULL);
238         gtk_assistant_set_page_header_image (GTK_ASSISTANT (this), this->priv->confirm_page_vbox, logo);
239         g_free (logo_filename);
240         g_object_unref (logo);
241
242         this->priv->preview = gl_mini_preview_new (MINI_PREVIEW_MIN_HEIGHT, MINI_PREVIEW_MIN_WIDTH);
243         gl_mini_preview_set_draw_arrow (GL_MINI_PREVIEW (this->priv->preview), TRUE);
244         gl_mini_preview_set_rotate (GL_MINI_PREVIEW (this->priv->preview), FALSE);
245         gtk_container_add (GTK_CONTAINER (this->priv->preview_vbox), this->priv->preview);
246
247         gtk_assistant_set_forward_page_func (GTK_ASSISTANT (this), (GtkAssistantPageFunc)forward_page_function, this, NULL);
248
249         g_signal_connect_swapped (G_OBJECT(this), "cancel", G_CALLBACK(cancel_cb), this);
250         g_signal_connect_swapped (G_OBJECT(this), "apply",  G_CALLBACK(apply_cb),  this);
251         g_signal_connect_swapped (G_OBJECT(this), "close",  G_CALLBACK(close_cb),  this);
252
253         g_signal_connect_swapped (G_OBJECT (this->priv->combo),         "changed", G_CALLBACK (combo_changed_cb),  this);
254         g_signal_connect_swapped (G_OBJECT (this->priv->normal_radio),  "toggled", G_CALLBACK (rotate_toggled_cb), this);
255         g_signal_connect_swapped (G_OBJECT (this->priv->rotated_radio), "toggled", G_CALLBACK (rotate_toggled_cb), this);
256
257         combo_changed_cb (this);
258
259         gl_debug (DEBUG_FILE, "END");
260 }
261
262
263 /*****************************************************************************/
264 /* Finalize Function.                                                        */
265 /*****************************************************************************/
266 static void 
267 gl_new_label_dialog_finalize (GObject *object)
268 {
269         glNewLabelDialog* this = GL_NEW_LABEL_DIALOG (object);;
270
271         gl_debug (DEBUG_FILE, "START");
272
273         g_return_if_fail (object != NULL);
274         g_return_if_fail (GL_IS_NEW_LABEL_DIALOG (this));
275         g_return_if_fail (this->priv != NULL);
276
277         g_object_unref (G_OBJECT (this->priv->builder));
278         g_free (this->priv);
279
280         G_OBJECT_CLASS (gl_new_label_dialog_parent_class)->finalize (object);
281
282         gl_debug (DEBUG_FILE, "END");
283
284 }
285
286
287 /*****************************************************************************/
288 /* NEW object properties dialog.                                             */
289 /*****************************************************************************/
290 GtkWidget *
291 gl_new_label_dialog_new (GtkWindow    *win)
292 {
293         GtkWidget *this;
294
295         gl_debug (DEBUG_FILE, "");
296
297         this = GTK_WIDGET (g_object_new (GL_TYPE_NEW_LABEL_DIALOG, NULL));
298
299         gtk_window_set_transient_for (GTK_WINDOW (this), win);
300
301         return this;
302 }
303
304
305 /*--------------------------------------------------------------------------*/
306 /* PRIVATE.  Handle non-linear forward traversal.                           */
307 /*--------------------------------------------------------------------------*/
308 static gint
309 forward_page_function (gint              current_page,
310                        glNewLabelDialog *this)
311 {
312         gchar                     *name;
313         lglTemplate               *template;
314         const lglTemplateFrame    *frame;
315         gdouble                    w, h;
316
317         switch (current_page)
318         {
319         case TEMPLATE_PAGE_NUM:
320                 name = gl_media_select_get_name (GL_MEDIA_SELECT (this->priv->combo));
321                 if ( name != NULL )
322                 {
323                         template = lgl_db_lookup_template_from_name (name);
324                         frame    = (lglTemplateFrame *)template->frames->data;
325                         lgl_template_frame_get_size (frame, &w, &h);
326
327                         if ( w == h )
328                         {
329                                 /* Skip rotate page for square and circular labels. */
330                                 return CONFIRM_PAGE_NUM;
331                         }
332                 }
333                 return ROTATE_PAGE_NUM;
334
335         case ROTATE_PAGE_NUM:
336                 return CONFIRM_PAGE_NUM;
337
338         case CONFIRM_PAGE_NUM:
339         default:
340                 return -1;
341         }
342
343         return -1;
344 }
345
346
347 /*--------------------------------------------------------------------------*/
348 /* PRIVATE.  cancel callback.                                               */
349 /*--------------------------------------------------------------------------*/
350 static void
351 cancel_cb (glNewLabelDialog *this)
352 {
353                                                                                
354         gtk_widget_destroy (GTK_WIDGET(this));
355
356 }
357
358
359 /*--------------------------------------------------------------------------*/
360 /* PRIVATE.  apply callback                                                 */
361 /*--------------------------------------------------------------------------*/
362 static void
363 apply_cb (glNewLabelDialog *this)
364 {
365
366         g_signal_emit (G_OBJECT (this), signals[COMPLETE], 0);
367
368 }
369
370                          
371 /*--------------------------------------------------------------------------*/
372 /* PRIVATE.  close callback                                                 */
373 /*--------------------------------------------------------------------------*/
374 static void
375 close_cb (glNewLabelDialog *this)
376 {
377                                                                                
378         gtk_widget_destroy (GTK_WIDGET(this));
379
380 }
381
382                          
383 /*---------------------------------------------------------------------------*/
384 /* PRIVATE.  Template changed callback.                                      */
385 /*---------------------------------------------------------------------------*/
386 static void
387 combo_changed_cb (glNewLabelDialog  *this)
388 {
389         gchar             *name;
390
391         gl_debug (DEBUG_FILE, "START");
392
393         name = gl_media_select_get_name (GL_MEDIA_SELECT (this->priv->combo));
394
395         gl_mini_label_preview_set_by_name (GL_MINI_LABEL_PREVIEW (this->priv->normal_preview),  name, FALSE);
396         gl_mini_label_preview_set_by_name (GL_MINI_LABEL_PREVIEW (this->priv->rotated_preview), name, TRUE);
397         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this->priv->normal_radio), TRUE);
398
399         gl_mini_preview_set_by_name (GL_MINI_PREVIEW (this->priv->preview), name);
400         set_info (this, name);
401
402         gtk_assistant_set_page_complete (GTK_ASSISTANT (this), this->priv->template_page_vbox, (name != NULL));
403
404         g_free (name);
405
406         gl_debug (DEBUG_FILE, "END");
407 }
408
409
410 /*---------------------------------------------------------------------------*/
411 /* PRIVATE.  Rotate toggled callback.                                        */
412 /*---------------------------------------------------------------------------*/
413 static void
414 rotate_toggled_cb (glNewLabelDialog  *this)
415 {
416         gboolean state;
417
418         gl_debug (DEBUG_FILE, "START");
419
420         state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (this->priv->rotated_radio));
421         gl_mini_preview_set_rotate (GL_MINI_PREVIEW (this->priv->preview), state);
422
423         gl_debug (DEBUG_FILE, "END");
424 }
425
426
427 /*---------------------------------------------------------------------------*/
428 /* PRIVATE.  Set information labels.                                         */
429 /*---------------------------------------------------------------------------*/
430 static void
431 set_info (glNewLabelDialog  *this,
432           const gchar       *name)
433 {
434         lglTemplate          *template;
435         lglTemplateFrame     *frame;
436         lglVendor            *vendor;
437         lglUnits              units;
438         gchar                *page_size_string;
439         gchar                *label_size_string;
440         gchar                *layout_string;
441         GList                *list, *p;
442         GString              *list_string;
443
444         template = lgl_db_lookup_template_from_name (name);
445         frame    = template->frames->data;
446         vendor   = lgl_db_lookup_vendor_from_name (template->brand);
447
448         units    = gl_prefs_model_get_units (gl_prefs);
449
450         page_size_string  = lgl_db_lookup_paper_name_from_id (template->paper_id);
451         label_size_string = lgl_template_frame_get_size_description (frame, units);
452         layout_string     = lgl_template_frame_get_layout_description (frame);
453
454         gtk_label_set_text (GTK_LABEL (this->priv->desc_label),       template->description);
455         gtk_label_set_text (GTK_LABEL (this->priv->page_size_label),  page_size_string);
456         gtk_label_set_text (GTK_LABEL (this->priv->label_size_label), label_size_string);
457         gtk_label_set_text (GTK_LABEL (this->priv->layout_label),     layout_string);
458
459         if ( vendor && vendor->url )
460         {
461                 gchar *escaped_url;
462                 gchar *markup;
463
464                 escaped_url = g_markup_escape_text (vendor->url, -1);
465                 markup = g_strdup_printf ("<a href='%s'>%s</a>", escaped_url, vendor->name);
466                 gtk_label_set_markup (GTK_LABEL (this->priv->vendor_label), markup);
467                 g_free (escaped_url);
468                 g_free (markup);
469         }
470         else
471         {
472                 /* FIXME: Using set_markup instead of set_text to clear out previous link. */
473                 gtk_label_set_markup (GTK_LABEL (this->priv->vendor_label), template->brand);
474         }
475
476         if ( template->product_url )
477         {
478                 gchar *escaped_url;
479                 gchar *markup;
480
481                 escaped_url = g_markup_escape_text (template->product_url, -1);
482                 markup = g_strdup_printf ("<a href='%s'>%s</a>", escaped_url, template->part);
483                 gtk_label_set_markup (GTK_LABEL (this->priv->part_label), markup);
484                 g_free (escaped_url);
485                 g_free (markup);
486         }
487         else
488         {
489                 /* FIXME: Using set_markup instead of set_text to clear out previous link. */
490                 gtk_label_set_markup (GTK_LABEL (this->priv->part_label), template->part);
491         }
492
493         list = lgl_db_get_similar_template_name_list (name);
494         list_string = g_string_new ("");
495         for ( p = list; p; p = p->next )
496         {
497                 g_string_append (list_string, (char *)p->data);
498                 if ( p->next )
499                 {
500                         g_string_append (list_string, "\n");
501                 }
502         }
503         gtk_label_set_text (GTK_LABEL (this->priv->similar_label), list_string->str);
504
505         lgl_db_free_template_name_list (list);
506         g_string_free (list_string, TRUE);
507
508         g_free (page_size_string);
509         g_free (label_size_string);
510         g_free (layout_string);
511 }
512
513
514 /*****************************************************************************/
515 /* Get template name.                                                        */
516 /*****************************************************************************/
517 gchar *
518 gl_new_label_dialog_get_template_name (glNewLabelDialog *this)
519 {
520         gchar *name;
521
522         name = gl_media_select_get_name (GL_MEDIA_SELECT (this->priv->combo));
523
524         return name;
525 }
526
527
528 /*****************************************************************************/
529 /* Set template name.                                                        */
530 /*****************************************************************************/
531 void
532 gl_new_label_dialog_set_template_name (glNewLabelDialog *this,
533                                        gchar            *name)
534 {
535         gl_mini_preview_set_by_name (GL_MINI_PREVIEW (this->priv->preview), name);
536         gl_media_select_set_name (GL_MEDIA_SELECT (this->priv->combo), name);
537         set_info (this, name);
538 }
539
540
541 /*****************************************************************************/
542 /* Get current filter parameters.                                            */
543 /*****************************************************************************/
544 void
545 gl_new_label_dialog_get_filter_parameters (glNewLabelDialog  *this,
546                                            gchar            **page_size_id,
547                                            gchar            **category_id)
548 {
549 }
550
551
552 /*****************************************************************************/
553 /* Set current filter parameters.                                            */
554 /*****************************************************************************/
555 void
556 gl_new_label_dialog_set_filter_parameters (glNewLabelDialog *this,
557                                            const gchar      *page_size_id,
558                                            const gchar      *category_id)
559 {
560 }
561
562
563 /*****************************************************************************/
564 /* Get rotate state.                                                         */
565 /*****************************************************************************/
566 gboolean
567 gl_new_label_dialog_get_rotate_state (glNewLabelDialog *this)
568 {
569         return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (this->priv->rotated_radio));
570 }
571
572
573 /*****************************************************************************/
574 /* Set rotate state.                                                         */
575 /*****************************************************************************/
576 void
577 gl_new_label_dialog_set_rotate_state (glNewLabelDialog *this,
578                                       gboolean          state)
579 {
580         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this->priv->rotated_radio), state);
581         gl_mini_preview_set_rotate (GL_MINI_PREVIEW (this->priv->preview), state);
582 }
583
584
585
586 /*
587  * Local Variables:       -- emacs
588  * mode: C                -- emacs
589  * c-basic-offset: 8      -- emacs
590  * tab-width: 8           -- emacs
591  * indent-tabs-mode: nil  -- emacs
592  * End:                   -- emacs
593  */