elementary - modified content_set/get/unset part names to be simple names.
[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_object_content_part_set(obj, "icon", p->icon);
28
29    if (p->on)
30      elm_object_text_part_set(obj, "on", p->on);
31    if (p->off)
32      elm_object_text_part_set(obj, "off", p->off);
33
34    if (p->state_exists)
35      elm_toggle_state_set(obj, p->state);
36 }
37
38 static Eina_Bool
39 external_toggle_param_set(void *data __UNUSED__, Evas_Object *obj, const Edje_External_Param *param)
40 {
41    if (!strcmp(param->name, "label"))
42      {
43         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
44           {
45              elm_object_text_set(obj, param->s);
46              return EINA_TRUE;
47           }
48      }
49    else if (!strcmp(param->name, "icon"))
50      {
51         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
52           {
53              Evas_Object *icon = external_common_param_icon_get(obj, param);
54              if ((strcmp(param->s, "")) && (!icon)) return EINA_FALSE;
55              elm_object_content_part_set(obj, "icon", icon);
56              return EINA_TRUE;
57           }
58      }
59    else if (!strcmp(param->name, "label on"))
60      {
61         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
62           {
63              elm_object_text_part_set(obj, "on", param->s);
64              return EINA_TRUE;
65           }
66      }
67    else if (!strcmp(param->name, "label off"))
68      {
69         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
70           {
71              elm_object_text_part_set(obj, "off", param->s);
72              return EINA_TRUE;
73           }
74      }
75    else if (!strcmp(param->name, "state"))
76      {
77         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_BOOL)
78           {
79              elm_toggle_state_set(obj, param->i);
80              return EINA_TRUE;
81           }
82      }
83
84    ERR("unknown parameter '%s' of type '%s'",
85        param->name, edje_external_param_type_str(param->type));
86
87    return EINA_FALSE;
88 }
89
90 static Eina_Bool
91 external_toggle_param_get(void *data __UNUSED__, const Evas_Object *obj, Edje_External_Param *param)
92 {
93    if (!strcmp(param->name, "label"))
94      {
95         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
96           {
97              param->s = elm_object_text_get(obj);
98              return EINA_TRUE;
99           }
100      }
101    else if (!strcmp(param->name, "icon"))
102      {
103         /* not easy to get icon name back from live object */
104         return EINA_FALSE;
105      }
106    else if (!strcmp(param->name, "label on"))
107      {
108         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
109           {
110              param->s = elm_object_text_part_get(obj, "on");
111              return EINA_TRUE;
112           }
113      }
114    else if (!strcmp(param->name, "label off"))
115      {
116         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
117           {
118              param->s = elm_object_text_part_get(obj, "off");
119              return EINA_TRUE;
120           }
121      }
122    else if (!strcmp(param->name, "state"))
123      {
124         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_BOOL)
125           {
126              param->i = elm_toggle_state_get(obj);
127              return EINA_TRUE;
128           }
129      }
130
131    ERR("unknown parameter '%s' of type '%s'",
132        param->name, edje_external_param_type_str(param->type));
133
134    return EINA_FALSE;
135 }
136
137 static void *
138 external_toggle_params_parse(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const Eina_List *params)
139 {
140    Elm_Params_Toggle *mem;
141    Edje_External_Param *param;
142    const Eina_List *l;
143
144    mem = calloc(1, sizeof(Elm_Params_Toggle));
145    if (!mem)
146      return NULL;
147
148    external_common_icon_param_parse(&mem->icon, obj, params);
149
150    EINA_LIST_FOREACH(params, l, param)
151      {
152         if (!strcmp(param->name, "state")) {
153            mem->state = param->i;
154            mem->state_exists = EINA_TRUE;
155           }
156         else if (!strcmp(param->name, "label on"))
157           mem->on = eina_stringshare_add(param->s);
158         else if (!strcmp(param->name, "label off"))
159           mem->off = eina_stringshare_add(param->s);
160      }
161
162    return mem;
163 }
164
165 static Evas_Object *external_toggle_content_get(void *data __UNUSED__,
166                 const Evas_Object *obj __UNUSED__, const char *content __UNUSED__)
167 {
168         ERR("No content.");
169         return NULL;
170 }
171
172 static void
173 external_toggle_params_free(void *params)
174 {
175    Elm_Params_Toggle *mem = params;
176
177    if (mem->on)
178      eina_stringshare_del(mem->on);
179    if (mem->off)
180      eina_stringshare_del(mem->off);
181    if (mem->label)
182       eina_stringshare_del(mem->label);
183    free(params);
184 }
185
186 static Edje_External_Param_Info external_toggle_params[] = {
187    DEFINE_EXTERNAL_COMMON_PARAMS,
188    EDJE_EXTERNAL_PARAM_INFO_STRING("label"),
189    EDJE_EXTERNAL_PARAM_INFO_STRING("icon"),
190    EDJE_EXTERNAL_PARAM_INFO_STRING_DEFAULT("label on", "ON"),
191    EDJE_EXTERNAL_PARAM_INFO_STRING_DEFAULT("label off", "OFF"),
192    EDJE_EXTERNAL_PARAM_INFO_BOOL("state"),
193    EDJE_EXTERNAL_PARAM_INFO_SENTINEL
194 };
195
196 DEFINE_EXTERNAL_ICON_ADD(toggle, "toggle")
197 DEFINE_EXTERNAL_TYPE_SIMPLE(toggle, "Toggle")