1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * object-editor.c: object properties editor module
8 * Copyright (C) 2003 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "object-editor.h"
28 #include <glib/gi18n.h>
29 #include <gtk/gtkimage.h>
30 #include <gtk/gtkfilechooserbutton.h>
31 #include <gtk/gtkcombobox.h>
32 #include <gtk/gtktogglebutton.h>
38 #include "object-editor-private.h"
42 /*===========================================*/
44 /*===========================================*/
46 /*===========================================*/
47 /* Private data types */
48 /*===========================================*/
50 /*===========================================*/
52 /*===========================================*/
54 /*===========================================*/
55 /* Local function prototypes */
56 /*===========================================*/
58 static void img_radio_toggled_cb (glObjectEditor *editor);
60 static void update_preview_cb (GtkFileChooser *file_chooser,
62 static void add_image_filters_to_chooser (GtkFileChooser *file_chooser);
64 static void img_selection_changed_cb (glObjectEditor *editor);
67 /*--------------------------------------------------------------------------*/
68 /* PRIVATE. Prepare image page. */
69 /*--------------------------------------------------------------------------*/
71 gl_object_editor_prepare_image_page (glObjectEditor *editor)
76 gl_debug (DEBUG_EDITOR, "START");
78 /* Extract widgets from XML tree. */
79 editor->priv->img_page_vbox = glade_xml_get_widget (editor->priv->gui,
81 editor->priv->img_file_radio = glade_xml_get_widget (editor->priv->gui,
83 editor->priv->img_key_radio = glade_xml_get_widget (editor->priv->gui,
85 editor->priv->img_file_button = glade_xml_get_widget (editor->priv->gui,
87 editor->priv->img_key_combo = glade_xml_get_widget (editor->priv->gui,
90 gl_util_combo_box_add_text_model ( GTK_COMBO_BOX(editor->priv->img_key_combo));
92 /* Modify file button properties. */
93 add_image_filters_to_chooser (GTK_FILE_CHOOSER (editor->priv->img_file_button));
94 preview = gtk_image_new ();
95 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (editor->priv->img_file_button), preview);
96 g_signal_connect (G_OBJECT (editor->priv->img_file_button),
98 G_CALLBACK (update_preview_cb), preview);
101 gtk_widget_show_all (editor->priv->img_page_vbox);
103 /* Connect signals */
104 g_signal_connect_swapped (G_OBJECT (editor->priv->img_file_button),
106 G_CALLBACK (img_selection_changed_cb),
108 g_signal_connect_swapped (G_OBJECT (editor->priv->img_key_combo),
110 G_CALLBACK (gl_object_editor_changed_cb),
112 g_signal_connect_swapped (G_OBJECT (editor->priv->img_file_radio),
114 G_CALLBACK (img_radio_toggled_cb),
116 g_signal_connect_swapped (G_OBJECT (editor->priv->img_key_radio),
118 G_CALLBACK (img_radio_toggled_cb),
121 gl_debug (DEBUG_EDITOR, "END");
124 /*--------------------------------------------------------------------------*/
125 /* PRIVATE. image radio callback. */
126 /*--------------------------------------------------------------------------*/
128 img_radio_toggled_cb (glObjectEditor *editor)
130 gl_debug (DEBUG_WDGT, "START");
132 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->img_file_radio))) {
133 gtk_widget_set_sensitive (editor->priv->img_file_button, TRUE);
134 gtk_widget_set_sensitive (editor->priv->img_key_combo, FALSE);
136 gtk_widget_set_sensitive (editor->priv->img_file_button, FALSE);
137 gtk_widget_set_sensitive (editor->priv->img_key_combo, TRUE);
140 /* Emit our "changed" signal */
141 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
143 gl_debug (DEBUG_WDGT, "END");
146 /*****************************************************************************/
148 /*****************************************************************************/
150 gl_object_editor_set_image (glObjectEditor *editor,
152 glTextNode *text_node)
154 gl_debug (DEBUG_EDITOR, "START");
156 g_signal_handlers_block_by_func (G_OBJECT (editor->priv->img_file_button),
157 G_CALLBACK (gl_object_editor_changed_cb),
159 g_signal_handlers_block_by_func (G_OBJECT (editor->priv->img_key_combo),
160 G_CALLBACK (gl_object_editor_changed_cb),
162 g_signal_handlers_block_by_func (G_OBJECT (editor->priv->img_file_radio),
163 G_CALLBACK (img_radio_toggled_cb),
165 g_signal_handlers_block_by_func (G_OBJECT (editor->priv->img_key_radio),
166 G_CALLBACK (img_radio_toggled_cb),
169 gtk_widget_set_sensitive (editor->priv->img_key_radio, merge_flag);
171 if (!text_node->field_flag) {
173 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
174 (editor->priv->img_file_radio), TRUE);
175 gtk_widget_set_sensitive (editor->priv->img_file_button, TRUE);
176 gtk_widget_set_sensitive (editor->priv->img_key_combo, FALSE);
178 if (text_node->data != NULL ) {
179 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(editor->priv->img_file_button),
182 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER(editor->priv->img_file_button));
186 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
187 (editor->priv->img_key_radio), TRUE);
189 gtk_widget_set_sensitive (editor->priv->img_file_button, FALSE);
190 gtk_widget_set_sensitive (editor->priv->img_key_combo, TRUE);
192 gl_util_combo_box_set_active_text (GTK_COMBO_BOX (editor->priv->img_key_combo),
196 g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->img_file_button),
197 G_CALLBACK (gl_object_editor_changed_cb),
199 g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->img_key_combo),
200 G_CALLBACK (gl_object_editor_changed_cb),
202 g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->img_file_radio),
203 G_CALLBACK (img_radio_toggled_cb),
205 g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->img_key_radio),
206 G_CALLBACK (img_radio_toggled_cb),
209 gl_debug (DEBUG_EDITOR, "END");
212 /*****************************************************************************/
214 /*****************************************************************************/
216 gl_object_editor_get_image (glObjectEditor *editor)
218 glTextNode *text_node;
220 gl_debug (DEBUG_EDITOR, "START");
222 text_node = g_new0(glTextNode,1);
224 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->img_file_radio))) {
225 text_node->field_flag = FALSE;
227 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(editor->priv->img_file_button));
229 text_node->field_flag = TRUE;
231 gtk_combo_box_get_active_text (GTK_COMBO_BOX (editor->priv->img_key_combo));
234 gl_debug (DEBUG_EDITOR, "text_node: field_flag=%d, data=%s",
235 text_node->field_flag, text_node->data);
237 gl_debug (DEBUG_EDITOR, "END");
242 /*--------------------------------------------------------------------------*/
243 /* PRIVATE. Update preview image. */
244 /*--------------------------------------------------------------------------*/
246 update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
251 gboolean have_preview;
253 preview = GTK_WIDGET (data);
254 filename = gtk_file_chooser_get_preview_filename (file_chooser);
257 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
258 have_preview = (pixbuf != NULL);
261 gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
263 gdk_pixbuf_unref (pixbuf);
265 gtk_file_chooser_set_preview_widget_active (file_chooser,
268 gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
272 /*--------------------------------------------------------------------------*/
273 /* PRIVATE. Add filters to file chooser. */
274 /* Based on eog's eog_file_selection_add_filter(). */
275 /*--------------------------------------------------------------------------*/
277 add_image_filters_to_chooser (GtkFileChooser *chooser)
279 GtkFileFilter *all_file_filter;
280 GtkFileFilter *all_img_filter;
283 GtkFileFilter *filter;
284 GSList *filters = NULL;
285 gchar **mime_types, **pattern, *tmp;
288 /* All Files Filter */
289 all_file_filter = gtk_file_filter_new ();
290 gtk_file_filter_set_name (all_file_filter, _("All Files"));
291 gtk_file_filter_add_pattern (all_file_filter, "*");
293 /* All Image Filter */
294 all_img_filter = gtk_file_filter_new ();
295 gtk_file_filter_set_name (all_img_filter, _("All Images"));
297 /* Individual image filters */
298 formats = gdk_pixbuf_get_formats ();
299 for (it = formats; it != NULL; it = it->next) {
301 GdkPixbufFormat *format;
302 filter = gtk_file_filter_new ();
303 gchar *description, *name;
305 format = (GdkPixbufFormat*) it->data;
307 /* Filter name: First description then file extension, eg. "The PNG-Format (*.png)".*/
308 description = gdk_pixbuf_format_get_description (format);
309 name = gdk_pixbuf_format_get_name (format);
310 filter_name = g_strdup_printf (_("%s (*.%s)"), description, name);
311 g_free (description);
313 gtk_file_filter_set_name (filter, filter_name);
314 g_free (filter_name);
316 mime_types = gdk_pixbuf_format_get_mime_types ((GdkPixbufFormat *) it->data);
317 for (i = 0; mime_types[i] != NULL; i++) {
318 gtk_file_filter_add_mime_type (filter, mime_types[i]);
319 gtk_file_filter_add_mime_type (all_img_filter, mime_types[i]);
321 g_strfreev (mime_types);
323 pattern = gdk_pixbuf_format_get_extensions ((GdkPixbufFormat *) it->data);
324 for (i = 0; pattern[i] != NULL; i++) {
325 tmp = g_strconcat ("*.", pattern[i], NULL);
326 gtk_file_filter_add_pattern (filter, tmp);
327 gtk_file_filter_add_pattern (all_img_filter, tmp);
330 g_strfreev (pattern);
332 filters = g_slist_prepend (filters, filter);
334 g_slist_free (formats);
336 /* Add filter to filechooser */
337 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), all_file_filter);
338 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), all_img_filter);
339 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser), all_img_filter);
341 for (it = filters; it != NULL; it = it->next) {
342 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser),
343 GTK_FILE_FILTER (it->data));
345 g_slist_free (filters);
348 /*--------------------------------------------------------------------------*/
349 /* PRIVATE. Selection changed callback. */
350 /*--------------------------------------------------------------------------*/
352 img_selection_changed_cb (glObjectEditor *editor)
356 gl_debug (DEBUG_EDITOR, "START");
358 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(editor->priv->img_file_button));
359 if (filename != NULL)
361 /* Emit our "changed" signal */
362 g_signal_emit (G_OBJECT (editor), gl_object_editor_signals[CHANGED], 0);
366 gl_debug (DEBUG_EDITOR, "END");