gtkviewport.c 24.9 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/. 
 */

Elliot Lee's avatar
Elliot Lee committed
27 28
#include "gtksignal.h"
#include "gtkviewport.h"
Alexander Larsson's avatar
Alexander Larsson committed
29
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
30

31
enum {
Alexander Larsson's avatar
Alexander Larsson committed
32 33 34 35
  PROP_0,
  PROP_HADJUSTMENT,
  PROP_VADJUSTMENT,
  PROP_SHADOW_TYPE
36 37 38
};


Elliot Lee's avatar
Elliot Lee committed
39 40
static void gtk_viewport_class_init               (GtkViewportClass *klass);
static void gtk_viewport_init                     (GtkViewport      *viewport);
41
static void gtk_viewport_destroy                  (GtkObject        *object);
Alexander Larsson's avatar
Alexander Larsson committed
42 43 44 45 46 47 48 49
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);
50
static void gtk_viewport_set_scroll_adjustments	  (GtkViewport	    *viewport,
51 52
						   GtkAdjustment    *hadjustment,
						   GtkAdjustment    *vadjustment);
Elliot Lee's avatar
Elliot Lee committed
53 54 55 56 57 58 59 60
static void gtk_viewport_map                      (GtkWidget        *widget);
static void gtk_viewport_unmap                    (GtkWidget        *widget);
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);
61 62
static void gtk_viewport_add                      (GtkContainer     *container,
						   GtkWidget        *widget);
Elliot Lee's avatar
Elliot Lee committed
63 64 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_changed       (GtkAdjustment    *adjustment,
						   gpointer          data);
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;

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

  if (!viewport_type)
    {
83
      static const GtkTypeInfo viewport_info =
Elliot Lee's avatar
Elliot Lee committed
84 85 86 87 88 89
      {
	"GtkViewport",
	sizeof (GtkViewport),
	sizeof (GtkViewportClass),
	(GtkClassInitFunc) gtk_viewport_class_init,
	(GtkObjectInitFunc) gtk_viewport_init,
90 91
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
92
        (GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
93 94
      };

95
      viewport_type = gtk_type_unique (GTK_TYPE_BIN, &viewport_info);
Elliot Lee's avatar
Elliot Lee committed
96 97 98 99 100 101 102 103
    }

  return viewport_type;
}

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

109
  object_class = (GtkObjectClass*) class;
Alexander Larsson's avatar
Alexander Larsson committed
110
  gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
111 112
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;
113 114
  parent_class = (GtkBinClass*) gtk_type_class (GTK_TYPE_BIN);

Alexander Larsson's avatar
Alexander Larsson committed
115 116
  gobject_class->set_property = gtk_viewport_set_property;
  gobject_class->get_property = gtk_viewport_get_property;
117
  object_class->destroy = gtk_viewport_destroy;
118
  
Elliot Lee's avatar
Elliot Lee committed
119 120 121 122 123 124 125
  widget_class->map = gtk_viewport_map;
  widget_class->unmap = gtk_viewport_unmap;
  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;
126
  widget_class->style_set = gtk_viewport_style_set;
127 128 129 130 131
  
  container_class->add = gtk_viewport_add;

  class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;

