Irreco for N900 (Maemo 5) update. Push for 0.8.* changes.
[irreco.git] / irreco / src / core / irreco_button_browser_dlg.c
blobea302a7aee0e9a5488a30f9735f7d1e012bbe6ef
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
4 * 2008 Joni Kokko (t5kojo01@students.oamk.fi)
5 * 2008 Harri Vattulainen (t5vaha01@students.oamk.fi)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program 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 this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "irreco_button_browser_dlg.h"
23 #include <hildon/hildon-banner.h>
27 /**
28 * @addtogroup IrrecoButtonBrowserDlg
29 * @ingroup Irreco
31 * Allow user to select one button from one theme.
33 * @{
36 /**
37 * @file
38 * Source file of @ref IrrecoButtonBrowserDlg.
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
44 /* Datatypes */
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
47 #define IRRECO_BUTTON_BROWSER_PREVIEW_WIDHT (IRRECO_SCREEN_WIDTH / 6)
48 #define IRRECO_BUTTON_BROWSER_PREVIEW_HEIGHT (IRRECO_SCREEN_HEIGHT / 6)
52 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
53 /* Prototypes */
54 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
56 static void
57 _irreco_button_browser_dlg_set_banner(IrrecoButtonBrowserDlg *self,
58 const gchar *text,
59 gdouble fraction);
60 static void
61 _irreco_button_browser_dlg_hide_banner(IrrecoButtonBrowserDlg *self);
62 static gboolean _irreco_button_browser_dlg_loader_images(
63 IrrecoButtonBrowserDlg *self);
64 static void irreco_button_browser_dlg_image_selection_changed(
65 GtkTreeSelection * selection,
66 IrrecoButtonBrowserDlg *self);
67 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
68 /* Construction & Destruction */
69 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
71 /**
72 * @name Construction & Destruction
73 * @{
76 G_DEFINE_TYPE(IrrecoButtonBrowserDlg, irreco_button_browser_dlg,
77 IRRECO_TYPE_INTERNAL_DLG)
79 static void irreco_button_browser_dlg_constructed(GObject *object)
81 IrrecoButtonBrowserDlg *self;
83 IRRECO_ENTER
85 G_OBJECT_CLASS(irreco_button_browser_dlg_parent_class)->constructed(
86 object);
87 self = IRRECO_BUTTON_BROWSER_DLG(object);
89 self->current_image = NULL;
91 /* Construct dialog. */
92 gtk_window_set_title(GTK_WINDOW(self), _("Select preview button"));
93 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
94 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
95 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
97 /* Create list for images */
98 self->images = IRRECO_LISTBOX_IMAGE(
99 irreco_listbox_image_new_with_autosize(200, 450, 180, 180));
100 irreco_listbox_set_select_new_rows(IRRECO_LISTBOX(self->images), FALSE);
101 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(self)->vbox),
102 GTK_WIDGET(self->images));
104 g_signal_connect(G_OBJECT(IRRECO_LISTBOX(self->images)->tree_selection),
105 "changed",
106 G_CALLBACK(irreco_button_browser_dlg_image_selection_changed),
107 self);
109 gtk_widget_show_all(GTK_WIDGET(self));
111 IRRECO_RETURN
114 static void irreco_button_browser_dlg_init(IrrecoButtonBrowserDlg *self)
116 IRRECO_ENTER
117 IRRECO_RETURN
120 static void irreco_button_browser_dlg_finalize(GObject *object)
122 IrrecoButtonBrowserDlg *self;
124 IRRECO_ENTER
126 self = IRRECO_BUTTON_BROWSER_DLG(object);
127 G_OBJECT_CLASS(irreco_button_browser_dlg_parent_class)->finalize(object);
129 IRRECO_RETURN
132 static void
133 irreco_button_browser_dlg_class_init(IrrecoButtonBrowserDlgClass *klass)
135 GObjectClass* object_class = G_OBJECT_CLASS (klass);
136 object_class->finalize = irreco_button_browser_dlg_finalize;
137 object_class->constructed = irreco_button_browser_dlg_constructed;
140 GtkWidget *irreco_button_browser_dlg_new(IrrecoData *irreco_data,
141 GtkWindow *parent_window)
143 IrrecoButtonBrowserDlg *self;
145 IRRECO_ENTER
147 self = g_object_new(IRRECO_TYPE_BUTTON_BROWSER_DLG,
148 "irreco-data", irreco_data,
149 NULL);
150 irreco_dlg_set_parent(IRRECO_DLG(self), parent_window);
152 IRRECO_RETURN_PTR(self);
155 /** @} */
159 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
160 /* Private Functions */
161 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
163 * @name Private Functions
164 * @{
168 * Show hildon progressbar banner.
170 * This function will create a new banner if one has not been created yet,
171 * if banner already exists, it's properties will be changed.
173 * @param text Text to show.
174 * @param fraction Value of progress.
176 static void
177 _irreco_button_browser_dlg_set_banner(IrrecoButtonBrowserDlg *self,
178 const gchar *text,
179 gdouble fraction)
181 IRRECO_ENTER
183 if (self->banner == NULL) {
184 self->banner = hildon_banner_show_progress(
185 GTK_WIDGET(self), NULL, "");
188 hildon_banner_set_text(HILDON_BANNER(self->banner), text);
189 hildon_banner_set_fraction(HILDON_BANNER(self->banner), fraction);
191 IRRECO_RETURN
195 * Destroy banner, if it exists.
197 static void
198 _irreco_button_browser_dlg_hide_banner(IrrecoButtonBrowserDlg *self)
200 IRRECO_ENTER
202 if (self->banner) {
203 gtk_widget_destroy(self->banner);
204 self->banner = NULL;
207 IRRECO_RETURN
211 * Background-image loader.
213 * This loader will create a list of background-images from the
214 * given theme and update the TreeView accordingly.
216 static gboolean _irreco_button_browser_dlg_loader_images(
217 IrrecoButtonBrowserDlg *self)
219 gint theme_button_count;
220 gfloat banner;
221 gboolean rvalue;
223 IRRECO_ENTER
225 theme_button_count = irreco_string_table_lenght(self->theme->buttons);
227 if (theme_button_count > 0) {
228 if(self->loader_index == 0) {
229 _irreco_button_browser_dlg_set_banner(self,
230 _("Loading ..."),
233 rvalue = TRUE;
234 } else {
235 rvalue = FALSE;
238 if(self->loader_index < theme_button_count){
239 IrrecoThemeButton *button_image;
240 const gchar *image_name;
241 /* get button */
242 irreco_string_table_index(self->theme->buttons,
243 self->loader_index,
244 &image_name,
245 (gpointer *) &button_image);
246 /* add button to list */
247 irreco_listbox_image_append(self->images,
248 button_image->name->str,
249 button_image,
250 button_image->image_up->str);
251 /* get and set banner fraction */
252 banner = (gfloat) self->loader_index /
253 (gfloat) theme_button_count;
254 _irreco_button_browser_dlg_set_banner(self,
255 _("Loading ..."),
256 banner);
257 /* get next button, next time */
258 self->loader_index++;
261 if(self->loader_index == theme_button_count) {
262 _irreco_button_browser_dlg_hide_banner(self);
263 rvalue = FALSE;
266 IRRECO_RETURN_BOOL(rvalue);
269 /** @} */
273 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
274 /* Public Functions */
275 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
278 * Show dialog, and ask user for input.
281 IrrecoThemeButton *irreco_button_browser_dlg_run(GtkWindow *parent_window,
282 IrrecoData *irreco_data,
283 IrrecoTheme * irreco_theme)
285 IrrecoButtonBrowserDlg *self;
286 gint response;
287 gboolean loop = TRUE;
288 IrrecoThemeButton *rvalue = NULL;
290 IRRECO_ENTER
292 self = (IrrecoButtonBrowserDlg *)irreco_button_browser_dlg_new(
293 irreco_data,
294 parent_window);
296 self->theme = irreco_theme;
298 /* init and start loading buttons */
299 self->loader_index = 0;
300 irreco_listbox_clear(IRRECO_LISTBOX(self->images));
301 g_idle_add((GSourceFunc)_irreco_button_browser_dlg_loader_images, self);
303 do {
304 response = gtk_dialog_run(GTK_DIALOG(self));
306 switch (response) {
307 case GTK_RESPONSE_DELETE_EVENT:
308 IRRECO_DEBUG("GTK_RESPONSE_DELETE_EVENT\n");
309 rvalue = NULL;
310 loop = FALSE;
311 break;
313 case GTK_RESPONSE_ACCEPT:
314 IRRECO_DEBUG("GTK_RESPONSE_ACCEPT\n");
315 /*rvalue = g_strdup(self->current_image->name->str);*/
316 rvalue = self->current_image;
317 loop = FALSE;
318 break;
320 default:
321 IRRECO_DEBUG("Something went horribly wrong\n");
322 break;
324 } while (loop);
326 gtk_widget_destroy(GTK_WIDGET(self));
328 IRRECO_RETURN_PTR(rvalue);
331 /** @} */
335 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
336 /* Events and Callbacks */
337 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
340 * @name Events and Callbacks
341 * @{
345 * Respond to image selection change.
347 * This function will store selected button to self->current_image
349 static void irreco_button_browser_dlg_image_selection_changed(
350 GtkTreeSelection *selection,
351 IrrecoButtonBrowserDlg *self)
353 gint sel_index = -1;
354 gchar *sel_label = NULL;
355 IrrecoThemeButton *button = NULL;
357 IRRECO_ENTER
359 if (irreco_listbox_get_selection(IRRECO_LISTBOX(self->images),
360 &sel_index, &sel_label, (gpointer *)&button)) {
361 self->current_image = button;
362 } else {
363 self->current_image = NULL;
366 g_free(sel_label);
367 gtk_dialog_response(GTK_DIALOG(self), GTK_RESPONSE_ACCEPT);
369 IRRECO_RETURN
372 /** @} */
376 /** @} */