]> Pileus Git - ~andy/gtk/blob - gtk/gtkbin.c
Mark abstract types as G_TYPE_FLAG_ABSTRACT. (#72383)
[~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 GTypeInfo bin_info =
54       {
55         sizeof (GtkBinClass),
56         NULL,            /* base_init */
57         NULL,            /* base_finalize */
58         (GClassInitFunc) gtk_bin_class_init,
59         NULL,            /* class_finalize */
60         NULL,            /* class_data */
61         sizeof (GtkBin),
62         0,               /* n_preallocs */
63         (GInstanceInitFunc) gtk_bin_init,
64         NULL,            /* value_table */
65       };
66
67       bin_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkBin", 
68                                          &bin_info, G_TYPE_FLAG_ABSTRACT);
69     }
70
71   return bin_type;
72 }
73
74 static void
75 gtk_bin_class_init (GtkBinClass *class)
76 {
77   GtkObjectClass *object_class;
78   GtkWidgetClass *widget_class;
79   GtkContainerClass *container_class;
80
81   object_class = (GtkObjectClass*) class;
82   widget_class = (GtkWidgetClass*) class;
83   container_class = (GtkContainerClass*) class;
84
85   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
86
87   container_class->add = gtk_bin_add;
88   container_class->remove = gtk_bin_remove;
89   container_class->forall = gtk_bin_forall;
90   container_class->child_type = gtk_bin_child_type;
91 }
92
93 static void
94 gtk_bin_init (GtkBin *bin)
95 {
96   GTK_WIDGET_SET_FLAGS (bin, GTK_NO_WINDOW);
97
98   bin->child = NULL;
99 }
100
101
102 static GtkType
103 gtk_bin_child_type (GtkContainer *container)
104 {
105   if (!GTK_BIN (container)->child)
106     return GTK_TYPE_WIDGET;
107   else
108     return GTK_TYPE_NONE;
109 }
110
111 static void
112 gtk_bin_add (GtkContainer *container,
113              GtkWidget    *child)
114 {
115   GtkBin *bin = GTK_BIN (container);
116
117   g_return_if_fail (GTK_IS_WIDGET (child));
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 = GTK_BIN (container);
140   gboolean widget_was_visible;
141
142   g_return_if_fail (GTK_IS_WIDGET (child));
143   g_return_if_fail (bin->child == child);
144
145   widget_was_visible = GTK_WIDGET_VISIBLE (child);
146   
147   gtk_widget_unparent (child);
148   bin->child = NULL;
149   
150   /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
151    * since that's what is needed by toplevels, which derive from GtkBin.
152    */
153   if (widget_was_visible)
154     gtk_widget_queue_resize (GTK_WIDGET (container));
155 }
156
157 static void
158 gtk_bin_forall (GtkContainer *container,
159                 gboolean      include_internals,
160                 GtkCallback   callback,
161                 gpointer      callback_data)
162 {
163   GtkBin *bin = GTK_BIN (container);
164
165   g_return_if_fail (callback != NULL);
166
167   if (bin->child)
168     (* callback) (bin->child, callback_data);
169 }
170
171 /**
172  * gtk_bin_get_child:
173  * @bin: a #GtkBin
174  * 
175  * Gets the child of the #GtkBin, or %NULL if the bin contains
176  * no child widget. The returned widget does not have a reference
177  * added, so you do not need to unref it.
178  * 
179  * Return value: pointer to child of the #GtkBin
180  **/
181 GtkWidget*
182 gtk_bin_get_child (GtkBin *bin)
183 {
184   g_return_val_if_fail (GTK_IS_BIN (bin), NULL);
185
186   return bin->child;
187 }