badfb16991849c29743621d96207c066e1bc25c3
[framework/uifw/ecore.git] / src / examples / ecore_evas_window_sizes_example.c
1 /**
2  * Simple @c Ecore_Evas example illustrating how to deal with window
3  * sizes
4  *
5  * You'll need at least one engine built for it (excluding the buffer
6  * one). See stdout/stderr for output.
7  *
8  * @verbatim
9  * gcc -o evas-smart-object evas-smart-object.c `pkg-config --libs --cflags evas ecore ecore-evas`
10  * @endverbatim
11  */
12
13 #ifdef HAVE_CONFIG_H
14
15 #include "config.h"
16 #else
17 #define __UNUSED__
18 #endif
19
20 #include <Ecore.h>
21 #include <Ecore_Evas.h>
22
23 #define WIDTH  (300)
24 #define HEIGHT (300)
25
26 static Ecore_Evas *ee;
27 static Evas_Object *text, *bg;
28 static Eina_Bool min_set = EINA_FALSE;
29 static Eina_Bool max_set = EINA_FALSE;
30 static Eina_Bool base_set = EINA_FALSE;
31 static Eina_Bool step_set = EINA_FALSE;
32
33 static const char commands[] = \
34   "commands are:\n"
35   "\tm - impose a minumum size to the window\n"
36   "\tx - impose a maximum size to the window\n"
37   "\tb - impose a base size to the window\n"
38   "\ts - impose a step size (different than 1 px) to the window\n"
39   "\th - print help\n";
40
41 /* to inform current window's size */
42 static void
43 _canvas_resize_cb(Ecore_Evas *ee)
44 {
45    int w, h;
46    char buf[1024];
47
48    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
49    snprintf(buf, sizeof(buf), "%d x %d", w, h);
50    evas_object_text_text_set(text, buf);
51    evas_object_move(text, (w - 150) / 2, (h - 50) / 2);
52
53    evas_object_resize(bg, w, h);
54 }
55
56 static void
57 _on_destroy(Ecore_Evas *ee __UNUSED__)
58 {
59    ecore_main_loop_quit();
60 }
61
62 static void
63 _on_keydown(void        *data __UNUSED__,
64             Evas        *evas __UNUSED__,
65             Evas_Object *o __UNUSED__,
66             void        *einfo)
67 {
68    Evas_Event_Key_Down *ev = einfo;
69
70    if (strcmp(ev->keyname, "h") == 0) /* print help */
71      {
72         fprintf(stdout, commands);
73         return;
74      }
75
76    if (strcmp(ev->keyname, "m") == 0) /* impose a minimum size on the window */
77      {
78         min_set = !min_set;
79
80         if (min_set)
81           {
82              ecore_evas_size_min_set(ee, WIDTH / 2, HEIGHT / 2);
83              fprintf(stdout, "Imposing a minimum size of %d x %d\n",
84                      WIDTH / 2, HEIGHT / 2);
85           }
86         else
87           {
88              ecore_evas_size_min_set(ee, 0, 0);
89              fprintf(stdout, "Taking off minimum size restriction from the"
90                              " window\n");
91           }
92         return;
93      }
94
95    if (strcmp(ev->keyname, "x") == 0) /* impose a maximum size on the window */
96      {
97         max_set = !max_set;
98
99         if (max_set)
100           {
101              ecore_evas_size_max_set(ee, WIDTH * 2, HEIGHT * 2);
102              fprintf(stdout, "Imposing a maximum size of %d x %d\n",
103                      WIDTH * 2, HEIGHT * 2);
104           }
105         else
106           {
107              ecore_evas_size_max_set(ee, 0, 0);
108              fprintf(stdout, "Taking off maximum size restriction from the"
109                              " window\n");
110           }
111         return;
112      }
113
114    if (strcmp(ev->keyname, "b") == 0) /* impose a base size on the window */
115      {
116         base_set = !base_set;
117
118         if (base_set)
119           {
120              ecore_evas_size_base_set(ee, WIDTH * 2, HEIGHT * 2);
121              fprintf(stdout, "Imposing a base size of %d x %d\n",
122                      WIDTH * 2, HEIGHT * 2);
123           }
124         else
125           {
126              ecore_evas_size_base_set(ee, 0, 0);
127              fprintf(stdout, "Taking off base size restriction from the"
128                              " window\n");
129           }
130         return;
131      }
132
133    if (strcmp(ev->keyname, "s") == 0) /* impose a step size on the window */
134      {
135         step_set = !step_set;
136
137         if (step_set)
138           {
139              ecore_evas_size_step_set(ee, 40, 40);
140              fprintf(stdout, "Imposing a step size of %d x %d\n", 40, 40);
141           }
142         else
143           {
144              ecore_evas_size_step_set(ee, 0, 0);
145              fprintf(stdout, "Taking off step size restriction from the"
146                              " window\n");
147           }
148         return;
149      }
150 }
151
152 int
153 main(void)
154 {
155    Evas *evas;
156
157    if (!ecore_evas_init())
158      return EXIT_FAILURE;
159
160    /* this will give you a window with an Evas canvas under the first
161     * engine available */
162    ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
163    if (!ee) goto error;
164
165    ecore_evas_callback_delete_request_set(ee, _on_destroy);
166    ecore_evas_title_set(ee, "Ecore_Evas window sizes example");
167    ecore_evas_callback_resize_set(ee, _canvas_resize_cb);
168    ecore_evas_show(ee);
169
170    evas = ecore_evas_get(ee);
171
172    bg = evas_object_rectangle_add(evas);
173    evas_object_color_set(bg, 255, 255, 255, 255);  /* white bg */
174    evas_object_move(bg, 0, 0);  /* at canvas' origin */
175    evas_object_resize(bg, WIDTH, HEIGHT);  /* covers full canvas */
176    evas_object_show(bg);
177
178    evas_object_focus_set(bg, EINA_TRUE);
179    evas_object_event_callback_add(
180      bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
181
182    text = evas_object_text_add(evas);
183    evas_object_color_set(text, 0, 0, 0, 255);
184    evas_object_resize(text, 150, 50);
185    evas_object_text_font_set(text, "Sans", 20);
186    evas_object_show(text);
187
188    _canvas_resize_cb(ee);
189    fprintf(stdout, commands);
190    ecore_main_loop_begin();
191
192    ecore_evas_free(ee);
193    ecore_evas_shutdown();
194
195    return 0;
196
197 error:
198    fprintf(stderr, "You got to have at least one Evas engine built"
199                    " and linked up to ecore-evas for this example to run"
200                    " properly.\n");
201    ecore_evas_shutdown();
202    return -1;
203 }
204