gtkbin.c 9.77 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27 28 29 30 31 32 33 34 35 36 37 38 39
/**
 * SECTION:gtkbin
 * @Short_description: A container with just one child
 * @Title: GtkBin
 *
 * The #GtkBin widget is a container with just one child.
 * It is not very useful itself, but it is useful for deriving subclasses,
 * since it provides common code needed for handling a single child widget.
 *
 * Many GTK+ widgets are subclasses of #GtkBin, including #GtkWindow,
 * #GtkButton, #GtkFrame, #GtkHandleBox or #GtkScrolledWindow.
 */

40
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
41
#include "gtkbin.h"
42
#include "gtksizerequest.h"
Matthias Clasen's avatar
Matthias Clasen committed
43
#include "gtkintl.h"
44

Elliot Lee's avatar
Elliot Lee committed
45

46
struct _GtkBinPrivate
47 48 49 50
{
  GtkWidget *child;
};

Manish Singh's avatar
Manish Singh committed
51 52 53 54 55 56 57 58 59
static void gtk_bin_add         (GtkContainer   *container,
			         GtkWidget      *widget);
static void gtk_bin_remove      (GtkContainer   *container,
			         GtkWidget      *widget);
static void gtk_bin_forall      (GtkContainer   *container,
				 gboolean	include_internals,
				 GtkCallback     callback,
				 gpointer        callback_data);
static GType gtk_bin_child_type (GtkContainer   *container);
Elliot Lee's avatar
Elliot Lee committed
60 61


62 63 64 65 66 67 68 69 70 71 72
static GtkSizeRequestMode gtk_bin_get_request_mode                (GtkWidget           *widget);
static void               gtk_bin_get_preferred_width_for_height  (GtkWidget           *widget,
                                                                   gint                 height,
                                                                   gint                *minimum_width,
                                                                   gint                *natural_width);
static void               gtk_bin_get_preferred_height_for_width  (GtkWidget           *widget,
                                                                   gint                 width,
                                                                   gint                *minimum_height,
                                                                   gint                *natural_height);

G_DEFINE_ABSTRACT_TYPE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER)
Elliot Lee's avatar
Elliot Lee committed
73 74 75 76

static void
gtk_bin_class_init (GtkBinClass *class)
{
77 78
  GtkWidgetClass *widget_class = (GtkWidgetClass*) class;
  GtkContainerClass *container_class = (GtkContainerClass*) class;
Elliot Lee's avatar
Elliot Lee committed
79

80 81 82
  widget_class->get_request_mode               = gtk_bin_get_request_mode;
  widget_class->get_preferred_width_for_height = gtk_bin_get_preferred_width_for_height;
  widget_class->get_preferred_height_for_width = gtk_bin_get_preferred_height_for_width;
Elliot Lee's avatar
Elliot Lee committed
83 84 85

  container_class->add = gtk_bin_add;
  container_class->remove = gtk_bin_remove;
86
  container_class->forall = gtk_bin_forall;
87
  container_class->child_type = gtk_bin_child_type;
88

89
  g_type_class_add_private (class, sizeof (GtkBinPrivate));
Elliot Lee's avatar
Elliot Lee committed
90 91 92 93 94
}

static void
gtk_bin_init (GtkBin *bin)
{
95
  GtkBinPrivate *priv;
96 97 98

  bin->priv = G_TYPE_INSTANCE_GET_PRIVATE (bin,
                                           GTK_TYPE_BIN,
99
                                           GtkBinPrivate);
100 101
  priv = bin->priv;

102
  gtk_widget_set_has_window (GTK_WIDGET (bin), FALSE);
Elliot Lee's avatar
Elliot Lee committed
103

104
  priv->child = NULL;
Elliot Lee's avatar
Elliot Lee committed
105 106 107
}


Manish Singh's avatar
Manish Singh committed
108
static GType
109 110
gtk_bin_child_type (GtkContainer *container)
{
111
  GtkBinPrivate *priv = GTK_BIN (container)->priv;
112 113

  if (!priv->child)
114 115
    return GTK_TYPE_WIDGET;
  else
Manish Singh's avatar
Manish Singh committed
116
    return G_TYPE_NONE;
117 118
}

