gtklayout.c 31 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
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
#include "config.h"
32 33
#include "gdkconfig.h"

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

40 41 42 43 44 45 46 47
typedef struct _GtkLayoutChild   GtkLayoutChild;

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

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

Havoc Pennington's avatar
Havoc Pennington committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
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);
70 71 72
static GObject *gtk_layout_constructor    (GType                  type,
					   guint                  n_properties,
					   GObjectConstructParam *properties);
Havoc Pennington's avatar
Havoc Pennington committed
73 74 75 76 77 78 79 80 81 82
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);
83 84
static void gtk_layout_add                (GtkContainer   *container,
					   GtkWidget      *widget);
Havoc Pennington's avatar
Havoc Pennington committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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);
108 109
static void gtk_layout_style_set          (GtkWidget      *widget,
					   GtkStyle       *old_style);
Havoc Pennington's avatar
Havoc Pennington committed
110

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

Matthias Clasen's avatar
Matthias Clasen committed
115
G_DEFINE_TYPE (GtkLayout, gtk_layout, GTK_TYPE_CONTAINER)
116 117 118

/* Public interface
 */
119 120
/**
 * gtk_layout_new:
121 122
 * @hadjustment: (allow-none): horizontal scroll adjustment, or %NULL
 * @vadjustment: (allow-none): vertical scroll adjustment, or %NULL
123 124 125 126 127 128 129
 * 
 * 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
 **/
130 131 132 133 134 135 136
  
GtkWidget*    
gtk_layout_new (GtkAdjustment *hadjustment,
		GtkAdjustment *vadjustment)
{
  GtkLayout *layout;

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

  return GTK_WIDGET (layout);
}

Tim Janik's avatar
Tim Janik committed
145 146 147 148 149 150 151
/**
 * gtk_layout_get_bin_window:
 * @layout: a #GtkLayout
 * 
 * Retrieve the bin window of the layout used for drawing operations.
 * 
 * Return value: a #GdkWindow
152 153
 *
 * Since: 2.14
Tim Janik's avatar
Tim Janik committed
154 155
 **/
GdkWindow*
156
gtk_layout_get_bin_window (GtkLayout *layout)
Tim Janik's avatar
Tim Janik committed
157 158 159 160 161 162
{
  g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);

  return layout->bin_window;
}

163 164 165 166 167 168 169 170 171 172 173 174 175
/**
 * 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
 **/
176 177 178 179 180 181 182
GtkAdjustment* 
gtk_layout_get_hadjustment (GtkLayout     *layout)
{
  g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);

  return layout->hadjustment;
}
183 184 185 186 187 188 189 190 191 192 193 194 195
/**
 * 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
 **/
196 197 198 199 200 201 202 203
GtkAdjustment* 
gtk_layout_get_vadjustment (GtkLayout     *layout)
{
  g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);

  return layout->vadjustment;
}

204 205 206 207 208 209
static GtkAdjustment *
new_default_adjustment (void)
{
  return GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
}

210 211 212 213 214
static void           
gtk_layout_set_adjustments (GtkLayout     *layout,
			    GtkAdjustment *hadj,
			    GtkAdjustment *vadj)
{
215 216
  gboolean need_adjust = FALSE;

217 218 219 220
  g_return_if_fail (GTK_IS_LAYOUT (layout));

  if (hadj)
    g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
221 222
  else if (layout->hadjustment)
    hadj = new_default_adjustment ();
223 224
  if (vadj)
    g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
225 226
  else if (layout->vadjustment)
    vadj = new_default_adjustment ();
227 228 229
  
  if (layout->hadjustment && (layout->hadjustment != hadj))
    {
Manish Singh's avatar
Manish Singh committed
230 231 232 233
      g_signal_handlers_disconnect_by_func (layout->hadjustment,
					    gtk_layout_adjustment_changed,
					    layout);
      g_object_unref (layout->hadjustment);
234 235 236 237
    }
  
  if (layout->vadjustment && (layout->vadjustment != vadj))
    {
Manish Singh's avatar
Manish Singh committed
238 239 240 241
      g_signal_handlers_disconnect_by_func (layout->vadjustment,
					    gtk_layout_adjustment_changed,
					    layout);
      g_object_unref (layout->vadjustment);
242 243 244 245 246
    }
  
  if (layout->hadjustment != hadj)
    {
      layout->hadjustment = hadj;
247
      g_object_ref_sink (layout->hadjustment);
248
      gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width, FALSE);
249
      
250
      g_signal_connect (layout->hadjustment, "value-changed",
Manish Singh's avatar
Manish Singh committed
251 252
			G_CALLBACK (gtk_layout_adjustment_changed),
			layout);
253
      need_adjust = TRUE;
254 255 256 257 258
    }
  
  if (layout->vadjustment != vadj)
    {
      layout->vadjustment = vadj;
259
      g_object_ref_sink (layout->vadjustment);
260
      gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height, FALSE);
