1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.GestureSwipe;
26 
27 private import glib.ConstructionException;
28 private import gobject.ObjectG;
29 private import gobject.Signals;
30 private import gtk.Gesture;
31 private import gtk.GestureSingle;
32 private import gtk.Widget;
33 private import gtk.c.functions;
34 public  import gtk.c.types;
35 public  import gtkc.gtktypes;
36 private import std.algorithm;
37 
38 
39 /**
40  * #GtkGestureSwipe is a #GtkGesture implementation able to recognize
41  * swipes, after a press/move/.../move/release sequence happens, the
42  * #GtkGestureSwipe::swipe signal will be emitted, providing the velocity
43  * and directionality of the sequence at the time it was lifted.
44  * 
45  * If the velocity is desired in intermediate points,
46  * gtk_gesture_swipe_get_velocity() can be called on eg. a
47  * #GtkGesture::update handler.
48  * 
49  * All velocities are reported in pixels/sec units.
50  */
51 public class GestureSwipe : GestureSingle
52 {
53 	/** the main Gtk struct */
54 	protected GtkGestureSwipe* gtkGestureSwipe;
55 
56 	/** Get the main Gtk struct */
57 	public GtkGestureSwipe* getGestureSwipeStruct(bool transferOwnership = false)
58 	{
59 		if (transferOwnership)
60 			ownedRef = false;
61 		return gtkGestureSwipe;
62 	}
63 
64 	/** the main Gtk struct as a void* */
65 	protected override void* getStruct()
66 	{
67 		return cast(void*)gtkGestureSwipe;
68 	}
69 
70 	protected override void setStruct(GObject* obj)
71 	{
72 		gtkGestureSwipe = cast(GtkGestureSwipe*)obj;
73 		super.setStruct(obj);
74 	}
75 
76 	/**
77 	 * Sets our main struct and passes it to the parent class.
78 	 */
79 	public this (GtkGestureSwipe* gtkGestureSwipe, bool ownedRef = false)
80 	{
81 		this.gtkGestureSwipe = gtkGestureSwipe;
82 		super(cast(GtkGestureSingle*)gtkGestureSwipe, ownedRef);
83 	}
84 
85 
86 	/** */
87 	public static GType getType()
88 	{
89 		return gtk_gesture_swipe_get_type();
90 	}
91 
92 	/**
93 	 * Returns a newly created #GtkGesture that recognizes swipes.
94 	 *
95 	 * Params:
96 	 *     widget = a #GtkWidget
97 	 *
98 	 * Returns: a newly created #GtkGestureSwipe
99 	 *
100 	 * Since: 3.14
101 	 *
102 	 * Throws: ConstructionException GTK+ fails to create the object.
103 	 */
104 	public this(Widget widget)
105 	{
106 		auto p = gtk_gesture_swipe_new((widget is null) ? null : widget.getWidgetStruct());
107 
108 		if(p is null)
109 		{
110 			throw new ConstructionException("null returned by new");
111 		}
112 
113 		this(cast(GtkGestureSwipe*) p, true);
114 	}
115 
116 	/**
117 	 * If the gesture is recognized, this function returns %TRUE and fill in
118 	 * @velocity_x and @velocity_y with the recorded velocity, as per the
119 	 * last event(s) processed.
120 	 *
121 	 * Params:
122 	 *     velocityX = return value for the velocity in the X axis, in pixels/sec
123 	 *     velocityY = return value for the velocity in the Y axis, in pixels/sec
124 	 *
125 	 * Returns: whether velocity could be calculated
126 	 *
127 	 * Since: 3.14
128 	 */
129 	public bool getVelocity(out double velocityX, out double velocityY)
130 	{
131 		return gtk_gesture_swipe_get_velocity(gtkGestureSwipe, &velocityX, &velocityY) != 0;
132 	}
133 
134 	protected class OnSwipeDelegateWrapper
135 	{
136 		void delegate(double, double, GestureSwipe) dlg;
137 		gulong handlerId;
138 
139 		this(void delegate(double, double, GestureSwipe) dlg)
140 		{
141 			this.dlg = dlg;
142 			onSwipeListeners ~= this;
143 		}
144 
145 		void remove(OnSwipeDelegateWrapper source)
146 		{
147 			foreach(index, wrapper; onSwipeListeners)
148 			{
149 				if (wrapper.handlerId == source.handlerId)
150 				{
151 					onSwipeListeners[index] = null;
152 					onSwipeListeners = std.algorithm.remove(onSwipeListeners, index);
153 					break;
154 				}
155 			}
156 		}
157 	}
158 	OnSwipeDelegateWrapper[] onSwipeListeners;
159 
160 	/**
161 	 * This signal is emitted when the recognized gesture is finished, velocity
162 	 * and direction are a product of previously recorded events.
163 	 *
164 	 * Params:
165 	 *     velocityX = velocity in the X axis, in pixels/sec
166 	 *     velocityY = velocity in the Y axis, in pixels/sec
167 	 *
168 	 * Since: 3.14
169 	 */
170 	gulong addOnSwipe(void delegate(double, double, GestureSwipe) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
171 	{
172 		auto wrapper = new OnSwipeDelegateWrapper(dlg);
173 		wrapper.handlerId = Signals.connectData(
174 			this,
175 			"swipe",
176 			cast(GCallback)&callBackSwipe,
177 			cast(void*)wrapper,
178 			cast(GClosureNotify)&callBackSwipeDestroy,
179 			connectFlags);
180 		return wrapper.handlerId;
181 	}
182 
183 	extern(C) static void callBackSwipe(GtkGestureSwipe* gestureswipeStruct, double velocityX, double velocityY, OnSwipeDelegateWrapper wrapper)
184 	{
185 		wrapper.dlg(velocityX, velocityY, wrapper.outer);
186 	}
187 
188 	extern(C) static void callBackSwipeDestroy(OnSwipeDelegateWrapper wrapper, GClosure* closure)
189 	{
190 		wrapper.remove(wrapper);
191 	}
192 }