aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/efl_ui_popup_anchor.c
blob: 20ff3d80fa6251d1a45c48f0baeaa1238d320f5b (plain) (blame)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#define EFL_UI_POPUP_PROTECTED
#define EFL_UI_POPUP_ANCHOR_BETA

#include <Elementary.h>

#include "elm_priv.h"
#include "efl_ui_popup_anchor_private.h"

#define MY_CLASS EFL_UI_POPUP_ANCHOR_CLASS
#define MY_CLASS_NAME "Efl.Ui.Popup.Anchor"

static void
_anchor_calc(Evas_Object *obj)
{
   ELM_WIDGET_DATA_GET_OR_RETURN(obj, wd);
   EFL_UI_POPUP_ANCHOR_DATA_GET(obj, sd);

   Evas_Coord_Rectangle anchor_geom = {0, 0, 0, 0};
   Evas_Coord_Size popup_size = {0, 0};
   Evas_Coord_Size parent_size = {0, 0};
   Eina_Position2D pos = {0, 0};

   Evas_Object *parent = efl_provider_find(obj, EFL_UI_WIN_CLASS);
   if (!parent)
     {
        ERR("Cannot find window parent");
        return;
     }

   evas_object_geometry_get(sd->anchor, &anchor_geom.x, &anchor_geom.y, &anchor_geom.w, &anchor_geom.h);
   evas_object_geometry_get(obj, NULL, NULL, &popup_size.w, &popup_size.h);
   evas_object_geometry_get(parent, NULL, NULL, &parent_size.w, &parent_size.h);

   sd->used_align = EFL_UI_POPUP_ALIGN_NONE;

   /* 1. Find align which display popup.
         It enables to shifting popup from exact position.
         LEFT, RIGHT - shift only y position within anchor object's height
         TOP, BOTTOM - shift only x position within anchor object's width
         CENTER - shift both x, y position within anchor object's area
    */

   for (int idx = 0; idx < 6; idx++)
     {
        Efl_Ui_Popup_Align cur_align;

        if (idx == 0)
          cur_align = sd->align;
        else
          cur_align = sd->priority[idx - 1];

        if (cur_align == EFL_UI_POPUP_ALIGN_NONE)
          continue;

        switch(cur_align)
          {
             case EFL_UI_POPUP_ALIGN_TOP:
               pos.x = anchor_geom.x + ((anchor_geom.w - popup_size.w) / 2);
               pos.y = (anchor_geom.y - popup_size.h);

               if ((pos.y < 0) ||
                   ((pos.y + popup_size.h) > parent_size.h) ||
                   (popup_size.w > parent_size.w))
                 continue;
               break;

             case EFL_UI_POPUP_ALIGN_LEFT:
               pos.x = (anchor_geom.x - popup_size.w);
               pos.y = anchor_geom.y + ((anchor_geom.h - popup_size.h) / 2);

               if ((pos.x < 0) ||
                   ((pos.x + popup_size.w) > parent_size.w) ||
                   (popup_size.h > parent_size.h))
                 continue;
               break;

             case EFL_UI_POPUP_ALIGN_RIGHT:
               pos.x = (anchor_geom.x + anchor_geom.w);
               pos.y = anchor_geom.y + ((anchor_geom.h - popup_size.h) / 2);

               if ((pos.x < 0) ||
                   ((pos.x + popup_size.w) > parent_size.w) ||
                   (popup_size.h > parent_size.h))
                 continue;
               break;

             case EFL_UI_POPUP_ALIGN_BOTTOM:
               pos.x = anchor_geom.x + ((anchor_geom.w - popup_size.w) / 2);
               pos.y = (anchor_geom.y + anchor_geom.h);

               if ((pos.y < 0) ||
                   ((pos.y + popup_size.h) > parent_size.h) ||
                   (popup_size.w > parent_size.w))
                 continue;
               break;

             case EFL_UI_POPUP_ALIGN_CENTER:
               pos.x = anchor_geom.x + ((anchor_geom.w - popup_size.w) / 2);
               pos.y = anchor_geom.y + ((anchor_geom.h - popup_size.h) / 2);

               if (popup_size.w > parent_size.w || popup_size.h > parent_size.h)
                 continue;
               break;

             default:
               continue;
          }

        if ((cur_align == EFL_UI_POPUP_ALIGN_TOP) ||
            (cur_align == EFL_UI_POPUP_ALIGN_BOTTOM) ||
            (cur_align == EFL_UI_POPUP_ALIGN_CENTER))
          {
             if (pos.x < 0)
               pos.x = 0;
             if ((pos.x + popup_size.w) > parent_size.w)
               pos.x = parent_size.w - popup_size.w;

             if ((pos.x > (anchor_geom.x + anchor_geom.w)) ||
                 ((pos.x + popup_size.w) < anchor_geom.x))
               continue;
          }

        if ((cur_align == EFL_UI_POPUP_ALIGN_LEFT) ||
            (cur_align == EFL_UI_POPUP_ALIGN_RIGHT) ||
            (cur_align == EFL_UI_POPUP_ALIGN_CENTER))
          {
             if (pos.y < 0)
               pos.y = 0;
             if ((pos.y + popup_size.h) > parent_size.h)
               pos.y = parent_size.h - popup_size.h;

             if ((pos.y > (anchor_geom.y + anchor_geom.h)) ||
                 ((pos.y + popup_size.h) < anchor_geom.y))
               continue;
          }

        sd->used_align = cur_align;
        goto end;
     }

   /* 2. Move popup to fit first valid align although entire popup can't display */

   for (int idx = 0; idx < 6; idx++)
     {
         Efl_Ui_Popup_Align cur_align;

         if (idx == 0)
           cur_align = sd->align;
         else
           cur_align = sd->priority[idx - 1];

         if (cur_align == EFL_UI_POPUP_ALIGN_NONE)
           continue;

         switch(cur_align)
           {
             case EFL_UI_POPUP_ALIGN_TOP:
               pos.x = anchor_geom.x + ((anchor_geom.w - popup_size.w) / 2);
               pos.y = (anchor_geom.y - popup_size.h);
               sd->used_align = cur_align;
               goto end;
               break;

             case EFL_UI_POPUP_ALIGN_LEFT:
               pos.x = (anchor_geom.x - popup_size.w);
               pos.y = anchor_geom.y + ((anchor_geom.h - popup_size.h) / 2);
               sd->used_align = cur_align;
               goto end;
               break;

             case EFL_UI_POPUP_ALIGN_RIGHT:
               pos.x = (anchor_geom.x + anchor_geom.w);
               pos.y = anchor_geom.y + ((anchor_geom.h - popup_size.h) / 2);
               sd->used_align = cur_align;
               goto end;
               break;

             case EFL_UI_POPUP_ALIGN_BOTTOM:
               pos.x = anchor_geom.x + ((anchor_geom.w - popup_size.w) / 2);
               pos.y = (anchor_geom.y + anchor_geom.h);
               sd->used_align = cur_align;
               goto end;
               break;

             case EFL_UI_POPUP_ALIGN_CENTER:
               pos.x = anchor_geom.x + ((anchor_geom.w - popup_size.w) / 2);
               pos.y = anchor_geom.y + ((anchor_geom.h - popup_size.h) / 2);
               sd->used_align = cur_align;
               goto end;
               break;

             default:
               break;
           }
     }

end:
   if (sd->used_align != EFL_UI_POPUP_ALIGN_NONE)
     efl_gfx_position_set(efl_super(obj, EFL_UI_POPUP_CLASS), pos);
}

