1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* caja-keep-last-vertical-box.c: Subclass of GtkBox that clips off
4 items that don't fit, except the last one.
5
6 Copyright (C) 2000 Eazel, Inc.
7
8 The Mate Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 The Mate Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public
19 License along with the Mate Library; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23 Author: John Sullivan <sullivan@eazel.com>,
24 */
25
26 #include <config.h>
27 #include "caja-keep-last-vertical-box.h"
28
29 static void caja_keep_last_vertical_box_size_allocate (GtkWidget *widget,
30 GtkAllocation *allocation);
31
G_DEFINE_TYPE(CajaKeepLastVerticalBox,caja_keep_last_vertical_box,GTK_TYPE_BOX)32 G_DEFINE_TYPE (CajaKeepLastVerticalBox, caja_keep_last_vertical_box, GTK_TYPE_BOX)
33
34 /* Standard class initialization function */
35 static void
36 caja_keep_last_vertical_box_class_init (CajaKeepLastVerticalBoxClass *klass)
37 {
38 GtkWidgetClass *widget_class;
39
40 widget_class = (GtkWidgetClass *) klass;
41
42 widget_class->size_allocate = caja_keep_last_vertical_box_size_allocate;
43 }
44
45 /* Standard object initialization function */
46 static void
caja_keep_last_vertical_box_init(CajaKeepLastVerticalBox * box)47 caja_keep_last_vertical_box_init (CajaKeepLastVerticalBox *box)
48 {
49 gtk_orientable_set_orientation (GTK_ORIENTABLE (box), GTK_ORIENTATION_VERTICAL);
50 }
51
52
53 /* caja_keep_last_vertical_box_new:
54 *
55 * Create a new vertical box that clips off items from the end that don't
56 * fit, except the last item, which is always kept. When packing this widget
57 * into another vbox, use TRUE for expand and TRUE for fill or this class's
58 * special clipping magic won't work because this widget's allocation might
59 * be larger than the available space.
60 *
61 * @spacing: Vertical space between items.
62 *
63 * Return value: A new CajaKeepLastVerticalBox
64 */
65 GtkWidget *
caja_keep_last_vertical_box_new(gint spacing)66 caja_keep_last_vertical_box_new (gint spacing)
67 {
68 CajaKeepLastVerticalBox *box;
69
70 box = CAJA_KEEP_LAST_VERTICAL_BOX (gtk_widget_new (caja_keep_last_vertical_box_get_type (), NULL));
71
72 gtk_box_set_spacing (GTK_BOX (box), spacing);
73
74 /* If homogeneous is TRUE and there are too many items to fit
75 * naturally, they will be squashed together to fit in the space.
76 * We want the ones that don't fit to be not shown at all, so
77 * we set homogeneous to FALSE.
78 */
79 gtk_box_set_homogeneous (GTK_BOX (box), FALSE);
80
81 return GTK_WIDGET (box);
82 }
83
84 static void
caja_keep_last_vertical_box_size_allocate(GtkWidget * widget,GtkAllocation * allocation)85 caja_keep_last_vertical_box_size_allocate (GtkWidget *widget,
86 GtkAllocation *allocation)
87 {
88 GList *children, *l;
89 GtkAllocation last_child_allocation, child_allocation, tiny_allocation;
90
91 g_return_if_fail (CAJA_IS_KEEP_LAST_VERTICAL_BOX (widget));
92 g_return_if_fail (allocation != NULL);
93
94 GTK_WIDGET_CLASS (caja_keep_last_vertical_box_parent_class)->size_allocate (widget, allocation);
95
96 children = gtk_container_get_children (GTK_CONTAINER (widget));
97 l = g_list_last (children);
98
99 if (l != NULL)
100 {
101 GtkWidget *last_child;
102
103 last_child = l->data;
104 l = l->prev;
105
106 gtk_widget_get_allocation (last_child, &last_child_allocation);
107
108 /* If last child doesn't fit vertically, prune items from the end of the
109 * list one at a time until it does.
110 */
111 if (last_child_allocation.y + last_child_allocation.height >
112 allocation->y + allocation->height)
113 {
114 GtkWidget *child = NULL;
115
116 while (l != NULL)
117 {
118 child = l->data;
119 l = l->prev;
120
121 gtk_widget_get_allocation (child, &child_allocation);
122
123 /* Reallocate this child's position so that it does not appear.
124 * Setting the width & height to 0 is not enough, as
125 * one pixel is still drawn. Must also move it outside
126 * visible range. For the cases I've seen, -1, -1 works fine.
127 * This might not work in all future cases. Alternatively, the
128 * items that don't fit could be hidden, but that would interfere
129 * with having other hidden children.
130 *
131 * Note that these children are having their size allocated twice,
132 * once by gtk_vbox_size_allocate and then again here. I don't
133 * know of any problems with this, but holler if you do.
134 */
135 tiny_allocation.x = tiny_allocation.y = -1;
136 tiny_allocation.height = tiny_allocation.width = 0;
137 gtk_widget_size_allocate (child, &tiny_allocation);
138
139 /* We're done if the special last item fits now. */
140 if (child_allocation.y + last_child_allocation.height <=
141 allocation->y + allocation->height)
142 {
143 last_child_allocation.y = child_allocation.y;
144 gtk_widget_size_allocate (last_child, &last_child_allocation);
145 break;
146 }
147
148 /* If the special last item still doesn't fit, but we've
149 * run out of earlier items, then the special last item is
150 * just too darn tall. Let's squash it down to fit in the box's
151 * allocation.
152 */
153 if (l == NULL)
154 {
155 last_child_allocation.y = allocation->y;
156 last_child_allocation.height = allocation->height;
157 gtk_widget_size_allocate (last_child, &last_child_allocation);
158 }
159 }
160 }
161 }
162 g_list_free (children);
163 }
164