Mostly minor fixes up until version 0.8.10.
[irreco.git] / irreco / src / core / irreco_theme_creator_buttons.c
blobb5f31030186c450bbed7b7d24398c8ba7b1ab3ae
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_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
100 static void
101 irreco_theme_creator_buttons_init (IrrecoThemeCreatorButtons *self)
103 /* TODO: Add initialization code here */
105 IRRECO_ENTER
107 gtk_box_set_spacing(GTK_BOX(self), 1);
109 /*BUTTONS*/
110 self->scroll_buttons = gtk_scrolled_window_new(NULL, NULL);
111 self->store_buttons = gtk_list_store_new(N_COLUMNS, G_TYPE_POINTER,
112 G_TYPE_STRING, GDK_TYPE_PIXBUF,
113 GDK_TYPE_PIXBUF);
114 self->view_buttons = gtk_tree_view_new();
115 gtk_box_pack_start_defaults(GTK_BOX(self), self->scroll_buttons);
118 /*BUTTONS COLUMNS*/
120 gtk_tree_view_set_model(GTK_TREE_VIEW(self->view_buttons),
121 GTK_TREE_MODEL(self->store_buttons));
122 g_object_unref(self->store_buttons);
124 /* Create pixbuf column. */
125 self->renderer_buttons = gtk_cell_renderer_pixbuf_new();
126 self->column_buttons = gtk_tree_view_column_new_with_attributes(
127 "Unpressed ", self->renderer_buttons,
128 "pixbuf", UNPIXBUF_COL, NULL);
129 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_buttons),
130 self->column_buttons);
132 self->renderer_buttons = gtk_cell_renderer_pixbuf_new();
133 self->column_buttons = gtk_tree_view_column_new_with_attributes(
134 "Pressed ",
135 self->renderer_buttons,
136 "pixbuf", PIXBUF_COL, NULL);
137 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_buttons),
138 self->column_buttons);
140 /* Create text column. */
141 self->renderer_buttons = gtk_cell_renderer_text_new();
142 self->column_buttons = gtk_tree_view_column_new_with_attributes(
143 "Name ",
144 self->renderer_buttons,
145 "text", TEXT_COL, NULL);
146 gtk_tree_view_append_column(GTK_TREE_VIEW(self->view_buttons),
147 self->column_buttons);
148 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(self->view_buttons),
149 TRUE);
151 /* Scroll_buttons*/
153 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->scroll_buttons),
154 GTK_POLICY_AUTOMATIC,
155 GTK_POLICY_AUTOMATIC);
156 gtk_container_add(GTK_CONTAINER(self->scroll_buttons),
157 self->view_buttons);
158 self->tree_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
159 self->view_buttons));
160 gtk_tree_selection_set_mode(self->tree_selection, GTK_SELECTION_SINGLE);
163 /* Signal handlers. */
164 g_signal_connect(G_OBJECT(self->tree_selection),
165 "changed",
166 G_CALLBACK(_image_selection_changed),
167 self);
168 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 * @{
200 static void _image_selection_changed(GtkTreeSelection * selection,
201 IrrecoThemeCreatorButtons *self)
203 GtkTreeIter iter;
204 GtkTreeModel *model = NULL;
205 GtkTreePath *path = NULL;
206 gint *path_indices = NULL;
207 IRRECO_ENTER
209 /* Get currect selection, if set. */
210 selection = gtk_tree_view_get_selection(
211 GTK_TREE_VIEW(self->view_buttons));
212 if (gtk_tree_selection_get_selected(self->tree_selection,
213 &model, &iter)) {
214 path = gtk_tree_model_get_path(model, &iter);
215 path_indices = gtk_tree_path_get_indices(path);
216 self->sel_index = path_indices[0];
217 gtk_tree_path_free(path);
219 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
220 DATA_COL, &self->current_button,
221 -1);
222 } else {
223 self->current_button = NULL;
226 IRRECO_RETURN
230 gboolean
231 irreco_theme_creator_buttons_get_iter(IrrecoThemeCreatorButtons *self,
232 gint index, GtkTreeIter * iter)
234 gboolean rvalue;
235 GtkTreeModel *model;
236 GtkTreePath *path;
237 IRRECO_ENTER
239 model = gtk_tree_view_get_model(GTK_TREE_VIEW(
240 self->view_buttons));
241 path = gtk_tree_path_new_from_indices(index, -1);
242 rvalue = gtk_tree_model_get_iter(model, iter, path);
243 gtk_tree_path_free(path);
244 IRRECO_RETURN_INT(rvalue);
247 gboolean
248 irreco_theme_creator_buttons_set_selection(IrrecoThemeCreatorButtons
249 *self, gint index)
251 GtkTreeIter iter;
252 IRRECO_ENTER
254 if (irreco_theme_creator_buttons_get_iter(self, index, &iter)) {
255 gtk_tree_selection_select_iter(self->tree_selection, &iter);
256 IRRECO_RETURN_BOOL(TRUE);
258 IRRECO_RETURN_BOOL(FALSE);
260 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
261 /* Public Functions */
262 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
265 * @name Public Functions
266 * @{
270 * Set parent window for popup dialogs.
272 void
273 irreco_theme_creator_buttons_set_parent_window(IrrecoThemeCreatorButtons *self,
274 GtkWindow *parent)
276 IRRECO_ENTER
277 self->parent_window = parent;
278 IRRECO_RETURN
281 void
282 irreco_theme_creator_buttons_refresh(IrrecoThemeCreatorButtons *self)
285 GdkPixbuf *unpixbuf = NULL;
286 GdkPixbuf *pixbuf = NULL;
287 GError *error = NULL;
288 IrrecoData *irreco_data = NULL;
289 IrrecoThemeManager *manager = NULL;
290 IrrecoTheme *theme;
291 GtkTreeIter iter;
292 IRRECO_ENTER
294 gtk_list_store_clear(self->store_buttons);
295 irreco_data = self->irreco_data;
296 manager = irreco_data->theme_manager;
298 theme = self->theme;
301 IRRECO_STRING_TABLE_FOREACH_DATA(theme->buttons,
302 IrrecoThemeButton *, button)
304 irreco_theme_button_print(button);
305 unpixbuf = gdk_pixbuf_new_from_file_at_scale(
306 button->image_up->str,
307 IRRECO_BUTTON_PREVIEW_WIDHT,
308 IRRECO_BUTTON_PREVIEW_HEIGHT,
309 GDK_INTERP_NEAREST, &error);
312 if (irreco_gerror_check_print(&error)) {
313 IRRECO_RETURN
316 pixbuf = gdk_pixbuf_new_from_file_at_scale(
317 button->image_down->str,
318 IRRECO_BUTTON_PREVIEW_WIDHT,
319 IRRECO_BUTTON_PREVIEW_HEIGHT,
320 GDK_INTERP_NEAREST, &error);
321 if (irreco_gerror_check_print(&error)) {
322 IRRECO_RETURN
325 gtk_list_store_append(self->store_buttons, &iter);
327 gtk_list_store_set(self->store_buttons, &iter,
328 DATA_COL, button,
329 UNPIXBUF_COL, unpixbuf,
330 PIXBUF_COL, pixbuf,
331 TEXT_COL,
332 button->name->str, -1);
333 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(
334 self->view_buttons));
335 IRRECO_STRING_TABLE_FOREACH_END
338 if (unpixbuf != NULL) g_object_unref(G_OBJECT(unpixbuf));
339 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
341 IRRECO_RETURN
344 IrrecoThemeButton
345 *irreco_theme_creator_buttons_get_selected_button(IrrecoThemeCreatorButtons
346 *self)
348 IRRECO_ENTER
349 IRRECO_RETURN_PTR(self->current_button);
352 gboolean
353 irreco_theme_creator_buttons_remove_selected(IrrecoThemeCreatorButtons *self)
355 gint index;
356 GtkTreeIter iter;
357 IRRECO_ENTER
359 _image_selection_changed(self->tree_selection, self);
360 if ((index = self->sel_index) == -1
361 || irreco_theme_creator_buttons_get_iter(self, index, &iter) == FALSE) {
362 IRRECO_RETURN_BOOL(FALSE);
364 if(!irreco_theme_creator_buttons_set_selection(self, index - 1)) {
365 irreco_theme_creator_buttons_set_selection(self, index + 1);
368 gtk_list_store_remove(self->store_buttons, &iter);
369 IRRECO_RETURN_BOOL(TRUE);