gtkviewport.c 26.3 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * 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>
Elliot Lee's avatar
Elliot Lee committed
28
#include "gtkviewport.h"
Alexander Larsson's avatar
Alexander Larsson committed
29
#include "gtkintl.h"
30
#include "gtkmarshalers.h"
31
#include "gtkprivate.h"
32
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
33

34
enum {
Alexander Larsson's avatar
Alexander Larsson committed
35 36 37 38
  PROP_0,
  PROP_HADJUSTMENT,
  PROP_VADJUSTMENT,
  PROP_SHADOW_TYPE
39 40 41
};


Elliot Lee's avatar
Elliot Lee committed
42 43
static void gtk_viewport_class_init               (GtkViewportClass *klass);
static void gtk_viewport_init                     (GtkViewport      *viewport);
44
static void gtk_viewport_finalize                 (GObject          *object);
45
static void gtk_viewport_destroy                  (GtkObject        *object);
Alexander Larsson's avatar
Alexander Larsson committed
46 47 48 49 50 51 52 53
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);
54
static void gtk_viewport_set_scroll_adjustments	  (GtkViewport	    *viewport,
55 56
						   GtkAdjustment    *hadjustment,
						   GtkAdjustment    *vadjustment);
Elliot Lee's avatar
Elliot Lee committed
57 58 59 60 61 62
static void gtk_viewport_realize                  (GtkWidget        *widget);
static void gtk_viewport_unrealize                (GtkWidget        *widget);
static void gtk_viewport_paint                    (GtkWidget        *widget,
						   GdkRectangle     *area);
static gint gtk_viewport_expose                   (GtkWidget        *widget,
						   GdkEventExpose   *event);
63 64
static void gtk_viewport_add                      (GtkContainer     *container,
						   GtkWidget        *widget);
Elliot Lee's avatar
Elliot Lee committed
65 66 67 68 69 70
static void gtk_viewport_size_request             (GtkWidget        *widget,
						   GtkRequisition   *requisition);
static void gtk_viewport_size_allocate            (GtkWidget        *widget,
						   GtkAllocation    *allocation);
static void gtk_viewport_adjustment_value_changed (GtkAdjustment    *adjustment,
						   gpointer          data);
71 72
static void gtk_viewport_style_set                (GtkWidget *widget,
			                           GtkStyle  *previous_style);
Elliot Lee's avatar
Elliot Lee committed
73

74 75
static GtkBinClass *parent_class;

Manish Singh's avatar
Manish Singh committed
76
GType
77
gtk_viewport_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
78
{
Manish Singh's avatar
Manish Singh committed
79
  static GType viewport_type = 0;
Elliot Lee's avatar
Elliot Lee committed
80 81 82

  if (!viewport_type)
    {
Manish Singh's avatar
Manish Singh committed
83
      static const GTypeInfo viewport_info =
Elliot Lee's avatar
Elliot Lee committed
84 85
      {
	sizeof (GtkViewportClass),
Manish Singh's avatar
Manish Singh committed
86 87 88 89 90 91 92 93
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_viewport_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkViewport),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_viewport_init,
Elliot Lee's avatar
Elliot Lee committed
94 95
      };

96
      viewport_type = g_type_register_static (GTK_TYPE_BIN, I_("GtkViewport"),
Manish Singh's avatar
Manish Singh committed
97
					      &viewport_info, 0);
Elliot Lee's avatar
Elliot Lee committed
98 99 100 101 102 103 104 105
    }

  return viewport_type;
}