static void
_anchor_geom_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   _anchor_calc(data);
}

static void
_anchor_del_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   EFL_UI_POPUP_ANCHOR_DATA_GET(data, sd);

   Evas_Object *parent = efl_provider_find(data, EFL_UI_WIN_CLASS);
   if (!parent)
     {
        ERR("Cannot find window parent");
        return;
     }

   efl_event_callback_del(parent, EFL_GFX_EVENT_RESIZE, _anchor_geom_cb, data);

   sd->anchor = NULL;
   efl_ui_popup_align_set(efl_super(data, MY_CLASS), sd->align);
}

static void
_anchor_detach(Evas_Object *obj)
{
   EFL_UI_POPUP_ANCHOR_DATA_GET(obj, sd);

   if (sd->anchor == NULL) return;

   Evas_Object *parent = efl_provider_find(obj, EFL_UI_WIN_CLASS);
   if (!parent)
     {
        ERR("Cannot find window parent");
        return;
     }

   efl_event_callback_del(parent, EFL_GFX_EVENT_RESIZE, _anchor_geom_cb, obj);
   efl_event_callback_del(sd->anchor, EFL_GFX_EVENT_RESIZE, _anchor_geom_cb, obj);
   efl_event_callback_del(sd->anchor, EFL_GFX_EVENT_MOVE, _anchor_geom_cb, obj);
   efl_event_callback_del(sd->anchor, EFL_EVENT_DEL, _anchor_del_cb, obj);
}

EOLIAN static void
_efl_ui_popup_anchor_anchor_set(Eo *obj, Efl_Ui_Popup_Anchor_Data *pd, Evas_Object *anchor)
{
   _anchor_detach(obj);
   pd->anchor = anchor;

   if (anchor == NULL)
      efl_ui_popup_align_set(efl_super(obj, MY_CLASS), pd->align);
   else
     {
        efl_ui_popup_align_set(efl_super(obj, MY_CLASS), EFL_UI_POPUP_ALIGN_NONE);

        _anchor_calc(obj);

        Evas_Object *parent = efl_provider_find(obj, EFL_UI_WIN_CLASS);
        if (!parent)
          {
              ERR("Cannot find window parent");
              return;
          }

        efl_event_callback_add(parent, EFL_GFX_EVENT_RESIZE, _anchor_geom_cb, obj);
        efl_event_callback_add(anchor, EFL_GFX_EVENT_RESIZE, _anchor_geom_cb, obj);
        efl_event_callback_add(anchor, EFL_GFX_EVENT_MOVE, _anchor_geom_cb, obj);
        efl_event_callback_add(anchor, EFL_EVENT_DEL, _anchor_del_cb, obj);
     }
}

