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 ecore_evas_window_sizes_example ecore_evas_window_sizes_example.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 EINA_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 "\tg - get the screen geometry\n"
40 "\th - print help\n";
41
42 /* to inform current window's size */
43 static void
_canvas_resize_cb(Ecore_Evas * ee_)44 _canvas_resize_cb(Ecore_Evas *ee_)
45 {
46 int w, h;
47 char buf[1024];
48
49 ecore_evas_geometry_get(ee_, NULL, NULL, &w, &h);
50 snprintf(buf, sizeof(buf), "%d x %d", w, h);
51 evas_object_text_text_set(text, buf);
52 evas_object_move(text, (w - 150) / 2, (h - 50) / 2);
53
54 evas_object_resize(bg, w, h);
55 }
56
57 static void
_on_destroy(Ecore_Evas * ee_ EINA_UNUSED)58 _on_destroy(Ecore_Evas *ee_ EINA_UNUSED)
59 {
60 ecore_main_loop_quit();
61 }
62
63 static void
_on_keydown(void * data EINA_UNUSED,Evas * evas EINA_UNUSED,Evas_Object * o EINA_UNUSED,void * einfo)64 _on_keydown(void *data EINA_UNUSED,
65 Evas *evas EINA_UNUSED,
66 Evas_Object *o EINA_UNUSED,
67 void *einfo)
68 {
69 Evas_Event_Key_Down *ev = einfo;
70
71 if (strcmp(ev->key, "h") == 0) /* print help */
72 {
73 printf(commands);
74 return;
75 }
76
77 if (strcmp(ev->key, "m") == 0) /* impose a minimum size on the window */
78 {
79 min_set = !min_set;
80
81 if (min_set)
82 {
83 ecore_evas_size_min_set(ee, WIDTH / 2, HEIGHT / 2);
84 printf("Imposing a minimum size of %d x %d\n",
85 WIDTH / 2, HEIGHT / 2);
86 }
87 else
88 {
89 ecore_evas_size_min_set(ee, 0, 0);
90 printf("Taking off minimum size restriction from the"
91 " window\n");
92 }
93 return;
94 }
95
96 if (strcmp(ev->key, "x") == 0) /* impose a maximum size on the window */
97 {
98 max_set = !max_set;
99
100 if (max_set)
101 {
102 ecore_evas_size_max_set(ee, WIDTH * 2, HEIGHT * 2);
103 printf("Imposing a maximum size of %d x %d\n",
104 WIDTH * 2, HEIGHT * 2);
105 }
106 else
107 {
108 ecore_evas_size_max_set(ee, 0, 0);
109 printf("Taking off maximum size restriction from the"
110 " window\n");
111 }
112 return;
113 }
114
115 if (strcmp(ev->key, "b") == 0) /* impose a base size on the window */
116 {
117 base_set = !base_set;
118
119 if (base_set)
120 {
121 ecore_evas_size_base_set(ee, WIDTH * 2, HEIGHT * 2);
122 printf("Imposing a base size of %d x %d\n",
123 WIDTH * 2, HEIGHT * 2);
124 }
125 else
126 {
127 ecore_evas_size_base_set(ee, 0, 0);
128 printf("Taking off base size restriction from the"
129 " window\n");
130 }
131 return;
132 }
133
134 if (strcmp(ev->key, "s") == 0) /* impose a step size on the window */
135 {
136 step_set = !step_set;
137
138 if (step_set)
139 {
140 ecore_evas_size_step_set(ee, 40, 40);
141 printf("Imposing a step size of %d x %d\n", 40, 40);
142 }
143 else
144 {
145 ecore_evas_size_step_set(ee, 0, 0);
146 printf("Taking off step size restriction from the"
147 " window\n");
148 }
149 return;
150 }
151
152 if (strcmp(ev->key, "g") == 0) /* get screen geometry */
153 {
154 int x, y, w, h;
155 ecore_evas_screen_geometry_get(ee, &x, &y, &w, &h);
156 printf("screen geometry: %d,%d, %dx%d\n", x, y, w, h);
157 return;
158 }
159 }
160
161 int
main(void)162 main(void)
163 {
164 Evas *evas;
165
166 if (!ecore_evas_init())
167 return EXIT_FAILURE;
168
169 /* this will give you a window with an Evas canvas under the first
170 * engine available */
171 ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
172 if (!ee) goto error;
173
174 ecore_evas_callback_delete_request_set(ee, _on_destroy);
175 ecore_evas_title_set(ee, "Ecore_Evas window sizes example");
176 ecore_evas_callback_resize_set(ee, _canvas_resize_cb);
177 ecore_evas_show(ee);
178
179 evas = ecore_evas_get(ee);
180
181 bg = evas_object_rectangle_add(evas);
182 evas_object_color_set(bg, 255, 255, 255, 255); /* white bg */
183 evas_object_move(bg, 0, 0); /* at canvas' origin */
184 evas_object_resize(bg, WIDTH, HEIGHT); /* covers full canvas */
185 evas_object_show(bg);
186
187 evas_object_focus_set(bg, EINA_TRUE);
188 evas_object_event_callback_add(
189 bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
190
191 text = evas_object_text_add(evas);
192 evas_object_color_set(text, 0, 0, 0, 255);
193 evas_object_resize(text, 150, 50);
194 evas_object_text_font_set(text, "Sans", 20);
195 evas_object_show(text);
196
197 _canvas_resize_cb(ee);
198 printf(commands);
199 ecore_main_loop_begin();
200
201 ecore_evas_free(ee);
202 ecore_evas_shutdown();
203
204 return 0;
205
206 error:
207 fprintf(stderr, "error: Requires at least one Evas engine built"
208 " and linked to ecore-evas for this example to run"
209 " properly.\n");
210 ecore_evas_shutdown();
211 return -1;
212 }
213
214