static void
gtk_viewport_class_init (GtkViewportClass *class)
{
106
  GtkObjectClass *object_class;
Alexander Larsson's avatar
Alexander Larsson committed
107
  GObjectClass   *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
108 109 110
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

111
  object_class = (GtkObjectClass*) class;
Alexander Larsson's avatar
Alexander Larsson committed
112
  gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
113 114
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;
Manish Singh's avatar
Manish Singh committed
115 116

  parent_class = g_type_class_peek_parent (class);
117

118
  gobject_class->finalize = gtk_viewport_finalize;
Alexander Larsson's avatar
Alexander Larsson committed
119 120
  gobject_class->set_property = gtk_viewport_set_property;
  gobject_class->get_property = gtk_viewport_get_property;
121
  object_class->destroy = gtk_viewport_destroy;
122
  
Elliot Lee's avatar
Elliot Lee committed
123 124 125 126 127
  widget_class->realize = gtk_viewport_realize;
  widget_class->unrealize = gtk_viewport_unrealize;
  widget_class->expose_event = gtk_viewport_expose;
  widget_class->size_request = gtk_viewport_size_request;
  widget_class->size_allocate = gtk_viewport_size_allocate;
128
  widget_class->style_set = gtk_viewport_style_set;
129 130 131 132 133
  
  container_class->add = gtk_viewport_add;

  class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;

Alexander Larsson's avatar
Alexander Larsson committed
134 135 136
  g_object_class_install_property (gobject_class,
                                   PROP_HADJUSTMENT,
                                   g_param_spec_object ("hadjustment",
137 138
							P_("Horizontal adjustment"),
							P_("The GtkAdjustment that determines the values of the horizontal position for this viewport"),
Alexander Larsson's avatar
Alexander Larsson committed
139
                                                        GTK_TYPE_ADJUSTMENT,
140
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
Alexander Larsson's avatar
Alexander Larsson committed
141 142 143 144

  g_object_class_install_property (gobject_class,
                                   PROP_VADJUSTMENT,
                                   g_param_spec_object ("vadjustment",
145 146
							P_("Vertical adjustment"),
							P_("The GtkAdjustment that determines the values of the vertical position for this viewport"),
Alexander Larsson's avatar
Alexander Larsson committed
147
                                                        GTK_TYPE_ADJUSTMENT,
148
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
Alexander Larsson's avatar
Alexander Larsson committed
149 150 151

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

159
  widget_class->set_scroll_adjustments_signal =
160
    g_signal_new (I_("set_scroll_adjustments"),
Manish Singh's avatar
Manish Singh committed
161 162 163 164 165 166 167 168
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkViewportClass, set_scroll_adjustments),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT_OBJECT,
		  G_TYPE_NONE, 2,
		  GTK_TYPE_ADJUSTMENT,
		  GTK_TYPE_ADJUSTMENT);
Elliot Lee's avatar
Elliot Lee committed
169 170
}

171
static void
Alexander Larsson's avatar
Alexander Larsson committed
172 173 174 175
gtk_viewport_set_property (GObject         *object,
			   guint            prop_id,
			   const GValue    *value,
			   GParamSpec      *pspec)
176 177 178 179 180
{
  GtkViewport *viewport;

  viewport = GTK_VIEWPORT (object);

Alexander Larsson's avatar
Alexander Larsson committed
181
  switch (prop_id)
182
    {
Alexander Larsson's avatar
Alexander Larsson committed
183 184
    case PROP_HADJUSTMENT:
      gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
185
      break;
Alexander Larsson's avatar
Alexander Larsson committed
186 187
    case PROP_VADJUSTMENT:
      gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
188
      break;
Alexander Larsson's avatar
Alexander Larsson committed
189 190
    case PROP_SHADOW_TYPE:
      gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
191
      break;
192
    default:
Alexander Larsson's avatar
Alexander Larsson committed
193
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 195 196 197 198
      break;
    }
}

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

  viewport = GTK_VIEWPORT (object);

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, viewport->hadjustment);
212
      break;
Alexander Larsson's avatar
Alexander Larsson committed
213
    case PROP_VADJUSTMENT:
214
      g_value_set_object (value, viewport->vadjustment);
215
      break;
Alexander Larsson's avatar
Alexander Larsson committed
216 217
    case PROP_SHADOW_TYPE:
      g_value_set_enum (value, viewport->shadow_type);
218 219
      break;
    default:
Alexander Larsson's avatar
Alexander Larsson committed
220
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 222 223 224
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
225 226 227 228 229
static void
gtk_viewport_init (GtkViewport *viewport)
{
  GTK_WIDGET_UNSET_FLAGS (viewport, GTK_NO_WINDOW);

230
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
231 232
  gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
  
Elliot Lee's avatar
Elliot Lee committed
233 234
  viewport->shadow_type = GTK_SHADOW_IN;
  viewport->view_window = NULL;
235
  viewport->bin_window = NULL;
Elliot Lee's avatar
Elliot Lee committed
236 237 238 239
  viewport->hadjustment = NULL;
  viewport->vadjustment = NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
240 241 242 243 244 245 246 247 248
/**
 * gtk_viewport_new:
 * @hadjustment: horizontal adjustment.
 * @vadjustment: vertical adjustment.
 * @returns: a new #GtkViewport.
 *
 * Creates a new #GtkViewport with the given adjustments.
 *
 **/
Elliot Lee's avatar
Elliot Lee committed
249 250 251 252
GtkWidget*
gtk_viewport_new (GtkAdjustment *hadjustment,
		  GtkAdjustment *vadjustment)
{
253 254 255 256 257 258 259 260 261
  GtkWidget *viewport;

  viewport = gtk_widget_new (GTK_TYPE_VIEWPORT,
			     "hadjustment", hadjustment,
			     "vadjustment", vadjustment,
			     NULL);

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

263 264 265 266
#define ADJUSTMENT_POINTER(viewport, orientation)         \
  (((orientation) == GTK_ORIENTATION_HORIZONTAL) ?        \
     &(viewport)->hadjustment : &(viewport)->vadjustment)

267
static void
268 269
viewport_disconnect_adjustment (GtkViewport    *viewport,
				GtkOrientation  orientation)
270
{
271
  GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
Elliot Lee's avatar
Elliot Lee committed
272

273
  if (*adjustmentp)
274
    {
275
      g_signal_handlers_disconnect_by_func (*adjustmentp,
Manish Singh's avatar
Manish Singh committed
276 277
					    gtk_viewport_adjustment_value_changed,
					    viewport);
278 279
      g_object_unref (*adjustmentp);
      *adjustmentp = NULL;
280
    }
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
}

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);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gtk_viewport_destroy (GtkObject *object)
{
  GtkViewport *viewport = GTK_VIEWPORT (object);

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

302
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
303 304
}

Matthias Clasen's avatar
Matthias Clasen committed
305 306 307 308 309 310 311 312
/**
 * gtk_viewport_get_hadjustment:
 * @viewport: a #GtkViewport.
 * 
 * Returns the horizontal adjustment of the viewport.
 *
 * Return value: the horizontal adjustment of @viewport.
 **/
Elliot Lee's avatar
Elliot Lee committed
313 314 315 316 317
GtkAdjustment*
gtk_viewport_get_hadjustment (GtkViewport *viewport)
{
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);

318 319 320
  if (!viewport->hadjustment)
    gtk_viewport_set_hadjustment (viewport, NULL);

Elliot Lee's avatar
Elliot Lee committed
321 322 323
  return viewport->hadjustment;
}

Matthias Clasen's avatar
Matthias Clasen committed
324 325 326 327 328 329 330 331
/**
 * gtk_viewport_get_vadjustment:
 * @viewport: a #GtkViewport.
 * 
 * Returns the vertical adjustment of the viewport.
 *
 * Return value: the vertical adjustment of @viewport.
 **/
Elliot Lee's avatar
Elliot Lee committed
332 333 334 335 336
GtkAdjustment*
gtk_viewport_get_vadjustment (GtkViewport *viewport)
{
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);

337 338 339
  if (!viewport->vadjustment)
    gtk_viewport_set_vadjustment (viewport, NULL);

Elliot Lee's avatar
Elliot Lee committed
340 341 342
  return viewport->vadjustment;
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
static void
viewport_get_view_allocation (GtkViewport   *viewport,
			      GtkAllocation *view_allocation)
{
  GtkWidget *widget = GTK_WIDGET (viewport);
  GtkAllocation *allocation = &widget->allocation;
  gint border_width = GTK_CONTAINER (viewport)->border_width;
  
  view_allocation->x = 0;
  view_allocation->y = 0;

  if (viewport->shadow_type != GTK_SHADOW_NONE)
    {
      view_allocation->x = widget->style->xthickness;
      view_allocation->y = widget->style->ythickness;
    }

  view_allocation->width = MAX (1, allocation->width - view_allocation->x * 2 - border_width * 2);
  view_allocation->height = MAX (1, allocation->height - view_allocation->y * 2 - border_width * 2);
}

static void
viewport_reclamp_adjustment (GtkAdjustment *adjustment,
			     gboolean      *value_changed)
{
  gdouble value = adjustment->value;
  
  value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
  if (value != adjustment->value)
    {
      adjustment->value = value;
      if (value_changed)
	*value_changed = TRUE;
    }
  else if (value_changed)
    *value_changed = FALSE;
}

static void
viewport_set_hadjustment_values (GtkViewport *viewport,
				 gboolean    *value_changed)
{
  GtkBin *bin = GTK_BIN (viewport);
  GtkAllocation view_allocation;
  GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
388
  gdouble old_page_size;
389 390
  gdouble old_upper;
  gdouble old_value;
391
  
392 393
  viewport_get_view_allocation (viewport, &view_allocation);  

394
  old_page_size = hadjustment->page_size;
395 396
  old_upper = hadjustment->upper;
  old_value = hadjustment->value;
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  hadjustment->page_size = view_allocation.width;
  hadjustment->step_increment = view_allocation.width * 0.1;
  hadjustment->page_increment = view_allocation.width * 0.9;
  
  hadjustment->lower = 0;

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
      GtkRequisition child_requisition;
      
      gtk_widget_get_child_requisition (bin->child, &child_requisition);
      hadjustment->upper = MAX (child_requisition.width, view_allocation.width);
    }
  else
    hadjustment->upper = view_allocation.width;

413 414
  if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL) 
    {
415 416
      gdouble dist = old_upper - (old_value + old_page_size);
      hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
417 418 419 420 421
      viewport_reclamp_adjustment (hadjustment, value_changed);
      *value_changed = (old_value != hadjustment->value);
    }
  else
    viewport_reclamp_adjustment (hadjustment, value_changed);
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
}