261
      
262
      g_signal_connect (layout->vadjustment, "value-changed",
Manish Singh's avatar
Manish Singh committed
263 264
			G_CALLBACK (gtk_layout_adjustment_changed),
			layout);
265
      need_adjust = TRUE;
266
    }
267

268 269 270
  /* vadj or hadj can be NULL while constructing; don't emit a signal
     then */
  if (need_adjust && vadj && hadj)
271
    gtk_layout_adjustment_changed (NULL, layout);
272 273
}

274
static void
275
gtk_layout_finalize (GObject *object)
276 277 278
{
  GtkLayout *layout = GTK_LAYOUT (object);

Manish Singh's avatar
Manish Singh committed
279 280
  g_object_unref (layout->hadjustment);
  g_object_unref (layout->vadjustment);
281

Matthias Clasen's avatar
Matthias Clasen committed
282
  G_OBJECT_CLASS (gtk_layout_parent_class)->finalize (object);
283 284
}

285 286 287
/**
 * gtk_layout_set_hadjustment:
 * @layout: a #GtkLayout
288
 * @adjustment: (allow-none): new scroll adjustment
289 290 291 292 293 294
 *
 * Sets the horizontal scroll adjustment for the layout.
 *
 * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
 * 
 **/
295 296 297 298 299 300
void           
gtk_layout_set_hadjustment (GtkLayout     *layout,
			    GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));

301
  gtk_layout_set_adjustments (layout, adjustment, layout->vadjustment);
Alexander Larsson's avatar
Alexander Larsson committed
302
  g_object_notify (G_OBJECT (layout), "hadjustment");
303 304
}
 
305 306 307
/**
 * gtk_layout_set_vadjustment:
 * @layout: a #GtkLayout
308
 * @adjustment: (allow-none): new scroll adjustment
309 310 311 312 313 314
 *
 * Sets the vertical scroll adjustment for the layout.
 *
 * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
 * 
 **/
315 316 317 318 319 320
void           
gtk_layout_set_vadjustment (GtkLayout     *layout,
			    GtkAdjustment *adjustment)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));
  
321
  gtk_layout_set_adjustments (layout, layout->hadjustment, adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
322
  g_object_notify (G_OBJECT (layout), "vadjustment");
323 324
}

Havoc Pennington's avatar
Havoc Pennington committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
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;
}
345

346 347 348 349 350 351 352 353 354 355 356
/**
 * 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.
 * 
 **/
357 358 359 360 361 362 363 364 365
void           
gtk_layout_put (GtkLayout     *layout, 
		GtkWidget     *child_widget, 
		gint           x, 
		gint           y)
{
  GtkLayoutChild *child;

  g_return_if_fail (GTK_IS_LAYOUT (layout));
366
  g_return_if_fail (GTK_IS_WIDGET (child_widget));
367 368 369 370 371 372 373 374 375
  
  child = g_new (GtkLayoutChild, 1);

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

  layout->children = g_list_append (layout->children, child);
  
376
  if (gtk_widget_get_realized (GTK_WIDGET (layout)))
377
    gtk_widget_set_parent_window (child->widget, layout->bin_window);
Havoc Pennington's avatar
Havoc Pennington committed
378

379
  gtk_widget_set_parent (child_widget, GTK_WIDGET (layout));
380 381
}

