EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / examples / eina_tiler_01.c
1 //Compile with:
2 //gcc eina_tiler_01.c -o eina_tiler_01 `pkg-config --cflags --libs ecore-evas ecore evas eina`
3
4 #include <Ecore_Evas.h>
5 #include <Ecore.h>
6 #include <Evas.h>
7 #include <Eina.h>
8
9 #define WINDOW_PAD (20)
10
11 static Eina_Tiler *tiler;
12 static Eina_Rectangle *input_rects;
13 static unsigned int input_count;
14 static unsigned int input_idx = 0, input_color_idx = 0, output_color_idx = 0;
15 static Eina_List *output_objs = NULL;
16 static Evas_Coord maxw, maxh, winw, winh;
17 static Evas *evas;
18
19 static const struct color {
20    unsigned char r, g, b;
21 } colors[] = {
22   {255, 0, 0},
23   {0, 255, 0},
24   {0, 0, 255},
25
26   {255, 128, 0},
27   {0, 255, 128},
28   {128, 0, 255},
29
30   {255, 255, 0},
31   {0, 255, 255},
32   {255, 0, 255},
33
34   {255, 0, 128},
35   {128, 255, 0},
36   {0, 128, 255},
37
38   {128, 128, 0},
39   {0, 128, 128},
40   {128, 0, 128},
41
42   {128, 0, 0},
43   {0, 128, 0},
44   {0, 0, 128},
45
46   {255, 128, 0},
47   {0, 255, 128},
48   {128, 0, 255},
49
50   {64, 64, 0},
51   {0, 64, 64},
52   {64, 0, 64},
53
54   {128, 128, 0},
55   {0, 128, 128},
56   {128, 0, 128},
57
58   {255, 0, 128},
59   {128, 255, 0},
60   {0, 128, 255},
61
62   {128, 64, 0},
63   {0, 128, 64},
64   {64, 0, 128},
65
66   {128, 0, 64},
67   {64, 128, 0},
68   {0, 64, 128}
69 };
70
71 #define MAX_COLORS (sizeof(colors) / sizeof(colors[0]))
72
73 static void
74 add_text(const char *text, int x, int y, int w)
75 {
76    Evas_Object *o = evas_object_text_add(evas);
77    evas_object_color_set(o, 0, 0, 0, 255);
78    evas_object_move(o, x, y);
79    evas_object_resize(o, w, WINDOW_PAD);
80    evas_object_text_font_set(o, "Sans", 10);
81    evas_object_text_text_set(o, text);
82    evas_object_show(o);
83 }
84
85 static void
86 output_rects_reset(void)
87 {
88    Evas_Object *o;
89    EINA_LIST_FREE(output_objs, o)
90      evas_object_del(o);
91    output_color_idx = 0;
92 }
93
94 static void
95 add_input_rect(const Eina_Rectangle *r)
96 {
97    Evas_Object *o;
98    Evas_Coord bx, by;
99
100    bx = WINDOW_PAD;
101    by = WINDOW_PAD;
102
103    o = evas_object_rectangle_add(evas);
104 #define C(comp) (((int)colors[input_color_idx].comp * 128) / 255)
105    evas_object_color_set(o, C(r), C(g), C(b), 128);
106 #undef C
107    evas_object_move(o, r->x + bx, r->y + by);
108    evas_object_resize(o, r->w, r->h);
109    evas_object_show(o);
110
111    input_color_idx = (input_color_idx + 1) % MAX_COLORS;
112
113    bx += maxw + WINDOW_PAD;
114
115    o = evas_object_rectangle_add(evas);
116    evas_object_color_set(o, 32, 32, 32, 128);
117    evas_object_move(o, r->x + bx, r->y + by);
118    evas_object_resize(o, r->w, 1);
119    evas_object_layer_set(o, EVAS_LAYER_MAX);
120    evas_object_show(o);
121
122    o = evas_object_rectangle_add(evas);
123    evas_object_color_set(o, 32, 32, 32, 128);
124    evas_object_move(o, r->x + bx, r->y + by);
125    evas_object_resize(o, 1, r->h);
126    evas_object_layer_set(o, EVAS_LAYER_MAX);
127    evas_object_show(o);
128
129    o = evas_object_rectangle_add(evas);
130    evas_object_color_set(o, 32, 32, 32, 128);
131    evas_object_move(o, r->x + bx, r->y + by + r->h);
132    evas_object_resize(o, r->w, 1);
133    evas_object_layer_set(o, EVAS_LAYER_MAX);
134    evas_object_show(o);
135
136    o = evas_object_rectangle_add(evas);
137    evas_object_color_set(o, 32, 32, 32, 128);
138    evas_object_move(o, r->x + bx + r->w, r->y + by);
139    evas_object_resize(o, 1, r->h);
140    evas_object_layer_set(o, EVAS_LAYER_MAX);
141    evas_object_show(o);
142 }
143
144 static void
145 add_output_rect(const Eina_Rectangle *r)
146 {
147    Evas_Object *o = evas_object_rectangle_add(evas);
148 #define C(comp) (((int)colors[output_color_idx].comp * 128) / 255)
149    evas_object_color_set(o, C(r), C(g), C(b), 128);
150 #undef C
151    evas_object_move(o, r->x + maxw + 2 * WINDOW_PAD, r->y + WINDOW_PAD);
152    evas_object_resize(o, r->w, r->h);
153    evas_object_show(o);
154
155    output_color_idx = (output_color_idx + 1) % MAX_COLORS;
156
157    output_objs = eina_list_append(output_objs, o);
158 }
159
160 static Eina_Bool
161 process_input(void *data)
162 {
163    Eina_Iterator *itr;
164    Eina_Rectangle r, *r1;
165    unsigned int out = 0;
166
167    if (input_idx == input_count)
168      {
169         add_text("Done. Close the window to exit",
170                  WINDOW_PAD, winh - WINDOW_PAD, winw - 2 * WINDOW_PAD);
171         return EINA_FALSE;
172      }
173
174    output_rects_reset();
175
176    r = input_rects[input_idx];
177    printf("Iteration #%u: %dx%d%+d%+d\n", input_idx, r.w, r.h, r.x, r.y);
178    input_idx++;
179    add_input_rect(&r);
180
181    eina_tiler_rect_add(tiler, &r);
182    itr = eina_tiler_iterator_new(tiler);
183    EINA_ITERATOR_FOREACH(itr, r1)
184      {
185         printf("\tOutput #%u: %dx%d%+d%+d\n", out, r1->w, r1->h, r1->x, r1->y);
186         add_output_rect(r1);
187         out++;
188      }
189    eina_iterator_free(itr);
190
191    return EINA_TRUE;
192 }
193
194 static void
195 usage(const char *progname)
196 {
197    fprintf(stderr,
198            "Usage:\n\n"
199            "\t%s <rect1> ... <rectN>\n\n"
200            "with rectangles being in the format:\n"
201            "\tWIDTHxHEIGHT<+->X<+->Y\n"
202            "examples:\n"
203            "\t100x100+10+10 - width=100, height=100 at x=10, y=10\n"
204            "\t150x50+5+6    - width=150, height=50 at x=5, y=6\n",
205            progname);
206 }
207
208 int
209 main(int argc, char *argv[])
210 {
211    Ecore_Evas *ee;
212    Evas_Object *o;
213    int i;
214
215    if (argc < 2)
216      {
217         usage(argv[0]);
218         return -2;
219      }
220
221    input_rects = calloc(argc - 1, sizeof(Eina_Rectangle));
222    input_count = 0;
223    maxw = 0;
224    maxh = 0;
225    for (i = 1; i < argc; i++)
226      {
227         Eina_Rectangle *r = input_rects + input_count;
228         char sx, sy;
229
230         if (sscanf(argv[i], "%dx%d%c%d%c%d",
231                    &(r->w), &(r->h), &sx, &(r->x), &sy, &(r->y)) == 6)
232           {
233              if (sx == '-') r->x *= -1;
234              if (sy == '-') r->y *= -1;
235
236              if (maxw < r->x + r->w) maxw = r->x + r->w;
237              if (maxh < r->y + r->h) maxh = r->y + r->h;
238              input_count++;
239           }
240         else
241           fprintf(stderr, "ERROR: invalid rectangle ignored: %s\n", argv[i]);
242      }
243
244    if (input_count == 0)
245      {
246         fputs("ERROR: Could not find any valid rectangle. Exit!\n", stderr);
247         usage(argv[0]);
248         free(input_rects);
249         return -3;
250      }
251
252    if ((maxw == 0) || (maxh == 0))
253      {
254         fputs("ERROR: All rectangles with size 0x0. Exit!\n", stderr);
255         usage(argv[0]);
256         free(input_rects);
257         return -3;
258      }
259
260    ecore_evas_init();
261    ecore_init();
262    evas_init();
263    eina_init();
264
265    winw = 2 * maxw + 3 * WINDOW_PAD;
266    winh = maxh + 2 * WINDOW_PAD;
267
268    ee = ecore_evas_new(NULL, 0, 0, winw, winh, NULL);
269    if (!ee)
270      {
271         fputs("ERROR: Could not create window. Check ecore-evas install.\n",
272               stderr);
273         goto end;
274      }
275
276    evas = ecore_evas_get(ee);
277
278    o = evas_object_rectangle_add(evas);
279    evas_object_color_set(o, 255, 255, 255, 255);
280    evas_object_resize(o, winw, winh);
281    evas_object_show(o);
282
283    add_text("Input", WINDOW_PAD, 0, maxw);
284    o = evas_object_rectangle_add(evas);
285    evas_object_color_set(o, 200, 200, 200, 255);
286    evas_object_move(o, WINDOW_PAD, WINDOW_PAD);
287    evas_object_resize(o, maxw, maxh);
288    evas_object_show(o);
289
290    add_text("Output", maxw + 2 * WINDOW_PAD, 0, maxw);
291    o = evas_object_rectangle_add(evas);
292    evas_object_color_set(o, 200, 200, 200, 255);
293    evas_object_move(o, maxw + 2 * WINDOW_PAD, WINDOW_PAD);
294    evas_object_resize(o, maxw, maxh);
295    evas_object_show(o);
296
297    tiler = eina_tiler_new(maxw, maxh);
298    ecore_timer_add(2.0, process_input, NULL);
299
300    ecore_evas_show(ee);
301    ecore_main_loop_begin();
302
303    eina_list_free(output_objs);
304    eina_tiler_free(tiler);
305    ecore_evas_free(ee);
306
307  end:
308    free(input_rects);
309
310    eina_shutdown();
311    evas_shutdown();
312    ecore_shutdown();
313    ecore_evas_shutdown();
314
315    return 0;
316 }