]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
Use GtkType/GType instead of uint.
[~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 "gtkbin.h"
28
29
30 static void gtk_bin_class_init (GtkBinClass    *klass);
31 static void gtk_bin_init       (GtkBin         *bin);
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 GtkType gtk_bin_child_type (GtkContainer*container);
41
42
43 static GtkContainerClass *parent_class = NULL;
44
45
46 GtkType
47 gtk_bin_get_type (void)
48 {
49   static GtkType bin_type = 0;
50
51   if (!bin_type)
52     {
53       static const GtkTypeInfo bin_info =
54       {
55         "GtkBin",
56         sizeof (GtkBin),
57         sizeof (GtkBinClass),
58         (GtkClassInitFunc) gtk_bin_class_init,
59         (GtkObjectInitFunc) gtk_bin_init,
60         /* reserved_1 */ NULL,
61         /* reserved_2 */ NULL,
62         (GtkClassInitFunc) NULL,
63       };
64
65       bin_type = gtk_type_unique (GTK_TYPE_CONTAINER, &bin_info);
66     }
67
68   return bin_type;
69 }
70
71 static void
72 gtk_bin_class_init (GtkBinClass *class)
73 {
74   GtkObjectClass *object_class;
75   GtkWidgetClass *widget_class;
76   GtkContainerClass *container_class;
77
78   object_class = (GtkObjectClass*) class;
79   widget_class = (GtkWidgetClass*) class;
80   container_class = (GtkContainerClass*) class;
81
82   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
83
84   container_class->add = gtk_bin_add;
85   container_class->remove = gtk_bin_remove;
86   container_class->forall = gtk_bin_forall;
87   container_class->child_type = gtk_bin_child_type;
88 }
89
90 static void
91 gtk_bin_init (GtkBin *bin)
92 {
93   GTK_WIDGET_SET_FLAGS (bin, GTK_NO_WINDOW);
94
95   bin->child = NULL;
96 }
97
98
99 static GtkType
100 gtk_bin_child_type (GtkContainer *container)
101 {
102   if (!GTK_BIN (container)->child)
103     return GTK_TYPE_WIDGET;
104   else
105     return GTK_TYPE_NONE;
106 }
107
108 static void
109 gtk_bin_add (GtkContainer *container,
110              GtkWidget    *child)
111 {
112   GtkBin *bin;
113
114   g_return_if_fail (GTK_IS_BIN (container));
115   g_return_if_fail (GTK_IS_WIDGET (child));
116
117   bin = GTK_BIN (container);
118
119   if (bin->child != NULL)
120     {
121       g_warning ("Attempting to add a widget with type %s to a %s, "
122                  "but as a GtkBin subclass a %s can only contain one widget at a time; "
123                  "it already contains a widget of type %s",
124                  g_type_name (G_OBJECT_TYPE (child)),
125                  g_type_name (G_OBJECT_TYPE (bin)),
126                  g_type_name (G_OBJECT_TYPE (bin)),
127                  g_type_name (G_OBJECT_TYPE (bin->child)));
128       return;
129     }
130
131   gtk_widget_set_parent (child, GTK_WIDGET (bin));
132   bin->child = child;
133 }
134
135 static void
136 gtk_bin_remove (GtkContainer *container,
137                 GtkWidget    *child)
138 {
139   GtkBin *bin;
140   gboolean widget_was_visible;
141
142   g_return_if_fail (GTK_IS_BIN (container));
143   g_return_if_fail (GTK_IS_WIDGET (child));
144
145   bin = GTK_BIN (container);
146   g_return_if_fail (bin->child == child);
147
148   widget_was_visible = GTK_WIDGET_VISIBLE (child);
149   
150   gtk_widget_unparent (child);
151   bin->child = NULL;
152   
153   /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
154    * since that's what is needed by toplevels, which derive from GtkBin.
155    */
156   if (widget_was_visible)
157     gtk_widget_queue_resize (GTK_WIDGET (container));
158 }
159
160 static void
161 gtk_bin_forall (GtkContainer *container,
162                 gboolean      include_internals,
163                 GtkCallback   callback,
164                 gpointer      callback_data)
165 {
166   GtkBin *bin;
167
168   g_return_if_fail (GTK_IS_BIN (container));
169   g_return_if_fail (callback != NULL);
170
171   bin = GTK_BIN (container);
172
173   if (bin->child)
174     (* callback) (bin->child, callback_data);
175 }
176
177 /**
178  * gtk_bin_get_child:
179  * @bin: a #GtkBin
180  * 
181  * Gets the child of the #GtkBin, or %NULL if the bin contains
182  * no child widget. The returned widget does not have a reference
183  * added, so you do not need to unref it.
184  * 
185  * Return value: pointer to child of the #GtkBin
186  **/
187 GtkWidget*
188 gtk_bin_get_child (GtkBin *bin)
189 {
190   g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
191
192   return bin->child;
193 }