]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
[introspection] Merge in Gtk-custom.c annotations
[~andy/gtk] / gtk / gtkbin.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28 #include "gtkbin.h"
29 #include "gtkextendedlayout.h"
30 #include "gtkintl.h"
31 #include "gtkalias.h"
32
33 static void gtk_bin_add         (GtkContainer   *container,
34                                  GtkWidget      *widget);
35 static void gtk_bin_remove      (GtkContainer   *container,
36                                  GtkWidget      *widget);
37 static void gtk_bin_forall      (GtkContainer   *container,
38                                  gboolean       include_internals,
39                                  GtkCallback     callback,
40                                  gpointer        callback_data);
41 static GType gtk_bin_child_type (GtkContainer   *container);
42
43 static void gtk_bin_extended_layout_interface_init (GtkExtendedLayoutIface *iface);
44
45 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER,
46                                   G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
47                                                          gtk_bin_extended_layout_interface_init))
48
49
50 static void
51 gtk_bin_class_init (GtkBinClass *class)
52 {
53   GtkContainerClass *container_class;
54
55   container_class = (GtkContainerClass*) class;
56
57   container_class->add = gtk_bin_add;
58   container_class->remove = gtk_bin_remove;
59   container_class->forall = gtk_bin_forall;
60   container_class->child_type = gtk_bin_child_type;
61 }
62
63 static void
64 gtk_bin_init (GtkBin *bin)
65 {
66   GTK_WIDGET_SET_FLAGS (bin, GTK_NO_WINDOW);
67
68   bin->child = NULL;
69 }
70
71
72 static GType
73 gtk_bin_child_type (GtkContainer *container)
74 {
75   if (!GTK_BIN (container)->child)
76     return GTK_TYPE_WIDGET;
77   else
78     return G_TYPE_NONE;
79 }
80
81 static void
82 gtk_bin_add (GtkContainer *container,
83              GtkWidget    *child)
84 {
85   GtkBin *bin = GTK_BIN (container);
86
87   if (bin->child != NULL)
88     {
89       g_warning ("Attempting to add a widget with type %s to a %s, "
90                  "but as a GtkBin subclass a %s can only contain one widget at a time; "
91                  "it already contains a widget of type %s",
92                  g_type_name (G_OBJECT_TYPE (child)),
93                  g_type_name (G_OBJECT_TYPE (bin)),
94                  g_type_name (G_OBJECT_TYPE (bin)),
95                  g_type_name (G_OBJECT_TYPE (bin->child)));
96       return;
97     }
98
99   gtk_widget_set_parent (child, GTK_WIDGET (bin));
100   bin->child = child;
101 }
102
103 static void
104 gtk_bin_remove (GtkContainer *container,
105                 GtkWidget    *child)
106 {
107   GtkBin *bin = GTK_BIN (container);
108   gboolean widget_was_visible;
109
110   g_return_if_fail (bin->child == child);
111
112   widget_was_visible = GTK_WIDGET_VISIBLE (child);
113   
114   gtk_widget_unparent (child);
115   bin->child = NULL;
116   
117   /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
118    * since that's what is needed by toplevels, which derive from GtkBin.
119    */
120   if (widget_was_visible)
121     gtk_widget_queue_resize (GTK_WIDGET (container));
122 }
123
124 static void
125 gtk_bin_forall (GtkContainer *container,
126                 gboolean      include_internals,
127                 GtkCallback   callback,
128                 gpointer      callback_data)
129 {
130   GtkBin *bin = GTK_BIN (container);
131
132   if (bin->child)
133     (* callback) (bin->child, callback_data);
134 }
135
136 /**
137  * gtk_bin_get_child:
138  * @bin: a #GtkBin
139  * 
140  * Gets the child of the #GtkBin, or %NULL if the bin contains
141  * no child widget. The returned widget does not have a reference
142  * added, so you do not need to unref it.
143  *
144  * Return value: (transfer none): pointer to child of the #GtkBin
145  **/
146 GtkWidget*
147 gtk_bin_get_child (GtkBin *bin)
148 {
149   g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
150
151   return bin->child;
152 }
153
154 static void
155 gtk_bin_extended_layout_get_desired_size (GtkExtendedLayout *layout,
156                                           GtkRequisition    *minimum_size,
157                                           GtkRequisition    *natural_size)
158 {
159   GtkBin *bin = GTK_BIN (layout);
160
161   g_return_if_fail (GTK_IS_EXTENDED_LAYOUT (bin->child));
162
163   gtk_extended_layout_get_desired_size (GTK_EXTENDED_LAYOUT (bin->child),
164                                         minimum_size, natural_size);
165 }
166
167 static void
168 gtk_bin_extended_layout_get_width_for_height (GtkExtendedLayout *layout,
169                                               gint               height,
170                                               gint              *minimum_width,
171                                               gint              *natural_width)
172 {
173   GtkBin *bin = GTK_BIN (layout);
174
175   g_return_if_fail (GTK_IS_EXTENDED_LAYOUT (bin->child));
176
177   gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (bin->child),
178                                             height, minimum_width, natural_width);
179 }
180
181 static void
182 gtk_bin_extended_layout_get_height_for_width (GtkExtendedLayout *layout,
183                                               gint               width,
184                                               gint              *minimum_height,
185                                               gint              *natural_height)
186 {
187   GtkBin *bin = GTK_BIN (layout);
188
189   g_return_if_fail (GTK_IS_EXTENDED_LAYOUT (bin->child));
190
191   gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (bin->child),
192                                             width, minimum_height, natural_height);
193 }
194
195 static void
196 gtk_bin_extended_layout_interface_init (GtkExtendedLayoutIface *iface)
197 {
198   iface->get_desired_size = gtk_bin_extended_layout_get_desired_size;
199   iface->get_height_for_width = gtk_bin_extended_layout_get_height_for_width;
200   iface->get_width_for_height = gtk_bin_extended_layout_get_width_for_height;
201 }
202
203 #define __GTK_BIN_C__
204 #include "gtkaliasdef.c"