gtklayout.c 31.3 KB
Newer Older
1 2
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
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.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18 19 20 21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * GtkLayout: Widget for scrolling of arbitrary-sized areas.
 *
 * Copyright Owen Taylor, 1998
22 23
 */

24
/*
25
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
26 27 28 29 30
 * 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/. 
 */

31 32
#include "gdkconfig.h"

33
#include "gtklayout.h"
34
#include "gtkprivate.h"
Alexander Larsson's avatar
Alexander Larsson committed
35
#include "gtkintl.h"
36
#include "gtkmarshalers.h"
37

38 39 40 41 42 43 44 45
typedef struct _GtkLayoutChild   GtkLayoutChild;

struct _GtkLayoutChild {
  GtkWidget *widget;
  gint x;
  gint y;
};

Alexander Larsson's avatar
Alexander Larsson committed
46 47 48 49 50 51 52 53
enum {
   PROP_0,
   PROP_HADJUSTMENT,
   PROP_VADJUSTMENT,
   PROP_WIDTH,
   PROP_HEIGHT
};

Havoc Pennington's avatar
Havoc Pennington committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
enum {
  CHILD_PROP_0,
  CHILD_PROP_X,
  CHILD_PROP_Y
};

static void gtk_layout_class_init         (GtkLayoutClass *class);
static void gtk_layout_get_property       (GObject        *object,
                                           guint           prop_id,
                                           GValue         *value,
                                           GParamSpec     *pspec);
static void gtk_layout_set_property       (GObject        *object,
                                           guint           prop_id,
                                           const GValue   *value,
                                           GParamSpec     *pspec);
69 70 71
static GObject *gtk_layout_constructor    (GType                  type,
					   guint                  n_properties,
					   GObjectConstructParam *properties);
Havoc Pennington's avatar
Havoc Pennington committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static void gtk_layout_init               (GtkLayout      *layout);
static void gtk_layout_finalize           (GObject        *object);
static void gtk_layout_realize            (GtkWidget      *widget);
static void gtk_layout_unrealize          (GtkWidget      *widget);
static void gtk_layout_map                (GtkWidget      *widget);
static void gtk_layout_size_request       (GtkWidget      *widget,
                                           GtkRequisition *requisition);
static void gtk_layout_size_allocate      (GtkWidget      *widget,
                                           GtkAllocation  *allocation);
static gint gtk_layout_expose             (GtkWidget      *widget,
                                           GdkEventExpose *event);
static void gtk_layout_remove             (GtkContainer   *container,
                                           GtkWidget      *widget);
static void gtk_layout_forall             (GtkContainer   *container,
                                           gboolean        include_internals,
                                           GtkCallback     callback,
                                           gpointer        callback_data);
static void gtk_layout_set_adjustments    (GtkLayout      *layout,
                                           GtkAdjustment  *hadj,
                                           GtkAdjustment  *vadj);
static void gtk_layout_set_child_property (GtkContainer   *container,
                                           GtkWidget      *child,
                                           guint           property_id,
                                           const GValue   *value,
                                           GParamSpec     *pspec);
static void gtk_layout_get_child_property (GtkContainer   *container,
                                           GtkWidget      *child,
                                           guint           property_id,
                                           GValue         *value,
                                           GParamSpec     *pspec);
static void gtk_layout_allocate_child     (GtkLayout      *layout,
                                           GtkLayoutChild *child);
static void gtk_layout_adjustment_changed (GtkAdjustment  *adjustment,
                                           GtkLayout      *layout);
106 107
static void gtk_layout_style_set          (GtkWidget      *widget,
					   GtkStyle       *old_style);
Havoc Pennington's avatar
Havoc Pennington committed
108

109
static void gtk_layout_set_adjustment_upper (GtkAdjustment *adj,
110 111
					     gdouble        upper,
					     gboolean       always_emit_changed);
112 113 114 115 116

static GtkWidgetClass *parent_class = NULL;

/* Public interface
 */
117 118 119 120 121 122 123 124 125 126 127
/**
 * gtk_layout_new:
 * @hadjustment: horizontal scroll adjustment, or %NULL
 * @vadjustment: vertical scroll adjustment, or %NULL
 * 
 * Creates a new #GtkLayout. Unless you have a specific adjustment
 * you'd like the layout to use for scrolling, pass %NULL for
 * @hadjustment and @vadjustment.
 * 
 * Return value: a new #GtkLayout
 **/
128 129 130 131 132 133 134
  
