tizen 2.4 release
[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 void /* unselect the item shown in the flip selector */
24 _unsel_cb(void        *data,
25           Evas_Object *obj,
26           void        *event_info)
27 {
28    Elm_Object_Item *it;
29    Evas_Object *fp = data;
30
31    it = elm_flipselector_selected_item_get(fp);
32    elm_flipselector_item_selected_set(it, EINA_FALSE);
33 }
34
35 void /* delete the item shown in the flip selector */
36 _del_cb(void        *data,
37         Evas_Object *obj,
38         void        *event_info)
39 {
40    Elm_Object_Item *it;
41    Evas_Object *fp = data;
42
43    it = elm_flipselector_selected_item_get(fp);
44    if (it) elm_object_item_del(it);
45 }
46
47 void /* underflow callback */
48 _underflow_cb(void        *data,
49               Evas_Object *obj,
50               void        *event_info)
51 {
52    fprintf(stdout, "Underflow!\n");
53 }
54
55 void /* overflow callback */
56 _overflow_cb(void        *data,
57              Evas_Object *obj,
58              void        *event_info)
59 {
60    fprintf(stdout, "Overflow!\n");
61 }
62
63 static Eina_Bool
64 _on_keydown(void              *data,
65             Evas_Object       *object,
66             Evas_Object       *src,
67             Evas_Callback_Type type,
68             void              *event_info)
69 {
70    Evas_Object *fs = data;
71    Evas_Event_Key_Down *ev = event_info;
72
73    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
74
75    if (strcmp(ev->keyname, "h") == 0) /* print help */
76      {
77         fprintf(stdout, "%s", commands);
78      }
79    else if (strcmp(ev->keyname, "n") == 0) /* flip to next item */
80      {
81         elm_flipselector_flip_next(fs);
82
83         fprintf(stdout, "Flipping to next item\n");
84      }
85    else if (strcmp(ev->keyname, "p") == 0) /* flip to previous item */
86      {
87         elm_flipselector_flip_prev(fs);
88
89         fprintf(stdout, "Flipping to previous item\n");
90      }
91    else if (strcmp(ev->keyname, "f") == 0) /* print first item's label */
92      {
93         Elm_Object_Item *it;
94
95         it = elm_flipselector_first_item_get(fs);
96
97         fprintf(stdout, "Flip selector's first item is: %s\n", it ?
98                 elm_object_item_text_get(it) : "none");
99      }
100    else if (strcmp(ev->keyname, "l") == 0) /* print last item's label */
101      {
102         Elm_Object_Item *it;
103
104         it = elm_flipselector_last_item_get(fs);
105
106         fprintf(stdout, "Flip selector's last item is: %s\n", it ?
107                 elm_object_item_text_get(it) : "none");
108      }
109    else if (strcmp(ev->keyname, "s") == 0) /* print selected item's label */
110      {
111         Elm_Object_Item *it;
112
113         it = elm_flipselector_selected_item_get(fs);
114
115         fprintf(stdout, "Flip selector's selected item is: %s\n", it ?
116                 elm_object_item_text_get(it) : "none");
117      }
118    else
119      return EINA_FALSE;
120
121    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
122    return EINA_TRUE;
123 }
124
125 EAPI_MAIN int
126 elm_main(int argc, char **argv)
127 {
128    unsigned int i;
129    Evas_Object *win, *bx, *fp, *bt;
130    static const char *lbl[] =
131      {
132         "Elementary",
133         "Evas",
134         "Eina",
135         "Edje",
136         "Eet",
137         "Ecore",
138         "Efreet",
139         "Eldbus"
140      };
141
142    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
143
144    win = elm_win_util_standard_add("flipselector", "Flip Selector Example");
145    elm_win_autodel_set(win, EINA_TRUE);
146
147    bx = elm_box_add(win);
148    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
149    elm_win_resize_object_add(win, bx);
150    evas_object_show(bx);
151
152    fp = elm_flipselector_add(win);
153    evas_object_size_hint_weight_set(fp, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
154    evas_object_smart_callback_add(fp, "underflowed", _overflow_cb, NULL);
155    evas_object_smart_callback_add(fp, "overflowed", _underflow_cb, NULL);
156    for (i = 0; i < sizeof(lbl) / sizeof(lbl[0]); i++)
157      elm_flipselector_item_append(fp, lbl[i], NULL, NULL);
158    elm_box_pack_end(bx, fp);
159    evas_object_show(fp);
160
161    bt = elm_button_add(win);
162    elm_object_text_set(bt, "Unselect item");
163    evas_object_smart_callback_add(bt, "clicked", _unsel_cb, fp);
164    elm_box_pack_end(bx, bt);
165    evas_object_show(bt);
166
167    bt = elm_button_add(win);
168    elm_object_text_set(bt, "Delete item");
169    evas_object_smart_callback_add(bt, "clicked", _del_cb, fp);
170    elm_box_pack_end(bx, bt);
171    evas_object_show(bt);
172
173    elm_object_event_callback_add(win, _on_keydown, fp);
174
175    evas_object_show(win);
176
177    fprintf(stdout, "%s", commands);
178    elm_run();
179
180    return 0;
181 }
182 ELM_MAIN()