diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/evas/src/examples/evas-aspect-hints.c | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/libraries/evas/src/examples/evas-aspect-hints.c b/libraries/evas/src/examples/evas-aspect-hints.c new file mode 100644 index 0000000..de8ed50 --- /dev/null +++ b/libraries/evas/src/examples/evas-aspect-hints.c | |||
@@ -0,0 +1,249 @@ | |||
1 | /** | ||
2 | * Simple Evas example illustrating aspect control hints on objects. | ||
3 | * | ||
4 | * You'll need at least one engine built for it (excluding the buffer | ||
5 | * one) and the png image loader also built. See stdout/stderr for | ||
6 | * output. | ||
7 | * | ||
8 | * You'll also need @b Edje for this one, as it has the only smart | ||
9 | * object implementing aspect control for children. | ||
10 | * | ||
11 | * @verbatim | ||
12 | * gcc -o evas-events evas-events.c `pkg-config --libs --cflags ecore-evas edje` | ||
13 | * @endverbatim | ||
14 | */ | ||
15 | |||
16 | #ifdef HAVE_CONFIG_H | ||
17 | |||
18 | #include "config.h" | ||
19 | #endif | ||
20 | |||
21 | #include <Ecore.h> | ||
22 | #include <Ecore_Evas.h> | ||
23 | #include <Edje.h> | ||
24 | #include <stdio.h> | ||
25 | #include <errno.h> | ||
26 | |||
27 | #define WIDTH 320 | ||
28 | #define HEIGHT 480 | ||
29 | |||
30 | static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png"; | ||
31 | static const char *edje_file_path = PACKAGE_EXAMPLES_DIR "/aspect.edj"; | ||
32 | |||
33 | struct test_data | ||
34 | { | ||
35 | Ecore_Evas *ee; | ||
36 | Evas *canvas; | ||
37 | Evas_Object *bg, *rect, *container, *border; | ||
38 | }; | ||
39 | |||
40 | static struct test_data d = {0}; | ||
41 | |||
42 | /* here just to keep our example's window size and background image's | ||
43 | * size in synchrony */ | ||
44 | static void | ||
45 | _canvas_resize_cb(Ecore_Evas *ee) | ||
46 | { | ||
47 | int w, h; | ||
48 | |||
49 | ecore_evas_geometry_get(ee, NULL, NULL, &w, &h); | ||
50 | evas_object_resize(d.bg, w, h); | ||
51 | } | ||
52 | |||
53 | static const char * | ||
54 | _get_aspect_name(Evas_Aspect_Control aspect) | ||
55 | { | ||
56 | switch (aspect) | ||
57 | { | ||
58 | case 0: | ||
59 | return "NONE"; | ||
60 | |||
61 | case 1: | ||
62 | return "NEITHER"; | ||
63 | |||
64 | case 2: | ||
65 | return "HORIZONTAL"; | ||
66 | |||
67 | case 3: | ||
68 | return "VERTICAL"; | ||
69 | |||
70 | case 4: | ||
71 | return "BOTH"; | ||
72 | |||
73 | default: | ||
74 | return "INVALID"; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | static void | ||
79 | _on_keydown(void *data __UNUSED__, | ||
80 | Evas *evas __UNUSED__, | ||
81 | Evas_Object *o, | ||
82 | void *einfo) | ||
83 | { | ||
84 | const Evas_Modifier *mods; | ||
85 | Evas_Event_Key_Down *ev = einfo; | ||
86 | |||
87 | mods = evas_key_modifier_get(evas_object_evas_get(o)); | ||
88 | |||
89 | if (evas_key_modifier_is_set(mods, "Shift") && | ||
90 | strcmp(ev->keyname, "h") == 0) /* print help */ | ||
91 | { | ||
92 | fprintf(stdout, "commands are:\n" | ||
93 | "\tc - cycle aspect control on object\n" | ||
94 | "\th - change horizontal aspect component\n" | ||
95 | "\tv - change vertical aspect component\n" | ||
96 | "\ts - print current object's status\n" | ||
97 | "\tH - print help\n"); | ||
98 | return; | ||
99 | } | ||
100 | |||
101 | if (strcmp(ev->keyname, "s") == 0) /* get aspect status of the obj */ | ||
102 | { | ||
103 | Evas_Coord w, h; | ||
104 | Evas_Aspect_Control aspect; | ||
105 | |||
106 | evas_object_size_hint_aspect_get(d.rect, &aspect, &w, &h); | ||
107 | |||
108 | fprintf(stdout, "Object has aspect %s, with horizontal compontent %d" | ||
109 | " and vertical compontent %d\n", | ||
110 | _get_aspect_name(aspect), w, h); | ||
111 | |||
112 | return; | ||
113 | } | ||
114 | |||
115 | if (strcmp(ev->keyname, "c") == 0) /* cycle aspect control on obj */ | ||
116 | { | ||
117 | Evas_Coord w, h; | ||
118 | Evas_Aspect_Control aspect; | ||
119 | |||
120 | evas_object_size_hint_aspect_get(d.rect, &aspect, &w, &h); | ||
121 | |||
122 | aspect = (aspect + 1) % 5; | ||
123 | |||
124 | evas_object_size_hint_aspect_set(d.rect, aspect, w, h); | ||
125 | |||
126 | fprintf(stdout, "Changing aspect control to %s\n", | ||
127 | _get_aspect_name(aspect)); | ||
128 | |||
129 | return; | ||
130 | } | ||
131 | |||
132 | if (strcmp(ev->keyname, "h") == 0) /* change horizontal aspect component */ | ||
133 | { | ||
134 | Evas_Coord w, h; | ||
135 | Evas_Aspect_Control aspect; | ||
136 | |||
137 | evas_object_size_hint_aspect_get(d.rect, &aspect, &w, &h); | ||
138 | |||
139 | w = (w + 1) % 3; | ||
140 | |||
141 | evas_object_size_hint_aspect_set(d.rect, aspect, w, h); | ||
142 | |||
143 | fprintf(stdout, "Changing horizontal aspect component to %d\n", w); | ||
144 | |||
145 | return; | ||
146 | } | ||
147 | |||
148 | if (strcmp(ev->keyname, "v") == 0) /* change vertical aspect component */ | ||
149 | { | ||
150 | Evas_Coord w, h; | ||
151 | Evas_Aspect_Control aspect; | ||
152 | |||
153 | evas_object_size_hint_aspect_get(d.rect, &aspect, &w, &h); | ||
154 | |||
155 | h = (h + 1) % 3; | ||
156 | |||
157 | evas_object_size_hint_aspect_set(d.rect, aspect, w, h); | ||
158 | |||
159 | fprintf(stdout, "Changing vertical aspect component to %d\n", h); | ||
160 | |||
161 | return; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | int | ||
166 | main(void) | ||
167 | { | ||
168 | Eina_Bool ret; | ||
169 | |||
170 | if (!ecore_evas_init()) | ||
171 | return EXIT_FAILURE; | ||
172 | |||
173 | if (!edje_init()) | ||
174 | return EXIT_FAILURE; | ||
175 | |||
176 | /* this will give you a window with an Evas canvas under the first | ||
177 | * engine available */ | ||
178 | d.ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL); | ||
179 | if (!d.ee) | ||
180 | goto error; | ||
181 | |||
182 | ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb); | ||
183 | ecore_evas_show(d.ee); | ||
184 | |||
185 | /* the canvas pointer, de facto */ | ||
186 | d.canvas = ecore_evas_get(d.ee); | ||
187 | |||
188 | d.bg = evas_object_rectangle_add(d.canvas); | ||
189 | evas_object_color_set(d.bg, 255, 255, 255, 255); /* white bg */ | ||
190 | evas_object_move(d.bg, 0, 0); /* at canvas' origin */ | ||
191 | evas_object_resize(d.bg, WIDTH, HEIGHT); /* covers full canvas */ | ||
192 | evas_object_show(d.bg); | ||
193 | |||
194 | evas_object_focus_set(d.bg, EINA_TRUE); | ||
195 | evas_object_event_callback_add( | ||
196 | d.bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL); | ||
197 | |||
198 | d.container = edje_object_add(d.canvas); | ||
199 | ret = edje_object_file_set(d.container, edje_file_path, "main"); | ||
200 | if (!ret) | ||
201 | { | ||
202 | Edje_Load_Error err = edje_object_load_error_get(d.container); | ||
203 | const char *msg = edje_load_error_str(err); | ||
204 | fprintf(stderr, "could not load 'main' from %s: %s", | ||
205 | edje_file_path, msg); | ||
206 | |||
207 | goto panic; | ||
208 | } | ||
209 | |||
210 | evas_object_move(d.container, (WIDTH / 4), (HEIGHT / 4)); | ||
211 | evas_object_resize(d.container, (WIDTH / 2), (HEIGHT / 2)); | ||
212 | evas_object_show(d.container); | ||
213 | |||
214 | d.rect = evas_object_rectangle_add(d.canvas); | ||
215 | evas_object_color_set(d.rect, 0, 0, 255, 255); | ||
216 | evas_object_size_hint_aspect_set(d.rect, EVAS_ASPECT_CONTROL_NONE, 1, 1); | ||
217 | evas_object_show(d.rect); | ||
218 | |||
219 | edje_object_part_swallow(d.container, "content", d.rect); | ||
220 | evas_object_smart_changed(d.container); | ||
221 | |||
222 | /* this is a border around the edje object, container of the | ||
223 | * rectangle we are going to experiment with (change its aspect | ||
224 | * hints). this way you can see how their sizes relate */ | ||
225 | d.border = evas_object_image_filled_add(d.canvas); | ||
226 | evas_object_image_file_set(d.border, border_img_path, NULL); | ||
227 | evas_object_image_border_set(d.border, 3, 3, 3, 3); | ||
228 | evas_object_image_border_center_fill_set(d.border, EVAS_BORDER_FILL_NONE); | ||
229 | evas_object_move(d.border, (WIDTH / 4) - 3, (HEIGHT / 4) - 3); | ||
230 | evas_object_resize(d.border, (WIDTH / 2) + 6, (HEIGHT / 2) + 6); | ||
231 | evas_object_show(d.border); | ||
232 | |||
233 | ecore_main_loop_begin(); | ||
234 | |||
235 | ecore_evas_free(d.ee); | ||
236 | ecore_evas_shutdown(); | ||
237 | edje_shutdown(); | ||
238 | return 0; | ||
239 | |||
240 | error: | ||
241 | fprintf(stderr, "you got to have at least one evas engine built and linked" | ||
242 | " up to ecore-evas for this example to run properly.\n"); | ||
243 | panic: | ||
244 | ecore_evas_free(d.ee); | ||
245 | ecore_evas_shutdown(); | ||
246 | edje_shutdown(); | ||
247 | |||
248 | return -1; | ||
249 | } | ||