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 /*========================================================*/
43 struct _glLabelPropertiesDialogPrivate
50 GtkLabel *description;
55 GtkLabel *similar_label;
57 GtkLabel *orientation_label;
58 GtkBox *orientation_box;
59 GtkRadioButton *normal_orientation;
60 GtkRadioButton *rotated_orientation;
65 /*========================================================*/
66 /* Private function prototypes */
67 /*========================================================*/
69 static void gl_label_properties_dialog_finalize (GObject *object);
71 static void gl_label_properties_dialog_rotation_cb (glLabelPropertiesDialog *dialog);
73 static void gl_label_properties_dialog_response_cb (glLabelPropertiesDialog *dialog,
76 static void gl_label_properties_dialog_construct (glLabelPropertiesDialog *dialog,
80 /*========================================================*/
81 /* Private globals. */
82 /*========================================================*/
85 /*****************************************************************************/
86 /* Boilerplate object stuff. */
87 /*****************************************************************************/
88 G_DEFINE_TYPE (glLabelPropertiesDialog, gl_label_properties_dialog, GTK_TYPE_DIALOG)
93 gl_label_properties_dialog_class_init (glLabelPropertiesDialogClass *class)
95 GObjectClass *object_class = G_OBJECT_CLASS (class);
97 gl_debug (DEBUG_UI, "");
99 object_class->finalize = gl_label_properties_dialog_finalize;
104 gl_label_properties_dialog_rotation_cb (glLabelPropertiesDialog *dialog)
106 gboolean rotate_flag;
110 gl_debug (DEBUG_UI, "START");
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);
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);
121 gl_debug (DEBUG_UI, "END");
126 gl_label_properties_dialog_response_cb (glLabelPropertiesDialog *dialog,
129 gl_debug (DEBUG_UI, "START");
133 case GL_RESPONSE_SELECT_OTHER:
134 case GTK_RESPONSE_CLOSE:
135 case GTK_RESPONSE_DELETE_EVENT:
136 gtk_widget_hide (GTK_WIDGET (dialog));
139 g_print ("response = %d", response);
140 g_assert_not_reached ();
143 gl_debug (DEBUG_UI, "END");
148 gl_label_properties_dialog_init (glLabelPropertiesDialog *dialog)
150 gchar *builder_filename;
152 static gchar *object_ids[] = {"label_properties_box", NULL};
153 GError *error = NULL;
156 gl_debug (DEBUG_UI, "START");
158 dialog->priv = g_new0 (glLabelPropertiesDialogPrivate, 1);
160 vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
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);
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);
172 g_critical ("%s\n\ngLabels may not be installed correctly!", error->message);
173 g_error_free (error);
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,
194 gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (dialog->priv->box));
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);
200 g_object_unref (G_OBJECT (builder));
202 gl_debug (DEBUG_UI, "END");
207 gl_label_properties_dialog_finalize (GObject *object)
209 glLabelPropertiesDialog* dialog = GL_LABEL_PROPERTIES_DIALOG (object);
211 gl_debug (DEBUG_UI, "START");
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);
218 G_OBJECT_CLASS (gl_label_properties_dialog_parent_class)->finalize (object);
220 gl_debug (DEBUG_UI, "END");
224 /* FIXME: Move this function to library */
226 lgl_template_get_margins (const lglTemplate *template,
231 const lglTemplateFrame *frame;
232 const lglTemplateLayout *layout;
234 /* Set margins to maximum */
235 *hmargin = template->page_width;
236 *vmargin = template->page_height;
238 /* and search lowest margin in layouts */
239 for (p = template->frames; p; p = p->next)
241 frame = (lglTemplateFrame *)p->data;
242 for (q = frame->all.layouts; q; q = q->next)
244 layout = (lglTemplateLayout *)q->data;
245 if (layout->x0 < *hmargin)
246 *hmargin = layout->x0;
247 if (layout->y0 < *vmargin)
248 *vmargin = layout->y0;
253 /* FIXME: Move this function to library */
255 lgl_template_get_margins_string (const lglTemplate *template)
258 gdouble units_per_point;
259 gdouble hmargin, vmargin;
260 gchar *hmargin_str, *vmargin_str;
263 units = gl_prefs_model_get_units (gl_prefs);
264 units_per_point = lgl_units_get_units_per_point (units);
266 lgl_template_get_margins (template, &hmargin, &vmargin);
268 if (units == LGL_UNITS_INCH)
270 hmargin_str = lgl_str_format_fraction (hmargin*units_per_point);
271 vmargin_str = lgl_str_format_fraction (vmargin*units_per_point);
275 hmargin_str = g_strdup_printf ("%.5g", hmargin*units_per_point);
276 vmargin_str = g_strdup_printf ("%.5g", vmargin*units_per_point);
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));
285 g_free (hmargin_str);
286 g_free (vmargin_str);
291 /*--------------------------------------------------------------------------*/
292 /* PRIVATE. Create properties widgets. */
293 /*--------------------------------------------------------------------------*/
295 gl_label_properties_dialog_construct (glLabelPropertiesDialog *dialog,
298 const lglTemplate *template;
299 gchar *template_name;
301 gdouble units_per_point;
308 gboolean rotate_flag;
311 gl_debug (DEBUG_UI, "START");
313 dialog->priv->label = label;
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);
321 vendor = lgl_db_lookup_vendor_from_name (template->brand);
322 if (vendor && vendor->url)
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);
332 gtk_label_set_label (dialog->priv->vendor, template->brand);
336 if (template->product_url)
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);
346 gtk_label_set_label (dialog->priv->partnum, template->part);
350 gtk_label_set_label (dialog->priv->description, template->description);
353 text = gl_label_get_dimensions_string (label);
354 gtk_label_set_label (dialog->priv->dimensions, text);
358 text = lgl_db_lookup_paper_name_from_id (template->paper_id);
359 gtk_label_set_label (dialog->priv->medium, text);
363 text = lgl_template_frame_get_layout_description (template->frames->data);
364 gtk_label_set_label (dialog->priv->layout, text);
368 text = lgl_template_get_margins_string (template);
369 gtk_label_set_label (dialog->priv->margins, text);
372 /* Similar products */
373 list = lgl_db_get_similar_template_name_list (template_name);
376 list_text = g_string_new ("");
377 for (p = list; p; p = p->next)
379 g_string_append (list_text, (char *)p->data);
381 g_string_append_c (list_text, '\n');
383 gtk_label_set_text (dialog->priv->similar, list_text->str);
384 g_string_free (list_text, TRUE);
388 gtk_widget_hide (GTK_WIDGET (dialog->priv->similar_label));
389 gtk_widget_hide (GTK_WIDGET (dialog->priv->similar));
393 gl_label_get_size (label, &w, &h);
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);
402 rotate_flag = gl_label_get_rotate_flag (label);
404 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->rotated_orientation), TRUE);
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);
412 gl_mini_preview_set_template (GL_MINI_PREVIEW (dialog->priv->preview), template);
414 g_free (template_name);
417 g_signal_connect_swapped (G_OBJECT (dialog->priv->normal_orientation),
419 G_CALLBACK (gl_label_properties_dialog_rotation_cb),
421 g_signal_connect_swapped (G_OBJECT (dialog->priv->rotated_orientation),
423 G_CALLBACK (gl_label_properties_dialog_rotation_cb),
425 g_signal_connect (G_OBJECT (dialog),
427 G_CALLBACK (gl_label_properties_dialog_response_cb),
430 gl_debug (DEBUG_UI, "END");
434 /*****************************************************************************/
435 /* NEW document properties dialog. */
436 /*****************************************************************************/
438 gl_label_properties_dialog_new (glLabel *label,
441 glLabelPropertiesDialog *dialog;
443 g_assert (GL_IS_LABEL (label));
445 gl_debug (DEBUG_UI, "START");
447 dialog = GL_LABEL_PROPERTIES_DIALOG (g_object_new (GL_TYPE_LABEL_PROPERTIES_DIALOG, NULL));
450 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
452 gl_label_properties_dialog_construct (GL_LABEL_PROPERTIES_DIALOG (dialog),
455 gl_debug (DEBUG_UI, "END");
457 return GTK_WIDGET (dialog);
464 * Local Variables: -- emacs
466 * c-basic-offset: 8 -- emacs
467 * tab-width: 8 -- emacs
468 * indent-tabs-mode: nil -- emacs