gtkviewport.c 31.6 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
#include "config.h"
28 29

#include "gtkviewport.h"
Javier Jardón's avatar
Javier Jardón committed
30

Alexander Larsson's avatar
Alexander Larsson committed
31
#include "gtkintl.h"
32
#include "gtkmarshalers.h"
Tadej Borovšak's avatar
Tadej Borovšak committed
33
#include "gtkscrollable.h"
34
#include "gtktypebuiltins.h"
35
#include "gtkprivate.h"
36

Elliot Lee's avatar
Elliot Lee committed
37

38 39 40 41 42 43 44 45 46
/**
 * SECTION:gtkviewport
 * @Short_description: An adapter which makes widgets scrollable
 * @Title: GtkViewport
 * @See_also:#GtkScrolledWindow, #GtkAdjustment
 *
 * The #GtkViewport widget acts as an adaptor class, implementing
 * scrollability for child widgets that lack their own scrolling
 * capabilities. Use #GtkViewport to scroll child widgets such as
47
 * #GtkGrid, #GtkBox, and so on.
48 49 50 51 52 53 54
 *
 * If a widget has native scrolling abilities, such as #GtkTextView,
 * #GtkTreeView or #GtkIconview, it can be added to a #GtkScrolledWindow
 * with gtk_container_add(). If a widget does not, you must first add the
 * widget to a #GtkViewport, then add the viewport to the scrolled window.
 * The convenience function gtk_scrolled_window_add_with_viewport() does
 * exactly this, so you can ignore the presence of the viewport.
55 56 57
 *
 * The #GtkViewport will start scrolling content only if allocated less
 * than the child widget's minimum size in a given orientation.
58 59
 */

60
struct _GtkViewportPrivate
61 62 63 64 65 66 67
{
  GtkAdjustment  *hadjustment;
  GtkAdjustment  *vadjustment;
  GtkShadowType   shadow_type;

  GdkWindow      *bin_window;
  GdkWindow      *view_window;
68 69 70 71 72

  /* GtkScrollablePolicy needs to be checked when
   * driving the scrollable adjustment values */
  guint hscroll_policy : 1;
  guint vscroll_policy : 1;
73 74
};

75
enum {
Alexander Larsson's avatar
Alexander Larsson committed
76 77 78
  PROP_0,
  PROP_HADJUSTMENT,
  PROP_VADJUSTMENT,
79 80
  PROP_HSCROLL_POLICY,
  PROP_VSCROLL_POLICY,
Alexander Larsson's avatar
Alexander Larsson committed
81
  PROP_SHADOW_TYPE
82 83 84
};


85
static void gtk_viewport_finalize                 (GObject          *object);
Alexander Larsson's avatar
Alexander Larsson committed
86 87 88 89 90 91 92 93
static void gtk_viewport_set_property             (GObject         *object,
						   guint            prop_id,
						   const GValue    *value,
						   GParamSpec      *pspec);
static void gtk_viewport_get_property             (GObject         *object,
						   guint            prop_id,
						   GValue          *value,
						   GParamSpec      *pspec);
94
static void gtk_viewport_destroy                  (GtkWidget        *widget);
Elliot Lee's avatar
Elliot Lee committed
95 96
static void gtk_viewport_realize                  (GtkWidget        *widget);
static void gtk_viewport_unrealize                (GtkWidget        *widget);
Benjamin Otte's avatar
Benjamin Otte committed
97 98
static gint gtk_viewport_draw                     (GtkWidget        *widget,
						   cairo_t          *cr);
99 100
static void gtk_viewport_add                      (GtkContainer     *container,
						   GtkWidget        *widget);
Elliot Lee's avatar
Elliot Lee committed
101 102 103 104
static void gtk_viewport_size_allocate            (GtkWidget        *widget,
						   GtkAllocation    *allocation);
static void gtk_viewport_adjustment_value_changed (GtkAdjustment    *adjustment,
						   gpointer          data);
105
static void gtk_viewport_style_updated            (GtkWidget        *widget);
Elliot Lee's avatar
Elliot Lee committed
106

107 108 109 110 111 112
static void gtk_viewport_get_preferred_width      (GtkWidget        *widget,
						   gint             *minimum_size,
						   gint             *natural_size);
static void gtk_viewport_get_preferred_height     (GtkWidget        *widget,
						   gint             *minimum_size,
						   gint             *natural_size);
113

114

Tadej Borovšak's avatar
Tadej Borovšak committed
115 116
G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
Elliot Lee's avatar
Elliot Lee committed
117 118 119 120

