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