1 /**
2  * Example of basic object manipulation in Evas.
3  *
4  * This example shows how to manipulate objects within an Ecore-Evas
5  * canvas.
6  *
7  * Please note that this example uses Evas' legacy API.  Compare this
8  * implementation with evas-object-manipulation-eo.c to learn how the
9  * new Eo API is used.
10  *
11  * You'll need at least one engine built for it (excluding the buffer
12  * one) and the png image loader also built. See stdout/stderr for
13  * output.
14  *
15  * @verbatim
16  * gcc -o evas-object-manipulation evas-object-manipulation.c `pkg-config --libs --cflags ecore evas ecore-evas`
17  * @endverbatim
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #else
23 #define PACKAGE_EXAMPLES_DIR "."
24 #endif
25 
26 #include <Ecore.h>
27 #include <Ecore_Evas.h>
28 
29 #include <stdio.h>
30 #include <errno.h>
31 #include "evas-common.h"
32 
33 #define WIDTH  (320)
34 #define HEIGHT (240)
35 
36 static const char *img_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/enlightenment.png";
37 static const char *border_img_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/red.png";
38 
39 struct test_data
40 {
41    Ecore_Evas  *ee;
42    Evas        *canvas;
43    Evas_Object *img, *bg, *clipper, *clipper_border, *text;
44 };
45 
46 static struct test_data d = {0};
47 
48 /* Keep the example's window size in sync with the background image's size */
49 static void
_canvas_resize_cb(Ecore_Evas * ee)50 _canvas_resize_cb(Ecore_Evas *ee)
51 {
52    int w, h;
53 
54    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
55    evas_object_resize(d.bg, w, h);
56 }
57 
58 /* Keyboard event callback routine, to enable the user to toggle various
59  * object properties on the clipper object.
60  */
61 static void
_on_keydown(void * data EINA_UNUSED,Evas * evas EINA_UNUSED,Evas_Object * o EINA_UNUSED,void * einfo)62 _on_keydown(void        *data EINA_UNUSED,
63             Evas        *evas EINA_UNUSED,
64             Evas_Object *o EINA_UNUSED,
65             void        *einfo)
66 {
67    Evas_Event_Key_Down *ev = einfo;
68 
69    if (strcmp(ev->key, "h") == 0)
70      {
71         /* h - print help */
72         printf("commands are:\n"
73                "\to - change clipper's opacity\n"
74                "\tr - toggle clipper's color between red and white\n"
75                "\tc - toggle clipper's clipping function\n"
76                "\tv - toggle clipper's visibility\n");
77         return;
78      }
79 
80    if (strcmp(ev->key, "o") == 0)
81      {
82         /* o - Change clipper's opacity */
83         int alpha, r, g, b;
84 
85         evas_object_color_get(d.clipper, &r, &g, &b, &alpha);
86         evas_color_argb_unpremul(alpha, &r, &g, &b);
87 
88         alpha -= 20;
89         if (alpha < 0)
90           alpha = 255;
91 
92         evas_color_argb_premul(alpha, &r, &g, &b);
93         evas_object_color_set(d.clipper, r, g, b, alpha);
94 
95         printf("Changing clipper's opacity: %d%%\n",
96                 (int)((alpha / 255.0) * 100));
97         return;
98      }
99 
100    if (strcmp(ev->key, "r") == 0)
101      {
102         /* r - Toggle clipper's color between red and white */
103         int alpha, r, g, b;
104 
105         printf("Changing clipper's color to");
106 
107         evas_object_color_get(d.clipper, &r, &g, &b, &alpha);
108         evas_color_argb_unpremul(alpha, &r, &g, &b);
109 
110         if (g > 0)
111           {
112              printf("red\n");
113              g = b = 0;
114           }
115         else
116           {
117              printf("white\n");
118              g = b = 255;
119           }
120 
121         evas_color_argb_premul(alpha, &r, &g, &b);
122         evas_object_color_set(d.clipper, r, g, b, alpha);
123         return;
124      }
125 
126    if (strcmp(ev->key, "c") == 0)
127      {
128         /* o - Toggle clipper's clipping function */
129         printf("Toggling clipping ");
130 
131         if (evas_object_clip_get(d.img) == d.clipper)
132           {
133              evas_object_clip_unset(d.img);
134              printf("off\n");
135           }
136         else
137           {
138              evas_object_clip_set(d.img, d.clipper);
139              printf("on\n");
140           }
141         return;
142      }
143 
144    if (strcmp(ev->key, "v") == 0)
145      {
146         /* v - Toggle clipper's visibility */
147         printf("Clipper is now ");
148 
149         if (evas_object_visible_get(d.clipper))
150           {
151              evas_object_hide(d.clipper);
152              printf("hidden\n");
153           }
154         else
155           {
156              evas_object_show(d.clipper);
157              printf("visible\n");
158           }
159         return;
160      }
161 }
162 
163 int
main(void)164 main(void)
165 {
166    int err;
167 
168    if (!ecore_evas_init())
169      return EXIT_FAILURE;
170 
171    d.ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);
172    if (!d.ee)
173      goto error;
174 
175    ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb);
176    ecore_evas_show(d.ee);
177 
178    d.canvas = ecore_evas_get(d.ee);
179 
180    /* Create background.  As mentioned earlier, the evas_object_*
181     * routines are part of the legacy Evas API; with the new API
182     * you should use code as shown in evas-object-manipulation-eo.c.
183     */
184    d.bg = evas_object_rectangle_add(d.canvas);
185    evas_object_name_set(d.bg, "background rectangle");
186    evas_object_color_set(d.bg, 255, 255, 255, 255); /* white bg */
187    evas_object_move(d.bg, 0, 0); /* at canvas' origin */
188    evas_object_resize(d.bg, WIDTH, HEIGHT); /* covers full canvas */
189    evas_object_show(d.bg);
190 
191    evas_object_focus_set(d.bg, EINA_TRUE);
192    evas_object_event_callback_add(
193      d.bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
194 
195    /* Load enlightenment.png as an image object, then make it fill the
196     * whole canvas area.
197     */
198    d.img = evas_object_image_filled_add(d.canvas);
199    evas_object_image_file_set(d.img, img_path, NULL);
200    err = evas_object_image_load_error_get(d.img);
201    if (err != EVAS_LOAD_ERROR_NONE)
202      {
203         goto panic;
204      }
205    else
206      {
207         evas_object_move(d.img, 0, 0);
208         evas_object_resize(d.img, WIDTH, HEIGHT);
209         evas_object_show(d.img);
210 
211         printf("Image object added, type is: %s\n",
212                 evas_object_type_get(d.img));
213      }
214 
215    /* Add a second image to the canvas - a red square this time.  It
216     * will be given a border to emphasize its position.
217     */
218    d.clipper_border = evas_object_image_filled_add(d.canvas);
219    evas_object_image_file_set(d.clipper_border, border_img_path, NULL);
220    err = evas_object_image_load_error_get(d.clipper_border);
221    if (err != EVAS_LOAD_ERROR_NONE)
222      {
223         goto panic;
224      }
225    else
226      {
227         evas_object_image_border_set(d.clipper_border, 3, 3, 3, 3);
228         evas_object_image_border_center_fill_set(
229           d.clipper_border, EVAS_BORDER_FILL_NONE);
230         evas_object_move(d.clipper_border, (WIDTH / 4) - 3, (HEIGHT / 4) - 3);
231         evas_object_resize(
232           d.clipper_border, (WIDTH / 2) + 6, (HEIGHT / 2) + 6);
233         evas_object_show(d.clipper_border);
234      }
235 
236    /* Lastly, add a rectangle.  It will be white (the default color for
237     * rectangles) and so won't change the color of anything it clips.
238     */
239    d.clipper = evas_object_rectangle_add(d.canvas);
240    evas_object_move(d.clipper, WIDTH / 4, HEIGHT / 4);
241    evas_object_resize(d.clipper, WIDTH / 2, HEIGHT / 2);
242    evas_object_clip_set(d.img, d.clipper);
243    evas_object_show(d.clipper);
244 
245    ecore_main_loop_begin();
246 
247    ecore_evas_free(d.ee);
248    ecore_evas_shutdown();
249    return 0;
250 
251 error:
252    fprintf(stderr, "error: Requires at least one Evas engine built and linked"
253                    " to ecore-evas for this example to run properly.\n");
254 panic:
255    ecore_evas_free(d.ee);
256    ecore_evas_shutdown();
257    return -1;
258 }
259