]> git.sur5r.net Git - glabels/blob - src/label-properties-dialog.c
Imported Upstream version 3.4.0
[glabels] / src / label-properties-dialog.c
1 /*
2  *  label-properties-dialog.c
3  *  Copyright (C) 2001-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 "label-properties-dialog.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include "builder-util.h"
29 #include "prefs.h"
30 #include "mini-preview.h"
31 #include "window.h"
32 #include "debug.h"
33
34
35 #define MINI_PREVIEW_MIN_WIDTH  270
36 #define MINI_PREVIEW_MIN_HEIGHT 360
37
38
39 /*========================================================*/
40 /* Private types.                                         */
41 /*========================================================*/
42
43 struct _glLabelPropertiesDialogPrivate
44 {
45         glLabel        *label;
46         GtkBox         *box;
47         GtkWidget      *preview;
48         GtkLabel       *vendor;
49         GtkLabel       *partnum;
50         GtkLabel       *description;
51         GtkLabel       *dimensions;
52         GtkLabel       *medium;
53         GtkLabel       *layout;
54         GtkLabel       *margins;
55         GtkLabel       *similar_label;
56         GtkLabel       *similar;
57         GtkLabel       *orientation_label;
58         GtkBox         *orientation_box;
59         GtkRadioButton *normal_orientation;
60         GtkRadioButton *rotated_orientation;
61 };
62
63
64
65 /*========================================================*/
66 /* Private function prototypes                            */
67 /*========================================================*/
68
69 static void gl_label_properties_dialog_finalize    (GObject *object);
70
71 static void gl_label_properties_dialog_rotation_cb (glLabelPropertiesDialog *dialog);
72
73 static void gl_label_properties_dialog_response_cb (glLabelPropertiesDialog *dialog,
74                                                     gint                     response);
75
76 static void gl_label_properties_dialog_construct   (glLabelPropertiesDialog *dialog,
77                                                     glLabel                 *label);
78
79
80 /*========================================================*/
81 /* Private globals.                                       */
82 /*========================================================*/
83
84
85 /*****************************************************************************/
86 /* Boilerplate object stuff.                                                 */
87 /*****************************************************************************/
88 G_DEFINE_TYPE (glLabelPropertiesDialog, gl_label_properties_dialog, GTK_TYPE_DIALOG)
89
90
91
92 static void
93 gl_label_properties_dialog_class_init (glLabelPropertiesDialogClass *class)
94 {
95         GObjectClass *object_class = G_OBJECT_CLASS (class);
96
97         gl_debug (DEBUG_UI, "");
98
99         object_class->finalize = gl_label_properties_dialog_finalize;
100 }
101
102
103 static void
104 gl_label_properties_dialog_rotation_cb (glLabelPropertiesDialog *dialog)
105 {
106         gboolean  rotate_flag;
107         glWindow *window;
108         glView   *view;
109
110         gl_debug (DEBUG_UI, "START");
111
112         rotate_flag = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->rotated_orientation));
113         gl_mini_preview_set_rotate (GL_MINI_PREVIEW (dialog->priv->preview), rotate_flag);
114         gl_label_set_rotate_flag (dialog->priv->label, rotate_flag, TRUE);
115
116         window = GL_WINDOW (gtk_window_get_transient_for (GTK_WINDOW (dialog)));
117         view = GL_VIEW (window->view);
118         if (view->zoom_to_fit_flag)
119                 gl_view_zoom_to_fit (view);
120
121         gl_debug (DEBUG_UI, "END");
122 }
123
124
125 static void
126 gl_label_properties_dialog_response_cb (glLabelPropertiesDialog *dialog,
127                                         gint                     response)
128 {
129         gl_debug (DEBUG_UI, "START");
130
131         switch (response)
132         {
133         case GL_RESPONSE_SELECT_OTHER:
134         case GTK_RESPONSE_CLOSE:
135         case GTK_RESPONSE_DELETE_EVENT:
136                 gtk_widget_hide (GTK_WIDGET (dialog));
137                 break;
138         default:
139                 g_print ("response = %d", response);
140                 g_assert_not_reached ();
141         }
142
143         gl_debug (DEBUG_UI, "END");
144 }
145
146
147 static void
148 gl_label_properties_dialog_init (glLabelPropertiesDialog *dialog)
149 {
150         gchar             *builder_filename;
151         GtkBuilder        *builder;
152         static gchar      *object_ids[] = {"label_properties_box", NULL};
153         GError            *error = NULL;
154         GtkWidget         *vbox;
155
156         gl_debug (DEBUG_UI, "START");
157
158         dialog->priv = g_new0 (glLabelPropertiesDialogPrivate, 1);
159
160         vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
161
162         /*Translators: Button label to choose different template*/
163         gtk_dialog_add_button (GTK_DIALOG (dialog), _("Choose _other…"), GL_RESPONSE_SELECT_OTHER);
164         gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Close"), GTK_RESPONSE_CLOSE);
165         gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
166
167         builder = gtk_builder_new ();
168         builder_filename = g_build_filename (GLABELS_DATA_DIR, "ui", "label-properties-dialog.ui", NULL);
169         gtk_builder_add_objects_from_file (builder, builder_filename, object_ids, &error);
170         g_free (builder_filename);
171         if (error) {
172                 g_critical ("%s\n\ngLabels may not be installed correctly!", error->message);
173                 g_error_free (error);
174                 return;
175         }
176
177         gl_builder_util_get_widgets (builder,
178                                      "label_properties_box", &dialog->priv->box,
179                                      "vendor_value", &dialog->priv->vendor,
180                                      "partnum_value", &dialog->priv->partnum,
181                                      "description_value", &dialog->priv->description,
182                                      "dimensions_value", &dialog->priv->dimensions,
183                                      "medium_value", &dialog->priv->medium,
184                                      "layout_value", &dialog->priv->layout,
185                                      "margins_value", &dialog->priv->margins,
186                                      "similar_label", &dialog->priv->similar_label,
187                                      "similar_value", &dialog->priv->similar,
188                                      "orientation_label", &dialog->priv->orientation_label,
189                                      "orientation_box", &dialog->priv->orientation_box,
190                                      "orientation_normal_radio", &dialog->priv->normal_orientation,
191                                      "orientation_rotated_radio", &dialog->priv->rotated_orientation,
192                                      NULL);
193
194         gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (dialog->priv->box));
195
196         dialog->priv->preview = gl_mini_preview_new (MINI_PREVIEW_MIN_HEIGHT, MINI_PREVIEW_MIN_WIDTH);
197         gtk_box_pack_start (dialog->priv->box, GTK_WIDGET (dialog->priv->preview), FALSE, FALSE, 0);
198         gtk_widget_show_all (dialog->priv->preview);
199
200         g_object_unref (G_OBJECT (builder));
201
202         gl_debug (DEBUG_UI, "END");
203 }
204
205
206 static void
207 gl_label_properties_dialog_finalize (GObject *object)
208 {
209         glLabelPropertiesDialog* dialog = GL_LABEL_PROPERTIES_DIALOG (object);
210
211         gl_debug (DEBUG_UI, "START");
212
213         g_return_if_fail (object != NULL);
214         g_return_if_fail (GL_IS_LABEL_PROPERTIES_DIALOG (dialog));
215         g_return_if_fail (dialog->priv != NULL);
216
217
218         G_OBJECT_CLASS (gl_label_properties_dialog_parent_class)->finalize (object);
219
220         gl_debug (DEBUG_UI, "END");
221 }
222
223
224 /* FIXME: Move this function to library */
225 static void
226 lgl_template_get_margins (const lglTemplate *template,
227                           gdouble           *hmargin,
228                           gdouble           *vmargin)
229 {
230         GList                   *p, *q;
231         const lglTemplateFrame  *frame;
232         const lglTemplateLayout *layout;
233
234         /* Set margins to maximum */
235         *hmargin = template->page_width;
236         *vmargin = template->page_height;
237
238         /* and search lowest margin in layouts */
239         for (p = template->frames; p; p = p->next)
240         {
241                 frame = (lglTemplateFrame *)p->data;
242                 for (q = frame->all.layouts; q; q = q->next)
243                 {
244                         layout = (lglTemplateLayout *)q->data;
245                         if (layout->x0 < *hmargin)
246                                 *hmargin = layout->x0;
247                         if (layout->y0 < *vmargin)
248                                 *vmargin = layout->y0;
249                 }
250         }
251 }
252
253 /* FIXME: Move this function to library */
254 gchar *
255 lgl_template_get_margins_string (const lglTemplate *template)
256 {
257         lglUnits  units;
258         gdouble   units_per_point;
259         gdouble   hmargin, vmargin;
260         gchar    *hmargin_str, *vmargin_str;
261         gchar    *margins_str;
262
263         units = gl_prefs_model_get_units (gl_prefs);
264         units_per_point = lgl_units_get_units_per_point (units);
265
266         lgl_template_get_margins (template, &hmargin, &vmargin);
267
268         if (units == LGL_UNITS_INCH)
269         {
270                 hmargin_str = lgl_str_format_fraction (hmargin*units_per_point);
271                 vmargin_str = lgl_str_format_fraction (vmargin*units_per_point);
272         }
273         else
274         {
275                 hmargin_str = g_strdup_printf ("%.5g", hmargin*units_per_point);
276                 vmargin_str = g_strdup_printf ("%.5g", vmargin*units_per_point);
277         }
278
279         /*Translators: first param is numeric value of horizontal margine, second*/
280         /*is numeric value of vertical margine and third is unit. Example:       */
281         /*"10 / 12.5 mm (horizontal / vertical)"*/
282         margins_str = g_strdup_printf (_("%s / %s %s (horizontal / vertical)"),
283                                        hmargin_str, vmargin_str, lgl_units_get_name (units));
284
285         g_free (hmargin_str);
286         g_free (vmargin_str);
287
288         return margins_str;
289 }
290
291 /*--------------------------------------------------------------------------*/
292 /* PRIVATE.  Create properties widgets.                                          */
293 /*--------------------------------------------------------------------------*/
294 static void
295 gl_label_properties_dialog_construct (glLabelPropertiesDialog *dialog,
296                                       glLabel                 *label)
297 {
298         const lglTemplate *template;
299         gchar             *template_name;
300         lglUnits           units;
301         gdouble            units_per_point;
302         lglVendor         *vendor;
303         gchar             *text;
304         gchar             *link;
305         gchar             *escaped_url;
306         GList             *list, *p;
307         GString           *list_text;
308         gboolean           rotate_flag;
309         gdouble            w, h;
310
311         gl_debug (DEBUG_UI, "START");
312
313         dialog->priv->label = label;
314
315         template = gl_label_get_template (label);
316         template_name = lgl_template_get_name (template);
317         units = gl_prefs_model_get_units (gl_prefs);
318         units_per_point = lgl_units_get_units_per_point (units);
319
320         /* Vendor */
321         vendor = lgl_db_lookup_vendor_from_name (template->brand);
322         if (vendor && vendor->url)
323         {
324                 escaped_url = g_markup_escape_text (vendor->url, -1);
325                 link = g_strconcat ("<a href=\"", escaped_url , "\">", template->brand, "</a>", NULL);
326                 gtk_label_set_markup (dialog->priv->vendor, link);
327                 g_free (escaped_url);
328                 g_free (link);
329         }
330         else
331         {
332                 gtk_label_set_label (dialog->priv->vendor, template->brand);
333         }
334
335         /* Part number */
336         if (template->product_url)
337         {
338                 escaped_url = g_markup_escape_text (template->product_url, -1);
339                 link = g_strconcat ("<a href=\"", escaped_url, "\">", template->part, "</a>", NULL);
340                 gtk_label_set_markup (dialog->priv->partnum, link);
341                 g_free (escaped_url);
342                 g_free (link);
343         }
344         else
345         {
346                 gtk_label_set_label (dialog->priv->partnum, template->part);
347         }
348
349         /* Description */
350         gtk_label_set_label (dialog->priv->description, template->description);
351
352         /* Dimensions */
353         text = gl_label_get_dimensions_string (label);
354         gtk_label_set_label (dialog->priv->dimensions, text);
355         g_free (text);
356
357         /* Medium */
358         text = lgl_db_lookup_paper_name_from_id (template->paper_id);
359         gtk_label_set_label (dialog->priv->medium, text);
360         g_free (text);
361
362         /* Layout */
363         text = lgl_template_frame_get_layout_description (template->frames->data);
364         gtk_label_set_label (dialog->priv->layout, text);
365         g_free (text);
366
367         /* Margins */
368         text = lgl_template_get_margins_string (template);
369         gtk_label_set_label (dialog->priv->margins, text);
370         g_free (text);
371
372         /* Similar products */
373         list = lgl_db_get_similar_template_name_list (template_name);
374         if (list)
375         {
376                 list_text = g_string_new ("");
377                 for (p = list; p; p = p->next)
378                 {
379                         g_string_append (list_text, (char *)p->data);
380                         if (p->next)
381                                 g_string_append_c (list_text, '\n');
382                 }
383                 gtk_label_set_text (dialog->priv->similar, list_text->str);
384                 g_string_free (list_text, TRUE);
385         }
386         else
387         {
388                 gtk_widget_hide (GTK_WIDGET (dialog->priv->similar_label));
389                 gtk_widget_hide (GTK_WIDGET (dialog->priv->similar));
390         }
391
392         /* Orientation */
393         gl_label_get_size (label, &w, &h);
394         if (w == h)
395         {
396                 gtk_widget_hide (GTK_WIDGET (dialog->priv->orientation_label));
397                 gtk_widget_hide (GTK_WIDGET (dialog->priv->orientation_box));
398                 gl_mini_preview_set_draw_arrow (GL_MINI_PREVIEW (dialog->priv->preview), FALSE);
399         }
400         else
401         {
402                 rotate_flag = gl_label_get_rotate_flag (label);
403                 if (rotate_flag)
404                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->rotated_orientation), TRUE);
405                 else
406                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->normal_orientation), TRUE);
407                 gl_mini_preview_set_draw_arrow (GL_MINI_PREVIEW (dialog->priv->preview), TRUE);
408                 gl_mini_preview_set_rotate (GL_MINI_PREVIEW (dialog->priv->preview), rotate_flag);
409         }
410
411         /* Preview */
412         gl_mini_preview_set_template (GL_MINI_PREVIEW (dialog->priv->preview), template);
413
414         g_free (template_name);
415         g_free (vendor);
416
417         g_signal_connect_swapped (G_OBJECT (dialog->priv->normal_orientation),
418                                   "toggled",
419                                   G_CALLBACK (gl_label_properties_dialog_rotation_cb),
420                                   dialog);
421         g_signal_connect_swapped (G_OBJECT (dialog->priv->rotated_orientation),
422                                   "toggled",
423                                   G_CALLBACK (gl_label_properties_dialog_rotation_cb),
424                                   dialog);
425         g_signal_connect (G_OBJECT (dialog),
426                           "response",
427                           G_CALLBACK (gl_label_properties_dialog_response_cb),
428                           NULL);
429
430         gl_debug (DEBUG_UI, "END");
431 }
432
433
434 /*****************************************************************************/
435 /* NEW document properties dialog.                                                   */
436 /*****************************************************************************/
437 GtkWidget*
438 gl_label_properties_dialog_new (glLabel *label,
439                                 GtkWindow *parent)
440 {
441         glLabelPropertiesDialog *dialog;
442
443         g_assert (GL_IS_LABEL (label));
444
445         gl_debug (DEBUG_UI, "START");
446
447         dialog = GL_LABEL_PROPERTIES_DIALOG (g_object_new (GL_TYPE_LABEL_PROPERTIES_DIALOG, NULL));
448
449         if (parent)
450                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
451
452         gl_label_properties_dialog_construct (GL_LABEL_PROPERTIES_DIALOG (dialog),
453                                               label);
454
455         gl_debug (DEBUG_UI, "END");
456
457         return GTK_WIDGET (dialog);
458 }
459
460
461
462
463 /*
464  * Local Variables:       -- emacs
465  * mode: C                -- emacs
466  * c-basic-offset: 8      -- emacs
467  * tab-width: 8           -- emacs
468  * indent-tabs-mode: nil  -- emacs
469  * End:                   -- emacs
470  */