gtkbin.c 10.9 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * 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/. 
 */

25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * 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.
 */

38
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
39
#include "gtkbin.h"
40
#include "gtksizerequest.h"
41
#include "gtkintl.h"
42

Elliot Lee's avatar
Elliot Lee committed
43

44
struct _GtkBinPrivate
45 46 47 48
{
  GtkWidget *child;
};

Manish Singh's avatar
Manish Singh committed
49 50 51 52 53 54 55 56 57
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
58

59 60 61 62 63 64
static void               gtk_bin_get_preferred_width             (GtkWidget           *widget,
                                                                   gint                *minimum_width,
                                                                   gint                *natural_width);
static void               gtk_bin_get_preferred_height            (GtkWidget           *widget,
                                                                   gint                *minimum_height,
                                                                   gint                *natural_height);
65 66 67 68 69 70 71 72
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);
73 74
static void               gtk_bin_size_allocate                   (GtkWidget           *widget,
                                                                   GtkAllocation       *allocation);
75

76
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkBin, gtk_bin, GTK_TYPE_CONTAINER)
Elliot Lee's avatar
Elliot Lee committed
77 78 79 80

static void
gtk_bin_class_init (GtkBinClass *class)
{
81 82
  GtkWidgetClass *widget_class = (GtkWidgetClass*) class;
  GtkContainerClass *container_class = (GtkContainerClass*) class;
Elliot Lee's avatar
Elliot Lee committed
83

84 85
  widget_class->get_preferred_width = gtk_bin_get_preferred_width;
  widget_class->get_preferred_height = gtk_bin_get_preferred_height;
86 87
  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;
88
  widget_class->size_allocate = gtk_bin_size_allocate;
Elliot Lee's avatar
Elliot Lee committed
89 90 91

  container_class->add = gtk_bin_add;
  container_class->remove = gtk_bin_remove;
92
  container_class->forall = gtk_bin_forall;
93
  container_class->child_type = gtk_bin_child_type;
Elliot Lee's avatar
Elliot Lee committed
94 95 96 97 98
}

static void
gtk_bin_init (GtkBin *bin)
{
99
  bin->priv = gtk_bin_get_instance_private (bin);
100

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


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

  if (!priv->child)
111 112
    return GTK_TYPE_WIDGET;
  else
Manish Singh's avatar
Manish Singh committed
113
    return G_TYPE_NONE;
114 115
}

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

123
  if (priv->child != NULL)
124 125 126 127
    {
      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
128 129 130
                 g_type_name (G_OBJECT_TYPE (child)),
                 g_type_name (G_OBJECT_TYPE (bin)),
                 g_type_name (G_OBJECT_TYPE (bin)),
131
                 g_type_name (G_OBJECT_TYPE (priv->child)));
132 133
      return;
    }
Elliot Lee's avatar
Elliot Lee committed
134

135
  gtk_widget_set_parent (child, GTK_WIDGET (bin));
136
  priv->child = child;
Elliot Lee's avatar
Elliot Lee committed
137 138 139 140
}

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

147
  g_return_if_fail (priv->child == child);
Elliot Lee's avatar
Elliot Lee committed
148

149
  widget_was_visible = gtk_widget_get_visible (child);
150 151
  
  gtk_widget_unparent (child);
152
  priv->child = NULL;
153
  