static void
gtk_viewport_class_init (GtkViewportClass *class)
{
Alexander Larsson's avatar
Alexander Larsson committed
121
  GObjectClass   *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
122 123 124
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

Alexander Larsson's avatar
Alexander Larsson committed
125
  gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
126 127
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;
Manish Singh's avatar
Manish Singh committed
128

129
  gobject_class->finalize = gtk_viewport_finalize;
Alexander Larsson's avatar
Alexander Larsson committed
130 131
  gobject_class->set_property = gtk_viewport_set_property;
  gobject_class->get_property = gtk_viewport_get_property;
132 133

  widget_class->destroy = gtk_viewport_destroy;
Elliot Lee's avatar
Elliot Lee committed
134 135
  widget_class->realize = gtk_viewport_realize;
  widget_class->unrealize = gtk_viewport_unrealize;
Benjamin Otte's avatar
Benjamin Otte committed
136
  widget_class->draw = gtk_viewport_draw;
Elliot Lee's avatar
Elliot Lee committed
137
  widget_class->size_allocate = gtk_viewport_size_allocate;
138
  widget_class->style_updated = gtk_viewport_style_updated;
139 140
  widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
  widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
141
  
142 143
  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_VIEWPORT);

144 145
  container_class->add = gtk_viewport_add;

Tadej Borovšak's avatar
Tadej Borovšak committed
146
  /* GtkScrollable implementation */
147 148 149 150
  g_object_class_override_property (gobject_class, PROP_HADJUSTMENT,    "hadjustment");
  g_object_class_override_property (gobject_class, PROP_VADJUSTMENT,    "vadjustment");
  g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
  g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
Alexander Larsson's avatar
Alexander Larsson committed
151 152 153

  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
154
                                   g_param_spec_enum ("shadow-type",
155 156
						      P_("Shadow type"),
						      P_("Determines how the shadowed box around the viewport is drawn"),
Alexander Larsson's avatar
Alexander Larsson committed
157 158
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_IN,
159
						      GTK_PARAM_READWRITE));
160

161
  g_type_class_add_private (class, sizeof (GtkViewportPrivate));
Elliot Lee's avatar
Elliot Lee committed
162 163
}

164
static void
Alexander Larsson's avatar
Alexander Larsson committed
165 166 167 168
gtk_viewport_set_property (GObject         *object,
			   guint            prop_id,
			   const GValue    *value,
			   GParamSpec      *pspec)