Elliot Lee's avatar
Elliot Lee committed
119 120
static void
gtk_bin_add (GtkContainer *container,
121
	     GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
122
{
123
  GtkBin *bin = GTK_BIN (container);
124
  GtkBinPrivate *priv = bin->priv;
Elliot Lee's avatar
Elliot Lee committed
125

126
  if (priv->child != NULL)
127 128 129 130
    {
      g_warning ("Attempting to add a widget with type %s to a %s, "
                 "but as a GtkBin subclass a %s can only contain one widget at a time; "
                 "it already contains a widget of type %s",
Tim Janik's avatar
Tim Janik committed
131 132 133
                 g_type_name (G_OBJECT_TYPE (child)),
                 g_type_name (G_OBJECT_TYPE (bin)),
                 g_type_name (G_OBJECT_TYPE (bin)),
134
                 g_type_name (G_OBJECT_TYPE (priv->child)));
135 136
      return;
    }
Elliot Lee's avatar
Elliot Lee committed
137

138
  gtk_widget_set_parent (child, GTK_WIDGET (bin));
139
  priv->child = child;
Elliot Lee's avatar
Elliot Lee committed
140 141 142 143
}

static void
gtk_bin_remove (GtkContainer *container,
144
		GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
145
{
146
  GtkBin *bin = GTK_BIN (container);
147
  GtkBinPrivate *priv = bin->priv;
148
  gboolean widget_was_visible;
Elliot Lee's avatar
Elliot Lee committed
149

150
  g_return_if_fail (priv->child == child);
Elliot Lee's avatar
Elliot Lee committed
151

152
  widget_was_visible = gtk_widget_get_visible (child);
153 154
  
  gtk_widget_unparent (child);
155
  priv->child = NULL;
156
  
157
  /* queue resize regardless of gtk_widget_get_visible (container),
158 159 160 161
   * since that's what is needed by toplevels, which derive from GtkBin.
   */
  if (widget_was_visible)
    gtk_widget_queue_resize (GTK_WIDGET (container));
Elliot Lee's avatar
Elliot Lee committed
162 163 164
}

static void
165 166 167 168
gtk_bin_forall (GtkContainer *container,
		gboolean      include_internals,
		GtkCallback   callback,
		gpointer      callback_data)
Elliot Lee's avatar
Elliot Lee committed
169
{
170
  GtkBin *bin = GTK_BIN (container);
171
  GtkBinPrivate *priv = bin->priv;
Elliot Lee's avatar
Elliot Lee committed
172

173 174
  if (priv->child)
    (* callback) (priv->child, callback_data);
Elliot Lee's avatar
Elliot Lee committed
175
}
Havoc Pennington's avatar
Havoc Pennington committed
176

177 178

/* GtkBin widgets define the padding and borders independantly so
179
 * we cannot provide a generic get_size() for the same reason
180 181 182 183 184 185 186
 * we never implemented size_request() here.
 *
 * But for cases where the GtkBin class's padding is constant and
 * does not vary based on allocation (most cases), we can at least 
 * deduce a common code path for the get_width_for_height()/get_height_for_width()
 * cases by using the delta of the base size requsts.
 */
187
static GtkSizeRequestMode
188
gtk_bin_get_request_mode (GtkWidget           *widget)
189
{
190
  GtkBin *bin = GTK_BIN (widget);
191
  GtkBinPrivate *priv = bin->priv;
192

193
  if (priv->child)
194
    return gtk_widget_get_request_mode (priv->child);
195

196
  return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
197 198
}

199
static void
200 201 202
get_child_padding_delta (GtkBin *bin,
			 gint   *delta_h,
			 gint   *delta_v)
203
{
204
  GtkBinPrivate *priv = bin->priv;
205
  gint hmin, vmin, hnat, vnat, child_hmin, child_vmin;
206

207 208 209 210 211
  /* we can't use gtk_widget_get_preferred_width() wrapper 
   * because we want our "original" request, not any external
   * adjustments from set_size_request() or whatever.  we have
   * to ask for natural also because NULL isn't allowed for the
   * direct vfuncs
212
   */
213
  GTK_WIDGET_GET_CLASS (bin)->get_preferred_width (GTK_WIDGET (bin), &hmin, &hnat);
214 215 216
  GTK_WIDGET_GET_CLASS (bin)->adjust_size_request (GTK_WIDGET (bin), 
						   GTK_ORIENTATION_HORIZONTAL, &hmin, &hnat);

217
  GTK_WIDGET_GET_CLASS (bin)->get_preferred_height (GTK_WIDGET (bin), &vmin, &vnat);
218 219
  GTK_WIDGET_GET_CLASS (bin)->adjust_size_request (GTK_WIDGET (bin), 
						   GTK_ORIENTATION_VERTICAL, &vmin, &vnat);
220

221 222
  gtk_widget_get_preferred_width (priv->child, &child_hmin, NULL);
  gtk_widget_get_preferred_height (priv->child, &child_vmin, NULL);
223

224 225
  *delta_h = hmin - child_hmin;
  *delta_v = vmin - child_vmin;
226 227 228
}

static void 
229 230 231 232
gtk_bin_get_preferred_width_for_height (GtkWidget *widget,
                                        gint       height,
                                        gint      *minimum_width,
                                        gint      *natural_width)
233
{
234
  GtkBin *bin = GTK_BIN (widget);
235
  GtkBinPrivate *priv = bin->priv;
236 237
  gint    hdelta, vdelta, child_min, child_nat;

238
  if (priv->child)
239 240
    {
      get_child_padding_delta (bin, &hdelta, &vdelta);
241

242 243 244
      gtk_widget_get_preferred_width_for_height (priv->child,
                                                 height - vdelta,
                                                 &child_min, &child_nat);
245

246 247 248 249 250 251 252
      if (minimum_width)
	*minimum_width = child_min + hdelta;
      
      if (natural_width)
	*natural_width = child_nat + hdelta;
    }
  else
253
    GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
254 255 256
}

static void
257 258 259 260
gtk_bin_get_preferred_height_for_width  (GtkWidget *widget,
                                         gint       width,
                                         gint      *minimum_height,
                                         gint      *natural_height)
261
{
262
  GtkBin *bin = GTK_BIN (widget);
263
  GtkBinPrivate *priv = bin->priv;
264 265
  gint    hdelta, vdelta, child_min, child_nat;

266
  if (priv->child)
267 268
    {
      get_child_padding_delta (bin, &hdelta, &vdelta);
269

270 271 272
      gtk_widget_get_preferred_height_for_width (priv->child,
                                                 width - hdelta,
                                                 &child_min, &child_nat);
273

274 275 276 277 278 279 280
      if (minimum_height)
	*minimum_height = child_min + vdelta;
      
      if (natural_height)
	*natural_height = child_nat + vdelta;
    }
  else
281
    GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
282 283 284
}


Havoc Pennington's avatar
Havoc Pennington committed
285 286 287 288 289 290 291
/**
 * gtk_bin_get_child:
 * @bin: a #GtkBin
 * 
 * Gets the child of the #GtkBin, or %NULL if the bin contains
 * no child widget. The returned widget does not have a reference
 * added, so you do not need to unref it.
292 293
 *
 * Return value: (transfer none): pointer to child of the #GtkBin
Havoc Pennington's avatar
Havoc Pennington committed
294 295 296 297 298 299
 **/
GtkWidget*
gtk_bin_get_child (GtkBin *bin)
{
  g_return_val_if_fail (GTK_IS_BIN (bin), NULL);

300
  return bin->priv->child;
Havoc Pennington's avatar
Havoc Pennington committed
301
}
302 303 304 305 306 307 308

void
_gtk_bin_set_child (GtkBin    *bin,
                    GtkWidget *widget)
{
  bin->priv->child = widget;
}