gtklayout.c 32.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2
 * 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 *
 * GtkLayout: Widget for scrolling of arbitrary-sized areas.
 *
 * Copyright Owen Taylor, 1998
20 21
 */

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

29
#include "config.h"
30

31
#include "gtklayout.h"
32

33
#include "gdk/gdk.h"
34

35
#include "gtkadjustment.h"
Alexander Larsson's avatar
Alexander Larsson committed
36
#include "gtkintl.h"
37
#include "gtkmarshalers.h"
38
#include "gtkprivate.h"
39
#include "gtkscrollable.h"
40

41

42 43 44 45 46 47 48
/**
 * SECTION:gtklayout
 * @Short_description: Infinite scrollable area containing child widgets
 *   and/or custom drawing
 * @Title: GtkLayout
 * @See_also: #GtkDrawingArea, #GtkScrolledWindow
 *
49
 * #GtkLayout is similar to #GtkDrawingArea in that it’s a “blank slate”
50
 * and doesn’t do anything but paint a blank background by default. It's
51
 * different in that it supports scrolling natively (you can add it to a
52
 * #GtkScrolledWindow), and it can contain child widgets, since it’s a
53
 * #GtkContainer. However if you’re just going to draw, a #GtkDrawingArea
54 55 56 57 58 59 60 61 62
 * is a better choice since it has lower overhead.
 *
 * When handling expose events on a #GtkLayout, you must draw to
 * GTK_LAYOUT (layout)->bin_window, rather than to
 * GTK_WIDGET (layout)->window, as you would for a drawing
 * area.
 */


63 64
typedef struct _GtkLayoutChild   GtkLayoutChild;

65
struct _GtkLayoutPrivate
66 67 68 69 70 71 72
{
  /* Properties */
  guint width;
  guint height;

  GtkAdjustment *hadjustment;
  GtkAdjustment *vadjustment;
73

74 75 76 77 78
  /* GtkScrollablePolicy needs to be checked when
   * driving the scrollable adjustment values */
  guint hscroll_policy : 1;
  guint vscroll_policy : 1;

79 80 81 82 83 84 85 86 87 88 89 90 91
  /* Properties */

  GdkVisibilityState visibility;
  GdkWindow *bin_window;

  GList *children;

  gint scroll_x;
  gint scroll_y;

  guint freeze_count;
};

92 93 94 95 96 97
struct _GtkLayoutChild {
  GtkWidget *widget;
  gint x;
  gint y;
};

Alexander Larsson's avatar
Alexander Larsson committed
98 99 100
enum {
   PROP_0,
   PROP_HADJUSTMENT,
101
   PROP_VADJUSTMENT,
102 103
   PROP_HSCROLL_POLICY,
   PROP_VSCROLL_POLICY,
Alexander Larsson's avatar
Alexander Larsson committed
104 105 106 107
   PROP_WIDTH,
   PROP_HEIGHT
};

Havoc Pennington's avatar
Havoc Pennington committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
enum {
  CHILD_PROP_0,
  CHILD_PROP_X,
  CHILD_PROP_Y
};

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);
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);
126 127 128 129 130 131
static void gtk_layout_get_preferred_width  (GtkWidget     *widget,
                                             gint          *minimum,
                                             gint          *natural);
static void gtk_layout_get_preferred_height (GtkWidget     *widget,
                                             gint          *minimum,
                                             gint          *natural);
Havoc Pennington's avatar
Havoc Pennington committed
132 133
static void gtk_layout_size_allocate      (GtkWidget      *widget,
                                           GtkAllocation  *allocation);
134 135
static gint gtk_layout_draw               (GtkWidget      *widget,
                                           cairo_t        *cr);
136 137
static void gtk_layout_add                (GtkContainer   *container,
					   GtkWidget      *widget);
Havoc Pennington's avatar
Havoc Pennington committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
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_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);
158
static void gtk_layout_style_updated      (GtkWidget      *widget);
Havoc Pennington's avatar
Havoc Pennington committed
159

160 161
static void gtk_layout_set_hadjustment_values (GtkLayout      *layout);
static void gtk_layout_set_vadjustment_values (GtkLayout      *layout);
162

