Merge branch 'master' into svn_merge
[framework/uifw/elementary.git] / src / lib / elm_dayselector.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Dayselector Dayselector
6  * @ingroup Elementary
7  *
8  * DaySelector. 
9  *
10  * Signals that you can add callbacks for are:
11  *
12  * dayselector,changed - This is called whenever the user changes the state of one of the check object. 
13  * 
14  */
15
16 typedef struct _Widget_Data Widget_Data;
17
18 struct _Widget_Data
19 {
20    Evas_Object *parent;
21    Evas_Object *base;
22    Evas_Object *check[7];
23 };
24
25 static const char *widtype = NULL;
26 static void _del_hook(Evas_Object *obj);
27 //TODO:static void _del_pre_hook(Evas_Object *obj);
28 static void _theme_hook(Evas_Object *obj);
29 //static void _sizing_eval(Evas_Object* obj);
30 //static void _dayselector_resize(void *data, Evas *e, Evas_Object *obj, void *event_info);
31 static void _changed_size_hints(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
32
33 static void
34 _del_hook(Evas_Object *obj)
35 {
36    Widget_Data *wd = (Widget_Data *) elm_widget_data_get(obj);
37    if (!wd) return;
38    free(wd);
39 }
40 /*
41 static void
42 _dayselector_resize(void *data, Evas *e, Evas_Object *obj, void *event_info)
43 {
44    _sizing_eval(obj);
45 }
46 */
47 static void
48 _changed_size_hints(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
49 {
50    Evas_Coord w, h;
51    Widget_Data *wd = elm_widget_data_get(obj);
52    if (!wd) return;
53
54    edje_object_size_min_get(wd->base, &w, &h);
55    evas_object_size_hint_min_set(obj, w * elm_scale_get(), h * elm_scale_get());
56 }
57
58 static void 
59 _check_clicked(void *data, Evas_Object *obj, void *event_info __UNUSED__)
60 {
61    static Elm_DaySelector_Day day;
62    Widget_Data* wd = (Widget_Data*) elm_widget_data_get(data);
63    int idx;
64    for (idx = 0; idx< 7; ++idx)
65      {
66         if (obj==wd->check[idx])
67           {
68              day = idx;
69              evas_object_smart_callback_call(data, "dayselector,changed", (void *) &day);
70              return ;
71         }
72      }
73 }
74
75 static void
76 _theme_hook(Evas_Object *obj)
77 {
78    char *day_name[] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat" };
79    char buf[256];
80    int idx;
81    Widget_Data* wd = (Widget_Data*) elm_widget_data_get(obj);
82    if (!wd) return;
83
84    _elm_theme_object_set(obj, wd->base, "dayselector", "base", elm_widget_style_get(obj));
85
86    for ( idx = 0; idx < 7; ++idx )
87      {
88         sprintf(buf, "dayselector/%s_%s", elm_widget_style_get(obj), day_name[idx]);
89         elm_object_style_set(wd->check[idx], buf );
90      }
91 }
92
93 /**
94  * Set the state of given check object.
95  *
96  * @param[in] obj            Dayselector
97  * @param[in] day        day user want to know. 
98  * @param[in] checked    state of the day. Eina_True is checked. 
99  *
100  * @ingroup Dayselector
101  */
102 EAPI void
103 elm_dayselector_check_state_set(Evas_Object *obj, Elm_DaySelector_Day day, Eina_Bool checked)
104 {
105    ELM_CHECK_WIDTYPE(obj, widtype);
106    Widget_Data* wd = (Widget_Data*) elm_widget_data_get(obj);
107    if (!wd) return;
108    elm_check_state_set(wd->check[day], checked);
109 }
110
111 /**
112  * Get the state of given check object.
113  *
114  * @param[in] obj               Dayselector
115  * @param[in] day        day user want to know. 
116  *
117  * @ingroup Dayselector
118  */
119 EAPI Eina_Bool 
120 elm_dayselector_check_state_get(Evas_Object *obj, Elm_DaySelector_Day day)
121 {
122    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
123    Widget_Data* wd = (Widget_Data*) elm_widget_data_get(obj);
124    if (!wd) return EINA_FALSE;
125    return elm_check_state_get(wd->check[day]);
126 }
127 /*
128 static void
129 _sizing_eval(Evas_Object* obj)
130 {
131    Evas_Coord w, h;
132    Evas_Coord min_w, min_h, max_h;
133    Widget_Data *wd = elm_widget_data_get(obj);
134    if (!wd) return;
135
136    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
137    edje_object_size_min_get(wd->base, &min_w, &min_h);
138    edje_object_size_max_get(wd->base, NULL, &max_h);
139
140    fprintf( stderr, "%d %d\n", w, h );
141    if (w < min_w) w = min_w;
142    if (h < min_h) h = min_h;
143    else if (h > max_h) h = max_h;
144
145    evas_object_resize(wd->base, w, h);  
146 }
147 */
148 /**
149  * Add the dayselector.
150  *
151  * @param[in] parent            Parent object.
152  *
153  * @ingroup Dayselector
154  */
155 EAPI Evas_Object *
156 elm_dayselector_add(Evas_Object *parent)
157 {
158    Evas_Object *obj;
159    Evas *e;
160    Widget_Data *wd;
161    int idx;
162    char *label[] = { "S", "M", "T", "W", "T", "F", "S" };
163    char *day_name[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
164    char *style_name[] = { "dayselector/sun_first_sun",
165                           "dayselector/sun_first_mon",
166                           "dayselector/sun_first_tue",
167                           "dayselector/sun_first_wed",
168                           "dayselector/sun_first_thu",
169                           "dayselector/sun_first_fri",
170                           "dayselector/sun_first_sat",
171    };
172    e = evas_object_evas_get(parent);
173    if (!e) return NULL;
174    wd = ELM_NEW(Widget_Data);
175    obj = elm_widget_add(e);
176    ELM_SET_WIDTYPE(widtype, "dayselector");
177    elm_widget_type_set(obj, "dayselector");
178    elm_widget_sub_object_add(parent, obj);
179    elm_widget_data_set(obj, wd);
180    elm_widget_del_hook_set(obj, _del_hook);
181    elm_widget_theme_hook_set(obj, _theme_hook);
182    wd->parent = parent;
183    //Base
184    wd->base = edje_object_add(e);
185    _elm_theme_object_set(obj, wd->base, "dayselector", "base", elm_widget_style_get(obj));
186    elm_object_style_set(wd->base, "dayselector");
187    elm_widget_resize_object_set(obj, wd->base); 
188    //Checks
189    for (idx=0; idx<7; ++idx)
190      {
191         wd->check[idx]=elm_check_add(wd->base);
192         elm_widget_sub_object_add(obj, wd->check[idx]);
193         evas_object_smart_callback_add(wd->check[idx], "changed", _check_clicked, obj);
194         elm_check_label_set(wd->check[idx], label[idx] );
195         edje_object_part_swallow(wd->base, day_name[idx], wd->check[idx]);
196         elm_object_style_set(wd->check[idx], style_name[idx]);
197      }
198    //evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _dayselector_resize, wd);
199    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
200    //_sizing_eval(obj);
201    return obj;
202 }