summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorDaniel Hirt <daniel.hirt@samsung.com>2015-07-01 16:24:31 +0100
committerTom Hacohen <tom@stosb.com>2015-07-01 16:24:31 +0100
commite8afd5241cb0fc99a6f6d2ba0e83d8987507a03a (patch)
treec68e9381fceccf531603c904dbd4dea7d9d81283 /src/examples
parent87a88b5685cb92c72248848c3f41559392745bb9 (diff)
Evas Textblock: Add obstacle objects feature
Summary: Introducing a new feature for Evas Textblock. This allows the layout to wrap around other evas objects. The following API is added: - obstacle_add - obstacle_del - obstacle_update Evas objects can now serve as textblock obstacles, if positioned and visible on the text area. The text will wrap around the obstacles according to the wrapping mode set to it. This also modifies the current wrapping code to handle obstacle wrap points as well. The wrap index query function is modified so that forward-scanning (specific cases) may be disabled when treating obstacle wrap point. RTL text is currently unsupported by this feature. Consult added docs and example for usage. @feature Test Plan: Evas example and test in evas_suite are provided with this. Reviewers: tasn Subscribers: raster, JackDanielZ, cedric Differential Revision: https://phab.enlightenment.org/D2405
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/evas/Makefile.am5
-rw-r--r--src/examples/evas/Makefile.examples3
-rw-r--r--src/examples/evas/evas-textblock-obstacles.c311
3 files changed, 318 insertions, 1 deletions
diff --git a/src/examples/evas/Makefile.am b/src/examples/evas/Makefile.am
index 5c39cbd724..d2716522bf 100644
--- a/src/examples/evas/Makefile.am
+++ b/src/examples/evas/Makefile.am
@@ -159,6 +159,11 @@ evas_text_SOURCES = evas-text.c
159evas_text_LDADD = $(ECORE_EVAS_COMMON_LDADD) 159evas_text_LDADD = $(ECORE_EVAS_COMMON_LDADD)
160evas_text_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS) 160evas_text_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS)
161 161
162EXTRA_PROGRAMS += evas_textblock_obstacles
163evas_textblock_obstacles_SOURCES = evas-textblock-obstacles.c
164evas_textblock_obstacles_LDADD = $(ECORE_EVAS_COMMON_LDADD)
165evas_textblock_obstacles_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS)
166
162EXTRA_PROGRAMS += evas_smart_object 167EXTRA_PROGRAMS += evas_smart_object
163evas_smart_object_SOURCES = evas-smart-object.c 168evas_smart_object_SOURCES = evas-smart-object.c
164evas_smart_object_LDADD = $(ECORE_EVAS_COMMON_LDADD) 169evas_smart_object_LDADD = $(ECORE_EVAS_COMMON_LDADD)
diff --git a/src/examples/evas/Makefile.examples b/src/examples/evas/Makefile.examples
index 313be1b3b0..0d2b33a28b 100644
--- a/src/examples/evas/Makefile.examples
+++ b/src/examples/evas/Makefile.examples
@@ -23,7 +23,8 @@ EXAMPLES= evas-aspect-hints \
23 evas-smart-object \ 23 evas-smart-object \
24 evas-stacking \ 24 evas-stacking \
25 evas-table \ 25 evas-table \
26 evas-text 26 evas-text \
27 evas-textblock-obstacles
27 28
28all: edje examples 29all: edje examples
29edje: $(EDJE_OBJS) 30edje: $(EDJE_OBJS)
diff --git a/src/examples/evas/evas-textblock-obstacles.c b/src/examples/evas/evas-textblock-obstacles.c
new file mode 100644
index 0000000000..babcfaee84
--- /dev/null
+++ b/src/examples/evas/evas-textblock-obstacles.c
@@ -0,0 +1,311 @@
1/**
2 * Evas textblock example for obstacles feature
3 *
4 * You'll need at least one engine built for it (excluding the buffer
5 * one). See stdout/stderr for output.
6 *
7 * You start with two registered obstacle objects. They are not visible
8 * at first, so the textblock simply shows the text that has been set to it.
9 * Once the obstacle is visible (show/hide keys in the example), the text will
10 * wrap around it.
11 * This example allows you to test two obstacles registered to the same
12 * textblock object. Also, you can play with size and position for each.
13 * Use the 'h' key to show the provided options for this test.
14 *
15 * @verbatim
16 * gcc -o evas-textblock-obstacles evas-textblock-obstacles.c `pkg-config --libs --cflags evas ecore 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#include <stdio.h>
29#include <errno.h>
30#include "evas-common.h"
31
32#define WIDTH (320)
33#define HEIGHT (240)
34
35#define POINTER_CYCLE(_ptr, _array) \
36 do \
37 { \
38 if ((unsigned int)(((unsigned char *)(_ptr)) - ((unsigned char *)(_array))) >= \
39 sizeof(_array)) \
40 _ptr = _array; \
41 } \
42 while(0)
43
44static const char *commands = \
45 "commands are:\n"
46 "\tt - change currently controlled obstacle\n"
47 "\tv - show/hide current obstacle\n"
48 "\ts - cycle current obstacle's size\n"
49 "\tp - change current obstacle's position (random)\n"
50 "\tw - cycle text wrapping modes (none/word/char/mixed)\n"
51 "\th - print help\n";
52
53struct text_preset_data
54{
55 const char **font_ptr;
56 const char *font[3];
57
58 const char **wrap_ptr;
59 const char *wrap[4];
60
61 Evas_Coord *obs_size_ptr;
62 Evas_Coord obs_size[3];
63
64 Evas_Object **obs_ptr; /* pointer to the currently controlled obstacle object */
65 Evas_Object *obs[2];
66};
67
68struct test_data
69{
70 Ecore_Evas *ee;
71 Evas *evas;
72 struct text_preset_data t_data;
73 Evas_Object *text, *bg;
74 Evas_Coord w, h;
75 Evas_Textblock_Style *st;
76};
77
78static struct test_data d = {0};
79
80static void
81_on_destroy(Ecore_Evas *ee EINA_UNUSED)
82{
83 ecore_main_loop_quit();
84}
85
86static void
87_canvas_resize_cb(Ecore_Evas *ee)
88{
89 int w, h;
90
91 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
92 evas_object_resize(d.bg, w, h);
93 evas_object_resize(d.text, w, h);
94 d.w = w;
95 d.h = h;
96}
97
98static unsigned int
99_getrand(unsigned int low, unsigned int high)
100{
101 return (rand() % (high - low)) + low;
102}
103
104static void
105_style_set(const char *wrap)
106{
107 char buf[2000];
108 snprintf(buf,
109 2000,
110 "DEFAULT='font=Sans font_size=16 color=#000 wrap=%s text_class=entry'"
111 "br='\n'"
112 "ps='ps'"
113 "tab='\t'",
114 wrap);
115 evas_textblock_style_set(d.st, buf);
116}
117
118static void
119_on_keydown(void *data EINA_UNUSED,
120 Evas *evas EINA_UNUSED,
121 Evas_Object *o EINA_UNUSED,
122 void *einfo)
123{
124 Evas_Event_Key_Down *ev = einfo;
125
126 if (strcmp(ev->key, "h") == 0) /* print help */
127 {
128 fprintf(stdout, commands);
129 return;
130 }
131
132 if (strcmp(ev->key, "t") == 0) /* change obstacle type */
133 {
134 (d.t_data.obs_ptr)++;
135 POINTER_CYCLE(d.t_data.obs_ptr, d.t_data.obs);
136
137 fprintf(stdout, "Now controlling obstacle: %p\n", *d.t_data.obs_ptr);
138
139 return;
140 }
141
142 if (strcmp(ev->key, "v") == 0) /* change obstacle visibility */
143 {
144 Eo *obj = *d.t_data.obs_ptr;
145 if (evas_object_visible_get(obj))
146 evas_object_hide(obj);
147 else
148 evas_object_show(obj);
149 fprintf(stdout, "Show/hide toggle for obstacle %p\n",
150 *d.t_data.obs_ptr);
151 evas_object_textblock_obstacles_update(d.text);
152
153 return;
154 }
155
156 if (strcmp(ev->key, "s") == 0) /* change obstacle size */
157 {
158 (d.t_data.obs_size_ptr)++;
159 POINTER_CYCLE(d.t_data.obs_size_ptr, d.t_data.obs_size);
160
161 evas_object_resize(*d.t_data.obs_ptr,
162 *d.t_data.obs_size_ptr,
163 *d.t_data.obs_size_ptr);
164
165 evas_object_textblock_obstacles_update(d.text);
166
167 fprintf(stdout, "Changing obstacle size to: %d,%d\n", *d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr);
168
169 return;
170 }
171 if (strcmp(ev->key, "p") == 0) /* change obstacle position */
172 {
173 Evas_Coord x, y;
174 Evas_Coord rx, ry, gx, gy;
175 x = _getrand(0, d.w);
176 y = _getrand(0, d.h);
177 evas_object_move(*d.t_data.obs_ptr, x, y);
178 evas_object_textblock_obstacles_update(d.text);
179
180 fprintf(stdout, "Changing obstacles position\n");
181 evas_object_move(*d.t_data.obs_ptr, x, y);
182 evas_object_geometry_get(d.t_data.obs[0], &rx, &ry, NULL, NULL);
183 evas_object_geometry_get(d.t_data.obs[1], &gx, &gy, NULL, NULL);
184 fprintf(stdout, "Obstacle #1 (red) : [%d,%d]\n", rx, ry);
185 fprintf(stdout, "Obstacle #2 (green): [%d,%d]\n", gx, gy);
186
187 return;
188 }
189 if (strcmp(ev->key, "w") == 0) /* change obstacle position */
190 {
191 (d.t_data.wrap_ptr)++;
192 POINTER_CYCLE(d.t_data.wrap_ptr, d.t_data.wrap);
193 fprintf(stdout, "Changing wrap mode to: %s\n", *d.t_data.wrap_ptr);
194 _style_set(*d.t_data.wrap_ptr);
195 evas_object_textblock_obstacles_update(d.text);
196
197 return;
198 }
199}
200
201static void
202_obs_init(Evas_Object *obj)
203{
204 evas_object_resize(obj, 50, 50);
205}
206
207static void
208_text_init()
209{
210 d.st = evas_textblock_style_new();
211 evas_object_textblock_style_set(d.text, d.st);
212 _style_set("word");
213
214 evas_object_textblock_text_markup_set(d.text,
215 "This is an example text to demonstrate the textblock object"
216 " with obstacle objects support."
217 " Any evas object <item size=72x16></item>can register itself as an obstacle to the textblock"
218 " object. Upon reg<color=#0ff>stering, it aff</color>ects the layout of the text in"
219 " certain situations. Usually, when the obstacle shows above the text"
220 " area, it will cause the layout of the text to split and move"
221 " parts of it, so that all text area is apparent."
222 );
223}
224
225int
226main(void)
227{
228 if (!ecore_evas_init())
229 return EXIT_FAILURE;
230
231 /* example obstacles types */
232 Evas_Object *rect, *rect2;
233
234 /* init values one is going to cycle through while running this
235 * example */
236 struct text_preset_data init_data =
237 {
238 .font = {"DejaVu", "Courier", "Utopia"},
239 .wrap = {"word", "char", "mixed", "none"},
240 .obs_size = {50, 70, 100},
241 .obs = {NULL, NULL},
242 };
243
244 d.t_data = init_data;
245 d.t_data.font_ptr = d.t_data.font;
246 d.t_data.obs_size_ptr = d.t_data.obs_size;
247 d.t_data.obs_ptr = d.t_data.obs;
248
249 /* this will give you a window with an Evas canvas under the first
250 * engine available */
251 d.ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
252 if (!d.ee)
253 goto error;
254 printf("Window size set to [%d,%d]\n", WIDTH, HEIGHT);
255
256 ecore_evas_callback_delete_request_set(d.ee, _on_destroy);
257 ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb);
258 ecore_evas_show(d.ee);
259
260 d.evas = ecore_evas_get(d.ee);
261
262 d.bg = evas_object_rectangle_add(d.evas);
263 evas_object_color_set(d.bg, 255, 255, 255, 255); /* white bg */
264 evas_object_move(d.bg, 0, 0); /* at canvas' origin */
265 evas_object_resize(d.bg, WIDTH, HEIGHT); /* covers full canvas */
266 evas_object_show(d.bg);
267
268 evas_object_focus_set(d.bg, EINA_TRUE);
269 evas_object_event_callback_add(
270 d.bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
271
272 d.text = evas_object_textblock_add(d.evas);
273 _text_init();
274 evas_object_resize(d.text, WIDTH, HEIGHT);
275 evas_object_move(d.text, 0, 0);
276 evas_object_show(d.text);
277 d.w = WIDTH;
278 d.h = HEIGHT;
279
280 /* init obstacles */
281 rect = evas_object_rectangle_add(d.evas);
282 d.t_data.obs[0] = rect;
283 evas_object_color_set(rect, 255, 0, 0, 255);
284 _obs_init(rect);
285 rect2 = evas_object_rectangle_add(d.evas);
286 d.t_data.obs[1] = rect2;
287 evas_object_color_set(rect2, 0, 255, 0, 255);
288 _obs_init(rect2);
289
290 evas_object_textblock_obstacle_add(d.text, rect);
291 evas_object_textblock_obstacle_add(d.text, rect2);
292
293 evas_object_show(d.t_data.obs[0]);
294 evas_object_show(d.t_data.obs[1]);
295
296 fprintf(stdout, commands);
297 ecore_main_loop_begin();
298
299 evas_textblock_style_free(d.st);
300 ecore_evas_free(d.ee);
301 ecore_evas_shutdown();
302
303 return 0;
304
305error:
306 fprintf(stderr, "you got to have at least one evas engine built and linked"
307 " up to ecore-evas for this example to run properly.\n");
308 ecore_evas_shutdown();
309 return -1;
310}
311