efl
273 строки · 8.0 Кб
1#define EFL_BETA_API_SUPPORT 1
2
3#include <Efl_Ui.h>
4
5/**
6* Example of canvas textblock obstacles.
7*
8* You start with two registered obstacle objects. They are not visible
9* at first, so the canvas textblock simply shows the text that has been set to it.
10* Once the obstacle is visible (show/hide keys in the example), the text will
11* wrap around it.
12* This example allows you to test two obstacles registered to the same
13* canvas textblock object. Also, you can play with size and position for each.
14* Use the 'h' key to show the provided options for this test.
15*
16* @verbatim
17* gcc -g efl_canvas_textblock_obstacles_example.c -o efl_canvas_textblock_obstacles_example `pkg-config --cflags --libs elementary`
18* @endverbatim
19*/
20
21#define WIDTH (360)
22#define HEIGHT (240)
23
24#define POINTER_CYCLE(_ptr, _array) \
25do \
26{ \
27if ((unsigned int)(((unsigned char *)(_ptr)) - ((unsigned char *)(_array))) >= \
28sizeof(_array)) \
29_ptr = _array; \
30} \
31while(0)
32
33static const char *commands = \
34"commands are:\n"
35"\tt - change currently controlled obstacle\n"
36"\tv - show/hide current obstacle\n"
37"\ts - cycle current obstacle's size\n"
38"\tp - change current obstacle's position (random)\n"
39"\tw - cycle text wrapping modes (none/word/char/mixed)\n"
40"\th - print help\n";
41
42struct text_preset_data
43{
44const char **font_ptr;
45const char *font[3];
46
47const char **wrap_ptr;
48const char *wrap[4];
49
50int *obs_size_ptr;
51int obs_size[3];
52
53Eo **obs_ptr; /* pointer to the currently controlled obstacle object */
54Eo *obs[2];
55};
56
57struct test_data
58{
59Eo *win, *box, *bg, *text;
60struct text_preset_data t_data;
61Eina_Size2D size;
62};
63
64static struct test_data d = {0};
65
66static unsigned int
67_getrand(unsigned int low, unsigned int high)
68{
69return (rand() % (high - low)) + low;
70}
71
72static void
73_style_set(const char *wrap)
74{
75char buf[2000];
76snprintf(buf,
772000,
78"font=Sans font_size=16 color=#000 wrap=%s",
79wrap);
80
81efl_canvas_textblock_style_apply(d.text, buf);
82}
83
84static void
85_key_down(void *data EINA_UNUSED, const Efl_Event *ev)
86{
87const char *key = efl_input_key_string_get(ev->info);
88if (!key)
89return;
90
91if (strcmp(key, "h") == 0) /* print help */
92{
93printf("%s\n", commands);
94return;
95}
96
97if (strcmp(key, "t") == 0) /* change obstacle type */
98{
99(d.t_data.obs_ptr)++;
100POINTER_CYCLE(d.t_data.obs_ptr, d.t_data.obs);
101
102printf("Now controlling obstacle: %p\n", *d.t_data.obs_ptr);
103
104return;
105}
106
107if (strcmp(key, "v") == 0) /* change obstacle visibility */
108{
109Eo *obj = *d.t_data.obs_ptr;
110if (efl_gfx_entity_visible_get(obj))
111efl_gfx_entity_visible_set(obj, EINA_FALSE);
112else
113efl_gfx_entity_visible_set(obj, EINA_TRUE);
114
115printf("Show/hide toggle for obstacle %p\n",
116*d.t_data.obs_ptr);
117
118efl_canvas_textblock_obstacles_update(d.text);
119
120return;
121}
122
123if (strcmp(key, "s") == 0) /* change obstacle size */
124{
125(d.t_data.obs_size_ptr)++;
126POINTER_CYCLE(d.t_data.obs_size_ptr, d.t_data.obs_size);
127
128efl_gfx_entity_size_set(*d.t_data.obs_ptr, EINA_SIZE2D(*d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr));
129
130efl_canvas_textblock_obstacles_update(d.text);
131
132printf("Changing obstacle size to: %d,%d\n",
133*d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr);
134
135return;
136}
137
138if (strcmp(key, "p") == 0) /* change obstacle position */
139{
140int x, y;
141x = _getrand(0, d.size.w);
142y = _getrand(0, d.size.h);
143
144efl_gfx_entity_position_set(*d.t_data.obs_ptr, EINA_POSITION2D(x, y));
145efl_canvas_textblock_obstacles_update(d.text);
146
147printf("Changing obstacles position\n");
148efl_gfx_entity_position_set(*d.t_data.obs_ptr, EINA_POSITION2D(x, y));
149
150Eina_Position2D r_rec = efl_gfx_entity_position_get(d.t_data.obs[0]);
151Eina_Position2D g_rec = efl_gfx_entity_position_get(d.t_data.obs[1]);
152
153printf("Obstacle #1 (red) : [%d,%d]\n", r_rec.x, r_rec.y);
154printf("Obstacle #2 (green): [%d,%d]\n", g_rec.x, g_rec.y);
155
156return;
157}
158
159if (strcmp(key, "w") == 0) /* change obstacle position */
160{
161(d.t_data.wrap_ptr)++;
162POINTER_CYCLE(d.t_data.wrap_ptr, d.t_data.wrap);
163printf("Changing wrap mode to: %s\n",
164*d.t_data.wrap_ptr);
165_style_set(*d.t_data.wrap_ptr);
166efl_canvas_textblock_obstacles_update(d.text);
167
168return;
169}
170}
171
172static void
173_win_resize(void *data EINA_UNUSED, const Efl_Event *ev)
174{
175Eina_Size2D sz;
176
177sz = efl_gfx_entity_size_get(ev->object);
178efl_gfx_entity_size_set(d.bg, sz);
179efl_gfx_entity_size_set(d.text, sz);
180
181d.size = sz;
182}
183
184static void
185_text_init()
186{
187_style_set("word");
188
189efl_text_markup_set(d.text,
190"This example text demonstrates the textblock object"
191" with obstacle objects support."
192" Any evas object <item size=72x16></item>can register itself as an obstacle to the textblock"
193" object. Upon regi<color=#0ff>stering, it aff</color>ects the layout of the text in"
194" certain situations. Usually, when the obstacle shows above the text"
195" area, it will cause the layout of the text to split and move"
196" parts of it, so that all text area is apparent."
197);
198}
199
200static void
201_gui_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
202{
203efl_exit(0);
204}
205
206static void
207_gui_setup()
208{
209/* init values one is going to cycle through while running this
210* example */
211struct text_preset_data init_data =
212{
213.font = {"DejaVu", "Courier", "Utopia"},
214.wrap = {"word", "char", "mixed", "none"},
215.obs_size = {50, 70, 100},
216.obs = {NULL, NULL},
217};
218
219d.t_data = init_data;
220d.t_data.font_ptr = d.t_data.font;
221d.t_data.obs_size_ptr = d.t_data.obs_size;
222d.t_data.obs_ptr = d.t_data.obs;
223
224d.win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get(),
225efl_text_set(efl_added, "Obstacles Example"),
226efl_ui_win_autodel_set(efl_added, EINA_TRUE));
227
228efl_gfx_entity_size_set(d.win, EINA_SIZE2D(WIDTH, HEIGHT));
229printf("Window size set to [%d,%d]\n", WIDTH, HEIGHT);
230
231efl_event_callback_add(d.win, EFL_UI_WIN_EVENT_DELETE_REQUEST, _gui_quit_cb, NULL);
232efl_event_callback_add(d.win, EFL_GFX_ENTITY_EVENT_SIZE_CHANGED, _win_resize, NULL);
233efl_event_callback_add(d.win, EFL_EVENT_KEY_DOWN, _key_down, NULL);
234
235d.bg = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
236efl_gfx_color_set(efl_added, 255, 255, 255, 255));
237
238efl_gfx_entity_size_set(d.bg, EINA_SIZE2D(WIDTH, HEIGHT));
239efl_gfx_entity_position_set(d.bg, EINA_POSITION2D(0, 0));
240
241d.text = efl_add(EFL_CANVAS_TEXTBLOCK_CLASS, d.win,
242efl_text_multiline_set(efl_added, EINA_TRUE));
243
244_text_init();
245efl_gfx_entity_size_set(d.text, EINA_SIZE2D(WIDTH, HEIGHT));
246efl_gfx_entity_position_set(d.text, EINA_POSITION2D(0, 0));
247
248d.size.w = WIDTH;
249d.size.h = HEIGHT;
250
251/* init obstacles */
252d.t_data.obs[0] = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
253efl_gfx_color_set(efl_added, 255, 0, 0, 255));
254
255efl_gfx_entity_size_set(d.t_data.obs[0], EINA_SIZE2D(50,50));
256
257d.t_data.obs[1] = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
258efl_gfx_color_set(efl_added, 0, 255, 0, 255));
259
260efl_gfx_entity_size_set(d.t_data.obs[1], EINA_SIZE2D(50,50));
261
262efl_canvas_textblock_obstacle_add(d.text, d.t_data.obs[0]);
263efl_canvas_textblock_obstacle_add(d.text, d.t_data.obs[1]);
264
265printf("%s\n", commands);
266}
267
268EAPI_MAIN void
269efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
270{
271_gui_setup();
272}
273EFL_MAIN()
274