2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi),
4 * Harri Vattulainen (t5vaha01@students.oamk.fi)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "irreco_button_browser_widget.h"
22 #include <hildon/hildon-banner.h>
25 (IrrecoButtonBrowserWidget
, irreco_button_browser_widget
, GTK_TYPE_VBOX
)
28 * @addtogroup IrrecoButtonBrowserWidget
31 * This widget helps selection of button.
33 * This widget allows button selection from themes
39 * Source file of @ref IrrecoButtonBrowserWidget.
42 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 static gboolean
irreco_button_browser_widget_populate_themes(
47 IrrecoButtonBrowserWidget
*self
);
49 static gboolean
irreco_button_browser_widget_loader_images(
50 IrrecoButtonBrowserWidget
*self
);
53 irreco_button_browser_widget_loader_start(IrrecoButtonBrowserWidget
*self
,
55 GtkTreeIter
*parent_iter
);
58 irreco_button_browser_widget_destroy_event(IrrecoButtonBrowserWidget
*self
,
61 static void irreco_button_browser_widget_theme_selection_changed(
62 GtkTreeSelection
* selection
,
63 IrrecoButtonBrowserWidget
*self
);
65 static void irreco_button_browser_widget_image_selection_changed(
66 GtkTreeSelection
* selection
,
67 IrrecoButtonBrowserWidget
*self
);
69 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
71 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
82 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
83 /* Construction & Destruction */
84 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
87 * @name Construction & Destruction
91 static void irreco_button_browser_widget_finalize (GObject
*object
)
95 if (G_OBJECT_CLASS(irreco_button_browser_widget_parent_class
)->finalize
)
96 G_OBJECT_CLASS(irreco_button_browser_widget_parent_class
)->finalize (object
);
101 static void irreco_button_browser_widget_class_init(IrrecoButtonBrowserWidgetClass
*klass
)
103 GObjectClass
*object_class
;
106 object_class
= G_OBJECT_CLASS (klass
);
107 object_class
->finalize
= irreco_button_browser_widget_finalize
;
112 static void irreco_button_browser_widget_init (IrrecoButtonBrowserWidget
*self
)
114 GtkWidget
*frame_for_themes
;
115 GtkWidget
*frame_for_images
;
118 self
->current_theme
= g_string_new("");
119 self
->current_image
= NULL
;
123 /* Create Frame for Themes */
124 frame_for_themes
= gtk_frame_new("");
125 gtk_frame_set_label_widget(GTK_FRAME(frame_for_themes
),
126 irreco_gtk_label_bold("Themes", 0, 0, 0, 0, 0, 0));
128 /* Create list of themes */
129 self
->themes
= IRRECO_LISTBOX_TEXT(
130 irreco_listbox_text_new_with_autosize(200, 200,
132 gtk_container_add(GTK_CONTAINER(frame_for_themes
),
133 GTK_WIDGET(self
->themes
));
137 /* Create frame for Images */
138 frame_for_images
= gtk_frame_new("");
139 gtk_frame_set_label_widget(GTK_FRAME(frame_for_images
),
140 irreco_gtk_label_bold("Images", 0, 0, 0, 0, 0, 0));
142 /* Create list of images */
143 self
->images
= IRRECO_LISTBOX_IMAGE(
144 irreco_listbox_image_new_with_autosize(400, 400,
146 irreco_listbox_set_select_new_rows(IRRECO_LISTBOX(self
->images
), FALSE
);
147 gtk_container_add(GTK_CONTAINER(frame_for_images
),
148 GTK_WIDGET(self
->images
));
150 /* Signal handlers. */
151 g_signal_connect(G_OBJECT(self
), "destroy",
152 G_CALLBACK(irreco_button_browser_widget_destroy_event
), NULL
);
154 g_signal_connect(G_OBJECT(IRRECO_LISTBOX(self
->themes
)->tree_selection
),
156 G_CALLBACK(irreco_button_browser_widget_theme_selection_changed
),
159 g_signal_connect(G_OBJECT(IRRECO_LISTBOX(self
->images
)->tree_selection
),
161 G_CALLBACK(irreco_button_browser_widget_image_selection_changed
),
165 self
->hbox
= g_object_new(GTK_TYPE_HBOX
, NULL
);
166 gtk_box_set_spacing(GTK_BOX(self
->hbox
), 8);
168 gtk_box_pack_start(GTK_BOX(self
->hbox
), GTK_WIDGET(frame_for_themes
),
171 gtk_box_pack_start(GTK_BOX(self
->hbox
), GTK_WIDGET(frame_for_images
),
174 gtk_box_pack_start(GTK_BOX(self
), GTK_WIDGET(self
->hbox
),
180 IrrecoButtonBrowserWidget
181 *irreco_button_browser_widget_new(IrrecoData
*irreco_data
)
183 IrrecoButtonBrowserWidget
*self
;
184 IrrecoStringTable
*themes
= irreco_data
->theme_manager
->themes
;
187 self
= IRRECO_BUTTON_BROWSER_WIDGET(
188 g_object_new (IRRECO_TYPE_BUTTON_BROWSER_WIDGET
, NULL
));
190 self
->irreco_data
= irreco_data
;
192 if (irreco_string_table_lenght(themes
) > 0) {
194 const gchar
*theme_name
;
195 irreco_button_browser_widget_populate_themes(self
);
196 irreco_listbox_set_selection(IRRECO_LISTBOX(self
->themes
), 0);
198 irreco_string_table_index(themes
, 0, &theme_name
, NULL
);
200 g_string_printf(self
->current_theme
, "%s", theme_name
);
202 irreco_button_browser_widget_loader_start(self
,
203 G_SOURCEFUNC(irreco_button_browser_widget_loader_images
), NULL
);
206 IRRECO_RETURN_PTR(GTK_WIDGET(self
));
211 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
212 /* Private Functions */
213 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
215 * @name Private Functions
220 * Show hildon progressbar banner.
222 * This function will create a new banner if one has not been created yet,
223 * if banner already exists, it's properties will be changed.
225 * @param text Text to show.
226 * @param fraction Value of progress.
229 irreco_button_browser_widget_set_banner(IrrecoButtonBrowserWidget
*self
,
234 if (self
->banner
== NULL
) {
235 self
->banner
= hildon_banner_show_progress(
236 GTK_WIDGET(self
), NULL
, "");
239 hildon_banner_set_text(HILDON_BANNER(self
->banner
), text
);
240 hildon_banner_set_fraction(HILDON_BANNER(self
->banner
), fraction
);
245 * Destroy banner, if it exists.
248 irreco_button_browser_widget_hide_banner(IrrecoButtonBrowserWidget
*self
)
252 gtk_widget_destroy(self
->banner
);
259 * Start a loader state machine if one is not running already.
262 irreco_button_browser_widget_loader_start(IrrecoButtonBrowserWidget
*self
,
263 GSourceFunc function
,
264 GtkTreeIter
*parent_iter
)
268 if (self
->loader_func_id
== 0) {
270 self
->loader_func_id
= g_idle_add(function
, self
);
272 IRRECO_ERROR("Loader function pointer not given.\n");
280 * Stop and cleanup loader if a loader is running.
283 irreco_button_browser_widget_loader_stop(IrrecoButtonBrowserWidget
*self
)
286 if (self
->loader_func_id
!= 0) {
287 g_source_remove(self
->loader_func_id
);
288 self
->loader_func_id
= 0;
289 self
->loader_state
= 0;
297 * This loader will request a list of themes from the IrrecoThemeManager and
298 * update the TreeView accordingly.
301 static gboolean
irreco_button_browser_widget_populate_themes(
302 IrrecoButtonBrowserWidget
*self
)
304 IrrecoStringTable
*themes
= NULL
;
305 IrrecoThemeManager
*theme_manager
= self
->irreco_data
->theme_manager
;
308 themes
= irreco_theme_manager_get_themes(theme_manager
);
310 if (themes
!= NULL
) {
311 IRRECO_STRING_TABLE_FOREACH_KEY(themes
, key
)
312 irreco_listbox_text_append(self
->themes
, key
, NULL
);
313 IRRECO_STRING_TABLE_FOREACH_END
315 irreco_error_dlg(GTK_WINDOW(self
),
316 "There's no themes installed");
319 IRRECO_RETURN_BOOL(TRUE
);
323 * Background-image loader.
325 * This loader will request a list of background-images from the
326 * IrrecoThemeManager and update the TreeView accordingly.
328 static gboolean
irreco_button_browser_widget_loader_images(
329 IrrecoButtonBrowserWidget
*self
)
333 switch (self
->loader_state
) {
334 case LOADER_STATE_INIT
:
335 irreco_button_browser_widget_set_banner(self
, _("Loading ..."), 0);
336 self
->loader_state
= LOADER_STATE_LOOP
;
337 self
->loader_index
= 0;
338 irreco_listbox_clear(IRRECO_LISTBOX(self
->images
));
339 IRRECO_RETURN_BOOL(TRUE
);
341 case LOADER_STATE_LOOP
: {
342 gint theme_button_count
;
344 const gchar
*image_name
;
345 IrrecoThemeManager
*manager
= self
->irreco_data
->theme_manager
;
348 irreco_string_table_get(manager
->themes
,
349 self
->current_theme
->str
,
350 (gpointer
*) &theme
);
352 theme_button_count
= irreco_string_table_lenght(theme
->buttons
);
354 if (theme_button_count
> 0) {
355 IrrecoThemeButton
*button_image
;
356 irreco_string_table_index(theme
->buttons
,
359 (gpointer
*) &button_image
);
361 irreco_listbox_image_append(self
->images
,
362 button_image
->name
->str
,
364 button_image
->image_up
->str
);
366 self
->current_image
= NULL
;
367 theme_button_count
= 1;
370 self
->loader_index
++;
371 banner
= (gfloat
)self
->loader_index
/ (gfloat
)theme_button_count
;
372 irreco_button_browser_widget_set_banner(self
, _("Loading ..."),
375 if(self
->loader_index
>= theme_button_count
) {
376 self
->loader_state
= LOADER_STATE_END
;
379 IRRECO_RETURN_BOOL(TRUE
);
382 case LOADER_STATE_END
:
383 irreco_button_browser_widget_hide_banner(self
);
384 irreco_button_browser_widget_loader_stop(self
);
387 IRRECO_RETURN_BOOL(FALSE
);
392 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
393 /* Public Functions */
394 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
397 * @name Public Functions
401 /* Add functions here. */
405 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
406 /* Events and Callbacks */
407 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
410 * @name Events and Callbacks
415 irreco_button_browser_widget_destroy_event(IrrecoButtonBrowserWidget
*self
,
419 irreco_button_browser_widget_loader_stop(self
);
423 static void irreco_button_browser_widget_theme_selection_changed(
424 GtkTreeSelection
*selection
,
425 IrrecoButtonBrowserWidget
*self
)
428 gchar
*sel_label
= NULL
;
429 gpointer sel_user_data
= NULL
;
432 if (self
->loader_func_id
!= 0) {
433 irreco_button_browser_widget_loader_stop(self
);
436 irreco_listbox_get_selection(IRRECO_LISTBOX(self
->themes
),
437 &sel_index
, &sel_label
, &sel_user_data
);
439 g_string_printf(self
->current_theme
, "%s", sel_label
);
441 irreco_button_browser_widget_loader_start(self
,
442 G_SOURCEFUNC(irreco_button_browser_widget_loader_images
), NULL
);
448 static void irreco_button_browser_widget_image_selection_changed(
449 GtkTreeSelection
*selection
,
450 IrrecoButtonBrowserWidget
*self
)
453 gchar
*sel_label
= NULL
;
454 IrrecoThemeButton
*button
= NULL
;
457 if (irreco_listbox_get_selection(IRRECO_LISTBOX(self
->images
),
458 &sel_index
, &sel_label
, (gpointer
*)&button
)) {
459 self
->current_image
= button
;
461 self
->current_image
= NULL
;