summaryrefslogtreecommitdiff
path: root/src/examples/elementary/efl_canvas_textblock_obstacles_example.c
blob: b2aca7ee2b601df57d1ff6c9a436173e5f0a6358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#define EFL_BETA_API_SUPPORT 1

#include <Efl_Ui.h>

 /**
 * Example of canvas textblock obstacles.
 *
 * You start with two registered obstacle objects. They are not visible
 * at first, so the canvas textblock simply shows the text that has been set to it.
 * Once the obstacle is visible (show/hide keys in the example), the text will
 * wrap around it.
 * This example allows you to test two obstacles registered to the same
 * canvas textblock object. Also, you can play with size and position for each.
 * Use the 'h' key to show the provided options for this test.
 *
 * @verbatim
 * gcc -g efl_canvas_textblock_obstacles_example.c -o efl_canvas_textblock_obstacles_example `pkg-config --cflags --libs elementary`
 * @endverbatim
 */

#define WIDTH  (360)
#define HEIGHT (240)

#define POINTER_CYCLE(_ptr, _array)                             \
  do                                                            \
    {                                                           \
       if ((unsigned int)(((unsigned char *)(_ptr)) - ((unsigned char *)(_array))) >= \
           sizeof(_array))                                      \
         _ptr = _array;                                         \
    }                                                           \
  while(0)

static const char *commands = \
  "commands are:\n"
  "\tt - change currently controlled obstacle\n"
  "\tv - show/hide current obstacle\n"
  "\ts - cycle current obstacle's size\n"
  "\tp - change current obstacle's position (random)\n"
  "\tw - cycle text wrapping modes (none/word/char/mixed)\n"
  "\th - print help\n";

struct text_preset_data
{
   const char        **font_ptr;
   const char         *font[3];

   const char        **wrap_ptr;
   const char         *wrap[4];

   int                *obs_size_ptr;
   int                 obs_size[3];

   Eo **obs_ptr; /* pointer to the currently controlled obstacle object */
   Eo *obs[2];
};

struct test_data
{
   Eo                     *win, *box, *bg, *text;
   struct text_preset_data t_data;
   Eina_Size2D             size;
};

static struct test_data d = {0};

static unsigned int
_getrand(unsigned int low, unsigned int high)
{
   return (rand() % (high - low)) + low;
}

static void
_style_set(const char *wrap)
{
   char buf[2000];
   snprintf(buf,
         2000,
         "font=Sans font_size=16 color=#000 wrap=%s",
         wrap);

   efl_canvas_textblock_style_apply(d.text, buf);
}

static void
_key_down(void *data EINA_UNUSED, const Efl_Event *ev)
{
   const char *key = efl_input_key_string_get(ev->info);
   if (!key)
     return;

   if (strcmp(key, "h") == 0) /* print help */
     {
        printf("%s\n", commands);
        return;
     }

   if (strcmp(key, "t") == 0) /* change obstacle type */
     {
        (d.t_data.obs_ptr)++;
        POINTER_CYCLE(d.t_data.obs_ptr, d.t_data.obs);

        printf("Now controlling obstacle: %p\n", *d.t_data.obs_ptr);

        return;
     }

   if (strcmp(key, "v") == 0) /* change obstacle visibility */
     {
        Eo *obj = *d.t_data.obs_ptr;
        if (efl_gfx_entity_visible_get(obj))
           efl_gfx_entity_visible_set(obj, EINA_FALSE);
        else
           efl_gfx_entity_visible_set(obj, EINA_TRUE);

        printf("Show/hide toggle for obstacle %p\n",
               *d.t_data.obs_ptr);

        efl_canvas_textblock_obstacles_update(d.text);

        return;
     }

   if (strcmp(key, "s") == 0) /* change obstacle size */
     {
        (d.t_data.obs_size_ptr)++;
        POINTER_CYCLE(d.t_data.obs_size_ptr, d.t_data.obs_size);

        efl_gfx_entity_size_set(*d.t_data.obs_ptr, EINA_SIZE2D(*d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr));

        efl_canvas_textblock_obstacles_update(d.text);

        printf("Changing obstacle size to: %d,%d\n",
               *d.t_data.obs_size_ptr, *d.t_data.obs_size_ptr);

        return;
     }

   if (strcmp(key, "p") == 0) /* change obstacle position */
     {
        int  x, y;
        x = _getrand(0, d.size.w);
        y = _getrand(0, d.size.h);

        efl_gfx_entity_position_set(*d.t_data.obs_ptr, EINA_POSITION2D(x, y));
        efl_canvas_textblock_obstacles_update(d.text);

        printf("Changing obstacles position\n");
        efl_gfx_entity_position_set(*d.t_data.obs_ptr, EINA_POSITION2D(x, y));

        Eina_Position2D r_rec = efl_gfx_entity_position_get(d.t_data.obs[0]);
        Eina_Position2D g_rec = efl_gfx_entity_position_get(d.t_data.obs[1]);

        printf("Obstacle #1 (red)  : [%d,%d]\n", r_rec.x, r_rec.y);
        printf("Obstacle #2 (green): [%d,%d]\n", g_rec.x, g_rec.y);

        return;
     }

   if (strcmp(key, "w") == 0) /* change obstacle position */
     {
        (d.t_data.wrap_ptr)++;
        POINTER_CYCLE(d.t_data.wrap_ptr, d.t_data.wrap);
        printf("Changing wrap mode to: %s\n",
               *d.t_data.wrap_ptr);
        _style_set(*d.t_data.wrap_ptr);
        efl_canvas_textblock_obstacles_update(d.text);

        return;
     }
}

