]> Pileus Git - ~andy/gtk/blob - gtk/gtkoverlay.c
overlay: do not to set uninitialized values in the main allocation
[~andy/gtk] / gtk / gtkoverlay.c
1 /*
2  * gtkoverlay.c
3  * This file is part of gtk
4  *
5  * Copyright (C) 2011 - Ignacio Casal Quinteiro, Mike Krüger
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22
23 #include "gtkoverlay.h"
24 #include "gtkbuildable.h"
25 #include "gtkscrolledwindow.h"
26 #include "gtkmarshalers.h"
27
28 #include "gtkprivate.h"
29 #include "gtkintl.h"
30
31 /**
32  * SECTION:gtkoverlay
33  * @short_description: A container which overlays widgets on top of each other
34  * @title: GtkOverlay
35  *
36  * GtkOverlay is a container which contains a single main child, on top
37  * of which it can place <firstterm>overlay</firstterm> widgets. The
38  * position of each overlay widget is determined by its #GtkWidget:halign
39  * and #GtkWidget:valign properties. E.g. a widget with both alignments
40  * set to %GTK_ALIGN_START will be placed at the top left corner of the
41  * main widget, whereas an overlay with halign set to %GTK_ALIGN_CENTER
42  * and valign set to %GTK_ALIGN_END will be placed a the bottom edge of
43  * the main widget, horizontally centered. The position can be adjusted
44  * by setting the margin properties of the child to non-zero values.
45  *
46  * More complicated placement of overlays is possible by connecting
47  * to the #GtkOverlay::get-child-position signal.
48  *
49  * <refsect2 id="GtkOverlay-BUILDER-UI">
50  * <title>GtkOverlay as GtkBuildable</title>
51  * <para>
52  * The GtkOverlay implementation of the GtkBuildable interface
53  * supports placing a child as an overlay by specifying "overlay" as
54  * the "type" attribute of a <tag class="starttag">child</tag> element.
55  * </para>
56  * </refsect2>
57  */
58
59 struct _GtkOverlayPrivate
60 {
61   GSList *children;
62 };
63
64 typedef struct _GtkOverlayChild GtkOverlayChild;
65
66 struct _GtkOverlayChild
67 {
68   GtkWidget *widget;
69   GdkWindow *window;
70 };
71
72 enum {
73   GET_CHILD_POSITION,
74   LAST_SIGNAL
75 };
76
77 static guint signals[LAST_SIGNAL] = { 0 };
78
79 static void gtk_overlay_buildable_init (GtkBuildableIface *iface);
80
81 G_DEFINE_TYPE_WITH_CODE (GtkOverlay, gtk_overlay, GTK_TYPE_BIN,
82                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
83                                                 gtk_overlay_buildable_init))
84
85 static GdkWindow *
86 gtk_overlay_create_child_window (GtkOverlay *overlay,
87                                  GtkWidget  *child)
88 {
89   GtkWidget *widget = GTK_WIDGET (overlay);
90   GtkAllocation allocation;
91   GdkWindow *window;
92   GdkWindowAttr attributes;
93   gint attributes_mask;
94
95   gtk_widget_get_allocation (child, &allocation);
96
97   attributes.window_type = GDK_WINDOW_CHILD;
98   attributes.wclass = GDK_INPUT_OUTPUT;
99   attributes.width = allocation.width;
100   attributes.height = allocation.height;
101   attributes.x = allocation.x;
102   attributes.y = allocation.y;
103   attributes_mask = GDK_WA_X | GDK_WA_Y;
104   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
105
106   window = gdk_window_new (gtk_widget_get_window (widget),
107                            &attributes, attributes_mask);
108   gdk_window_set_user_data (window, overlay);
109   gtk_style_context_set_background (gtk_widget_get_style_context (widget), window);
110
111   gtk_widget_set_parent_window (child, window);
112
113   return window;
114 }
115
116 static void
117 gtk_overlay_get_main_widget_allocation (GtkOverlay *overlay,
118                                         GtkAllocation *main_alloc_out)
119 {
120   GtkWidget *main_widget;
121   GtkAllocation main_alloc;
122
123   main_widget = gtk_bin_get_child (GTK_BIN (overlay));
124
125   /* special-case scrolled windows */
126   if (GTK_IS_SCROLLED_WINDOW (main_widget))
127     {
128       GtkWidget *grandchild;
129       gint x, y;
130       gboolean res;
131
132       grandchild = gtk_bin_get_child (GTK_BIN (main_widget));
133       res = gtk_widget_translate_coordinates (grandchild, main_widget, 0, 0, &x, &y);
134
135       if (res)
136         {
137           main_alloc.x = x;
138           main_alloc.y = y;
139         }
140       else
141         {
142           main_alloc.x = 0;
143           main_alloc.y = 0;
144         }
145
146       main_alloc.width = gtk_widget_get_allocated_width (grandchild);
147       main_alloc.height = gtk_widget_get_allocated_height (grandchild);
148     }
149   else
150     {
151       main_alloc.x = 0;
152       main_alloc.y = 0;
153       main_alloc.width = gtk_widget_get_allocated_width (main_widget);
154       main_alloc.height = gtk_widget_get_allocated_height (main_widget);
155     }
156
157   if (main_alloc_out)
158     *main_alloc_out = main_alloc;
159 }
160
161 static void
162 gtk_overlay_child_allocate (GtkOverlay      *overlay,
163                             GtkOverlayChild *child)
164 {
165   gint left, right, top, bottom;
166   GtkAllocation allocation, child_allocation, overlay_allocation;
167   gboolean result;
168
169   if (gtk_widget_get_mapped (GTK_WIDGET (overlay)))
170     {
171       if (gtk_widget_get_visible (child->widget))
172         gdk_window_show (child->window);
173       else if (gdk_window_is_visible (child->window))
174         gdk_window_hide (child->window);
175     }
176
177   if (!gtk_widget_get_visible (child->widget))
178     return;
179
180   g_signal_emit (overlay, signals[GET_CHILD_POSITION],
181                  0, child->widget, &allocation, &result);
182
183   gtk_widget_get_allocation (GTK_WIDGET (overlay), &overlay_allocation);
184
185   allocation.x += overlay_allocation.x;
186   allocation.y += overlay_allocation.y;
187
188   /* put the margins outside the window; also arrange things
189    * so that the adjusted child allocation still ends up at 0, 0
190    */
191   left = gtk_widget_get_margin_left (child->widget);
192   right = gtk_widget_get_margin_right (child->widget);
193   top = gtk_widget_get_margin_top (child->widget);
194   bottom = gtk_widget_get_margin_bottom (child->widget);
195
196   child_allocation.x = - left;
197   child_allocation.y = - top;
198   child_allocation.width = allocation.width;
199   child_allocation.height = allocation.height;
200
201   allocation.x += left;
202   allocation.y += top;
203   allocation.width -= left + right;
204   allocation.height -= top + bottom;
205
206   if (child->window)
207     gdk_window_move_resize (child->window,
208                             allocation.x, allocation.y,
209                             allocation.width, allocation.height);
210
211   gtk_widget_size_allocate (child->widget, &child_allocation);
212 }
213
214 static void
215 gtk_overlay_get_preferred_width (GtkWidget *widget,
216                                  gint      *minimum,
217                                  gint      *natural)
218 {
219   GtkBin *bin = GTK_BIN (widget);
220   GtkWidget *child;
221
222   if (minimum)
223     *minimum = 0;
224
225   if (natural)
226     *natural = 0;
227
228   child = gtk_bin_get_child (bin);
229   if (child && gtk_widget_get_visible (child))
230     gtk_widget_get_preferred_width (child, minimum, natural);
231 }
232
233 static void
234 gtk_overlay_get_preferred_height (GtkWidget *widget,
235                                   gint      *minimum,
236                                   gint      *natural)
237 {
238   GtkBin *bin = GTK_BIN (widget);
239   GtkWidget *child;
240
241   if (minimum)
242     *minimum = 0;
243
244   if (natural)
245     *natural = 0;
246
247   child = gtk_bin_get_child (bin);
248   if (child && gtk_widget_get_visible (child))
249     gtk_widget_get_preferred_height (child, minimum, natural);
250 }
251
252 static void
253 gtk_overlay_size_allocate (GtkWidget     *widget,
254                            GtkAllocation *allocation)
255 {
256   GtkOverlay *overlay = GTK_OVERLAY (widget);
257   GtkOverlayPrivate *priv = overlay->priv;
258   GSList *children;
259   GtkWidget *main_widget;
260
261   GTK_WIDGET_CLASS (gtk_overlay_parent_class)->size_allocate (widget, allocation);
262
263   main_widget = gtk_bin_get_child (GTK_BIN (overlay));
264   if (!main_widget || !gtk_widget_get_visible (main_widget))
265     return;
266
267   gtk_widget_size_allocate (main_widget, allocation);
268
269   for (children = priv->children; children; children = children->next)
270     {
271       gtk_overlay_child_allocate (overlay, children->data);
272     }
273 }
274
275 static GtkAlign
276 effective_align (GtkAlign         align,
277                  GtkTextDirection direction)
278 {
279   switch (align)
280     {
281     case GTK_ALIGN_START:
282       return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_END : GTK_ALIGN_START;
283     case GTK_ALIGN_END:
284       return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_START : GTK_ALIGN_END;
285     default:
286       return align;
287     }
288 }
289
290 static gboolean
291 gtk_overlay_get_child_position (GtkOverlay    *overlay,
292                                 GtkWidget     *widget,
293                                 GtkAllocation *alloc)
294 {
295   GtkAllocation main_alloc;
296   GtkRequisition req;
297   GtkAlign halign;
298   GtkTextDirection direction;
299
300   gtk_overlay_get_main_widget_allocation (overlay, &main_alloc);
301   gtk_widget_get_preferred_size (widget, NULL, &req);
302
303   alloc->x = main_alloc.x;
304   alloc->width = MIN (main_alloc.width, req.width);
305
306   direction = gtk_widget_get_direction (widget);
307
308   halign = gtk_widget_get_halign (widget);
309   switch (effective_align (halign, direction))
310     {
311     case GTK_ALIGN_START:
312       /* nothing to do */
313       break;
314     case GTK_ALIGN_FILL:
315       alloc->width = main_alloc.width;
316       break;
317     case GTK_ALIGN_CENTER:
318       alloc->x += main_alloc.width / 2 - req.width / 2;
319       break;
320     case GTK_ALIGN_END:
321       alloc->x += main_alloc.width - req.width;
322       break;
323     }
324
325   alloc->y = main_alloc.y;
326   alloc->height = MIN (main_alloc.height, req.height);
327
328   switch (gtk_widget_get_valign (widget))
329     {
330     case GTK_ALIGN_START:
331       /* nothing to do */
332       break;
333     case GTK_ALIGN_FILL:
334       alloc->height = main_alloc.height;
335       break;
336     case GTK_ALIGN_CENTER:
337       alloc->y += main_alloc.height / 2 - req.height / 2;
338       break;
339     case GTK_ALIGN_END:
340       alloc->y += main_alloc.height - req.height;
341       break;
342     }
343
344   return TRUE;
345 }
346
347 static void
348 gtk_overlay_realize (GtkWidget *widget)
349 {
350   GtkOverlay *overlay = GTK_OVERLAY (widget);
351   GtkOverlayPrivate *priv = overlay->priv;
352   GtkOverlayChild *child;
353   GSList *children;
354
355   GTK_WIDGET_CLASS (gtk_overlay_parent_class)->realize (widget);
356
357   for (children = priv->children; children; children = children->next)
358     {
359       child = children->data;
360
361       if (child->window == NULL)
362         child->window = gtk_overlay_create_child_window (overlay, child->widget);
363     }
364 }
365
366 static void
367 gtk_overlay_unrealize (GtkWidget *widget)
368 {
369   GtkOverlay *overlay = GTK_OVERLAY (widget);
370   GtkOverlayPrivate *priv = overlay->priv;
371   GtkOverlayChild *child;
372   GSList *children;
373
374   for (children = priv->children; children; children = children->next)
375     {
376       child = children->data;
377
378       gtk_widget_set_parent_window (child->widget, NULL);
379       gdk_window_set_user_data (child->window, NULL);
380       gdk_window_destroy (child->window);
381       child->window = NULL;
382     }
383
384   GTK_WIDGET_CLASS (gtk_overlay_parent_class)->unrealize (widget);
385 }
386
387 static void
388 gtk_overlay_map (GtkWidget *widget)
389 {
390   GtkOverlay *overlay = GTK_OVERLAY (widget);
391   GtkOverlayPrivate *priv = overlay->priv;
392   GtkOverlayChild *child;
393   GSList *children;
394
395   GTK_WIDGET_CLASS (gtk_overlay_parent_class)->map (widget);
396
397   for (children = priv->children; children; children = children->next)
398     {
399       child = children->data;
400
401       if (child->window != NULL &&
402           gtk_widget_get_visible (child->widget) &&
403           gtk_widget_get_child_visible (child->widget))
404         gdk_window_show (child->window);
405     }
406 }
407
408 static void
409 gtk_overlay_unmap (GtkWidget *widget)
410 {
411   GtkOverlay *overlay = GTK_OVERLAY (widget);
412   GtkOverlayPrivate *priv = overlay->priv;
413   GtkOverlayChild *child;
414   GSList *children;
415
416   for (children = priv->children; children; children = children->next)
417     {
418       child = children->data;
419
420       if (child->window != NULL &&
421           gdk_window_is_visible (child->window))
422         gdk_window_hide (child->window);
423     }
424
425   GTK_WIDGET_CLASS (gtk_overlay_parent_class)->unmap (widget);
426 }
427
428 static gboolean
429 gtk_overlay_draw (GtkWidget *widget,
430                   cairo_t   *cr)
431 {
432   GtkOverlay *overlay = GTK_OVERLAY (widget);
433   GtkOverlayPrivate *priv = overlay->priv;
434   GtkOverlayChild *child;
435   GSList *children;
436
437   for (children = priv->children; children; children = children->next)
438     {
439       child = children->data;
440
441       if (gtk_cairo_should_draw_window (cr, child->window))
442         {
443           cairo_save (cr);
444           gtk_cairo_transform_to_window (cr, widget, child->window);
445           gtk_render_background (gtk_widget_get_style_context (widget),
446                                  cr,
447                                  0, 0,
448                                  gdk_window_get_width (child->window),
449                                  gdk_window_get_height (child->window));
450           cairo_restore (cr);
451         }
452     }
453
454   GTK_WIDGET_CLASS (gtk_overlay_parent_class)->draw (widget, cr);
455
456   return FALSE;
457 }
458
459 static void
460 gtk_overlay_remove (GtkContainer *container,
461                     GtkWidget    *widget)
462 {
463   GtkOverlayPrivate *priv = GTK_OVERLAY (container)->priv;
464   GtkOverlayChild *child;
465   GSList *children;
466
467   for (children = priv->children; children; children = children->next)
468     {
469       child = children->data;
470
471       if (child->widget == widget)
472         {
473           if (child->window != NULL)
474             {
475               gdk_window_set_user_data (child->window, NULL);
476               gdk_window_destroy (child->window);
477             }
478
479           gtk_widget_unparent (widget);
480
481           priv->children = g_slist_delete_link (priv->children, children);
482           g_slice_free (GtkOverlayChild, child);
483
484           return;
485         }
486     }
487
488   GTK_CONTAINER_CLASS (gtk_overlay_parent_class)->remove (container, widget);
489 }
490
491 static void
492 gtk_overlay_forall (GtkContainer *overlay,
493                     gboolean      include_internals,
494                     GtkCallback   callback,
495                     gpointer      callback_data)
496 {
497   GtkOverlayPrivate *priv = GTK_OVERLAY (overlay)->priv;
498   GtkOverlayChild *child;
499   GSList *children;
500   GtkWidget *main_widget;
501
502   main_widget = gtk_bin_get_child (GTK_BIN (overlay));
503   if (main_widget)
504     (* callback) (main_widget, callback_data);
505
506   children = priv->children;
507   while (children)
508     {
509       child = children->data;
510       children = children->next;
511
512       (* callback) (child->widget, callback_data);
513     }
514 }
515
516 static void
517 gtk_overlay_class_init (GtkOverlayClass *klass)
518 {
519   GObjectClass *object_class = G_OBJECT_CLASS (klass);
520   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
521   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
522
523   widget_class->get_preferred_width = gtk_overlay_get_preferred_width;
524   widget_class->get_preferred_height = gtk_overlay_get_preferred_height;
525   widget_class->size_allocate = gtk_overlay_size_allocate;
526   widget_class->realize = gtk_overlay_realize;
527   widget_class->unrealize = gtk_overlay_unrealize;
528   widget_class->map = gtk_overlay_map;
529   widget_class->unmap = gtk_overlay_unmap;
530   widget_class->draw = gtk_overlay_draw;
531
532   container_class->remove = gtk_overlay_remove;
533   container_class->forall = gtk_overlay_forall;
534
535   klass->get_child_position = gtk_overlay_get_child_position;
536
537   /**
538    * GtkOverlay::get-child-position:
539    * @overlay: the #GtkOverlay
540    * @widget: the child widget to position
541    * @allocation: (out): return location for the allocation
542    *
543    * The ::get-child-position signal is emitted to determine
544    * the position and size of any overlay child widgets. A
545    * handler for this signal should fill @allocation with
546    * the desired position and size for @widget, relative to
547    * the 'main' child of @overlay.
548    *
549    * The default handler for this signal uses the @widget's
550    * halign and valign properties to determine the position
551    * and gives the widget its natural size (except that an
552    * alignment of %GTK_ALIGN_FILL will cause the overlay to
553    * be full-width/height). If the main child is a
554    * #GtkScrolledWindow, the overlays are placed relative
555    * to its contents.
556    *
557    * Return: %TRUE if the @allocation has been filled
558    */
559   signals[GET_CHILD_POSITION] =
560     g_signal_new (I_("get-child-position"),
561                   G_TYPE_FROM_CLASS (object_class),
562                   G_SIGNAL_RUN_LAST,
563                   G_STRUCT_OFFSET (GtkOverlayClass, get_child_position),
564                   _gtk_boolean_handled_accumulator, NULL,
565                   _gtk_marshal_BOOLEAN__OBJECT_BOXED,
566                   G_TYPE_BOOLEAN, 2,
567                   GTK_TYPE_WIDGET,
568                   GDK_TYPE_RECTANGLE | G_SIGNAL_TYPE_STATIC_SCOPE);
569
570   g_type_class_add_private (object_class, sizeof (GtkOverlayPrivate));
571 }
572
573 static void
574 gtk_overlay_init (GtkOverlay *overlay)
575 {
576   overlay->priv = G_TYPE_INSTANCE_GET_PRIVATE (overlay, GTK_TYPE_OVERLAY, GtkOverlayPrivate);
577
578   gtk_widget_set_has_window (GTK_WIDGET (overlay), FALSE);
579 }
580
581 static void
582 gtk_overlay_buildable_add_child (GtkBuildable *buildable,
583                                  GtkBuilder   *builder,
584                                  GObject      *child,
585                                  const gchar  *type)
586 {
587   if (type && strcmp (type, "overlay") == 0)
588     gtk_overlay_add_overlay (GTK_OVERLAY (buildable), GTK_WIDGET (child));
589   else if (!type)
590     gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
591   else
592     GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
593 }
594
595 static void
596 gtk_overlay_buildable_init (GtkBuildableIface *iface)
597 {
598   iface->add_child = gtk_overlay_buildable_add_child;
599 }
600
601 /**
602  * gtk_overlay_new:
603  *
604  * Creates a new #GtkOverlay.
605  *
606  * Returns: a new #GtkOverlay object.
607  *
608  * Since: 3.2
609  */
610 GtkWidget *
611 gtk_overlay_new (void)
612 {
613   return g_object_new (GTK_TYPE_OVERLAY, NULL);
614 }
615
616 /**
617  * gtk_overlay_add_overlay:
618  * @overlay: a #GtkOverlay
619  * @widget: a #GtkWidget to be added to the container
620  *
621  * Adds @widget to @overlay.
622  *
623  * The widget will be stacked on top of the main widget
624  * added with gtk_container_add().
625  *
626  * The position at which @widget is placed is determined
627  * from its #GtkWidget:halign and #GtkWidget:valign properties.
628  *
629  * Since: 3.2
630  */
631 void
632 gtk_overlay_add_overlay (GtkOverlay *overlay,
633                          GtkWidget  *widget)
634 {
635   GtkOverlayPrivate *priv = overlay->priv;
636   GtkOverlayChild *child;
637
638   g_return_if_fail (GTK_IS_OVERLAY (overlay));
639   g_return_if_fail (GTK_IS_WIDGET (widget));
640
641   child = g_slice_new0 (GtkOverlayChild);
642   child->widget = widget;
643
644   priv->children = g_slist_append (priv->children, child);
645
646   if (gtk_widget_get_realized (GTK_WIDGET (overlay)))
647     {
648       child->window = gtk_overlay_create_child_window (overlay, widget);
649       gtk_widget_set_parent (widget, GTK_WIDGET (overlay));
650       gtk_overlay_child_allocate (overlay, child);
651     }
652   else
653     gtk_widget_set_parent (widget, GTK_WIDGET (overlay));
654
655 }