Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / examples / edje-color-class.c
1 /**
2  * Simple Edje example illustrating color class functions.
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  * edje_cc color-class.edc && gcc -o edje-table edje-color-class.c `pkg-config --libs --cflags evas ecore ecore-evas edje`
9  * @endverbatim
10  */
11
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #else
15 # define __UNUSED__
16 #endif
17
18 #include <Ecore.h>
19 #include <Ecore_Evas.h>
20 #include <Edje.h>
21
22 #define WIDTH  (400)
23 #define HEIGHT (400)
24
25 typedef int color[4];           /* rgba */
26
27 static Ecore_Evas *ee1, *ee2;
28 static Evas *evas1, *evas2;
29 static Evas_Object *bg1, *edje_obj1, *bg2, *edje_obj2;
30 static const char *selected_class;
31
32 static color colors_init_data[] =
33   {{255, 0, 0, 255},            /* red */
34    {0, 255, 0, 255},            /* green */
35    {0, 0, 255, 255},            /* blue */
36    {0, 0, 0, 255},              /* black */
37    {255, 255, 255, 255},        /* white */
38    {128, 128, 128, 255},        /* gray */
39    {255, 255, 0, 255},          /* yellow */
40    {255, 0, 255, 255}           /* pink */
41   };
42
43 static char *color_names[] =
44   {"red", "green", "blue", "black", "white",
45    "gray", "yellow", "pink"};
46
47 static Eina_Bool
48 _get_color_from_name(const char *n, color *c)
49 {
50    int i;
51    for (i = 0; i < 8; i++)
52      if (!strcmp(n, color_names[i]))
53        {
54           (*c)[0] = (colors_init_data[i])[0];
55           (*c)[1] = (colors_init_data[i])[1];
56           (*c)[2] = (colors_init_data[i])[2];
57           (*c)[3] = (colors_init_data[i])[3];
58           return EINA_TRUE;
59        }
60
61    return EINA_FALSE;
62 }
63
64 static void
65 _color_classes_print(void)
66 {
67    Eina_List *classes;
68    char *class_name;
69
70    fprintf(stdout, "Getting the color classes\n\n");
71    classes = edje_color_class_list();
72    EINA_LIST_FREE(classes, class_name)
73      {
74         int r1, r2, r3, g1, g2, g3, b1, b2, b3,
75           a1, a2, a3;
76
77         fprintf(stdout, "\ncolor class: %s\n", class_name);
78         if (!edje_color_class_get(class_name, &r1, &g1, &b1, &a1,
79                                   &r2, &g2, &b2, &a2, &r3, &g3, &b3, &a3))
80           fprintf(stderr, "Cannot get the color class\n");
81         else
82           {
83
84              fprintf(stdout,"Object color r: %d g: %d b: %d a: %d\n",
85                      r1, g1, b1, a1);
86              fprintf(stdout,"Text outline color r: %d g: %d b: %d a: %d\n",
87                      r2, g2, b2, a2);
88              fprintf(stdout,"Text shadow color r: %d g: %d b: %d a: %d\n",
89                      r3, g3, b3, a3);
90           }
91         free(class_name);
92      }
93 }
94
95 static void
96 _on_destroy(Ecore_Evas *ee __UNUSED__)
97 {
98    ecore_main_loop_quit();
99 }
100
101 static void
102 _on_mouse_down(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info)
103 {
104    Evas_Event_Mouse_Down *ev = event_info;
105
106    if (ev->button == 1)
107      {
108         if (obj == edje_obj1)
109           edje_color_class_del(selected_class);
110         else
111           edje_object_color_class_del(edje_obj2, selected_class);
112      }
113 }
114
115 /* here just to keep our example's window size
116  * in synchrony. */
117 static void
118 _canvas_resize_cb(Ecore_Evas *_ee)
119 {
120    int w, h;
121
122    ecore_evas_geometry_get(_ee, NULL, NULL, &w, &h);
123
124    if (_ee == ee1)
125      {
126         evas_object_resize(bg1, w, h);
127         evas_object_resize(edje_obj1, w, h);
128      }
129    else
130      {
131         evas_object_resize(bg2, w, h);
132         evas_object_resize(edje_obj2, w, h);
133      }
134 }
135
136 static void
137 _color_class_callback_delete(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
138                              const char *emission, void *source __UNUSED__)
139 {
140    if (!strcmp(data, "process"))
141      fprintf(stdout, "Color class: %s deleted on process level\n", emission);
142    else
143      fprintf(stdout, "Color class: %s deleted on object level\n", emission);
144 }
145
146 static int
147 _create_windows(const char *edje_file_path)
148 {
149   /* this will give you a window with an Evas canvas under the first
150     * engine available */
151    ee1 = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
152    if (!ee1)
153      return 0;
154    ee2 = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
155    if (!ee2)
156      return 0;
157
158    ecore_evas_callback_destroy_set(ee1, _on_destroy);
159    ecore_evas_callback_resize_set(ee1, _canvas_resize_cb);
160    ecore_evas_title_set(ee1, "Edje Color Class Example");
161
162    ecore_evas_callback_destroy_set(ee2, _on_destroy);
163    ecore_evas_callback_resize_set(ee2, _canvas_resize_cb);
164    ecore_evas_title_set(ee2, "Edje Object Color Class Example");
165
166    evas1 = ecore_evas_get(ee1);
167    evas2 = ecore_evas_get(ee2);
168
169    bg1 = evas_object_rectangle_add(evas1);
170    evas_object_color_set(bg1, 255, 255, 255, 255); /* white bg */
171    evas_object_move(bg1, 0, 0);                    /* at canvas' origin */
172    evas_object_resize(bg1, WIDTH, HEIGHT);         /* covers full canvas */
173    evas_object_show(bg1);
174
175    bg2 = evas_object_rectangle_add(evas2);
176    evas_object_color_set(bg2, 255, 255, 255, 255); /* white bg */
177    evas_object_move(bg2, 0, 0);                    /* at canvas' origin */
178    evas_object_resize(bg2, WIDTH, HEIGHT);         /* covers full canvas */
179    evas_object_show(bg2);
180
181    edje_obj1 = edje_object_add(evas1);
182    evas_object_event_callback_add(edje_obj1, EVAS_CALLBACK_MOUSE_DOWN,
183                                   _on_mouse_down, NULL);
184
185    edje_object_file_set(edje_obj1, edje_file_path, "example_color_class");
186    evas_object_move(edje_obj1, 0, 0); /* at canvas' origin */
187    evas_object_resize(edje_obj1, WIDTH, HEIGHT);
188    edje_object_part_text_set(edje_obj1, "part_four", "EDJE EXAMPLE");
189    edje_object_signal_callback_add(edje_obj1, "color_class,del", "*",
190                                    (Edje_Signal_Cb) _color_class_callback_delete,
191                                    "process");
192    evas_object_show(edje_obj1);
193
194    edje_obj2 = edje_object_add(evas2);
195    evas_object_event_callback_add(edje_obj2, EVAS_CALLBACK_MOUSE_DOWN,
196                                   _on_mouse_down, NULL);
197
198    edje_object_file_set(edje_obj2, edje_file_path, "example_color_class");
199    evas_object_move(edje_obj2, 0, 0); /* at canvas' origin */
200    evas_object_resize(edje_obj2, WIDTH, HEIGHT);
201    edje_object_part_text_set(edje_obj2, "part_four", "EDJE OBJECT EXAMPLE");
202    edje_object_signal_callback_add(edje_obj2, "color_class,del", "*",
203                                    (Edje_Signal_Cb) _color_class_callback_delete,
204                                    "object");
205    evas_object_show(edje_obj2);
206
207    return 1;
208 }
209
210 int
211 main(int argc, char *argv[])
212 {
213    char         edje_file_path[PATH_MAX];
214    const char  *edje_file = "color-class.edj";
215    Eina_Prefix *pfx;
216    color        c1, c2, c3;
217    int          i;
218
219    if (argc != 5)
220      {
221         fprintf(stderr, "You have to use: %s color_class_name color1, color2," \
222                 "color3\n", argv[0]);
223         fprintf(stderr, "Available colors:\n");
224         for (i = 0; i < 8; i++)
225           fprintf(stderr, "%s\n", color_names[i]);
226
227         return EXIT_FAILURE;
228      }
229
230    selected_class = argv[1];
231    if (!(_get_color_from_name(argv[2], &c1) &&
232          _get_color_from_name(argv[3], &c2) &&
233          _get_color_from_name(argv[4], &c3)))
234      {
235         fprintf(stderr, "Color not available!\n");
236         return EXIT_FAILURE;
237      }
238
239    if (!ecore_evas_init())
240      return EXIT_FAILURE;
241
242    if (!edje_init())
243      goto shutdown_ecore_evas;
244
245    pfx = eina_prefix_new(argv[0], main,
246                          "EDJE_EXAMPLES",
247                          "edje/examples",
248                          edje_file,
249                          PACKAGE_BIN_DIR,
250                          PACKAGE_LIB_DIR,
251                          PACKAGE_DATA_DIR,
252                          PACKAGE_DATA_DIR);
253    if (!pfx)
254      goto shutdown_edje;
255
256    snprintf(edje_file_path, sizeof(edje_file_path),
257             "%s/examples/%s", eina_prefix_data_get(pfx), edje_file);
258    if (!_create_windows(edje_file_path))
259      goto free_prefix;
260
261    edje_color_class_set(argv[1],                     /* class name   */
262                         c1[0], c1[1], c1[2], c1[3],  /* Object color */
263                         c2[0], c2[1], c2[2], c2[3],  /* Text outline */
264                         c3[0], c3[1], c3[2], c3[3]); /* Text shadow  */
265
266    /* Setting an arbitrary value just to see the difference between */
267    /* process level and object level */
268    edje_object_color_class_set(edje_obj2, argv[1], /* class name   */
269                                128, 180, 77, 255,  /* Object color */
270                                200, 22, 86, 255,   /* Text outline */
271                                39, 90, 187, 255);  /* Text shadow  */
272
273    _color_classes_print();
274
275    ecore_evas_show(ee1);
276    ecore_evas_show(ee2);
277
278    ecore_main_loop_begin();
279
280    eina_prefix_free(pfx);
281    ecore_evas_free(ee1);
282    ecore_evas_shutdown();
283    edje_shutdown();
284
285    return EXIT_SUCCESS;
286
287  free_prefix:
288    eina_prefix_free(pfx);
289  shutdown_edje:
290    edje_shutdown();
291  shutdown_ecore_evas:
292    ecore_evas_shutdown();
293
294    return EXIT_FAILURE;
295 }