static void
_win_resize(void *data EINA_UNUSED, const Efl_Event *ev)
{
   Eina_Size2D sz;

   sz = efl_gfx_entity_size_get(ev->object);
   efl_gfx_entity_size_set(d.bg, sz);
   efl_gfx_entity_size_set(d.text, sz);

   d.size = sz;
}

static void
_text_init()
{
   _style_set("word");

   efl_text_markup_set(d.text,
         "This example text demonstrates the textblock object"
         " with obstacle objects support."
         " Any evas object <item size=72x16></item>can register itself as an obstacle to the textblock"
         " object. Upon regi<color=#0ff>stering, it aff</color>ects the layout of the text in"
         " certain situations. Usually, when the obstacle shows above the text"
         " area, it will cause the layout of the text to split and move"
         " parts of it, so that all text area is apparent."
         );
}

static void
_gui_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
   efl_exit(0);
}

static void
_gui_setup()
{
   /* init values one is going to cycle through while running this
    * example */
   struct text_preset_data init_data =
   {
      .font = {"DejaVu", "Courier", "Utopia"},
      .wrap = {"word", "char", "mixed", "none"},
      .obs_size = {50, 70, 100},
      .obs = {NULL, NULL},
   };

   d.t_data = init_data;
   d.t_data.font_ptr = d.t_data.font;
   d.t_data.obs_size_ptr = d.t_data.obs_size;
   d.t_data.obs_ptr = d.t_data.obs;

   d.win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get(),
                 efl_text_set(efl_added, "Obstacles Example"),
                 efl_ui_win_autodel_set(efl_added, EINA_TRUE));

   efl_gfx_entity_size_set(d.win, EINA_SIZE2D(WIDTH, HEIGHT));
   printf("Window size set to [%d,%d]\n", WIDTH, HEIGHT);

   efl_event_callback_add(d.win, EFL_UI_WIN_EVENT_DELETE_REQUEST, _gui_quit_cb, NULL);
   efl_event_callback_add(d.win, EFL_GFX_ENTITY_EVENT_SIZE_CHANGED, _win_resize, NULL);
   efl_event_callback_add(d.win, EFL_EVENT_KEY_DOWN, _key_down, NULL);

   d.bg = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
           efl_gfx_color_set(efl_added, 255, 255, 255, 255));

   efl_gfx_entity_size_set(d.bg, EINA_SIZE2D(WIDTH, HEIGHT));
   efl_gfx_entity_position_set(d.bg, EINA_POSITION2D(0, 0));

   d.text = efl_add(EFL_CANVAS_TEXTBLOCK_CLASS, d.win,
           efl_text_multiline_set(efl_added, EINA_TRUE));

   _text_init();
   efl_gfx_entity_size_set(d.text, EINA_SIZE2D(WIDTH, HEIGHT));
   efl_gfx_entity_position_set(d.text, EINA_POSITION2D(0, 0));

   d.size.w = WIDTH;
   d.size.h = HEIGHT;

   /* init obstacles */
   d.t_data.obs[0] = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
           efl_gfx_color_set(efl_added, 255, 0, 0, 255));

   efl_gfx_entity_size_set(d.t_data.obs[0], EINA_SIZE2D(50,50));

   d.t_data.obs[1] = efl_add(EFL_CANVAS_RECTANGLE_CLASS, d.win,
           efl_gfx_color_set(efl_added, 0, 255, 0, 255));

   efl_gfx_entity_size_set(d.t_data.obs[1], EINA_SIZE2D(50,50));

   efl_canvas_textblock_obstacle_add(d.text, d.t_data.obs[0]);
   efl_canvas_textblock_obstacle_add(d.text, d.t_data.obs[1]);

   printf("%s\n", commands);
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   _gui_setup();
}
EFL_MAIN()