154
  /* queue resize regardless of gtk_widget_get_visible (container),
155 156 157 158
   * 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
159 160 161
}

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

170 171
  if (priv->child)
    (* callback) (priv->child, callback_data);
Elliot Lee's avatar
Elliot Lee committed
172
}
Havoc Pennington's avatar
Havoc Pennington committed
173

174 175 176 177 178 179 180 181
static int
gtk_bin_get_effective_border_width (GtkBin *bin)
{
  if (GTK_CONTAINER_CLASS (GTK_BIN_GET_CLASS (bin))->_handle_border_width)
    return 0;

  return gtk_container_get_border_width (GTK_CONTAINER (bin));
}
182

183 184 185 186 187 188 189
static void
gtk_bin_get_preferred_width (GtkWidget *widget,
                             gint      *minimum_width,
                             gint      *natural_width)
{
  GtkBin *bin = GTK_BIN (widget);
  GtkBinPrivate *priv = bin->priv;
190
  gint border_width;
191

192 193 194
  *minimum_width = 0;
  *natural_width = 0;

195
  if (priv->child && gtk_widget_get_visible (priv->child))
196 197 198 199 200 201 202
    {
      gint child_min, child_nat;
      gtk_widget_get_preferred_width (priv->child,
                                      &child_min, &child_nat);
      *minimum_width = child_min;
      *natural_width = child_nat;
    }
203 204 205 206

  border_width = gtk_bin_get_effective_border_width (bin);
  *minimum_width += 2 * border_width;
  *natural_width += 2 * border_width;
207 208 209 210 211 212 213 214 215
}

static void
gtk_bin_get_preferred_height (GtkWidget *widget,
                              gint      *minimum_height,
                              gint      *natural_height)
{
  GtkBin *bin = GTK_BIN (widget);
  GtkBinPrivate *priv = bin->priv;
216
  gint border_width;
217

218 219 220
  *minimum_height = 0;
  *natural_height = 0;

221
  if (priv->child && gtk_widget_get_visible (priv->child))
222 223 224 225 226 227 228
    {
      gint child_min, child_nat;
      gtk_widget_get_preferred_height (priv->child,
                                       &child_min, &child_nat);
      *minimum_height = child_min;
      *natural_height = child_nat;
    }
229 230 231 232

  border_width = gtk_bin_get_effective_border_width (bin);
  *minimum_height += 2 * border_width;
  *natural_height += 2 * border_width;
233 234
}

235
static void 
236 237 238 239
gtk_bin_get_preferred_width_for_height (GtkWidget *widget,
                                        gint       height,
                                        gint      *minimum_width,
                                        gint      *natural_width)
240
{
241
  GtkBin *bin = GTK_BIN (widget);
242
  GtkBinPrivate *priv = bin->priv;
243
  gint border_width;
244

245 246 247
  *minimum_width = 0;
  *natural_width = 0;

248 249
  border_width = gtk_bin_get_effective_border_width (bin);

250
  if (priv->child && gtk_widget_get_visible (priv->child))
251
    {
252 253
      gint child_min, child_nat;
      gtk_widget_get_preferred_width_for_height (priv->child, height - 2 * border_width,
254
                                                 &child_min, &child_nat);
255

256 257
      *minimum_width = child_min;
      *natural_width = child_nat;
258
    }
259 260 261

  *minimum_width += 2 * border_width;
  *natural_width += 2 * border_width;
262 263 264
}

static void
265 266 267 268
gtk_bin_get_preferred_height_for_width  (GtkWidget *widget,
                                         gint       width,
                                         gint      *minimum_height,
                                         gint      *natural_height)
269
{
270
  GtkBin *bin = GTK_BIN (widget);
271
  GtkBinPrivate *priv = bin->priv;
272
  gint border_width;
273

274 275 276
  *minimum_height = 0;
  *natural_height = 0;

277 278
  border_width = gtk_bin_get_effective_border_width (bin);

279
  if (priv->child && gtk_widget_get_visible (priv->child))
280
    {
281 282
      gint child_min, child_nat;
      gtk_widget_get_preferred_height_for_width (priv->child, width - 2 * border_width,
283
                                                 &child_min, &child_nat);
284

285 286
      *minimum_height = child_min;
      *natural_height = child_nat;
287
    }
288 289 290

  *minimum_height += 2 * border_width;
  *natural_height += 2 * border_width;
291 292
}

293 294 295 296 297 298 299 300 301
static void
gtk_bin_size_allocate (GtkWidget     *widget,
                       GtkAllocation *allocation)
{
  GtkBin *bin = GTK_BIN (widget);
  GtkBinPrivate *priv = bin->priv;

  gtk_widget_set_allocation (widget, allocation);

302
  if (priv->child && gtk_widget_get_visible (priv->child))
303 304 305 306 307 308 309 310 311 312 313
    {
      GtkAllocation child_allocation;
      gint border_width = gtk_bin_get_effective_border_width (bin);

      child_allocation.x = allocation->x + border_width;
      child_allocation.y = allocation->y + border_width;
      child_allocation.width = allocation->width - 2 * border_width;
      child_allocation.height = allocation->height - 2 * border_width;

      gtk_widget_size_allocate (priv->child, &child_allocation);
    }
314
}
315

Havoc Pennington's avatar
Havoc Pennington committed
316 317 318 319 320 321 322
/**
 * 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.
323
 *
324 325
 * Returns: (transfer none) (nullable): the child of @bin, or %NULL if it does
 * not have a child.
Havoc Pennington's avatar
Havoc Pennington committed
326 327 328 329 330 331
 **/
GtkWidget*
gtk_bin_get_child (GtkBin *bin)
{
  g_return_val_if_fail (GTK_IS_BIN (bin), NULL);

332
  return bin->priv->child;
Havoc Pennington's avatar
Havoc Pennington committed
333
}
334

335 336 337 338 339 340
void
_gtk_bin_set_child (GtkBin    *bin,
                    GtkWidget *widget)
{
  bin->priv->child = widget;
}