Alexander Larsson's avatar
Alexander Larsson committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  g_object_class_install_property (gobject_class,
                                   PROP_HADJUSTMENT,
                                   g_param_spec_object ("hadjustment",
							_("Horizontal adjustment"),
							_("The GtkAdjustment that determines the values of the horizontal position for this viewport."),
                                                        GTK_TYPE_ADJUSTMENT,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
                                   PROP_VADJUSTMENT,
                                   g_param_spec_object ("vadjustment",
							_("Vertical adjustment"),
							_("The GtkAdjustment that determines the values of the vertical position for this viewport."),
                                                        GTK_TYPE_ADJUSTMENT,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
                                   g_param_spec_enum ("shadow_type",
						      _("Shadow type"),
						      _("Determines how the shadowed box around the viewport is drawn."),
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_IN,
						      G_PARAM_READWRITE));
156

157 158
  widget_class->set_scroll_adjustments_signal =
    gtk_signal_new ("set_scroll_adjustments",
159
		    GTK_RUN_LAST,
160
		    GTK_CLASS_TYPE (object_class),
161
		    GTK_SIGNAL_OFFSET (GtkViewportClass, set_scroll_adjustments),
Tim Janik's avatar
Tim Janik committed
162
		    gtk_marshal_VOID__OBJECT_OBJECT,
163
		    GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
Elliot Lee's avatar
Elliot Lee committed
164 165
}

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

  viewport = GTK_VIEWPORT (object);

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

static void
Alexander Larsson's avatar
Alexander Larsson committed
194 195 196 197
gtk_viewport_get_property (GObject         *object,
			   guint            prop_id,
			   GValue          *value,
			   GParamSpec      *pspec)
198 199 200 201 202
{
  GtkViewport *viewport;

  viewport = GTK_VIEWPORT (object);

Alexander Larsson's avatar
Alexander Larsson committed
203
  switch (prop_id)
204
    {
Alexander Larsson's avatar
Alexander Larsson committed
205
    case PROP_HADJUSTMENT:
206
      g_value_set_object (value, G_OBJECT (viewport->hadjustment));
207
      break;
Alexander Larsson's avatar
Alexander Larsson committed
208
    case PROP_VADJUSTMENT:
209
      g_value_set_object (value, G_OBJECT (viewport->vadjustment));
210
      break;
Alexander Larsson's avatar
Alexander Larsson committed
211 212
    case PROP_SHADOW_TYPE:
      g_value_set_enum (value, viewport->shadow_type);
213 214
      break;
    default:
Alexander Larsson's avatar
Alexander Larsson committed
215
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 217 218 219
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
220 221 222 223 224
static void
gtk_viewport_init (GtkViewport *viewport)
{
  GTK_WIDGET_UNSET_FLAGS (viewport, GTK_NO_WINDOW);

225 226
  gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
  
Elliot Lee's avatar
Elliot Lee committed
227 228
  viewport->shadow_type = GTK_SHADOW_IN;
  viewport->view_window = NULL;
229
  viewport->bin_window = NULL;
Elliot Lee's avatar
Elliot Lee committed
230 231 232 233 234 235 236 237
  viewport->hadjustment = NULL;
  viewport->vadjustment = NULL;
}

GtkWidget*
gtk_viewport_new (GtkAdjustment *hadjustment,
		  GtkAdjustment *vadjustment)
{
238 239 240 241 242 243 244 245 246
  GtkWidget *viewport;

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

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

248 249 250 251
static void
gtk_viewport_destroy (GtkObject *object)
{
  GtkViewport *viewport = GTK_VIEWPORT (object);
Elliot Lee's avatar
Elliot Lee committed
252

253
  if (viewport->hadjustment)
254 255 256 257 258
    {
      gtk_signal_disconnect_by_data (GTK_OBJECT (viewport->hadjustment), viewport);
      gtk_object_unref (GTK_OBJECT (viewport->hadjustment));
      viewport->hadjustment = NULL;
    }
259
  if (viewport->vadjustment)
260 261 262 263 264
    {
      gtk_signal_disconnect_by_data (GTK_OBJECT (viewport->vadjustment), viewport);
      gtk_object_unref (GTK_OBJECT (viewport->vadjustment));
      viewport->vadjustment = NULL;
    }
265

266
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
267 268
}

Elliot Lee's avatar
Elliot Lee committed
269 270 271 272 273 274
GtkAdjustment*
gtk_viewport_get_hadjustment (GtkViewport *viewport)
{
  g_return_val_if_fail (viewport != NULL, NULL);
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);

275 276 277
  if (!viewport->hadjustment)
    gtk_viewport_set_hadjustment (viewport, NULL);

Elliot Lee's avatar
Elliot Lee committed
278 279 280 281 282 283 284 285 286
  return viewport->hadjustment;
}

GtkAdjustment*
gtk_viewport_get_vadjustment (GtkViewport *viewport)
{
  g_return_val_if_fail (viewport != NULL, NULL);
  g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);

287 288 289
  if (!viewport->vadjustment)
    gtk_viewport_set_vadjustment (viewport, NULL);

Elliot Lee's avatar
Elliot Lee committed
290 291 292 293 294 295 296 297 298
  return viewport->vadjustment;
}

void
gtk_viewport_set_hadjustment (GtkViewport   *viewport,
			      GtkAdjustment *adjustment)
{
  g_return_if_fail (viewport != NULL);
  g_return_if_fail (GTK_IS_VIEWPORT (viewport));
299 300
  if (adjustment)
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
Elliot Lee's avatar
Elliot Lee committed
301

302
  if (viewport->hadjustment && viewport->hadjustment != adjustment)
Elliot Lee's avatar
Elliot Lee committed
303
    {
304
      gtk_signal_disconnect_by_data (GTK_OBJECT (viewport->hadjustment), viewport);
305
      gtk_object_unref (GTK_OBJECT (viewport->hadjustment));
306
      viewport->hadjustment = NULL;
307 308 309 310 311
    }

  if (!adjustment)
    adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
						     0.0, 0.0, 0.0));
Elliot Lee's avatar
Elliot Lee committed
312

313 314
  if (viewport->hadjustment != adjustment)
    {
315 316
      viewport->hadjustment = adjustment;
      gtk_object_ref (GTK_OBJECT (viewport->hadjustment));
317 318
      gtk_object_sink (GTK_OBJECT (viewport->hadjustment));
      
319
      gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
320 321
			  (GtkSignalFunc) gtk_viewport_adjustment_changed,
			  (gpointer) viewport);
322
      gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
323 324
			  (GtkSignalFunc) gtk_viewport_adjustment_value_changed,
			  (gpointer) viewport);
Elliot Lee's avatar
Elliot Lee committed
325

326
      gtk_viewport_adjustment_changed (adjustment, viewport);
327
    }
