aboutsummaryrefslogtreecommitdiffstats
path: root/lib/widgets/ephy-overlay-escaping-child.c
blob: 65ce8322ebec7bd7da524553ee0ffe88a304a74c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 *
 * Copyright © 2011 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "ephy-overlay-escaping-child.h"

#define EPHY_OVERLAY_ESCAPING_CHILD_DEFAULT_DISTANCE 20

G_DEFINE_TYPE (EphyOverlayEscapingChild, ephy_overlay_escaping_child, GEDIT_TYPE_OVERLAY_CHILD);

/* properties */
enum
{
  PROP_0,
  PROP_ESCAPING_DISTANCE
};

struct _EphyOverlayEscapingChildPrivate
{
  guint escaping_distance;
  GtkAllocation initial_allocation;
  GdkRectangle escaping_area;
};

/* If the pointer leaves the window, restore the widget position */
static gboolean
parent_leave_notify_event (GtkWidget *widget,
                           GdkEventMotion *event,
                           GtkWidget *parent)
{
  EphyOverlayEscapingChildPrivate *priv = EPHY_OVERLAY_ESCAPING_CHILD (widget)->priv;
  GtkAllocation alloc;

  gtk_widget_get_allocation (widget, &alloc);
  alloc.y = priv->initial_allocation.y;
  gtk_widget_size_allocate (widget, &alloc);

  return FALSE;
}

/* this should be in Gdk...really */
static gboolean
is_point_in_rectangle (int point_x,
                       int point_y,
                       GdkRectangle rectangle)
{
  int rectangle_x_higher_bound = rectangle.x + rectangle.width;
  int rectangle_y_higher_bound = rectangle.y + rectangle.height;

  return point_x >= rectangle.x && point_x < rectangle_x_higher_bound
    && point_y >= rectangle.y && point_y < rectangle_y_higher_bound;
}

/* Keep the widget-pointer distance at at least
 * EphyOverlayEscapingChildPrivate::escaping_distance by sliding the widget
 * away if needed.
 */
static gboolean
parent_motion_notify_event (GtkWidget *widget,
                            GdkEventMotion *event,
                            GtkWidget *parent)
{
  EphyOverlayEscapingChildPrivate *priv = EPHY_OVERLAY_ESCAPING_CHILD (widget)->priv;
  int distance_x, distance_y;
  GtkAllocation alloc;

  gtk_widget_get_allocation (widget, &alloc);

  if (is_point_in_rectangle (event->x, event->y, priv->escaping_area)) {
    gtk_widget_get_pointer (widget, &distance_x, &distance_y);
    alloc.y += priv->escaping_distance + distance_y;
  }
  else {
    /* Put the widget at its original position if we are out of the escaping
     * zone. Do nothing if it is already there.
     */
    if (alloc.y == priv->initial_allocation.y)
      return FALSE;
    alloc.y = priv->initial_allocation.y;
  }

  gtk_widget_size_allocate (widget, &alloc);

  return FALSE;
}

/* When the parent overlay is resized, the child relative position is modified.
 * So we update our initial_allocation to this new value and redefine our
 * escaping area.
 */
static void
parent_size_allocate (GtkWidget    *widget,
                      GdkRectangle *allocation,
                      GtkWidget      *parent)
{
  EphyOverlayEscapingChildPrivate *priv = EPHY_OVERLAY_ESCAPING_CHILD (widget)->priv;
  GtkAllocation initial_allocation;

  gtk_widget_get_allocation (widget, &initial_allocation);
  priv->escaping_area = priv->initial_allocation = initial_allocation;

  /* Define an escaping area around the widget.
   * Current implementation only handle horizontal lowerside widgets
   */
  priv->escaping_area.height += priv->escaping_distance;
  /* escape on both right and left */
  priv->escaping_area.width += 2 * priv->escaping_distance;
  priv->escaping_area.x -= priv->escaping_distance;
  priv->escaping_area.y -= priv->escaping_distance;
}

/* Install listeners on our overlay parents to locate the pointer
 * and our relative position.
 */
static void
ephy_overlay_escaping_child_parent_set (GtkWidget *widget,
                                        GtkWidget *previous_parent)
{
  GtkWidget *parent;

  if (previous_parent != NULL) {
    g_signal_handlers_disconnect_by_func (previous_parent,
                                          G_CALLBACK (parent_motion_notify_event),
                                          widget);
    g_signal_handlers_disconnect_by_func (previous_parent,
                                          G_CALLBACK (parent_leave_notify_event),
                                          widget);
    g_signal_handlers_disconnect_by_func (previous_parent,
                                          G_CALLBACK (parent_size_allocate),
                                          widget);
  }

  parent = gtk_widget_get_parent (widget);
  if (parent == NULL)
    return;

  g_signal_connect_swapped (parent,
                            "motion-notify-event",
                            G_CALLBACK (parent_motion_notify_event),
                            widget);
  g_signal_connect_swapped (parent,
                            "leave-notify-event",
                            G_CALLBACK (parent_leave_notify_event),
                            widget);
  g_signal_connect_swapped (parent,
                            "size-allocate",
                            G_CALLBACK (parent_size_allocate),
                            widget);

}

