added irreco_theme_button_set_from_button function.
[irreco.git] / irreco / src / core / irreco_theme_button.c
blob3bb9de8d84652cbe222b7f382a1c7254ff0f7a50
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi)
4 * Pekka Gehör (pegu6@msn.com)
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_theme_button.h"
23 /**
24 * @addtogroup IrrecoThemeButton
25 * @ingroup Irreco
27 * Contains information of button.
29 * @{
32 /**
33 * @file
34 * Source file of @ref IrrecoThemeButton.
37 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
38 /* Prototypes */
39 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /* Construction & Destruction */
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /**
46 * @name Construction & Destruction
47 * @{
50 IrrecoThemeButton *irreco_theme_button_new(const char *theme_name)
52 IrrecoThemeButton *self;
53 IRRECO_ENTER
55 self = g_slice_new0(IrrecoThemeButton);
57 self->theme_name = g_string_new(theme_name);
58 self->style_name = g_string_new("");
59 self->name = g_string_new("");
60 self->image_up = g_string_new("");
61 self->image_down = g_string_new("");
62 self->text_format_up = g_string_new("");
63 self->text_format_down = g_string_new("");
64 self->text_padding = 5;
66 /* Default to center alignment. */
67 self->text_h_align = 0.5;
68 self->text_v_align = 0.5;
70 IRRECO_RETURN_PTR(self);
73 void irreco_theme_button_free(IrrecoThemeButton *self)
75 IRRECO_ENTER
77 g_assert(self != NULL);
79 g_string_free(self->theme_name, TRUE);
80 self->theme_name = NULL;
82 g_string_free(self->style_name, TRUE);
83 self->style_name = NULL;
85 g_string_free(self->name, TRUE);
86 self->name = NULL;
88 g_string_free(self->image_up, TRUE);
89 self->image_up = NULL;
91 g_string_free(self->image_down, TRUE);
92 self->image_down = NULL;
94 g_string_free(self->text_format_up, TRUE);
95 self->text_format_up = NULL;
97 g_string_free(self->text_format_down, TRUE);
98 self->text_format_down = NULL;
100 g_slice_free(IrrecoThemeButton, self);
101 self = NULL;
103 IRRECO_RETURN
106 /** @} */
108 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
109 /* Private Functions */
110 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
112 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
113 /* Public Functions */
114 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
117 * @name Public Functions
118 * @{
121 void irreco_theme_button_set(IrrecoThemeButton *self,
122 const char *style_name,
123 const char *name,
124 gboolean allow_text,
125 const char *image_up,
126 const char *image_down,
127 const char *text_format_up,
128 const char *text_format_down,
129 gint text_padding,
130 gfloat text_h_align,
131 gfloat text_v_align)
133 IRRECO_ENTER
135 if (style_name != NULL) {
136 g_string_printf(self->style_name, "%s", style_name);
138 if (name != NULL) {
139 g_string_printf(self->name, "%s", name);
141 self->allow_text = allow_text;
142 if (image_up != NULL) {
143 g_string_printf(self->image_up, "%s", image_up);
145 if (image_down != NULL) {
146 g_string_printf(self->image_down, "%s", image_down);
147 } else {
148 g_string_printf(self->image_down, "%s", image_up);
150 if (text_format_up != NULL) {
151 g_string_printf(self->text_format_up, "%s", text_format_up);
153 if (text_format_down != NULL) {
154 g_string_printf(self->text_format_down, "%s", text_format_down);
157 self->text_padding = text_padding;
159 /* Set Horisontal aligment. */
160 if (self->text_h_align < 0) {
161 self->text_h_align = 0;
162 } else if (self->text_h_align > 1) {
163 self->text_h_align = 1;
164 } else {
165 self->text_h_align = text_h_align;
168 /* Set vertical alignment. */
169 if (self->text_v_align < 0) {
170 self->text_v_align = 0;
171 } else if (self->text_v_align > 1) {
172 self->text_v_align = 1;
173 } else {
174 self->text_v_align = text_v_align;
177 IRRECO_RETURN
179 void irreco_theme_button_set_from_button(IrrecoThemeButton *self,
180 IrrecoThemeButton *button)
182 IRRECO_ENTER
183 irreco_theme_button_set(self,
184 button->style_name->str,
185 button->name->str,
186 button->allow_text,
187 button->image_up->str,
188 button->image_down->str,
189 button->text_format_up->str,
190 button->text_format_down->str,
191 button->text_padding,
192 button->text_h_align,
193 button->text_v_align);
194 IRRECO_RETURN
196 void irreco_theme_button_print(IrrecoThemeButton *self)
198 IRRECO_ENTER
200 IRRECO_DEBUG("--------------------------------------------\n");
201 IRRECO_DEBUG("theme_name: %s\n", self->theme_name->str);
202 IRRECO_DEBUG("style_name: %s\n", self->style_name->str);
203 IRRECO_DEBUG("button_name: %s\n", self->name->str);
204 IRRECO_DEBUG("allow_text: %d\n", self->allow_text);
205 IRRECO_DEBUG("image_up: %s\n",self->image_up->str);
206 IRRECO_DEBUG("image_down: %s\n", self->image_down->str);
207 IRRECO_DEBUG("text_format_up: %s\n", self->text_format_up->str);
208 IRRECO_DEBUG("text_format_down: %s\n", self->text_format_down->str);
209 IRRECO_DEBUG("text_padding: %d\n", self->text_padding);
210 IRRECO_DEBUG("--------------------------------------------\n");
212 IRRECO_RETURN
215 IrrecoThemeButton *irreco_theme_button_copy(IrrecoThemeButton *self)
217 IrrecoThemeButton *new = NULL;
218 IRRECO_ENTER
220 new = irreco_theme_button_new(self->theme_name->str);
221 irreco_theme_button_set(new, self->style_name->str, self->name->str,
222 self->allow_text, self->image_up->str,
223 self->image_down->str, self->text_format_up->str,
224 self->text_format_down->str, self->text_padding,
225 self->text_h_align, self->text_v_align);
227 IRRECO_RETURN_PTR(new);
231 * IrrecoThemeBg new from dir
234 IrrecoThemeButton *irreco_theme_button_new_from_dir(const gchar *dir)
236 IrrecoThemeButton *self = NULL;
238 IRRECO_ENTER
240 self = irreco_theme_button_new(NULL);
241 irreco_theme_button_read(self, dir);
242 IRRECO_RETURN_PTR(self);
245 void irreco_theme_button_read(IrrecoThemeButton *self, const gchar *dir)
248 IrrecoKeyFile *keyfile;
249 GString *conf = NULL;
250 gchar *name = NULL;
251 gchar *up = NULL;
252 gchar *down = NULL;
253 gboolean allow_text;
254 gchar *text_format_up = NULL;
255 gchar *text_format_down = NULL;
256 gint text_padding = 5;
257 gfloat text_h_align = 0.5;
258 gfloat text_v_align = 0.5;
259 GString *style_name = g_string_new(self->name->str);
261 IRRECO_ENTER
263 conf = g_string_new(dir);
264 g_string_append_printf(conf, "/button.conf");
265 keyfile = irreco_keyfile_create(dir,
266 conf->str,
267 "theme-button");
268 if (keyfile == NULL) goto end;
270 /* Required fields. */
271 if (!irreco_keyfile_get_str(keyfile, "name", &name) ||
272 !irreco_keyfile_get_path(keyfile, "up", &up)) {
273 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
274 keyfile->group);
275 goto end;
278 /* Optional fields. */
279 irreco_keyfile_get_path(keyfile, "down", &down);
280 irreco_keyfile_get_bool(keyfile, "allow-text", &allow_text);
281 irreco_keyfile_get_str(keyfile, "text-format-up", &text_format_up);
282 irreco_keyfile_get_str(keyfile, "text-format-down", &text_format_down);
283 irreco_keyfile_get_int(keyfile, "text-padding", &text_padding);
284 irreco_keyfile_get_float(keyfile, "text-h-align", &text_h_align);
285 irreco_keyfile_get_float(keyfile, "text-v-align", &text_v_align);
287 g_string_append_printf(style_name,"%s", name);
289 irreco_theme_button_set(self, style_name->str,
290 name, allow_text,
291 up, down,
292 text_format_up, text_format_down,
293 text_padding,
294 text_h_align,
295 text_v_align);
297 irreco_theme_button_print(self);
299 end:
300 if(keyfile != NULL) irreco_keyfile_destroy(keyfile);
301 if(name != NULL) g_free(name);
302 if(name != NULL) g_free(up);
303 if(name != NULL) g_free(down);
304 if(name != NULL) g_free(text_format_up);
305 if(name != NULL) g_free(text_format_down);
306 g_string_free(style_name, TRUE);
310 IRRECO_RETURN
314 /** @} */
316 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
317 /* Events and Callbacks */
318 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
320 /** @} */