Alexander Larsson's avatar
Alexander Larsson committed
328 329

  g_object_notify (G_OBJECT (viewport), "hadjustment");
Elliot Lee's avatar
Elliot Lee committed
330 331 332 333 334 335 336 337
}

void
gtk_viewport_set_vadjustment (GtkViewport   *viewport,
			      GtkAdjustment *adjustment)
{
  g_return_if_fail (viewport != NULL);
  g_return_if_fail (GTK_IS_VIEWPORT (viewport));
338 339
  if (adjustment)
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
Elliot Lee's avatar
Elliot Lee committed
340

341
  if (viewport->vadjustment && viewport->vadjustment != adjustment)
Elliot Lee's avatar
Elliot Lee committed
342
    {
343
      gtk_signal_disconnect_by_data (GTK_OBJECT (viewport->vadjustment), viewport);
344
      gtk_object_unref (GTK_OBJECT (viewport->vadjustment));
345
      viewport->vadjustment = NULL;
346
    }
Elliot Lee's avatar
Elliot Lee committed
347

348 349 350 351 352 353
  if (!adjustment)
    adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
						     0.0, 0.0, 0.0));

  if (viewport->vadjustment != adjustment)
    {
354 355
      viewport->vadjustment = adjustment;
      gtk_object_ref (GTK_OBJECT (viewport->vadjustment));
356
      gtk_object_sink (GTK_OBJECT (viewport->vadjustment));
357 358
      
      gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
359 360
			  (GtkSignalFunc) gtk_viewport_adjustment_changed,
			  (gpointer) viewport);
361
      gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
362 363
			  (GtkSignalFunc) gtk_viewport_adjustment_value_changed,
			  (gpointer) viewport);
364

365
      gtk_viewport_adjustment_changed (adjustment, viewport);
366
    }
Alexander Larsson's avatar
Alexander Larsson committed
367 368

  g_object_notify (G_OBJECT (viewport), "vadjustment");
Elliot Lee's avatar
Elliot Lee committed
369 370
}

371
static void
372 373 374
gtk_viewport_set_scroll_adjustments (GtkViewport      *viewport,
				     GtkAdjustment    *hadjustment,
				     GtkAdjustment    *vadjustment)
375 376 377 378 379 380 381
{
  if (viewport->hadjustment != hadjustment)
    gtk_viewport_set_hadjustment (viewport, hadjustment);
  if (viewport->vadjustment != vadjustment)
    gtk_viewport_set_vadjustment (viewport, vadjustment);
}

Elliot Lee's avatar
Elliot Lee committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
void
gtk_viewport_set_shadow_type (GtkViewport   *viewport,
			      GtkShadowType  type)
{
  g_return_if_fail (viewport != NULL);
  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
399 400

  g_object_notify (G_OBJECT (viewport), "shadow_type");
Elliot Lee's avatar
Elliot Lee committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
}


