Irreco for N900 (Maemo 5) update. Push for 0.8.* changes.
[irreco.git] / irreco / src / core / irreco_theme_creator_backgrounds.c
blob0c366d925edce53ecb110429b16252d59612fdd5
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
118 static void
119 irreco_theme_creator_backgrounds_init (IrrecoThemeCreatorBackgrounds *self)
121 /* TODO: Add initialization code here */
123 /*Backgrounds widgets*/
125 IRRECO_ENTER
127 gtk_box_set_spacing(GTK_BOX(self), 1);
129 /*BACKGROUNDS*/
131 self->scroll_backgrounds = gtk_scrolled_window_new(NULL, NULL);
132 self->store_backgrounds = gtk_list_store_new(N_COLUMNS, G_TYPE_POINTER,
133 G_TYPE_STRING,
134 GDK_TYPE_PIXBUF);
135 self->view_backgrounds = gtk_tree_view_new();
137 gtk_box_pack_start_defaults(GTK_BOX(self), self->scroll_backgrounds);
139 /*BACKGROUNDS COLUMNS*/
140 gtk_tree_view_set_model(GTK_TREE_VIEW(self->view_backgrounds),
141 GTK_TREE_MODEL(self->store_backgrounds));
142 g_object_unref(self->store_backgrounds);
144 /* Create pixbuf column. */
145 self->renderer_backgrounds = gtk_cell_renderer_pixbuf_new();
146 self->column_backgrounds = gtk_tree_view_column_new_with_attributes(
147 "Background ",
148 self->renderer_backgrounds,
149 "pixbuf", PIXBUF_COL, NULL);
150 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_backgrounds),
151 self->column_backgrounds);
153 /* Create text column. */
154 self->renderer_backgrounds = gtk_cell_renderer_text_new();
155 self->column_backgrounds = gtk_tree_view_column_new_with_attributes(
156 "Name ",
157 self->renderer_backgrounds,
158 "text", TEXT_COL, NULL);
159 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_backgrounds),
160 self->column_backgrounds);
161 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self->view_backgrounds),
162 TRUE);
165 /* Scroll_backgrounds*/
167 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(
168 self->scroll_backgrounds),
169 GTK_POLICY_AUTOMATIC,
170 GTK_POLICY_AUTOMATIC);
171 gtk_container_add(GTK_CONTAINER(self->scroll_backgrounds),
172 self->view_backgrounds);
174 self->tree_selection = gtk_tree_view_get_selection(
175 GTK_TREE_VIEW(self->view_backgrounds));
176 gtk_tree_selection_set_mode(self->tree_selection,
177 GTK_SELECTION_SINGLE);
179 /* Signal handlers. */
180 g_signal_connect(G_OBJECT(self->tree_selection),
181 "changed",
182 G_CALLBACK(_image_selection_changed),
183 self);
184 IRRECO_RETURN
187 GtkWidget*
188 irreco_theme_creator_backgrounds_new(GtkWindow *parent, IrrecoData *irreco_data,
189 IrrecoTheme * irreco_theme)
191 IrrecoThemeCreatorBackgrounds *self;
192 IRRECO_ENTER
194 self = g_object_new(IRRECO_TYPE_THEME_CREATOR_BACKGROUNDS,
195 "irreco-data", irreco_data, NULL);
197 irreco_theme_creator_backgrounds_set_parent_window(self, parent);
198 self->parent_window = GTK_WINDOW(parent);
199 self->irreco_data = irreco_data;
200 self->theme = irreco_theme;
202 irreco_theme_creator_backgrounds_refresh(self);
204 IRRECO_RETURN_PTR(GTK_WIDGET(self));
207 /** @} */
209 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
210 /* Private Functions */
211 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
214 * @name Private Functions
215 * @{
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);
283 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
284 /* Public Functions */
285 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
288 * @name Public Functions
289 * @{
293 * Set parent window for popup dialogs.
295 void
296 irreco_theme_creator_backgrounds_set_parent_window(
297 IrrecoThemeCreatorBackgrounds *self,
298 GtkWindow *parent)
300 IRRECO_ENTER
301 self->parent_window = parent;
302 IRRECO_RETURN
306 * Append row to the listbox with resized image.
309 /** @} */
310 void
311 irreco_theme_creator_backgrounds_refresh(IrrecoThemeCreatorBackgrounds *self)
314 GdkPixbuf *pixbuf = NULL;
315 GError *error = NULL;
316 IrrecoData *irreco_data = NULL;
317 IrrecoThemeManager *manager = NULL;
318 GtkTreeIter iter;
319 IrrecoTheme *theme;
320 IRRECO_ENTER
322 gtk_list_store_clear(self->store_backgrounds);
323 irreco_data = self->irreco_data;
324 manager = irreco_data->theme_manager;
325 theme = self->theme;
327 IRRECO_STRING_TABLE_FOREACH_DATA(theme->backgrounds,
328 IrrecoThemeBg *, background)
330 irreco_theme_bg_print(background);
331 pixbuf = gdk_pixbuf_new_from_file_at_scale(
332 background->image_path->str,
333 IRRECO_BACKGROUNDS_PREVIEW_WIDHT,
334 IRRECO_BACKGROUNDS_PREVIEW_HEIGHT,
335 GDK_INTERP_NEAREST, &error);
337 if (irreco_gerror_check_print(&error)) {
338 IRRECO_RETURN
340 gtk_list_store_append(self->store_backgrounds,
341 &iter);
342 gtk_list_store_set(self->store_backgrounds,
343 &iter, DATA_COL,
344 background, PIXBUF_COL,
345 pixbuf, TEXT_COL,
346 background->image_name->str,
347 -1);
349 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(
350 self->view_backgrounds));
352 IRRECO_STRING_TABLE_FOREACH_END
354 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
356 IRRECO_RETURN
359 void _backgrounds_row_activated_event(GtkTreeView *tree_view,
360 GtkTreePath *path,
361 GtkTreeViewColumn *column,
362 IrrecoThemeCreatorBackgrounds *self)
364 IRRECO_ENTER
365 IRRECO_RETURN
368 IrrecoThemeBg *
369 irreco_theme_creator_backgrounds_get_selected_bg(
370 IrrecoThemeCreatorBackgrounds *self)
373 IRRECO_ENTER
374 IRRECO_RETURN_PTR(self->current_bg);
377 gboolean
378 irreco_theme_creator_backgrounds_remove_selected(IrrecoThemeCreatorBackgrounds *self)
380 gint index;
381 GtkTreeIter iter;
382 IRRECO_ENTER
384 _image_selection_changed(self->tree_selection, self);
385 if ((index = self->sel_index) == -1
386 || irreco_theme_creator_backgrounds_get_iter(self, index, &iter) == FALSE) {
387 IRRECO_RETURN_BOOL(FALSE);
389 if(!irreco_theme_creator_backgrounds_set_selection(self, index - 1)) {
390 irreco_theme_creator_backgrounds_set_selection(self, index + 1);
393 gtk_list_store_remove(self->store_backgrounds, &iter);
394 IRRECO_RETURN_BOOL(TRUE);