169 170 171 172 173
{
  GtkViewport *viewport;

  viewport = GTK_VIEWPORT (object);

Alexander Larsson's avatar
Alexander Larsson committed
174
  switch (prop_id)
175
    {
Alexander Larsson's avatar
Alexander Larsson committed
176 177
    case PROP_HADJUSTMENT:
      gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
178
      break;
Alexander Larsson's avatar
Alexander Larsson committed
179 180
    case PROP_VADJUSTMENT:
      gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
181
      break;
182 183 184 185 186 187 188 189
    case PROP_HSCROLL_POLICY:
      viewport->priv->hscroll_policy = g_value_get_enum (value);
      gtk_widget_queue_resize (GTK_WIDGET (viewport));
      break;
    case PROP_VSCROLL_POLICY:
      viewport->priv->vscroll_policy = g_value_get_enum (value);
      gtk_widget_queue_resize (GTK_WIDGET (viewport));
      break;
Alexander Larsson's avatar
Alexander Larsson committed
190 191
    case PROP_SHADOW_TYPE:
      gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
192
      break;
193
    default:
Alexander Larsson's avatar
Alexander Larsson committed
194
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195 196 197 198 199
      break;
    }
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
200 201 202 203
gtk_viewport_get_property (GObject         *object,
			   guint            prop_id,
			   GValue          *value,
			   GParamSpec      *pspec)
204
{
205
  GtkViewport *viewport = GTK_VIEWPORT (object);
206
  GtkViewportPrivate *priv = viewport->priv;
207

Alexander Larsson's avatar
Alexander Larsson committed
208
  switch (prop_id)
209
    {
Alexander Larsson's avatar
Alexander Larsson committed
210
    case PROP_HADJUSTMENT:
211
      g_value_set_object (value, priv->hadjustment);
212
      break;
Alexander Larsson's avatar
Alexander Larsson committed
213
    case PROP_VADJUSTMENT:
214
      g_value_set_object (value, priv->vadjustment);
215
      break;
216 217 218 219 220 221
    case PROP_HSCROLL_POLICY:
      g_value_set_enum (value, priv->hscroll_policy);
      break;
    case PROP_VSCROLL_POLICY:
      g_value_set_enum (value, priv->vscroll_policy);
      break;
Alexander Larsson's avatar
Alexander Larsson committed
222
    case PROP_SHADOW_TYPE:
223
      g_value_set_enum (value, priv->shadow_type);
224 225
      break;
    default:
Alexander Larsson's avatar
Alexander Larsson committed
226
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 228 229 230
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
231 232 233
static void
gtk_viewport_init (GtkViewport *viewport)
{
234
  GtkViewportPrivate *priv;
235 236 237

  viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
                                                GTK_TYPE_VIEWPORT,
238
                                                GtkViewportPrivate);
239 240
  priv = viewport->priv;

241
  gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
Elliot Lee's avatar
Elliot Lee committed
242

243
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
244
  gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
245 246 247 248 249 250

  priv->shadow_type = GTK_SHADOW_IN;
  priv->view_window = NULL;
  priv->bin_window = NULL;
  priv->hadjustment = NULL;
  priv->vadjustment = NULL;
Elliot Lee's avatar
Elliot Lee committed
251 252
}

Matthias Clasen's avatar
Matthias Clasen committed
253 254
/**
 * gtk_viewport_new:
Matthias Clasen's avatar
Matthias Clasen committed
255 256
 * @hadjustment: horizontal adjustment
 * @vadjustment: vertical adjustment
Matthias Clasen's avatar
Matthias Clasen committed
257 258 259
 *
 * Creates a new #GtkViewport with the given adjustments.
 *
Matthias Clasen's avatar
Matthias Clasen committed
260 261
 * Returns: a new #GtkViewport
 */
Elliot Lee's avatar
Elliot Lee committed
262 263 264 265
GtkWidget*
gtk_viewport_new (GtkAdjustment *hadjustment,
		  GtkAdjustment *vadjustment)
{
266 267
  GtkWidget *viewport;

268
  viewport = g_object_new (GTK_TYPE_VIEWPORT,
269 270 271 272 273 274
			     "hadjustment", hadjustment,
			     "vadjustment", vadjustment,
			     NULL);

  return viewport;
}
Elliot Lee's avatar
Elliot Lee committed
275

276 277
#define ADJUSTMENT_POINTER(viewport, orientation)         \
  (((orientation) == GTK_ORIENTATION_HORIZONTAL) ?        \
278
     &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
279

280
static void
281 282
viewport_disconnect_adjustment (GtkViewport    *viewport,
				GtkOrientation  orientation)
283
{
284
  GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
Elliot Lee's avatar
Elliot Lee committed
285

286
  if (*adjustmentp)
287
    {
288
      g_signal_handlers_disconnect_by_func (*adjustmentp,
Manish Singh's avatar
Manish Singh committed
289 290
					    gtk_viewport_adjustment_value_changed,
					    viewport);
291 292
      g_object_unref (*adjustmentp);
      *adjustmentp = NULL;
293
    }
294 295 296 297 298 299 300 301 302 303
}

static void
gtk_viewport_finalize (GObject *object)
{
  GtkViewport *viewport = GTK_VIEWPORT (object);

  viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
  viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);

Matthias Clasen's avatar
Matthias Clasen committed
304
  G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
305 306 307
}

static void
308
gtk_viewport_destroy (GtkWidget *widget)
309
{
310
  GtkViewport *viewport = GTK_VIEWPORT (widget);
311 312 313

  viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
  viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
314

315
  GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
316 317
}

318 319 320 321 322 323 324
static void
viewport_get_view_allocation (GtkViewport   *viewport,
			      GtkAllocation *view_allocation)
{
  GtkViewportPrivate *priv = viewport->priv;
  GtkWidget *widget = GTK_WIDGET (viewport);
  GtkAllocation allocation;
325 326 327
  GtkStyleContext *context;
  GtkStateFlags state;
  GtkBorder padding, border;
328 329 330 331 332 333 334 335
  guint border_width;

  gtk_widget_get_allocation (widget, &allocation);
  border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));

  view_allocation->x = 0;
  view_allocation->y = 0;

336 337 338 339 340
  context = gtk_widget_get_style_context (widget);
  state = gtk_widget_get_state_flags (widget);
  gtk_style_context_get_padding (context, state, &padding);
  gtk_style_context_get_border (context, state, &border);

341 342
  if (priv->shadow_type != GTK_SHADOW_NONE)
    {
343 344
      view_allocation->x = border.left;
      view_allocation->y = border.top;
345 346
    }

347 348 349 350 351 352 353 354 355 356
  view_allocation->x += padding.left;
  view_allocation->y += padding.right;
  view_allocation->width = MAX (1, allocation.width - padding.left - padding.right - border_width * 2);
  view_allocation->height = MAX (1, allocation.height - padding.top - padding.bottom - border_width * 2);

  if (priv->shadow_type != GTK_SHADOW_NONE)
    {
      view_allocation->width = MAX (1, view_allocation->width - border.left - border.right);
      view_allocation->height = MAX (1, view_allocation->height - border.top - border.bottom);
    }
357 358
}

Matthias Clasen's avatar
Matthias Clasen committed
359 360 361
/**
 * gtk_viewport_get_hadjustment:
 * @viewport: a #GtkViewport.
Matthias Clasen's avatar
Matthias Clasen committed
362
 *
Matthias Clasen's avatar
Matthias Clasen committed
363 364
 * Returns the horizontal adjustment of the viewport.
 *
Matthias Clasen's avatar
Matthias Clasen committed
365
 * Return value: (transfer none): the horizontal adjustment of @viewport.
Tadej Borovšak's avatar
Tadej Borovšak committed
366 367
 *
 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
Matthias Clasen's avatar
Matthias Clasen committed
368
 **/
Elliot Lee's avatar
Elliot Lee committed
369 370 371
GtkAdjustment*
gtk_viewport_get_hadjustment (GtkViewport *viewport)
{
372
  GtkViewportPrivate *priv;
373

Elliot Lee's avatar
Elliot Lee committed
374 375
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);

376 377 378
  priv = viewport->priv;

  if (!priv->hadjustment)
