]> Pileus Git - ~andy/gtk/blob - gtk/gtkbubblewindow.c
Really keep GtkBubbleWindow private
[~andy/gtk] / gtk / gtkbubblewindow.c
1 /* GTK - The GIMP Toolkit
2  * Copyright © 2013 Carlos Garnacho <carlosg@gnome.org>
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  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * 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  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * GtkBubbleWindow is a bubble-like context window, primarily mean for
20  * context-dependent helpers on touch interfaces.
21  *
22  * In order to place a GtkBubbleWindow to point to some other area,
23  * use gtk_bubble_window_set_relative_to(), gtk_bubble_window_set_pointing_to()
24  * and gtk_bubble_window_set_position(). Although it is usually  more
25  * convenient to use gtk_bubble_window_popup() which handles all of those
26  * at once.
27  *
28  * By default, no grabs are performed on the GtkBubbleWindow, leaving
29  * the popup/popdown semantics up to the caller, gtk_bubble_window_grab()
30  * can be used to grab the window for a device pair, bringing #GtkMenu alike
31  * popdown behavior by default on keyboard/pointer interaction. Grabs need
32  * to be undone through gtk_bubble_window_ungrab().
33  */
34
35 #include "config.h"
36 #include <gdk/gdk.h>
37 #include <cairo-gobject.h>
38 #include "gtkbubblewindowprivate.h"
39 #include "gtktypebuiltins.h"
40 #include "gtkmain.h"
41 #include "gtkprivate.h"
42 #include "gtkintl.h"
43
44 #define TAIL_GAP_WIDTH 24
45 #define TAIL_HEIGHT    12
46
47 #define POS_IS_VERTICAL(p) ((p) == GTK_POS_TOP || (p) == GTK_POS_BOTTOM)
48
49 #define GRAB_EVENT_MASK                             \
50   GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | \
51   GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |       \
52   GDK_POINTER_MOTION_MASK
53
54 typedef struct _GtkBubbleWindowPrivate GtkBubbleWindowPrivate;
55
56 enum {
57   PROP_RELATIVE_TO = 1,
58   PROP_POINTING_TO,
59   PROP_POSITION
60 };
61
62 struct _GtkBubbleWindowPrivate
63 {
64   GdkDevice *device;
65   GdkWindow *relative_to;
66   cairo_rectangle_int_t pointing_to;
67   gint win_x;
68   gint win_y;
69   guint has_pointing_to    : 1;
70   guint grabbed            : 1;
71   guint preferred_position : 2;
72   guint final_position     : 2;
73 };
74
75 G_DEFINE_TYPE (GtkBubbleWindow, _gtk_bubble_window, GTK_TYPE_WINDOW)
76
77 static void
78 _gtk_bubble_window_init (GtkBubbleWindow *window)
79 {
80   GtkWidget *widget;
81   GdkScreen *screen;
82   GdkVisual *visual;
83
84   widget = GTK_WIDGET (window);
85   window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
86                                                GTK_TYPE_BUBBLE_WINDOW,
87                                                GtkBubbleWindowPrivate);
88   gtk_window_set_default_size (GTK_WINDOW (window),
89                                TAIL_GAP_WIDTH, TAIL_GAP_WIDTH);
90   gtk_widget_set_app_paintable (widget, TRUE);
91
92   screen = gtk_widget_get_screen (widget);
93   visual = gdk_screen_get_rgba_visual (screen);
94
95   if (visual)
96     gtk_widget_set_visual (widget, visual);
97 }
98
99 static GObject *
100 gtk_bubble_window_constructor (GType                  type,
101                                guint                  n_construct_properties,
102                                GObjectConstructParam *construct_properties)
103 {
104   GObject *object;
105
106   object =
107     G_OBJECT_CLASS (_gtk_bubble_window_parent_class)->constructor (type,
108                                                                   n_construct_properties,
109                                                                   construct_properties);
110   g_object_set (object, "type", GTK_WINDOW_POPUP, NULL);
111
112   return object;
113 }
114
115 static void
116 gtk_bubble_window_set_property (GObject      *object,
117                                 guint         prop_id,
118                                 const GValue *value,
119                                 GParamSpec   *pspec)
120 {
121   switch (prop_id)
122     {
123     case PROP_RELATIVE_TO:
124       _gtk_bubble_window_set_relative_to (GTK_BUBBLE_WINDOW (object),
125                                           g_value_get_object (value));
126       break;
127     case PROP_POINTING_TO:
128       _gtk_bubble_window_set_pointing_to (GTK_BUBBLE_WINDOW (object),
129                                           g_value_get_boxed (value));
130       break;
131     case PROP_POSITION:
132       _gtk_bubble_window_set_position (GTK_BUBBLE_WINDOW (object),
133                                        g_value_get_enum (value));
134       break;
135     default:
136       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137     }
138 }
139
140 static void
141 gtk_bubble_window_get_property (GObject    *object,
142                                 guint       prop_id,
143                                 GValue     *value,
144                                 GParamSpec *pspec)
145 {
146   GtkBubbleWindowPrivate *priv = GTK_BUBBLE_WINDOW (object)->priv;
147
148   switch (prop_id)
149     {
150     case PROP_RELATIVE_TO:
151       g_value_set_object (value, priv->relative_to);
152       break;
153     case PROP_POINTING_TO:
154       g_value_set_boxed (value, &priv->pointing_to);
155       break;
156     case PROP_POSITION:
157       g_value_set_enum (value, priv->preferred_position);
158       break;
159     default:
160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161     }
162 }
163
164 static void
165 gtk_bubble_window_finalize (GObject *object)
166 {
167   GtkBubbleWindow *window = GTK_BUBBLE_WINDOW (object);
168   GtkBubbleWindowPrivate *priv = window->priv;
169
170   _gtk_bubble_window_popdown (window);
171
172   if (priv->relative_to)
173     g_object_unref (priv->relative_to);
174
175   G_OBJECT_CLASS (_gtk_bubble_window_parent_class)->finalize (object);
176 }
177
178 static void
179 gtk_bubble_window_get_pointed_to_coords (GtkBubbleWindow       *window,
180                                          gint                  *x,
181                                          gint                  *y,
182                                          cairo_rectangle_int_t *root_rect)
183 {
184   GtkBubbleWindowPrivate *priv = window->priv;
185   cairo_rectangle_int_t rect;
186   GdkScreen *screen;
187
188   rect = priv->pointing_to;
189   screen = gtk_widget_get_screen (GTK_WIDGET (window));
190
191   if (priv->relative_to)
192     gdk_window_get_root_coords (priv->relative_to,
193                                 rect.x, rect.y, &rect.x, &rect.y);
194
195   if (POS_IS_VERTICAL (priv->final_position))
196     {
197       *x = CLAMP (rect.x + (rect.width / 2),
198                   0, gdk_screen_get_width (screen));
199       *y = rect.y;
200
201       if (priv->final_position == GTK_POS_BOTTOM)
202         (*y) += rect.height;
203     }
204   else
205     {
206       *y = CLAMP (rect.y + (rect.height / 2),
207                   0, gdk_screen_get_height (screen));
208       *x = rect.x;
209
210       if (priv->final_position == GTK_POS_RIGHT)
211         (*x) += rect.width;
212     }
213
214   if (root_rect)
215     *root_rect = rect;
216 }
217
218 static void
219 gtk_bubble_window_apply_tail_path (GtkBubbleWindow *window,
220                                    cairo_t         *cr,
221                                    GtkAllocation   *allocation)
222 {
223   GtkBubbleWindowPrivate *priv = window->priv;
224   gint base, tip, x, y;
225
226   gtk_bubble_window_get_pointed_to_coords (window, &x, &y, NULL);
227
228   if (priv->final_position == GTK_POS_BOTTOM ||
229       priv->final_position == GTK_POS_RIGHT)
230     {
231       base = TAIL_HEIGHT;
232       tip = 0;
233     }
234   else if (priv->final_position == GTK_POS_TOP)
235     {
236       base = allocation->height - TAIL_HEIGHT;
237       tip = allocation->height;
238     }
239   else if (priv->final_position == GTK_POS_LEFT)
240     {
241       base = allocation->width - TAIL_HEIGHT;
242       tip = allocation->width;
243     }
244
245   if (POS_IS_VERTICAL (priv->final_position))
246     {
247       cairo_move_to (cr, CLAMP (x - priv->win_x - TAIL_GAP_WIDTH / 2,
248                                 0, allocation->width - TAIL_GAP_WIDTH), base);
249       cairo_line_to (cr, CLAMP (x - priv->win_x, 0, allocation->width), tip);
250       cairo_line_to (cr, CLAMP (x - priv->win_x + TAIL_GAP_WIDTH / 2,
251                                 TAIL_GAP_WIDTH, allocation->width), base);
252     }
253   else
254     {
255       cairo_move_to (cr, base,
256                      CLAMP (y - priv->win_y - TAIL_GAP_WIDTH / 2,
257                             0, allocation->height - TAIL_GAP_WIDTH));
258       cairo_line_to (cr, tip, CLAMP (y - priv->win_y, 0, allocation->height));
259       cairo_line_to (cr, base,
260                      CLAMP (y - priv->win_y + TAIL_GAP_WIDTH / 2,
261                             TAIL_GAP_WIDTH, allocation->height));
262     }
263 }
264
265 static void
266 gtk_bubble_window_apply_border_path (GtkBubbleWindow *window,
267                                      cairo_t         *cr)
268 {
269   GtkBubbleWindowPrivate *priv;
270   GtkAllocation allocation;
271
272   priv = window->priv;
273   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
274
275   gtk_bubble_window_apply_tail_path (window, cr, &allocation);
276
277   if (priv->final_position == GTK_POS_TOP)
278     {
279       cairo_line_to (cr, allocation.width, allocation.height - TAIL_HEIGHT);
280       cairo_line_to (cr, allocation.width, 0);
281       cairo_line_to (cr, 0, 0);
282       cairo_line_to (cr, 0, allocation.height - TAIL_HEIGHT);
283     }
284   else if (priv->final_position == GTK_POS_BOTTOM)
285     {
286       cairo_line_to (cr, allocation.width, TAIL_HEIGHT);
287       cairo_line_to (cr, allocation.width, allocation.height);
288       cairo_line_to (cr, 0, allocation.height);
289       cairo_line_to (cr, 0, TAIL_HEIGHT);
290     }
291   else if (priv->final_position == GTK_POS_LEFT)
292     {
293       cairo_line_to (cr, allocation.width - TAIL_HEIGHT, allocation.height);
294       cairo_line_to (cr, 0, allocation.height);
295       cairo_line_to (cr, 0, 0);
296       cairo_line_to (cr, allocation.width - TAIL_HEIGHT, 0);
297     }
298   else if (priv->final_position == GTK_POS_RIGHT)
299     {
300       cairo_line_to (cr, TAIL_HEIGHT, 0);
301       cairo_line_to (cr, allocation.width, 0);
302       cairo_line_to (cr, allocation.width, allocation.height);
303       cairo_line_to (cr, TAIL_HEIGHT, allocation.height);
304     }
305
306   cairo_close_path (cr);
307 }
308
309 static void
310 gtk_bubble_window_update_shape (GtkBubbleWindow *window)
311 {
312   cairo_surface_t *surface;
313   cairo_region_t *region;
314   GdkWindow *win;
315   cairo_t *cr;
316
317   win = gtk_widget_get_window (GTK_WIDGET (window));
318   surface =
319     gdk_window_create_similar_surface (win,
320                                        CAIRO_CONTENT_COLOR_ALPHA,
321                                        gdk_window_get_width (win),
322                                        gdk_window_get_height (win));
323
324   cr = cairo_create (surface);
325   gtk_bubble_window_apply_border_path (window, cr);
326   cairo_fill (cr);
327   cairo_destroy (cr);
328
329   region = gdk_cairo_region_create_from_surface (surface);
330   cairo_surface_destroy (surface);
331
332   if (!gtk_widget_is_composited (GTK_WIDGET (window)))
333     gtk_widget_shape_combine_region (GTK_WIDGET (window), region);
334
335   gtk_widget_input_shape_combine_region (GTK_WIDGET (window), region);
336   cairo_region_destroy (region);
337 }
338
339 static void
340 gtk_bubble_window_update_position (GtkBubbleWindow *window)
341 {
342   GtkBubbleWindowPrivate *priv;
343   cairo_rectangle_int_t rect;
344   GtkAllocation allocation;
345   gint win_x, win_y, x, y;
346   GdkScreen *screen;
347
348   priv = window->priv;
349   screen = gtk_widget_get_screen (GTK_WIDGET (window));
350   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
351   priv->final_position = priv->preferred_position;
352   rect = priv->pointing_to;
353
354   gtk_bubble_window_get_pointed_to_coords (window, &x, &y, &rect);
355
356   /* Check whether there's enough room on the
357    * preferred side, move to the opposite one if not.
358    */
359   if (priv->preferred_position == GTK_POS_TOP && rect.y < allocation.height)
360     priv->final_position = GTK_POS_BOTTOM;
361   else if (priv->preferred_position == GTK_POS_BOTTOM &&
362            rect.y > gdk_screen_get_height (screen) - allocation.height)
363     priv->final_position = GTK_POS_TOP;
364   else if (priv->preferred_position == GTK_POS_LEFT && rect.x < allocation.width)
365     priv->final_position = GTK_POS_RIGHT;
366   else if (priv->preferred_position == GTK_POS_RIGHT &&
367            rect.x > gdk_screen_get_width (screen) - allocation.width)
368     priv->final_position = GTK_POS_LEFT;
369
370   if (POS_IS_VERTICAL (priv->final_position))
371     {
372       win_x = CLAMP (x - allocation.width / 2,
373                      0, gdk_screen_get_width (screen) - allocation.width);
374       win_y = y;
375
376       if (priv->final_position == GTK_POS_TOP)
377         win_y -= allocation.height;
378     }
379   else
380     {
381       win_y = CLAMP (y - allocation.height / 2,
382                      0, gdk_screen_get_height (screen) - allocation.height);
383       win_x = x;
384
385       if (priv->final_position == GTK_POS_LEFT)
386         win_x -= allocation.width;
387
388     }
389
390   priv->win_x = win_x;
391   priv->win_y = win_y;
392   gtk_window_move (GTK_WINDOW (window), win_x, win_y);
393
394   gtk_widget_queue_resize (GTK_WIDGET (window));
395 }
396
397 static gboolean
398 gtk_bubble_window_draw (GtkWidget *widget,
399                         cairo_t   *cr)
400 {
401   GtkStyleContext *context;
402   GtkAllocation allocation;
403   GtkWidget *child;
404   GdkRGBA *border;
405
406   cairo_save (cr);
407   context = gtk_widget_get_style_context (widget);
408   gtk_widget_get_allocation (widget, &allocation);
409
410   gtk_render_background (context, cr, 0, 0,
411                          allocation.width, allocation.height);
412
413   if (gtk_widget_is_composited (widget))
414     {
415       cairo_save (cr);
416       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
417       cairo_set_source_rgba (cr, 0, 0, 0, 0);
418       cairo_paint (cr);
419       cairo_restore (cr);
420
421       gtk_bubble_window_apply_border_path (GTK_BUBBLE_WINDOW (widget), cr);
422       cairo_clip (cr);
423     }
424
425   gtk_render_background (context, cr, 0, 0,
426                          allocation.width, allocation.height);
427
428   gtk_style_context_get (context, gtk_widget_get_state_flags (widget),
429                          GTK_STYLE_PROPERTY_BORDER_COLOR, &border,
430                          NULL);
431
432   gtk_bubble_window_apply_border_path (GTK_BUBBLE_WINDOW (widget), cr);
433   gdk_cairo_set_source_rgba (cr, border);
434   cairo_stroke (cr);
435
436   child = gtk_bin_get_child (GTK_BIN (widget));
437
438   if (child)
439     gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
440
441   cairo_restore (cr);
442
443   gdk_rgba_free (border);
444
445   return TRUE;
446 }
447
448 static void
449 gtk_bubble_window_get_preferred_width (GtkWidget *widget,
450                                        gint      *minimum_width,
451                                        gint      *natural_width)
452 {
453   GtkBubbleWindowPrivate *priv;
454   GtkWidget *child;
455   gint min, nat;
456
457   priv = GTK_BUBBLE_WINDOW (widget)->priv;
458   child = gtk_bin_get_child (GTK_BIN (widget));
459   min = nat = 0;
460
461   if (child)
462     gtk_widget_get_preferred_width (child, &min, &nat);
463
464   if (!POS_IS_VERTICAL (priv->final_position))
465     {
466       min += TAIL_HEIGHT;
467       nat += TAIL_HEIGHT;
468     }
469
470   if (minimum_width)
471     *minimum_width = MAX (min, TAIL_GAP_WIDTH);
472
473   if (natural_width)
474     *natural_width = MAX (nat, TAIL_GAP_WIDTH);
475 }
476
477 static void
478 gtk_bubble_window_get_preferred_height (GtkWidget *widget,
479                                         gint      *minimum_height,
480                                         gint      *natural_height)
481 {
482   GtkBubbleWindowPrivate *priv;
483   GtkWidget *child;
484   gint min, nat;
485
486   priv = GTK_BUBBLE_WINDOW (widget)->priv;
487   child = gtk_bin_get_child (GTK_BIN (widget));
488   min = nat = 0;
489
490   if (child)
491       gtk_widget_get_preferred_height (child, &min, &nat);
492
493   if (POS_IS_VERTICAL (priv->final_position))
494     {
495       min += TAIL_HEIGHT;
496       nat += TAIL_HEIGHT;
497     }
498
499   if (minimum_height)
500     *minimum_height = MAX (min, TAIL_GAP_WIDTH);
501
502   if (natural_height)
503     *natural_height = MAX (nat, TAIL_GAP_WIDTH);
504 }
505
506 static void
507 gtk_bubble_window_size_allocate (GtkWidget     *widget,
508                                  GtkAllocation *allocation)
509 {
510   GtkBubbleWindowPrivate *priv;
511   GtkWidget *child;
512
513   priv = GTK_BUBBLE_WINDOW (widget)->priv;
514   gtk_widget_set_allocation (widget, allocation);
515   child = gtk_bin_get_child (GTK_BIN (widget));
516
517   if (child)
518     {
519       GtkAllocation child_alloc;
520
521       child_alloc.x = child_alloc.y = 0;
522       child_alloc.width = allocation->width;
523       child_alloc.height = allocation->height;
524
525       if (POS_IS_VERTICAL (priv->final_position))
526         child_alloc.height -= TAIL_HEIGHT;
527       else
528         child_alloc.width -= TAIL_HEIGHT;
529
530       if (priv->final_position == GTK_POS_BOTTOM)
531         child_alloc.y += TAIL_HEIGHT;
532       else if (priv->final_position == GTK_POS_RIGHT)
533         child_alloc.x += TAIL_HEIGHT;
534
535       gtk_widget_size_allocate (child, &child_alloc);
536     }
537
538   if (gtk_widget_get_realized (widget))
539     gtk_bubble_window_update_shape (GTK_BUBBLE_WINDOW (widget));
540
541   if (gtk_widget_get_visible (widget))
542     gtk_bubble_window_update_position (GTK_BUBBLE_WINDOW (widget));
543 }
544
545 static gboolean
546 gtk_bubble_window_button_press (GtkWidget      *widget,
547                                 GdkEventButton *event)
548 {
549   GtkWidget *child;
550
551   child = gtk_bin_get_child (GTK_BIN (widget));
552
553   if (child && event->window == gtk_widget_get_window (widget))
554     {
555       GtkAllocation child_alloc;
556
557       gtk_widget_get_allocation (child, &child_alloc);
558
559       if (event->x < child_alloc.x ||
560           event->x > child_alloc.x + child_alloc.width ||
561           event->y < child_alloc.y ||
562           event->y > child_alloc.y + child_alloc.height)
563         _gtk_bubble_window_popdown (GTK_BUBBLE_WINDOW (widget));
564     }
565   else
566     _gtk_bubble_window_popdown (GTK_BUBBLE_WINDOW (widget));
567
568   return GDK_EVENT_PROPAGATE;
569 }
570
571 static gboolean
572 gtk_bubble_window_key_press (GtkWidget   *widget,
573                              GdkEventKey *event)
574 {
575   if (event->keyval == GDK_KEY_Escape)
576     {
577       _gtk_bubble_window_popdown (GTK_BUBBLE_WINDOW (widget));
578       return GDK_EVENT_STOP;
579     }
580
581   return GDK_EVENT_PROPAGATE;
582 }
583
584 static gboolean
585 gtk_bubble_window_grab_broken (GtkWidget          *widget,
586                                GdkEventGrabBroken *grab_broken)
587 {
588   GtkBubbleWindow *window = GTK_BUBBLE_WINDOW (widget);
589   GtkBubbleWindowPrivate *priv;
590   GdkDevice *event_device;
591
592   priv = window->priv;
593   event_device = gdk_event_get_device ((GdkEvent *) grab_broken);
594
595   if (event_device == priv->device ||
596       event_device == gdk_device_get_associated_device (priv->device))
597     _gtk_bubble_window_ungrab (window);
598
599   return FALSE;
600 }
601
602 static void
603 gtk_bubble_window_grab_notify (GtkWidget *widget,
604                                gboolean   was_grabbed)
605 {
606   GtkBubbleWindow *window = GTK_BUBBLE_WINDOW (widget);
607   GtkBubbleWindowPrivate *priv;
608
609   priv = window->priv;
610
611   if (priv->device && gtk_widget_device_is_shadowed (widget, priv->device))
612     _gtk_bubble_window_ungrab (window);
613 }
614
615 static void
616 gtk_bubble_window_screen_changed (GtkWidget *widget,
617                                   GdkScreen *previous_screen)
618 {
619   GdkScreen *screen;
620   GdkVisual *visual;
621
622   screen = gtk_widget_get_screen (widget);
623   visual = gdk_screen_get_rgba_visual (screen);
624
625   if (visual)
626     gtk_widget_set_visual (widget, visual);
627 }
628
629 static void
630 _gtk_bubble_window_class_init (GtkBubbleWindowClass *klass)
631 {
632   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
633   GObjectClass *object_class = G_OBJECT_CLASS (klass);
634
635   object_class->constructor = gtk_bubble_window_constructor;
636   object_class->set_property = gtk_bubble_window_set_property;
637   object_class->get_property = gtk_bubble_window_get_property;
638   object_class->finalize = gtk_bubble_window_finalize;
639
640   widget_class->get_preferred_width = gtk_bubble_window_get_preferred_width;
641   widget_class->get_preferred_height = gtk_bubble_window_get_preferred_height;
642   widget_class->size_allocate = gtk_bubble_window_size_allocate;
643   widget_class->draw = gtk_bubble_window_draw;
644   widget_class->button_press_event = gtk_bubble_window_button_press;
645   widget_class->key_press_event = gtk_bubble_window_key_press;
646   widget_class->grab_broken_event = gtk_bubble_window_grab_broken;
647   widget_class->grab_notify = gtk_bubble_window_grab_notify;
648   widget_class->screen_changed = gtk_bubble_window_screen_changed;
649
650   g_object_class_install_property (object_class,
651                                    PROP_RELATIVE_TO,
652                                    g_param_spec_object ("relative-to",
653                                                         P_("Relative to"),
654                                                         P_("Window the bubble window points to"),
655                                                         GDK_TYPE_WINDOW,
656                                                         GTK_PARAM_READWRITE));
657   g_object_class_install_property (object_class,
658                                    PROP_POINTING_TO,
659                                    g_param_spec_boxed ("pointing-to",
660                                                        P_("Pointing to"),
661                                                        P_("Rectangle the bubble window points to"),
662                                                        CAIRO_GOBJECT_TYPE_RECTANGLE_INT,
663                                                        GTK_PARAM_READWRITE));
664   g_object_class_install_property (object_class,
665                                    PROP_POSITION,
666                                    g_param_spec_enum ("position",
667                                                       P_("Position"),
668                                                       P_("Position to place the bubble window"),
669                                                       GTK_TYPE_POSITION_TYPE, GTK_POS_TOP,
670                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
671
672   g_type_class_add_private (klass, sizeof (GtkBubbleWindowPrivate));
673 }
674
675 static void
676 gtk_bubble_window_update_relative_to (GtkBubbleWindow *window,
677                                       GdkWindow       *relative_to)
678 {
679   GtkBubbleWindowPrivate *priv;
680
681   priv = window->priv;
682
683   if (priv->relative_to == relative_to)
684     return;
685
686   if (priv->relative_to)
687     g_object_unref (priv->relative_to);
688
689   priv->relative_to = (relative_to) ? g_object_ref (relative_to) : NULL;
690   g_object_notify (G_OBJECT (window), "relative-to");
691 }
692
693 static void
694 gtk_bubble_window_update_pointing_to (GtkBubbleWindow       *window,
695                                       cairo_rectangle_int_t *pointing_to)
696 {
697   GtkBubbleWindowPrivate *priv;
698
699   priv = window->priv;
700   priv->pointing_to = *pointing_to;
701   priv->has_pointing_to = TRUE;
702   g_object_notify (G_OBJECT (window), "pointing-to");
703 }
704
705 static void
706 gtk_bubble_window_update_preferred_position (GtkBubbleWindow *window,
707                                              GtkPositionType  position)
708 {
709   GtkBubbleWindowPrivate *priv;
710
711   priv = window->priv;
712   priv->preferred_position = position;
713   g_object_notify (G_OBJECT (window), "position");
714 }
715
716 /*
717  * gtk_bubble_window_new:
718  *
719  * Returns a newly created #GtkBubblewindow
720  *
721  * Returns: a new #GtkBubbleWindow
722  *
723  * Since: 3.8
724  */
725 GtkWidget *
726 _gtk_bubble_window_new (void)
727 {
728   return g_object_new (GTK_TYPE_BUBBLE_WINDOW, NULL);
729 }
730
731 /*
732  * gtk_bubble_window_set_relative_to:
733  * @window: a #GtkBubbleWindow
734  * @relative_to: a #GdkWindow
735  *
736  * Sets the #GdkWindow to act as the origin of coordinates of
737  * @window, or %NULL to use the root window. See
738  * gtk_bubble_window_set_pointing_to()
739  *
740  * If @window is currently visible, it will be moved to reflect
741  * this change.
742  *
743  * Since: 3.8
744  */
745 void
746 _gtk_bubble_window_set_relative_to (GtkBubbleWindow *window,
747                                     GdkWindow       *relative_to)
748 {
749   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
750   g_return_if_fail (!relative_to || GDK_IS_WINDOW (relative_to));
751
752   gtk_bubble_window_update_relative_to (window, relative_to);
753
754   if (gtk_widget_get_visible (GTK_WIDGET (window)))
755     gtk_bubble_window_update_position (window);
756 }
757
758 /*
759  * gtk_bubble_window_get_relative_to:
760  * @window: a #GtkBubbleWindow
761  *
762  * Returns the #GdkWindow used as the origin of coordinates.
763  * If @window is currently visible, it will be moved to reflect
764  * this change.
765  *
766  * Returns: the #GdkWindow @window is placed upon
767  *
768  * Since: 3.8
769  */
770 GdkWindow *
771 _gtk_bubble_window_get_relative_to (GtkBubbleWindow *window)
772 {
773   GtkBubbleWindowPrivate *priv;
774
775   g_return_val_if_fail (GTK_IS_BUBBLE_WINDOW (window), NULL);
776
777   priv = window->priv;
778
779   return priv->relative_to;
780 }
781
782 /*
783  * gtk_bubble_window_set_pointing_to:
784  * @window: a #GtkBubbleWindow
785  * @rect: rectangle to point to
786  *
787  * Sets the rectangle that @window will point to, the coordinates
788  * of this rectangle are relative to the #GdkWindow set through
789  * gtk_bubble_window_set_relative_to().
790  *
791  * Since: 3.8
792  */
793 void
794 _gtk_bubble_window_set_pointing_to (GtkBubbleWindow       *window,
795                                     cairo_rectangle_int_t *rect)
796 {
797   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
798   g_return_if_fail (rect != NULL);
799
800   gtk_bubble_window_update_pointing_to (window, rect);
801
802   if (gtk_widget_get_visible (GTK_WIDGET (window)))
803     gtk_bubble_window_update_position (window);
804 }
805
806 /*
807  * gtk_bubble_window_get_pointing_to:
808  * @window: a #GtkBubbleWindow
809  * @rect: (out): location to store the rectangle
810  *
811  * If a rectangle to point to is set, this function will return
812  * %TRUE and fill in @rect with the rectangle @window is currently
813  * pointing to.
814  *
815  * Returns: %TRUE if a rectangle is set
816  *
817  * Since: 3.8
818  */
819 gboolean
820 _gtk_bubble_window_get_pointing_to (GtkBubbleWindow       *window,
821                                     cairo_rectangle_int_t *rect)
822 {
823   GtkBubbleWindowPrivate *priv;
824
825   g_return_val_if_fail (GTK_IS_BUBBLE_WINDOW (window), FALSE);
826
827   priv = window->priv;
828
829   if (rect)
830     *rect = priv->pointing_to;
831
832   return priv->has_pointing_to;
833 }
834
835 /*
836  * gtk_bubble_window_set_position:
837  * @window: a #GtkBubbleWindow
838  * @position: preferred bubble position
839  *
840  * Sets the preferred position for @window to appear.
841  * If @window is currently visible, it will be moved to reflect
842  * this change.
843  *
844  * <note>
845  *   This preference will be respected where possible, although
846  *   on lack of space (eg. if close to the screen edges), the
847  *   #GtkBubbleWindow may choose to appear on the opposite side
848  * </note>
849  *
850  * Since: 3.8
851  */
852 void
853 _gtk_bubble_window_set_position (GtkBubbleWindow *window,
854                                  GtkPositionType  position)
855 {
856   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
857   g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
858
859   gtk_bubble_window_update_preferred_position (window, position);
860
861   if (gtk_widget_get_visible (GTK_WIDGET (window)))
862     gtk_bubble_window_update_position (window);
863 }
864
865 /*
866  * gtk_bubble_window_get_position:
867  * @window: a #GtkBubbleWindow
868  *
869  * Returns the preferred position to place @window
870  *
871  * Returns: The preferred position
872  *
873  * Since: 3.8
874  */
875 GtkPositionType
876 _gtk_bubble_window_get_position (GtkBubbleWindow *window)
877 {
878   GtkBubbleWindowPrivate *priv;
879
880   g_return_val_if_fail (GTK_IS_BUBBLE_WINDOW (window), GTK_POS_TOP);
881
882   priv = window->priv;
883
884   return priv->preferred_position;
885 }
886
887 /*
888  * gtk_bubble_window_popup:
889  * @window: a #GtkBubbleWindow
890  * @relative_to: #GdkWindow to position upon
891  * @pointing_to: rectangle to point to, in @relative_to coordinates
892  * @position: preferred position for @window
893  *
894  * This function sets atomically all #GtkBubbleWindow position
895  * parameters, and shows/updates @window
896  *
897  * Since: 3.8
898  */
899 void
900 _gtk_bubble_window_popup (GtkBubbleWindow       *window,
901                           GdkWindow             *relative_to,
902                           cairo_rectangle_int_t *pointing_to,
903                           GtkPositionType        position)
904 {
905   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
906   g_return_if_fail (!relative_to || GDK_IS_WINDOW (relative_to));
907   g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
908   g_return_if_fail (pointing_to != NULL);
909
910   gtk_bubble_window_update_preferred_position (window, position);
911   gtk_bubble_window_update_relative_to (window, relative_to);
912   gtk_bubble_window_update_pointing_to (window, pointing_to);
913
914   if (!gtk_widget_get_visible (GTK_WIDGET (window)))
915     gtk_widget_show (GTK_WIDGET (window));
916
917   gtk_bubble_window_update_position (window);
918 }
919
920 /*
921  * gtk_bubble_window_popdown:
922  * @window: a #GtkBubbleWindow
923  *
924  * Removes the window from the screen
925  *
926  * <note>
927  *   If a grab was previously added through gtk_bubble_window_grab(),
928  *   the grab will be removed by this function.
929  * </note>
930  *
931  * Since: 3.8
932  */
933 void
934 _gtk_bubble_window_popdown (GtkBubbleWindow *window)
935 {
936   GtkBubbleWindowPrivate *priv = window->priv;
937
938   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
939
940   if (priv->grabbed)
941     _gtk_bubble_window_ungrab (window);
942
943   if (gtk_widget_get_visible (GTK_WIDGET (window)))
944     gtk_widget_hide (GTK_WIDGET (window));
945 }
946
947 /*
948  * gtk_bubble_window_grab:
949  * @window: a #GtkBubbleWindow
950  * @device: a master #GdkDevice
951  * @activate_time: timestamp to perform the grab
952  *
953  * This function performs GDK and GTK+ grabs on @device and
954  * its paired #GdkDevice. After this call all pointer/keyboard
955  * events will be handled by @window.
956  *
957  * Calling this also brings in a #GtkMenu alike behavior, clicking
958  * outside the #GtkBubbleWindow or pressing the Escape key will
959  * popdown the menu by default.
960  *
961  * <note>
962  *   If there was a previous grab, it will be undone before doing
963  *   the requested grab.
964  * </note>
965  *
966  * Returns: %TRUE if the grab was successful
967  *
968  * Since: 3.8
969  */
970 gboolean
971 _gtk_bubble_window_grab (GtkBubbleWindow *window,
972                          GdkDevice       *device,
973                          guint32          activate_time)
974 {
975   GtkBubbleWindowPrivate *priv;
976   GdkDevice *other_device;
977   GdkWindow *grab_window;
978   GdkGrabStatus status;
979
980   g_return_val_if_fail (GTK_IS_BUBBLE_WINDOW (window), FALSE);
981   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
982   g_return_val_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER, FALSE);
983
984   priv = window->priv;
985
986   if (!priv->has_pointing_to ||
987       gdk_window_is_destroyed (priv->relative_to))
988     return FALSE;
989
990   if (priv->device)
991     _gtk_bubble_window_ungrab (window);
992
993   gtk_widget_realize (GTK_WIDGET (window));
994   grab_window = gtk_widget_get_window (GTK_WIDGET (window));
995   other_device = gdk_device_get_associated_device (device);
996
997   status = gdk_device_grab (device, grab_window,
998                             GDK_OWNERSHIP_WINDOW, TRUE, GRAB_EVENT_MASK,
999                             NULL, activate_time);
1000
1001   if (status == GDK_GRAB_SUCCESS)
1002     {
1003       status = gdk_device_grab (other_device, grab_window,
1004                                 GDK_OWNERSHIP_WINDOW, TRUE, GRAB_EVENT_MASK,
1005                                 NULL, activate_time);
1006
1007       /* Ungrab the first device on error */
1008       if (status != GDK_GRAB_SUCCESS)
1009         gdk_device_ungrab (device, activate_time);
1010     }
1011
1012   if (status == GDK_GRAB_SUCCESS)
1013     {
1014       gtk_device_grab_add (GTK_WIDGET (window), device, TRUE);
1015       priv->device = device;
1016     }
1017
1018   return status == GDK_GRAB_SUCCESS;
1019 }
1020
1021 /*
1022  * gtk_bubble_window_ungrab:
1023  * @window: a #GtkBubbleWindow
1024  *
1025  * This functions undoes a grab added through gtk_bubble_window_grab()
1026  * in this @window,
1027  *
1028  * Since: 3.8
1029  */
1030 void
1031 _gtk_bubble_window_ungrab (GtkBubbleWindow *window)
1032 {
1033   GtkBubbleWindowPrivate *priv;
1034
1035   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
1036
1037   priv = window->priv;
1038
1039   if (!priv->device)
1040     return;
1041
1042   gdk_device_ungrab (priv->device, GDK_CURRENT_TIME);
1043   gdk_device_ungrab (gdk_device_get_associated_device (priv->device),
1044                      GDK_CURRENT_TIME);
1045   gtk_device_grab_remove (GTK_WIDGET (window), priv->device);
1046   priv->device = NULL;
1047 }