163
G_DEFINE_TYPE_WITH_CODE (GtkLayout, gtk_layout, GTK_TYPE_CONTAINER,
164
                         G_ADD_PRIVATE (GtkLayout)
165
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
166 167 168

/* Public interface
 */
169 170
/**
 * gtk_layout_new:
171 172
 * @hadjustment: (allow-none): horizontal scroll adjustment, or %NULL
 * @vadjustment: (allow-none): vertical scroll adjustment, or %NULL
173 174
 * 
 * Creates a new #GtkLayout. Unless you have a specific adjustment
175
 * you’d like the layout to use for scrolling, pass %NULL for
176 177
 * @hadjustment and @vadjustment.
 * 
178
 * Returns: a new #GtkLayout
179
 **/
180 181 182 183 184 185 186
  
GtkWidget*    
gtk_layout_new (GtkAdjustment *hadjustment,
		GtkAdjustment *vadjustment)
{
  GtkLayout *layout;

187 188 189 190
  layout = g_object_new (GTK_TYPE_LAYOUT,
			 "hadjustment", hadjustment,
			 "vadjustment", vadjustment,
			 NULL);
191 192 193 194

  return GTK_WIDGET (layout);
}

Tim Janik's avatar
Tim Janik committed
195 196 197
/**
 * gtk_layout_get_bin_window:
 * @layout: a #GtkLayout
198
 *
Tim Janik's avatar
Tim Janik committed
199
 * Retrieve the bin window of the layout used for drawing operations.
200
 *
201
 * Returns: (transfer none): a #GdkWindow
202 203
 *
 * Since: 2.14
Tim Janik's avatar
Tim Janik committed
204 205
 **/
GdkWindow*
206
gtk_layout_get_bin_window (GtkLayout *layout)
Tim Janik's avatar
Tim Janik committed
207 208 209
{
  g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);

210
  return layout->priv->bin_window;
Tim Janik's avatar
Tim Janik committed
211 212
}

213 214 215
/**
 * gtk_layout_get_hadjustment:
 * @layout: a #GtkLayout
216
 *
217 218 219 220 221 222
 * 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.
223
 *
224
 * Returns: (transfer none): horizontal scroll adjustment
225 226
 *
 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
227
 **/
228 229
GtkAdjustment*
gtk_layout_get_hadjustment (GtkLayout *layout)
230 231 232
{
  g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);

233
  return layout->priv->hadjustment;
234
}
235 236 237
/**
 * gtk_layout_get_vadjustment:
 * @layout: a #GtkLayout
238
 *
239 240 241 242 243 244
 * 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.
245
 *
246
 * Returns: (transfer none): vertical scroll adjustment
247 248
 *
 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
249
 **/
250 251
GtkAdjustment*
gtk_layout_get_vadjustment (GtkLayout *layout)
252 253 254
{
  g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);

255
  return layout->priv->vadjustment;
256 257
}

258 259
static void
gtk_layout_set_hadjustment_values (GtkLayout *layout)
260
{
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  GtkLayoutPrivate *priv = layout->priv;
  GtkAllocation  allocation;
  GtkAdjustment *adj = priv->hadjustment;
  gdouble old_value;
  gdouble new_value;
  gdouble new_upper;

  gtk_widget_get_allocation (GTK_WIDGET (layout), &allocation);

  old_value = gtk_adjustment_get_value (adj);
  new_upper = MAX (allocation.width, priv->width);

  g_object_set (adj,
                "lower", 0.0,
                "upper", new_upper,
                "page-size", (gdouble)allocation.width,
                "step-increment", allocation.width * 0.1,
                "page-increment", allocation.width * 0.9,
                NULL);

  new_value = CLAMP (old_value, 0, new_upper - allocation.width);
  if (new_value != old_value)
    gtk_adjustment_set_value (adj, new_value);
284 285
}

286 287
static void
gtk_layout_set_vadjustment_values (GtkLayout *layout)
288
{
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  GtkAllocation  allocation;
  GtkAdjustment *adj = layout->priv->vadjustment;
  gdouble old_value;
  gdouble new_value;
  gdouble new_upper;

  gtk_widget_get_allocation (GTK_WIDGET (layout), &allocation);

  old_value = gtk_adjustment_get_value (adj);
  new_upper = MAX (allocation.height, layout->priv->height);

  g_object_set (adj,
                "lower", 0.0,
                "upper", new_upper,
                "page-size", (gdouble)allocation.height,
                "step-increment", allocation.height * 0.1,
                "page-increment", allocation.height * 0.9,
                NULL);

  new_value = CLAMP (old_value, 0, new_upper - allocation.height);
  if (new_value != old_value)
    gtk_adjustment_set_value (adj, new_value);
311 312
}