static void
gtk_viewport_map (GtkWidget *widget)
{
  GtkBin *bin;

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

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
  bin = GTK_BIN (widget);

  if (bin->child &&
      GTK_WIDGET_VISIBLE (bin->child) &&
      !GTK_WIDGET_MAPPED (bin->child))
    gtk_widget_map (bin->child);
419 420

  gdk_window_show (widget->window);
Elliot Lee's avatar
Elliot Lee committed
421 422 423 424 425 426 427 428 429 430
}

static void
gtk_viewport_unmap (GtkWidget *widget)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_VIEWPORT (widget));

  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
  
431
  gdk_window_hide (widget->window);
Elliot Lee's avatar
Elliot Lee committed
432 433 434 435 436
}

static void
gtk_viewport_realize (GtkWidget *widget)
{
437
  GtkBin *bin;
Elliot Lee's avatar
Elliot Lee committed
438 439 440
  GtkViewport *viewport;
  GdkWindowAttr attributes;
  gint attributes_mask;
441
  gint event_mask;
442
  gint border_width;
Elliot Lee's avatar
Elliot Lee committed
443 444 445 446

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

447 448
  border_width = GTK_CONTAINER (widget)->border_width;

449
  bin = GTK_BIN (widget);
Elliot Lee's avatar
Elliot Lee committed
450 451 452
  viewport = GTK_VIEWPORT (widget);
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

453 454 455 456
  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
457 458 459 460
  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);
461 462

  event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