GtkWidget*    
gtk_layout_new (GtkAdjustment *hadjustment,
		GtkAdjustment *vadjustment)
{
  GtkLayout *layout;

135 136 137 138
  layout = g_object_new (GTK_TYPE_LAYOUT,
			 "hadjustment", hadjustment,
			 "vadjustment", vadjustment,
			 NULL);
139 140 141 142

  return GTK_WIDGET (layout);
}

143 144 145 146 147 148 149 150 151 152 153 154 155
/**
 * gtk_layout_get_hadjustment:
 * @layout: a #GtkLayout
 * 
 * This function should only be called after the layout has been
 * placed in a #GtkScrolledWindow or otherwise configured for
 * scrolling. It returns the #GtkAdjustment used for communication
 * between the horizontal scrollbar and @layout.
 *
 * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
 * 
 * Return value: horizontal scroll adjustment
 **/
156 157 158 159 160 161 162
GtkAdjustment* 
gtk_layout_get_hadjustment (GtkLayout     *layout)
{
  g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);

  return layout->hadjustment;
}
163 164 165 166 167 168 169 170 171 172 173 174 175
/**
 * gtk_layout_get_vadjustment:
 * @layout: a #GtkLayout
 * 
 * This function should only be called after the layout has been
 * placed in a #GtkScrolledWindow or otherwise configured for
 * scrolling. It returns the #GtkAdjustment used for communication
 * between the vertical scrollbar and @layout.
 *
 * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
 * 
 * Return value: vertical scroll adjustment
 **/
176 177 178 179 180 181 182 183
GtkAdjustment* 
gtk_layout_get_vadjustment (GtkLayout     *layout)
{
  g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);

  return layout->vadjustment;
}

184 185 186 187 188 189
static GtkAdjustment *
new_default_adjustment (void)
{
  return GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
}

190 191 192 193 194
static void           
gtk_layout_set_adjustments (GtkLayout     *layout,
			    GtkAdjustment *hadj,
			    GtkAdjustment *vadj)
{
195 196
  gboolean need_adjust = FALSE;

197 198 199 200
  g_return_if_fail (GTK_IS_LAYOUT (layout));

  if (hadj)
    g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
201 202
  else if (layout->hadjustment)
    hadj = new_default_adjustment ();
203 204
  if (vadj)
    g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
205 206
  else if (layout->vadjustment)
    vadj = new_default_adjustment ();
207 208 209
  
  if (layout->hadjustment && (layout->hadjustment != hadj))
    {
Manish Singh's avatar
Manish Singh committed
210 211 212 213
      g_signal_handlers_disconnect_by_func (layout->hadjustment,
					    gtk_layout_adjustment_changed,
					    layout);
      g_object_unref (layout->hadjustment);
214 215 216 217
    }
  
  if (layout->vadjustment && (layout->vadjustment != vadj))
    {
Manish Singh's avatar
Manish Singh committed
218 219 220 221
      g_signal_handlers_disconnect_by_func (layout->vadjustment,
					    gtk_layout_adjustment_changed,
					    layout);
      g_object_unref (layout->vadjustment);
222 223 224 225 226
    }
  
  if (layout->hadjustment != hadj)
    {
      layout->hadjustment = hadj;
Manish Singh's avatar
Manish Singh committed
227
      g_object_ref (layout->hadjustment);
228
      gtk_object_sink (GTK_OBJECT (layout->hadjustment));
229
      gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width, FALSE);
230
      
Manish Singh's avatar
Manish Singh committed
231 232 233
      g_signal_connect (layout->hadjustment, "value_changed",
			G_CALLBACK (gtk_layout_adjustment_changed),
			layout);
234
      need_adjust = TRUE;
235 236 237 238 239
    }
  
  if (layout->vadjustment != vadj)
    {
      layout->vadjustment = vadj;
Manish Singh's avatar
Manish Singh committed
240
      g_object_ref (layout->vadjustment);
241
      gtk_object_sink (GTK_OBJECT (layout->vadjustment));
242
      gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height, FALSE);
243
      
Manish Singh's avatar
Manish Singh committed
244 245 246
      g_signal_connect (layout->vadjustment, "value_changed",
			G_CALLBACK (gtk_layout_adjustment_changed),
			layout);
247
      need_adjust = TRUE;
248
    }
249

250 251 252
  /* vadj or hadj can be NULL while constructing; don't emit a signal
     then */
  if (need_adjust && vadj && hadj)
253
    gtk_layout_adjustment_changed (NULL, layout);
254 255
}

