SVN Merging for button, radio, check and toggle
[framework/uifw/elementary.git] / src / examples / flipselector_example.c
1 /**
2  * Simple Elementary's <b>flip selector widget</b> example, illustrating its
3  * usage and API.
4  *
5  * See stdout/stderr for output. Compile with:
6  *
7  * @verbatim
8  * gcc -g `pkg-config --cflags --libs elementary` flipselector_example.c -o flipselector_example
9  * @endverbatim
10  */
11
12 #include <Elementary.h>
13 #ifdef HAVE_CONFIG_H
14 # include "elementary_config.h"
15 #else
16 # define __UNUSED__
17 #endif
18
19 static const char *commands = \
20   "commands are:\n"
21   "\tn - flip to next item\n"
22   "\tp - flip to previous item\n"
23   "\tf - print first item's label\n"
24   "\tl - print last item's label\n"
25   "\ts - print selected item's label\n"
26   "\th - print help\n";
27
28 static void
29 _on_done(void        *data __UNUSED__,
30          Evas_Object *obj __UNUSED__,
31          void        *event_info __UNUSED__)
32 {
33    elm_exit();
34 }
35
36 void /* unselect the item shown in the flip selector */
37 _unsel_cb(void        *data,
38           Evas_Object *obj __UNUSED__,
39           void        *event_info __UNUSED__)
40 {
41    Elm_Object_Item *it;
42    Evas_Object *fp = data;
43
44    it = elm_flipselector_selected_item_get(fp);
45    elm_flipselector_item_selected_set(it, EINA_FALSE);
46 }
47
48 void /* delete the item shown in the flip selector */
49 _del_cb(void        *data,
50           Evas_Object *obj __UNUSED__,
51           void        *event_info __UNUSED__)
52 {
53    Elm_Object_Item *it;
54    Evas_Object *fp = data;
55
56    it = elm_flipselector_selected_item_get(fp);
57    if (it) elm_object_item_del(it);
58 }
59
60 void /* underflow callback */
61 _underflow_cb(void        *data __UNUSED__,
62               Evas_Object *obj __UNUSED__,
63               void        *event_info __UNUSED__)
64 {
65    fprintf(stdout, "Underflow!\n");
66 }
67
68 void /* overflow callback */
69 _overflow_cb(void        *data __UNUSED__,
70              Evas_Object *obj __UNUSED__,
71              void        *event_info __UNUSED__)
72 {
73    fprintf(stdout, "Overflow!\n");
74 }
75
76 static void
77 _on_keydown(void              *data,
78             Evas_Object       *object __UNUSED__,
79             Evas_Object       *src __UNUSED__,
80             Evas_Callback_Type type,
81             void              *event_info)
82 {
83    Evas_Object *fs = data;
84    Evas_Event_Key_Down *ev = event_info;
85
86    if (type != EVAS_CALLBACK_KEY_DOWN) return;
87
88    if (strcmp(ev->keyname, "h") == 0) /* print help */
89      {
90         fprintf(stdout, commands);
91         return;
92      }
93
94    if (strcmp(ev->keyname, "n") == 0) /* flip to next item */
95      {
96          elm_flipselector_flip_next(fs);
97
98         fprintf(stdout, "Flipping to next item\n");
99
100         return;
101      }
102
103    if (strcmp(ev->keyname, "p") == 0) /* flip to previous item */
104      {
105          elm_flipselector_flip_prev(fs);
106
107         fprintf(stdout, "Flipping to previous item\n");
108
109         return;
110      }
111
112    if (strcmp(ev->keyname, "f") == 0) /* print first item's label */
113      {
114          Elm_Object_Item *it;
115
116          it = elm_flipselector_first_item_get(fs);
117
118          fprintf(stdout, "Flip selector's first item is: %s\n", it ?
119                  elm_object_item_text_get(it) : "none");
120
121         return;
122      }
123
124    if (strcmp(ev->keyname, "l") == 0) /* print last item's label */
125      {
126          Elm_Object_Item *it;
127
128          it = elm_flipselector_last_item_get(fs);
129
130          fprintf(stdout, "Flip selector's last item is: %s\n", it ?
131                  elm_object_item_text_get(it) : "none");
132
133         return;
134      }
135
136    if (strcmp(ev->keyname, "s") == 0) /* print selected item's label */
137      {
138          Elm_Object_Item *it;
139
140          it = elm_flipselector_selected_item_get(fs);
141
142          fprintf(stdout, "Flip selector's selected item is: %s\n", it ?
143                  elm_object_item_text_get(it) : "none");
144
145         return;
146      }
147 }
148
149 EAPI_MAIN int
150 elm_main(int argc __UNUSED__, char **argv __UNUSED__)
151 {
152    unsigned int i;
153    Evas_Object *win, *bg, *bx, *fp, *bt;
154    static const char *lbl[] =
155    {
156       "Elementary",
157       "Evas",
158       "Eina",
159       "Edje",
160       "Eet",
161       "Ecore",
162       "Efreet",
163       "Edbus"
164    };
165
166    win = elm_win_add(NULL, "flipselector", ELM_WIN_BASIC);
167    elm_win_title_set(win, "Flip Selector Example");
168    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
169
170    bg = elm_bg_add(win);
171    elm_win_resize_object_add(win, bg);
172    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
173    evas_object_show(bg);
174
175    bx = elm_box_add(win);
176    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
177    elm_win_resize_object_add(win, bx);
178    evas_object_show(bx);
179
180    fp = elm_flipselector_add(win);
181    evas_object_size_hint_weight_set(fp, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
182    evas_object_smart_callback_add(fp, "underflowed", _overflow_cb, NULL);
183    evas_object_smart_callback_add(fp, "overflowed", _underflow_cb, NULL);
184    for (i = 0; i < sizeof(lbl) / sizeof(lbl[0]); i++)
185      elm_flipselector_item_append(fp, lbl[i], NULL, NULL);
186    elm_box_pack_end(bx, fp);
187    evas_object_show(fp);
188
189    bt = elm_button_add(win);
190    elm_object_text_set(bt, "Unselect item");
191    evas_object_smart_callback_add(bt, "clicked", _unsel_cb, fp);
192    elm_box_pack_end(bx, bt);
193    evas_object_show(bt);
194
195    bt = elm_button_add(win);
196    elm_object_text_set(bt, "Delete item");
197    evas_object_smart_callback_add(bt, "clicked", _del_cb, fp);
198    elm_box_pack_end(bx, bt);
199    evas_object_show(bt);
200
201    elm_object_event_callback_add(win, (Elm_Event_Cb)_on_keydown, fp);
202
203    evas_object_show(win);
204
205    fprintf(stdout, commands);
206    elm_run();
207    return 0;
208 }
209 ELM_MAIN()