379 380
    gtk_viewport_set_hadjustment (viewport, NULL);

381
  return priv->hadjustment;
Elliot Lee's avatar
Elliot Lee committed
382 383
}

Matthias Clasen's avatar
Matthias Clasen committed
384 385 386 387 388 389
/**
 * gtk_viewport_get_vadjustment:
 * @viewport: a #GtkViewport.
 * 
 * Returns the vertical adjustment of the viewport.
 *
Matthias Clasen's avatar
Matthias Clasen committed
390
 * Return value: (transfer none): the vertical adjustment of @viewport.
Tadej Borovšak's avatar
Tadej Borovšak committed
391 392
 *
 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
Matthias Clasen's avatar
Matthias Clasen committed
393
 **/
Elliot Lee's avatar
Elliot Lee committed
394 395 396
GtkAdjustment*
gtk_viewport_get_vadjustment (GtkViewport *viewport)
{
397
  GtkViewportPrivate *priv;
398

Elliot Lee's avatar
Elliot Lee committed
399 400
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);

401 402 403
  priv = viewport->priv;

  if (!priv->vadjustment)
404 405
    gtk_viewport_set_vadjustment (viewport, NULL);

406
  return priv->vadjustment;
Elliot Lee's avatar
Elliot Lee committed
407 408
}

409
static void
410
viewport_set_hadjustment_values (GtkViewport *viewport)
411 412 413 414
{
  GtkBin *bin = GTK_BIN (viewport);
  GtkAllocation view_allocation;
  GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
Javier Jardón's avatar
Javier Jardón committed
415
  GtkWidget *child;
416
  gdouble upper, value;
417
  
418 419
  viewport_get_view_allocation (viewport, &view_allocation);  

Javier Jardón's avatar
Javier Jardón committed
420 421
  child = gtk_bin_get_child (bin);
  if (child && gtk_widget_get_visible (child))
422
    {
423 424 425 426 427 428 429
      gint minimum_width, natural_width;
      gint scroll_height;
      
      if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
	gtk_widget_get_preferred_height (child, &scroll_height, NULL);
      else
	gtk_widget_get_preferred_height (child, NULL, &scroll_height);
430

431
      gtk_widget_get_preferred_width_for_height (child,
432
                                                 MAX (view_allocation.height, scroll_height),
433
                                                 &minimum_width,
434 435 436
                                                 &natural_width);

      if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
437
	upper = MAX (minimum_width, view_allocation.width);
438
      else
439
	upper = MAX (natural_width, view_allocation.width);
440 441
    }
  else
442
    upper = view_allocation.width;
443

444 445 446
  value = gtk_adjustment_get_value (hadjustment);
  /* We clamp to the left in RTL mode */
  if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
447
    {
448 449 450 451
      gdouble dist = gtk_adjustment_get_upper (hadjustment)
                     - value
                     - gtk_adjustment_get_page_size (hadjustment);
      value = upper - dist - view_allocation.width;
452
    }
453 454 455 456 457 458 459 460

  gtk_adjustment_configure (hadjustment,
                            value,
                            0,
                            upper,
                            view_allocation.width * 0.1,
                            view_allocation.width * 0.9,
                            view_allocation.width);
461 462 463
}

static void
464
viewport_set_vadjustment_values (GtkViewport *viewport)
465 466 467 468
{
  GtkBin *bin = GTK_BIN (viewport);
  GtkAllocation view_allocation;
  GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
Javier Jardón's avatar
Javier Jardón committed
469
  GtkWidget *child;
470
  gdouble upper;
471 472 473

  viewport_get_view_allocation (viewport, &view_allocation);  

Javier Jardón's avatar
Javier Jardón committed
474 475
  child = gtk_bin_get_child (bin);
  if (child && gtk_widget_get_visible (child))
476
    {
477 478 479 480 481 482 483
      gint minimum_height, natural_height;
      gint scroll_width;

      if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
	gtk_widget_get_preferred_width (child, &scroll_width, NULL);
      else
	gtk_widget_get_preferred_width (child, NULL, &scroll_width);
Javier Jardón's avatar
Javier Jardón committed
484

485
      gtk_widget_get_preferred_height_for_width (child,
486
                                                 MAX (view_allocation.width, scroll_width),
487
                                                 &minimum_height,
488
                                                 &natural_height);
489

490
      if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
491
	upper = MAX (minimum_height, view_allocation.height);
492
      else
493
	upper = MAX (natural_height, view_allocation.height);
494 495
    }
  else
496 497 498 499 500 501 502 503 504
    upper = view_allocation.height;

  gtk_adjustment_configure (vadjustment,
                            gtk_adjustment_get_value (vadjustment),
                            0,
                            upper,
                            view_allocation.height * 0.1,
                            view_allocation.height * 0.9,
                            view_allocation.height);
505 506 507 508 509 510 511 512 513
}

static void
viewport_set_adjustment (GtkViewport    *viewport,
			 GtkOrientation  orientation,
			 GtkAdjustment  *adjustment)
{
  GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);

514
  if (adjustment && adjustment == *adjustmentp)
515 516 517
    return;

  if (!adjustment)