/* When the mouse is over us, translate the event coords and slide the widget
 * accordingly
 */
static gboolean
ephy_overlay_escaping_child_motion_notify_event (GtkWidget *widget,
                                                 GdkEventMotion *event)
{
  EphyOverlayEscapingChildPrivate *priv = EPHY_OVERLAY_ESCAPING_CHILD (widget)->priv;

  event->x += priv->initial_allocation.x;
  event->y += priv->initial_allocation.y;
  return parent_motion_notify_event (widget, event, gtk_widget_get_parent (widget));
}

/* Make our event window propagate mouse motion events, so we can slide the widget,
 * when hovered.
 */
static void
ephy_overlay_escaping_child_realize (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (ephy_overlay_escaping_child_parent_class)->realize (widget);
  GdkWindow *window = gtk_widget_get_window (widget);
  GdkEventMask events = gdk_window_get_events (window);
  events |= GDK_POINTER_MOTION_MASK;
  gdk_window_set_events (window, events);
}

static void
ephy_overlay_escaping_child_get_property (GObject *object,
                                          guint property_id,
                                          GValue *value,
                                          GParamSpec *pspec)
{
  EphyOverlayEscapingChild *self = EPHY_OVERLAY_ESCAPING_CHILD (object);
  EphyOverlayEscapingChildPrivate *priv = self->priv;

  switch (property_id) {
  case PROP_ESCAPING_DISTANCE:
    g_value_set_uint (value, priv->escaping_distance);
  break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  break;
  }
}

static void
ephy_overlay_escaping_child_set_property (GObject *object,
                                          guint property_id,
                                          const GValue *value,
                                          GParamSpec *pspec)
{
  EphyOverlayEscapingChild *self = EPHY_OVERLAY_ESCAPING_CHILD (object);
  EphyOverlayEscapingChildPrivate *priv = self->priv;

  switch (property_id)
  {
  case PROP_ESCAPING_DISTANCE:
    priv->escaping_distance = g_value_get_uint (value);
  break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  break;
  }
}

static void
ephy_overlay_escaping_child_class_init (EphyOverlayEscapingChildClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  g_type_class_add_private (klass, sizeof(EphyOverlayEscapingChildPrivate));

  gobject_class->get_property = ephy_overlay_escaping_child_get_property;
  gobject_class->set_property = ephy_overlay_escaping_child_set_property;

  widget_class->parent_set = ephy_overlay_escaping_child_parent_set;
  widget_class->motion_notify_event = ephy_overlay_escaping_child_motion_notify_event;
  widget_class->realize = ephy_overlay_escaping_child_realize;

  g_object_class_install_property (gobject_class,
                                   PROP_ESCAPING_DISTANCE,
                                   g_param_spec_uint ("escaping-distance",
                                                      "Escaping distance",
                                                      "Maximum distance between the mouse pointer and the widget",
                                                      0,
                                                      G_MAXUINT,
                                                      EPHY_OVERLAY_ESCAPING_CHILD_DEFAULT_DISTANCE,
                                                      G_PARAM_CONSTRUCT_ONLY |
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_STATIC_STRINGS));
}

static void
ephy_overlay_escaping_child_init (EphyOverlayEscapingChild *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                            EPHY_TYPE_OVERLAY_ESCAPING_CHILD,
                                            EphyOverlayEscapingChildPrivate);
}

/**
 * ephy_overlay_escaping_child_new:
 * @widget: the wrapped #GtkWidget
 * @escaping_distance: the distance from which the widget escapes the mouse
 * pointer
 *
 * Creates a new #EphyOverlayEscapingChild object wrapping the provided
 * widget. The widget will stay at a minimal distance of @escaping_distance pixel
 * from the mouse pointer.
 *
 * Returns: a new #EphyOverlayEscapingChild object
 */
EphyOverlayEscapingChild *
ephy_overlay_escaping_child_new_with_distance (GtkWidget *widget,
                                               guint escaping_distance)
{
  return g_object_new (EPHY_TYPE_OVERLAY_ESCAPING_CHILD,
                       "widget", widget,
                       "escaping-distance", escaping_distance,
                       NULL);
}

/**
 * ephy_overlay_escaping_child_new:
 * @widget: the wrapped #GtkWidget
 *
 * Creates a new #EphyOverlayEscapingChild object wrapping the provided
 * widget. The widget will stay at a minimal distance of 20 pixels from
 * the mouse pointer.
 *
 * Returns: a new #EphyOverlayEscapingChild object
 */
EphyOverlayEscapingChild *
ephy_overlay_escaping_child_new (GtkWidget *widget)
{
  return g_object_new (EPHY_TYPE_OVERLAY_ESCAPING_CHILD,
                       "widget", widget,
                       NULL);
}