Added button image data transmission from
[irreco.git] / irreco / src / core / irreco_background_creator_dlg.c
blob67649d8dd900909986332f3793fa5b1f0e21f595
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * irreco - Ir Remote Control
4 * Copyright (C) 2008 Pekka Gehör (pegu6@msn.com)
5 *
6 * irreco_background_creator_dlg.c is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * irreco_background_creator_dlg.c is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "irreco_background_creator_dlg.h"
21 #include "irreco_theme_creator_backgrounds.h"
22 #include "irreco_theme_creator_dlg.h"
23 #include <hildon/hildon-banner.h>
24 #include <hildon/hildon-color-button.h>
25 #include <hildon/hildon-file-chooser-dialog.h>
28 /**
29 * @addtogroup IrrecoBackgroundCreatorDlg
30 * @ingroup Irreco
33 * @{
36 /**
37 * @file
38 * Source file of @ref IrrecoBackgroundCreatorDlg.
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
41 /* Datatypes */
42 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
44 #define IRRECO_BACKGROUND_PREVIEW_WIDHT (IRRECO_SCREEN_WIDTH/2)
45 #define IRRECO_BACKGROUND_PREVIEW_HEIGHT (IRRECO_SCREEN_HEIGHT/2)
47 /** Loader states. */
48 enum
50 LOADER_STATE_INIT,
51 LOADER_STATE_THEME,
52 LOADER_STATE_BUTTONS,
53 LOADER_STATE_BACKGROUNDS,
54 LOADER_STATE_END
57 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
58 /* Prototypes */
59 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
60 static void _signal_image_clicked(GtkButton *button,
61 IrrecoBackgroundCreatorDlg *self);
62 static gboolean _draw_preview_image(IrrecoBackgroundCreatorDlg *self,
63 const gchar *image);
65 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
66 /* Construction & Destruction */
67 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
69 /**
70 * @name Construction & Destruction
71 * @{
75 G_DEFINE_TYPE (IrrecoBackgroundCreatorDlg, irreco_background_creator_dlg,
76 IRRECO_TYPE_INTERNAL_DLG)
78 static void irreco_background_creator_dlg_constructed(GObject *object)
81 IrrecoData *irreco_data;
82 IrrecoBackgroundCreatorDlg *self;
84 GtkWidget *table;
85 GtkWidget *frame;
86 GtkWidget *label_name;
87 GtkWidget *label_select;
88 GtkWidget *button_select;
90 IRRECO_ENTER
92 G_OBJECT_CLASS(irreco_background_creator_dlg_parent_class)->constructed(object);
94 self = IRRECO_BACKGROUND_CREATOR_DLG(object);
96 irreco_data = irreco_internal_dlg_get_irreco_data(IRRECO_INTERNAL_DLG(self));
98 /* Construct dialog. */
99 gtk_window_set_title(GTK_WINDOW(self), _("Background Creator "));
100 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
101 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
102 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
104 /*Buttons*/
105 self->cancel_button= gtk_dialog_add_button(GTK_DIALOG(self),
106 _("Cancel"),
107 GTK_RESPONSE_CANCEL);
108 self->add_button= gtk_dialog_add_button(GTK_DIALOG(self),
109 _("Add"), GTK_RESPONSE_OK);
111 /* Create widgets. */
113 table = gtk_table_new(8, 9, TRUE);
114 label_name = gtk_label_new(_("Name:"));
115 label_select = gtk_label_new(_("Background:"));
116 self->entry_name = gtk_entry_new();
117 frame = gtk_frame_new(NULL);
118 self->preview_image = gtk_image_new();
119 button_select = gtk_button_new_with_label(_("Select"));
121 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self)->vbox),
122 table);
124 /* Set widgets on the table */
126 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
127 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
129 gtk_table_attach_defaults(GTK_TABLE(table), label_name, 0, 3, 0, 1);
130 gtk_table_attach_defaults(GTK_TABLE(table), label_select, 0, 3, 1, 2);
131 gtk_table_attach_defaults(GTK_TABLE(table),
132 self->entry_name, 3, 9, 0, 1);
133 gtk_table_attach_defaults(GTK_TABLE(table), button_select, 3, 9, 1, 2);
134 gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 9, 2, 8);
136 gtk_container_add(GTK_CONTAINER(frame), self->preview_image);
138 /* Button signals. */
139 g_signal_connect(G_OBJECT(button_select), "clicked",
140 G_CALLBACK(_signal_image_clicked),
141 self);
143 gtk_widget_show_all(GTK_WIDGET(self));
145 IRRECO_RETURN
147 static void
148 irreco_background_creator_dlg_init (IrrecoBackgroundCreatorDlg *self)
150 IRRECO_ENTER
151 self->filename = g_string_new(NULL);
152 IRRECO_RETURN
155 static void
156 irreco_background_creator_dlg_finalize(GObject *object)
158 /* TODO: Add deinitalization code here */
159 IrrecoBackgroundCreatorDlg *self;
160 IRRECO_ENTER
162 self = IRRECO_BACKGROUND_CREATOR_DLG(object);
163 g_string_free(self->filename, TRUE);
164 self->filename = NULL;
166 G_OBJECT_CLASS(irreco_background_creator_dlg_parent_class)->finalize(object);
167 IRRECO_RETURN
171 static void
172 irreco_background_creator_dlg_class_init(IrrecoBackgroundCreatorDlgClass *klass)
174 GObjectClass* object_class = G_OBJECT_CLASS (klass);
175 object_class->finalize = irreco_background_creator_dlg_finalize;
176 object_class->constructed = irreco_background_creator_dlg_constructed;
178 GtkWidget
179 *irreco_background_creator_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
181 IrrecoBackgroundCreatorDlg *self;
183 IRRECO_ENTER
185 self = g_object_new(IRRECO_TYPE_BACKGROUND_CREATOR_DLG,
186 "irreco-data", irreco_data, NULL);
187 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
189 IRRECO_RETURN_PTR(self);
193 * @deprecated
194 * @todo Replace calls to irreco_background_creator_dlg_create() with
195 * irreco_background_creator_dlg_new().
197 IrrecoBackgroundCreatorDlg
198 *irreco_background_creator_dlg_create(IrrecoData *irreco_data,
199 GtkWindow *parent_window)
201 IRRECO_ENTER
202 IRRECO_RETURN_PTR(irreco_background_creator_dlg_new(
203 irreco_data, parent_window));
206 /** @} */
208 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
209 /* Private Functions */
210 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
212 * @name Private Functions
213 * @{
216 * Draw preview with current image.
218 static gboolean
219 _draw_preview_image(IrrecoBackgroundCreatorDlg *self, const gchar *image)
221 GError *error = NULL;
222 GdkPixbuf *pixbuf = NULL;
224 IRRECO_ENTER
225 g_assert(self != NULL);
227 /* Attempt to load the image. */
228 if (image != NULL) {
229 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
230 IRRECO_BACKGROUND_PREVIEW_WIDHT,
231 IRRECO_BACKGROUND_PREVIEW_HEIGHT,
232 TRUE, &error);
233 if (irreco_gerror_check_print(&error)) {
234 IRRECO_RETURN_BOOL(FALSE);
237 gtk_widget_realize(GTK_WIDGET(self->preview_image));
239 gtk_image_set_from_pixbuf(GTK_IMAGE(self->preview_image),
240 GDK_PIXBUF(pixbuf));
242 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
244 IRRECO_RETURN_BOOL(TRUE);
248 static void _select_image(GtkButton *button, IrrecoBackgroundCreatorDlg *self)
251 GtkWidget *file_dlg = NULL;
252 gchar *image_dir = NULL;
253 IRRECO_ENTER
255 /* Create image select dialog. */
257 file_dlg = hildon_file_chooser_dialog_new(GTK_WINDOW(self),
258 GTK_FILE_CHOOSER_ACTION_OPEN);
259 gtk_window_set_title(GTK_WINDOW(file_dlg),_("Select background image"));
260 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(file_dlg), TRUE);
261 image_dir = g_build_path("/", getenv("HOME"), "MyDocs/.images/", NULL);
262 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dlg),
263 image_dir);
264 g_free(image_dir);
266 /* Loop until user cancels or we get a valid image. */
267 gtk_widget_show_all(GTK_WIDGET(file_dlg));
268 while (gtk_dialog_run(GTK_DIALOG(file_dlg)) == GTK_RESPONSE_OK) {
269 gchar *filename = gtk_file_chooser_get_filename(
270 GTK_FILE_CHOOSER(file_dlg));
272 /* Attempt to display the image. */
273 if (_draw_preview_image(self, filename)) {
274 irreco_gstring_set_and_free(self->filename, filename);
275 filename = NULL;
276 break;
278 /* Cleanup */
279 } else {
280 gchar *basename = g_path_get_basename(filename);
281 irreco_error_dlg_printf(GTK_WINDOW(file_dlg),
282 _("Cannot open image \"%s\""),
283 basename);
284 IRRECO_PRINTF("Image invalid.\n");
286 g_free(basename);
287 g_free(filename);
291 gtk_widget_destroy(file_dlg);
292 IRRECO_RETURN
295 /** @} */
297 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
298 /* Functions */
299 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
302 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
303 /* Public Functions */
304 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
306 IrrecoThemeBg *irreco_background_creator_dlg_run(IrrecoData *irreco_data,
307 IrrecoTheme * irreco_theme,
308 GtkWindow *parent_window,
309 IrrecoThemeBg *bg)
311 IrrecoBackgroundCreatorDlg *self;
312 gint response;
313 gboolean loop = TRUE;
314 /*IrrecoThemeBg *rvalue = NULL;*/
316 IRRECO_ENTER
318 self = (IrrecoBackgroundCreatorDlg*)irreco_background_creator_dlg_create(
319 irreco_data, parent_window);
320 self->irreco_data = irreco_data;
323 if(irreco_theme != NULL) {
324 self->theme = irreco_theme;
326 irreco_theme_bg_print(bg);
327 gtk_entry_set_text(GTK_ENTRY(self->entry_name),
328 bg->image_name->str);
331 _draw_preview_image(self, bg->image_path->str);
335 do {
336 response = gtk_dialog_run(GTK_DIALOG(self));
337 switch (response) {
338 case GTK_RESPONSE_OK:
340 irreco_theme_bg_set(bg,
341 gtk_entry_get_text(GTK_ENTRY(
342 self->entry_name)),
343 self->filename->str);
346 /*self->loader_state = LOADER_STATE_INIT;*/
347 loop = FALSE;
349 break;
351 case GTK_RESPONSE_CANCEL:
352 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
353 loop = FALSE;
354 break;
356 /* case GTK_RESPONSE_CLOSE:
357 IRRECO_DEBUG("GTK_RESPONSE_CLOSE\n");
358 _loader_stop(self);
359 loop = FALSE;
360 rvalue = TRUE;
361 break;*/
363 default:
364 IRRECO_DEBUG("default\n");
365 break;
368 } while (loop);
370 gtk_widget_destroy(GTK_WIDGET(self));
372 IRRECO_RETURN_PTR(bg);
375 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
376 /* Events and Callbacks */
377 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
380 * @name Events and Callbacks
381 * @{
384 static void
385 _signal_image_clicked(GtkButton *button, IrrecoBackgroundCreatorDlg *self)
387 IRRECO_ENTER
388 _select_image(button, self);
389 IRRECO_RETURN
393 /** @} */