256
static void
257
gtk_layout_finalize (GObject *object)
258 259 260
{
  GtkLayout *layout = GTK_LAYOUT (object);

Manish Singh's avatar
Manish Singh committed
261 262
  g_object_unref (layout->hadjustment);
  g_object_unref (layout->vadjustment);
263

264
  G_OBJECT_CLASS (parent_class)->finalize (object);
265 266
}

267 268 269 270 271 272 273 274 275 276
/**
 * gtk_layout_set_hadjustment:
 * @layout: a #GtkLayout
 * @adjustment: new scroll adjustment
 *
 * Sets the horizontal scroll adjustment for the layout.
 *
 * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
 * 
 **/
277 278 279 280 281 282
void           
gtk_layout_set_hadjustment (GtkLayout     *layout,
			    GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));

283
  gtk_layout_set_adjustments (layout, adjustment, layout->vadjustment);
Alexander Larsson's avatar
Alexander Larsson committed
284
  g_object_notify (G_OBJECT (layout), "hadjustment");
285 286
}
 
287 288 289 290 291 292 293 294 295 296
/**
 * gtk_layout_set_vadjustment:
 * @layout: a #GtkLayout
 * @adjustment: new scroll adjustment
 *
 * Sets the vertical scroll adjustment for the layout.
 *
 * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
 * 
 **/
297 298 299 300 301 302
void           
gtk_layout_set_vadjustment (GtkLayout     *layout,
			    GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));
  
303
  gtk_layout_set_adjustments (layout, layout->hadjustment, adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
304
  g_object_notify (G_OBJECT (layout), "vadjustment");
305 306
}

Havoc Pennington's avatar
Havoc Pennington committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
static GtkLayoutChild*
get_child (GtkLayout  *layout,
           GtkWidget  *widget)
{
  GList *children;
  
  children = layout->children;
  while (children)
    {
      GtkLayoutChild *child;
      
      child = children->data;
      children = children->next;

      if (child->widget == widget)
        return child;
    }

  return NULL;
}
327

328 329 330 331 332 333 334 335 336 337 338
/**
 * gtk_layout_put:
 * @layout: a #GtkLayout
 * @child_widget: child widget
 * @x: X position of child widget
 * @y: Y position of child widget
 *
 * Adds @child_widget to @layout, at position (@x,@y).
 * @layout becomes the new parent container of @child_widget.
 * 
 **/
339 340 341 342 343 344 345 346 347
void           
gtk_layout_put (GtkLayout     *layout, 
		GtkWidget     *child_widget, 
		gint           x, 
		gint           y)
{
  GtkLayoutChild *child;

  g_return_if_fail (GTK_IS_LAYOUT (layout));
348
  g_return_if_fail (GTK_IS_WIDGET (child_widget));
349 350 351 352 353 354 355 356 357
  
  child = g_new (GtkLayoutChild, 1);

  child->widget = child_widget;
  child->x = x;
  child->y = y;

  layout->children = g_list_append (layout->children, child);
  
358 359
  if (GTK_WIDGET_REALIZED (layout))
    gtk_widget_set_parent_window (child->widget, layout->bin_window);
Havoc Pennington's avatar
Havoc Pennington committed
360

361
  gtk_widget_set_parent (child_widget, GTK_WIDGET (layout));
362 363
}

Havoc Pennington's avatar
Havoc Pennington committed
364 365 366 367 368 369 370
static void
gtk_layout_move_internal (GtkLayout       *layout,
                          GtkWidget       *widget,
                          gboolean         change_x,
                          gint             x,
                          gboolean         change_y,
                          gint             y)
371 372 373 374
{
  GtkLayoutChild *child;
  
  g_return_if_fail (GTK_IS_LAYOUT (layout));
Havoc Pennington's avatar
Havoc Pennington committed
375 376 377 378 379 380
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (widget->parent == GTK_WIDGET (layout));  
  
  child = get_child (layout, widget);

  g_assert (child);
381

Havoc Pennington's avatar
Havoc Pennington committed
382 383 384
  gtk_widget_freeze_child_notify (widget);
  
  if (change_x)
385
    {
Havoc Pennington's avatar
Havoc Pennington committed
386 387 388
      child->x = x;
      gtk_widget_child_notify (widget, "x");
    }
389

Havoc Pennington's avatar
Havoc Pennington committed
390 391 392 393 394
  if (change_y)
    {
      child->y = y;
      gtk_widget_child_notify (widget, "y");
    }
395

Havoc Pennington's avatar
Havoc Pennington committed
396 397 398
  gtk_widget_thaw_child_notify (widget);
  
  if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (layout))
