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