463 464 465
  /* 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
466 467 468

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

469 470 471
  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
472

473 474
  if (viewport->shadow_type != GTK_SHADOW_NONE)
    {
475 476
      attributes.x = widget->style->xthickness;
      attributes.y = widget->style->ythickness;
477 478 479 480 481 482 483
    }
  else
    {
      attributes.x = 0;
      attributes.y = 0;
    }

484 485
  attributes.width = MAX (1, (gint)widget->allocation.width - attributes.x * 2 - border_width * 2);
  attributes.height = MAX (1, (gint)widget->allocation.height - attributes.y * 2 - border_width * 2);
486
  attributes.event_mask = 0;
Elliot Lee's avatar
Elliot Lee committed
487

488
  viewport->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
Elliot Lee's avatar
Elliot Lee committed
489 490 491 492
  gdk_window_set_user_data (viewport->view_window, viewport);

  attributes.x = 0;
  attributes.y = 0;
493 494 495 496 497 498 499

  if (bin->child)
    {
      attributes.width = viewport->hadjustment->upper;
      attributes.height = viewport->vadjustment->upper;
    }
  
500
  attributes.event_mask = event_mask;
Elliot Lee's avatar
Elliot Lee committed
501

502 503 504 505
  viewport->bin_window = gdk_window_new (viewport->view_window, &attributes, attributes_mask);
  gdk_window_set_user_data (viewport->bin_window, viewport);

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

508
  widget->style = gtk_style_attach (widget->style, widget->window);
Elliot Lee's avatar
Elliot Lee committed
509
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
510
  gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
511 512 513 514 515 516

   gtk_paint_flat_box(widget->style, viewport->bin_window, GTK_STATE_NORMAL,
		      GTK_SHADOW_NONE,
		      NULL, widget, "viewportbin",
		      0, 0, -1, -1);
   
517
  gdk_window_show (viewport->bin_window);
Elliot Lee's avatar
Elliot Lee committed
518 519 520 521 522 523 524 525 526 527 528 529 530
  gdk_window_show (viewport->view_window);
}

static void
gtk_viewport_unrealize (GtkWidget *widget)
{
  GtkViewport *viewport;

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

  viewport = GTK_VIEWPORT (widget);

531
  gdk_window_set_user_data (viewport->view_window, NULL);
Elliot Lee's avatar
Elliot Lee committed
532
  gdk_window_destroy (viewport->view_window);
533
  viewport->view_window = NULL;
534

535
  gdk_window_set_user_data (viewport->bin_window, NULL);
536 537
  gdk_window_destroy (viewport->bin_window);
  viewport->bin_window = NULL;
538 539 540

  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
Elliot Lee's avatar
Elliot Lee committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
}

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

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

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

557
      gtk_draw_shadow (widget->style, widget->window,
Elliot Lee's avatar
Elliot Lee committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
		       GTK_STATE_NORMAL, viewport->shadow_type,
		       0, 0, -1, -1);
    }
}

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

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_VIEWPORT (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

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

580
      if (event->window == widget->window)
Elliot Lee's avatar
Elliot Lee committed
581
	gtk_viewport_paint (widget, &event->area);
582 583 584 585 586 587 588 589 590
      else if (event->window == viewport->bin_window)
	{
	  child_event = *event;

	  gtk_paint_flat_box(widget->style, viewport->bin_window, 
			     GTK_STATE_NORMAL, GTK_SHADOW_NONE,
			     &event->area, widget, "viewportbin",
			     0, 0, -1, -1);
	  
591
	  (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
592 593
	}
	
Elliot Lee's avatar
Elliot Lee committed
594 595 596 597 598 599

    }

  return FALSE;
}

600 601
static void
gtk_viewport_add (GtkContainer *container,
602
		  GtkWidget    *child)
603 604 605 606 607
{
  GtkBin *bin;

  g_return_if_fail (container != NULL);
  g_return_if_fail (GTK_IS_VIEWPORT (container));
608
  g_return_if_fail (child != NULL);
609 610

  bin = GTK_BIN (container);
611
  g_return_if_fail (bin->child == NULL);
612

613
  gtk_widget_set_parent_window (child, GTK_VIEWPORT (bin)->bin_window);
614

615
  GTK_CONTAINER_CLASS (parent_class)->add (container, child);
616 617
}

Elliot Lee's avatar
Elliot Lee committed
618 619 620 621 622 623
static void
gtk_viewport_size_request (GtkWidget      *widget,
			   GtkRequisition *requisition)
{
  GtkViewport *viewport;
  GtkBin *bin;
624
  GtkRequisition child_requisition;
Elliot Lee's avatar
Elliot Lee committed
625 626 627 628 629 630 631 632 633

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

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

  requisition->width = (GTK_CONTAINER (widget)->border_width +
634
			GTK_WIDGET (widget)->style->xthickness) * 2 + 5;
Elliot Lee's avatar
Elliot Lee committed
635 636

  requisition->height = (GTK_CONTAINER (widget)->border_width * 2 +
637
			 GTK_WIDGET (widget)->style->ythickness) * 2 + 5;
Elliot Lee's avatar
Elliot Lee committed
638 639

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
640
    {
641 642 643
      gtk_widget_size_request (bin->child, &child_requisition);
      requisition->width += child_requisition.width;
      requisition->height += child_requisition.height;
644
    }
Elliot Lee's avatar
Elliot Lee committed
645 646 647 648 649 650 651 652 653 654
}

static void
gtk_viewport_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
{
  GtkViewport *viewport;
  GtkBin *bin;
  GtkAllocation child_allocation;
  gint hval, vval;
Owen Taylor's avatar
Owen Taylor committed
655
  gint border_width;
Elliot Lee's avatar
Elliot Lee committed
656 657 658 659 660 661 662 663 664

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

  widget->allocation = *allocation;
  viewport = GTK_VIEWPORT (widget);
  bin = GTK_BIN (widget);

665 666 667 668 669 670
  /* demand creation */
  if (!viewport->hadjustment)
    gtk_viewport_set_hadjustment (viewport, NULL);
  if (!viewport->vadjustment)
    gtk_viewport_set_hadjustment (viewport, NULL);

Owen Taylor's avatar
Owen Taylor committed
671 672
  border_width = GTK_CONTAINER (widget)->border_width;

673 674
  child_allocation.x = 0;
  child_allocation.y = 0;
Elliot Lee's avatar
Elliot Lee committed
675

676 677
  if (viewport->shadow_type != GTK_SHADOW_NONE)
    {
678 679
      child_allocation.x = GTK_WIDGET (viewport)->style->xthickness;
      child_allocation.y = GTK_WIDGET (viewport)->style->ythickness;
680 681
    }

682 683
  child_allocation.width = MAX (1, allocation->width - child_allocation.x * 2 - border_width * 2);
  child_allocation.height = MAX (1, allocation->height - child_allocation.y * 2 - border_width * 2);
