Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / eail / eail_background.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * @file eail_background.c
22  * @brief EailBackground implementation
23  */
24
25 #include <Elementary.h>
26
27 #include "eail_background.h"
28
29 static void atk_image_interface_init(AtkImageIface *iface);
30
31 /**
32  * @brief EailBackground type definition
33  */
34 G_DEFINE_TYPE_WITH_CODE(EailBackground,
35                         eail_background,
36                         EAIL_TYPE_WIDGET,
37                         G_IMPLEMENT_INTERFACE(ATK_TYPE_IMAGE,
38                                               atk_image_interface_init));
39
40 /* AtkObject */
41 static void eail_background_initialize(AtkObject *obj, gpointer data);
42 static void eail_background_finalize(GObject *object);
43 static AtkAttributeSet *eail_background_get_attributes(AtkObject *obj);
44
45 /* AtkImage */
46 static void eail_background_get_image_position (AtkImage *image,
47                                                 gint *x,
48                                                 gint *y,
49                                                 AtkCoordType coord_type);
50 static const gchar *eail_background_get_image_description(AtkImage *image);
51 static gboolean eail_background_set_image_description(AtkImage *image,
52                                                       const gchar *description);
53 static void eail_background_get_image_size(AtkImage *image,
54                                            gint *width,
55                                            gint *height);
56 const gchar *eail_background_get_image_locale(AtkImage *image);
57
58
59 /*
60  * Implementation of the *AtkObject* interface
61  */
62
63 /**
64  * @brief EailBackground class initializer
65  *
66  * @param klass EailBackgroundClass instance
67  */
68 static void
69 eail_background_class_init(EailBackgroundClass *klass)
70 {
71    AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
72    GObjectClass *object_class = G_OBJECT_CLASS(klass);
73
74    atk_class->initialize = eail_background_initialize;
75    atk_class->get_attributes = eail_background_get_attributes;
76
77    object_class->finalize = eail_background_finalize;
78 }
79
80 /**
81  * @brief EailBackground initializer
82  *
83  * @param obj AtkObject (EailBackground) instance to be initialized
84  * @param data additional data
85  */
86 static void
87 eail_background_initialize(AtkObject *obj, gpointer data)
88 {
89    ATK_OBJECT_CLASS(eail_background_parent_class)->initialize(obj, data);
90
91    obj->role = ATK_ROLE_IMAGE;
92 }
93
94 /**
95  * @brief EailBackground finalizer
96  *
97  * Destroys the object and allocated resources.
98  *
99  * @param object EailBackground instance to be finalized
100  */
101 static void
102 eail_background_finalize(GObject *object)
103 {
104    EailBackground *bg = EAIL_BACKGROUND(object);
105
106    if (bg->description) free(bg->description);
107
108    G_OBJECT_CLASS(eail_background_parent_class)->finalize(object);
109 }
110
111 /**
112  * @brief EailBackground instance initializer
113  *
114  * @param background EailBackground instance
115  */
116 static void
117 eail_background_init(EailBackground *background)
118 {
119    background->description = NULL;
120 }
121
122 /**
123  * @brief Gets obj's attributes set
124  *
125  * The caller must unreference it when it is no longer needed.
126  *
127  * @param obj AtkObject instance
128  * @return AtkAttributeSet containing obj's attributes
129  */
130 static AtkAttributeSet *
131 eail_background_get_attributes(AtkObject *obj)
132 {
133    AtkAttribute *attr;
134    AtkAttributeSet *attributes;
135    Evas_Object *widget;
136    int r, g, b;
137    gchar color[8];
138    const char *mode, *file;
139
140    attributes = ATK_OBJECT_CLASS(eail_background_parent_class)->get_attributes(obj);
141
142    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
143    if (!widget) return attributes;
144
145    elm_bg_file_get(widget, &file, NULL);
146    if (!file)
147      {
148         elm_bg_color_get(widget, &r, &g, &b);
149         g_snprintf(color, sizeof(color), "#%02x%02x%02x", r, g, b);
150         attr = g_new(AtkAttribute, 1);
151         attr->name = g_strdup("color");
152         attr->value = g_strdup(color);
153         attributes = g_slist_append(attributes, attr);
154      }
155    else
156      {
157         attr = g_new(AtkAttribute, 1);
158         attr->name = g_strdup("file");
159         attr->value = g_strdup(file);
160         attributes = g_slist_append(attributes, attr);
161      }
162
163    switch (elm_bg_option_get(widget))
164      {
165        case ELM_BG_OPTION_CENTER:
166            mode = "center";
167            break;
168        case ELM_BG_OPTION_SCALE:
169            mode = "scale";
170            break;
171        case ELM_BG_OPTION_STRETCH:
172            mode = "stretch";
173            break;
174        case ELM_BG_OPTION_TILE:
175            mode = "tile";
176            break;
177        default:
178            mode = "";
179            break;
180      }
181
182    attr = g_new(AtkAttribute, 1);
183    attr->name = g_strdup("display mode");
184    attr->value = g_strdup(mode);
185    attributes = g_slist_append(attributes, attr);
186
187    return attributes;
188 }
189
190
191 /*
192  * AtkImage interface implementation
193  */
194
195 /**
196  * @brief AtkImage interface initializer
197  *
198  * @param iface AtkImageIface instance
199  */
200 static void
201 atk_image_interface_init(AtkImageIface *iface)
202 {
203    g_return_if_fail(iface);
204
205    iface->get_image_description = eail_background_get_image_description;
206    iface->set_image_description = eail_background_set_image_description;
207    iface->get_image_size = eail_background_get_image_size;
208    iface->get_image_position = eail_background_get_image_position;
209    iface->get_image_locale = eail_background_get_image_locale;
210 }
211
212 /**
213  * @brief Gets background's image position.
214  *
215  * Position is in the form of a point specifying background top-left corner.
216  *
217  * @param image AtkImage instance
218  * @param [out] x x coordinate or -1 if value cannot be obtained
219  * @param [out] y y coordinate or -1 if value cannot be obtained
220  * @param coord_type specifies whether the coordinates are relative to the screen or to the component's top level window
221  */
222 static void
223 eail_background_get_image_position(AtkImage *image,
224                                    gint *x,
225                                    gint *y,
226                                    AtkCoordType coord_type)
227 {
228    atk_component_get_position(ATK_COMPONENT(image), x, y, coord_type);
229 }
230
231 /**
232  * @brief Gets background image's description
233  *
234  * @param image AtkImage instance
235  * @return string representing image's description
236  */
237 static const gchar *
238 eail_background_get_image_description(AtkImage *image)
239 {
240    EailBackground *bg;
241    const gchar *desc;
242
243    g_return_val_if_fail(EAIL_IS_BACKGROUND(image), NULL);
244
245    bg = EAIL_BACKGROUND(image);
246    if (!bg) return NULL;
247
248    desc = bg->description;
249
250    return desc;
251 }
252
253 /**
254  * @brief Sets background image's description
255  *
256  * @param image AtkImage instance
257  * @param description new image description
258  * @return TRUE if description was set successfully, FALSE otherwise
259  */
260 static gboolean
261 eail_background_set_image_description(AtkImage *image,
262                                       const gchar *description)
263 {
264    EailBackground *bg;
265
266    g_return_val_if_fail(EAIL_IS_BACKGROUND(image), FALSE);
267
268    bg = EAIL_BACKGROUND(image);
269    if (!bg) return FALSE;
270
271    if (bg->description)
272      free(bg->description);
273
274    bg->description = g_strdup(description);
275    if (!bg->description) return FALSE;
276
277    return TRUE;
278 }
279
280 /**
281  * @brief Gets background's widget size
282  *
283  * @param image AtkImage instance
284  * @param [out] width background's width or -1 if value cannot be obtained
285  * @param [out] height background's height or -1 if value cannot be obtained
286  */
287 static void
288 eail_background_get_image_size(AtkImage *image,
289                                gint *width,
290                                gint *height)
291 {
292    EailBackground *bg;
293    Evas_Object *widget;
294
295    bg = EAIL_BACKGROUND(image);
296    if (!bg)
297      *width = *height = -1;
298
299    widget = eail_widget_get_widget(EAIL_WIDGET(image));
300    if (!widget)
301      return;
302
303    evas_object_geometry_get(widget, NULL, NULL, width, height);
304 }
305
306 /**
307  * @brief Gets background's locale (LC_MESSAGES variable)
308  *
309  * @param image AtkImage instance
310  * @return string corresponding to the POSIX LC_MESSAGES
311  * locale used by the image description, or NULL if
312  * the image does not specify a locale
313  *
314  */
315 const gchar *
316 eail_background_get_image_locale(AtkImage *image)
317 {
318     return setlocale(LC_MESSAGES, NULL);
319 }