Havoc Pennington's avatar
Havoc Pennington committed
382 383 384 385 386 387 388
static void
gtk_layout_move_internal (GtkLayout       *layout,
                          GtkWidget       *widget,
                          gboolean         change_x,
                          gint             x,
                          gboolean         change_y,
                          gint             y)
389 390
{
  GtkLayoutChild *child;
391

Havoc Pennington's avatar
Havoc Pennington committed
392 393 394
  child = get_child (layout, widget);

  g_assert (child);
395

Havoc Pennington's avatar
Havoc Pennington committed
396 397 398
  gtk_widget_freeze_child_notify (widget);
  
  if (change_x)
399
    {
Havoc Pennington's avatar
Havoc Pennington committed
400 401 402
      child->x = x;
      gtk_widget_child_notify (widget, "x");
    }
403

Havoc Pennington's avatar
Havoc Pennington committed
404 405 406 407 408
  if (change_y)
    {
      child->y = y;
      gtk_widget_child_notify (widget, "y");
    }
409

Havoc Pennington's avatar
Havoc Pennington committed
410 411
  gtk_widget_thaw_child_notify (widget);
  
412 413 414
  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
415
}
416

417 418 419 420 421 422 423 424 425 426
/**
 * 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
427 428 429 430 431 432 433 434 435 436 437
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);
438 439
}

440
static void
441
gtk_layout_set_adjustment_upper (GtkAdjustment *adj,
442 443
				 gdouble        upper,
				 gboolean       always_emit_changed)
444
{
445 446 447 448 449
  gboolean changed = FALSE;
  gboolean value_changed = FALSE;
  
  gdouble min = MAX (0., upper - adj->page_size);

450 451 452
  if (upper != adj->upper)
    {
      adj->upper = upper;
453 454
      changed = TRUE;
    }
455
      
456 457 458 459
  if (adj->value > min)
    {
      adj->value = min;
      value_changed = TRUE;
460
    }
461 462
  
  if (changed || always_emit_changed)
Manish Singh's avatar
Manish Singh committed
463
    gtk_adjustment_changed (adj);
464
  if (value_changed)
Manish Singh's avatar
Manish Singh committed
465
    gtk_adjustment_value_changed (adj);
466 467
}

468 469 470 471 472 473 474 475 476
/**
 * 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.
 * 
 **/
477 478 479 480 481
void
gtk_layout_set_size (GtkLayout     *layout, 
		     guint          width,
		     guint          height)
{
482 483
  GtkWidget *widget;
  
484
  g_return_if_fail (GTK_IS_LAYOUT (layout));
485
  
486 487
  widget = GTK_WIDGET (layout);
  
488
  g_object_freeze_notify (G_OBJECT (layout));
Alexander Larsson's avatar
Alexander Larsson committed
489 490 491 492 493 494 495 496 497 498
  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");
     }
499
  g_object_thaw_notify (G_OBJECT (layout));
500

501
  if (layout->hadjustment)
502
    gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width, FALSE);
503
  if (layout->vadjustment)
504
    gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height, FALSE);
505

506
  if (gtk_widget_get_realized (widget))
507 508 509 510 511
    {
      width = MAX (width, widget->allocation.width);
      height = MAX (height, widget->allocation.height);
      gdk_window_resize (layout->bin_window, width, height);
    }
512 513
}

514 515 516
/**
 * gtk_layout_get_size:
 * @layout: a #GtkLayout
517 518
 * @width: (allow-none): location to store the width set on @layout, or %NULL
 * @height: (allow-none): location to store the height set on @layout, or %NULL
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
 *
 * 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;
}

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

548
  layout->freeze_count++;
549 550
}

551 552 553 554 555 556
/**
 * gtk_layout_thaw:
 * @layout: a #GtkLayout
 * 
 * This is a deprecated function, it doesn't do anything useful.
 **/
557 558 559 560 561
void
gtk_layout_thaw (GtkLayout *layout)
{
  g_return_if_fail (GTK_IS_LAYOUT (layout));

562
  if (layout->freeze_count)
Manish Singh's avatar
Manish Singh committed
563 564 565 566 567 568 569 570
    {
      if (!(--layout->freeze_count))
	{
	  gtk_widget_queue_draw (GTK_WIDGET (layout));
	  gdk_window_process_updates (GTK_WIDGET (layout)->window, TRUE);
	}
    }

571 572 573 574 575 576 577
}