static void
viewport_set_vadjustment_values (GtkViewport *viewport,
				 gboolean    *value_changed)
{
  GtkBin *bin = GTK_BIN (viewport);
  GtkAllocation view_allocation;
  GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);

  viewport_get_view_allocation (viewport, &view_allocation);  

  vadjustment->page_size = view_allocation.height;
  vadjustment->step_increment = view_allocation.height * 0.1;
  vadjustment->page_increment = view_allocation.height * 0.9;
  
  vadjustment->lower = 0;

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
      GtkRequisition child_requisition;
      
      gtk_widget_get_child_requisition (bin->child, &child_requisition);
      vadjustment->upper = MAX (child_requisition.height, view_allocation.height);
    }
  else
    vadjustment->upper = view_allocation.height;

  viewport_reclamp_adjustment (vadjustment, value_changed);
}

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

461
  if (adjustment && adjustment == *adjustmentp)
462 463 464 465 466
    return;

  if (!adjustment)
    adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
						     0.0, 0.0, 0.0));
467 468
  if (*adjustmentp)
    g_object_unref (*adjustmentp);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  *adjustmentp = adjustment;
  g_object_ref (adjustment);
  gtk_object_sink (GTK_OBJECT (adjustment));

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    viewport_set_hadjustment_values (viewport, &value_changed);
  else
    viewport_set_vadjustment_values (viewport, &value_changed);

  g_signal_connect (adjustment, "value_changed",
		    G_CALLBACK (gtk_viewport_adjustment_value_changed),
		    viewport);

  gtk_adjustment_changed (adjustment);
  
  if (value_changed)
    gtk_adjustment_value_changed (adjustment);
  else
    gtk_viewport_adjustment_value_changed (adjustment, viewport);
}