Javier Jardón's avatar
Javier Jardón committed
518
    adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
519
  viewport_disconnect_adjustment (viewport, orientation);
520
  *adjustmentp = adjustment;
521
  g_object_ref_sink (adjustment);
522 523

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
524
    viewport_set_hadjustment_values (viewport);
525
  else
526
    viewport_set_vadjustment_values (viewport);
527

528
  g_signal_connect (adjustment, "value-changed",
529 530 531
		    G_CALLBACK (gtk_viewport_adjustment_value_changed),
		    viewport);

532
  gtk_viewport_adjustment_value_changed (adjustment, viewport);
533 534
}

Matthias Clasen's avatar
Matthias Clasen committed
535 536 537
/**
 * gtk_viewport_set_hadjustment:
 * @viewport: a #GtkViewport.
538 539
 * @adjustment: (allow-none): a #GtkAdjustment.
 *
Matthias Clasen's avatar
Matthias Clasen committed
540
 * Sets the horizontal adjustment of the viewport.
Tadej Borovšak's avatar
Tadej Borovšak committed
541 542
 *
 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
Matthias Clasen's avatar
Matthias Clasen committed
543
 **/
Elliot Lee's avatar
Elliot Lee committed
544 545 546 547 548
void
gtk_viewport_set_hadjustment (GtkViewport   *viewport,
			      GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_VIEWPORT (viewport));
549 550
  if (adjustment)
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
Elliot Lee's avatar
Elliot Lee committed
551

552
  viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
553 554

  g_object_notify (G_OBJECT (viewport), "hadjustment");
Elliot Lee's avatar
Elliot Lee committed
555 556
}

Matthias Clasen's avatar
Matthias Clasen committed
557 558 559
/**
 * gtk_viewport_set_vadjustment:
 * @viewport: a #GtkViewport.
560 561
 * @adjustment: (allow-none): a #GtkAdjustment.
 *
Matthias Clasen's avatar
Matthias Clasen committed
562
 * Sets the vertical adjustment of the viewport.
Tadej Borovšak's avatar
Tadej Borovšak committed
563 564
 *
 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
Matthias Clasen's avatar
Matthias Clasen committed
565
 **/
Elliot Lee's avatar
Elliot Lee committed
566 567 568 569 570
void
gtk_viewport_set_vadjustment (GtkViewport   *viewport,
			      GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_VIEWPORT (viewport));
571 572
  if (adjustment)
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
Elliot Lee's avatar
Elliot Lee committed
573

574
  viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
575

576
  g_object_notify (G_OBJECT (viewport), "vadjustment");
Elliot Lee's avatar
Elliot Lee committed
577 578
}

Matthias Clasen's avatar
Matthias Clasen committed
579 580 581 582 583 584 585
/** 
 * gtk_viewport_set_shadow_type:
 * @viewport: a #GtkViewport.
 * @type: the new shadow type.
 *
 * Sets the shadow type of the viewport.
 **/ 
Elliot Lee's avatar
Elliot Lee committed
586 587 588 589
void
gtk_viewport_set_shadow_type (GtkViewport   *viewport,
			      GtkShadowType  type)
{
590
  GtkViewportPrivate *priv;
591 592
  GtkAllocation allocation;
  GtkWidget *widget;
593

Elliot Lee's avatar
Elliot Lee committed
594 595
  g_return_if_fail (GTK_IS_VIEWPORT (viewport));

596
  widget = GTK_WIDGET (viewport);
597 598 599
  priv = viewport->priv;

  if ((GtkShadowType) priv->shadow_type != type)
Elliot Lee's avatar
Elliot Lee committed
600
    {
601
      priv->shadow_type = type;
Elliot Lee's avatar
Elliot Lee committed
602

603
      if (gtk_widget_get_visible (widget))
Elliot Lee's avatar
Elliot Lee committed
604
	{
605 606 607 608
          gtk_widget_get_allocation (widget, &allocation);
          gtk_widget_size_allocate (widget, &allocation);
          gtk_widget_set_allocation (widget, &allocation);
          gtk_widget_queue_draw (widget);
Elliot Lee's avatar
Elliot Lee committed
609
	}
Alexander Larsson's avatar
Alexander Larsson committed
610

611
      g_object_notify (G_OBJECT (viewport), "shadow-type");
612
    }
Elliot Lee's avatar
Elliot Lee committed
613 614
}

615 616 617 618 619 620
/**
 * gtk_viewport_get_shadow_type:
 * @viewport: a #GtkViewport
 *
 * Gets the shadow type of the #GtkViewport. See
 * gtk_viewport_set_shadow_type().
621
 *
622 623 624 625 626 627 628
 * Return value: the shadow type 
 **/
GtkShadowType
gtk_viewport_get_shadow_type (GtkViewport *viewport)
{
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);

629
  return viewport->priv->shadow_type;
630
}
Elliot Lee's avatar
Elliot Lee committed
631

