Added theme_bg_save functio and clean code
[irreco.git] / irreco / src / core / irreco_theme_creator_backgrounds.c
blobd2c0c1fbff8c0440eab2d98bd05ffe4278d062c5
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_theme_creator_backgrounds.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_theme_creator_backgrounds.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_theme_creator_backgrounds.h"
21 #include "irreco_theme_creator_dlg.h"
22 #include "irreco_listbox.h"
24 /**
25 * @addtogroup IrrecoThemeCreatorBackgrounds
26 * @ingroup Irreco
28 * User interface for IrrecoThemeCreatorDlgBackgrounds.
30 * @{
33 /**
34 * @file
35 * Source file of @ref IrrecoThemeCreatorBackgrounds.
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 /* Datatypes */
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 #define IRRECO_BACKGROUNDS_PREVIEW_WIDHT (IRRECO_SCREEN_WIDTH / 4.5)
43 #define IRRECO_BACKGROUNDS_PREVIEW_HEIGHT (IRRECO_SCREEN_HEIGHT / 4.5)
45 enum
47 DATA_COL,
48 TEXT_COL,
49 PIXBUF_COL,
50 N_COLUMNS
52 /** Loader states. */
53 enum
55 LOADER_STATE_INIT,
56 LOADER_STATE_LOOP,
57 LOADER_STATE_END,
58 LOADER_STATE_CLEANUP
63 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
64 /* Prototypes. */
65 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
66 static void _image_selection_changed(GtkTreeSelection * selection,
67 IrrecoThemeCreatorBackgrounds *self);
69 gboolean
70 _get_selection(IrrecoThemeCreatorBackgrounds *self, gint *index, gchar **label,
71 gpointer *user_data);
72 gboolean
73 irreco_theme_creator_backgrounds_get_iter(IrrecoThemeCreatorBackgrounds *self,
74 gint index, GtkTreeIter * iter);
75 gboolean
76 irreco_theme_creator_backgrounds_set_selection(IrrecoThemeCreatorBackgrounds
77 *self, gint index);
80 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
81 /* Construction & Destruction */
82 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
84 /**
85 * @name Construction & Destruction
86 * @{
89 G_DEFINE_TYPE (IrrecoThemeCreatorBackgrounds, irreco_theme_creator_backgrounds,
90 IRRECO_TYPE_INTERNAL_WIDGET)
93 static void
94 irreco_theme_creator_backgrounds_finalize (GObject *object)
96 /* TODO: Add deinitalization code here */
97 IRRECO_ENTER
98 G_OBJECT_CLASS(
99 irreco_theme_creator_backgrounds_parent_class)->finalize
100 (object);
101 IRRECO_RETURN
105 static void
106 irreco_theme_creator_backgrounds_class_init(
107 IrrecoThemeCreatorBackgroundsClass *klass)
109 GObjectClass *object_class;
110 IRRECO_ENTER
112 object_class = G_OBJECT_CLASS (klass);
113 object_class->finalize = irreco_theme_creator_backgrounds_finalize;
114 IRRECO_RETURN
117 static void
118 irreco_theme_creator_backgrounds_init (IrrecoThemeCreatorBackgrounds *self)
120 /* TODO: Add initialization code here */
122 /*Backgrounds widgets*/
124 IRRECO_ENTER
126 gtk_box_set_spacing(GTK_BOX(self), 1);
128 /*BACKGROUNDS*/
130 self->scroll_backgrounds = gtk_scrolled_window_new(NULL, NULL);
131 self->store_backgrounds = gtk_list_store_new(N_COLUMNS, G_TYPE_POINTER,
132 G_TYPE_STRING,
133 GDK_TYPE_PIXBUF);
134 self->view_backgrounds = gtk_tree_view_new();
136 gtk_box_pack_start_defaults(GTK_BOX(self), self->scroll_backgrounds);
138 /*BACKGROUNDS COLUMNS*/
139 gtk_tree_view_set_model(GTK_TREE_VIEW(self->view_backgrounds),
140 GTK_TREE_MODEL(self->store_backgrounds));
141 g_object_unref(self->store_backgrounds);
143 /* Create pixbuf column. */
144 self->renderer_backgrounds = gtk_cell_renderer_pixbuf_new();
145 self->column_backgrounds = gtk_tree_view_column_new_with_attributes(
146 "Background ",
147 self->renderer_backgrounds,
148 "pixbuf", PIXBUF_COL, NULL);
149 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_backgrounds),
150 self->column_backgrounds);
152 /* Create text column. */
153 self->renderer_backgrounds = gtk_cell_renderer_text_new();
154 self->column_backgrounds = gtk_tree_view_column_new_with_attributes(
155 "Name ",
156 self->renderer_backgrounds,
157 "text", TEXT_COL, NULL);
158 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_backgrounds),
159 self->column_backgrounds);
160 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self->view_backgrounds),
161 TRUE);
164 /* Scroll_backgrounds*/
166 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(
167 self->scroll_backgrounds),
168 GTK_POLICY_AUTOMATIC,
169 GTK_POLICY_AUTOMATIC);
170 gtk_container_add(GTK_CONTAINER(self->scroll_backgrounds),
171 self->view_backgrounds);
173 self->tree_selection = gtk_tree_view_get_selection(
174 GTK_TREE_VIEW(self->view_backgrounds));
175 gtk_tree_selection_set_mode(self->tree_selection,
176 GTK_SELECTION_SINGLE);
178 /* Signal handlers. */
179 g_signal_connect(G_OBJECT(self->tree_selection),
180 "changed",
181 G_CALLBACK(_image_selection_changed),
182 self);
183 IRRECO_RETURN
186 GtkWidget*
187 irreco_theme_creator_backgrounds_new(GtkWindow *parent, IrrecoData *irreco_data,
188 IrrecoTheme * irreco_theme)
190 IrrecoThemeCreatorBackgrounds *self;
191 IRRECO_ENTER
193 self = g_object_new(IRRECO_TYPE_THEME_CREATOR_BACKGROUNDS,
194 "irreco-data", irreco_data, NULL);
196 irreco_theme_creator_backgrounds_set_parent_window(self, parent);
197 self->parent_window = GTK_WINDOW(parent);
198 self->irreco_data = irreco_data;
199 self->theme = irreco_theme;
201 irreco_theme_creator_backgrounds_refresh(self);
203 IRRECO_RETURN_PTR(GTK_WIDGET(self));
206 /** @} */
208 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
209 /* Private Functions */
210 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
213 * @name Private Functions
214 * @{
219 *Get information on the background picture
222 static void _image_selection_changed(GtkTreeSelection * selection,
223 IrrecoThemeCreatorBackgrounds *self)
225 GtkTreeIter iter;
226 GtkTreeModel *model = NULL;
227 GtkTreePath *path = NULL;
228 gint *path_indices = NULL;
229 IRRECO_ENTER
231 /* Get currect selection, if set. */
232 selection = gtk_tree_view_get_selection(
233 GTK_TREE_VIEW(self->view_backgrounds));
234 if (gtk_tree_selection_get_selected(self->tree_selection,
235 &model, &iter)) {
237 path = gtk_tree_model_get_path(model, &iter);
238 path_indices = gtk_tree_path_get_indices(path);
239 self->sel_index = path_indices[0];
240 gtk_tree_path_free(path);
242 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
243 DATA_COL, &self->current_bg,
244 -1);
245 } else {
246 self->current_bg = NULL;
248 IRRECO_RETURN
252 gboolean
253 irreco_theme_creator_backgrounds_get_iter(IrrecoThemeCreatorBackgrounds *self,
254 gint index, GtkTreeIter * iter)
256 gboolean rvalue;
257 GtkTreeModel *model;
258 GtkTreePath *path;
259 IRRECO_ENTER
261 model = gtk_tree_view_get_model(GTK_TREE_VIEW(
262 self->view_backgrounds));
263 path = gtk_tree_path_new_from_indices(index, -1);
264 rvalue = gtk_tree_model_get_iter(model, iter, path);
265 gtk_tree_path_free(path);
266 IRRECO_RETURN_INT(rvalue);
269 gboolean
270 irreco_theme_creator_backgrounds_set_selection(IrrecoThemeCreatorBackgrounds
271 *self, gint index)
273 GtkTreeIter iter;
274 IRRECO_ENTER
276 if (irreco_theme_creator_backgrounds_get_iter(self, index, &iter)) {
277 gtk_tree_selection_select_iter(self->tree_selection, &iter);
278 IRRECO_RETURN_BOOL(TRUE);
280 IRRECO_RETURN_BOOL(FALSE);
284 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
285 /* Public Functions */
286 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
289 * @name Public Functions
290 * @{
294 * Set parent window for popup dialogs.
296 void
297 irreco_theme_creator_backgrounds_set_parent_window(
298 IrrecoThemeCreatorBackgrounds *self,
299 GtkWindow *parent)
301 IRRECO_ENTER
302 self->parent_window = parent;
303 IRRECO_RETURN
307 * Append row to the listbox with resized image.
310 /** @} */
311 void
312 irreco_theme_creator_backgrounds_refresh(IrrecoThemeCreatorBackgrounds *self)
315 GdkPixbuf *pixbuf = NULL;
316 GError *error = NULL;
317 IrrecoData *irreco_data = NULL;
318 IrrecoThemeManager *manager = NULL;
319 GtkTreeIter iter;
320 gint theme_bg_count;
321 const gchar *image_name;
322 IrrecoTheme *theme;
323 gint i=0;
325 IRRECO_ENTER
326 gtk_list_store_clear(self->store_backgrounds);
327 irreco_data = self->irreco_data;
328 manager = irreco_data->theme_manager;
329 theme = self->theme;
331 IRRECO_DEBUG("Pointer: %p \n", (void*) theme);
332 IRRECO_DEBUG("Pointer: %p \n", (void*) self->theme);
333 IRRECO_DEBUG("Pointer: %p \n", (void*) theme->backgrounds);
335 theme_bg_count = irreco_string_table_lenght(theme->backgrounds);
336 if (theme_bg_count > 0) {
337 while(theme_bg_count > 0) {
338 IrrecoThemeBg *background_image;
340 irreco_string_table_index(theme->backgrounds, i,
341 &image_name,
342 (gpointer *)
343 &background_image);
345 irreco_theme_bg_print(background_image);
346 pixbuf = gdk_pixbuf_new_from_file_at_scale(
347 background_image->image_path->str,
348 IRRECO_BACKGROUNDS_PREVIEW_WIDHT,
349 IRRECO_BACKGROUNDS_PREVIEW_HEIGHT,
350 GDK_INTERP_NEAREST, &error);
352 if (irreco_gerror_check_print(&error)) {
353 IRRECO_RETURN
355 gtk_list_store_append(self->store_backgrounds,
356 &iter);
357 gtk_list_store_set(self->store_backgrounds,
358 &iter, DATA_COL,
359 background_image, PIXBUF_COL,
360 pixbuf, TEXT_COL,
361 background_image->image_name->str,
362 -1);
364 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(
365 self->view_backgrounds));
367 theme_bg_count -=1;
368 i++;
371 } else {
375 /*if (image_name != NULL) g_free(image_name);*/
376 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
378 IRRECO_RETURN
382 void _backgrounds_row_activated_event(GtkTreeView *tree_view,
383 GtkTreePath *path,
384 GtkTreeViewColumn *column,
385 IrrecoThemeCreatorBackgrounds *self)
387 IRRECO_ENTER
388 IRRECO_RETURN
391 IrrecoThemeBg *
392 irreco_theme_creator_backgrounds_get_selected_bg(
393 IrrecoThemeCreatorBackgrounds *self)
396 IRRECO_ENTER
397 IRRECO_RETURN_PTR(self->current_bg);
400 gboolean
401 irreco_theme_creator_backgrounds_remove_selected(IrrecoThemeCreatorBackgrounds *self)
403 gint index;
404 GtkTreeIter iter;
405 IRRECO_ENTER
407 if ((index = self->sel_index) == -1
408 || irreco_theme_creator_backgrounds_get_iter(self, index, &iter) == FALSE) {
409 IRRECO_RETURN_BOOL(FALSE);
412 irreco_theme_creator_backgrounds_set_selection(self, index - 1)
413 || irreco_theme_creator_backgrounds_set_selection(self, index + 1);
415 gtk_list_store_remove(self->store_backgrounds, &iter);
416 IRRECO_RETURN_BOOL(TRUE);