1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GtkHWrapBox: Horizontal wrapping box widget
5  * Copyright (C) 1999 Tim Janik
6  *
7  * This library is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 3 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library.  If not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include "config.h"
23 
24 #undef GSEAL_ENABLE
25 #undef GTK_DISABLE_DEPRECATED
26 
27 #include "gtkhwrapbox.h"
28 
29 #include "libgimpmath/gimpmath.h"
30 
31 
32 /* --- prototypes --- */
33 static void    gtk_hwrap_box_class_init    (GtkHWrapBoxClass   *klass);
34 static void    gtk_hwrap_box_init          (GtkHWrapBox        *hwbox);
35 static void    gtk_hwrap_box_size_request  (GtkWidget          *widget,
36                                             GtkRequisition     *requisition);
37 static void    gtk_hwrap_box_size_allocate (GtkWidget          *widget,
38                                             GtkAllocation      *allocation);
39 static GSList* reverse_list_row_children   (GtkWrapBox         *wbox,
40                                             GtkWrapBoxChild   **child_p,
41                                             GtkAllocation      *area,
42                                             guint              *max_height,
43                                             gboolean           *can_vexpand);
44 
45 
46 /* --- variables --- */
47 static gpointer parent_class = NULL;
48 
49 
50 /* --- functions --- */
51 GType
gtk_hwrap_box_get_type(void)52 gtk_hwrap_box_get_type (void)
53 {
54   static GType hwrap_box_type = 0;
55 
56   if (! hwrap_box_type)
57     {
58       const GTypeInfo hwrap_box_info =
59       {
60         sizeof (GtkHWrapBoxClass),
61         NULL,                /* base_init */
62         NULL,                /* base_finalize */
63         (GClassInitFunc) gtk_hwrap_box_class_init,
64         NULL,                /* class_finalize */
65         NULL,                /* class_data */
66         sizeof (GtkHWrapBox),
67         0,                /* n_preallocs */
68         (GInstanceInitFunc) gtk_hwrap_box_init,
69       };
70 
71       hwrap_box_type = g_type_register_static (GTK_TYPE_WRAP_BOX, "GtkHWrapBox",
72                                                &hwrap_box_info, 0);
73     }
74 
75   return hwrap_box_type;
76 }
77 
78 static void
gtk_hwrap_box_class_init(GtkHWrapBoxClass * class)79 gtk_hwrap_box_class_init (GtkHWrapBoxClass *class)
80 {
81   GtkWidgetClass *widget_class;
82   GtkWrapBoxClass *wrap_box_class;
83 
84   widget_class = GTK_WIDGET_CLASS (class);
85   wrap_box_class = GTK_WRAP_BOX_CLASS (class);
86 
87   parent_class = g_type_class_peek_parent (class);
88 
89   widget_class->size_request = gtk_hwrap_box_size_request;
90   widget_class->size_allocate = gtk_hwrap_box_size_allocate;
91 
92   wrap_box_class->rlist_line_children = reverse_list_row_children;
93 }
94 
95 static void
gtk_hwrap_box_init(GtkHWrapBox * hwbox)96 gtk_hwrap_box_init (GtkHWrapBox *hwbox)
97 {
98   hwbox->max_child_width = 0;
99   hwbox->max_child_height = 0;
100 }
101 
102 GtkWidget*
gtk_hwrap_box_new(gboolean homogeneous)103 gtk_hwrap_box_new (gboolean homogeneous)
104 {
105   return g_object_new (GTK_TYPE_HWRAP_BOX, "homogeneous", homogeneous, NULL);
106 }
107 
108 static inline void
get_child_requisition(GtkWrapBox * wbox,GtkWidget * child,GtkRequisition * child_requisition)109 get_child_requisition (GtkWrapBox     *wbox,
110                        GtkWidget      *child,
111                        GtkRequisition *child_requisition)
112 {
113   if (wbox->homogeneous)
114     {
115       GtkHWrapBox *hwbox = GTK_HWRAP_BOX (wbox);
116 
117       child_requisition->width = hwbox->max_child_width;
118       child_requisition->height = hwbox->max_child_height;
119     }
120   else
121     gtk_widget_get_child_requisition (child, child_requisition);
122 }
123 
124 static gfloat
get_layout_size(GtkHWrapBox * this,guint max_width,guint * width_inc)125 get_layout_size (GtkHWrapBox *this,
126                  guint        max_width,
127                  guint       *width_inc)
128 {
129   GtkWrapBox *wbox = GTK_WRAP_BOX (this);
130   GtkWrapBoxChild *child;
131   guint n_rows, left_over = 0, total_height = 0;
132   gboolean last_row_filled = TRUE;
133 
134   *width_inc = this->max_child_width + 1;
135 
136   n_rows = 0;
137   for (child = wbox->children; child; child = child->next)
138     {
139       GtkWrapBoxChild *row_child;
140       GtkRequisition child_requisition;
141       guint row_width, row_height, n = 1;
142 
143       if (!GTK_WIDGET_VISIBLE (child->widget))
144         continue;
145 
146       get_child_requisition (wbox, child->widget, &child_requisition);
147       if (!last_row_filled)
148         *width_inc = MIN (*width_inc, child_requisition.width - left_over);
149       row_width = child_requisition.width;
150       row_height = child_requisition.height;
151       for (row_child = child->next; row_child && n < wbox->child_limit; row_child = row_child->next)
152         {
153           if (GTK_WIDGET_VISIBLE (row_child->widget))
154             {
155               get_child_requisition (wbox, row_child->widget, &child_requisition);
156               if (row_width + wbox->hspacing + child_requisition.width > max_width)
157                 break;
158               row_width += wbox->hspacing + child_requisition.width;
159               row_height = MAX (row_height, child_requisition.height);
160               n++;
161             }
162           child = row_child;
163         }
164       last_row_filled = n >= wbox->child_limit;
165       left_over = last_row_filled ? 0 : max_width - (row_width + wbox->hspacing);
166       total_height += (n_rows ? wbox->vspacing : 0) + row_height;
167       n_rows++;
168     }
169 
170   if (*width_inc > this->max_child_width)
171     *width_inc = 0;
172 
173   return MAX (total_height, 1);
174 }
175 
176 static void
gtk_hwrap_box_size_request(GtkWidget * widget,GtkRequisition * requisition)177 gtk_hwrap_box_size_request (GtkWidget      *widget,
178                             GtkRequisition *requisition)
179 {
180   GtkHWrapBox *this = GTK_HWRAP_BOX (widget);
181   GtkWrapBox *wbox = GTK_WRAP_BOX (widget);
182   GtkWrapBoxChild *child;
183   gfloat ratio_dist, layout_width = 0;
184   guint row_inc = 0;
185 
186   g_return_if_fail (requisition != NULL);
187 
188   requisition->width = 0;
189   requisition->height = 0;
190   this->max_child_width = 0;
191   this->max_child_height = 0;
192 
193   /* size_request all children */
194   for (child = wbox->children; child; child = child->next)
195     if (GTK_WIDGET_VISIBLE (child->widget))
196       {
197         GtkRequisition child_requisition;
198 
199         gtk_widget_size_request (child->widget, &child_requisition);
200 
201         this->max_child_width = MAX (this->max_child_width, child_requisition.width);
202         this->max_child_height = MAX (this->max_child_height, child_requisition.height);
203       }
204 
205   /* figure all possible layouts */
206   ratio_dist = 32768;
207   layout_width = this->max_child_width;
208   do
209     {
210       gfloat layout_height;
211       gfloat ratio, dist;
212 
213       layout_width += row_inc;
214       layout_height = get_layout_size (this, layout_width, &row_inc);
215       ratio = layout_width / layout_height;                /*<h2v-skip>*/
216       dist = MAX (ratio, wbox->aspect_ratio) - MIN (ratio, wbox->aspect_ratio);
217       if (dist < ratio_dist)
218         {
219           ratio_dist = dist;
220           requisition->width = layout_width;
221           requisition->height = layout_height;
222         }
223 
224       /* g_print ("ratio for width %d height %d = %f\n",
225          (gint) layout_width,
226          (gint) layout_height,
227          ratio);
228       */
229     }
230   while (row_inc);
231 
232   requisition->width += GTK_CONTAINER (wbox)->border_width * 2; /*<h2v-skip>*/
233   requisition->height += GTK_CONTAINER (wbox)->border_width * 2; /*<h2v-skip>*/
234   /* g_print ("chosen: width %d, height %d\n",
235      requisition->width,
236      requisition->height);
237   */
238 }
239 
240 static GSList*
reverse_list_row_children(GtkWrapBox * wbox,GtkWrapBoxChild ** child_p,GtkAllocation * area,guint * max_child_size,gboolean * expand_line)241 reverse_list_row_children (GtkWrapBox       *wbox,
242                            GtkWrapBoxChild **child_p,
243                            GtkAllocation    *area,
244                            guint            *max_child_size,
245                            gboolean         *expand_line)
246 {
247   GSList *slist = NULL;
248   guint width = 0, row_width = area->width;
249   GtkWrapBoxChild *child = *child_p;
250 
251   *max_child_size = 0;
252   *expand_line = FALSE;
253 
254   while (child && !GTK_WIDGET_VISIBLE (child->widget))
255     {
256       *child_p = child->next;
257       child = *child_p;
258     }
259 
260   if (child)
261     {
262       GtkRequisition child_requisition;
263       guint n = 1;
264 
265       get_child_requisition (wbox, child->widget, &child_requisition);
266       width += child_requisition.width;
267       *max_child_size = MAX (*max_child_size, child_requisition.height);
268       *expand_line |= child->vexpand;
269       slist = g_slist_prepend (slist, child);
270       *child_p = child->next;
271       child = *child_p;
272 
273       while (child && n < wbox->child_limit)
274         {
275           if (GTK_WIDGET_VISIBLE (child->widget))
276             {
277               get_child_requisition (wbox, child->widget, &child_requisition);
278               if (width + wbox->hspacing + child_requisition.width > row_width ||
279                   child->wrapped)
280                 break;
281               width += wbox->hspacing + child_requisition.width;
282               *max_child_size = MAX (*max_child_size, child_requisition.height);
283               *expand_line |= child->vexpand;
284               slist = g_slist_prepend (slist, child);
285               n++;
286             }
287           *child_p = child->next;
288           child = *child_p;
289         }
290     }
291 
292   return slist;
293 }
294 
295 static void
layout_row(GtkWrapBox * wbox,GtkAllocation * area,GSList * children,guint children_per_line,gboolean vexpand)296 layout_row (GtkWrapBox    *wbox,
297             GtkAllocation *area,
298             GSList        *children,
299             guint          children_per_line,
300             gboolean       vexpand)
301 {
302   GSList *slist;
303   guint n_children = 0, n_expand_children = 0, have_expand_children = 0;
304   gint total_width = 0;
305   gfloat x, width, extra;
306   GtkAllocation child_allocation;
307 
308   for (slist = children; slist; slist = slist->next)
309     {
310       GtkWrapBoxChild *child = slist->data;
311       GtkRequisition child_requisition;
312 
313       n_children++;
314       if (child->hexpand)
315         n_expand_children++;
316 
317       get_child_requisition (wbox, child->widget, &child_requisition);
318       total_width += child_requisition.width;
319     }
320 
321   width = MAX (1, area->width - (n_children - 1) * wbox->hspacing);
322   if (width > total_width)
323     extra = width - total_width;
324   else
325     extra = 0;
326   have_expand_children = n_expand_children && extra;
327 
328   x = area->x;
329   if (wbox->homogeneous)
330     {
331       width = MAX (1, area->width - (children_per_line - 1) * wbox->hspacing);
332       width /= ((gdouble) children_per_line);
333       extra = 0;
334     }
335   else if (have_expand_children && wbox->justify != GTK_JUSTIFY_FILL)
336     {
337       width = extra;
338       extra /= ((gdouble) n_expand_children);
339     }
340   else
341     {
342       if (wbox->justify == GTK_JUSTIFY_FILL)
343         {
344           width = extra;
345           have_expand_children = TRUE;
346           n_expand_children = n_children;
347           extra /= ((gdouble) n_expand_children);
348         }
349       else if (wbox->justify == GTK_JUSTIFY_CENTER)
350         {
351           x += extra / 2;
352           width = 0;
353           extra = 0;
354         }
355       else if (wbox->justify == GTK_JUSTIFY_LEFT)
356         {
357           width = 0;
358           extra = 0;
359         }
360       else if (wbox->justify == GTK_JUSTIFY_RIGHT)
361         {
362           x += extra;
363           width = 0;
364           extra = 0;
365         }
366     }
367 
368   n_children = 0;
369   for (slist = children; slist; slist = slist->next)
370     {
371       GtkWrapBoxChild *child = slist->data;
372 
373       child_allocation.x = x;
374       child_allocation.y = area->y;
375       if (wbox->homogeneous)
376         {
377           child_allocation.height = area->height;
378           child_allocation.width = width;
379           x += child_allocation.width + wbox->hspacing;
380         }
381       else
382         {
383           GtkRequisition child_requisition;
384 
385           get_child_requisition (wbox, child->widget, &child_requisition);
386 
387           if (child_requisition.height >= area->height)
388             child_allocation.height = area->height;
389           else
390             {
391               child_allocation.height = child_requisition.height;
392               if (wbox->line_justify == GTK_JUSTIFY_FILL || child->vfill)
393                 child_allocation.height = area->height;
394               else if (child->vexpand || wbox->line_justify == GTK_JUSTIFY_CENTER)
395                 child_allocation.y += (area->height - child_requisition.height) / 2;
396               else if (wbox->line_justify == GTK_JUSTIFY_BOTTOM)
397                 child_allocation.y += area->height - child_requisition.height;
398             }
399 
400           if (have_expand_children)
401             {
402               child_allocation.width = child_requisition.width;
403               if (child->hexpand || wbox->justify == GTK_JUSTIFY_FILL)
404                 {
405                   guint space;
406 
407                   n_expand_children--;
408                   space = extra * n_expand_children;
409                   space = width - space;
410                   width -= space;
411                   if (child->hfill)
412                     child_allocation.width += space;
413                   else
414                     {
415                       child_allocation.x += space / 2;
416                       x += space;
417                     }
418                 }
419             }
420           else
421             {
422               /* g_print ("child_allocation.x %d += %d * %f ",
423                        child_allocation.x, n_children, extra); */
424               child_allocation.x += n_children * extra;
425               /* g_print ("= %d\n",
426                        child_allocation.x); */
427               child_allocation.width = MIN (child_requisition.width,
428                                             area->width - child_allocation.x + area->x);
429             }
430         }
431 
432       x += child_allocation.width + wbox->hspacing;
433       gtk_widget_size_allocate (child->widget, &child_allocation);
434       n_children++;
435     }
436 }
437 
438 typedef struct _Line Line;
439 struct _Line
440 {
441   GSList  *children;
442   guint16  min_size;
443   guint    expand : 1;
444   Line    *next;
445 };
446 
447 static void
layout_rows(GtkWrapBox * wbox,GtkAllocation * area)448 layout_rows (GtkWrapBox    *wbox,
449              GtkAllocation *area)
450 {
451   GtkWrapBoxChild *next_child;
452   guint min_height;
453   gboolean vexpand;
454   GSList *slist;
455   Line *line_list = NULL;
456   guint total_height = 0, n_expand_lines = 0, n_lines = 0;
457   gfloat shrink_height;
458   guint children_per_line;
459 
460   next_child = wbox->children;
461   slist = GTK_WRAP_BOX_GET_CLASS (wbox)->rlist_line_children (wbox,
462                                                               &next_child,
463                                                               area,
464                                                               &min_height,
465                                                               &vexpand);
466   slist = g_slist_reverse (slist);
467 
468   children_per_line = g_slist_length (slist);
469   while (slist)
470     {
471       Line *line = g_slice_new (Line);
472 
473       line->children = slist;
474       line->min_size = min_height;
475       total_height += min_height;
476       line->expand = vexpand;
477       if (vexpand)
478         n_expand_lines++;
479       line->next = line_list;
480       line_list = line;
481       n_lines++;
482 
483       slist = GTK_WRAP_BOX_GET_CLASS (wbox)->rlist_line_children (wbox,
484                                                                   &next_child,
485                                                                   area,
486                                                                   &min_height,
487                                                                   &vexpand);
488       slist = g_slist_reverse (slist);
489     }
490 
491   if (total_height > area->height)
492     shrink_height = total_height - area->height;
493   else
494     shrink_height = 0;
495 
496   if (1) /* reverse lines and shrink */
497     {
498       Line *prev = NULL, *last = NULL;
499       gfloat n_shrink_lines = n_lines;
500 
501       while (line_list)
502         {
503           Line *tmp = line_list->next;
504 
505           if (shrink_height)
506             {
507               Line *line = line_list;
508               guint shrink_fract = shrink_height / n_shrink_lines + 0.5;
509 
510               if (line->min_size > shrink_fract)
511                 {
512                   shrink_height -= shrink_fract;
513                   line->min_size -= shrink_fract;
514                 }
515               else
516                 {
517                   shrink_height -= line->min_size - 1;
518                   line->min_size = 1;
519                 }
520             }
521           n_shrink_lines--;
522 
523           last = line_list;
524           line_list->next = prev;
525           prev = line_list;
526           line_list = tmp;
527         }
528       line_list = last;
529     }
530 
531   if (n_lines)
532     {
533       Line *line;
534       gfloat y, height, extra = 0;
535 
536       height = area->height;
537       height = MAX (n_lines, height - (n_lines - 1) * wbox->vspacing);
538 
539       if (wbox->homogeneous)
540         height /= ((gdouble) n_lines);
541       else if (n_expand_lines)
542         {
543           height = MAX (0, height - total_height);
544           extra = height / ((gdouble) n_expand_lines);
545         }
546       else
547         height = 0;
548 
549       y = area->y;
550       line = line_list;
551       while (line)
552         {
553           GtkAllocation row_allocation;
554           Line *next_line = line->next;
555 
556           row_allocation.x = area->x;
557           row_allocation.width = area->width;
558           if (wbox->homogeneous)
559             row_allocation.height = height;
560           else
561             {
562               row_allocation.height = line->min_size;
563 
564               if (line->expand)
565                 row_allocation.height += extra;
566             }
567 
568           row_allocation.y = y;
569 
570           y += row_allocation.height + wbox->vspacing;
571           layout_row (wbox,
572                       &row_allocation,
573                       line->children,
574                       children_per_line,
575                       line->expand);
576 
577           g_slist_free (line->children);
578           line = next_line;
579         }
580 
581       g_slice_free_chain (Line, line_list, next);
582     }
583 }
584 
585 static void
gtk_hwrap_box_size_allocate(GtkWidget * widget,GtkAllocation * allocation)586 gtk_hwrap_box_size_allocate (GtkWidget     *widget,
587                              GtkAllocation *allocation)
588 {
589   GtkWrapBox *wbox = GTK_WRAP_BOX (widget);
590   GtkAllocation area;
591   gint border = GTK_CONTAINER (wbox)->border_width; /*<h2v-skip>*/
592 
593   widget->allocation = *allocation;
594   area.x = allocation->x + border;
595   area.y = allocation->y + border;
596   area.width = MAX (1, (gint) allocation->width - border * 2);
597   area.height = MAX (1, (gint) allocation->height - border * 2);
598 
599   /*<h2v-off>*/
600   /* g_print ("got: width %d, height %d\n",
601      allocation->width,
602      allocation->height);
603   */
604   /*<h2v-on>*/
605 
606   layout_rows (wbox, &area);
607 }
608