/* Basic Object handling procedures
 */
static void
gtk_layout_class_init (GtkLayoutClass *class)
{
Alexander Larsson's avatar
Alexander Larsson committed
578
  GObjectClass *gobject_class;
579 580 581
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

Alexander Larsson's avatar
Alexander Larsson committed
582
  gobject_class = (GObjectClass*) class;
583 584 585
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;

Alexander Larsson's avatar
Alexander Larsson committed
586 587
  gobject_class->set_property = gtk_layout_set_property;
  gobject_class->get_property = gtk_layout_get_property;
588
  gobject_class->finalize = gtk_layout_finalize;
589
  gobject_class->constructor = gtk_layout_constructor;
590

Havoc Pennington's avatar
Havoc Pennington committed
591 592 593 594 595 596
  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",
597 598
                                                                P_("X position"),
                                                                P_("X position of child widget"),
Havoc Pennington's avatar
Havoc Pennington committed
599 600 601
                                                                G_MININT,
                                                                G_MAXINT,
                                                                0,
602
                                                                GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
603 604 605 606

  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_Y,
					      g_param_spec_int ("y",
607 608
                                                                P_("Y position"),
                                                                P_("Y position of child widget"),
Havoc Pennington's avatar
Havoc Pennington committed
609 610 611
                                                                G_MININT,
                                                                G_MAXINT,
                                                                0,
612
                                                                GTK_PARAM_READWRITE));
Havoc Pennington's avatar
Havoc Pennington committed
613
  