Matthias Clasen's avatar
Matthias Clasen committed
490 491 492 493 494 495 496
/**
 * gtk_viewport_set_hadjustment:
 * @viewport: a #GtkViewport.
 * @adjustment: a #GtkAdjustment.
 * 
 * Sets the horizontal adjustment of the viewport.
 **/
Elliot Lee's avatar
Elliot Lee committed
497 498 499 500 501
void
gtk_viewport_set_hadjustment (GtkViewport   *viewport,
			      GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_VIEWPORT (viewport));
502 503
  if (adjustment)
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
Elliot Lee's avatar
Elliot Lee committed
504

505
  viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
506 507

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

Matthias Clasen's avatar
Matthias Clasen committed
510 511 512 513 514 515 516
/**
 * gtk_viewport_set_vadjustment:
 * @viewport: a #GtkViewport.
 * @adjustment: a #GtkAdjustment.
 * 
 * Sets the vertical adjustment of the viewport.
 **/
Elliot Lee's avatar
Elliot Lee committed
517 518 519 520 521
void
gtk_viewport_set_vadjustment (GtkViewport   *viewport,
			      GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_VIEWPORT (viewport));
522 523
  if (adjustment)
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
Elliot Lee's avatar
Elliot Lee committed
524

525
  viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
526

527
  g_object_notify (G_OBJECT (viewport), "vadjustment");
Elliot Lee's avatar
Elliot Lee committed
528 529
}

