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"
23 * @addtogroup IrrecoButtonLayout
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.
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
52 * @name Construction & Destruction
56 IrrecoButtonLayout
*irreco_button_layout_create(GtkWidget
* container
,
57 IrrecoCmdChainManager
*manager
)
59 IrrecoButtonLayout
*irreco_layout
;
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
)
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
);
96 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
97 /* Public Functions */
98 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
101 * @name Public Functions
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
,
113 g_assert(irreco_layout
!= NULL
);
114 g_string_assign(irreco_layout
->name
, name
);
119 * Set background style.
121 void irreco_button_layout_set_bg_type(IrrecoButtonLayout
* irreco_layout
,
122 IrrecoButtonLayoutBgType type
)
125 irreco_layout
->background_type
= type
;
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
)
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
);
145 * Get background image
147 * Returns: Image location or NULL.
150 irreco_button_layout_get_bg_image(IrrecoButtonLayout
* irreco_layout
)
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
)
166 memcpy(&irreco_layout
->background_color
, color
, sizeof(GdkColor
));
167 irreco_layout
->background_color
.pixel
= 0;
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
)
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
;
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
);
199 void irreco_button_layout_set_size(IrrecoButtonLayout
* irreco_layout
,
200 gint width
, gint height
)
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
);
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
)
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");
231 IRRECO_ERROR("Container is not GtkFixed or GtkLayout\n");
234 irreco_button_layout_destroy_widgets(irreco_layout
);
235 irreco_layout
->container
= container
;
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
,
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
),
252 } else if (GTK_IS_LAYOUT(irreco_layout
->container
)) {
253 gtk_layout_put(GTK_LAYOUT(irreco_layout
->container
),
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
,
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
),
272 } else if (GTK_IS_LAYOUT(irreco_layout
->container
)) {
273 gtk_layout_move(GTK_LAYOUT(irreco_layout
->container
),
281 * Destroy widgets and remove callbacks.
283 void irreco_button_layout_reset(IrrecoButtonLayout
* irreco_layout
)
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
);
298 * Foreach frappers. These simply call the obvious function on all IrrecoButtons.
300 void irreco_button_layout_destroy_buttons(IrrecoButtonLayout
* irreco_layout
)
303 g_assert(irreco_layout
!= NULL
);
305 IRRECO_PTR_ARRAY_BACKWARDS(irreco_layout
->button_array
, IrrecoButton
*,
307 irreco_button_destroy(irreco_button
);
308 IRRECO_PTR_ARRAY_BACKWARDS_END
312 void irreco_button_layout_destroy_widgets(IrrecoButtonLayout
* irreco_layout
)
315 g_assert(irreco_layout
!= NULL
);
317 IRRECO_PTR_ARRAY_BACKWARDS(irreco_layout
->button_array
, IrrecoButton
*,
319 irreco_button_destroy_widget(irreco_button
);
320 IRRECO_PTR_ARRAY_BACKWARDS_END
324 void irreco_button_layout_create_widgets(IrrecoButtonLayout
* irreco_layout
)
327 g_assert(irreco_layout
!= NULL
);
329 IRRECO_PTR_ARRAY_FORWARDS(irreco_layout
->button_array
, IrrecoButton
*,
331 irreco_button_create_widget(irreco_button
);
332 IRRECO_PTR_ARRAY_BACKWARDS_END
336 void irreco_button_layout_validate_positions(IrrecoButtonLayout
* irreco_layout
)
340 g_assert(irreco_layout
!= NULL
);
342 IRRECO_PTR_ARRAY_BACKWARDS(irreco_layout
->button_array
, IrrecoButton
*,
344 x
= irreco_button
->x
;
345 y
= irreco_button
->y
;
346 irreco_button_move(irreco_button
, &x
, &y
);
347 IRRECO_PTR_ARRAY_BACKWARDS_END
351 void irreco_button_layout_reindex(IrrecoButtonLayout
* irreco_layout
)
355 g_assert(irreco_layout
!= NULL
);
357 IRRECO_PTR_ARRAY_FORWARDS(irreco_layout
->button_array
, IrrecoButton
*,
359 irreco_button
->index
= index
++;
360 IRRECO_PTR_ARRAY_BACKWARDS_END
364 void irreco_button_layout_buttons_up(IrrecoButtonLayout
* irreco_layout
)
367 g_assert(irreco_layout
!= NULL
);
369 IRRECO_PTR_ARRAY_FORWARDS(irreco_layout
->button_array
, IrrecoButton
*,
371 irreco_button_up(irreco_button
);
372 IRRECO_PTR_ARRAY_BACKWARDS_END