Alexander Larsson's avatar
Alexander Larsson committed
614 615 616
  g_object_class_install_property (gobject_class,
				   PROP_HADJUSTMENT,
				   g_param_spec_object ("hadjustment",
617 618
							P_("Horizontal adjustment"),
							P_("The GtkAdjustment for the horizontal position"),
Alexander Larsson's avatar
Alexander Larsson committed
619
							GTK_TYPE_ADJUSTMENT,
620
							GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
621 622 623 624
  
  g_object_class_install_property (gobject_class,
				   PROP_VADJUSTMENT,
				   g_param_spec_object ("vadjustment",
625 626
							P_("Vertical adjustment"),
							P_("The GtkAdjustment for the vertical position"),
Alexander Larsson's avatar
Alexander Larsson committed
627
							GTK_TYPE_ADJUSTMENT,
628
							GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
629 630 631 632

  g_object_class_install_property (gobject_class,
				   PROP_WIDTH,
				   g_param_spec_uint ("width",
633 634
						     P_("Width"),
						     P_("The width of the layout"),
Alexander Larsson's avatar
Alexander Larsson committed
635 636 637
						     0,
						     G_MAXINT,
						     100,
638
						     GTK_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
639 640 641
  g_object_class_install_property (gobject_class,
				   PROP_HEIGHT,
				   g_param_spec_uint ("height",
642 643
						     P_("Height"),
						     P_("The height of the layout"),
Alexander Larsson's avatar
Alexander Larsson committed
644 645 646
						     0,
						     G_MAXINT,
						     100,
647
						     GTK_PARAM_READWRITE));
648 649 650 651 652 653
  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;
654
  widget_class->style_set = gtk_layout_style_set;
655

656
  container_class->add = gtk_layout_add;
657 658 659 660 661
  container_class->remove = gtk_layout_remove;
  container_class->forall = gtk_layout_forall;

  class->set_scroll_adjustments = gtk_layout_set_adjustments;

662 663 664 665 666 667 668 669 670
  /**
   * GtkLayout::set-scroll-adjustments
   * @horizontal: the horizontal #GtkAdjustment
   * @vertical: the vertical #GtkAdjustment
   *
   * Set the scroll adjustments for the layout. Usually scrolled containers
   * like #GtkScrolledWindow will emit this signal to connect two instances
   * of #GtkScrollbar to the scroll directions of the #GtkLayout.
   */
671
  widget_class->set_scroll_adjustments_signal =
672
    g_signal_new (I_("set-scroll-adjustments"),
Manish Singh's avatar
Manish Singh committed
673 674 675 676 677 678 679 680
		  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);
681 682
}

Alexander Larsson's avatar
Alexander Larsson committed
683 684 685 686 687 688 689 690 691 692 693
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:
694
      g_value_set_object (value, layout->hadjustment);
Alexander Larsson's avatar
Alexander Larsson committed
695 696
      break;
    case PROP_VADJUSTMENT:
697
      g_value_set_object (value, layout->vadjustment);
Alexander Larsson's avatar
Alexander Larsson committed
698 699 700 701 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
      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
743 744 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
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
794

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
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;
811 812

  layout->freeze_count = 0;
813 814
}

815 816 817 818 819 820 821 822 823
static GObject *
gtk_layout_constructor (GType                  type,
			guint                  n_properties,
			GObjectConstructParam *properties)
{
  GtkLayout *layout;
  GObject *object;
  GtkAdjustment *hadj, *vadj;
  
Matthias Clasen's avatar
Matthias Clasen committed
824 825 826
  object = G_OBJECT_CLASS (gtk_layout_parent_class)->constructor (type,
								  n_properties,
								  properties);
827 828 829 830 831 832 833 834 835 836 837 838

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

839 840 841 842 843 844
/* Widget methods
 */

static void 
gtk_layout_realize (GtkWidget *widget)
{
845
  GtkLayout *layout = GTK_LAYOUT (widget);
846 847 848 849
  GList *tmp_list;
  GdkWindowAttr attributes;
  gint attributes_mask;

850
  gtk_widget_set_realized (widget, TRUE);
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865

  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);
866
  gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
867 868
  gdk_window_set_user_data (widget->window, widget);

869 870
  attributes.x = - layout->hadjustment->value,
  attributes.y = - layout->vadjustment->value;
871 872
  attributes.width = MAX (layout->width, widget->allocation.width);
  attributes.height = MAX (layout->height, widget->allocation.height);
873
  attributes.event_mask = GDK_EXPOSURE_MASK | GDK_SCROLL_MASK | 
874
                          gtk_widget_get_events (widget);
875 876 877 878 879 880 881 882 883 884 885 886 887

  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, layout->bin_window, GTK_STATE_NORMAL);

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

      gtk_widget_set_parent_window (child->widget, layout->bin_window);
890 891 892
    }
}

893
static void
894 895
gtk_layout_style_set (GtkWidget *widget,
                      GtkStyle  *old_style)
896
{
897
  GTK_WIDGET_CLASS (gtk_layout_parent_class)->style_set (widget, old_style);
898

899
  if (gtk_widget_get_realized (widget))
900 901 902 903 904
    {
      gtk_style_set_background (widget->style, GTK_LAYOUT (widget)->bin_window, GTK_STATE_NORMAL);
    }
}

905
static void
906 907
gtk_layout_map (GtkWidget *widget)
{
908
  GtkLayout *layout = GTK_LAYOUT (widget);
909 910 911 912 913 914 915 916
  GList *tmp_list;

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

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

919
      if (gtk_widget_get_visible (child->widget))
920
	{
921
	  if (!gtk_widget_get_mapped (child->widget))
922 923
	    gtk_widget_map (child->widget);
	}
924
    }
925 926 927

  gdk_window_show (layout->bin_window);
  gdk_window_show (widget->window);
928 929 930 931 932
}

static void 
gtk_layout_unrealize (GtkWidget *widget)
{
933
  GtkLayout *layout = GTK_LAYOUT (widget);
934 935 936 937 938

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

939
  GTK_WIDGET_CLASS (gtk_layout_parent_class)->unrealize (widget);
940 941 942 943 944 945
}

static void     
gtk_layout_size_request (GtkWidget     *widget,
			 GtkRequisition *requisition)
{
946
  GtkLayout *layout = GTK_LAYOUT (widget);
947 948
  GList *tmp_list;

949 950 951
  requisition->width = 0;
  requisition->height = 0;

952 953 954 955 956
  tmp_list = layout->children;

  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
957 958
      GtkRequisition child_requisition;
      
959
      tmp_list = tmp_list->next;
960

961
      gtk_widget_size_request (child->widget, &child_requisition);
962 963 964 965 966 967 968
    }
}

