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 gtkc.gtk;
34 public  import gtkc.gtktypes;
35 private import std.algorithm;
36 
37 
38 /**
39  * #GtkGestureSwipe is a #GtkGesture implementation able to recognize
40  * swipes, after a press/move/.../move/release sequence happens, the
41  * #GtkGestureSwipe::swipe signal will be emitted, providing the velocity
42  * and directionality of the sequence at the time it was lifted.
43  * 
44  * If the velocity is desired in intermediate points,
45  * gtk_gesture_swipe_get_velocity() can be called on eg. a
46  * #GtkGesture::update handler.
47  * 
48  * All velocities are reported in pixels/sec units.
49  */
50 public class GestureSwipe : GestureSingle
51 {
52 	/** the main Gtk struct */
53 	protected GtkGestureSwipe* gtkGestureSwipe;
54 
55 	/** Get the main Gtk struct */
56 	public GtkGestureSwipe* getGestureSwipeStruct(bool transferOwnership = false)
57 	{
58 		if (transferOwnership)
59 			ownedRef = false;
60 		return gtkGestureSwipe;
61 	}
62 
63 	/** the main Gtk struct as a void* */
64 	protected override void* getStruct()
65 	{
66 		return cast(void*)gtkGestureSwipe;
67 	}
68 
69 	protected override void setStruct(GObject* obj)
70 	{
71 		gtkGestureSwipe = cast(GtkGestureSwipe*)obj;
72 		super.setStruct(obj);
73 	}
74 
75 	/**
76 	 * Sets our main struct and passes it to the parent class.
77 	 */
78 	public this (GtkGestureSwipe* gtkGestureSwipe, bool ownedRef = false)
79 	{
80 		this.gtkGestureSwipe = gtkGestureSwipe;
81 		super(cast(GtkGestureSingle*)gtkGestureSwipe, ownedRef);
82 	}
83 
84 
85 	/** */
86 	public static GType getType()
87 	{
88 		return gtk_gesture_swipe_get_type();
89 	}
90 
91 	/**
92 	 * Returns a newly created #GtkGesture that recognizes swipes.
93 	 *
94 	 * Params:
95 	 *     widget = a #GtkWidget
96 	 *
97 	 * Returns: a newly created #GtkGestureSwipe
98 	 *
99 	 * Since: 3.14
100 	 *
101 	 * Throws: ConstructionException GTK+ fails to create the object.
102 	 */
103 	public this(Widget widget)
104 	{
105 		auto p = gtk_gesture_swipe_new((widget is null) ? null : widget.getWidgetStruct());
106 		
107 		if(p is null)
108 		{
109 			throw new ConstructionException("null returned by new");
110 		}
111 		
112 		this(cast(GtkGestureSwipe*) p, true);
113 	}
114 
115 	/**
116 	 * If the gesture is recognized, this function returns %TRUE and fill in
117 	 * @velocity_x and @velocity_y with the recorded velocity, as per the
118 	 * last event(s) processed.
119 	 *
120 	 * Params:
121 	 *     velocityX = return value for the velocity in the X axis, in pixels/sec
122 	 *     velocityY = return value for the velocity in the Y axis, in pixels/sec
123 	 *
124 	 * Returns: whether velocity could be calculated
125 	 *
126 	 * Since: 3.14
127 	 */
128 	public bool getVelocity(out double velocityX, out double velocityY)
129 	{
130 		return gtk_gesture_swipe_get_velocity(gtkGestureSwipe, &velocityX, &velocityY) != 0;
131 	}
132 
133 	protected class OnSwipeDelegateWrapper
134 	{
135 		static OnSwipeDelegateWrapper[] listeners;
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 			this.listeners ~= this;
143 		}
144 		
145 		void remove(OnSwipeDelegateWrapper source)
146 		{
147 			foreach(index, wrapper; listeners)
148 			{
149 				if (wrapper.handlerId == source.handlerId)
150 				{
151 					listeners[index] = null;
152 					listeners = std.algorithm.remove(listeners, index);
153 					break;
154 				}
155 			}
156 		}
157 	}
158 
159 	/**
160 	 * This signal is emitted when the recognized gesture is finished, velocity
161 	 * and direction are a product of previously recorded events.
162 	 *
163 	 * Params:
164 	 *     velocityX = velocity in the X axis, in pixels/sec
165 	 *     velocityY = velocity in the Y axis, in pixels/sec
166 	 *
167 	 * Since: 3.14
168 	 */
169 	gulong addOnSwipe(void delegate(double, double, GestureSwipe) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
170 	{
171 		auto wrapper = new OnSwipeDelegateWrapper(dlg);
172 		wrapper.handlerId = Signals.connectData(
173 			this,
174 			"swipe",
175 			cast(GCallback)&callBackSwipe,
176 			cast(void*)wrapper,
177 			cast(GClosureNotify)&callBackSwipeDestroy,
178 			connectFlags);
179 		return wrapper.handlerId;
180 	}
181 	
182 	extern(C) static void callBackSwipe(GtkGestureSwipe* gestureswipeStruct, double velocityX, double velocityY, OnSwipeDelegateWrapper wrapper)
183 	{
184 		wrapper.dlg(velocityX, velocityY, wrapper.outer);
185 	}
186 	
187 	extern(C) static void callBackSwipeDestroy(OnSwipeDelegateWrapper wrapper, GClosure* closure)
188 	{
189 		wrapper.remove(wrapper);
190 	}
191 }