2008-04-30 A. Walton <awalton@gnome.org>
[nautilus.git] / libnautilus-private / nautilus-keep-last-vertical-box.c
blob8aabb560ac27d07728021b29dea6469a7b7f5f4f
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /* nautilus-keep-last-vertical-box.c: Subclass of GtkVBox that clips off
4 items that don't fit, except the last one.
6 Copyright (C) 2000 Eazel, Inc.
8 The Gnome 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.
13 The Gnome 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.
18 You should have received a copy of the GNU Library General Public
19 License along with the Gnome Library; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Author: John Sullivan <sullivan@eazel.com>,
26 #include <config.h>
27 #include "nautilus-keep-last-vertical-box.h"
29 #include <eel/eel-gtk-macros.h>
31 static void nautilus_keep_last_vertical_box_class_init (NautilusKeepLastVerticalBoxClass *class);
32 static void nautilus_keep_last_vertical_box_init (NautilusKeepLastVerticalBox *box);
33 static void nautilus_keep_last_vertical_box_size_allocate (GtkWidget *widget,
34 GtkAllocation *allocation);
36 EEL_CLASS_BOILERPLATE (NautilusKeepLastVerticalBox, nautilus_keep_last_vertical_box, GTK_TYPE_VBOX)
38 /* Standard class initialization function */
39 static void
40 nautilus_keep_last_vertical_box_class_init (NautilusKeepLastVerticalBoxClass *klass)
42 GtkWidgetClass *widget_class;
44 widget_class = (GtkWidgetClass *) klass;
46 widget_class->size_allocate = nautilus_keep_last_vertical_box_size_allocate;
49 /* Standard object initialization function */
50 static void
51 nautilus_keep_last_vertical_box_init (NautilusKeepLastVerticalBox *box)
56 /* nautilus_keep_last_vertical_box_new:
58 * Create a new vertical box that clips off items from the end that don't
59 * fit, except the last item, which is always kept. When packing this widget
60 * into another vbox, use TRUE for expand and TRUE for fill or this class's
61 * special clipping magic won't work because this widget's allocation might
62 * be larger than the available space.
64 * @spacing: Vertical space between items.
66 * Return value: A new NautilusKeepLastVerticalBox
68 GtkWidget *
69 nautilus_keep_last_vertical_box_new (gint spacing)
71 NautilusKeepLastVerticalBox *box;
73 box = NAUTILUS_KEEP_LAST_VERTICAL_BOX (gtk_widget_new (nautilus_keep_last_vertical_box_get_type (), NULL));
75 GTK_BOX (box)->spacing = spacing;
77 /* If homogeneous is TRUE and there are too many items to fit
78 * naturally, they will be squashed together to fit in the space.
79 * We want the ones that don't fit to be not shown at all, so
80 * we set homogeneous to FALSE.
82 GTK_BOX (box)->homogeneous = FALSE;
84 return GTK_WIDGET (box);
87 static void
88 nautilus_keep_last_vertical_box_size_allocate (GtkWidget *widget,
89 GtkAllocation *allocation)
91 GtkBox *box;
92 GtkBoxChild *last_child, *child;
93 GList *children;
94 GtkAllocation last_child_allocation, child_allocation, tiny_allocation;
96 g_return_if_fail (NAUTILUS_IS_KEEP_LAST_VERTICAL_BOX (widget));
97 g_return_if_fail (allocation != NULL);
99 EEL_CALL_PARENT (GTK_WIDGET_CLASS, size_allocate, (widget, allocation));
101 box = GTK_BOX (widget);
102 children = g_list_last (box->children);
104 if (children != NULL) {
105 last_child = children->data;
106 children = children->prev;
108 last_child_allocation = last_child->widget->allocation;
110 /* If last child doesn't fit vertically, prune items from the end of the
111 * list one at a time until it does.
113 if (last_child_allocation.y + last_child_allocation.height >
114 allocation->y + allocation->height) {
116 while (children != NULL) {
117 child = children->data;
118 children = children->prev;
120 child_allocation = child->widget->allocation;
122 /* Reallocate this child's position so that it does not appear.
123 * Setting the width & height to 0 is not enough, as
124 * one pixel is still drawn. Must also move it outside
125 * visible range. For the cases I've seen, -1, -1 works fine.
126 * This might not work in all future cases. Alternatively, the
127 * items that don't fit could be hidden, but that would interfere
128 * with having other hidden children.
130 * Note that these children are having their size allocated twice,
131 * once by gtk_vbox_size_allocate and then again here. I don't
132 * know of any problems with this, but holler if you do.
134 tiny_allocation.x = tiny_allocation.y = -1;
135 tiny_allocation.height = tiny_allocation.width = 0;
136 gtk_widget_size_allocate (child->widget, &tiny_allocation);
138 /* We're done if the special last item fits now. */
139 if (child_allocation.y + last_child_allocation.height <=
140 allocation->y + allocation->height) {
141 last_child_allocation.y = child_allocation.y;
142 gtk_widget_size_allocate (last_child->widget, &last_child_allocation);
143 break;
146 /* If the special last item still doesn't fit, but we've
147 * run out of earlier items, then the special last item is
148 * just too darn tall. Let's squash it down to fit in the box's
149 * allocation.
151 if (children == NULL) {
152 last_child_allocation.y = allocation->y;
153 last_child_allocation.height = allocation->height;
154 gtk_widget_size_allocate (last_child->widget, &last_child_allocation);