Tizen 2.1 base
[framework/uifw/ecore.git] / src / examples / ecore_evas_ews_example.c
1 /**
2  * Ecore example illustrating the ews of ecore evas usage.
3  *
4  * You'll need at least one Evas engine built for it (excluding the
5  * buffer one). See stdout/stderr for output.
6  *
7  * @verbatim
8  * gcc -o ecore_evas_ews_example ecore_evas_ews_example.c `pkg-config --libs --cflags ecore-evas`
9  * @endverbatim
10  */
11
12 #include <Ecore.h>
13 #include <Ecore_Evas.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <ctype.h>
17
18 static Eina_Bool
19 _wm_win_add(void *data, int type, void *event_info)
20 {
21    Ecore_Evas *ee = event_info;
22    printf("WM: new window=%p\n", ee);
23    return EINA_TRUE;
24 }
25
26 static Eina_Bool
27 _wm_win_move(void *data, int type, void *event_info)
28 {
29    Ecore_Evas *ee = event_info;
30    int x, y;
31    ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
32    printf("WM: window=%p moved to %d,%d\n", ee, x, y);
33    return EINA_TRUE;
34 }
35
36 static Eina_Bool
37 _wm_win_resize(void *data, int type, void *event_info)
38 {
39    Ecore_Evas *ee = event_info;
40    int w, h;
41    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
42    printf("WM: window=%p resized to %dx%d\n", ee, w, h);
43    return EINA_TRUE;
44 }
45
46 static Eina_Bool
47 _wm_win_show(void *data, int type, void *event_info)
48 {
49    Ecore_Evas *ee = event_info;
50    printf("WM: show window=%p\n", ee);
51    return EINA_TRUE;
52 }
53
54 static void
55 optional_ews_window_manager_setup(void)
56 {
57    ecore_event_handler_add(ECORE_EVAS_EWS_EVENT_ADD, _wm_win_add, NULL);
58    ecore_event_handler_add(ECORE_EVAS_EWS_EVENT_MOVE, _wm_win_move, NULL);
59    ecore_event_handler_add(ECORE_EVAS_EWS_EVENT_RESIZE, _wm_win_resize, NULL);
60    ecore_event_handler_add(ECORE_EVAS_EWS_EVENT_SHOW, _wm_win_show, NULL);
61
62    /* one may use any known unique identifier, like an app function pointer */
63    ecore_evas_ews_manager_set(optional_ews_window_manager_setup);
64 }
65
66 static void
67 optional_ews_setup(void)
68 {
69    Evas_Object *bg;
70    Evas *e;
71
72    ecore_evas_ews_setup(0, 0, 800, 600); /* "screen" size */
73    e = ecore_evas_ews_evas_get(); /* forces "screen" to be allocated */
74
75    bg = evas_object_rectangle_add(e);
76    evas_object_color_set(bg, 128, 32, 32, 255);
77    ecore_evas_ews_background_set(bg);
78 }
79
80 static Eina_Bool
81 _stdin_cb(void *data, Ecore_Fd_Handler *handler)
82 {
83    const Eina_List *l;
84    Ecore_Evas *ee;
85    char c = getchar();
86
87    if (c == EOF)
88      {
89         ecore_main_loop_quit();
90         return EINA_FALSE;
91      }
92
93    switch (c) {
94       case 'h':
95         printf("hide all windows\n");
96         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
97           ecore_evas_hide(ee);
98         break;
99
100       case 's':
101         printf("show all windows\n");
102         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
103           ecore_evas_show(ee);
104         break;
105
106       case 'l':
107         printf("move all windows left\n");
108         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
109           {
110              int x, y;
111              ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
112              ecore_evas_move(ee, x - 10, y);
113           }
114         break;
115
116       case 'r':
117         printf("move all windows right\n");
118         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
119           {
120              int x, y;
121              ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
122              ecore_evas_move(ee, x + 10, y);
123           }
124         break;
125
126       case 't':
127         printf("move all windows top\n");
128         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
129           {
130              int x, y;
131              ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
132              ecore_evas_move(ee, x, y - 10);
133           }
134         break;
135
136       case 'b':
137         printf("move all windows bottom\n");
138         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
139           {
140              int x, y;
141              ecore_evas_geometry_get(ee, &x, &y, NULL, NULL);
142              ecore_evas_move(ee, x, y + 10);
143           }
144         break;
145
146       case 'S':
147         printf("make all windows smaller\n");
148         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
149           {
150              int w, h;
151              ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
152              ecore_evas_resize(ee, w - 10, h - 10);
153           }
154         break;
155
156       case 'B':
157         printf("make all windows bigger\n");
158         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
159           {
160              int w, h;
161              ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
162              ecore_evas_resize(ee, w + 10, h + 10);
163           }
164         break;
165
166       case 'm':
167         printf("make all windows unmaximized\n");
168         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
169           ecore_evas_maximized_set(ee, EINA_FALSE);
170         break;
171
172       case 'M':
173         printf("make all windows maximized\n");
174         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
175           ecore_evas_maximized_set(ee, EINA_TRUE);
176         break;
177
178       case 'i':
179         printf("make all windows uniconified\n");
180         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
181           ecore_evas_iconified_set(ee, EINA_FALSE);
182         break;
183
184       case 'I':
185         printf("make all windows iconified\n");
186         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
187           ecore_evas_iconified_set(ee, EINA_TRUE);
188         break;
189
190       case 'f':
191         printf("make all windows unfullscreen\n");
192         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
193           ecore_evas_fullscreen_set(ee, EINA_FALSE);
194         break;
195
196       case 'F':
197         printf("make all windows fullscreen\n");
198         EINA_LIST_FOREACH(ecore_evas_ews_children_get(), l, ee)
199           ecore_evas_fullscreen_set(ee, EINA_TRUE);
200         break;
201
202       case 'q':
203         printf("quit\n");
204         ecore_main_loop_quit();
205         break;
206
207       default:
208         if (!isspace(c))
209           printf("Unknown command: %c\n", c);
210      }
211    return ECORE_CALLBACK_RENEW;
212 }
213
214 static void
215 _on_delete(Ecore_Evas *ee)
216 {
217    free(ecore_evas_data_get(ee, "key"));
218    ecore_main_loop_quit();
219 }
220
221 int
222 main(void)
223 {
224    Ecore_Evas *ee;
225    Evas *canvas;
226    Evas_Object *bg;
227
228    if (ecore_evas_init() <= 0)
229      return 1;
230
231    optional_ews_setup();
232    optional_ews_window_manager_setup();
233
234    /* everything should look similar to ecore_evas_basic_example */
235    ee = ecore_evas_ews_new(0, 0, 200, 200);
236    ecore_evas_title_set(ee, "Ecore Evas EWS Example");
237    ecore_evas_show(ee);
238
239    ecore_evas_data_set(ee, "key", strdup("hello"));
240    ecore_evas_callback_delete_request_set(ee, _on_delete);
241
242    printf("Using %s engine!\n", ecore_evas_engine_name_get(ee));
243
244    canvas = ecore_evas_get(ee);
245    if (ecore_evas_ecore_evas_get(canvas) == ee)
246      printf("Everything is sane!\n");
247
248    bg = evas_object_rectangle_add(canvas);
249    evas_object_color_set(bg, 0, 0, 255, 255);
250    evas_object_resize(bg, 200, 200);
251    evas_object_show(bg);
252    ecore_evas_object_associate(ee, bg, ECORE_EVAS_OBJECT_ASSOCIATE_BASE);
253
254    /* moving the window should move it in the screen */
255    ecore_evas_move(ee, 50, 50);
256
257    ecore_main_fd_handler_add(STDIN_FILENO,
258                              ECORE_FD_READ | ECORE_FD_ERROR,
259                              _stdin_cb,
260                              NULL, NULL, NULL);
261
262    ecore_main_loop_begin();
263
264    ecore_evas_free(ee);
265    ecore_evas_shutdown();
266
267    return 0;
268 }
269