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 "prefs-dialog.h"
25 #include <glib/gi18n.h>
29 #include "color-combo.h"
31 #include "font-combo.h"
32 #include "font-util.h"
33 #include "builder-util.h"
38 /*========================================================*/
39 /* Private macros and constants. */
40 /*========================================================*/
42 #define US_LETTER_ID "US-Letter"
46 /*========================================================*/
48 /*========================================================*/
50 struct _glPrefsDialogPrivate
54 /* Units properties */
55 GtkWidget *units_points_radio;
56 GtkWidget *units_inches_radio;
57 GtkWidget *units_mm_radio;
59 /* Page size properties */
60 GtkWidget *page_size_us_letter_radio;
61 GtkWidget *page_size_a4_radio;
63 /* Default text properties */
64 GtkWidget *text_family_hbox;
65 GtkWidget *text_family_combo;
66 GtkWidget *text_size_spin;
67 GtkWidget *text_bold_toggle;
68 GtkWidget *text_italic_toggle;
69 GtkWidget *text_color_hbox;
70 GtkWidget *text_color_combo;
71 GtkWidget *text_left_toggle;
72 GtkWidget *text_center_toggle;
73 GtkWidget *text_right_toggle;
74 GtkWidget *text_line_spacing_spin;
76 /* Default line properties */
77 GtkWidget *line_width_spin;
78 GtkWidget *line_color_hbox;
79 GtkWidget *line_color_combo;
81 /* Default fill properties */
82 GtkWidget *fill_color_hbox;
83 GtkWidget *fill_color_combo;
85 /* Prevent recursion */
86 gboolean stop_signals;
90 /*========================================================*/
91 /* Private globals. */
92 /*========================================================*/
95 /*========================================================*/
96 /* Private function prototypes. */
97 /*========================================================*/
99 static void gl_prefs_dialog_finalize (GObject *object);
100 static void gl_prefs_dialog_construct (glPrefsDialog *dialog);
102 static void response_cb (glPrefsDialog *dialog,
106 static void construct_locale_page (glPrefsDialog *dialog);
107 static void construct_object_page (glPrefsDialog *dialog);
109 static void align_toggle_cb (GtkToggleButton *toggle,
110 glPrefsDialog *dialog);
112 static void update_locale_page_from_prefs (glPrefsDialog *dialog);
113 static void update_object_page_from_prefs (glPrefsDialog *dialog);
115 static void update_prefs_from_locale_page (glPrefsDialog *dialog);
116 static void update_prefs_from_object_page (glPrefsDialog *dialog);
119 /*****************************************************************************/
120 /* Boilerplate object stuff. */
121 /*****************************************************************************/
122 G_DEFINE_TYPE (glPrefsDialog, gl_prefs_dialog, GTK_TYPE_DIALOG)
126 gl_prefs_dialog_class_init (glPrefsDialogClass *class)
128 GObjectClass *object_class = G_OBJECT_CLASS (class);
130 gl_debug (DEBUG_PREFS, "");
132 gl_prefs_dialog_parent_class = g_type_class_peek_parent (class);
134 object_class->finalize = gl_prefs_dialog_finalize;
139 gl_prefs_dialog_init (glPrefsDialog *dialog)
141 gchar *builder_filename;
142 static gchar *object_ids[] = { "prefs_notebook",
143 "adjustment1", "adjustment2", "adjustment3",
145 GError *error = NULL;
147 gl_debug (DEBUG_PREFS, "START");
149 dialog->priv = g_new0 (glPrefsDialogPrivate, 1);
151 dialog->priv->builder = gtk_builder_new ();
152 builder_filename = g_build_filename (GLABELS_DATA_DIR, "ui", "prefs-dialog.ui", NULL);
153 gtk_builder_add_objects_from_file (dialog->priv->builder, builder_filename, object_ids, &error);
154 g_free (builder_filename);
156 g_critical ("%s\n\ngLabels may not be installed correctly!", error->message);
157 g_error_free (error);
161 gtk_dialog_add_button (GTK_DIALOG(dialog), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
162 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
164 g_signal_connect(G_OBJECT (dialog), "response",
165 G_CALLBACK (response_cb), NULL);
167 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
168 gtk_window_set_title (GTK_WINDOW (dialog), _("gLabels Preferences"));
169 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
171 gl_debug (DEBUG_PREFS, "END");
176 gl_prefs_dialog_finalize (GObject *object)
178 glPrefsDialog* dialog = GL_PREFS_DIALOG (object);
180 gl_debug (DEBUG_PREFS, "START");
182 g_return_if_fail (object != NULL);
183 g_return_if_fail (GL_IS_PREFS_DIALOG (dialog));
184 g_return_if_fail (dialog->priv != NULL);
186 if (dialog->priv->builder)
188 g_object_unref (G_OBJECT (dialog->priv->builder));
190 g_free (dialog->priv);
192 G_OBJECT_CLASS (gl_prefs_dialog_parent_class)->finalize (object);
194 gl_debug (DEBUG_PREFS, "END");
198 /*****************************************************************************/
199 /* NEW preferences dialog. */
200 /*****************************************************************************/
202 gl_prefs_dialog_new (GtkWindow *parent)
206 gl_debug (DEBUG_PREFS, "START");
208 dialog = GTK_WIDGET (g_object_new (GL_TYPE_PREFS_DIALOG, NULL));
211 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
213 gl_prefs_dialog_construct (GL_PREFS_DIALOG(dialog));
216 gl_debug (DEBUG_PREFS, "END");
222 /*---------------------------------------------------------------------------*/
223 /* PRIVATE. Construct composite widget. */
224 /*---------------------------------------------------------------------------*/
226 gl_prefs_dialog_construct (glPrefsDialog *dialog)
231 g_return_if_fail (GL_IS_PREFS_DIALOG (dialog));
232 g_return_if_fail (dialog->priv != NULL);
234 vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
236 gl_builder_util_get_widgets (dialog->priv->builder,
237 "prefs_notebook", ¬ebook,
239 gtk_box_pack_start (GTK_BOX (vbox), notebook, FALSE, FALSE, 0);
241 construct_locale_page (dialog);
242 construct_object_page (dialog);
244 update_locale_page_from_prefs (dialog);
245 update_object_page_from_prefs (dialog);
247 gtk_widget_show_all (vbox);
251 /*---------------------------------------------------------------------------*/
252 /* PRIVATE. "Response" callback. */
253 /*---------------------------------------------------------------------------*/
255 response_cb (glPrefsDialog *dialog,
259 gl_debug (DEBUG_VIEW, "START");
261 g_return_if_fail(dialog != NULL);
262 g_return_if_fail(GTK_IS_DIALOG(dialog));
265 case GTK_RESPONSE_CLOSE:
266 gtk_widget_hide (GTK_WIDGET(dialog));
268 case GTK_RESPONSE_DELETE_EVENT:
271 g_print ("response = %d", response);
272 g_assert_not_reached ();
275 gl_debug (DEBUG_VIEW, "END");
279 /*--------------------------------------------------------------------------*/
280 /* PRIVATE. Build Locale Properties Notebook Tab */
281 /*--------------------------------------------------------------------------*/
283 construct_locale_page (glPrefsDialog *dialog)
286 gl_builder_util_get_widgets (dialog->priv->builder,
287 "units_points_radio", &dialog->priv->units_points_radio,
288 "units_inches_radio", &dialog->priv->units_inches_radio,
289 "units_mm_radio", &dialog->priv->units_mm_radio,
290 "page_size_us_letter_radio", &dialog->priv->page_size_us_letter_radio,
291 "page_size_a4_radio", &dialog->priv->page_size_a4_radio,
294 g_signal_connect_swapped (
295 G_OBJECT(dialog->priv->units_points_radio),
296 "toggled", G_CALLBACK(update_prefs_from_locale_page), G_OBJECT(dialog));
297 g_signal_connect_swapped (
298 G_OBJECT(dialog->priv->units_inches_radio),
299 "toggled", G_CALLBACK(update_prefs_from_locale_page), G_OBJECT(dialog));
300 g_signal_connect_swapped (
301 G_OBJECT(dialog->priv->units_mm_radio),
302 "toggled", G_CALLBACK(update_prefs_from_locale_page), G_OBJECT(dialog));
303 g_signal_connect_swapped (
304 G_OBJECT(dialog->priv->page_size_us_letter_radio),
305 "toggled", G_CALLBACK(update_prefs_from_locale_page), G_OBJECT(dialog));
306 g_signal_connect_swapped (
307 G_OBJECT(dialog->priv->page_size_a4_radio),
308 "toggled", G_CALLBACK(update_prefs_from_locale_page), G_OBJECT(dialog));
312 /*--------------------------------------------------------------------------*/
313 /* PRIVATE. Build Default Object Properties Notebook Tab */
314 /*--------------------------------------------------------------------------*/
316 construct_object_page (glPrefsDialog *dialog)
318 gl_builder_util_get_widgets (dialog->priv->builder,
319 "text_family_hbox", &dialog->priv->text_family_hbox,
320 "text_size_spin", &dialog->priv->text_size_spin,
321 "text_bold_toggle", &dialog->priv->text_bold_toggle,
322 "text_italic_toggle", &dialog->priv->text_italic_toggle,
323 "text_color_hbox", &dialog->priv->text_color_hbox,
324 "text_left_toggle", &dialog->priv->text_left_toggle,
325 "text_center_toggle", &dialog->priv->text_center_toggle,
326 "text_right_toggle", &dialog->priv->text_right_toggle,
327 "text_line_spacing_spin", &dialog->priv->text_line_spacing_spin,
328 "line_width_spin", &dialog->priv->line_width_spin,
329 "line_color_hbox", &dialog->priv->line_color_hbox,
330 "fill_color_hbox", &dialog->priv->fill_color_hbox,
333 dialog->priv->text_family_combo = gl_font_combo_new ("Sans");
335 dialog->priv->text_color_combo = gl_color_combo_new (_("Default"),
336 GL_COLOR_TEXT_DEFAULT,
337 gl_prefs_model_get_default_text_color (gl_prefs));
338 dialog->priv->line_color_combo = gl_color_combo_new (_("No Line"),
340 gl_prefs_model_get_default_line_color (gl_prefs));
341 dialog->priv->fill_color_combo = gl_color_combo_new (_("No Fill"),
343 gl_prefs_model_get_default_fill_color (gl_prefs));
345 gtk_box_pack_start (GTK_BOX (dialog->priv->text_family_hbox),
346 dialog->priv->text_family_combo,
348 gtk_box_pack_start (GTK_BOX (dialog->priv->text_color_hbox),
349 dialog->priv->text_color_combo,
351 gtk_box_pack_start (GTK_BOX (dialog->priv->line_color_hbox),
352 dialog->priv->line_color_combo,
354 gtk_box_pack_start (GTK_BOX (dialog->priv->fill_color_hbox),
355 dialog->priv->fill_color_combo,
358 g_signal_connect_swapped (G_OBJECT(dialog->priv->text_family_combo),
360 G_CALLBACK(update_prefs_from_object_page),
362 g_signal_connect_swapped (G_OBJECT(dialog->priv->text_size_spin),
364 G_CALLBACK(update_prefs_from_object_page),
366 g_signal_connect_swapped (G_OBJECT(dialog->priv->text_bold_toggle),
368 G_CALLBACK(update_prefs_from_object_page),
370 g_signal_connect_swapped (G_OBJECT(dialog->priv->text_italic_toggle),
372 G_CALLBACK(update_prefs_from_object_page),
374 g_signal_connect_swapped (G_OBJECT(dialog->priv->text_color_combo),
376 G_CALLBACK(update_prefs_from_object_page),
379 g_signal_connect (G_OBJECT(dialog->priv->text_left_toggle),
381 G_CALLBACK(align_toggle_cb),
383 g_signal_connect (G_OBJECT(dialog->priv->text_center_toggle),
385 G_CALLBACK(align_toggle_cb),
387 g_signal_connect (G_OBJECT(dialog->priv->text_right_toggle),
389 G_CALLBACK(align_toggle_cb),
392 g_signal_connect_swapped (G_OBJECT(dialog->priv->text_line_spacing_spin),
394 G_CALLBACK(update_prefs_from_object_page),
397 g_signal_connect_swapped (G_OBJECT(dialog->priv->line_width_spin),
399 G_CALLBACK(update_prefs_from_object_page),
401 g_signal_connect_swapped (G_OBJECT(dialog->priv->line_color_combo),
403 G_CALLBACK(update_prefs_from_object_page),
406 g_signal_connect_swapped (G_OBJECT(dialog->priv->fill_color_combo),
408 G_CALLBACK(update_prefs_from_object_page),
413 /*--------------------------------------------------------------------------*/
414 /* PRIVATE. Alignment togglebutton callback. */
415 /*--------------------------------------------------------------------------*/
417 align_toggle_cb (GtkToggleButton *toggle,
418 glPrefsDialog *dialog)
420 if (gtk_toggle_button_get_active (toggle))
423 if (GTK_WIDGET (toggle) == GTK_WIDGET (dialog->priv->text_left_toggle))
425 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
426 (dialog->priv->text_center_toggle),
428 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
429 (dialog->priv->text_right_toggle),
432 else if (GTK_WIDGET (toggle) == GTK_WIDGET (dialog->priv->text_center_toggle))
434 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
435 (dialog->priv->text_left_toggle),
437 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
438 (dialog->priv->text_right_toggle),
441 else if (GTK_WIDGET (toggle) == GTK_WIDGET (dialog->priv->text_right_toggle))
443 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
444 (dialog->priv->text_left_toggle),
446 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
447 (dialog->priv->text_center_toggle),
451 update_prefs_from_object_page (dialog);
457 /*--------------------------------------------------------------------------*/
458 /* PRIVATE. Update locale page widgets from current prefs. */
459 /*--------------------------------------------------------------------------*/
461 update_locale_page_from_prefs (glPrefsDialog *dialog)
463 dialog->priv->stop_signals = TRUE;
465 switch (gl_prefs_model_get_units (gl_prefs)) {
466 case LGL_UNITS_POINT:
467 gtk_toggle_button_set_active (
468 GTK_TOGGLE_BUTTON(dialog->priv->units_points_radio),
472 gtk_toggle_button_set_active (
473 GTK_TOGGLE_BUTTON(dialog->priv->units_inches_radio),
477 gtk_toggle_button_set_active (
478 GTK_TOGGLE_BUTTON(dialog->priv->units_mm_radio),
482 g_message ("Illegal units"); /* Should not happen */
486 if ( g_ascii_strcasecmp(gl_prefs_model_get_default_page_size (gl_prefs), US_LETTER_ID) == 0)
488 gtk_toggle_button_set_active (
489 GTK_TOGGLE_BUTTON(dialog->priv->page_size_us_letter_radio), TRUE);
491 else if ( g_ascii_strcasecmp(gl_prefs_model_get_default_page_size (gl_prefs), A4_ID) == 0)
493 gtk_toggle_button_set_active (
494 GTK_TOGGLE_BUTTON(dialog->priv->page_size_a4_radio), TRUE);
498 g_message ("Unknown default page size"); /* Shouldn't happen */
499 gtk_toggle_button_set_active (
500 GTK_TOGGLE_BUTTON(dialog->priv->page_size_a4_radio), TRUE);
503 dialog->priv->stop_signals = FALSE;
507 /*--------------------------------------------------------------------------*/
508 /* PRIVATE. Update object page widgets from current prefs. */
509 /*--------------------------------------------------------------------------*/
511 update_object_page_from_prefs (glPrefsDialog *dialog)
514 dialog->priv->stop_signals = TRUE;
516 gl_font_combo_set_family (GL_FONT_COMBO (dialog->priv->text_family_combo),
517 gl_prefs_model_get_default_font_family (gl_prefs));
519 gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->priv->text_size_spin),
520 gl_prefs_model_get_default_font_size (gl_prefs));
522 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->text_bold_toggle),
523 (gl_prefs_model_get_default_font_weight (gl_prefs) == PANGO_WEIGHT_BOLD));
525 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->text_italic_toggle),
526 gl_prefs_model_get_default_font_italic_flag (gl_prefs));
528 gl_color_combo_set_color (GL_COLOR_COMBO(dialog->priv->text_color_combo),
529 gl_prefs_model_get_default_text_color (gl_prefs));
531 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->text_left_toggle),
532 (gl_prefs_model_get_default_text_alignment (gl_prefs) == PANGO_ALIGN_LEFT));
533 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->text_center_toggle),
534 (gl_prefs_model_get_default_text_alignment (gl_prefs) == PANGO_ALIGN_CENTER));
535 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->text_right_toggle),
536 (gl_prefs_model_get_default_text_alignment (gl_prefs) == PANGO_ALIGN_RIGHT));
538 gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->priv->text_line_spacing_spin),
539 gl_prefs_model_get_default_text_line_spacing (gl_prefs));
541 gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->priv->line_width_spin),
542 gl_prefs_model_get_default_line_width (gl_prefs));
544 gl_color_combo_set_color (GL_COLOR_COMBO(dialog->priv->line_color_combo),
545 gl_prefs_model_get_default_line_color (gl_prefs));
548 gl_color_combo_set_color (GL_COLOR_COMBO(dialog->priv->fill_color_combo),
549 gl_prefs_model_get_default_fill_color (gl_prefs));
552 dialog->priv->stop_signals = FALSE;
556 /*--------------------------------------------------------------------------*/
557 /* PRIVATE. Update prefs from current state of locale page widgets. */
558 /*--------------------------------------------------------------------------*/
560 update_prefs_from_locale_page (glPrefsDialog *dialog)
562 if (dialog->priv->stop_signals) return;
564 if (gtk_toggle_button_get_active (
565 GTK_TOGGLE_BUTTON(dialog->priv->units_points_radio)))
567 gl_prefs_model_set_units (gl_prefs, LGL_UNITS_POINT);
569 if (gtk_toggle_button_get_active (
570 GTK_TOGGLE_BUTTON(dialog->priv->units_inches_radio)))
572 gl_prefs_model_set_units (gl_prefs, LGL_UNITS_INCH);
574 if (gtk_toggle_button_get_active (
575 GTK_TOGGLE_BUTTON(dialog->priv->units_mm_radio)))
577 gl_prefs_model_set_units (gl_prefs, LGL_UNITS_MM);
580 if (gtk_toggle_button_get_active (
581 GTK_TOGGLE_BUTTON(dialog->priv->page_size_us_letter_radio)))
583 gl_prefs_model_set_default_page_size (gl_prefs, US_LETTER_ID);
585 if (gtk_toggle_button_get_active (
586 GTK_TOGGLE_BUTTON(dialog->priv->page_size_a4_radio)))
588 gl_prefs_model_set_default_page_size (gl_prefs, A4_ID);
593 /*--------------------------------------------------------------------------*/
594 /* PRIVATE. Update prefs from current state of object page widgets. */
595 /*--------------------------------------------------------------------------*/
597 update_prefs_from_object_page (glPrefsDialog *dialog)
602 if (dialog->priv->stop_signals) return;
604 gl_prefs_model_set_default_font_family (gl_prefs,
605 gl_font_combo_get_family (GL_FONT_COMBO (dialog->priv->text_family_combo)));
606 gl_prefs_model_set_default_font_size (gl_prefs,
607 gtk_spin_button_get_value (GTK_SPIN_BUTTON(dialog->priv->text_size_spin)));
609 if (gtk_toggle_button_get_active
610 (GTK_TOGGLE_BUTTON (dialog->priv->text_bold_toggle)))
612 gl_prefs_model_set_default_font_weight (gl_prefs, PANGO_WEIGHT_BOLD);
616 gl_prefs_model_set_default_font_weight (gl_prefs, PANGO_WEIGHT_NORMAL);
619 gl_prefs_model_set_default_font_italic_flag (gl_prefs,
620 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
621 (dialog->priv->text_italic_toggle)));
623 color = gl_color_combo_get_color (GL_COLOR_COMBO(dialog->priv->text_color_combo),
627 gl_prefs_model_set_default_text_color (gl_prefs, color);
630 if (gtk_toggle_button_get_active
631 (GTK_TOGGLE_BUTTON (dialog->priv->text_left_toggle)))
633 gl_prefs_model_set_default_text_alignment (gl_prefs, GTK_JUSTIFY_LEFT);
635 else if (gtk_toggle_button_get_active
636 (GTK_TOGGLE_BUTTON (dialog->priv->text_right_toggle)))
638 gl_prefs_model_set_default_text_alignment (gl_prefs, GTK_JUSTIFY_RIGHT);
640 else if (gtk_toggle_button_get_active
641 (GTK_TOGGLE_BUTTON (dialog->priv->text_center_toggle)))
643 gl_prefs_model_set_default_text_alignment (gl_prefs, GTK_JUSTIFY_CENTER);
647 /* Should not happen. */
648 gl_prefs_model_set_default_text_alignment (gl_prefs, GTK_JUSTIFY_LEFT);
652 gl_prefs_model_set_default_text_line_spacing (gl_prefs,
653 gtk_spin_button_get_value (GTK_SPIN_BUTTON(dialog->priv->text_line_spacing_spin)));
655 gl_prefs_model_set_default_line_width (gl_prefs,
656 gtk_spin_button_get_value (GTK_SPIN_BUTTON(dialog->priv->line_width_spin)));
658 color = gl_color_combo_get_color (GL_COLOR_COMBO(dialog->priv->line_color_combo),
662 gl_prefs_model_set_default_line_color (gl_prefs, color);
666 color = gl_color_combo_get_color (GL_COLOR_COMBO(dialog->priv->fill_color_combo),
670 gl_prefs_model_set_default_fill_color (gl_prefs, color);
677 * Local Variables: -- emacs
679 * c-basic-offset: 8 -- emacs
680 * tab-width: 8 -- emacs
681 * indent-tabs-mode: nil -- emacs