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.DragSource; 26 27 private import gdk.ContentProvider; 28 private import gdk.Drag; 29 private import gdk.PaintableIF; 30 private import glib.ConstructionException; 31 private import gobject.ObjectG; 32 private import gobject.Signals; 33 private import gtk.GestureSingle; 34 private import gtk.c.functions; 35 public import gtk.c.types; 36 private import std.algorithm; 37 38 39 /** 40 * `GtkDragSource` is an event controller to initiate Drag-And-Drop operations. 41 * 42 * `GtkDragSource` can be set up with the necessary 43 * ingredients for a DND operation ahead of time. This includes 44 * the source for the data that is being transferred, in the form 45 * of a [class@Gdk.ContentProvider], the desired action, and the icon to 46 * use during the drag operation. After setting it up, the drag 47 * source must be added to a widget as an event controller, using 48 * [method@Gtk.Widget.add_controller]. 49 * 50 * ```c 51 * static void 52 * my_widget_init (MyWidget *self) 53 * { 54 * GtkDragSource *drag_source = gtk_drag_source_new (); 55 * 56 * g_signal_connect (drag_source, "prepare", G_CALLBACK (on_drag_prepare), self); 57 * g_signal_connect (drag_source, "drag-begin", G_CALLBACK (on_drag_begin), self); 58 * 59 * gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drag_source)); 60 * } 61 * ``` 62 * 63 * Setting up the content provider and icon ahead of time only makes 64 * sense when the data does not change. More commonly, you will want 65 * to set them up just in time. To do so, `GtkDragSource` has 66 * [signal@Gtk.DragSource::prepare] and [signal@Gtk.DragSource::drag-begin] 67 * signals. 68 * 69 * The ::prepare signal is emitted before a drag is started, and 70 * can be used to set the content provider and actions that the 71 * drag should be started with. 72 * 73 * ```c 74 * static GdkContentProvider * 75 * on_drag_prepare (GtkDragSource *source, 76 * double x, 77 * double y, 78 * MyWidget *self) 79 * { 80 * // This widget supports two types of content: GFile objects 81 * // and GdkPixbuf objects; GTK will handle the serialization 82 * // of these types automatically 83 * GFile *file = my_widget_get_file (self); 84 * GdkPixbuf *pixbuf = my_widget_get_pixbuf (self); 85 * 86 * return gdk_content_provider_new_union ((GdkContentProvider *[2]) { 87 * gdk_content_provider_new_typed (G_TYPE_FILE, file), 88 * gdk_content_provider_new_typed (GDK_TYPE_PIXBUF, pixbuf), 89 * }, 2); 90 * } 91 * ``` 92 * 93 * The ::drag-begin signal is emitted after the `GdkDrag` object has 94 * been created, and can be used to set up the drag icon. 95 * 96 * ```c 97 * static void 98 * on_drag_begin (GtkDragSource *source, 99 * GtkDrag *drag, 100 * MyWidget *self) 101 * { 102 * // Set the widget as the drag icon 103 * GdkPaintable *paintable = gtk_widget_paintable_new (GTK_WIDGET (self)); 104 * gtk_drag_source_set_icon (source, paintable, 0, 0); 105 * g_object_unref (paintable); 106 * } 107 * ``` 108 * 109 * During the DND operation, `GtkDragSource` emits signals that 110 * can be used to obtain updates about the status of the operation, 111 * but it is not normally necessary to connect to any signals, 112 * except for one case: when the supported actions include 113 * %GDK_ACTION_MOVE, you need to listen for the 114 * [signal@Gtk.DragSource::drag-end] signal and delete the 115 * data after it has been transferred. 116 */ 117 public class DragSource : GestureSingle 118 { 119 /** the main Gtk struct */ 120 protected GtkDragSource* gtkDragSource; 121 122 /** Get the main Gtk struct */ 123 public GtkDragSource* getDragSourceStruct(bool transferOwnership = false) 124 { 125 if (transferOwnership) 126 ownedRef = false; 127 return gtkDragSource; 128 } 129 130 /** the main Gtk struct as a void* */ 131 protected override void* getStruct() 132 { 133 return cast(void*)gtkDragSource; 134 } 135 136 /** 137 * Sets our main struct and passes it to the parent class. 138 */ 139 public this (GtkDragSource* gtkDragSource, bool ownedRef = false) 140 { 141 this.gtkDragSource = gtkDragSource; 142 super(cast(GtkGestureSingle*)gtkDragSource, ownedRef); 143 } 144 145 146 /** */ 147 public static GType getType() 148 { 149 return gtk_drag_source_get_type(); 150 } 151 152 /** 153 * Creates a new `GtkDragSource` object. 154 * 155 * Returns: the new `GtkDragSource` 156 * 157 * Throws: ConstructionException GTK+ fails to create the object. 158 */ 159 public this() 160 { 161 auto __p = gtk_drag_source_new(); 162 163 if(__p is null) 164 { 165 throw new ConstructionException("null returned by new"); 166 } 167 168 this(cast(GtkDragSource*) __p, true); 169 } 170 171 /** 172 * Cancels a currently ongoing drag operation. 173 */ 174 public void dragCancel() 175 { 176 gtk_drag_source_drag_cancel(gtkDragSource); 177 } 178 179 /** 180 * Gets the actions that are currently set on the `GtkDragSource`. 181 * 182 * Returns: the actions set on @source 183 */ 184 public GdkDragAction getActions() 185 { 186 return gtk_drag_source_get_actions(gtkDragSource); 187 } 188 189 /** 190 * Gets the current content provider of a `GtkDragSource`. 191 * 192 * Returns: the `GdkContentProvider` of @source 193 */ 194 public ContentProvider getContent() 195 { 196 auto __p = gtk_drag_source_get_content(gtkDragSource); 197 198 if(__p is null) 199 { 200 return null; 201 } 202 203 return ObjectG.getDObject!(ContentProvider)(cast(GdkContentProvider*) __p); 204 } 205 206 /** 207 * Returns the underlying `GdkDrag` object for an ongoing drag. 208 * 209 * Returns: the `GdkDrag` of the current 210 * drag operation, or %NULL 211 */ 212 public Drag getDrag() 213 { 214 auto __p = gtk_drag_source_get_drag(gtkDragSource); 215 216 if(__p is null) 217 { 218 return null; 219 } 220 221 return ObjectG.getDObject!(Drag)(cast(GdkDrag*) __p); 222 } 223 224 /** 225 * Sets the actions on the `GtkDragSource`. 226 * 227 * During a DND operation, the actions are offered to potential 228 * drop targets. If @actions include %GDK_ACTION_MOVE, you need 229 * to listen to the [signal@Gtk.DragSource::drag-end] signal and 230 * handle @delete_data being %TRUE. 231 * 232 * This function can be called before a drag is started, 233 * or in a handler for the [signal@Gtk.DragSource::prepare] signal. 234 * 235 * Params: 236 * actions = the actions to offer 237 */ 238 public void setActions(GdkDragAction actions) 239 { 240 gtk_drag_source_set_actions(gtkDragSource, actions); 241 } 242 243 /** 244 * Sets a content provider on a `GtkDragSource`. 245 * 246 * When the data is requested in the cause of a DND operation, 247 * it will be obtained from the content provider. 248 * 249 * This function can be called before a drag is started, 250 * or in a handler for the [signal@Gtk.DragSource::prepare] signal. 251 * 252 * You may consider setting the content provider back to 253 * %NULL in a [signal@Gtk.DragSource::drag-end] signal handler. 254 * 255 * Params: 256 * content = a `GdkContentProvider`, or %NULL 257 */ 258 public void setContent(ContentProvider content) 259 { 260 gtk_drag_source_set_content(gtkDragSource, (content is null) ? null : content.getContentProviderStruct()); 261 } 262 263 /** 264 * Sets a paintable to use as icon during DND operations. 265 * 266 * The hotspot coordinates determine the point on the icon 267 * that gets aligned with the hotspot of the cursor. 268 * 269 * If @paintable is %NULL, a default icon is used. 270 * 271 * This function can be called before a drag is started, or in 272 * a [signal@Gtk.DragSource::prepare] or 273 * [signal@Gtk.DragSource::drag-begin] signal handler. 274 * 275 * Params: 276 * paintable = the #GdkPaintable to use as icon, or %NULL 277 * hotX = the hotspot X coordinate on the icon 278 * hotY = the hotspot Y coordinate on the icon 279 */ 280 public void setIcon(PaintableIF paintable, int hotX, int hotY) 281 { 282 gtk_drag_source_set_icon(gtkDragSource, (paintable is null) ? null : paintable.getPaintableStruct(), hotX, hotY); 283 } 284 285 /** 286 * Emitted on the drag source when a drag is started. 287 * 288 * It can be used to e.g. set a custom drag icon with 289 * [method@Gtk.DragSource.set_icon]. 290 * 291 * Params: 292 * drag = the `GdkDrag` object 293 */ 294 gulong addOnDragBegin(void delegate(Drag, DragSource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 295 { 296 return Signals.connect(this, "drag-begin", dlg, connectFlags ^ ConnectFlags.SWAPPED); 297 } 298 299 /** 300 * Emitted on the drag source when a drag has failed. 301 * 302 * The signal handler may handle a failed drag operation based on 303 * the type of error. It should return %TRUE if the failure has been handled 304 * and the default "drag operation failed" animation should not be shown. 305 * 306 * Params: 307 * drag = the `GdkDrag` object 308 * reason = information on why the drag failed 309 * 310 * Returns: %TRUE if the failed drag operation has been already handled 311 */ 312 gulong addOnDragCancel(bool delegate(Drag, GdkDragCancelReason, DragSource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 313 { 314 return Signals.connect(this, "drag-cancel", dlg, connectFlags ^ ConnectFlags.SWAPPED); 315 } 316 317 /** 318 * Emitted on the drag source when a drag is finished. 319 * 320 * A typical reason to connect to this signal is to undo 321 * things done in [signal@Gtk.DragSource::prepare] or 322 * [signal@Gtk.DragSource::drag-begin] handlers. 323 * 324 * Params: 325 * drag = the `GdkDrag` object 326 * deleteData = %TRUE if the drag was performing %GDK_ACTION_MOVE, 327 * and the data should be deleted 328 */ 329 gulong addOnDragEnd(void delegate(Drag, bool, DragSource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 330 { 331 return Signals.connect(this, "drag-end", dlg, connectFlags ^ ConnectFlags.SWAPPED); 332 } 333 334 /** 335 * Emitted when a drag is about to be initiated. 336 * 337 * It returns the `GdkContentProvider` to use for the drag that is about 338 * to start. The default handler for this signal returns the value of 339 * the [property@Gtk.DragSource:content] property, so if you set up that 340 * property ahead of time, you don't need to connect to this signal. 341 * 342 * Params: 343 * x = the X coordinate of the drag starting point 344 * y = the Y coordinate fo the drag starting point 345 * 346 * Returns: a `GdkContentProvider`, or %NULL 347 */ 348 gulong addOnPrepare(ContentProvider delegate(double, double, DragSource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 349 { 350 return Signals.connect(this, "prepare", dlg, connectFlags ^ ConnectFlags.SWAPPED); 351 } 352 }