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