summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorabdulleh Ghujeh <a.ghujeh@samsung.com>2020-01-03 11:24:35 +0100
committerXavi Artigas <xavierartigas@yahoo.es>2020-01-03 11:42:31 +0100
commit7107d6cf903ef3587c7af46ca9edd72aa983e5fc (patch)
tree7b0948dc970eeb9b0f7a04e7affcdf7c5f9001e9 /src/examples
parentc9cc3ab3f0d018434157913914fb68e49ec602da (diff)
Example: Add efl_canvas_textblock_obstacles example
Summary: Add efl canvas textblock example based on unified APIs (converted from legacy) Reviewers: ali.alzyod, segfaultxavi Reviewed By: segfaultxavi Subscribers: woohyun, segfaultxavi, cedric, #reviewers, #committers Tags: #efl Maniphest Tasks: T8460 Differential Revision: https://phab.enlightenment.org/D10955
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/elementary/.gitignore1
-rw-r--r--src/examples/elementary/efl_canvas_textblock_obstacles_example.c273
-rw-r--r--src/examples/elementary/meson.build3
3 files changed, 276 insertions, 1 deletions
diff --git a/src/examples/elementary/.gitignore b/src/examples/elementary/.gitignore
index 89ec3ec650..84d951ef90 100644
--- a/src/examples/elementary/.gitignore
+++ b/src/examples/elementary/.gitignore
@@ -171,3 +171,4 @@
171/efl_ui_theme_example_02 171/efl_ui_theme_example_02
172/efl_ui_relative_container_example_01 172/efl_ui_relative_container_example_01
173/efl_ui_relative_container_example_02 173/efl_ui_relative_container_example_02
174/efl_canvas_textblock_obstacles_example
diff --git a/src/examples/elementary/efl_canvas_textblock_obstacles_example.c b/src/examples/elementary/efl_canvas_textblock_obstacles_example.c
new file mode 100644
index 0000000000..d14123a801
--- /dev/null
+++ b/src/examples/elementary/efl_canvas_textblock_obstacles_example.c
@@ -0,0 +1,273 @@
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) \
25 do \
26 { \
27 if ((unsigned int)(((unsigned char *)(_ptr)) - ((unsigned char *)(_array))) >= \
28 sizeof(_array)) \
29 _ptr = _array; \
30 } \
31 while(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{
44 const char **font_ptr;
45 const char *font[3];
46
47 const char **wrap_ptr;
48 const char *wrap[4];
49
50 int *obs_size_ptr;
51 int obs_size[3];
52
53 Eo **obs_ptr; /* pointer to the currently controlled obstacle object */
54 Eo *obs[2];
55};
56
57struct test_data
58{
59 Eo *win, *box, *bg, *text;
60 struct text_preset_data t_data;
61 Eina_Size2D size;
62};
63
64static struct test_data d = {0};
65
66static unsigned int
67_getrand(unsigned int low, unsigned int high)
68{
69 return (rand() % (high - low)) + low;
70}
71
72static void
73_style_set(const char *wrap)
74{
75 char buf[2000];
76 snprintf(buf,
77 2000,
78 "font=Sans font_size=16 color=#000 wrap=%s",
79 wrap);
80
81 efl_canvas_textblock_style_apply(d.text, buf);
82}
83
84static void
85_key_down(void *data EINA_UNUSED, const Efl_Event *ev)
86{
87 const char *key = efl_input_key_string_get(ev->info);
88 if (!key)
89 return;
90
91 if (strcmp(key, "h") == 0) /* print help */
92 {
93 printf("%s\n", commands);
94 return;
95 }
96
97 if (strcmp(key, "t") == 0) /* change obstacle type */
98 {
99 (d.t_data.obs_ptr)++;
100 POINTER_CYCLE(d.t_data.obs_ptr, d.t_data.obs);
101
102 printf("Now controlling obstacle: %p\n", *d.t_data.obs_ptr);
103
104 return;
105 }
106
107 if (strcmp(key, "v") == 0) /* change obstacle visibility */
108 {
109 Eo *obj = *d.t_data.obs_ptr;
110 if (efl_gfx_entity_visible_get(obj))
111 efl_gfx_entity_visible_set(obj, EINA_FALSE);
112 else
113 efl_gfx_entity_visible_set(obj, EINA_TRUE);
114
115 printf("Show/hide toggle for obstacle %p\n",
116 *d.t_data.obs_ptr);
117
118 efl_canvas_textblock_obstacles_update(d.text);
119
120 return;
121 }
122
123 if (strcmp(key, "s") == 0) /* change obstacle size */
124 {
125 (d.t_data.obs_size_ptr)++;
126 POINTER_CYCLE(d.t_data.obs_size_ptr, d.t_data.obs_size);
127
128 efl_gfx_entity_size_set(*d.t_data.obs_ptr, EINA_SIZE2D(*d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr));
129
130 efl_canvas_textblock_obstacles_update(d.text);
131
132 printf("Changing obstacle size to: %d,%d\n",
133 *d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr);
134
135 return;
136 }
137
138 if (strcmp(key, "p") == 0) /* change obstacle position */
139 {
140 int x, y;
141 x = _getrand(0, d.size.w);
142 y = _getrand(0, d.size.h);
143
144 efl_gfx_entity_position_set(*d.t_data.obs_ptr, EINA_POSITION2D(x, y));
145 efl_canvas_textblock_obstacles_update(d.text);
146
147 printf("Changing obstacles position\n");
148 efl_gfx_entity_position_set(*d.t_data.obs_ptr, EINA_POSITION2D(x, y));
149
150 Eina_Position2D r_rec = efl_gfx_entity_position_get(d.t_data.obs[0]);
151 Eina_Position2D g_rec = efl_gfx_entity_position_get(d.t_data.obs[1]);
152
153 printf("Obstacle #1 (red) : [%d,%d]\n", r_rec.x, r_rec.y);
154 printf("Obstacle #2 (green): [%d,%d]\n", g_rec.x, g_rec.y);
155
156 return;
157 }
158
159 if (strcmp(key, "w") == 0) /* change obstacle position */
160 {
161 (d.t_data.wrap_ptr)++;
162 POINTER_CYCLE(d.t_data.wrap_ptr, d.t_data.wrap);
163 printf("Changing wrap mode to: %s\n",
164 *d.t_data.wrap_ptr);
165 _style_set(*d.t_data.wrap_ptr);
166 efl_canvas_textblock_obstacles_update(d.text);
167
168 return;
169 }
170}
171
172static void
173_win_resize(void *data EINA_UNUSED, const Efl_Event *ev)
174{
175 Eina_Size2D sz;
176
177 sz = efl_gfx_entity_size_get(ev->object);
178 efl_gfx_entity_size_set(d.bg, sz);
179 efl_gfx_entity_size_set(d.text, sz);
180
181 d.size = sz;
182}
183
184static void
185_text_init()
186{
187 _style_set("word");
188
189 efl_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{
203 efl_exit(0);
204}
205
206static void
207_gui_setup()
208{
209 /* init values one is going to cycle through while running this
210 * example */
211 struct 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
219 d.t_data = init_data;
220 d.t_data.font_ptr = d.t_data.font;
221 d.t_data.obs_size_ptr = d.t_data.obs_size;
222 d.t_data.obs_ptr = d.t_data.obs;
223
224 d.win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get(),
225 efl_text_set(efl_added, "Obstacles Example"),
226 efl_ui_win_autodel_set(efl_added, EINA_TRUE));
227
228 efl_gfx_entity_size_set(d.win, EINA_SIZE2D(WIDTH, HEIGHT));
229 printf("Window size set to [%d,%d]\n", WIDTH, HEIGHT);
230
231 efl_event_callback_add(d.win, EFL_UI_WIN_EVENT_DELETE_REQUEST, _gui_quit_cb, NULL);
232 efl_event_callback_add(d.win, EFL_GFX_ENTITY_EVENT_SIZE_CHANGED, _win_resize, NULL);
233 efl_event_callback_add(d.win, EFL_EVENT_KEY_DOWN, _key_down, NULL);
234
235 d.bg = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
236 efl_gfx_color_set(efl_added, 255, 255, 255, 255));
237
238 efl_gfx_entity_size_set(d.bg, EINA_SIZE2D(WIDTH, HEIGHT));
239 efl_gfx_entity_position_set(d.bg, EINA_POSITION2D(0, 0));
240
241 d.text = efl_add(EFL_CANVAS_TEXTBLOCK_CLASS, d.win,
242 efl_text_multiline_set(efl_added, EINA_TRUE));
243
244 _text_init();
245 efl_gfx_entity_size_set(d.text, EINA_SIZE2D(WIDTH, HEIGHT));
246 efl_gfx_entity_position_set(d.text, EINA_POSITION2D(0, 0));
247
248 d.size.w = WIDTH;
249 d.size.h = HEIGHT;
250
251 /* init obstacles */
252 d.t_data.obs[0] = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
253 efl_gfx_color_set(efl_added, 255, 0, 0, 255));
254
255 efl_gfx_entity_size_set(d.t_data.obs[0], EINA_SIZE2D(50,50));
256
257 d.t_data.obs[1] = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
258 efl_gfx_color_set(efl_added, 0, 255, 0, 255));
259
260 efl_gfx_entity_size_set(d.t_data.obs[1], EINA_SIZE2D(50,50));
261
262 efl_canvas_textblock_obstacle_add(d.text, d.t_data.obs[0]);
263 efl_canvas_textblock_obstacle_add(d.text, d.t_data.obs[1]);
264
265 printf("%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()
diff --git a/src/examples/elementary/meson.build b/src/examples/elementary/meson.build
index e88ddfe567..cdb08d4b35 100644
--- a/src/examples/elementary/meson.build
+++ b/src/examples/elementary/meson.build
@@ -120,7 +120,8 @@ examples = [
120 'efl_ui_slideshow_example', 120 'efl_ui_slideshow_example',
121 'efl_ui_radio_example_01', 121 'efl_ui_radio_example_01',
122 'efl_ui_grid_example_1', 122 'efl_ui_grid_example_1',
123 'efl_ui_grid_view_example_1' 123 'efl_ui_grid_view_example_1',
124 'efl_canvas_textblock_obstacles_example'
124] 125]
125 126
126foreach example : examples 127foreach example : examples