[SLP Merge] 2011/7/14 12:00PM
[framework/uifw/elementary.git] / src / edje_externals / elm_toggle.c
1 #include <string.h>
2
3 #include "private.h"
4
5 typedef struct _Elm_Params_Toggle
6 {
7    Elm_Params base;
8    const char *label;
9    Evas_Object *icon;
10    const char *on, *off;
11    Eina_Bool state:1;
12    Eina_Bool state_exists:1;
13 } Elm_Params_Toggle;
14
15 static void
16 external_toggle_state_set(void *data __UNUSED__, Evas_Object *obj, const void *from_params, const void *to_params, float pos __UNUSED__)
17 {
18    const Elm_Params_Toggle *p;
19
20    if (to_params) p = to_params;
21    else if (from_params) p = from_params;
22    else return;
23
24    if (p->label)
25      elm_object_text_set(obj, p->label);
26    if (p->icon)
27      elm_toggle_icon_set(obj, p->icon);
28
29    if ((p->on) && (p->off))
30      elm_toggle_states_labels_set(obj, p->on, p->off);
31    else if ((p->on) || (p->off))
32      {
33         const char *on, *off;
34         elm_toggle_states_labels_get(obj, &on, &off);
35         if (p->on)
36           elm_toggle_states_labels_set(obj, p->on, off);
37         else
38           elm_toggle_states_labels_set(obj, on, p->off);
39      }
40
41    if (p->state_exists)
42      elm_toggle_state_set(obj, p->state);
43 }
44
45 static Eina_Bool
46 external_toggle_param_set(void *data __UNUSED__, Evas_Object *obj, const Edje_External_Param *param)
47 {
48    if (!strcmp(param->name, "label"))
49      {
50         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
51           {
52              elm_object_text_set(obj, param->s);
53              return EINA_TRUE;
54           }
55      }
56    else if (!strcmp(param->name, "icon"))
57      {
58         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
59           {
60              Evas_Object *icon = external_common_param_icon_get(obj, param);
61              if ((strcmp(param->s, "")) && (!icon)) return EINA_FALSE;
62              elm_toggle_icon_set(obj, icon);
63              return EINA_TRUE;
64           }
65      }
66    else if (!strcmp(param->name, "label on"))
67      {
68         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
69           {
70              const char *on, *off;
71              elm_toggle_states_labels_get(obj, &on, &off);
72              elm_toggle_states_labels_set(obj, param->s, off);
73              return EINA_TRUE;
74           }
75      }
76    else if (!strcmp(param->name, "label off"))
77      {
78         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
79           {
80              const char *on, *off;
81              elm_toggle_states_labels_get(obj, &on, &off);
82              elm_toggle_states_labels_set(obj, on, param->s);
83              return EINA_TRUE;
84           }
85      }
86    else if (!strcmp(param->name, "state"))
87      {
88         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_BOOL)
89           {
90              elm_toggle_state_set(obj, param->i);
91              return EINA_TRUE;
92           }
93      }
94
95    ERR("unknown parameter '%s' of type '%s'",
96        param->name, edje_external_param_type_str(param->type));
97
98    return EINA_FALSE;
99 }
100
101 static Eina_Bool
102 external_toggle_param_get(void *data __UNUSED__, const Evas_Object *obj, Edje_External_Param *param)
103 {
104    if (!strcmp(param->name, "label"))
105      {
106         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
107           {
108              param->s = elm_object_text_get(obj);
109              return EINA_TRUE;
110           }
111      }
112    else if (!strcmp(param->name, "icon"))
113      {
114         /* not easy to get icon name back from live object */
115         return EINA_FALSE;
116      }
117    else if (!strcmp(param->name, "label on"))
118      {
119         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
120           {
121              const char *on, *off;
122              elm_toggle_states_labels_get(obj, &on, &off);
123              param->s = on;
124              return EINA_TRUE;
125           }
126      }
127    else if (!strcmp(param->name, "label off"))
128      {
129         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
130           {
131              const char *on, *off;
132              elm_toggle_states_labels_get(obj, &on, &off);
133              param->s = off;
134              return EINA_TRUE;
135           }
136      }
137    else if (!strcmp(param->name, "state"))
138      {
139         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_BOOL)
140           {
141              param->i = elm_toggle_state_get(obj);
142              return EINA_TRUE;
143           }
144      }
145
146    ERR("unknown parameter '%s' of type '%s'",
147        param->name, edje_external_param_type_str(param->type));
148
149    return EINA_FALSE;
150 }
151
152 static void *
153 external_toggle_params_parse(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const Eina_List *params)
154 {
155    Elm_Params_Toggle *mem;
156    Edje_External_Param *param;
157    const Eina_List *l;
158
159    mem = calloc(1, sizeof(Elm_Params_Toggle));
160    if (!mem)
161      return NULL;
162
163    external_common_icon_param_parse(&mem->icon, obj, params);
164
165    EINA_LIST_FOREACH(params, l, param)
166      {
167         if (!strcmp(param->name, "state")) {
168            mem->state = param->i;
169            mem->state_exists = EINA_TRUE;
170           }
171         else if (!strcmp(param->name, "label on"))
172           mem->on = eina_stringshare_add(param->s);
173         else if (!strcmp(param->name, "label off"))
174           mem->off = eina_stringshare_add(param->s);
175      }
176
177    return mem;
178 }
179
180 static Evas_Object *external_toggle_content_get(void *data __UNUSED__,
181                 const Evas_Object *obj __UNUSED__, const char *content __UNUSED__)
182 {
183         ERR("No content.");
184         return NULL;
185 }
186
187 static void
188 external_toggle_params_free(void *params)
189 {
190    Elm_Params_Toggle *mem = params;
191
192    if (mem->on)
193      eina_stringshare_del(mem->on);
194    if (mem->off)
195      eina_stringshare_del(mem->off);
196    if (mem->label)
197       eina_stringshare_del(mem->label);
198    free(params);
199 }
200
201 static Edje_External_Param_Info external_toggle_params[] = {
202    DEFINE_EXTERNAL_COMMON_PARAMS,
203    EDJE_EXTERNAL_PARAM_INFO_STRING("label"),
204    EDJE_EXTERNAL_PARAM_INFO_STRING("icon"),
205    EDJE_EXTERNAL_PARAM_INFO_STRING_DEFAULT("label on", "ON"),
206    EDJE_EXTERNAL_PARAM_INFO_STRING_DEFAULT("label off", "OFF"),
207    EDJE_EXTERNAL_PARAM_INFO_BOOL("state"),
208    EDJE_EXTERNAL_PARAM_INFO_SENTINEL
209 };
210
211 DEFINE_EXTERNAL_ICON_ADD(toggle, "toggle")
212 DEFINE_EXTERNAL_TYPE_SIMPLE(toggle, "Toggle")