Fixed edit bg and button functions and clean code.
[irreco.git] / irreco / src / core / irreco_theme_creator_buttons.c
blob3495e4386541963eaedbe2d456216ebe537239c1
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)
5 *
6 * irreco_theme_creator_buttons.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_buttons.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_buttons.h"
21 #include "irreco_theme_creator_dlg.h"
22 #include "irreco_background_creator_dlg.h"
23 #include "irreco_button_creator_dlg.h"
25 /**
26 * @addtogroup IrrecoThemeCreatorBackgrounds
27 * @ingroup Irreco
29 * User interface for IrrecoThemeCreatorDlgBackgrounds.
31 * @{
34 /**
35 * @file
36 * Source file of @ref IrrecoThemeCreatorBackgrounds.
39 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 /* Datatypes */
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
43 #define IRRECO_BUTTON_PREVIEW_WIDHT (IRRECO_SCREEN_HEIGHT / 5)
44 #define IRRECO_BUTTON_PREVIEW_HEIGHT (IRRECO_SCREEN_HEIGHT / 5)
46 enum
48 DATA_COL,
49 TEXT_COL,
50 UNPIXBUF_COL,
51 PIXBUF_COL,
52 N_COLUMNS
54 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
55 /* Prototypes. */
56 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
57 static void _image_selection_changed(GtkTreeSelection * selection,
58 IrrecoThemeCreatorButtons *self);
60 gboolean
61 irreco_theme_creator_buttons_get_iter(IrrecoThemeCreatorButtons *self,
62 gint index, GtkTreeIter * iter);
63 gboolean
64 irreco_theme_creator_buttons_set_selection(IrrecoThemeCreatorButtons
65 *self, gint index);
66 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
67 /* Construction & Destruction */
68 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
70 /**
71 * @name Construction & Destruction
72 * @{
75 G_DEFINE_TYPE (IrrecoThemeCreatorButtons, irreco_theme_creator_buttons,
76 IRRECO_TYPE_INTERNAL_WIDGET)
78 static void
79 irreco_theme_creator_buttons_finalize (GObject *object)
81 /* TODO: Add deinitalization code here */
82 IRRECO_ENTER
83 G_OBJECT_CLASS (
84 irreco_theme_creator_buttons_parent_class)->finalize (object);
85 IRRECO_RETURN
88 static void
89 irreco_theme_creator_buttons_class_init (IrrecoThemeCreatorButtonsClass *klass)
91 GObjectClass *object_class;
92 IRRECO_ENTER
94 object_class = G_OBJECT_CLASS (klass);
95 object_class->finalize = irreco_theme_creator_buttons_finalize;
96 IRRECO_RETURN
99 static void
100 irreco_theme_creator_buttons_init (IrrecoThemeCreatorButtons *self)
102 /* TODO: Add initialization code here */
104 IRRECO_ENTER
106 gtk_box_set_spacing(GTK_BOX(self), 1);
108 /*BUTTONS*/
109 self->scroll_buttons = gtk_scrolled_window_new(NULL, NULL);
110 self->store_buttons = gtk_list_store_new(N_COLUMNS, G_TYPE_POINTER,
111 G_TYPE_STRING, GDK_TYPE_PIXBUF,
112 GDK_TYPE_PIXBUF);
113 self->view_buttons = gtk_tree_view_new();
114 gtk_box_pack_start_defaults(GTK_BOX(self), self->scroll_buttons);
117 /*BUTTONS COLUMNS*/
119 gtk_tree_view_set_model(GTK_TREE_VIEW(self->view_buttons),
120 GTK_TREE_MODEL(self->store_buttons));
121 g_object_unref(self->store_buttons);
123 /* Create pixbuf column. */
124 self->renderer_buttons = gtk_cell_renderer_pixbuf_new();
125 self->column_buttons = gtk_tree_view_column_new_with_attributes(
126 "Unpressed ", self->renderer_buttons,
127 "pixbuf", UNPIXBUF_COL, NULL);
128 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_buttons),
129 self->column_buttons);
131 self->renderer_buttons = gtk_cell_renderer_pixbuf_new();
132 self->column_buttons = gtk_tree_view_column_new_with_attributes(
133 "Pressed ",
134 self->renderer_buttons,
135 "pixbuf", PIXBUF_COL, NULL);
136 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_buttons),
137 self->column_buttons);
139 /* Create text column. */
140 self->renderer_buttons = gtk_cell_renderer_text_new();
141 self->column_buttons = gtk_tree_view_column_new_with_attributes(
142 "Name ",
143 self->renderer_buttons,
144 "text", TEXT_COL, NULL);
145 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_buttons),
146 self->column_buttons);
147 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self->view_buttons),
148 TRUE);
150 /* Scroll_buttons*/
152 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->scroll_buttons),
153 GTK_POLICY_AUTOMATIC,
154 GTK_POLICY_AUTOMATIC);
155 gtk_container_add(GTK_CONTAINER(self->scroll_buttons),
156 self->view_buttons);
157 self->tree_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
158 self->view_buttons));
159 gtk_tree_selection_set_mode(self->tree_selection, GTK_SELECTION_SINGLE);
162 /* Signal handlers. */
163 g_signal_connect(G_OBJECT(self->tree_selection),
164 "changed",
165 G_CALLBACK(_image_selection_changed),
166 self);
167 IRRECO_RETURN
171 GtkWidget* irreco_theme_creator_buttons_new(GtkWindow *parent,
172 IrrecoData *irreco_data,
173 IrrecoTheme * irreco_theme)
175 IrrecoThemeCreatorButtons *self;
176 IRRECO_ENTER
178 self = g_object_new(IRRECO_TYPE_THEME_CREATOR_BUTTONS,
179 "irreco-data", irreco_data,
180 NULL);
182 irreco_theme_creator_buttons_set_parent_window(self, parent);
183 self->parent_window = GTK_WINDOW(parent);
184 self->irreco_data = irreco_data;
185 self->theme = irreco_theme;
186 irreco_theme_creator_buttons_refresh(self);
187 IRRECO_RETURN_PTR(GTK_WIDGET(self));
190 /** @} */
191 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
192 /* Private Functions */
193 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
196 * @name Private Functions
197 * @{
199 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
200 /* Public Functions */
201 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
204 * @name Public Functions
205 * @{
209 * Set parent window for popup dialogs.
211 void
212 irreco_theme_creator_buttons_set_parent_window(IrrecoThemeCreatorButtons *self,
213 GtkWindow *parent)
215 IRRECO_ENTER
216 self->parent_window = parent;
217 IRRECO_RETURN
220 void
221 irreco_theme_creator_buttons_refresh(IrrecoThemeCreatorButtons *self)
224 GdkPixbuf *unpixbuf = NULL;
225 GdkPixbuf *pixbuf = NULL;
226 GError *error = NULL;
227 IrrecoData *irreco_data = NULL;
228 IrrecoThemeManager *manager = NULL;
229 gint i=0;
230 gint theme_button_count;
231 const gchar *image_name;
232 IrrecoTheme *theme;
233 GtkTreeIter iter;
234 IRRECO_ENTER
236 gtk_list_store_clear(self->store_buttons);
237 irreco_data = self->irreco_data;
238 manager = irreco_data->theme_manager;
240 theme = self->theme;
242 theme_button_count = irreco_string_table_lenght(theme->buttons);
244 if (theme_button_count > 0) {
245 while(theme_button_count > 0) {
247 IrrecoThemeButton *button_image;
249 irreco_string_table_index(theme->buttons, i,
250 &image_name,
251 (gpointer *)
252 &button_image);
253 /*unpixbuf = gdk_pixbuf_new_from_file(
254 button_image->image_up->str,
255 &error);*/
257 unpixbuf = gdk_pixbuf_new_from_file_at_scale(
258 button_image->image_up->str,
259 IRRECO_BUTTON_PREVIEW_WIDHT,
260 IRRECO_BUTTON_PREVIEW_HEIGHT,
261 GDK_INTERP_NEAREST, &error);
264 if (irreco_gerror_check_print(&error)) {
265 IRRECO_RETURN
267 /*pixbuf = gdk_pixbuf_new_from_file(
268 button_image->image_down->str,
269 &error);*/
270 pixbuf = gdk_pixbuf_new_from_file_at_scale(
271 button_image->image_down->str,
272 IRRECO_BUTTON_PREVIEW_WIDHT,
273 IRRECO_BUTTON_PREVIEW_HEIGHT,
274 GDK_INTERP_NEAREST, &error);
275 if (irreco_gerror_check_print(&error)) {
276 IRRECO_RETURN
279 gtk_list_store_append(self->store_buttons, &iter);
281 gtk_list_store_set(self->store_buttons, &iter,
282 DATA_COL, button_image,
283 UNPIXBUF_COL, unpixbuf,
284 PIXBUF_COL, pixbuf,
285 TEXT_COL,
286 button_image->name->str, -1);
288 theme_button_count -=1;
289 i++;
292 } else {
296 if (unpixbuf != NULL) g_object_unref(G_OBJECT(unpixbuf));
297 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
299 IRRECO_RETURN
303 static void _image_selection_changed(GtkTreeSelection * selection,
304 IrrecoThemeCreatorButtons *self)
306 GtkTreeIter iter;
307 GtkTreeModel *model = NULL;
308 IRRECO_ENTER
310 /* Get currect selection, if set. */
311 selection = gtk_tree_view_get_selection(
312 GTK_TREE_VIEW(self->view_buttons));
313 if (gtk_tree_selection_get_selected(self->tree_selection,
314 &model, &iter)) {
315 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
316 DATA_COL, &self->current_button,
317 -1);
320 IRRECO_RETURN
324 IrrecoThemeButton
325 *irreco_theme_creator_buttons_get_selected_button(IrrecoThemeCreatorButtons
326 *self)
328 IRRECO_ENTER
329 IRRECO_RETURN_PTR(self->current_button);
333 gboolean
334 irreco_theme_creator_buttons_remove_selected(IrrecoThemeCreatorButtons *self)
336 gint index;
337 GtkTreeIter iter;
339 IRRECO_ENTER
341 if ((index = self->sel_index) == -1
342 || irreco_theme_creator_buttons_get_iter(self, index, &iter) == FALSE) {
343 IRRECO_RETURN_BOOL(FALSE);
346 irreco_theme_creator_buttons_set_selection(self, index - 1)
347 || irreco_theme_creator_buttons_set_selection(self, index + 1);
349 gtk_list_store_remove(self->store_buttons, &iter);
350 IRRECO_RETURN_BOOL(TRUE);
353 gboolean
354 irreco_theme_creator_buttons_get_iter(IrrecoThemeCreatorButtons *self,
355 gint index, GtkTreeIter * iter)
357 gboolean rvalue;
358 GtkTreeModel *model;
359 GtkTreePath *path;
360 IRRECO_ENTER
362 model = gtk_tree_view_get_model(GTK_TREE_VIEW(
363 self->view_buttons));
364 path = gtk_tree_path_new_from_indices(index, -1);
365 rvalue = gtk_tree_model_get_iter(model, iter, path);
366 gtk_tree_path_free(path);
367 IRRECO_RETURN_INT(rvalue);
371 gboolean
372 irreco_theme_creator_buttons_set_selection(IrrecoThemeCreatorButtons
373 *self, gint index)
375 GtkTreeIter iter;
376 IRRECO_ENTER
378 if (irreco_theme_creator_buttons_get_iter(self, index, &iter)) {
379 gtk_tree_selection_select_iter(self->tree_selection, &iter);
380 IRRECO_RETURN_BOOL(TRUE);
382 IRRECO_RETURN_BOOL(FALSE);