313
static void
314
gtk_layout_finalize (GObject *object)
315 316
{
  GtkLayout *layout = GTK_LAYOUT (object);
317
  GtkLayoutPrivate *priv = layout->priv;
318

319 320
  g_object_unref (priv->hadjustment);
  g_object_unref (priv->vadjustment);
321

Matthias Clasen's avatar
Matthias Clasen committed
322
  G_OBJECT_CLASS (gtk_layout_parent_class)->finalize (object);
323 324
}

325 326 327
static void
gtk_layout_do_set_hadjustment (GtkLayout     *layout,
                               GtkAdjustment *adjustment)
328
{
329
  GtkLayoutPrivate *priv;
330 331 332

  priv = layout->priv;

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  if (adjustment && priv->hadjustment == adjustment)
        return;

  if (priv->hadjustment != NULL)
    {
      g_signal_handlers_disconnect_by_func (priv->hadjustment,
                                            gtk_layout_adjustment_changed,
                                            layout);
      g_object_unref (priv->hadjustment);
    }

  if (adjustment == NULL)
    adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);

  g_signal_connect (adjustment, "value-changed",
                    G_CALLBACK (gtk_layout_adjustment_changed), layout);
  priv->hadjustment = g_object_ref_sink (adjustment);
  gtk_layout_set_hadjustment_values (layout);

Alexander Larsson's avatar
Alexander Larsson committed
352
  g_object_notify (G_OBJECT (layout), "hadjustment");
353
}
354

355
/**
356
 * gtk_layout_set_hadjustment:
357
 * @layout: a #GtkLayout
358
 * @adjustment: (allow-none): new scroll adjustment
359
 *
360
 * Sets the horizontal scroll adjustment for the layout.
361 362
 *
 * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
363
 *
364
 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
365
 **/
366
void
367
gtk_layout_set_hadjustment (GtkLayout     *layout,
368
                            GtkAdjustment *adjustment)
369 370
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));
371
  g_return_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment));
372

373 374 375 376 377 378 379 380 381
  gtk_layout_do_set_hadjustment (layout, adjustment);
}

static void
gtk_layout_do_set_vadjustment (GtkLayout     *layout,
                               GtkAdjustment *adjustment)
{
  GtkLayoutPrivate *priv;

382 383
  priv = layout->priv;

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  if (adjustment && priv->vadjustment == adjustment)
        return;

  if (priv->vadjustment != NULL)
    {
      g_signal_handlers_disconnect_by_func (priv->vadjustment,
                                            gtk_layout_adjustment_changed,
                                            layout);
      g_object_unref (priv->vadjustment);
    }

  if (adjustment == NULL)
    adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);

  g_signal_connect (adjustment, "value-changed",
                    G_CALLBACK (gtk_layout_adjustment_changed), layout);
  priv->vadjustment = g_object_ref_sink (adjustment);
  gtk_layout_set_vadjustment_values (layout);

Alexander Larsson's avatar
Alexander Larsson committed
403
  g_object_notify (G_OBJECT (layout), "vadjustment");
404 405
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/**
 * gtk_layout_set_vadjustment:
 * @layout: a #GtkLayout
 * @adjustment: (allow-none): new scroll adjustment
 *
 * Sets the vertical scroll adjustment for the layout.
 *
 * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
 *
 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
 **/
void
gtk_layout_set_vadjustment (GtkLayout     *layout,
                            GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));
  g_return_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment));

  gtk_layout_do_set_vadjustment (layout, adjustment);
}

