Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/src/sugar-push-scroller.c
blob: aaff3f6604f5f00435b7177fc83ab39f8cbe0415 (plain)
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
/*
 *  Copyright © 2002 Ricardo Fernádez Pascual
 *  Copyright © 2005 Crispin Flowerday
 *  Copyright © 2005 Christian Persch
 *  Copyright © 2005 Samuel Abels
 *  Copyright (C) 2006, Red Hat, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id: ephy-push-scroller.c,v 1.6 2006/09/13 19:01:24 chpe Exp $
 */

/* this file is based on work of Daniel Erat for galeon 1 */

#include "sugar-push-scroller.h"

#include <gtk/gtkimage.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkmain.h>

#include <stdlib.h>

#define SUGAR_PUSH_SCROLLER_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), SUGAR_TYPE_PUSH_SCROLLER, SugarPushScrollerPrivate))

struct _SugarPushScrollerPrivate
{
	SugarBrowser *browser;
	GdkCursor *cursor;
	guint start_x;
	guint start_y;
	guint active : 1;
};

G_DEFINE_TYPE(SugarPushScroller, sugar_push_scroller, G_TYPE_OBJECT)

/* private functions */

static gboolean
sugar_push_scroller_motion_cb(GtkWidget         *widget,
			      			  GdkEventMotion    *event,
			      			  SugarPushScroller *scroller)
{
	SugarPushScrollerPrivate *priv = scroller->priv;
	int x_dist, x_dist_abs, y_dist, y_dist_abs;

	if (!priv->active) {
		return FALSE;
	}

	/* get distance between last known cursor position and cursor */
	x_dist = priv->start_x - event->x_root;
	x_dist_abs = abs(x_dist);
	y_dist = priv->start_y - event->y_root;
	y_dist_abs = abs(y_dist);

	/* scroll */
	sugar_browser_scroll_pixels(priv->browser, x_dist, y_dist);

	priv->start_x = event->x_root;
	priv->start_y = event->y_root;

	return TRUE;
}

/* public functions */

void
sugar_push_scroller_start (SugarPushScroller *scroller,
			  			   SugarBrowser      *browser,
			  			   int                x,
			  			   int				  y)
{
	SugarPushScrollerPrivate *priv = scroller->priv;
	GtkWidget *widget, *window;
	guint32 timestamp;

	g_return_if_fail (browser != NULL);

	if (priv->active)
		return;

	if (gdk_pointer_is_grabbed ())
		return;

	priv->active = TRUE;

	/* FIXME is this good enough? */
	timestamp = gtk_get_current_event_time();

	g_object_ref (scroller);

	priv->browser = browser;

	window = gtk_widget_get_toplevel(GTK_WIDGET(browser));
	g_object_ref (window);

	/* set positions */
	priv->start_x = x;
	priv->start_y = y;

	g_signal_connect(window, "motion-notify-event",
			         G_CALLBACK (sugar_push_scroller_motion_cb), scroller);

	/* grab the pointer */
	widget = GTK_WIDGET(window);
	gtk_grab_add(widget);
	if (gdk_pointer_grab(widget->window, FALSE,
			      GDK_POINTER_MOTION_MASK |
			      GDK_BUTTON_PRESS_MASK |
			      GDK_BUTTON_RELEASE_MASK,
			      NULL, priv->cursor, timestamp) != GDK_GRAB_SUCCESS) {
		sugar_push_scroller_stop(scroller, timestamp);
		return;
	}
}

void
sugar_push_scroller_stop (SugarPushScroller *scroller,
			 			  guint32            timestamp)
{
	SugarPushScrollerPrivate *priv = scroller->priv;
	GtkWidget *widget, *window;

	if (priv->active == FALSE)
		return;

	window = gtk_widget_get_toplevel(GTK_WIDGET(priv->browser));

	/* disconnect the signals before ungrabbing! */
	g_signal_handlers_disconnect_matched (window,
					      G_SIGNAL_MATCH_DATA, 0, 0, 
					      NULL, NULL, scroller);

	/* ungrab the pointer if it's grabbed */
	if (gdk_pointer_is_grabbed())
	{
		gdk_pointer_ungrab(timestamp);
	}

	gdk_keyboard_ungrab(timestamp);

	widget = GTK_WIDGET(window);
	gtk_grab_remove(widget);

	g_object_unref(window);

	priv->browser = NULL;
	priv->active = FALSE;

	g_object_unref(scroller);
}

/* class implementation */

static void 
sugar_push_scroller_init (SugarPushScroller *scroller)
{
	SugarPushScrollerPrivate *priv;
	priv = scroller->priv = SUGAR_PUSH_SCROLLER_GET_PRIVATE(scroller);
	priv->active = FALSE;
	priv->cursor = gdk_cursor_new(GDK_FLEUR);
}

static void
sugar_push_scroller_finalize (GObject *object)
{
	SugarPushScroller *scroller = SUGAR_PUSH_SCROLLER(object);
	SugarPushScrollerPrivate *priv = scroller->priv;

	gdk_cursor_unref(priv->cursor);

	G_OBJECT_CLASS(sugar_push_scroller_parent_class)->finalize(object);
}

static void
sugar_push_scroller_class_init (SugarPushScrollerClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	object_class->finalize = sugar_push_scroller_finalize;

	g_type_class_add_private(klass, sizeof (SugarPushScrollerPrivate));
}