530
static void
531 532 533
gtk_viewport_set_scroll_adjustments (GtkViewport      *viewport,
				     GtkAdjustment    *hadjustment,
				     GtkAdjustment    *vadjustment)
534
{
535 536
  gtk_viewport_set_hadjustment (viewport, hadjustment);
  gtk_viewport_set_vadjustment (viewport, vadjustment);
537 538
}

Matthias Clasen's avatar
Matthias Clasen committed
539 540 541 542 543 544 545
/** 
 * 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
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
void
gtk_viewport_set_shadow_type (GtkViewport   *viewport,
			      GtkShadowType  type)
{
  g_return_if_fail (GTK_IS_VIEWPORT (viewport));

  if ((GtkShadowType) viewport->shadow_type != type)
    {
      viewport->shadow_type = type;

      if (GTK_WIDGET_VISIBLE (viewport))
	{
	  gtk_widget_size_allocate (GTK_WIDGET (viewport), &(GTK_WIDGET (viewport)->allocation));
	  gtk_widget_queue_draw (GTK_WIDGET (viewport));
	}
Alexander Larsson's avatar
Alexander Larsson committed
561

562
      g_object_notify (G_OBJECT (viewport), "shadow-type");
563
    }
Elliot Lee's avatar
Elliot Lee committed
564 565
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
/**
 * gtk_viewport_get_shadow_type:
 * @viewport: a #GtkViewport
 *
 * Gets the shadow type of the #GtkViewport. See
 * gtk_viewport_set_shadow_type().
 
 * 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);

  return viewport->shadow_type;
}
Elliot Lee's avatar
Elliot Lee committed
582 583 584 585

static void
gtk_viewport_realize (GtkWidget *widget)
{
586 587 588 589 590 591 592
  GtkViewport *viewport = GTK_VIEWPORT (widget);
  GtkBin *bin = GTK_BIN (widget);
  GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
  GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
  gint border_width = GTK_CONTAINER (widget)->border_width;
  
  GtkAllocation view_allocation;
Elliot Lee's avatar
Elliot Lee committed
593 594
  GdkWindowAttr attributes;
  gint attributes_mask;
595
  gint event_mask;
Elliot Lee's avatar
Elliot Lee committed
596 597 598

  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

599 600 601 602
  attributes.x = widget->allocation.x + border_width;
  attributes.y = widget->allocation.y + border_width;
  attributes.width = widget->allocation.width - border_width * 2;
  attributes.height = widget->allocation.height - border_width * 2;
Elliot Lee's avatar
Elliot Lee committed
603 604 605 606
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
607 608

  event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
609 610 611
  /* 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
612 613 614

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

615 616 617
  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
				   &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, viewport);
Elliot Lee's avatar
Elliot Lee committed
618

619 620 621 622 623 624
  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;
625
  attributes.event_mask = 0;
Elliot Lee's avatar
Elliot Lee committed
626

627
  viewport->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
Elliot Lee's avatar
Elliot Lee committed
628 629
  gdk_window_set_user_data (viewport->view_window, viewport);

630 631
  gdk_window_set_back_pixmap (viewport->view_window, NULL, FALSE);
  
632 633 634 635
  attributes.x = - hadjustment->value;
  attributes.y = - vadjustment->value;
  attributes.width = hadjustment->upper;
  attributes.height = vadjustment->upper;
636
  
637
  attributes.event_mask = event_mask;
Elliot Lee's avatar
Elliot Lee committed
638

639 640 641 642
  viewport->bin_window = gdk_window_new (viewport->view_window, &attributes, attributes_mask);
  gdk_window_set_user_data (viewport->bin_window, viewport);

  if (bin->child)
643
    gtk_widget_set_parent_window (bin->child, viewport->bin_window);
Elliot Lee's avatar
Elliot Lee committed
644

645
  widget->style = gtk_style_attach (widget->style, widget->window);
Elliot Lee's avatar
Elliot Lee committed
646
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
647
  gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
648

649 650 651 652 653 654
  /* Call paint here to allow a theme to set the background without flashing
   */
  gtk_paint_flat_box(widget->style, viewport->bin_window, GTK_STATE_NORMAL,
		     GTK_SHADOW_NONE,
		     NULL, widget, "viewportbin",
		     0, 0, -1, -1);
655
   
656
  gdk_window_show (viewport->bin_window);
Elliot Lee's avatar
Elliot Lee committed
657 658 659 660 661 662
  gdk_window_show (viewport->view_window);
}