jacob berkman's avatar
jacob berkman committed
399
    gtk_widget_queue_resize (GTK_WIDGET (widget));
Havoc Pennington's avatar
Havoc Pennington committed
400
}
401

402 403 404 405 406 407 408 409 410 411
/**
 * gtk_layout_move:
 * @layout: a #GtkLayout
 * @child_widget: a current child of @layout
 * @x: X position to move to
 * @y: Y position to move to
 *
 * Moves a current child of @layout to a new position.
 * 
 **/
Havoc Pennington's avatar
Havoc Pennington committed
412 413 414 415 416 417 418 419 420 421 422
void           
gtk_layout_move (GtkLayout     *layout, 
		 GtkWidget     *child_widget, 
		 gint           x, 
		 gint           y)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));
  g_return_if_fail (GTK_IS_WIDGET (child_widget));
  g_return_if_fail (child_widget->parent == GTK_WIDGET (layout));  

  gtk_layout_move_internal (layout, child_widget, TRUE, x, TRUE, y);
423 424
}

425
static void
426
gtk_layout_set_adjustment_upper (GtkAdjustment *adj,
427 428
				 gdouble        upper,
				 gboolean       always_emit_changed)
429
{
430 431 432 433 434
  gboolean changed = FALSE;
  gboolean value_changed = FALSE;
  
  gdouble min = MAX (0., upper - adj->page_size);

435 436 437
  if (upper != adj->upper)
    {
      adj->upper = upper;
438 439
      changed = TRUE;
    }
440
      
441 442 443 444
  if (adj->value > min)
    {
      adj->value = min;
      value_changed = TRUE;
445
    }
446 447
  
  if (changed || always_emit_changed)
Manish Singh's avatar
Manish Singh committed
448
    gtk_adjustment_changed (adj);
449
  if (value_changed)
Manish Singh's avatar
Manish Singh committed
450
    gtk_adjustment_value_changed (adj);
451 452
}

453 454 455 456 457 458 459 460 461
/**
 * gtk_layout_set_size:
 * @layout: a #GtkLayout
 * @width: width of entire scrollable area
 * @height: height of entire scrollable area
 *
 * Sets the size of the scrollable area of the layout.
 * 
 **/
462 463 464 465 466
void
gtk_layout_set_size (GtkLayout     *layout, 
		     guint          width,
		     guint          height)
{
467 468
  GtkWidget *widget;
  
469 470
  g_return_if_fail (GTK_IS_LAYOUT (layout));

471 472
  widget = GTK_WIDGET (layout);
  
473
  g_object_freeze_notify (G_OBJECT (layout));
Alexander Larsson's avatar
Alexander Larsson committed
474 475 476 477 478 479 480 481 482 483
  if (width != layout->width)
     {
	layout->width = width;
	g_object_notify (G_OBJECT (layout), "width");
     }
  if (height != layout->height)
     {
	layout->height = height;
	g_object_notify (G_OBJECT (layout), "height");
     }
484
  g_object_thaw_notify (G_OBJECT (layout));
485

486
  if (layout->hadjustment)
487
    gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width, FALSE);
488
  if (layout->vadjustment)
489
    gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height, FALSE);
490 491

  if (GTK_WIDGET_REALIZED (layout))
492 493 494 495 496
    {
      width = MAX (width, widget->allocation.width);
      height = MAX (height, widget->allocation.height);
      gdk_window_resize (layout->bin_window, width, height);
    }
497 498
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
/**
 * gtk_layout_get_size:
 * @layout: a #GtkLayout
 * @width: location to store the width set on @layout, or %NULL
 * @height: location to store the height set on @layout, or %NULL
 *
 * Gets the size that has been set on the layout, and that determines
 * the total extents of the layout's scrollbar area. See
 * gtk_layout_set_size ().
 **/
void
gtk_layout_get_size (GtkLayout *layout,
		     guint     *width,
		     guint     *height)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));

  if (width)
    *width = layout->width;
  if (height)
    *height = layout->height;
}

522 523 524 525 526 527
/**
 * gtk_layout_freeze:
 * @layout: a #GtkLayout
 * 
 * This is a deprecated function, it doesn't do anything useful.
 **/
528 529 530 531 532
void
gtk_layout_freeze (GtkLayout *layout)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));

533
  layout->freeze_count++;
534 535
}

536 537 538 539 540 541
/**
 * gtk_layout_thaw:
 * @layout: a #GtkLayout
 * 
 * This is a deprecated function, it doesn't do anything useful.
 **/