Havoc Pennington's avatar
Havoc Pennington committed
427 428 429 430
static GtkLayoutChild*
get_child (GtkLayout  *layout,
           GtkWidget  *widget)
{
431
  GtkLayoutPrivate *priv = layout->priv;
Havoc Pennington's avatar
Havoc Pennington committed
432
  GList *children;
433 434

  children = priv->children;
Havoc Pennington's avatar
Havoc Pennington committed
435 436 437 438 439 440 441 442 443 444 445 446 447
  while (children)
    {
      GtkLayoutChild *child;
      
      child = children->data;
      children = children->next;

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

  return NULL;
}
448

449 450 451 452 453 454 455 456 457 458 459
/**
 * 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.
 * 
 **/
460 461 462 463 464 465
void           
gtk_layout_put (GtkLayout     *layout, 
		GtkWidget     *child_widget, 
		gint           x, 
		gint           y)
{
466
  GtkLayoutPrivate *priv;
467 468 469
  GtkLayoutChild *child;

  g_return_if_fail (GTK_IS_LAYOUT (layout));
470
  g_return_if_fail (GTK_IS_WIDGET (child_widget));
471 472 473

  priv = layout->priv;

474 475 476 477 478 479
  child = g_new (GtkLayoutChild, 1);

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

480 481
  priv->children = g_list_append (priv->children, child);

482
  if (gtk_widget_get_realized (GTK_WIDGET (layout)))
483
    gtk_widget_set_parent_window (child->widget, priv->bin_window);
Havoc Pennington's avatar
Havoc Pennington committed
484

485
  gtk_widget_set_parent (child_widget, GTK_WIDGET (layout));
486 487
}

Havoc Pennington's avatar
Havoc Pennington committed
488 489 490 491 492 493 494
static void
gtk_layout_move_internal (GtkLayout       *layout,
                          GtkWidget       *widget,
                          gboolean         change_x,
                          gint             x,
                          gboolean         change_y,
                          gint             y)
495 496
{
  GtkLayoutChild *child;
497

Havoc Pennington's avatar
Havoc Pennington committed
498 499 500
  child = get_child (layout, widget);

  g_assert (child);
501

Havoc Pennington's avatar
Havoc Pennington committed
502 503 504
  gtk_widget_freeze_child_notify (widget);
  
  if (change_x)
505
    {
Havoc Pennington's avatar
Havoc Pennington committed
506 507 508
      child->x = x;
      gtk_widget_child_notify (widget, "x");
    }
509

Havoc Pennington's avatar
Havoc Pennington committed
510 511 512 513 514
  if (change_y)
    {
      child->y = y;
      gtk_widget_child_notify (widget, "y");
    }
515

Havoc Pennington's avatar
Havoc Pennington committed
516 517
  gtk_widget_thaw_child_notify (widget);
  
518 519 520
  if (gtk_widget_get_visible (widget) &&
      gtk_widget_get_visible (GTK_WIDGET (layout)))
    gtk_widget_queue_resize (widget);
Havoc Pennington's avatar
Havoc Pennington committed
521
}
522

523 524 525 526 527 528 529 530 531 532
/**
 * 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
533 534 535 536 537 538 539 540
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));
541
  g_return_if_fail (gtk_widget_get_parent (child_widget) == GTK_WIDGET (layout));
Havoc Pennington's avatar
Havoc Pennington committed
542 543

  gtk_layout_move_internal (layout, child_widget, TRUE, x, TRUE, y);
544 545
}

546 547 548 549 550 551 552 553 554
/**
 * 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.
 * 
 **/
555 556 557 558 559
void
gtk_layout_set_size (GtkLayout     *layout, 
		     guint          width,
		     guint          height)
{
560
  GtkLayoutPrivate *priv;
561
  GtkWidget *widget;
562

563
  g_return_if_fail (GTK_IS_LAYOUT (layout));
564 565

  priv = layout->priv;
566
  widget = GTK_WIDGET (layout);
567

568
  g_object_freeze_notify (G_OBJECT (layout));
569
  if (width != priv->width)
Alexander Larsson's avatar
Alexander Larsson committed
570
     {
571
	priv->width = width;
Alexander Larsson's avatar
Alexander Larsson committed
572 573
	g_object_notify (G_OBJECT (layout), "width");
     }
574
  if (height != priv->height)
Alexander Larsson's avatar
Alexander Larsson committed
575
     {
576
	priv->height = height;
Alexander Larsson's avatar
Alexander Larsson committed
577 578
	g_object_notify (G_OBJECT (layout), "height");
     }
579
  g_object_thaw_notify (G_OBJECT (layout));
580

581
  if (gtk_widget_get_realized (widget))
582
    {
583 584 585 586 587
      GtkAllocation allocation;

      gtk_widget_get_allocation (widget, &allocation);
      width = MAX (width, allocation.width);
      height = MAX (height, allocation.height);
588
      gdk_window_resize (priv->bin_window, width, height);
589
    }
590 591 592

  gtk_layout_set_hadjustment_values (layout);
  gtk_layout_set_vadjustment_values (layout);
593 594
}

595 596 597
/**
 * gtk_layout_get_size:
 * @layout: a #GtkLayout
598 599 600 601
 * @width: (out) (allow-none): location to store the width set on
 *     @layout, or %NULL
 * @height: (out) (allow-none): location to store the height set on
 *     @layout, or %NULL
602 603
 *
 * Gets the size that has been set on the layout, and that determines
604
 * the total extents of the layout’s scrollbar area. See
605 606 607 608 609 610 611
 * gtk_layout_set_size ().
 **/
void
gtk_layout_get_size (GtkLayout *layout,
		     guint     *width,
		     guint     *height)
{
612
  GtkLayoutPrivate *priv;
613

614 615
  g_return_if_fail (GTK_IS_LAYOUT (layout));

616 617
  priv = layout->priv;

618
  if (width)
619
    *width = priv->width;
620
  if (height)
621
    *height = priv->height;
622 623
}

624 625 626 627 628
/* Basic Object handling procedures
 */
static void
gtk_layout_class_init (GtkLayoutClass *class)
{
Alexander Larsson's avatar
Alexander Larsson committed
629
  GObjectClass *gobject_class;
630 631 632
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

Alexander Larsson's avatar
Alexander Larsson committed
633
  gobject_class = (GObjectClass*) class;
634 635 636
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;

Alexander Larsson's avatar
Alexander Larsson committed
637 638
  gobject_class->set_property = gtk_layout_set_property;
  gobject_class->get_property = gtk_layout_get_property;
639
  gobject_class->finalize = gtk_layout_finalize;
640

Havoc Pennington's avatar
Havoc Pennington committed
641 642 643 644 645 646
  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",
647 648
                                                                P_("X position"),
                                                                P_("X position of child widget"),
Havoc Pennington's avatar
Havoc Pennington committed
649 650 651
                                                                G_MININT,
                                                                G_MAXINT,
                                                                0,
652
                                                                GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
653 654 655 656

  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_Y,
					      g_param_spec_int ("y",
657 658
                                                                P_("Y position"),
                                                                P_("Y position of child widget"),
Havoc Pennington's avatar
Havoc Pennington committed
659 660 661
                                                                G_MININT,
                                                                G_MAXINT,
                                                                0,
662
                                                                GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
663
  
664
  /* Scrollable interface */
665 666 667 668
  g_object_class_override_property (gobject_class, PROP_HADJUSTMENT,    "hadjustment");
  g_object_class_override_property (gobject_class, PROP_VADJUSTMENT,    "vadjustment");
  g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
  g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
Alexander Larsson's avatar
Alexander Larsson committed
669 670 671 672

  g_object_class_install_property (gobject_class,
				   PROP_WIDTH,
				   g_param_spec_uint ("width",
673 674
						     P_("Width"),
						     P_("The width of the layout"),
Alexander Larsson's avatar
Alexander Larsson committed
675 676 677
						     0,
						     G_MAXINT,
						     100,
678
						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
Alexander Larsson's avatar
Alexander Larsson committed
679 680 681
  g_object_class_install_property (gobject_class,
				   PROP_HEIGHT,
				   g_param_spec_uint ("height",
682 683
						     P_("Height"),
						     P_("The height of the layout"),
Alexander Larsson's avatar
Alexander Larsson committed
684 685 686
						     0,
						     G_MAXINT,
						     100,
687
						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
688 689 690
  widget_class->realize = gtk_layout_realize;
  widget_class->unrealize = gtk_layout_unrealize;
  widget_class->map = gtk_layout_map;
691 692
  widget_class->get_preferred_width = gtk_layout_get_preferred_width;
  widget_class->get_preferred_height = gtk_layout_get_preferred_height;
693
  widget_class->size_allocate = gtk_layout_size_allocate;
694
  widget_class->draw = gtk_layout_draw;
695
  widget_class->style_updated = gtk_layout_style_updated;
696

697
  container_class->add = gtk_layout_add;
698 699
  container_class->remove = gtk_layout_remove;
  container_class->forall = gtk_layout_forall;
700 701
}

Alexander Larsson's avatar
Alexander Larsson committed
702 703 704 705 706 707 708
static void
gtk_layout_get_property (GObject     *object,
			 guint        prop_id,
			 GValue      *value,
			 GParamSpec  *pspec)
{
  GtkLayout *layout = GTK_LAYOUT (object);
709
  GtkLayoutPrivate *priv = layout->priv;
710

Alexander Larsson's avatar
Alexander Larsson committed
711 712 713
  switch (prop_id)
    {
    case PROP_HADJUSTMENT:
714
      g_value_set_object (value, priv->hadjustment);
Alexander Larsson's avatar
Alexander Larsson committed
715 716
      break;
    case PROP_VADJUSTMENT:
717
      g_value_set_object (value, priv->vadjustment);
Alexander Larsson's avatar
Alexander Larsson committed
718
      break;
719 720 721 722 723 724
    case PROP_HSCROLL_POLICY:
      g_value_set_enum (value, priv->hscroll_policy);
      break;
    case PROP_VSCROLL_POLICY:
      g_value_set_enum (value, priv->vscroll_policy);
      break;
Alexander Larsson's avatar
Alexander Larsson committed
725
    case PROP_WIDTH:
726
      g_value_set_uint (value, priv->width);
Alexander Larsson's avatar
Alexander Larsson committed
727 728
      break;
    case PROP_HEIGHT:
729
      g_value_set_uint (value, priv->height);
Alexander Larsson's avatar
Alexander Larsson committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743
      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);
744
  GtkLayoutPrivate *priv = layout->priv;
745

Alexander Larsson's avatar
Alexander Larsson committed
746 747 748
  switch (prop_id)
    {
    case PROP_HADJUSTMENT:
749
      gtk_layout_do_set_hadjustment (layout, g_value_get_object (value));
Alexander Larsson's avatar
Alexander Larsson committed
750 751
      break;
    case PROP_VADJUSTMENT:
752
      gtk_layout_do_set_vadjustment (layout, g_value_get_object (value));
Alexander Larsson's avatar
Alexander Larsson committed
753
      break;
754
    case PROP_HSCROLL_POLICY:
755 756 757 758 759 760
      if (priv->hscroll_policy != g_value_get_enum (value))
        {
          priv->hscroll_policy = g_value_get_enum (value);
          gtk_widget_queue_resize (GTK_WIDGET (layout));
          g_object_notify_by_pspec (object, pspec);
        }
761 762
      break;
    case PROP_VSCROLL_POLICY:
763 764 765 766 767 768
      if (priv->vscroll_policy != g_value_get_enum (value))
        {
          priv->vscroll_policy = g_value_get_enum (value);
          gtk_widget_queue_resize (GTK_WIDGET (layout));
          g_object_notify_by_pspec (object, pspec);
        }
769
      break;
Alexander Larsson's avatar
Alexander Larsson committed
770
    case PROP_WIDTH:
771
      gtk_layout_set_size (layout, g_value_get_uint (value), priv->height);
Alexander Larsson's avatar
Alexander Larsson committed
772 773
      break;
    case PROP_HEIGHT:
774
      gtk_layout_set_size (layout, priv->width, g_value_get_uint (value));
Alexander Larsson's avatar
Alexander Larsson committed
775 776 777 778 779 780 781
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
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 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
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
833

834 835 836
static void
gtk_layout_init (GtkLayout *layout)
{
837
  GtkLayoutPrivate *priv;
838

839
  layout->priv = gtk_layout_get_instance_private (layout);
840
  priv = layout->priv;
841

842
  priv->children = NULL;
843

844 845
  priv->width = 100;
  priv->height = 100;
846

847 848
  priv->hadjustment = NULL;
  priv->vadjustment = NULL;
849

850 851 852 853 854 855 856
  priv->bin_window = NULL;

  priv->scroll_x = 0;
  priv->scroll_y = 0;
  priv->visibility = GDK_VISIBILITY_PARTIAL;

  priv->freeze_count = 0;
857 858 859 860
}

/* Widget methods
 */
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
static void
set_background (GtkWidget *widget)
{
  GtkLayoutPrivate *priv;

  if (gtk_widget_get_realized (widget))
    {
      priv = GTK_LAYOUT (widget)->priv;

      /* We still need to call gtk_style_context_set_background() here for
       * GtkLayout, since subclasses like EelCanvas depend on a background to
       * be set since the beginning of the draw() implementation.
       * This should be revisited next time we have a major API break.
       */
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_style_context_set_background (gtk_widget_get_style_context (widget), priv->bin_window);
      G_GNUC_END_IGNORE_DEPRECATIONS;
    }
}
880 881 882 883

static void 
gtk_layout_realize (GtkWidget *widget)
{
884
  GtkLayout *layout = GTK_LAYOUT (widget);
885
  GtkLayoutPrivate *priv = layout->priv;
886 887
  GtkAllocation allocation;
  GdkWindow *window;
888
  GdkWindowAttr attributes;
889
  GList *tmp_list;
890 891
  gint attributes_mask;

892
  gtk_widget_set_realized (widget, TRUE);
893

894 895
  gtk_widget_get_allocation (widget, &allocation);

896
  attributes.window_type = GDK_WINDOW_CHILD;
897 898 899 900
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
901 902 903 904
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;

905
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
906

907 908 909
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
910
  gtk_widget_register_window (widget, window);
911 912

  gtk_widget_get_allocation (widget, &allocation);
913

914 915
  attributes.x = - gtk_adjustment_get_value (priv->hadjustment),
  attributes.y = - gtk_adjustment_get_value (priv->vadjustment);
916 917
  attributes.width = MAX (priv->width, allocation.width);
  attributes.height = MAX (priv->height, allocation.height);
918 919
  attributes.event_mask = GDK_EXPOSURE_MASK | GDK_SCROLL_MASK |
                          GDK_SMOOTH_SCROLL_MASK | 
920
                          gtk_widget_get_events (widget);
921

922 923
  priv->bin_window = gdk_window_new (window,
                                     &attributes, attributes_mask);
924
  gtk_widget_register_window (widget, priv->bin_window);
925
  set_background (widget);
926

927
  tmp_list = priv->children;
928 929 930 931
  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
      tmp_list = tmp_list->next;
932

933
      gtk_widget_set_parent_window (child->widget, priv->bin_window);
934 935 936
    }
}

937
static void
938
gtk_layout_style_updated (GtkWidget *widget)
939
{
940
  GTK_WIDGET_CLASS (gtk_layout_parent_class)->style_updated (widget);
941

942
  set_background (widget);
943 944
}

945
static void
946 947
gtk_layout_map (GtkWidget *widget)
{
948
  GtkLayout *layout = GTK_LAYOUT (widget);
949
  GtkLayoutPrivate *priv = layout->priv;
950 951
  GList *tmp_list;

952
  gtk_widget_set_mapped (widget, TRUE);
953

954
  tmp_list = priv->children;
955 956 957
  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
958
      tmp_list = tmp_list->next;
959

960
      if (gtk_widget_get_visible (child->widget))
961
	{
962
	  if (!gtk_widget_get_mapped (child->widget))
963 964
	    gtk_widget_map (child->widget);
	}
965
    }
966

967
  gdk_window_show (priv->bin_window);
968
  gdk_window_show (gtk_widget_get_window (widget));
969 970 971 972 973
}

static void 
gtk_layout_unrealize (GtkWidget *widget)
{
974
  GtkLayout *layout = GTK_LAYOUT (widget);
975
  GtkLayoutPrivate *priv = layout->priv;
976

977
  gtk_widget_unregister_window (widget, priv->bin_window);
978 979
  gdk_window_destroy (priv->bin_window);
  priv->bin_window = NULL;
980

981
  GTK_WIDGET_CLASS (gtk_layout_parent_class)->unrealize (widget);
982 983
}

984 985 986 987 988 989 990 991 992 993 994 995
static void
gtk_layout_get_preferred_width (GtkWidget *widget,
                                gint      *minimum,
                                gint      *natural)
{
  *minimum = *natural = 0;
}

static void
gtk_layout_get_preferred_height (GtkWidget *widget,
                                 gint      *minimum,
                                 gint      *natural)
996
{
997
  *minimum = *natural = 0;
998 999
}

1000
static void
1001 1002 1003
gtk_layout_size_allocate (GtkWidget     *widget,
			  GtkAllocation *allocation)
{
1004
  GtkLayout *layout = GTK_LAYOUT (widget);
1005
  GtkLayoutPrivate *priv = layout->priv;
1006 1007
  GList *tmp_list;

1008
  gtk_widget_set_allocation (widget, allocation);
1009

1010
  tmp_list = priv->children;
1011 1012 1013 1014 1015

  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
      tmp_list = tmp_list->next;
1016 1017

      gtk_layout_allocate_child (layout, child);
1018 1019
    }

1020
  if (gtk_widget_get_realized (widget))
1021
    {
1022
      gdk_window_move_resize (gtk_widget_get_window (widget),
1023 1024
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);
1025

1026 1027 1028
      gdk_window_resize (priv->bin_window,
			 MAX (priv->width, allocation->width),
			 MAX (priv->height, allocation->height));
1029 1030
    }

1031 1032
  gtk_layout_set_hadjustment_values (layout);
  gtk_layout_set_vadjustment_values (layout);
1033 1034
}

1035 1036 1037
static gboolean
gtk_layout_draw (GtkWidget *widget,
                 cairo_t   *cr)
1038
{
1039
  GtkLayout *layout = GTK_LAYOUT (widget);
1040
  GtkLayoutPrivate *priv = layout->priv;
1041

1042 1043
  if (gtk_cairo_should_draw_window (cr, priv->bin_window))
    GTK_WIDGET_CLASS (gtk_layout_parent_class)->draw (widget, cr);
1044 1045 1046 1047

  return FALSE;
}

1048
/* Container methods
1049 1050
 */
static void
1051 1052 1053 1054 1055 1056 1057
gtk_layout_add (GtkContainer *container,
		GtkWidget    *widget)
{
  gtk_layout_put (GTK_LAYOUT (container), widget, 0, 0);
}

static void
1058 1059 1060
gtk_layout_remove (GtkContainer *container, 
		   GtkWidget    *widget)
{
1061
  GtkLayout *layout = GTK_LAYOUT (container);
1062
  GtkLayoutPrivate *priv = layout->priv;
1063
  GList *tmp_list;
1064
  GtkLayoutChild *child = NULL;
1065

1066
  tmp_list = priv->children;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
  while (tmp_list)
    {
      child = tmp_list->data;
      if (child->widget == widget)
	break;
      tmp_list = tmp_list->next;
    }

  if (tmp_list)
    {
      gtk_widget_unparent (widget);

1079
      priv->children = g_list_remove_link (priv->children, tmp_list);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
      g_list_free_1 (tmp_list);
      g_free (child);
    }
}

static void
gtk_layout_forall (GtkContainer *container,
		   gboolean      include_internals,
		   GtkCallback   callback,
		   gpointer      callback_data)
{
1091
  GtkLayout *layout = GTK_LAYOUT (container);
1092
  GtkLayoutPrivate *priv = layout->priv;
1093 1094 1095
  GtkLayoutChild *child;
  GList *tmp_list;

1096
  tmp_list = priv->children;
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
  while (tmp_list)
    {
      child = tmp_list->data;
      tmp_list = tmp_list->next;

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

/* Operations on children
 */

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

1116 1117
  allocation.x = child->x;
  allocation.y = child->y;
1118

1119
  gtk_widget_get_preferred_size (child->widget, &requisition, NULL);
1120 1121
  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
/* Callbacks */

static void
gtk_layout_adjustment_changed (GtkAdjustment *adjustment,
			       GtkLayout     *layout)
{
1132
  GtkLayoutPrivate *priv = layout->priv;
1133 1134

  if (priv->freeze_count)
1135 1136
    return;

1137
  if (gtk_widget_get_realized (GTK_WIDGET (layout)))
1138
    {
1139
      gdk_window_move (priv->bin_window,
1140 1141
		       - gtk_adjustment_get_value (priv->hadjustment),
		       - gtk_adjustment_get_value (priv->vadjustment));
1142 1143
    }
}