tizen 2.4 release
[framework/uifw/elementary.git] / src / examples / transit_example_03.c
1 //Compile with:
2 //gcc -o transit_example_03 transit_example_03.c `pkg-config --cflags --libs elementary` -DDATA_DIR="\"<directory>\""
3 //where directory is the a path where images/icon_07.png can be found.
4
5 #include <Elementary.h>
6
7 /* structure to hold context for many callbacks */
8 struct Context
9 {
10    Eina_Bool events_enabled;
11    Eina_Bool auto_reverse;
12    Eina_Bool final_state_keep;
13    int repeat_times;
14    Elm_Transit_Tween_Mode tween_mode;
15    Evas_Object *obj;
16 };
17
18 static void
19 _transit_translation(Elm_Transit *trans)
20 {
21    /* considering the original position (x0, y0), moves the object from
22     * (x0 - 20, y0 - 50) to (x0 + 70, y0 + 150) */
23    elm_transit_effect_translation_add(trans, -20, -50, 70, 150);
24 }
25
26 static void
27 _transit_color(Elm_Transit *trans)
28 {
29    /* changes the object color from 100, 255, 100, 255 to
30     * 40, 10, 40, 50 */
31    elm_transit_effect_color_add(trans, 100, 255, 100, 255, 40, 10, 40, 50);
32 }
33
34 static void
35 _transit_rotation(Elm_Transit *trans)
36 {
37    /* rotates the object from its original angle to 135 degrees to the right */
38    elm_transit_effect_rotation_add(trans, 0.0, 135.0);
39 }
40
41 static void
42 _transit_wipe(Elm_Transit *trans)
43 {
44    /* hide the object clipping it from the left to the right */
45    elm_transit_effect_wipe_add(trans,
46                                ELM_TRANSIT_EFFECT_WIPE_TYPE_HIDE,
47                                ELM_TRANSIT_EFFECT_WIPE_DIR_RIGHT);
48 }
49
50 static void
51 _transit_zoom(Elm_Transit *trans)
52 {
53    /* zoom the object from its original size to 2x */
54    elm_transit_effect_zoom_add(trans, 1.0, 2.0);
55 }
56
57 static void
58 _transit_resizing(Elm_Transit *trans)
59 {
60    /* resize the object from 250x100 to 400x160 */
61    elm_transit_effect_resizing_add(trans, 250, 100, 400, 160);
62 }
63
64 /* helper structure that will hold the transit checkboxes string, callbacks
65  * and checked statuses */
66 static struct {
67      const char *label;
68      void (*transition_add_cb)(Elm_Transit *);
69      Eina_Bool checked;
70 } _transitions[] = {
71        { "Translation", _transit_translation, EINA_FALSE },
72        { "Color", _transit_color, EINA_FALSE },
73        { "Rotation", _transit_rotation, EINA_FALSE },
74        { "Wipe", _transit_wipe, EINA_FALSE },
75        { "Zoom", _transit_zoom, EINA_FALSE },
76        { "Resizing", _transit_resizing, EINA_FALSE },
77        { NULL, NULL, EINA_FALSE }
78 };
79
80 /* add a checkbox to the box with the given label, and uses the checked
81  * pointer as state_pointer to this checkbox */
82 static void
83 _checkbox_transition_add(Evas_Object *box, const char *label, Eina_Bool *checked)
84 {
85    Evas_Object *check = elm_check_add(elm_object_parent_widget_get(box));
86    evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
87    evas_object_size_hint_align_set(check, 0.0, 0.0);
88    elm_object_text_set(check, label);
89    elm_check_state_pointer_set(check, checked);
90    elm_box_pack_end(box, check);
91    evas_object_show(check);
92 }
93
94 static void
95 _transit_start(void *data, Evas_Object *o, void *event_info)
96 {
97    Elm_Transit *trans = NULL;
98    int i;
99    struct Context *ctxt = data;
100    Evas_Object *obj = ctxt->obj; // the object on which the transition will be
101                                  // applied
102
103    // FIXME: Should check if there's another transit going before starting a new
104    // one
105
106    /* initialization: create the transition and add the object to it */
107    trans = elm_transit_add();
108    elm_transit_object_add(trans, obj);
109
110    /* from our helper structure and array, check if the specified transition is
111     * checked and use its callback to add this transition to trans */
112    for (i = 0; _transitions[i].label; i++)
113      {
114         if (_transitions[i].checked)
115           _transitions[i].transition_add_cb(trans);
116      }
117
118    /* get the various options for this transition from the context structure */
119    elm_transit_event_enabled_set(trans, ctxt->events_enabled);
120    elm_transit_auto_reverse_set(trans, ctxt->auto_reverse);
121    elm_transit_objects_final_state_keep_set(trans, ctxt->final_state_keep);
122    elm_transit_tween_mode_set(trans, ctxt->tween_mode);
123    elm_transit_repeat_times_set(trans, ctxt->repeat_times);
124
125    /* set the transition time to 2 seconds and start it */
126    elm_transit_duration_set(trans, 2.0);
127    elm_transit_go(trans);
128 }
129
130 /* callback useful just to know whether we can receive events from the
131  * object or not */
132 static void
133 _object_clicked(void *data, Evas_Object *o, void *event_info)
134 {
135    printf("object clicked!\n");
136 }
137
138 /* update our context with the given value for repeat count */
139 static void
140 _cb_repeat_changed(void *data, Evas_Object *obj, void *event)
141 {
142    int *repeat_cnt = data;
143
144    *repeat_cnt = elm_spinner_value_get(obj);
145 }
146
147 /* update our context with the given tween mode for the transition */
148 static void
149 _cb_tween_changed(void *data, Evas_Object *obj, void *event)
150 {
151    Elm_Transit_Tween_Mode *mode = data;
152    double val = 0.0;
153
154    val = elm_spinner_value_get(obj);
155    if (val == 1.0)
156      *mode = ELM_TRANSIT_TWEEN_MODE_LINEAR;
157    else if (val == 2.0)
158      *mode = ELM_TRANSIT_TWEEN_MODE_SINUSOIDAL;
159    else if (val == 3.0)
160      *mode = ELM_TRANSIT_TWEEN_MODE_DECELERATE;
161    else if (val == 4.0)
162      *mode = ELM_TRANSIT_TWEEN_MODE_ACCELERATE;
163 }
164
165 EAPI_MAIN int
166 elm_main(int argc, char **argv)
167 {
168    Evas_Object *win, *obj, *icon, *box, *vbox, *vbox2, *hbox, *btn;
169    Evas_Object *cbox, *dummy, *spinner;
170    char buf[PATH_MAX];
171    int i;
172    struct Context context;
173
174    /* initialize our context */
175    context.events_enabled = EINA_FALSE;
176    context.auto_reverse = EINA_FALSE;
177    context.final_state_keep = EINA_FALSE;
178    context.repeat_times = 0;
179    context.tween_mode = ELM_TRANSIT_TWEEN_MODE_LINEAR;
180
181    elm_app_info_set(elm_main, "elementary", "images/icon_07.png");
182    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
183
184    /* add a window */
185    win = elm_win_util_standard_add("transit", "Transit Example");
186    elm_win_autodel_set(win, EINA_TRUE);
187
188    /* add a vertical box that will hold everything */
189    box = elm_box_add(win);
190    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
191    elm_win_resize_object_add(win, box);
192    evas_object_show(box);
193
194    /* a dummy background to create some space for the animation */
195    dummy = elm_bg_add(win);
196    evas_object_size_hint_weight_set(dummy, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
197    elm_box_pack_end(box, dummy);
198    evas_object_show(dummy);
199
200    /* add an object that we are going to play with */
201    /* this object isn't packed inside the box because we don't want it to have
202     * its size, position, aspect or anything else controled by the container */
203    obj = elm_button_add(win);
204    elm_object_text_set(obj, "Transformed object!");
205    icon = elm_icon_add(win);
206    snprintf(buf, sizeof(buf), "%s/images/icon_07.png", elm_app_data_dir_get());
207    elm_image_file_set(icon, buf, NULL);
208    elm_object_part_content_set(obj, "icon", icon);
209    evas_object_move(obj, 160, 60);
210    evas_object_resize(obj, 250, 100);
211    evas_object_show(obj);
212    context.obj = obj;
213
214    /* a callback to know if clicks are being received */
215    evas_object_smart_callback_add(obj, "clicked", _object_clicked, NULL);
216
217    /* button to start our transition */
218    btn = elm_button_add(win);
219    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
220    elm_object_text_set(btn, "Transit!");
221    elm_box_pack_end(box, btn);
222    evas_object_show(btn);
223    evas_object_smart_callback_add(btn, "clicked", _transit_start, &context);
224
225    /* horizontal box to help visual organization */
226    hbox = elm_box_add(win);
227    elm_box_horizontal_set(hbox, EINA_TRUE);
228    evas_object_size_hint_weight_set(hbox, EVAS_HINT_EXPAND, EVAS_HINT_FILL);
229    evas_object_size_hint_align_set(hbox, EVAS_HINT_FILL, 0.0);
230    elm_box_pack_end(box, hbox);
231    evas_object_show(hbox);
232
233    /* horizontal box that will hold the many transition checkboxes */
234    vbox = elm_box_add(win);
235    evas_object_size_hint_weight_set(vbox, EVAS_HINT_EXPAND, EVAS_HINT_FILL);
236    evas_object_size_hint_align_set(vbox, EVAS_HINT_FILL, 0.0);
237
238    /* create the respective checkboxes based on our helper structure and
239     * array */
240    for (i = 0; _transitions[i].label; i++)
241      _checkbox_transition_add(vbox, _transitions[i].label,
242                               &_transitions[i].checked);
243
244    elm_box_pack_end(hbox, vbox);
245    evas_object_show(vbox);
246
247    /* vertical box that will hold the many transition option checkboxes */
248    vbox2 = elm_box_add(win);
249    evas_object_size_hint_weight_set(vbox2, EVAS_HINT_EXPAND, EVAS_HINT_FILL);
250    evas_object_size_hint_align_set(vbox2, EVAS_HINT_FILL, 0.0);
251    elm_box_pack_end(hbox, vbox2);
252    evas_object_show(vbox2);
253
254    /* the rest of this code adds widgets to control some of the behavior of
255     * the transitions */
256    cbox = elm_check_add(win);
257    evas_object_size_hint_weight_set(cbox, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
258    evas_object_size_hint_align_set(cbox, 0.0, 0.0);
259    elm_object_text_set(cbox, "Events enabled");
260    elm_check_state_pointer_set(cbox, &context.events_enabled);
261    elm_box_pack_end(vbox2, cbox);
262    evas_object_show(cbox);
263
264    cbox = elm_check_add(win);
265    evas_object_size_hint_weight_set(cbox, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
266    evas_object_size_hint_align_set(cbox, 0.0, 0.0);
267    elm_object_text_set(cbox, "Auto reverse");
268    elm_check_state_pointer_set(cbox, &context.auto_reverse);
269    elm_box_pack_end(vbox2, cbox);
270    evas_object_show(cbox);
271
272    cbox = elm_check_add(win);
273    evas_object_size_hint_weight_set(cbox, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
274    evas_object_size_hint_align_set(cbox, 0.0, 0.0);
275    elm_object_text_set(cbox, "Keep final state");
276    elm_check_state_pointer_set(cbox, &context.final_state_keep);
277    elm_box_pack_end(vbox2, cbox);
278    evas_object_show(cbox);
279
280    spinner = elm_spinner_add(win);
281    elm_object_style_set(spinner, "vertical");
282    elm_spinner_min_max_set(spinner, 0, 4);
283    elm_spinner_label_format_set(spinner, "%.0f");
284    elm_spinner_editable_set(spinner, EINA_FALSE);
285    evas_object_size_hint_weight_set(spinner, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
286    evas_object_size_hint_align_set(spinner, 0.0, EVAS_HINT_FILL);
287    evas_object_smart_callback_add(spinner, "changed", _cb_repeat_changed, &context.repeat_times);
288    elm_box_pack_end(vbox2, spinner);
289    evas_object_show(spinner);
290
291    spinner = elm_spinner_add(win);
292    elm_object_style_set(spinner, "vertical");
293    elm_spinner_min_max_set(spinner, 1, 4);
294    elm_spinner_label_format_set(spinner, "%.0f");
295    elm_spinner_editable_set(spinner, EINA_FALSE);
296    elm_spinner_special_value_add(spinner, 1, "linear");
297    elm_spinner_special_value_add(spinner, 2, "sinusoidal");
298    elm_spinner_special_value_add(spinner, 3, "decelerate");
299    elm_spinner_special_value_add(spinner, 4, "accelerate");
300    evas_object_size_hint_weight_set(spinner, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
301    evas_object_size_hint_align_set(spinner, 0.0, EVAS_HINT_FILL);
302    evas_object_smart_callback_add(spinner, "changed", _cb_tween_changed, &context.tween_mode);
303    elm_box_pack_end(vbox2, spinner);
304    evas_object_show(spinner);
305
306    evas_object_show(win);
307
308    elm_run();
309
310    return 0;
311 }
312 ELM_MAIN()