added a few amendments
[irreco.git] / irreco / src / core / irreco_background_creator_dlg.c
blob183c2fa9872e16824936bf557324a0eafda906e5
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)
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.5)
45 #define IRRECO_BACKGROUND_PREVIEW_HEIGHT (IRRECO_SCREEN_HEIGHT/2.5)
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);
64 void
65 _set_bg_details(IrrecoBackgroundCreatorDlg *self, IrrecoTheme *irreco_theme,
66 IrrecoThemeBg *bg);
68 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
69 /* Construction & Destruction */
70 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
72 /**
73 * @name Construction & Destruction
74 * @{
78 G_DEFINE_TYPE (IrrecoBackgroundCreatorDlg, irreco_background_creator_dlg,
79 IRRECO_TYPE_INTERNAL_DLG)
81 static void irreco_background_creator_dlg_constructed(GObject *object)
84 IrrecoBackgroundCreatorDlg *self;
85 GtkWidget *table;
86 GtkWidget *frame;
87 GtkWidget *label_name;
88 GtkWidget *label_select;
89 GtkWidget *button_select;
90 GtkWidget *vbox_preview;
91 IRRECO_ENTER
93 G_OBJECT_CLASS(
94 irreco_background_creator_dlg_parent_class)->constructed(object);
96 self = IRRECO_BACKGROUND_CREATOR_DLG(object);
98 /* Construct dialog. */
99 gtk_window_set_title(GTK_WINDOW(self), _("Create a Background"));
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);
110 gtk_widget_set_sensitive(self->add_button, FALSE);
112 /* Create widgets. */
114 table = gtk_table_new(8, 9, TRUE);
115 label_name = gtk_label_new(_("Name:"));
116 label_select = gtk_label_new(_("Background:"));
117 self->entry_name = gtk_entry_new();
118 frame = gtk_frame_new(NULL);
119 vbox_preview = gtk_vbox_new(FALSE, 8);
120 self->label_size = gtk_label_new("");
121 self->preview_image = gtk_image_new();
122 button_select = gtk_button_new_with_label(_("Select"));
124 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self)->vbox),
125 table);
127 /* equal to the text of the left-side */
128 gtk_misc_set_alignment(GTK_MISC(label_name), 0, 0.5);
129 gtk_misc_set_alignment(GTK_MISC(label_select), 0, 0.5);
131 /* Set widgets on the table */
133 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
134 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
136 gtk_table_attach_defaults(GTK_TABLE(table), label_name, 0, 3, 0, 1);
137 gtk_table_attach_defaults(GTK_TABLE(table), label_select, 0, 3, 1, 2);
138 gtk_table_attach_defaults(GTK_TABLE(table),
139 self->entry_name, 3, 9, 0, 1);
140 gtk_table_attach_defaults(GTK_TABLE(table), button_select, 3, 9, 1, 2);
141 gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 9, 2, 8);
143 gtk_container_add(GTK_CONTAINER(frame), vbox_preview);
144 gtk_container_add(GTK_CONTAINER(vbox_preview), self->preview_image);
145 gtk_container_add(GTK_CONTAINER(vbox_preview), self->label_size);
148 /* Button signals. */
149 g_signal_connect(G_OBJECT(button_select), "clicked",
150 G_CALLBACK(_signal_image_clicked),
151 self);
153 gtk_widget_show_all(GTK_WIDGET(self));
155 IRRECO_RETURN
158 static void
159 irreco_background_creator_dlg_init (IrrecoBackgroundCreatorDlg *self)
161 IRRECO_ENTER
162 self->filename = g_string_new(NULL);
163 IRRECO_RETURN
166 static void
167 irreco_background_creator_dlg_finalize(GObject *object)
169 /* TODO: Add deinitalization code here */
170 IrrecoBackgroundCreatorDlg *self;
171 IRRECO_ENTER
173 self = IRRECO_BACKGROUND_CREATOR_DLG(object);
174 g_string_free(self->filename, TRUE);
175 self->filename = NULL;
177 G_OBJECT_CLASS(irreco_background_creator_dlg_parent_class)->finalize(object);
178 IRRECO_RETURN
182 static void
183 irreco_background_creator_dlg_class_init(IrrecoBackgroundCreatorDlgClass *klass)
185 GObjectClass* object_class = G_OBJECT_CLASS (klass);
186 object_class->finalize = irreco_background_creator_dlg_finalize;
187 object_class->constructed = irreco_background_creator_dlg_constructed;
190 GtkWidget
191 *irreco_background_creator_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
193 IrrecoBackgroundCreatorDlg *self;
195 IRRECO_ENTER
197 self = g_object_new(IRRECO_TYPE_BACKGROUND_CREATOR_DLG,
198 "irreco-data", irreco_data, NULL);
199 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
201 IRRECO_RETURN_PTR(self);
205 * @deprecated
206 * @todo Replace calls to irreco_background_creator_dlg_create() with
207 * irreco_background_creator_dlg_new().
209 IrrecoBackgroundCreatorDlg
210 *irreco_background_creator_dlg_create(IrrecoData *irreco_data,
211 GtkWindow *parent_window)
213 IRRECO_ENTER
214 IRRECO_RETURN_PTR(irreco_background_creator_dlg_new(
215 irreco_data, parent_window));
218 /** @} */
220 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
221 /* Private Functions */
222 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
224 * @name Private Functions
225 * @{
228 * Draw preview with current image.
230 static gboolean
231 _draw_preview_image(IrrecoBackgroundCreatorDlg *self, const gchar *image)
233 GError *error = NULL;
234 GdkPixbuf *pixbuf = NULL;
235 GdkPixbuf *pixbuf_size = NULL;
236 GString *size = g_string_new(NULL);
237 gint widht;
238 gint height;
239 IRRECO_ENTER
240 g_assert(self != NULL);
242 /* Attempt to load the image. */
243 if (image != NULL) {
244 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
245 IRRECO_BACKGROUND_PREVIEW_WIDHT,
246 IRRECO_BACKGROUND_PREVIEW_HEIGHT,
247 TRUE, &error);
248 /* set sensitive if image are selected */
249 gtk_widget_set_sensitive(self->add_button, TRUE);
251 if (irreco_gerror_check_print(&error)) {
252 IRRECO_RETURN_BOOL(FALSE);
255 gtk_widget_realize(GTK_WIDGET(self->preview_image));
257 /* Show image real size */
258 pixbuf_size = gdk_pixbuf_new_from_file(image, &error);
260 widht = gdk_pixbuf_get_width(pixbuf_size);
261 height = gdk_pixbuf_get_height(pixbuf_size);
262 g_string_printf(size, "%sx%s", g_strdup_printf("%d", widht),
263 g_strdup_printf("%d", height));
266 gtk_label_set_text(GTK_LABEL(self->label_size), size->str);
267 if (pixbuf_size != NULL) g_object_unref(G_OBJECT(pixbuf_size));
270 gtk_image_set_from_pixbuf(GTK_IMAGE(self->preview_image),
271 GDK_PIXBUF(pixbuf));
272 /* Set image path */
273 g_string_printf(self->filename, "%s", image);
275 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
276 g_string_free(size, TRUE);
279 IRRECO_RETURN_BOOL(TRUE);
282 static void _select_image(GtkButton *button, IrrecoBackgroundCreatorDlg *self)
285 GtkWidget *file_dlg = NULL;
286 gchar *image_dir = NULL;
287 IRRECO_ENTER
289 /* Create image select dialog. */
291 file_dlg = hildon_file_chooser_dialog_new(GTK_WINDOW(self),
292 GTK_FILE_CHOOSER_ACTION_OPEN);
293 gtk_window_set_title(GTK_WINDOW(file_dlg),_("Select background image"));
294 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(file_dlg), TRUE);
295 image_dir = g_build_path("/", getenv("HOME"), "MyDocs/.images/", NULL);
296 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dlg),
297 image_dir);
298 g_free(image_dir);
300 /* Loop until user cancels or we get a valid image. */
301 gtk_widget_show_all(GTK_WIDGET(file_dlg));
302 while (gtk_dialog_run(GTK_DIALOG(file_dlg)) == GTK_RESPONSE_OK) {
303 gchar *filename = gtk_file_chooser_get_filename(
304 GTK_FILE_CHOOSER(file_dlg));
306 /* Attempt to display the image. */
307 if (_draw_preview_image(self, filename)) {
308 irreco_gstring_set_and_free(self->filename, filename);
309 filename = NULL;
310 break;
312 /* Cleanup */
313 } else {
314 gchar *basename = g_path_get_basename(filename);
315 irreco_error_dlg_printf(GTK_WINDOW(file_dlg),
316 _("Cannot open image \"%s\""),
317 basename);
318 IRRECO_PRINTF("Image invalid.\n");
320 g_free(basename);
321 g_free(filename);
325 gtk_widget_destroy(file_dlg);
326 IRRECO_RETURN
328 gboolean
329 irreco_background_creator_dlg_check_details(IrrecoBackgroundCreatorDlg *self)
331 gboolean rvalue = TRUE;
332 IRRECO_ENTER
334 /*check that it is not the same name button*/
335 if (irreco_string_table_exists (self->theme->backgrounds,
336 gtk_entry_get_text(GTK_ENTRY(self->entry_name)))) {
337 irreco_error_dlg(GTK_WINDOW(self),
338 "Background has already existed");
339 rvalue = FALSE;
341 IRRECO_RETURN_BOOL(rvalue);
343 /** @} */
345 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
346 /* Functions */
347 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
350 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
351 /* Public Functions */
352 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
353 gboolean irreco_background_creator_dlg_run(IrrecoData *irreco_data,
354 IrrecoTheme *irreco_theme,
355 GtkWindow *parent_window,
356 IrrecoThemeBg *bg)
358 IrrecoBackgroundCreatorDlg *self;
359 gint response;
360 gboolean loop = TRUE;
361 gboolean rvalue = FALSE;
362 gboolean edit = FALSE;
363 IRRECO_ENTER
365 self = (IrrecoBackgroundCreatorDlg*)irreco_background_creator_dlg_create(
366 irreco_data, parent_window);
367 self->irreco_data = irreco_data;
368 self->theme = irreco_theme;
369 IRRECO_DEBUG("Pointer: %p \n", (void*) self->theme);
370 IRRECO_DEBUG("Pointer: %p \n", (void*) irreco_theme);
372 irreco_theme_bg_print(bg);
374 if (g_utf8_strlen(bg->image_name->str, 1) >0) {
376 /* Sets the bg details */
377 _set_bg_details(self, irreco_theme, bg);
378 edit = TRUE;
381 do {
382 response = gtk_dialog_run(GTK_DIALOG(self));
383 switch (response) {
384 case GTK_RESPONSE_OK:
386 if (edit) {
387 irreco_theme_bg_set(bg,
388 gtk_entry_get_text(GTK_ENTRY(
389 self->entry_name)),
390 self->filename->str);
391 irreco_theme_bg_print(bg);
392 loop = FALSE;
393 rvalue = TRUE;
395 } else {
396 /* Check button name */
397 if (irreco_background_creator_dlg_check_details(
398 self)) {
399 irreco_theme_bg_set(bg,
400 gtk_entry_get_text(
401 GTK_ENTRY(
402 self->entry_name)),
403 self->filename->str);
404 irreco_theme_bg_print(bg);
405 loop = FALSE;
406 rvalue = TRUE;
407 } else {
408 rvalue = FALSE;
409 loop = TRUE;
413 break;
414 case GTK_RESPONSE_CANCEL:
415 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
416 rvalue = FALSE;
417 loop = FALSE;
418 break;
420 default:
421 IRRECO_DEBUG("default\n");
422 break;
425 } while (loop);
427 gtk_widget_destroy(GTK_WIDGET(self));
429 IRRECO_RETURN_BOOL(rvalue);
432 void
433 _set_bg_details(IrrecoBackgroundCreatorDlg *self, IrrecoTheme *irreco_theme,
434 IrrecoThemeBg *bg)
436 IRRECO_ENTER
438 gtk_entry_set_text(GTK_ENTRY(self->entry_name),
439 bg->image_name->str);
441 _draw_preview_image(self, bg->image_path->str);
443 /* Set button label */
444 gtk_button_set_label(GTK_BUTTON(self->add_button), "Save");
445 gtk_window_set_title(GTK_WINDOW(self), _("Edit Background"));
446 gtk_widget_set_sensitive(self->entry_name, FALSE);
448 IRRECO_RETURN
451 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
452 /* Events and Callbacks */
453 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
456 * @name Events and Callbacks
457 * @{
460 static void
461 _signal_image_clicked(GtkButton *button, IrrecoBackgroundCreatorDlg *self)
463 IRRECO_ENTER
464 _select_image(button, self);
465 IRRECO_RETURN
469 /** @} */