static void
gtk_viewport_unrealize (GtkWidget *widget)
{
663
  GtkViewport *viewport = GTK_VIEWPORT (widget);
Elliot Lee's avatar
Elliot Lee committed
664

665
  gdk_window_set_user_data (viewport->view_window, NULL);
Elliot Lee's avatar
Elliot Lee committed
666
  gdk_window_destroy (viewport->view_window);
667
  viewport->view_window = NULL;
668

669
  gdk_window_set_user_data (viewport->bin_window, NULL);
670 671
  gdk_window_destroy (viewport->bin_window);
  viewport->bin_window = NULL;
672 673 674

  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
Elliot Lee's avatar
Elliot Lee committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
}

static void
gtk_viewport_paint (GtkWidget    *widget,
		    GdkRectangle *area)
{
  GtkViewport *viewport;

  g_return_if_fail (GTK_IS_VIEWPORT (widget));
  g_return_if_fail (area != NULL);

  if (GTK_WIDGET_DRAWABLE (widget))
    {
      viewport = GTK_VIEWPORT (widget);

Manish Singh's avatar
Manish Singh committed
690 691 692 693
      gtk_paint_shadow (widget->style, widget->window,
			GTK_STATE_NORMAL, viewport->shadow_type,
			NULL, widget, "viewport",
			0, 0, -1, -1);
Elliot Lee's avatar
Elliot Lee committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
    }
}

static gint
gtk_viewport_expose (GtkWidget      *widget,
		     GdkEventExpose *event)
{
  GtkViewport *viewport;
  GtkBin *bin;

  if (GTK_WIDGET_DRAWABLE (widget))
    {
      viewport = GTK_VIEWPORT (widget);
      bin = GTK_BIN (widget);

709
      if (event->window == widget->window)
Elliot Lee's avatar
Elliot Lee committed
710
	gtk_viewport_paint (widget, &event->area);
711 712 713 714 715 716 717
      else if (event->window == viewport->bin_window)
	{
	  gtk_paint_flat_box(widget->style, viewport->bin_window, 
			     GTK_STATE_NORMAL, GTK_SHADOW_NONE,
			     &event->area, widget, "viewportbin",
			     0, 0, -1, -1);
	  
718
	  (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
719
	}
Elliot Lee's avatar
Elliot Lee committed
720 721 722 723 724
    }

  return FALSE;
}

725 726
static void
gtk_viewport_add (GtkContainer *container,
727
		  GtkWidget    *child)
728 729 730
{
  GtkBin *bin;

731
  g_return_if_fail (GTK_IS_WIDGET (child));
732 733

  bin = GTK_BIN (container);
734
  g_return_if_fail (bin->child == NULL);
735

736
  gtk_widget_set_parent_window (child, GTK_VIEWPORT (bin)->bin_window);
737

738
  GTK_CONTAINER_CLASS (parent_class)->add (container, child);
739 740
}

Elliot Lee's avatar
Elliot Lee committed
741 742 743 744 745 746
static void
gtk_viewport_size_request (GtkWidget      *widget,
			   GtkRequisition *requisition)
{
  GtkViewport *viewport;
  GtkBin *bin;
747
  GtkRequisition child_requisition;
Elliot Lee's avatar
Elliot Lee committed
748 749 750 751 752

  viewport = GTK_VIEWPORT (widget);
  bin = GTK_BIN (widget);

  requisition->width = (GTK_CONTAINER (widget)->border_width +
753
			GTK_WIDGET (widget)->style->xthickness) * 2;
Elliot Lee's avatar
Elliot Lee committed
754 755

  requisition->height = (GTK_CONTAINER (widget)->border_width * 2 +
756
			 GTK_WIDGET (widget)->style->ythickness) * 2;
Elliot Lee's avatar
Elliot Lee committed
757 758

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
759
    {
760 761 762
      gtk_widget_size_request (bin->child, &child_requisition);
      requisition->width += child_requisition.width;
      requisition->height += child_requisition.height;
763
    }
Elliot Lee's avatar
Elliot Lee committed
764 765 766 767 768 769
}

static void
gtk_viewport_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
{
770 771 772
  GtkViewport *viewport = GTK_VIEWPORT (widget);
  GtkBin *bin = GTK_BIN (widget);
  gint border_width = GTK_CONTAINER (widget)->border_width;
773
  gboolean hadjustment_value_changed, vadjustment_value_changed;
Elliot Lee's avatar
Elliot Lee committed
774

775 776
  GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
  GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
777

778 779 780 781 782 783 784 785 786 787
  /* If our size changed, and we have a shadow, queue a redraw on widget->window to
   * redraw the shadow correctly.
   */
  if (GTK_WIDGET_MAPPED (widget) &&
      viewport->shadow_type != GTK_SHADOW_NONE &&
      (widget->allocation.width != allocation->width ||
       widget->allocation.height != allocation->height))
    gdk_window_invalidate_rect (widget->window, NULL, FALSE);
  
  widget->allocation = *allocation;
Owen Taylor's avatar
Owen Taylor committed
788

789 790
  viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
  viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
Elliot Lee's avatar
Elliot Lee committed
791 792 793

  if (GTK_WIDGET_REALIZED (widget))
    {
794 795 796 797
      GtkAllocation view_allocation;
      
      viewport_get_view_allocation (viewport, &view_allocation);
  
798
      gdk_window_move_resize (widget->window,
Owen Taylor's avatar
Owen Taylor committed
799 800 801 802
			      allocation->x + border_width,
			      allocation->y + border_width,
			      allocation->width - border_width * 2,
			      allocation->height - border_width * 2);
Elliot Lee's avatar
Elliot Lee committed
803 804

      gdk_window_move_resize (viewport->view_window,
805 806 807 808
			      view_allocation.x,
			      view_allocation.y,
			      view_allocation.width,
			      view_allocation.height);
Elliot Lee's avatar
Elliot Lee committed
809 810 811 812
    }

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
813
      GtkAllocation child_allocation;
814
      
Elliot Lee's avatar
Elliot Lee committed
815 816
      child_allocation.x = 0;
      child_allocation.y = 0;
817 818
      child_allocation.width = hadjustment->upper;
      child_allocation.height = vadjustment->upper;
Elliot Lee's avatar
Elliot Lee committed
819

Owen Taylor's avatar
Owen Taylor committed
820
      if (GTK_WIDGET_REALIZED (widget))
821 822 823 824 825
	gdk_window_move_resize (viewport->bin_window,
				- hadjustment->value,
				- vadjustment->value,
				child_allocation.width,
				child_allocation.height);
Elliot Lee's avatar
Elliot Lee committed
826 827 828 829

      gtk_widget_size_allocate (bin->child, &child_allocation);
    }

830 831 832 833 834 835
  gtk_adjustment_changed (hadjustment);
  gtk_adjustment_changed (vadjustment);
  if (hadjustment_value_changed)
    gtk_adjustment_value_changed (hadjustment);
  if (vadjustment_value_changed)
    gtk_adjustment_value_changed (vadjustment);
Elliot Lee's avatar
Elliot Lee committed
836 837 838 839 840 841 842 843 844
}