542 543 544 545 546
void
gtk_layout_thaw (GtkLayout *layout)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));

547
  if (layout->freeze_count)
Manish Singh's avatar
Manish Singh committed
548 549 550 551 552 553 554 555
    {
      if (!(--layout->freeze_count))
	{
	  gtk_widget_queue_draw (GTK_WIDGET (layout));
	  gdk_window_process_updates (GTK_WIDGET (layout)->window, TRUE);
	}
    }

556 557 558 559
}

/* Basic Object handling procedures
 */
Manish Singh's avatar
Manish Singh committed
560
GType
561 562
gtk_layout_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
563
  static GType layout_type = 0;
564 565 566

  if (!layout_type)
    {
567
      static const GTypeInfo layout_info =
568 569
      {
	sizeof (GtkLayoutClass),
Manish Singh's avatar
Manish Singh committed
570 571
	NULL,		/* base_init */
	NULL,		/* base_finalize */
572
	(GClassInitFunc) gtk_layout_class_init,
Manish Singh's avatar
Manish Singh committed
573 574
	NULL,		/* class_finalize */
	NULL,		/* class_data */
575
	sizeof (GtkLayout),
Manish Singh's avatar
Manish Singh committed
576
	0,		/* n_preallocs */
577
	(GInstanceInitFunc) gtk_layout_init,
578 579
      };

Manish Singh's avatar
Manish Singh committed
580 581
      layout_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkLayout",
					    &layout_info, 0);
582 583 584 585 586 587 588 589
    }

  return layout_type;
}

static void
gtk_layout_class_init (GtkLayoutClass *class)
{
Alexander Larsson's avatar
Alexander Larsson committed
590
  GObjectClass *gobject_class;
591 592 593
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

Alexander Larsson's avatar
Alexander Larsson committed
594
  gobject_class = (GObjectClass*) class;
595 596 597
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;

Manish Singh's avatar
Manish Singh committed
598
  parent_class = g_type_class_peek_parent (class);
599

Alexander Larsson's avatar
Alexander Larsson committed
600 601
  gobject_class->set_property = gtk_layout_set_property;
  gobject_class->get_property = gtk_layout_get_property;
602
  gobject_class->finalize = gtk_layout_finalize;
603
  gobject_class->constructor = gtk_layout_constructor;
604

Havoc Pennington's avatar
Havoc Pennington committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  container_class->set_child_property = gtk_layout_set_child_property;
  container_class->get_child_property = gtk_layout_get_child_property;

  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_X,
					      g_param_spec_int ("x",
                                                                _("X position"),
                                                                _("X position of child widget"),
                                                                G_MININT,
                                                                G_MAXINT,
                                                                0,
                                                                G_PARAM_READWRITE));

  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_Y,
					      g_param_spec_int ("y",
                                                                _("Y position"),
                                                                _("Y position of child widget"),
                                                                G_MININT,
                                                                G_MAXINT,
                                                                0,
                                                                G_PARAM_READWRITE));
  
Alexander Larsson's avatar
Alexander Larsson committed
628 629 630 631
  g_object_class_install_property (gobject_class,
				   PROP_HADJUSTMENT,
				   g_param_spec_object ("hadjustment",
							_("Horizontal adjustment"),
632
							_("The GtkAdjustment for the horizontal position"),
Alexander Larsson's avatar
Alexander Larsson committed
633 634 635 636 637 638 639
							GTK_TYPE_ADJUSTMENT,
							G_PARAM_READWRITE));
  
  g_object_class_install_property (gobject_class,
				   PROP_VADJUSTMENT,
				   g_param_spec_object ("vadjustment",
							_("Vertical adjustment"),
640
							_("The GtkAdjustment for the vertical position"),
Alexander Larsson's avatar
Alexander Larsson committed
641 642 643 644 645 646 647
							GTK_TYPE_ADJUSTMENT,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
				   PROP_WIDTH,
				   g_param_spec_uint ("width",
						     _("Width"),
648
						     _("The width of the layout"),
Alexander Larsson's avatar
Alexander Larsson committed
649 650 651 652 653 654 655 656
						     0,
						     G_MAXINT,
						     100,
						     G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
				   PROP_HEIGHT,
				   g_param_spec_uint ("height",
						     _("Height"),
657
						     _("The height of the layout"),
Alexander Larsson's avatar
Alexander Larsson committed
658 659 660 661
						     0,
						     G_MAXINT,
						     100,
						     G_PARAM_READWRITE));
662 663 664 665 666 667
  widget_class->realize = gtk_layout_realize;
  widget_class->unrealize = gtk_layout_unrealize;
  widget_class->map = gtk_layout_map;
  widget_class->size_request = gtk_layout_size_request;
  widget_class->size_allocate = gtk_layout_size_allocate;
  widget_class->expose_event = gtk_layout_expose;
668
  widget_class->style_set = gtk_layout_style_set;
669

670 671 672 673 674
  container_class->remove = gtk_layout_remove;
  container_class->forall = gtk_layout_forall;

  class->set_scroll_adjustments = gtk_layout_set_adjustments;

675
  widget_class->set_scroll_adjustments_signal =
Manish Singh's avatar
Manish Singh committed
676 677 678 679 680 681 682 683 684
    g_signal_new ("set_scroll_adjustments",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkLayoutClass, set_scroll_adjustments),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT_OBJECT,
		  G_TYPE_NONE, 2,
		  GTK_TYPE_ADJUSTMENT,
		  GTK_TYPE_ADJUSTMENT);
685 686
}