632 633 634 635 636 637
/**
 * gtk_viewport_get_bin_window:
 * @viewport: a #GtkViewport
 *
 * Gets the bin window of the #GtkViewport.
 *
Matthias Clasen's avatar
Matthias Clasen committed
638
 * Return value: (transfer none): a #GdkWindow
639 640 641 642 643 644 645 646
 *
 * Since: 2.20
 **/
GdkWindow*
gtk_viewport_get_bin_window (GtkViewport *viewport)
{
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);

647
  return viewport->priv->bin_window;
648 649
}

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
/**
 * gtk_viewport_get_view_window:
 * @viewport: a #GtkViewport
 *
 * Gets the view window of the #GtkViewport.
 *
 * Return value: (transfer none): a #GdkWindow
 *
 * Since: 2.22
 **/
GdkWindow*
gtk_viewport_get_view_window (GtkViewport *viewport)
{
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);

665
  return viewport->priv->view_window;
666 667
}

Elliot Lee's avatar
Elliot Lee committed
668 669 670
static void
gtk_viewport_realize (GtkWidget *widget)
{
671
  GtkViewport *viewport = GTK_VIEWPORT (widget);
672
  GtkViewportPrivate *priv = viewport->priv;
673 674 675
  GtkBin *bin = GTK_BIN (widget);
  GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
  GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
676
  GtkAllocation allocation;
677
  GtkAllocation view_allocation;
678
  GtkStyleContext *context;
Javier Jardón's avatar
Javier Jardón committed
679
  GtkWidget *child;
680
  GdkWindow *window;
Elliot Lee's avatar
Elliot Lee committed
681 682
  GdkWindowAttr attributes;
  gint attributes_mask;
683
  gint event_mask;
Javier Jardón's avatar
Javier Jardón committed
684
  guint border_width;
Elliot Lee's avatar
Elliot Lee committed
685

686 687
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

688
  gtk_widget_set_realized (widget, TRUE);
Elliot Lee's avatar
Elliot Lee committed
689

690 691 692 693 694 695
  gtk_widget_get_allocation (widget, &allocation);

  attributes.x = allocation.x + border_width;
  attributes.y = allocation.y + border_width;
  attributes.width = allocation.width - border_width * 2;
  attributes.height = allocation.height - border_width * 2;
Elliot Lee's avatar
Elliot Lee committed
696 697 698
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
699 700

  event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
701 702 703
  /* We select on button_press_mask so that button 4-5 scrolls are trapped.
   */
  attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
Elliot Lee's avatar
Elliot Lee committed
704

705
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
Elliot Lee's avatar
Elliot Lee committed
706

707 708 709 710
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
  gdk_window_set_user_data (window, viewport);
Elliot Lee's avatar
Elliot Lee committed
711

712 713 714 715 716 717
  viewport_get_view_allocation (viewport, &view_allocation);
  
  attributes.x = view_allocation.x;
  attributes.y = view_allocation.y;
  attributes.width = view_allocation.width;
  attributes.height = view_allocation.height;
718
  attributes.event_mask = 0;
Elliot Lee's avatar
Elliot Lee committed
719

720 721
  priv->view_window = gdk_window_new (window,
                                      &attributes, attributes_mask);
722
  gdk_window_set_user_data (priv->view_window, viewport);
Elliot Lee's avatar
Elliot Lee committed
723

724 725 726 727
  attributes.x = - gtk_adjustment_get_value (hadjustment);
  attributes.y = - gtk_adjustment_get_value (vadjustment);
  attributes.width = gtk_adjustment_get_upper (hadjustment);
  attributes.height = gtk_adjustment_get_upper (vadjustment);
728
  
729
  attributes.event_mask = event_mask;
Elliot Lee's avatar
Elliot Lee committed
730

731 732
  priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
  gdk_window_set_user_data (priv->bin_window, viewport);
733

Javier Jardón's avatar
Javier Jardón committed
734 735
  child = gtk_bin_get_child (bin);
  if (child)
736
    gtk_widget_set_parent_window (child, priv->bin_window);
Elliot Lee's avatar
Elliot Lee committed
737

738 739 740
  context = gtk_widget_get_style_context (widget);
  gtk_style_context_set_background (context, window);
  gtk_style_context_set_background (context, priv->bin_window);
741

742 743
  gdk_window_show (priv->bin_window);
  gdk_window_show (priv->view_window);
Elliot Lee's avatar
Elliot Lee committed
744 745 746 747 748
}

static void
gtk_viewport_unrealize (GtkWidget *widget)
{
749
  GtkViewport *viewport = GTK_VIEWPORT (widget);
750
  GtkViewportPrivate *priv = viewport->priv;
Elliot Lee's avatar
Elliot Lee committed
751

752 753 754
  gdk_window_set_user_data (priv->view_window, NULL);
  gdk_window_destroy (priv->view_window);
  priv->view_window = NULL;
755

756 757 758
  gdk_window_set_user_data (priv->bin_window, NULL);
  gdk_window_destroy (priv->bin_window);
  priv->bin_window = NULL;
759

760
  GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
Elliot Lee's avatar
Elliot Lee committed
761 762 763
}

static gint
Benjamin Otte's avatar
Benjamin Otte committed
764 765
gtk_viewport_draw (GtkWidget *widget,
                   cairo_t   *cr)
