1 /*
2  * This file is part of d-handy.
3  *
4  * d-handy 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  * d-handy 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 d-handy; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module handy.SwipeTracker;
20 
21 private import glib.ConstructionException;
22 private import gobject.ObjectG;
23 private import gobject.Signals;
24 private import gtk.OrientableIF;
25 private import gtk.OrientableT;
26 private import handy.SwipeableIF;
27 private import handy.c.functions;
28 public  import handy.c.types;
29 private import std.algorithm;
30 
31 
32 /** */
33 public class SwipeTracker : ObjectG, OrientableIF
34 {
35 	/** the main Gtk struct */
36 	protected HdySwipeTracker* hdySwipeTracker;
37 
38 	/** Get the main Gtk struct */
39 	public HdySwipeTracker* getSwipeTrackerStruct(bool transferOwnership = false)
40 	{
41 		if (transferOwnership)
42 			ownedRef = false;
43 		return hdySwipeTracker;
44 	}
45 
46 	/** the main Gtk struct as a void* */
47 	protected override void* getStruct()
48 	{
49 		return cast(void*)hdySwipeTracker;
50 	}
51 
52 	/**
53 	 * Sets our main struct and passes it to the parent class.
54 	 */
55 	public this (HdySwipeTracker* hdySwipeTracker, bool ownedRef = false)
56 	{
57 		this.hdySwipeTracker = hdySwipeTracker;
58 		super(cast(GObject*)hdySwipeTracker, ownedRef);
59 	}
60 
61 	// add the Orientable capabilities
62 	mixin OrientableT!(HdySwipeTracker);
63 
64 
65 	/** */
66 	public static GType getType()
67 	{
68 		return hdy_swipe_tracker_get_type();
69 	}
70 
71 	/**
72 	 * Create a new #HdySwipeTracker object on @widget.
73 	 *
74 	 * Params:
75 	 *     swipeable = a #GtkWidget to add the tracker on
76 	 *
77 	 * Returns: the newly created #HdySwipeTracker object
78 	 *
79 	 * Since: 1.0
80 	 *
81 	 * Throws: ConstructionException GTK+ fails to create the object.
82 	 */
83 	public this(SwipeableIF swipeable)
84 	{
85 		auto __p = hdy_swipe_tracker_new((swipeable is null) ? null : swipeable.getSwipeableStruct());
86 
87 		if(__p is null)
88 		{
89 			throw new ConstructionException("null returned by new");
90 		}
91 
92 		this(cast(HdySwipeTracker*) __p, true);
93 	}
94 
95 	/**
96 	 * Get whether @self can be dragged with mouse pointer.
97 	 *
98 	 * Returns: %TRUE is mouse dragging is allowed
99 	 *
100 	 * Since: 1.0
101 	 */
102 	public bool getAllowMouseDrag()
103 	{
104 		return hdy_swipe_tracker_get_allow_mouse_drag(hdySwipeTracker) != 0;
105 	}
106 
107 	/**
108 	 * Get whether @self is enabled. When it's not enabled, no events will be
109 	 * processed. Generally widgets will want to expose this via a property.
110 	 *
111 	 * Returns: %TRUE if @self is enabled
112 	 *
113 	 * Since: 1.0
114 	 */
115 	public bool getEnabled()
116 	{
117 		return hdy_swipe_tracker_get_enabled(hdySwipeTracker) != 0;
118 	}
119 
120 	/**
121 	 * Get whether @self is reversing the swipe direction.
122 	 *
123 	 * Returns: %TRUE is the direction is reversed
124 	 *
125 	 * Since: 1.0
126 	 */
127 	public bool getReversed()
128 	{
129 		return hdy_swipe_tracker_get_reversed(hdySwipeTracker) != 0;
130 	}
131 
132 	/**
133 	 * Get @self's swipeable widget.
134 	 *
135 	 * Returns: the swipeable widget
136 	 *
137 	 * Since: 1.0
138 	 */
139 	public SwipeableIF getSwipeable()
140 	{
141 		auto __p = hdy_swipe_tracker_get_swipeable(hdySwipeTracker);
142 
143 		if(__p is null)
144 		{
145 			return null;
146 		}
147 
148 		return ObjectG.getDObject!(SwipeableIF)(cast(HdySwipeable*) __p);
149 	}
150 
151 	/**
152 	 * Set whether @self can be dragged with mouse pointer. This should usually be
153 	 * %FALSE.
154 	 *
155 	 * Params:
156 	 *     allowMouseDrag = whether to allow mouse dragging
157 	 *
158 	 * Since: 1.0
159 	 */
160 	public void setAllowMouseDrag(bool allowMouseDrag)
161 	{
162 		hdy_swipe_tracker_set_allow_mouse_drag(hdySwipeTracker, allowMouseDrag);
163 	}
164 
165 	/**
166 	 * Set whether @self is enabled. When it's not enabled, no events will be
167 	 * processed. Usually widgets will want to expose this via a property.
168 	 *
169 	 * Params:
170 	 *     enabled = whether to enable to swipe tracker
171 	 *
172 	 * Since: 1.0
173 	 */
174 	public void setEnabled(bool enabled)
175 	{
176 		hdy_swipe_tracker_set_enabled(hdySwipeTracker, enabled);
177 	}
178 
179 	/**
180 	 * Set whether to reverse the swipe direction. If @self is horizontal,
181 	 * can be used for supporting RTL text direction.
182 	 *
183 	 * Params:
184 	 *     reversed = whether to reverse the swipe direction
185 	 *
186 	 * Since: 1.0
187 	 */
188 	public void setReversed(bool reversed)
189 	{
190 		hdy_swipe_tracker_set_reversed(hdySwipeTracker, reversed);
191 	}
192 
193 	/**
194 	 * Move the current progress value by @delta. This can be used to adjust the
195 	 * current position if snap points move during the gesture.
196 	 *
197 	 * Params:
198 	 *     delta = the position delta
199 	 *
200 	 * Since: 1.0
201 	 */
202 	public void shiftPosition(double delta)
203 	{
204 		hdy_swipe_tracker_shift_position(hdySwipeTracker, delta);
205 	}
206 
207 	/**
208 	 * This signal is emitted when a possible swipe is detected.
209 	 *
210 	 * The @direction value can be used to restrict the swipe to a certain
211 	 * direction.
212 	 *
213 	 * Params:
214 	 *     direction = The direction of the swipe
215 	 *     direct = %TRUE if the swipe is directly triggered by a gesture,
216 	 *         %FALSE if it's triggered via a #HdySwipeGroup
217 	 *
218 	 * Since: 1.0
219 	 */
220 	gulong addOnBeginSwipe(void delegate(HdyNavigationDirection, bool, SwipeTracker) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
221 	{
222 		return Signals.connect(this, "begin-swipe", dlg, connectFlags ^ ConnectFlags.SWAPPED);
223 	}
224 
225 	/**
226 	 * This signal is emitted as soon as the gesture has stopped.
227 	 *
228 	 * Params:
229 	 *     duration = Snap-back animation duration in milliseconds
230 	 *     to = The progress value to animate to
231 	 *
232 	 * Since: 1.0
233 	 */
234 	gulong addOnEndSwipe(void delegate(long, double, SwipeTracker) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
235 	{
236 		return Signals.connect(this, "end-swipe", dlg, connectFlags ^ ConnectFlags.SWAPPED);
237 	}
238 
239 	/**
240 	 * This signal is emitted every time the progress value changes.
241 	 *
242 	 * Params:
243 	 *     progress = The current animation progress value
244 	 *
245 	 * Since: 1.0
246 	 */
247 	gulong addOnUpdateSwipe(void delegate(double, SwipeTracker) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
248 	{
249 		return Signals.connect(this, "update-swipe", dlg, connectFlags ^ ConnectFlags.SWAPPED);
250 	}
251 }