]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
gtk/gtkaction.c gtk/gtkassistant.c gtk/gtkbin.c gtk/gtkbox.c
[~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 "gtkintl.h"
30 #include "gtkalias.h"
31
32 static void gtk_bin_add         (GtkContainer   *container,
33                                  GtkWidget      *widget);
34 static void gtk_bin_remove      (GtkContainer   *container,
35                                  GtkWidget      *widget);
36 static void gtk_bin_forall      (GtkContainer   *container,
37                                  gboolean       include_internals,
38                                  GtkCallback     callback,
39                                  gpointer        callback_data);
40 static GType gtk_bin_child_type (GtkContainer   *container);
41
42
43 G_DEFINE_ABSTRACT_TYPE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER)
44
45 static void
46 gtk_bin_class_init (GtkBinClass *class)
47 {
48   GtkContainerClass *container_class;
49
50   container_class = (GtkContainerClass*) class;
51
52   container_class->add = gtk_bin_add;
53   container_class->remove = gtk_bin_remove;
54   container_class->forall = gtk_bin_forall;
55   container_class->child_type = gtk_bin_child_type;
56 }
57
58 static void
59 gtk_bin_init (GtkBin *bin)
60 {
61   GTK_WIDGET_SET_FLAGS (bin, GTK_NO_WINDOW);
62
63   bin->child = NULL;
64 }
65
66
67 static GType
68 gtk_bin_child_type (GtkContainer *container)
69 {
70   if (!GTK_BIN (container)->child)
71     return GTK_TYPE_WIDGET;
72   else
73     return G_TYPE_NONE;
74 }
75
76 static void
77 gtk_bin_add (GtkContainer *container,
78              GtkWidget    *child)
79 {
80   GtkBin *bin = GTK_BIN (container);
81
82   if (bin->child != NULL)
83     {
84       g_warning ("Attempting to add a widget with type %s to a %s, "
85                  "but as a GtkBin subclass a %s can only contain one widget at a time; "
86                  "it already contains a widget of type %s",
87                  g_type_name (G_OBJECT_TYPE (child)),
88                  g_type_name (G_OBJECT_TYPE (bin)),
89                  g_type_name (G_OBJECT_TYPE (bin)),
90                  g_type_name (G_OBJECT_TYPE (bin->child)));
91       return;
92     }
93
94   gtk_widget_set_parent (child, GTK_WIDGET (bin));
95   bin->child = child;
96 }
97
98 static void
99 gtk_bin_remove (GtkContainer *container,
100                 GtkWidget    *child)
101 {
102   GtkBin *bin = GTK_BIN (container);
103   gboolean widget_was_visible;
104
105   g_return_if_fail (bin->child == child);
106
107   widget_was_visible = GTK_WIDGET_VISIBLE (child);
108   
109   gtk_widget_unparent (child);
110   bin->child = NULL;
111   
112   /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
113    * since that's what is needed by toplevels, which derive from GtkBin.
114    */
115   if (widget_was_visible)
116     gtk_widget_queue_resize (GTK_WIDGET (container));
117 }
118
119 static void
120 gtk_bin_forall (GtkContainer *container,
121                 gboolean      include_internals,
122                 GtkCallback   callback,
123                 gpointer      callback_data)
124 {
125   GtkBin *bin = GTK_BIN (container);
126
127   if (bin->child)
128     (* callback) (bin->child, callback_data);
129 }
130
131 /**
132  * gtk_bin_get_child:
133  * @bin: a #GtkBin
134  * 
135  * Gets the child of the #GtkBin, or %NULL if the bin contains
136  * no child widget. The returned widget does not have a reference
137  * added, so you do not need to unref it.
138  * 
139  * Return value: pointer to child of the #GtkBin
140  **/
141 GtkWidget*
142 gtk_bin_get_child (GtkBin *bin)
143 {
144   g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
145
146   return bin->child;
147 }
148
149 #define __GTK_BIN_C__
150 #include "gtkaliasdef.c"