static void
gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
				       gpointer       data)
{
  GtkViewport *viewport;
  GtkBin *bin;

845
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
Elliot Lee's avatar
Elliot Lee committed
846 847 848 849 850
  g_return_if_fail (GTK_IS_VIEWPORT (data));

  viewport = GTK_VIEWPORT (data);
  bin = GTK_BIN (data);

851 852
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child)  &&
      GTK_WIDGET_REALIZED (viewport))
Elliot Lee's avatar
Elliot Lee committed
853
    {
854 855 856 857
      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
858

859 860 861
      gdk_window_get_position (viewport->bin_window, &old_x, &old_y);
      new_x = - hadjustment->value;
      new_y = - vadjustment->value;
Elliot Lee's avatar
Elliot Lee committed
862

863
      if (new_x != old_x || new_y != old_y)
864
	{
865
	  gdk_window_move (viewport->bin_window, new_x, new_y);
866 867
	  gdk_window_process_updates (viewport->bin_window, TRUE);
	}
Elliot Lee's avatar
Elliot Lee committed
868 869
    }
}
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

static void
gtk_viewport_style_set (GtkWidget *widget,
			GtkStyle  *previous_style)
{
   GtkViewport *viewport;
   
   if (GTK_WIDGET_REALIZED (widget) &&
       !GTK_WIDGET_NO_WINDOW (widget))
     {
	viewport = GTK_VIEWPORT (widget);
	
	gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
	gtk_style_set_background (widget->style, widget->window, widget->state);
     }
}
886 887 888

#define __GTK_VIEWPORT_C__
#include "gtkaliasdef.c"