root/gtkimageview/src/mouse_handler.c

Revision 201, 3.5 kB (checked in by bjourne, 6 years ago)

Introduce a new test in tests/test-tool-selector.c and fix the
existing one which complained because the window. Also, don't mind if
the grab fails, it isn't critical.

Line 
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; coding: utf-8 -*-
2  *
3  * Copyright © 2007 Björn Lindqvist <bjourne@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2, or
8  * (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <stdlib.h>
21 #include "mouse_handler.h"
22
23 /**
24  * mouse_handler_new:
25  * @grab_cursor: Cursor to use when grabbing.
26  * @returns: A new #MouseHandler
27  *
28  * Creates a new mouse handler. Note that the mouse handler never
29  * owns, the passed in @grab_cursor -- you must always free it
30  * yourself.
31  **/
32 MouseHandler *
33 mouse_handler_new (GdkCursor *grab_cursor)
34 {
35     MouseHandler *mh = g_new0 (MouseHandler, 1);
36     mh->pressed = FALSE;
37     mh->dragging = FALSE;
38     mh->drag_base.x = 0;
39     mh->drag_base.y = 0;
40     mh->drag_ofs.x = 0;
41     mh->drag_ofs.y = 0;
42     mh->grab_cursor = grab_cursor;
43     return mh;
44 }
45
46 static gboolean
47 mouse_handler_grab_pointer (MouseHandler *mh,
48                             GdkWindow    *window,
49                             guint32       time)
50 {
51     int mask = (GDK_POINTER_MOTION_MASK
52                 | GDK_POINTER_MOTION_HINT_MASK
53                 | GDK_BUTTON_RELEASE_MASK);
54     int retval = gdk_pointer_grab (window,
55                                    FALSE,
56                                    mask,
57                                    NULL,
58                                    mh->grab_cursor,
59                                    time);
60     return retval == GDK_GRAB_SUCCESS;
61                
62 }
63
64 /**
65  * mouse_handler_button_press:
66  * @mh: a #MouseHandler
67  * @ev: the #GdkEventButton event to handle.
68  * @returns: %TRUE if the event was handled (left mouse button was
69  *      pressed), %FALSE otherwise.
70  *
71  * Handles a button press event. If left mouse button is pressed an
72  * attempt to grab the pointer is made. The drag base and drag offset
73  * is reset to the coordinate for the button event.
74  **/
75 gboolean
76 mouse_handler_button_press (MouseHandler   *mh,
77                             GdkEventButton *ev)
78 {
79     if (ev->button != 1)
80         return FALSE;
81     mouse_handler_grab_pointer (mh, ev->window, ev->time);
82     mh->pressed = TRUE;
83     mh->drag_base.x = ev->x;
84     mh->drag_base.y = ev->y;
85     mh->drag_ofs.x = ev->x;
86     mh->drag_ofs.y = ev->y;
87     return TRUE;
88 }
89
90 gboolean
91 mouse_handler_button_release (MouseHandler   *mh,
92                               GdkEventButton *ev)
93 {
94     if (ev->button != 1)
95         return FALSE;
96     gdk_pointer_ungrab (ev->time);
97     mh->pressed = FALSE;
98     mh->dragging = FALSE;
99     return FALSE;
100 }
101
102 void
103 mouse_handler_motion_notify (MouseHandler   *mh,
104                              GdkEventMotion *ev)
105 {
106     if (mh->pressed)
107         mh->dragging = TRUE;
108
109     mh->drag_ofs.x = ev->x;
110     mh->drag_ofs.y = ev->y;
111 }
112
113 Pos
114 mouse_handler_get_drag_delta (MouseHandler *mh)
115 {
116     Pos p;
117     p.x = mh->drag_base.x - mh->drag_ofs.x;
118     p.y = mh->drag_base.y - mh->drag_ofs.y;
119     return p;
120 }
121
Note: See TracBrowser for help on using the browser.