Clean code and added a few amendments (if()).
[irreco.git] / irreco / src / core / irreco_button_layout.c
blob03ef5512eaff74dc421dde34cfdd1d65ff917309
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco_button.h"
22 /**
23 * @addtogroup IrrecoButtonLayout
24 * @ingroup Irreco
26 * Buttonlayout is mainly an array of buttons. Since all buttons will be
27 * allocated dynamically, is is a good idea to have an object that will
28 * manage all those allocations, and can free them when it is destroyed.
30 * Also IRRECO must support several button layouts for different devices, so
31 * this object provides that too.
33 * @{
36 /**
37 * @file
38 * Source file of @ref IrrecoButtonLayout.
42 const gchar *irreco_button_layout_default_image =
43 IRRECO_BG_IMAGE_DIR "/" IRRECO_DEFAULT_BG_IMAGE;
44 const GdkColor irreco_button_layout_default_color =
45 {0, 0x946B, 0x946B, 0x946B};
47 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 /* Construction & Destruction */
49 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
51 /**
52 * @name Construction & Destruction
53 * @{
56 IrrecoButtonLayout *irreco_button_layout_create(GtkWidget * container,
57 IrrecoCmdChainManager *manager)
59 IrrecoButtonLayout *irreco_layout;
60 IRRECO_ENTER
62 irreco_layout = g_slice_new0(IrrecoButtonLayout);
63 irreco_layout->name = g_string_new(NULL);
64 irreco_layout->background_image = g_string_new(NULL);
65 irreco_layout->button_array = g_ptr_array_new();
67 if (manager != NULL) {
68 irreco_layout->hardkey_map = irreco_hardkey_map_new(manager);
71 irreco_button_layout_set_bg_color(irreco_layout,
72 &irreco_button_layout_default_color);
73 irreco_button_layout_set_container(irreco_layout, container);
74 IRRECO_RETURN_PTR(irreco_layout);
77 void irreco_button_layout_destroy(IrrecoButtonLayout * irreco_layout)
79 IRRECO_ENTER
80 g_assert(irreco_layout != NULL);
82 irreco_hardkey_map_free(irreco_layout->hardkey_map);
83 irreco_button_layout_destroy_buttons(irreco_layout);
84 g_ptr_array_free(irreco_layout->button_array, TRUE);
85 g_string_free(irreco_layout->name, TRUE);
86 g_string_free(irreco_layout->background_image, TRUE);
87 g_slice_free(IrrecoButtonLayout, irreco_layout);
89 IRRECO_RETURN
92 /** @} */
96 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
97 /* Public Functions */
98 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
101 * @name Public Functions
102 * @{
105 * Set name of layout.
107 * This is basically a way to store the Remote name displayend in the UI.
109 void irreco_button_layout_set_name(IrrecoButtonLayout * irreco_layout,
110 const gchar * name)
112 IRRECO_ENTER
113 g_assert(irreco_layout != NULL);
114 g_string_assign(irreco_layout->name, name);
115 IRRECO_RETURN
119 * Set background style.
121 void irreco_button_layout_set_bg_type(IrrecoButtonLayout * irreco_layout,
122 IrrecoButtonLayoutBgType type)
124 IRRECO_ENTER
125 irreco_layout->background_type = type;
126 IRRECO_RETURN
130 * Set background image of layout.
132 * This is basically a way to store the Remote name displayend in the UI.
134 void irreco_button_layout_set_bg_image(IrrecoButtonLayout * irreco_layout,
135 const gchar * background_image)
137 IRRECO_ENTER
138 g_assert(irreco_layout != NULL);
139 g_string_set_size(irreco_layout->background_image, 0);
140 g_string_append(irreco_layout->background_image, background_image);
141 IRRECO_RETURN
145 * Get background image
147 * Returns: Image location or NULL.
149 const gchar *
150 irreco_button_layout_get_bg_image(IrrecoButtonLayout * irreco_layout)
152 IRRECO_ENTER
153 if (irreco_str_isempty(irreco_layout->background_image->str)) {
154 IRRECO_RETURN_PTR(NULL);
156 IRRECO_RETURN_STR(irreco_layout->background_image->str);
160 * Set background color.
162 void irreco_button_layout_set_bg_color(IrrecoButtonLayout * irreco_layout,
163 const GdkColor * color)
165 IRRECO_ENTER
166 memcpy(&irreco_layout->background_color, color, sizeof(GdkColor));
167 irreco_layout->background_color.pixel = 0;
168 IRRECO_RETURN
172 * Get bg variables according to IrrecoButtonLayout->background_type.
174 void irreco_button_layout_get_bg(IrrecoButtonLayout * irreco_layout,
175 const gchar ** image,
176 const GdkColor ** color)
178 IRRECO_ENTER
180 *image = NULL;
181 *color = NULL;
183 switch (irreco_layout->background_type) {
184 case IRRECO_BACKGROUND_DEFAULT:
185 *image = irreco_button_layout_default_image;
186 *color = &irreco_button_layout_default_color;
187 break;
189 case IRRECO_BACKGROUND_IMAGE:
190 *image = irreco_button_layout_get_bg_image(irreco_layout);
191 case IRRECO_BACKGROUND_COLOR:
192 *color = irreco_button_layout_get_bg_color(irreco_layout);
193 break;
196 IRRECO_RETURN
199 void irreco_button_layout_set_size(IrrecoButtonLayout * irreco_layout,
200 gint width, gint height)
202 IRRECO_ENTER
203 g_assert(irreco_layout != NULL);
205 irreco_layout->width = width;
206 irreco_layout->height = height;
207 IRRECO_DEBUG("Layout size: w%i h%i\n", irreco_layout->width,
208 irreco_layout->height);
209 irreco_button_layout_validate_positions(irreco_layout);
211 IRRECO_RETURN
215 * Set the container where the buttons are shown.
216 * The container should be GtkFixed or GtkLayout.
218 void irreco_button_layout_set_container(IrrecoButtonLayout * irreco_layout,
219 GtkWidget * container)
221 IRRECO_ENTER
222 g_assert(irreco_layout != NULL);
224 if (container == NULL){
225 IRRECO_DEBUG("Container is NULL.\n");
226 }else if (GTK_IS_FIXED(container)) {
227 IRRECO_DEBUG("Container type is GtkFixed.\n");
228 } else if (GTK_IS_LAYOUT(container)) {
229 IRRECO_DEBUG("Container type is GtkLayout.\n");
230 } else {
231 IRRECO_ERROR("Container is not GtkFixed or GtkLayout\n");
234 irreco_button_layout_destroy_widgets(irreco_layout);
235 irreco_layout->container = container;
237 IRRECO_RETURN
241 * Call gtk_fixed_put() or gtk_layout_put() depending on container type.
243 void irreco_button_layout_container_put(IrrecoButtonLayout * irreco_layout,
244 GtkWidget *child_widget,
245 gint x, gint y)
247 IRRECO_ENTER
248 if (irreco_layout->container == NULL) IRRECO_RETURN
249 if (GTK_IS_FIXED(irreco_layout->container)) {
250 gtk_fixed_put(GTK_FIXED(irreco_layout->container),
251 child_widget, x, y);
252 } else if (GTK_IS_LAYOUT(irreco_layout->container)) {
253 gtk_layout_put(GTK_LAYOUT(irreco_layout->container),
254 child_widget, x, y);
257 IRRECO_RETURN
261 * Call gtk_fixed_move() or gtk_layout_move() depending on container type.
263 void irreco_button_layout_container_move(IrrecoButtonLayout * irreco_layout,
264 GtkWidget *child_widget,
265 gint x, gint y)
267 IRRECO_ENTER
268 if (irreco_layout->container == NULL) IRRECO_RETURN
269 if (GTK_IS_FIXED(irreco_layout->container)) {
270 gtk_fixed_move(GTK_FIXED(irreco_layout->container),
271 child_widget, x, y);
272 } else if (GTK_IS_LAYOUT(irreco_layout->container)) {
273 gtk_layout_move(GTK_LAYOUT(irreco_layout->container),
274 child_widget, x, y);
277 IRRECO_RETURN
281 * Destroy widgets and remove callbacks.
283 void irreco_button_layout_reset(IrrecoButtonLayout * irreco_layout)
285 IRRECO_ENTER
286 if (irreco_layout == NULL) IRRECO_RETURN
287 irreco_button_layout_destroy_widgets(irreco_layout);
288 irreco_button_layout_set_motion_callback(irreco_layout, NULL);
289 irreco_button_layout_set_press_callback(irreco_layout, NULL);
290 irreco_button_layout_set_release_callback(irreco_layout, NULL);
291 irreco_button_layout_set_callback_data(irreco_layout, NULL);
292 irreco_button_layout_buttons_up(irreco_layout);
293 IRRECO_RETURN
298 * Foreach frappers. These simply call the obvious function on all IrrecoButtons.
300 void irreco_button_layout_destroy_buttons(IrrecoButtonLayout * irreco_layout)
302 IRRECO_ENTER
303 g_assert(irreco_layout != NULL);
305 IRRECO_PTR_ARRAY_BACKWARDS(irreco_layout->button_array, IrrecoButton *,
306 irreco_button)
307 irreco_button_destroy(irreco_button);
308 IRRECO_PTR_ARRAY_BACKWARDS_END
310 IRRECO_RETURN
312 void irreco_button_layout_destroy_widgets(IrrecoButtonLayout * irreco_layout)
314 IRRECO_ENTER
315 g_assert(irreco_layout != NULL);
317 IRRECO_PTR_ARRAY_BACKWARDS(irreco_layout->button_array, IrrecoButton *,
318 irreco_button)
319 irreco_button_destroy_widget(irreco_button);
320 IRRECO_PTR_ARRAY_BACKWARDS_END
322 IRRECO_RETURN
324 void irreco_button_layout_create_widgets(IrrecoButtonLayout * irreco_layout)
326 IRRECO_ENTER
327 g_assert(irreco_layout != NULL);
329 IRRECO_PTR_ARRAY_FORWARDS(irreco_layout->button_array, IrrecoButton *,
330 irreco_button)
331 irreco_button_create_widget(irreco_button);
332 IRRECO_PTR_ARRAY_BACKWARDS_END
334 IRRECO_RETURN
336 void irreco_button_layout_validate_positions(IrrecoButtonLayout * irreco_layout)
338 gint x, y;
339 IRRECO_ENTER
340 g_assert(irreco_layout != NULL);
342 IRRECO_PTR_ARRAY_BACKWARDS(irreco_layout->button_array, IrrecoButton *,
343 irreco_button)
344 x = irreco_button->x;
345 y = irreco_button->y;
346 irreco_button_move(irreco_button, &x, &y);
347 IRRECO_PTR_ARRAY_BACKWARDS_END
349 IRRECO_RETURN
351 void irreco_button_layout_reindex(IrrecoButtonLayout * irreco_layout)
353 guint index = 0;
354 IRRECO_ENTER
355 g_assert(irreco_layout != NULL);
357 IRRECO_PTR_ARRAY_FORWARDS(irreco_layout->button_array, IrrecoButton *,
358 irreco_button)
359 irreco_button->index = index++;
360 IRRECO_PTR_ARRAY_BACKWARDS_END
362 IRRECO_RETURN
364 void irreco_button_layout_buttons_up(IrrecoButtonLayout * irreco_layout)
366 IRRECO_ENTER
367 g_assert(irreco_layout != NULL);
369 IRRECO_PTR_ARRAY_FORWARDS(irreco_layout->button_array, IrrecoButton *,
370 irreco_button)
371 irreco_button_up(irreco_button);
372 IRRECO_PTR_ARRAY_BACKWARDS_END
374 IRRECO_RETURN
376 /** @} */
377 /** @} */