Alexander Larsson's avatar
Alexander Larsson committed
687 688 689 690 691 692 693 694 695 696 697
static void
gtk_layout_get_property (GObject     *object,
			 guint        prop_id,
			 GValue      *value,
			 GParamSpec  *pspec)
{
  GtkLayout *layout = GTK_LAYOUT (object);
  
  switch (prop_id)
    {
    case PROP_HADJUSTMENT:
698
      g_value_set_object (value, layout->hadjustment);
Alexander Larsson's avatar
Alexander Larsson committed
699 700
      break;
    case PROP_VADJUSTMENT:
701
      g_value_set_object (value, layout->vadjustment);
Alexander Larsson's avatar
Alexander Larsson committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
      break;
    case PROP_WIDTH:
      g_value_set_uint (value, layout->width);
      break;
    case PROP_HEIGHT:
      g_value_set_uint (value, layout->height);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_layout_set_property (GObject      *object,
			 guint         prop_id,
			 const GValue *value,
			 GParamSpec   *pspec)
{
  GtkLayout *layout = GTK_LAYOUT (object);
  
  switch (prop_id)
    {
    case PROP_HADJUSTMENT:
      gtk_layout_set_hadjustment (layout, 
				  (GtkAdjustment*) g_value_get_object (value));
      break;
    case PROP_VADJUSTMENT:
      gtk_layout_set_vadjustment (layout, 
				  (GtkAdjustment*) g_value_get_object (value));
      break;
    case PROP_WIDTH:
      gtk_layout_set_size (layout, g_value_get_uint (value),
			   layout->height);
      break;
    case PROP_HEIGHT:
      gtk_layout_set_size (layout, layout->width,
			   g_value_get_uint (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
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
static void
gtk_layout_set_child_property (GtkContainer    *container,
                               GtkWidget       *child,
                               guint            property_id,
                               const GValue    *value,
                               GParamSpec      *pspec)
{
  switch (property_id)
    {
    case CHILD_PROP_X:
      gtk_layout_move_internal (GTK_LAYOUT (container),
                                child,
                                TRUE, g_value_get_int (value),
                                FALSE, 0);
      break;
    case CHILD_PROP_Y:
      gtk_layout_move_internal (GTK_LAYOUT (container),
                                child,
                                FALSE, 0,
                                TRUE, g_value_get_int (value));
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      break;
    }
}

static void
gtk_layout_get_child_property (GtkContainer *container,
                               GtkWidget    *child,
                               guint         property_id,
                               GValue       *value,
                               GParamSpec   *pspec)
{
  GtkLayoutChild *layout_child;

  layout_child = get_child (GTK_LAYOUT (container), child);
  
  switch (property_id)
    {
    case CHILD_PROP_X:
      g_value_set_int (value, layout_child->x);
      break;
    case CHILD_PROP_Y:
      g_value_set_int (value, layout_child->y);
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      break;
    }
}
Alexander Larsson's avatar
Alexander Larsson committed
798

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
static void
gtk_layout_init (GtkLayout *layout)
{
  layout->children = NULL;

  layout->width = 100;
  layout->height = 100;

  layout->hadjustment = NULL;
  layout->vadjustment = NULL;

  layout->bin_window = NULL;

  layout->scroll_x = 0;
  layout->scroll_y = 0;
  layout->visibility = GDK_VISIBILITY_PARTIAL;
815 816

  layout->freeze_count = 0;
817 818
}

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
static GObject *
gtk_layout_constructor (GType                  type,
			guint                  n_properties,
			GObjectConstructParam *properties)
{
  GtkLayout *layout;
  GObject *object;
  GtkAdjustment *hadj, *vadj;
  
  object = G_OBJECT_CLASS (parent_class)->constructor (type,
						       n_properties,
						       properties);

  layout = GTK_LAYOUT (object);

  hadj = layout->hadjustment ? layout->hadjustment : new_default_adjustment ();
  vadj = layout->vadjustment ? layout->vadjustment : new_default_adjustment ();

  if (!layout->hadjustment || !layout->vadjustment)
    gtk_layout_set_adjustments (layout, hadj, vadj);

  return object;
}

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
/* Widget methods
 */

static void 
gtk_layout_realize (GtkWidget *widget)
{
  GList *tmp_list;
  GtkLayout *layout;
  GdkWindowAttr attributes;
  gint attributes_mask;

  g_return_if_fail (GTK_IS_LAYOUT (widget));

  layout = GTK_LAYOUT (widget);
  GTK_WIDGET_SET_FLAGS (layout, GTK_REALIZED);

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
				   &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, widget);

875 876
  attributes.x = - layout->hadjustment->value,
  attributes.y = - layout->vadjustment->value;
877 878
  attributes.width = MAX (layout->width, widget->allocation.width);
  attributes.height = MAX (layout->height, widget->allocation.height);
879
  attributes.event_mask = GDK_EXPOSURE_MASK | GDK_SCROLL_MASK | 
880
                          gtk_widget_get_events (widget);
881 882 883 884 885 886 887 888 889 890 891 892 893 894

  layout->bin_window = gdk_window_new (widget->window,
					&attributes, attributes_mask);
  gdk_window_set_user_data (layout->bin_window, widget);

  widget->style = gtk_style_attach (widget->style, widget->window);
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
  gtk_style_set_background (widget->style, layout->bin_window, GTK_STATE_NORMAL);

  tmp_list = layout->children;
  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
      tmp_list = tmp_list->next;
895 896

      gtk_widget_set_parent_window (child->widget, layout->bin_window);
897 898 899
    }
}

900 901 902 903 904 905 906 907 908 909 910 911
static void
gtk_layout_style_set (GtkWidget *widget, GtkStyle *old_style)
{
  if (GTK_WIDGET_CLASS (parent_class)->style_set)
    (* GTK_WIDGET_CLASS (parent_class)->style_set) (widget, old_style);

  if (GTK_WIDGET_REALIZED (widget))
    {
      gtk_style_set_background (widget->style, GTK_LAYOUT (widget)->bin_window, GTK_STATE_NORMAL);
    }
}

912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
static void 
gtk_layout_map (GtkWidget *widget)
{
  GList *tmp_list;
  GtkLayout *layout;

  g_return_if_fail (GTK_IS_LAYOUT (widget));

  layout = GTK_LAYOUT (widget);

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

  tmp_list = layout->children;
  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
928
      tmp_list = tmp_list->next;
929

930
      if (GTK_WIDGET_VISIBLE (child->widget))
931
	{
932
	  if (!GTK_WIDGET_MAPPED (child->widget))
933 934
	    gtk_widget_map (child->widget);
	}
935
    }
936 937 938

  gdk_window_show (layout->bin_window);
  gdk_window_show (widget->window);
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
}

static void 
gtk_layout_unrealize (GtkWidget *widget)
{
  GtkLayout *layout;

  g_return_if_fail (GTK_IS_LAYOUT (widget));

  layout = GTK_LAYOUT (widget);

  gdk_window_set_user_data (layout->bin_window, NULL);
  gdk_window_destroy (layout->bin_window);
  layout->bin_window = NULL;

954 955
  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
956 957 958 959 960 961 962 963 964 965 966 967 968
}

static void     
gtk_layout_size_request (GtkWidget     *widget,
			 GtkRequisition *requisition)
{
  GList *tmp_list;
  GtkLayout *layout;

  g_return_if_fail (GTK_IS_LAYOUT (widget));

  layout = GTK_LAYOUT (widget);

969 970 971
  requisition->width = 0;
  requisition->height = 0;

972 973 974 975 976
  tmp_list = layout->children;

  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
977 978
      GtkRequisition child_requisition;
      
979
      tmp_list = tmp_list->next;
980

981
      gtk_widget_size_request (child->widget, &child_requisition);
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    }
}

static void     
gtk_layout_size_allocate (GtkWidget     *widget,
			  GtkAllocation *allocation)
{
  GList *tmp_list;
  GtkLayout *layout;

  g_return_if_fail (GTK_IS_LAYOUT (widget));

  widget->allocation = *allocation;
  
  layout = GTK_LAYOUT (widget);

  tmp_list = layout->children;

  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
      tmp_list = tmp_list->next;
1004 1005

      gtk_layout_allocate_child (layout, child);
1006 1007 1008 1009 1010 1011 1012
    }

  if (GTK_WIDGET_REALIZED (widget))
    {
      gdk_window_move_resize (widget->window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);
1013 1014 1015 1016

      gdk_window_resize (layout->bin_window,
			 MAX (layout->width, allocation->width),
			 MAX (layout->height, allocation->height));
1017 1018 1019
    }

  layout->hadjustment->page_size = allocation->width;
1020
  layout->hadjustment->page_increment = allocation->width * 0.9;
1021
  layout->hadjustment->lower = 0;
1022 1023
  /* set_adjustment_upper() emits ::changed */
  gtk_layout_set_adjustment_upper (layout->hadjustment, MAX (allocation->width, layout->width), TRUE);
1024 1025

  layout->vadjustment->page_size = allocation->height;
1026
  layout->vadjustment->page_increment = allocation->height * 0.9;
1027
  layout->vadjustment->lower = 0;
1028
  layout->vadjustment->upper = MAX (allocation->height, layout->height);
1029
  gtk_layout_set_adjustment_upper (layout->vadjustment, MAX (allocation->height, layout->height), TRUE);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
}

static gint 
gtk_layout_expose (GtkWidget *widget, GdkEventExpose *event)
{
  GtkLayout *layout;

  g_return_val_if_fail (GTK_IS_LAYOUT (widget), FALSE);

  layout = GTK_LAYOUT (widget);

1041
  if (event->window != layout->bin_window)
1042 1043
    return FALSE;
  
1044
  (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

  return FALSE;
}

/* Container method
 */
static void
gtk_layout_remove (GtkContainer *container, 
		   GtkWidget    *widget)
{
  GList *tmp_list;
  GtkLayout *layout;
1057
  GtkLayoutChild *child = NULL;
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  
  g_return_if_fail (GTK_IS_LAYOUT (container));
  
  layout = GTK_LAYOUT (container);

  tmp_list = layout->children;
  while (tmp_list)
    {
      child = tmp_list->data;
      if (child->widget == widget)
	break;
      tmp_list = tmp_list->next;
    }

  if (tmp_list)
    {
      gtk_widget_unparent (widget);

      layout->children = g_list_remove_link (layout->children, tmp_list);
      g_list_free_1 (tmp_list);
      g_free (child);
    }
}

static void
gtk_layout_forall (GtkContainer *container,
		   gboolean      include_internals,
		   GtkCallback   callback,
		   gpointer      callback_data)
{
  GtkLayout *layout;
  GtkLayoutChild *child;
  GList *tmp_list;

  g_return_if_fail (GTK_IS_LAYOUT (container));
  g_return_if_fail (callback != NULL);

  layout = GTK_LAYOUT (container);

  tmp_list = layout->children;
  while (tmp_list)
    {
      child = tmp_list->data;
      tmp_list = tmp_list->next;

      (* callback) (child->widget, callback_data);
    }
}

/* Operations on children
 */

1110 1111 1112 1113 1114
static void
gtk_layout_allocate_child (GtkLayout      *layout,
			   GtkLayoutChild *child)
{
  GtkAllocation allocation;
1115
  GtkRequisition requisition;
1116

1117 1118
  allocation.x = child->x;
  allocation.y = child->y;
1119 1120 1121
  gtk_widget_get_child_requisition (child->widget, &requisition);
  allocation.width = requisition.width;
  allocation.height = requisition.height;
1122 1123 1124 1125
  
  gtk_widget_size_allocate (child->widget, &allocation);
}

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
/* Callbacks */

static void
gtk_layout_adjustment_changed (GtkAdjustment *adjustment,
			       GtkLayout     *layout)
{
  GtkWidget *widget;

  widget = GTK_WIDGET (layout);

1136
  if (layout->freeze_count)
1137 1138
    return;

1139
  if (GTK_WIDGET_REALIZED (layout))
1140
    {
1141 1142 1143
      gdk_window_move (layout->bin_window,
		       - layout->hadjustment->value,
		       - layout->vadjustment->value);
1144
      
1145
      gdk_window_process_updates (layout->bin_window, TRUE);
1146 1147
    }
}