Elliot Lee's avatar
Elliot Lee committed
766
{
767 768
  GtkViewport *viewport = GTK_VIEWPORT (widget);
  GtkViewportPrivate *priv = viewport->priv;
769
  GtkStyleContext *context;
770
  int x, y;
771

772 773
  context = gtk_widget_get_style_context (widget);

774
  if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
Elliot Lee's avatar
Elliot Lee committed
775
    {
776 777 778 779
      gtk_style_context_save (context);
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);

      gtk_render_frame (context, cr, 0, 0,
780 781
                        gdk_window_get_width (gtk_widget_get_window (widget)),
                        gdk_window_get_height (gtk_widget_get_window (widget)));
782 783

      gtk_style_context_restore (context);
784 785
    }
  
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
  if (gtk_cairo_should_draw_window (cr, priv->view_window))
    {
      /* This is a cute hack to ensure the contents of bin_window are
       * restricted to where they are visible. We only need to do this
       * clipping when called via gtk_widget_draw() and not in expose
       * events. And when that happens every window (including this one)
       * should be drawn.
       */
      gdk_window_get_position (priv->view_window, &x, &y);
      cairo_rectangle (cr, x, y, 
                       gdk_window_get_width (priv->view_window),
                       gdk_window_get_height (priv->view_window));
      cairo_clip (cr);
    }

801 802 803
  if (gtk_cairo_should_draw_window (cr, priv->bin_window))
    {
      gdk_window_get_position (priv->bin_window, &x, &y);
804 805 806
      gtk_render_background (context, cr, x, y,
                             gdk_window_get_width (priv->bin_window),
                             gdk_window_get_height (priv->bin_window));
807 808

      GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
Elliot Lee's avatar
Elliot Lee committed
809 810 811 812 813
    }

  return FALSE;
}

814 815
static void
gtk_viewport_add (GtkContainer *container,
816
		  GtkWidget    *child)
817
{
818
  GtkBin *bin = GTK_BIN (container);
819
  GtkViewport *viewport = GTK_VIEWPORT (bin);
820
  GtkViewportPrivate *priv = viewport->priv;
821

Javier Jardón's avatar
Javier Jardón committed
822
  g_return_if_fail (gtk_bin_get_child (bin) == NULL);
823

824
  gtk_widget_set_parent_window (child, priv->bin_window);
825

Matthias Clasen's avatar
Matthias Clasen committed
826
  GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
827 828
}

Elliot Lee's avatar
Elliot Lee committed
829 830 831 832
static void
gtk_viewport_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
{
833
  GtkAllocation widget_allocation;
834
  GtkViewport *viewport = GTK_VIEWPORT (widget);
835
  GtkViewportPrivate *priv = viewport->priv;
836
  GtkBin *bin = GTK_BIN (widget);
837
  guint border_width;
838 839
  GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
  GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
840
  GtkAllocation child_allocation;
Javier Jardón's avatar
Javier Jardón committed
841
  GtkWidget *child;
842

843 844
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

845 846 847
  /* If our size changed, and we have a shadow, queue a redraw on widget->window to
   * redraw the shadow correctly.
   */
848
  gtk_widget_get_allocation (widget, &widget_allocation);
849
  if (gtk_widget_get_mapped (widget) &&
850
      priv->shadow_type != GTK_SHADOW_NONE &&
851 852 853 854 855 856
      (widget_allocation.width != allocation->width ||
       widget_allocation.height != allocation->height))
    gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);

  gtk_widget_set_allocation (widget, allocation);

857 858 859 860 861
  g_object_freeze_notify (G_OBJECT (hadjustment));
  g_object_freeze_notify (G_OBJECT (vadjustment));

  viewport_set_hadjustment_values (viewport);
  viewport_set_vadjustment_values (viewport);
862 863 864
  
  child_allocation.x = 0;
  child_allocation.y = 0;
865 866
  child_allocation.width = gtk_adjustment_get_upper (hadjustment);
  child_allocation.height = gtk_adjustment_get_upper (vadjustment);
867
  if (gtk_widget_get_realized (widget))
Elliot Lee's avatar
Elliot Lee committed
868
    {
869
      GtkAllocation view_allocation;
870 871

      gdk_window_move_resize (gtk_widget_get_window (widget),
Owen Taylor's avatar
Owen Taylor committed
872 873 874 875
			      allocation->x + border_width,
			      allocation->y + border_width,
			      allocation->width - border_width * 2,
			      allocation->height - border_width * 2);
876 877
      
      viewport_get_view_allocation (viewport, &view_allocation);
878
      gdk_window_move_resize (priv->view_window,
879 880 881 882
			      view_allocation.x,
			      view_allocation.y,
			      view_allocation.width,
			      view_allocation.height);
883
      gdk_window_move_resize (priv->bin_window,
884 885
                              - gtk_adjustment_get_value (hadjustment),
                              - gtk_adjustment_get_value (vadjustment),
886 887
                              child_allocation.width,
                              child_allocation.height);
Elliot Lee's avatar
Elliot Lee committed
888
    }
