summaryrefslogtreecommitdiff
path: root/src/lib/elementary/els_box.c
blob: 965cd9862911477e8011e8026f7bd3322d1e2541 (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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>

#include "elm_priv.h"
#include "els_box.h"

/* calculate an object's aspected size */
static Eina_Bool
_box_object_aspect_calc(int *ow, int *oh, int minw, int minh, int maxw, int maxh,
                        int fw /* fill width */, int fh /* fill height */,
                        int ww /* "maximum" width */, int hh /* "maximum" height */,
                        Evas_Aspect_Control aspect, double ratio)
{
   *ow = minw;
   *oh = minh;

   switch (aspect)
     {
      case EVAS_ASPECT_CONTROL_HORIZONTAL:
        /* set height using aspect+width */
        if (fw) *ow = ww;
        if ((maxw >= 0) && (maxw < *ow)) *ow = maxw;
        *oh = (1 / ratio) * *ow;
        /* apply min/max */
        if ((maxh >= 0) && (maxh < *oh)) *oh = maxh;
        else if ((minh >= 0) && (minh > *oh)) *oh = minh;
        else return EINA_TRUE;
        return EINA_FALSE;
      case EVAS_ASPECT_CONTROL_VERTICAL:
        /* set width using aspect+height */
        if (fh) *oh = hh;
        if ((maxh >= 0) && (maxh < *oh)) *oh = maxh;
        *ow = ratio * *oh;
        /* apply min/max */
        if ((maxw >= 0) && (maxw < *ow)) *ow = maxw;
        else if ((minw >= 0) && (minw > *ow)) *ow = minw;
        else return EINA_TRUE;
        return EINA_FALSE;
      case EVAS_ASPECT_CONTROL_BOTH:
        /* try width then height */

        /* set width using aspect+height */
        if (fh) *oh = hh;
        if ((maxh >= 0) && (maxh < *oh)) *oh = maxh;
        *ow = ratio * *oh;
        /* apply min/max */
        if ((maxw >= 0) && (maxw < *ow)) *ow = maxw;
        else if ((minw >= 0) && (minw > *ow)) *ow = minw;
        else return EINA_TRUE;
        /* set height using aspect+width */
        if (fw) *ow = ww;
        if ((maxw >= 0) && (maxw < *ow)) *ow = maxw;
        *oh = (1 / ratio) * *ow;
        /* apply min/max */
        if ((maxh >= 0) && (maxh < *oh)) *oh = maxh;
        else if ((minh >= 0) && (minh > *oh)) *oh = minh;
        else return EINA_TRUE;
        /* fallthrough on BOTH failure */
      default: break;
     }
   /* on failure or ASPECT_NONE, use default size calc:
    * - apply fill
    * - apply max size constraints
    */
   if (fw) *ow = ww;
   if ((maxw >= 0) && (maxw < *ow)) *ow = maxw;
   if (fh) *oh = hh;
   if ((maxh >= 0) && (maxh < *oh)) *oh = maxh;
   return EINA_FALSE;
}

/* add box w/h padding to min/max totals */
static void
_smart_extents_padding_calc(Evas_Object_Box_Data *priv, int *minw, int *minh, int *maxw, int *maxh, Eina_Bool horizontal)
{
   int c;

   if ((*maxw >= 0) && (*minw > *maxw)) *maxw = *minw;
   if ((*maxh >= 0) && (*minh > *maxh)) *maxh = *minh;
   c = eina_list_count(priv->children) - 1;
   if (c > 0)
     {
        if (horizontal)
          {
             *minw += priv->pad.h * c;
             if (*maxw != -1) *maxw += priv->pad.h * c;
          }
        else
          {
             *minh += priv->pad.v * c;
             if (*maxh != -1) *maxh += priv->pad.v * c;
          }
     }
}

/* calculate extents for non-homogeneous layout;
 * called twice if aspected items exist
 */
static Eina_Bool
_smart_extents_non_homogeneous_calc(Evas_Object_Box_Data *priv, int w, int h, int *minw, int *minh, int *maxw, int *maxh, double expand, Eina_Bool horizontal, Eina_Bool do_aspect)
{
   const Eina_List *l;
   Evas_Object_Box_Option *opt;
   int mnw, mnh, mxw, mxh, cminw, cminh;
   Evas_Coord pad_l, pad_r, pad_t, pad_b;
   Evas_Coord *rw, *rh, *rxw, *rxh, *rminw, *rminh, *rmaxw, *rmaxh;
   Eina_Bool max = EINA_TRUE, asp = EINA_FALSE;

   cminw = *minw, cminh = *minh;
   *minw = *minh = 0;
   *maxw = *maxh = -1;
   /* use pointers to values to simplify horizontal vs vertical calculations into
    * a single algorithm for both orientations
    */
   if (!horizontal)
     {
        rw = &mnw;
        rh = &mnh;
        rxw = &mxw;
        rxh = &mxh;
        rminw = minw;
        rminh = minh;
        rmaxw = maxw;
        rmaxh = maxh;
     }
   else
     {
        rw = &mnh;
        rh = &mnw;
        rxw = &mxh;
        rxh = &mxw;
        rminw = minh;
        rminh = minw;
        rmaxw = maxh;
        rmaxh = maxw;
     }
   EINA_LIST_FOREACH(priv->children, l, opt)
     {
        Evas_Aspect_Control aspect = EVAS_ASPECT_CONTROL_NONE;
        int asx, asy, ow = 0, oh = 0, *rrw, *rrh;

        if (!horizontal)
          rrw = &ow, rrh = &oh;
        else
          rrw = &oh, rrh = &ow;

        evas_object_size_hint_padding_get(opt->obj, &pad_l, &pad_r, &pad_t, &pad_b);
        evas_object_size_hint_combined_min_get(opt->obj, &mnw, &mnh);
        mnw += pad_l + pad_r;
        mnh += pad_t + pad_b;
        if (*rminw < *rw) *rminw = *rw;
        *rminh += *rh;

        evas_object_size_hint_aspect_get(opt->obj, &aspect, &asx, &asy);
        if (aspect && ((!EINA_DBL_NONZERO(asx)) || (asx < 0.0) ||
          (!EINA_DBL_NONZERO(asy)) || (asy < 0.0)))
          {
             aspect = EVAS_ASPECT_CONTROL_NONE;
             ERR("Invalid aspect specified!");
          }
        /* return whether any aspected items exist */
        asp |= !!aspect;

        evas_object_size_hint_max_get(opt->obj, &mxw, &mxh);
        if (mxw >= 0) mxw += pad_l + pad_r;
        if (mxh >= 0) mxh += pad_t + pad_b;
        if (*rxh < 0)
          {
             *rmaxh = -1;
             max = EINA_FALSE;
          }
        if (max) *rmaxh += *rxh;

        if (do_aspect && aspect)
          {
             int ww, hh, fw = 0, fh = 0;
             double wx, wy, ax, ay;

             evas_object_size_hint_weight_get(opt->obj, &wx, &wy);

             if (horizontal)
               {
                  /* use min size to start */
                  ww = mnw;
                  if ((expand > 0) && (wx > 0.0))
                    {
                       /* add remaining container value after applying weight hint */
                       ow = ((w - cminw) * wx) / expand;
                       ww += ow;
                    }
                  hh = h;
               }
             else
               {
                  hh = mnh;
                  if ((expand > 0) && (wy > 0.0))
                    {
                       oh = ((h - cminh) * wy) / expand;
                       hh += oh;
                    }
                  ww = w;
               }
             evas_object_size_hint_align_get(opt->obj, &ax, &ay);
             if (ax < 0) fw = 1;
             if (ay < 0) fh = 1;

             /* if aspecting succeeds, use aspected size for min size */
             if (_box_object_aspect_calc(&ow, &oh, mnw, mnh, mxw, mxh,
                 fw, fh, ww, hh, aspect, asx / (double)asy))
               {
                  *rminh += (*rrh - *rh);
                  if (*rminw < *rrw) *rminw = *rrw;
               }
          }
        if (*rxw >= 0)
          {
             if (*rmaxw == -1) *rmaxw = *rxw;
             else if (*rmaxw > *rxw) *rmaxw = *rxw;
          }
     }
   return asp;
}

static void
_smart_extents_calculate(Evas_Object *box, Evas_Object_Box_Data *priv, int w, int h, double expand, Eina_Bool horizontal, Eina_Bool homogeneous)
{
   Evas_Coord minw, minh, mnw, mnh, maxw, maxh;
   Evas_Coord pad_l, pad_r, pad_t, pad_b;
   const Eina_List *l;
   Evas_Object_Box_Option *opt;
   int c;

   minw = 0;
   minh = 0;
   maxw = -1;
   maxh = -1;
   c = eina_list_count(priv->children);
   if (homogeneous || (c == 1))
     {
        Evas_Aspect_Control paspect = -1; //causes overflow
        int pasx = -1, pasy = -1;

        EINA_LIST_FOREACH(priv->children, l, opt)
          {
             Evas_Aspect_Control aspect = EVAS_ASPECT_CONTROL_NONE;
             int asx, asy, ow = 0, oh = 0, fw, fh, ww, hh;
             double ax, ay;

             evas_object_size_hint_align_get(opt->obj, &ax, &ay);
             if (ax < 0) fw = 1;
             if (ay < 0) fh = 1;

             evas_object_size_hint_padding_get(opt->obj, &pad_l, &pad_r, &pad_t, &pad_b);
             evas_object_size_hint_combined_min_get(opt->obj, &mnw, &mnh);
             mnw += pad_l + pad_r;
             mnh += pad_t + pad_b;
             if (minh < mnh) minh = mnh;
             if (minw < mnw) minw = mnw;

             evas_object_size_hint_aspect_get(opt->obj, &aspect, &asx, &asy);
             if (aspect && ((asx < 1) || (asy < 1)))
               {
                  aspect = EVAS_ASPECT_CONTROL_NONE;
                  ERR("Invalid aspect specified!");
               }
             if ((unsigned) paspect < 100) //value starts overflowed as UINT_MAX
               {
                  /* this condition can cause some items to not be the same size,
                   * resulting in a non-homogeneous homogeneous layout
                   */
                  if ((aspect != paspect) || (asx != pasx) || (asy != pasy))
                    ERR("Homogeneous box with differently-aspected items!");
               }

             evas_object_size_hint_max_get(opt->obj, &mnw, &mnh);
             if (mnh >= 0)
               {
                  mnh += pad_t + pad_b;
                  if (maxh == -1) maxh = mnh;
                  else if (maxh > mnh) maxh = mnh;
               }
             if (mnw >= 0)
               {
                  mnw += pad_l + pad_r;
                  if (maxw == -1) maxw = mnw;
                  else if (maxw > mnw) maxw = mnw;
               }
             if (aspect)
               {
                  if (horizontal)
                    {
                       ww = ((w - (c - 1) * priv->pad.h) / c);
                       hh = h;
                    }
                  else
                    {
                       hh = ((h - (c - 1) * priv->pad.v) / c);
                       ww = w;
                    }
                  if (_box_object_aspect_calc(&ow, &oh, mnw, mnh, maxw, maxh,
                    fw, fh, ww, hh, aspect, asx / (double)asy))
                    {
                       if ((oh > mnh) && (minh < oh)) minh = oh;
                       if ((ow > mnw) && (minw < ow)) minw = ow;
                    }
               }

             paspect = aspect;
             pasx = asx, pasy = asy;
          }
        if (horizontal)
          {
             minw *= c;
             if (maxw != -1)
                maxw *= c;
             else maxw = -1;
          }
        else
          {
             minh *= c;
             if (maxh != -1)
                maxh *= c;
             else maxh = -1;
          }
     }
   else
     {
        /* returns true if at least one item has aspect hint */
        if (_smart_extents_non_homogeneous_calc(priv, w, h, &minw, &minh, &maxw, &maxh, expand, horizontal, 0))
          {
             /* aspect can only be accurately calculated after the full (non-aspected) min size of the box has
              * been calculated due to the use of this min size during aspect calculations
              */
             int aminw = minw;
             int aminh = minh;
             _smart_extents_padding_calc(priv, &minw, &minh, &maxw, &maxh, horizontal);
             _smart_extents_non_homogeneous_calc(priv, w, h, &aminw, &aminh, &maxw, &maxh, expand, horizontal, 1);
             if (horizontal) minh = aminh;
             else minw = aminw;
          }
     }
   _smart_extents_padding_calc(priv, &minw, &minh, &maxw, &maxh, horizontal);
   evas_object_size_hint_min_set(box, minw, minh);
   evas_object_size_hint_max_set(box, maxw, maxh);
}

void
_els_box_layout(Evas_Object *o, Evas_Object_Box_Data *priv, Eina_Bool horizontal, Eina_Bool homogeneous, Eina_Bool rtl)
{
   Evas_Coord x, y, w, h, xx, yy;
   const Eina_List *l;
   Evas_Object *obj;
   Evas_Coord minw, minh;
   int count = 0;
   double expand = 0.0;
   double ax, ay;
   double wx, wy;
   double *rwy;
   Evas_Object_Box_Option *opt;

   evas_object_geometry_get(o, &x, &y, &w, &h);
   /* accummulate expand after switched x and y for horizontal mode */
   if (!horizontal)
     rwy = &wy;
   else
     rwy = &wx;
   EINA_LIST_FOREACH(priv->children, l, opt)
     {
        evas_object_size_hint_weight_get(opt->obj, &wx, &wy);
        if (*rwy > 0.0) expand += *rwy;
     }
   _smart_extents_calculate(o, priv, w, h, expand, horizontal, homogeneous);
   evas_object_geometry_get(o, &x, &y, &w, &h);

   evas_object_size_hint_combined_min_get(o, &minw, &minh);
   evas_object_box_align_get(o, &ax, &ay);
   /* if object size is less than min, apply align to trigger viewporting */
   if (w < minw)
     {
        x = x + ((w - minw) * (1.0 - ax));
        w = minw;
     }
   if (h < minh)
     {
        y = y + ((h - minh) * (1.0 - ay));
        h = minh;
     }
   count = eina_list_count(priv->children);

   if (!expand)
     {
        if (rtl) ax = 1.0 - ax;
        if (horizontal)
          {
             x += (double)(w - minw) * ax;
             w = minw;
          }
        else
          {
             y += (double)(h - minh) * ay;
             h = minh;
          }
     }
   xx = x;
   yy = y;
   EINA_LIST_FOREACH(priv->children, l, opt)
     {
        Evas_Coord mnw, mnh, mxw, mxh;
        Evas_Coord pad_l, pad_r, pad_t, pad_b;
        int fw, fh, xw, xh;//fillw, fillw, expandw, expandh
        Evas_Aspect_Control aspect = EVAS_ASPECT_CONTROL_NONE;
        int asx, asy;

        obj = opt->obj;
        evas_object_size_hint_align_get(obj, &ax, &ay);
        evas_object_size_hint_weight_get(obj, &wx, &wy);
        evas_object_size_hint_padding_get(obj, &pad_l, &pad_r, &pad_t, &pad_b);
        evas_object_size_hint_combined_min_get(obj, &mnw, &mnh);
        mnw += pad_l + pad_r;
        mnh += pad_t + pad_b;
        evas_object_size_hint_max_get(obj, &mxw, &mxh);
        if (mxw >= 0) mxw += pad_l + pad_r;
        if (mxh >= 0) mxh += pad_t + pad_b;
        evas_object_size_hint_aspect_get(obj, &aspect, &asx, &asy);
        if (aspect && ((asx < 1) || (asy < 1)))
          aspect = EVAS_ASPECT_CONTROL_NONE;
        fw = fh = 0;
        xw = xh = 0;
        /* align(-1) means fill to maximum apportioned size */
        if (ax == -1.0) {fw = 1; ax = 0.5;}
        if (ay == -1.0) {fh = 1; ay = 0.5;}
        if (rtl) ax = 1.0 - ax;
        if (wx > 0.0) xw = 1;
        if (wy > 0.0) xh = 1;
        if (horizontal)
          {
             Evas_Coord ww, hh, ow = 0, oh = 0;

             if (homogeneous)
               {
                  ww = ((w - (count - 1) * priv->pad.h) / (Evas_Coord)count);
               }
             else
               {
                  ww = mnw;
                  if ((expand > 0) && (xw))
                    {
                       ow = ((w - minw) * wx) / expand;
                       ww += ow;
                    }
               }
             hh = h;

             _box_object_aspect_calc(&ow, &oh, mnw, mnh, mxw, mxh, fw, fh, ww, hh, aspect, asx / (double)asy);
             /* non-homogeneous, aspected, expending items are calculated based on object size
              * during extents calc, so use this for positioning during layout as well
              */
             if (xw && aspect && (!homogeneous))
               ww = ow;
             evas_object_move(obj,
                              ((!rtl) ? (xx + pad_l) : (x + (w - (xx - x) - ww) + pad_r))
                              + (Evas_Coord)(((double)(ww - ow)) * ax),
                              yy + (Evas_Coord)(((double)(hh - oh)) * ay) + pad_t);
             ow -= pad_l + pad_r;
             oh -= pad_t + pad_b;
             evas_object_resize(obj, ow, oh);
             xx += ww;
             xx += priv->pad.h;
          }
        else
          {
             Evas_Coord ww, hh, ow = 0, oh = 0;

             if (homogeneous)
               {
                  hh = ((h - (count - 1) * priv->pad.v) / (Evas_Coord)count);
               }
             else
               {
                  hh = mnh;
                  if ((expand > 0) && (xh))
                    {
                       oh = ((h - minh) * wy) / expand;
                       hh += oh;
                    }
               }
             ww = w;

             _box_object_aspect_calc(&ow, &oh, mnw, mnh, mxw, mxh, fw, fh, ww, hh, aspect, asx / (double)asy);
             if (xh && aspect && (!homogeneous))
               hh = oh;
             evas_object_move(obj,
                              xx + (Evas_Coord)(((double)(ww - ow)) * ax) + pad_l,
                              yy + (Evas_Coord)(((double)(hh - oh)) * ay) + pad_t);
             ow -= pad_l + pad_r;
             oh -= pad_t + pad_b;
             evas_object_resize(obj, ow, oh);
             yy += hh;
             yy += priv->pad.v;
          }
     }
}