Elliot Lee's avatar
Elliot Lee committed
684 685 686

  if (GTK_WIDGET_REALIZED (widget))
    {
687
      gdk_window_move_resize (widget->window,
Owen Taylor's avatar
Owen Taylor committed
688 689 690 691
			      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
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

      gdk_window_move_resize (viewport->view_window,
			      child_allocation.x,
			      child_allocation.y,
			      child_allocation.width,
			      child_allocation.height);
    }

  viewport->hadjustment->page_size = child_allocation.width;
  viewport->hadjustment->page_increment = viewport->hadjustment->page_size / 2;
  viewport->hadjustment->step_increment = 10;

  viewport->vadjustment->page_size = child_allocation.height;
  viewport->vadjustment->page_increment = viewport->vadjustment->page_size / 2;
  viewport->vadjustment->step_increment = 10;

  hval = viewport->hadjustment->value;
  vval = viewport->vadjustment->value;

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
713 714 715
      GtkRequisition child_requisition;
      gtk_widget_get_child_requisition (bin->child, &child_requisition);
      
Elliot Lee's avatar
Elliot Lee committed
716
      viewport->hadjustment->lower = 0;
717
      viewport->hadjustment->upper = MAX (child_requisition.width,
Elliot Lee's avatar
Elliot Lee committed
718 719 720 721 722 723 724
					  child_allocation.width);

      hval = CLAMP (hval, 0,
		    viewport->hadjustment->upper -
		    viewport->hadjustment->page_size);

      viewport->vadjustment->lower = 0;
725
      viewport->vadjustment->upper = MAX (child_requisition.height,
Elliot Lee's avatar
Elliot Lee committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
					  child_allocation.height);

      vval = CLAMP (vval, 0,
		    viewport->vadjustment->upper -
		    viewport->vadjustment->page_size);
    }

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
      child_allocation.x = 0;
      child_allocation.y = 0;

      child_allocation.width = viewport->hadjustment->upper;
      child_allocation.height = viewport->vadjustment->upper;

Owen Taylor's avatar
Owen Taylor committed
741 742 743 744
      if (GTK_WIDGET_REALIZED (widget))
	gdk_window_resize (viewport->bin_window,
			   child_allocation.width,
			   child_allocation.height);
Elliot Lee's avatar
Elliot Lee committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

      child_allocation.x = 0;
      child_allocation.y = 0;
      gtk_widget_size_allocate (bin->child, &child_allocation);
    }

  gtk_signal_emit_by_name (GTK_OBJECT (viewport->hadjustment), "changed");
  gtk_signal_emit_by_name (GTK_OBJECT (viewport->vadjustment), "changed");
  if (viewport->hadjustment->value != hval)
    {
      viewport->hadjustment->value = hval;
      gtk_signal_emit_by_name (GTK_OBJECT (viewport->hadjustment), "value_changed");
    }
  if (viewport->vadjustment->value != vval)
    {
      viewport->vadjustment->value = vval;
      gtk_signal_emit_by_name (GTK_OBJECT (viewport->vadjustment), "value_changed");
    }
}

static void
gtk_viewport_adjustment_changed (GtkAdjustment *adjustment,
				 gpointer       data)
{
  GtkViewport *viewport;

  g_return_if_fail (adjustment != NULL);
  g_return_if_fail (data != NULL);
  g_return_if_fail (GTK_IS_VIEWPORT (data));

  viewport = GTK_VIEWPORT (data);
}

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

  g_return_if_fail (adjustment != NULL);
  g_return_if_fail (data != NULL);
  g_return_if_fail (GTK_IS_VIEWPORT (data));

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

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
      child_allocation.x = 0;
      child_allocation.y = 0;

      if (viewport->hadjustment->lower != (viewport->hadjustment->upper -
					   viewport->hadjustment->page_size))
	child_allocation.x =  viewport->hadjustment->lower - viewport->hadjustment->value;

      if (viewport->vadjustment->lower != (viewport->vadjustment->upper -
					   viewport->vadjustment->page_size))
	child_allocation.y = viewport->vadjustment->lower - viewport->vadjustment->value;

      if (GTK_WIDGET_REALIZED (viewport))
807 808 809 810 811 812 813
	{
	  gdk_window_move (viewport->bin_window,
			   child_allocation.x,
			   child_allocation.y);
      
	  gdk_window_process_updates (viewport->bin_window, TRUE);
	}
Elliot Lee's avatar
Elliot Lee committed
814 815
    }
}
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831

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