static void     
gtk_layout_size_allocate (GtkWidget     *widget,
			  GtkAllocation *allocation)
{
969
  GtkLayout *layout = GTK_LAYOUT (widget);
970 971 972 973 974 975 976 977 978 979
  GList *tmp_list;

  widget->allocation = *allocation;

  tmp_list = layout->children;

  while (tmp_list)
    {
      GtkLayoutChild *child = tmp_list->data;
      tmp_list = tmp_list->next;
980 981

      gtk_layout_allocate_child (layout, child);
982 983
    }

984
  if (gtk_widget_get_realized (widget))
985 986 987 988
    {
      gdk_window_move_resize (widget->window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);
989 990 991 992

      gdk_window_resize (layout->bin_window,
			 MAX (layout->width, allocation->width),
			 MAX (layout->height, allocation->height));
993 994 995
    }

  layout->hadjustment->page_size = allocation->width;
996
  layout->hadjustment->page_increment = allocation->width * 0.9;
997
  layout->hadjustment->lower = 0;
998 999
  /* set_adjustment_upper() emits ::changed */
  gtk_layout_set_adjustment_upper (layout->hadjustment, MAX (allocation->width, layout->width), TRUE);
1000 1001

  layout->vadjustment->page_size = allocation->height;
1002
  layout->vadjustment->page_increment = allocation->height * 0.9;
1003
  layout->vadjustment->lower = 0;
1004
  layout->vadjustment->upper = MAX (allocation->height, layout->height);
1005
  gtk_layout_set_adjustment_upper (layout->vadjustment, MAX (allocation->height, layout->height), TRUE);
1006 1007 1008
}

static gint 
1009 1010
gtk_layout_expose (GtkWidget      *widget,
                   GdkEventExpose *event)
1011
{
1012
  GtkLayout *layout = GTK_LAYOUT (widget);
1013

1014
  if (event->window != layout->bin_window)
1015
    return FALSE;
1016 1017

  GTK_WIDGET_CLASS (gtk_layout_parent_class)->expose_event (widget, event);
1018 1019 1020 1021

  return FALSE;
}

1022
/* Container methods
1023 1024
 */
static void
1025 1026 1027 1028 1029 1030 1031
gtk_layout_add (GtkContainer *container,
		GtkWidget    *widget)
{
  gtk_layout_put (GTK_LAYOUT (container), widget, 0, 0);
}

static void
1032 1033 1034
gtk_layout_remove (GtkContainer *container, 
		   GtkWidget    *widget)
{
1035
  GtkLayout *layout = GTK_LAYOUT (container);
1036
  GList *tmp_list;
1037
  GtkLayoutChild *child = NULL;
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

  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)
{
1064
  GtkLayout *layout = GTK_LAYOUT (container);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
  GtkLayoutChild *child;
  GList *tmp_list;

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

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

/* Operations on children
 */

1081 1082 1083 1084 1085
static void
gtk_layout_allocate_child (GtkLayout      *layout,
			   GtkLayoutChild *child)
{
  GtkAllocation allocation;
1086
  GtkRequisition requisition;
1087

1088 1089
  allocation.x = child->x;
  allocation.y = child->y;
1090 1091 1092
  gtk_widget_get_child_requisition (child->widget, &requisition);
  allocation.width = requisition.width;
  allocation.height = requisition.height;
1093 1094 1095 1096
  
  gtk_widget_size_allocate (child->widget, &allocation);
}

1097 1098 1099 1100 1101 1102
/* Callbacks */

static void
gtk_layout_adjustment_changed (GtkAdjustment *adjustment,
			       GtkLayout     *layout)
{
1103
  if (layout->freeze_count)
1104 1105
    return;

1106
  if (gtk_widget_get_realized (GTK_WIDGET (layout)))
1107
    {
1108 1109 1110
      gdk_window_move (layout->bin_window,
		       - layout->hadjustment->value,
		       - layout->vadjustment->value);
1111
      
1112
      gdk_window_process_updates (layout->bin_window, TRUE);
1113 1114
    }
}
1115 1116 1117

#define __GTK_LAYOUT_C__
#include "gtkaliasdef.c"