equal to the label text of the left-side
[irreco.git] / irreco / src / core / irreco_background_creator_dlg.c
blob4eebbed45a6e5a48f84d6bbc7d85bd25f355ccc4
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 IrrecoData *irreco_data;
85 IrrecoBackgroundCreatorDlg *self;
87 GtkWidget *table;
88 GtkWidget *frame;
89 GtkWidget *label_name;
90 GtkWidget *label_select;
91 GtkWidget *button_select;
92 GtkWidget *vbox_preview;
94 IRRECO_ENTER
96 G_OBJECT_CLASS(
97 irreco_background_creator_dlg_parent_class)->constructed(object);
99 self = IRRECO_BACKGROUND_CREATOR_DLG(object);
101 irreco_data = irreco_internal_dlg_get_irreco_data(IRRECO_INTERNAL_DLG(
102 self));
104 /* Construct dialog. */
105 gtk_window_set_title(GTK_WINDOW(self), _("Background Creator "));
106 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
107 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
108 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
110 /*Buttons*/
111 self->cancel_button = gtk_dialog_add_button(GTK_DIALOG(self),
112 _("Cancel"),
113 GTK_RESPONSE_CANCEL);
114 self->add_button = gtk_dialog_add_button(GTK_DIALOG(self),
115 _("Add"), GTK_RESPONSE_OK);
116 gtk_widget_set_sensitive(self->add_button, FALSE);
118 /* Create widgets. */
120 table = gtk_table_new(8, 9, TRUE);
121 label_name = gtk_label_new(_("Name:"));
122 label_select = gtk_label_new(_("Background:"));
123 self->entry_name = gtk_entry_new();
124 frame = gtk_frame_new(NULL);
125 vbox_preview = gtk_vbox_new(FALSE, 8);
126 self->label_size = gtk_label_new("");
127 self->preview_image = gtk_image_new();
128 button_select = gtk_button_new_with_label(_("Select"));
130 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self)->vbox),
131 table);
133 /* equal to the text of the left-side */
134 gtk_misc_set_alignment(GTK_MISC(label_name), 0, 0.5);
135 gtk_misc_set_alignment(GTK_MISC(label_select), 0, 0.5);
137 /* Set widgets on the table */
139 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
140 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
142 gtk_table_attach_defaults(GTK_TABLE(table), label_name, 0, 3, 0, 1);
143 gtk_table_attach_defaults(GTK_TABLE(table), label_select, 0, 3, 1, 2);
144 gtk_table_attach_defaults(GTK_TABLE(table),
145 self->entry_name, 3, 9, 0, 1);
146 gtk_table_attach_defaults(GTK_TABLE(table), button_select, 3, 9, 1, 2);
147 gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 9, 2, 8);
149 gtk_container_add(GTK_CONTAINER(frame), vbox_preview);
150 gtk_container_add(GTK_CONTAINER(vbox_preview), self->preview_image);
151 gtk_container_add(GTK_CONTAINER(vbox_preview), self->label_size);
154 /* Button signals. */
155 g_signal_connect(G_OBJECT(button_select), "clicked",
156 G_CALLBACK(_signal_image_clicked),
157 self);
159 gtk_widget_show_all(GTK_WIDGET(self));
161 IRRECO_RETURN
163 static void
164 irreco_background_creator_dlg_init (IrrecoBackgroundCreatorDlg *self)
166 IRRECO_ENTER
167 self->filename = g_string_new(NULL);
168 IRRECO_RETURN
171 static void
172 irreco_background_creator_dlg_finalize(GObject *object)
174 /* TODO: Add deinitalization code here */
175 IrrecoBackgroundCreatorDlg *self;
176 IRRECO_ENTER
178 self = IRRECO_BACKGROUND_CREATOR_DLG(object);
179 g_string_free(self->filename, TRUE);
180 self->filename = NULL;
182 G_OBJECT_CLASS(irreco_background_creator_dlg_parent_class)->finalize(object);
183 IRRECO_RETURN
187 static void
188 irreco_background_creator_dlg_class_init(IrrecoBackgroundCreatorDlgClass *klass)
190 GObjectClass* object_class = G_OBJECT_CLASS (klass);
191 object_class->finalize = irreco_background_creator_dlg_finalize;
192 object_class->constructed = irreco_background_creator_dlg_constructed;
194 GtkWidget
195 *irreco_background_creator_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
197 IrrecoBackgroundCreatorDlg *self;
199 IRRECO_ENTER
201 self = g_object_new(IRRECO_TYPE_BACKGROUND_CREATOR_DLG,
202 "irreco-data", irreco_data, NULL);
203 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
205 IRRECO_RETURN_PTR(self);
209 * @deprecated
210 * @todo Replace calls to irreco_background_creator_dlg_create() with
211 * irreco_background_creator_dlg_new().
213 IrrecoBackgroundCreatorDlg
214 *irreco_background_creator_dlg_create(IrrecoData *irreco_data,
215 GtkWindow *parent_window)
217 IRRECO_ENTER
218 IRRECO_RETURN_PTR(irreco_background_creator_dlg_new(
219 irreco_data, parent_window));
222 /** @} */
224 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
225 /* Private Functions */
226 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
228 * @name Private Functions
229 * @{
232 * Draw preview with current image.
234 static gboolean
235 _draw_preview_image(IrrecoBackgroundCreatorDlg *self, const gchar *image)
237 GError *error = NULL;
238 GdkPixbuf *pixbuf = NULL;
239 GdkPixbuf *pixbuf_size = NULL;
240 GString *size = g_string_new(NULL);
241 gint widht;
242 gint height;
243 IRRECO_ENTER
244 g_assert(self != NULL);
246 /* Attempt to load the image. */
247 if (image != NULL) {
248 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
249 IRRECO_BACKGROUND_PREVIEW_WIDHT,
250 IRRECO_BACKGROUND_PREVIEW_HEIGHT,
251 TRUE, &error);
252 /* set sensitive if image are selected */
253 gtk_widget_set_sensitive(self->add_button, TRUE);
255 if (irreco_gerror_check_print(&error)) {
256 IRRECO_RETURN_BOOL(FALSE);
259 gtk_widget_realize(GTK_WIDGET(self->preview_image));
261 /* Show image real size */
262 pixbuf_size = gdk_pixbuf_new_from_file(image, &error);
264 widht = gdk_pixbuf_get_width(pixbuf_size);
265 height = gdk_pixbuf_get_height(pixbuf_size);
266 g_string_printf(size, "%sx%s", g_strdup_printf("%d", widht),
267 g_strdup_printf("%d", height));
270 gtk_label_set_text(GTK_LABEL(self->label_size), size->str);
271 if (pixbuf_size != NULL) g_object_unref(G_OBJECT(pixbuf_size));
274 gtk_image_set_from_pixbuf(GTK_IMAGE(self->preview_image),
275 GDK_PIXBUF(pixbuf));
276 /* Set image path */
277 g_string_printf(self->filename, "%s", image);
279 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
280 g_string_free(size, TRUE);
283 IRRECO_RETURN_BOOL(TRUE);
287 static void _select_image(GtkButton *button, IrrecoBackgroundCreatorDlg *self)
290 GtkWidget *file_dlg = NULL;
291 gchar *image_dir = NULL;
292 IRRECO_ENTER
294 /* Create image select dialog. */
296 file_dlg = hildon_file_chooser_dialog_new(GTK_WINDOW(self),
297 GTK_FILE_CHOOSER_ACTION_OPEN);
298 gtk_window_set_title(GTK_WINDOW(file_dlg),_("Select background image"));
299 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(file_dlg), TRUE);
300 image_dir = g_build_path("/", getenv("HOME"), "MyDocs/.images/", NULL);
301 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dlg),
302 image_dir);
303 g_free(image_dir);
305 /* Loop until user cancels or we get a valid image. */
306 gtk_widget_show_all(GTK_WIDGET(file_dlg));
307 while (gtk_dialog_run(GTK_DIALOG(file_dlg)) == GTK_RESPONSE_OK) {
308 gchar *filename = gtk_file_chooser_get_filename(
309 GTK_FILE_CHOOSER(file_dlg));
311 /* Attempt to display the image. */
312 if (_draw_preview_image(self, filename)) {
313 irreco_gstring_set_and_free(self->filename, filename);
314 filename = NULL;
315 break;
317 /* Cleanup */
318 } else {
319 gchar *basename = g_path_get_basename(filename);
320 irreco_error_dlg_printf(GTK_WINDOW(file_dlg),
321 _("Cannot open image \"%s\""),
322 basename);
323 IRRECO_PRINTF("Image invalid.\n");
325 g_free(basename);
326 g_free(filename);
330 gtk_widget_destroy(file_dlg);
331 IRRECO_RETURN
334 /** @} */
336 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
337 /* Functions */
338 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
341 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
342 /* Public Functions */
343 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
344 gboolean irreco_background_creator_dlg_run(IrrecoData *irreco_data,
345 IrrecoTheme *irreco_theme,
346 GtkWindow *parent_window,
347 IrrecoThemeBg *bg)
349 IrrecoBackgroundCreatorDlg *self;
350 gint response;
351 gboolean loop = TRUE;
352 gboolean rvalue = FALSE;
354 IRRECO_ENTER
356 self = (IrrecoBackgroundCreatorDlg*)irreco_background_creator_dlg_create(
357 irreco_data, parent_window);
358 self->irreco_data = irreco_data;
359 self->theme = irreco_theme;
360 IRRECO_DEBUG("Pointer: %p \n", (void*) self->theme);
361 IRRECO_DEBUG("Pointer: %p \n", (void*) irreco_theme);
363 irreco_theme_bg_print(bg);
365 if (g_utf8_strlen(bg->image_name->str, 1) >0) {
367 /* Sets the bg details */
368 _set_bg_details(self, irreco_theme, bg);
371 do {
372 response = gtk_dialog_run(GTK_DIALOG(self));
373 switch (response) {
374 case GTK_RESPONSE_OK:
376 irreco_theme_bg_set(bg,
377 gtk_entry_get_text(GTK_ENTRY(
378 self->entry_name)),
379 self->filename->str);
380 irreco_theme_bg_print(bg);
381 loop = FALSE;
382 rvalue = TRUE;
384 break;
385 case GTK_RESPONSE_CANCEL:
386 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
387 rvalue = FALSE;
388 loop = FALSE;
389 break;
391 default:
392 IRRECO_DEBUG("default\n");
393 break;
396 } while (loop);
398 gtk_widget_destroy(GTK_WIDGET(self));
400 IRRECO_RETURN_BOOL(rvalue);
403 void
404 _set_bg_details(IrrecoBackgroundCreatorDlg *self, IrrecoTheme *irreco_theme,
405 IrrecoThemeBg *bg)
407 IRRECO_ENTER
409 gtk_entry_set_text(GTK_ENTRY(self->entry_name),
410 bg->image_name->str);
412 _draw_preview_image(self, bg->image_path->str);
414 /* Set button label */
415 gtk_button_set_label(GTK_BUTTON(self->add_button), "Save");
416 gtk_window_set_title(GTK_WINDOW(self), _("Background Editor "));
418 IRRECO_RETURN
421 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
422 /* Events and Callbacks */
423 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
426 * @name Events and Callbacks
427 * @{
430 static void
431 _signal_image_clicked(GtkButton *button, IrrecoBackgroundCreatorDlg *self)
433 IRRECO_ENTER
434 _select_image(button, self);
435 IRRECO_RETURN
439 /** @} */