2 * label-properties-dialog.c
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 "label-properties-dialog.h"
25 #include <glib/gi18n.h>
28 #include "builder-util.h"
30 #include "mini-preview.h"
35 #define MINI_PREVIEW_MIN_WIDTH 270
36 #define MINI_PREVIEW_MIN_HEIGHT 360
39 /*========================================================*/
41 /*========================================================*/
50 GtkLabel *description;
55 GtkLabel *similar_label;
57 GtkLabel *orientation_label;
58 GtkBox *orientation_box;
59 GtkRadioButton *normal_orientation;
60 GtkRadioButton *rotated_orientation;
62 } glLabelPropertiesDialogPrivate;
65 struct _glLabelPropertiesDialog
69 glLabelPropertiesDialogPrivate *priv;
74 /*========================================================*/
75 /* Private function prototypes */
76 /*========================================================*/
78 static void gl_label_properties_dialog_finalize (GObject *object);
80 static void gl_label_properties_dialog_rotation_cb (glLabelPropertiesDialog *dialog);
82 static void gl_label_properties_dialog_response_cb (glLabelPropertiesDialog *dialog,
85 static void gl_label_properties_dialog_construct (glLabelPropertiesDialog *dialog,
89 /*========================================================*/
90 /* Private globals. */
91 /*========================================================*/
94 /*****************************************************************************/
95 /* Boilerplate object stuff. */
96 /*****************************************************************************/
98 G_DEFINE_TYPE_WITH_PRIVATE (glLabelPropertiesDialog, gl_label_properties_dialog, GTK_TYPE_DIALOG)
103 gl_label_properties_dialog_class_init (glLabelPropertiesDialogClass *class)
105 GObjectClass *object_class = G_OBJECT_CLASS (class);
107 gl_debug (DEBUG_UI, "");
109 object_class->finalize = gl_label_properties_dialog_finalize;
114 gl_label_properties_dialog_rotation_cb (glLabelPropertiesDialog *dialog)
116 gboolean rotate_flag;
120 gl_debug (DEBUG_UI, "START");
122 rotate_flag = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->rotated_orientation));
123 gl_mini_preview_set_rotate (GL_MINI_PREVIEW (dialog->priv->preview), rotate_flag);
124 gl_label_set_rotate_flag (dialog->priv->label, rotate_flag, TRUE);
126 window = GL_WINDOW (gtk_window_get_transient_for (GTK_WINDOW (dialog)));
127 view = GL_VIEW (window->view);
128 if (view->zoom_to_fit_flag)
129 gl_view_zoom_to_fit (view);
131 gl_debug (DEBUG_UI, "END");
136 gl_label_properties_dialog_response_cb (glLabelPropertiesDialog *dialog,
139 gl_debug (DEBUG_UI, "START");
143 case GL_RESPONSE_SELECT_OTHER:
144 case GTK_RESPONSE_CLOSE:
145 case GTK_RESPONSE_DELETE_EVENT:
146 gtk_widget_hide (GTK_WIDGET (dialog));
149 g_print ("response = %d", response);
150 g_assert_not_reached ();
153 gl_debug (DEBUG_UI, "END");
158 gl_label_properties_dialog_init (glLabelPropertiesDialog *dialog)
160 gchar *builder_filename;
162 static gchar *object_ids[] = {"label_properties_box", NULL};
163 GError *error = NULL;
166 gl_debug (DEBUG_UI, "START");
168 dialog->priv = gl_label_properties_dialog_get_instance_private (dialog);
170 vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
172 /*Translators: Button label to choose different template*/
173 gtk_dialog_add_button (GTK_DIALOG (dialog), _("Choose _other…"), GL_RESPONSE_SELECT_OTHER);
174 gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Close"), GTK_RESPONSE_CLOSE);
175 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
177 builder = gtk_builder_new ();
178 builder_filename = g_build_filename (GLABELS_DATA_DIR, "ui", "label-properties-dialog.ui", NULL);
179 gtk_builder_add_objects_from_file (builder, builder_filename, object_ids, &error);
180 g_free (builder_filename);
182 g_critical ("%s\n\ngLabels may not be installed correctly!", error->message);
183 g_error_free (error);
187 gl_builder_util_get_widgets (builder,
188 "label_properties_box", &dialog->priv->box,
189 "vendor_value", &dialog->priv->vendor,
190 "partnum_value", &dialog->priv->partnum,
191 "description_value", &dialog->priv->description,
192 "dimensions_value", &dialog->priv->dimensions,
193 "medium_value", &dialog->priv->medium,
194 "layout_value", &dialog->priv->layout,
195 "margins_value", &dialog->priv->margins,
196 "similar_label", &dialog->priv->similar_label,
197 "similar_value", &dialog->priv->similar,
198 "orientation_label", &dialog->priv->orientation_label,
199 "orientation_box", &dialog->priv->orientation_box,
200 "orientation_normal_radio", &dialog->priv->normal_orientation,
201 "orientation_rotated_radio", &dialog->priv->rotated_orientation,
204 gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (dialog->priv->box));
206 dialog->priv->preview = gl_mini_preview_new (MINI_PREVIEW_MIN_HEIGHT, MINI_PREVIEW_MIN_WIDTH);
207 gtk_box_pack_start (dialog->priv->box, GTK_WIDGET (dialog->priv->preview), FALSE, FALSE, 0);
208 gtk_widget_show_all (dialog->priv->preview);
210 g_object_unref (G_OBJECT (builder));
212 gl_debug (DEBUG_UI, "END");
217 gl_label_properties_dialog_finalize (GObject *object)
219 glLabelPropertiesDialog* dialog = GL_LABEL_PROPERTIES_DIALOG (object);
221 gl_debug (DEBUG_UI, "START");
223 g_return_if_fail (object != NULL);
224 g_return_if_fail (GL_IS_LABEL_PROPERTIES_DIALOG (dialog));
225 g_return_if_fail (dialog->priv != NULL);
228 G_OBJECT_CLASS (gl_label_properties_dialog_parent_class)->finalize (object);
230 gl_debug (DEBUG_UI, "END");
234 /* FIXME: Move this function to library */
236 lgl_template_get_margins (const lglTemplate *template,
241 const lglTemplateFrame *frame;
242 const lglTemplateLayout *layout;
244 /* Set margins to maximum */
245 *hmargin = template->page_width;
246 *vmargin = template->page_height;
248 /* and search lowest margin in layouts */
249 for (p = template->frames; p; p = p->next)
251 frame = (lglTemplateFrame *)p->data;
252 for (q = frame->all.layouts; q; q = q->next)
254 layout = (lglTemplateLayout *)q->data;
255 if (layout->x0 < *hmargin)
256 *hmargin = layout->x0;
257 if (layout->y0 < *vmargin)
258 *vmargin = layout->y0;
263 /* FIXME: Move this function to library */
265 lgl_template_get_margins_string (const lglTemplate *template)
268 gdouble units_per_point;
269 gdouble hmargin, vmargin;
270 gchar *hmargin_str, *vmargin_str;
273 units = gl_prefs_model_get_units (gl_prefs);
274 units_per_point = lgl_units_get_units_per_point (units);
276 lgl_template_get_margins (template, &hmargin, &vmargin);
278 if (units == LGL_UNITS_INCH)
280 hmargin_str = lgl_str_format_fraction (hmargin*units_per_point);
281 vmargin_str = lgl_str_format_fraction (vmargin*units_per_point);
285 hmargin_str = g_strdup_printf ("%.5g", hmargin*units_per_point);
286 vmargin_str = g_strdup_printf ("%.5g", vmargin*units_per_point);
289 /*Translators: first param is numeric value of horizontal margine, second*/
290 /*is numeric value of vertical margine and third is unit. Example: */
291 /*"10 / 12.5 mm (horizontal / vertical)"*/
292 margins_str = g_strdup_printf (_("%s / %s %s (horizontal / vertical)"),
293 hmargin_str, vmargin_str, lgl_units_get_name (units));
295 g_free (hmargin_str);
296 g_free (vmargin_str);
301 /*--------------------------------------------------------------------------*/
302 /* PRIVATE. Create properties widgets. */
303 /*--------------------------------------------------------------------------*/
305 gl_label_properties_dialog_construct (glLabelPropertiesDialog *dialog,
308 const lglTemplate *template;
309 gchar *template_name;
311 gdouble units_per_point;
318 gboolean rotate_flag;
321 gl_debug (DEBUG_UI, "START");
323 dialog->priv->label = label;
325 template = gl_label_get_template (label);
326 template_name = lgl_template_get_name (template);
327 units = gl_prefs_model_get_units (gl_prefs);
328 units_per_point = lgl_units_get_units_per_point (units);
331 vendor = lgl_db_lookup_vendor_from_name (template->brand);
332 if (vendor && vendor->url)
334 escaped_url = g_markup_escape_text (vendor->url, -1);
335 link = g_strconcat ("<a href=\"", escaped_url , "\">", template->brand, "</a>", NULL);
336 gtk_label_set_markup (dialog->priv->vendor, link);
337 g_free (escaped_url);
342 gtk_label_set_label (dialog->priv->vendor, template->brand);
346 if (template->product_url)
348 escaped_url = g_markup_escape_text (template->product_url, -1);
349 link = g_strconcat ("<a href=\"", escaped_url, "\">", template->part, "</a>", NULL);
350 gtk_label_set_markup (dialog->priv->partnum, link);
351 g_free (escaped_url);
356 gtk_label_set_label (dialog->priv->partnum, template->part);
360 gtk_label_set_label (dialog->priv->description, template->description);
363 text = gl_label_get_dimensions_string (label);
364 gtk_label_set_label (dialog->priv->dimensions, text);
368 text = lgl_db_lookup_paper_name_from_id (template->paper_id);
369 gtk_label_set_label (dialog->priv->medium, text);
373 text = lgl_template_frame_get_layout_description (template->frames->data);
374 gtk_label_set_label (dialog->priv->layout, text);
378 text = lgl_template_get_margins_string (template);
379 gtk_label_set_label (dialog->priv->margins, text);
382 /* Similar products */
383 list = lgl_db_get_similar_template_name_list (template_name);
386 list_text = g_string_new ("");
387 for (p = list; p; p = p->next)
389 g_string_append (list_text, (char *)p->data);
391 g_string_append_c (list_text, '\n');
393 gtk_label_set_text (dialog->priv->similar, list_text->str);
394 g_string_free (list_text, TRUE);
398 gtk_widget_hide (GTK_WIDGET (dialog->priv->similar_label));
399 gtk_widget_hide (GTK_WIDGET (dialog->priv->similar));
403 gl_label_get_size (label, &w, &h);
406 gtk_widget_hide (GTK_WIDGET (dialog->priv->orientation_label));
407 gtk_widget_hide (GTK_WIDGET (dialog->priv->orientation_box));
408 gl_mini_preview_set_draw_arrow (GL_MINI_PREVIEW (dialog->priv->preview), FALSE);
412 rotate_flag = gl_label_get_rotate_flag (label);
414 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->rotated_orientation), TRUE);
416 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->normal_orientation), TRUE);
417 gl_mini_preview_set_draw_arrow (GL_MINI_PREVIEW (dialog->priv->preview), TRUE);
418 gl_mini_preview_set_rotate (GL_MINI_PREVIEW (dialog->priv->preview), rotate_flag);
422 gl_mini_preview_set_template (GL_MINI_PREVIEW (dialog->priv->preview), template);
424 g_free (template_name);
427 g_signal_connect_swapped (G_OBJECT (dialog->priv->normal_orientation),
429 G_CALLBACK (gl_label_properties_dialog_rotation_cb),
431 g_signal_connect_swapped (G_OBJECT (dialog->priv->rotated_orientation),
433 G_CALLBACK (gl_label_properties_dialog_rotation_cb),
435 g_signal_connect (G_OBJECT (dialog),
437 G_CALLBACK (gl_label_properties_dialog_response_cb),
440 gl_debug (DEBUG_UI, "END");
444 /*****************************************************************************/
445 /* NEW document properties dialog. */
446 /*****************************************************************************/
448 gl_label_properties_dialog_new (glLabel *label,
451 glLabelPropertiesDialog *dialog;
453 g_assert (GL_IS_LABEL (label));
455 gl_debug (DEBUG_UI, "START");
457 dialog = GL_LABEL_PROPERTIES_DIALOG (g_object_new (GL_TYPE_LABEL_PROPERTIES_DIALOG, NULL));
460 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
462 gl_label_properties_dialog_construct (GL_LABEL_PROPERTIES_DIALOG (dialog),
465 gl_debug (DEBUG_UI, "END");
467 return GTK_WIDGET (dialog);
474 * Local Variables: -- emacs
476 * c-basic-offset: 8 -- emacs
477 * tab-width: 8 -- emacs
478 * indent-tabs-mode: nil -- emacs