EOLIAN static Efl_Object *
_efl_ui_popup_anchor_anchor_get(Eo *obj EINA_UNUSED, Efl_Ui_Popup_Anchor_Data *pd)
{
   return pd->anchor;
}

EOLIAN static void
_efl_ui_popup_anchor_align_priority_set(Eo *obj EINA_UNUSED,
                                        Efl_Ui_Popup_Anchor_Data *pd,
                                        Efl_Ui_Popup_Align first,
                                        Efl_Ui_Popup_Align second,
                                        Efl_Ui_Popup_Align third,
                                        Efl_Ui_Popup_Align fourth,
                                        Efl_Ui_Popup_Align fifth)
{
   pd->priority[0] = first;
   pd->priority[1] = second;
   pd->priority[2] = third;
   pd->priority[3] = fourth;
   pd->priority[4] = fifth;
}

EOLIAN static void
_efl_ui_popup_anchor_align_priority_get(Eo *obj EINA_UNUSED,
                                        Efl_Ui_Popup_Anchor_Data *pd,
                                        Efl_Ui_Popup_Align *first,
                                        Efl_Ui_Popup_Align *second,
                                        Efl_Ui_Popup_Align *third,
                                        Efl_Ui_Popup_Align *fourth,
                                        Efl_Ui_Popup_Align *fifth)
{
   if (first) *first = pd->priority[0];
   if (second) *second = pd->priority[1];
   if (third) *third = pd->priority[2];
   if (fourth) *fourth = pd->priority[3];
   if (fifth) *fifth = pd->priority[4];
}

EOLIAN static void
_efl_ui_popup_anchor_efl_gfx_position_set(Eo *obj, Efl_Ui_Popup_Anchor_Data *pd EINA_UNUSED, Eina_Position2D pos)
{
   _anchor_detach(obj);

   pd->anchor = NULL;
   pd->align = EFL_UI_POPUP_ALIGN_NONE;

   efl_gfx_position_set(efl_super(obj, MY_CLASS), pos);
}

EOLIAN static void
_efl_ui_popup_anchor_elm_layout_sizing_eval(Eo *obj, Efl_Ui_Popup_Anchor_Data *pd EINA_UNUSED)
{
   elm_layout_sizing_eval(efl_super(obj, MY_CLASS));

   if (pd->anchor != NULL)
     _anchor_calc(obj);
}

EOLIAN static void
_efl_ui_popup_anchor_efl_ui_popup_align_set(Eo *obj, Efl_Ui_Popup_Anchor_Data *pd, Efl_Ui_Popup_Align type)
{
   pd->align = type;
   if (pd->anchor == NULL)
      efl_ui_popup_align_set(efl_super(obj, MY_CLASS), type);
   else
      _anchor_calc(obj);
}

EOLIAN static Efl_Ui_Popup_Align
_efl_ui_popup_anchor_efl_ui_popup_align_get(Eo *obj EINA_UNUSED, Efl_Ui_Popup_Anchor_Data *pd)
{
   return pd->align;
}

EOLIAN static void
_efl_ui_popup_anchor_efl_canvas_group_group_add(Eo *obj, Efl_Ui_Popup_Anchor_Data *pd EINA_UNUSED)
{
   ELM_WIDGET_DATA_GET_OR_RETURN(obj, wd);

   efl_canvas_group_add(efl_super(obj, MY_CLASS));
   elm_widget_sub_object_parent_add(obj);

   pd->priority[0] = EFL_UI_POPUP_ALIGN_TOP;
   pd->priority[1] = EFL_UI_POPUP_ALIGN_LEFT;
   pd->priority[2] = EFL_UI_POPUP_ALIGN_RIGHT;
   pd->priority[3] = EFL_UI_POPUP_ALIGN_BOTTOM;
   pd->priority[4] = EFL_UI_POPUP_ALIGN_CENTER;
}

EOLIAN static void
_efl_ui_popup_anchor_efl_canvas_group_group_del(Eo *obj, Efl_Ui_Popup_Anchor_Data *pd EINA_UNUSED)
{
   efl_canvas_group_del(efl_super(obj, MY_CLASS));
}

EOLIAN static void
_efl_ui_popup_anchor_class_constructor(Efl_Class *klass)
{
   evas_smart_legacy_type_register(MY_CLASS_NAME, klass);
}

#define EFL_UI_POPUP_ANCHOR_EXTRA_OPS \
   EFL_CANVAS_GROUP_ADD_DEL_OPS(efl_ui_popup_anchor), \
   ELM_LAYOUT_SIZING_EVAL_OPS(efl_ui_popup_anchor)

#include "efl_ui_popup_anchor.eo.c"