aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/efl_ui_box_flow.c
blob: 2600acffd90f41f042e45e6a8d16e92ce4f8aba1 (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
#include "efl_ui_box_private.h"
#include "efl_ui_container_layout.h"

#define MY_CLASS EFL_UI_BOX_FLOW_CLASS

typedef struct _Efl_Ui_Box_Flow_Data Efl_Ui_Box_Flow_Data;

struct _Efl_Ui_Box_Flow_Data
{
};

typedef struct _Item_Calc Item_Calc;
typedef struct _Row_Calc Row_Calc;

struct _Item_Calc
{
   EINA_INLIST;

   Evas_Object *obj;
   Row_Calc *row;
   double weight_factor;
   Efl_Ui_Container_Item_Hints hints[2]; /* 0 is x-axis, 1 is y-axis */
};

struct _Row_Calc
{
   EINA_INLIST;

   Evas_Object *obj;
   int item_count;
   int min_sum;
   int hgsize;
   double weight_sum;
   double cross_weight;
   double cross_space;
   double cur_pos;
   double weight_factor;
   Efl_Ui_Container_Item_Hints hint;
};

static int
_item_weight_sort_cb(const void *l1, const void *l2)
{
   Item_Calc *it1, *it2;

   it1 = EINA_INLIST_CONTAINER_GET(l1, Item_Calc);
   it2 = EINA_INLIST_CONTAINER_GET(l2, Item_Calc);

   return it2->weight_factor <= it1->weight_factor ? -1 : 1;
}

static int
_row_weight_sort_cb(const void *l1, const void *l2)
{
   Row_Calc *it1, *it2;

   it1 = EINA_INLIST_CONTAINER_GET(l1, Row_Calc);
   it2 = EINA_INLIST_CONTAINER_GET(l2, Row_Calc);

   return it2->weight_factor <= it1->weight_factor ? -1 : 1;
}

EOLIAN static void
_efl_ui_box_flow_efl_pack_layout_layout_update(Eo *obj, Efl_Ui_Box_Flow_Data *pd EINA_UNUSED)
{
   Efl_Ui_Box_Data *bd = efl_data_scope_get(obj, EFL_UI_BOX_CLASS);
   Eo *child;
   Eina_List *li;
   Eina_Inlist *inlist = NULL;
   Item_Calc *items, *item;
   Row_Calc *rows, *row;
   Efl_Ui_Container_Item_Hints *hints, *hint;
   Eina_Bool axis = !efl_ui_dir_is_horizontal(bd->dir, EINA_FALSE);
   Eina_Bool c_axis = !axis;
   int want[2] = { 0, 0 };
   int rc = 0, count, i = 0, id, item_last = 0;
   double cur_pos, cross_weight_sum = 0, cross_min_sum = 0, min_sum = 0;
   Efl_Ui_Container_Layout_Calc box_calc[2]; /* 0 is x-axis, 1 is y-axis */

   count = eina_list_count(bd->children);
   if (!count)
     {
        efl_gfx_hint_size_restricted_min_set(obj, EINA_SIZE2D(0, 0));
        return;
     }

   _efl_ui_container_layout_init(obj, box_calc);

   items = alloca(count * sizeof(*items));
   rows = alloca(count * sizeof(*rows));
   memset(rows, 0, count * sizeof(*rows));

#ifdef DEBUG
   memset(items, 0, count * sizeof(*items));
#endif

   // scan all items, get their properties, calculate total weight & min size
   EINA_LIST_FOREACH(bd->children, li, child)
     {
        item = &items[i++];
        item->obj = child;
        hints = item->hints;

        _efl_ui_container_layout_item_init(item->obj, hints);

        if ((bd->homogeneous && !axis) || box_calc[0].fill)
          hints[0].weight = 1;
        else if (hints[0].weight < 0)
          hints[0].weight = 0;

        if ((bd->homogeneous && axis) || box_calc[1].fill)
          hints[1].weight = 1;
        else if (hints[1].weight < 0)
          hints[1].weight = 0;

        if (want[axis] < hints[axis].space)
          want[axis] = hints[axis].space;

        if (bd->homogeneous)
          continue;

        if (i == 1)
          {
             min_sum = hints[axis].space;
          }
        else if (box_calc[axis].size < (min_sum + hints[axis].space + box_calc[axis].pad))
          {
             min_sum = hints[axis].space;
             rc++;
          }
        else
          {
             min_sum += (hints[axis].space + box_calc[axis].pad);
          }

        row = &rows[rc];
        item->row = row;

        if (row->cross_weight < hints[c_axis].weight)
          row->cross_weight = hints[c_axis].weight;
        if (row->cross_space < hints[c_axis].space)
          row->cross_space = hints[c_axis].space;
        row->weight_sum += hints[axis].weight;
        row->min_sum += hints[axis].space;
        row->item_count++;
     }

   // initialize homogeneous properties
   if (bd->homogeneous)
     {
        min_sum = 0;
        for (i = 0; i < count; i++)
          {
             item = &items[i];
             hints = items[i].hints;

             if (i == 0)
               {
                  min_sum = want[axis];
               }
             else if (box_calc[axis].size < (min_sum + want[axis] + box_calc[axis].pad))
               {
                  min_sum = want[axis];
                  rc++;
               }
             else
               {
                  min_sum += (want[axis] + box_calc[axis].pad);
               }

             row = &rows[rc];
             item->row = row;

             if (row->cross_weight < hints[c_axis].weight)
               row->cross_weight = hints[c_axis].weight;
             if (row->cross_space < hints[c_axis].space)
               row->cross_space = hints[c_axis].space;
             row->item_count++;
          }
     }

   // calculate item space of each row
   for (id = 0, i = 0; id <= rc; id++)
     {
        int box_size;

        row = &rows[id];
        row->cur_pos = box_calc[axis].pos;

        box_size = box_calc[axis].size -
                   (box_calc[axis].pad * (row->item_count - 1));
        row->hgsize = box_size / row->item_count;

        cross_min_sum += row->cross_space;
        cross_weight_sum += row->cross_weight;

        if (bd->homogeneous)
          continue;

        if (row->weight_sum > 0)
          {
             int calc_size;
             double orig_weight = row->weight_sum;

             calc_size = box_size;
             inlist = NULL;

             item_last += row->item_count;
             for (; i < item_last; i++)
               {
                  double denom;
                  hint = &items[i].hints[axis];

                  denom = (hint->weight * box_size) - (orig_weight * hint->space);
                  if (denom > 0)
                    {
                       items[i].weight_factor = (hint->weight * box_size) / denom;
                       inlist = eina_inlist_sorted_insert(inlist, EINA_INLIST_GET(&items[i]),
                                                          _item_weight_sort_cb);

                    }
                  else
                    {
                       calc_size -= hint->space;
                       row->weight_sum -= hint->weight;
                    }
               }

             EINA_INLIST_FOREACH(inlist, item)
               {
                  double weight_len;
                  hint = &item->hints[axis];

                  weight_len = (calc_size * hint->weight) / row->weight_sum;
                  if (hint->space < weight_len)
                    {
                       hint->space = weight_len;
                    }
                  else
                    {
                       row->weight_sum -= hint->weight;
                       calc_size -= hint->space;
                    }
               }
          }
        else if (EINA_DBL_EQ(row->weight_sum, 0))
          {
             row->cur_pos += (box_size - row->min_sum) * box_calc[axis].align;
          }
     }

   // calculate row space
   box_calc[c_axis].size -= (box_calc[c_axis].pad * rc);
   cur_pos = box_calc[c_axis].pos;
   if ((box_calc[c_axis].size > cross_min_sum))
     {
        if (cross_weight_sum > 0)
          {
             int orig_size, calc_size;
             double orig_weight = cross_weight_sum;

             calc_size = orig_size = box_calc[c_axis].size;
             inlist = NULL;

             for (i = 0; i <= rc; i++)
               {
                  double denom;
                  row = &rows[i];

                  denom = (row->cross_weight * orig_size) -
                          (orig_weight * row->cross_space);
                  if (denom > 0)
                    {
                       row->weight_factor = (row->cross_weight * orig_size) / denom;
                       inlist = eina_inlist_sorted_insert(inlist, EINA_INLIST_GET(row),
                                                          _row_weight_sort_cb);

                    }
                  else
                    {
                       calc_size -= row->cross_space;
                       cross_weight_sum -= row->cross_weight;
                    }
               }

             EINA_INLIST_FOREACH(inlist, row)
               {
                  double weight_len;

                  weight_len = (calc_size * row->cross_weight) / cross_weight_sum;
                  if (row->cross_space < weight_len)
                    {
                       row->cross_space = weight_len;
                    }
                  else
                    {
                       cross_weight_sum -= row->cross_weight;
                       calc_size -= row->cross_space;
                    }
               }
          }
        else if (EINA_DBL_EQ(cross_weight_sum, 0))
          {
             cur_pos += (box_calc[c_axis].size - cross_min_sum) * box_calc[c_axis].align;
          }
     }

   // calculate item geometry
   int item_size[2], item_pos[2], sw, sh;

   row = NULL;
   for (i = 0; i < count; i++)
     {
        item = &items[i];
        hints = items[i].hints;

        if (row && (row != item->row))
          cur_pos += row->cross_space + box_calc[c_axis].pad;

        row = item->row;

        if (bd->homogeneous)
          hints[axis].space = row->hgsize;
        hints[c_axis].space = row->cross_space;
        sw = hints[0].space - (hints[0].margin[0] + hints[0].margin[1]);
        sh = hints[1].space - (hints[1].margin[0] + hints[1].margin[1]);

        item_size[0] = ((hints[0].weight > 0) && hints[0].fill) ? sw : 0;
        item_size[1] = ((hints[1].weight > 0) && hints[1].fill) ? sh : 0;

        _efl_ui_container_layout_min_max_calc(hints, &item_size[0], &item_size[1],
                                (hints[0].aspect > 0) && (hints[1].aspect > 0));

        item_pos[axis] = row->cur_pos + 0.5;
        item_pos[c_axis] = cur_pos + 0.5;

        item_pos[0] += (hints[0].margin[0] +
                        ((sw - item_size[0]) * hints[0].align));
        item_pos[1] += (hints[1].margin[0] +
                        ((sh - item_size[1]) * hints[1].align));

        row->cur_pos += hints[axis].space + box_calc[axis].pad;

        efl_gfx_entity_geometry_set(items[i].obj,
                                    EINA_RECT(item_pos[0], item_pos[1],
                                              item_size[0], item_size[1]));
     }

   want[axis] += (box_calc[axis].margin[0] + box_calc[axis].margin[1]);
   want[c_axis] = (box_calc[c_axis].margin[0] + box_calc[c_axis].margin[1]) +
                  (box_calc[c_axis].pad * rc) + cross_min_sum;

   efl_gfx_hint_size_restricted_min_set(obj, EINA_SIZE2D(want[0], want[1]));

   efl_event_callback_call(obj, EFL_PACK_EVENT_LAYOUT_UPDATED, NULL);
}

#include "efl_ui_box_flow.eo.c"