Javier Jardón's avatar
Javier Jardón committed
889 890 891 892

  child = gtk_bin_get_child (bin);
  if (child && gtk_widget_get_visible (child))
    gtk_widget_size_allocate (child, &child_allocation);
Elliot Lee's avatar
Elliot Lee committed
893

894 895
  g_object_thaw_notify (G_OBJECT (hadjustment));
  g_object_thaw_notify (G_OBJECT (vadjustment));
Elliot Lee's avatar
Elliot Lee committed
896 897 898 899 900 901
}

static void
gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
				       gpointer       data)
{
902
  GtkViewport *viewport = GTK_VIEWPORT (data);
903
  GtkViewportPrivate *priv = viewport->priv;
904
  GtkBin *bin = GTK_BIN (data);
Javier Jardón's avatar
Javier Jardón committed
905
  GtkWidget *child;
Elliot Lee's avatar
Elliot Lee committed
906

Javier Jardón's avatar
Javier Jardón committed
907 908
  child = gtk_bin_get_child (bin);
  if (child && gtk_widget_get_visible (child) &&
909
      gtk_widget_get_realized (GTK_WIDGET (viewport)))
Elliot Lee's avatar
Elliot Lee committed
910
    {
911 912 913 914
      GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
      GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
      gint old_x, old_y;
      gint new_x, new_y;
Elliot Lee's avatar
Elliot Lee committed
915

916
      gdk_window_get_position (priv->bin_window, &old_x, &old_y);
917 918
      new_x = - gtk_adjustment_get_value (hadjustment);
      new_y = - gtk_adjustment_get_value (vadjustment);
Elliot Lee's avatar
Elliot Lee committed
919

920
      if (new_x != old_x || new_y != old_y)
921
	{
922 923
	  gdk_window_move (priv->bin_window, new_x, new_y);
	  gdk_window_process_updates (priv->bin_window, TRUE);
924
	}
Elliot Lee's avatar
Elliot Lee committed
925 926
    }
}
927 928

static void
929
gtk_viewport_style_updated (GtkWidget *widget)
930
{
931 932
   GTK_WIDGET_CLASS (gtk_viewport_parent_class)->style_updated (widget);

933
   if (gtk_widget_get_realized (widget) &&
934
       gtk_widget_get_has_window (widget))
935
     {
936
        GtkStyleContext *context;
937
        GtkViewport *viewport = GTK_VIEWPORT (widget);
938
        GtkViewportPrivate *priv = viewport->priv;
939

940 941 942
        context = gtk_widget_get_style_context (widget);
        gtk_style_context_set_background (context, priv->bin_window);
        gtk_style_context_set_background (context, gtk_widget_get_window (widget));
943 944
     }
}
945

946 947

static void
948 949 950 951
gtk_viewport_get_preferred_size (GtkWidget      *widget,
                                 GtkOrientation  orientation,
                                 gint           *minimum_size,
                                 gint           *natural_size)
952
{
953
  GtkViewport *viewport = GTK_VIEWPORT (widget);
954
  GtkViewportPrivate *priv = viewport->priv;
955 956 957
  GtkStyleContext *context;
  GtkStateFlags state;
  GtkBorder padding, border;
958 959 960
  GtkWidget *child;
  gint       child_min, child_nat;
  gint       minimum, natural;
961

962
  child = gtk_bin_get_child (GTK_BIN (widget));
963

964 965 966
  /* XXX This should probably be (border_width * 2); but GTK+ has
   * been doing this with a single border for a while now...
   */
967
  minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
968

969 970 971 972
  context = gtk_widget_get_style_context (GTK_WIDGET (widget));
  state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
  gtk_style_context_get_padding (context, state, &padding);

973
  if (priv->shadow_type != GTK_SHADOW_NONE)
974
    {
975 976
      gtk_style_context_get_border (context, state, &border);

977
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
978
        minimum += border.left + border.right;
979
      else
980
        minimum += border.top + border.bottom;
981 982
    }

983 984 985 986 987
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    minimum += padding.left + padding.right;
  else
    minimum += padding.top + padding.bottom;

988 989
  natural = minimum;

990 991
  if (child && gtk_widget_get_visible (child))
    {
992
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
993
	gtk_widget_get_preferred_width (child, &child_min, &child_nat);
994
      else
995
	gtk_widget_get_preferred_height (child, &child_min, &child_nat);
996

997 998
      minimum += child_min;
      natural += child_nat;
999
    }
1000 1001 1002 1003 1004 1005

  if (minimum_size)
    *minimum_size = minimum;

  if (natural_size)
    *natural_size = natural;
1006 1007 1008
}

static void
1009 1010 1011
gtk_viewport_get_preferred_width (GtkWidget *widget,
                                  gint      *minimum_size,
                                  gint      *natural_size)
1012
{
1013
  gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1014 1015 1016
}

static void
1017 1018 1019
gtk_viewport_get_preferred_height (GtkWidget *widget,
                                   gint      *minimum_size,
                                   gint      *natural_size)
1020
{
1021
  gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1022
}