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.SelectionData;
26 
27 private import gdk.Display;
28 private import gdkpixbuf.Pixbuf;
29 private import glib.Str;
30 private import gobject.ObjectG;
31 private import gtk.TargetEntry;
32 private import gtk.TextBuffer;
33 private import gtk.Widget;
34 private import gtk.c.functions;
35 public  import gtk.c.types;
36 public  import gtkc.gtktypes;
37 private import gtkd.Loader;
38 
39 
40 /** */
41 public class SelectionData
42 {
43 	/** the main Gtk struct */
44 	protected GtkSelectionData* gtkSelectionData;
45 	protected bool ownedRef;
46 
47 	/** Get the main Gtk struct */
48 	public GtkSelectionData* getSelectionDataStruct(bool transferOwnership = false)
49 	{
50 		if (transferOwnership)
51 			ownedRef = false;
52 		return gtkSelectionData;
53 	}
54 
55 	/** the main Gtk struct as a void* */
56 	protected void* getStruct()
57 	{
58 		return cast(void*)gtkSelectionData;
59 	}
60 
61 	/**
62 	 * Sets our main struct and passes it to the parent class.
63 	 */
64 	public this (GtkSelectionData* gtkSelectionData, bool ownedRef = false)
65 	{
66 		this.gtkSelectionData = gtkSelectionData;
67 		this.ownedRef = ownedRef;
68 	}
69 
70 	~this ()
71 	{
72 		if ( Linker.isLoaded(LIBRARY_GTK) && ownedRef )
73 			gtk_selection_data_free(gtkSelectionData);
74 	}
75 
76 	/**
77 	 * Retrieves the raw data of the selection.
78 	 *
79 	 * Return: the raw data of the selection.
80 	 *
81 	 * Since: 2.14
82 	 */
83 	public char* getData()
84 	{
85 		return gtk_selection_data_get_data(gtkSelectionData);
86 	}
87 
88 	/**
89 	 */
90 
91 	/** */
92 	public static GType getType()
93 	{
94 		return gtk_selection_data_get_type();
95 	}
96 
97 	/**
98 	 * Makes a copy of a #GtkSelectionData-struct and its data.
99 	 *
100 	 * Returns: a pointer to a copy of @data.
101 	 */
102 	public SelectionData copy()
103 	{
104 		auto p = gtk_selection_data_copy(gtkSelectionData);
105 
106 		if(p is null)
107 		{
108 			return null;
109 		}
110 
111 		return ObjectG.getDObject!(SelectionData)(cast(GtkSelectionData*) p, true);
112 	}
113 
114 	/**
115 	 * Frees a #GtkSelectionData-struct returned from
116 	 * gtk_selection_data_copy().
117 	 */
118 	public void free()
119 	{
120 		gtk_selection_data_free(gtkSelectionData);
121 		ownedRef = false;
122 	}
123 
124 	/**
125 	 * Retrieves the data type of the selection.
126 	 *
127 	 * Returns: the data type of the selection.
128 	 *
129 	 * Since: 2.14
130 	 */
131 	public GdkAtom getDataType()
132 	{
133 		return gtk_selection_data_get_data_type(gtkSelectionData);
134 	}
135 
136 	/**
137 	 * Retrieves the raw data of the selection along with its length.
138 	 *
139 	 * Returns: the raw data of the selection
140 	 *
141 	 * Since: 3.0
142 	 */
143 	public char[] getDataWithLength()
144 	{
145 		int length;
146 
147 		auto p = gtk_selection_data_get_data_with_length(gtkSelectionData, &length);
148 
149 		return p[0 .. length];
150 	}
151 
152 	/**
153 	 * Retrieves the display of the selection.
154 	 *
155 	 * Returns: the display of the selection.
156 	 *
157 	 * Since: 2.14
158 	 */
159 	public Display getDisplay()
160 	{
161 		auto p = gtk_selection_data_get_display(gtkSelectionData);
162 
163 		if(p is null)
164 		{
165 			return null;
166 		}
167 
168 		return ObjectG.getDObject!(Display)(cast(GdkDisplay*) p);
169 	}
170 
171 	/**
172 	 * Retrieves the format of the selection.
173 	 *
174 	 * Returns: the format of the selection.
175 	 *
176 	 * Since: 2.14
177 	 */
178 	public int getFormat()
179 	{
180 		return gtk_selection_data_get_format(gtkSelectionData);
181 	}
182 
183 	/**
184 	 * Retrieves the length of the raw data of the selection.
185 	 *
186 	 * Returns: the length of the data of the selection.
187 	 *
188 	 * Since: 2.14
189 	 */
190 	public int getLength()
191 	{
192 		return gtk_selection_data_get_length(gtkSelectionData);
193 	}
194 
195 	/**
196 	 * Gets the contents of the selection data as a #GdkPixbuf.
197 	 *
198 	 * Returns: if the selection data
199 	 *     contained a recognized image type and it could be converted to a
200 	 *     #GdkPixbuf, a newly allocated pixbuf is returned, otherwise
201 	 *     %NULL.  If the result is non-%NULL it must be freed with
202 	 *     g_object_unref().
203 	 *
204 	 * Since: 2.6
205 	 */
206 	public Pixbuf getPixbuf()
207 	{
208 		auto p = gtk_selection_data_get_pixbuf(gtkSelectionData);
209 
210 		if(p is null)
211 		{
212 			return null;
213 		}
214 
215 		return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p, true);
216 	}
217 
218 	/**
219 	 * Retrieves the selection #GdkAtom of the selection data.
220 	 *
221 	 * Returns: the selection #GdkAtom of the selection data.
222 	 *
223 	 * Since: 2.16
224 	 */
225 	public GdkAtom getSelection()
226 	{
227 		return gtk_selection_data_get_selection(gtkSelectionData);
228 	}
229 
230 	/**
231 	 * Retrieves the target of the selection.
232 	 *
233 	 * Returns: the target of the selection.
234 	 *
235 	 * Since: 2.14
236 	 */
237 	public GdkAtom getTarget()
238 	{
239 		return gtk_selection_data_get_target(gtkSelectionData);
240 	}
241 
242 	/**
243 	 * Gets the contents of @selection_data as an array of targets.
244 	 * This can be used to interpret the results of getting
245 	 * the standard TARGETS target that is always supplied for
246 	 * any selection.
247 	 *
248 	 * Params:
249 	 *     targets = location to store an array of targets. The result stored
250 	 *         here must be freed with g_free().
251 	 *
252 	 * Returns: %TRUE if @selection_data contains a valid
253 	 *     array of targets, otherwise %FALSE.
254 	 */
255 	public bool getTargets(out GdkAtom[] targets)
256 	{
257 		GdkAtom* outtargets = null;
258 		int nAtoms;
259 
260 		auto p = gtk_selection_data_get_targets(gtkSelectionData, &outtargets, &nAtoms) != 0;
261 
262 		targets = outtargets[0 .. nAtoms];
263 
264 		return p;
265 	}
266 
267 	/**
268 	 * Gets the contents of the selection data as a UTF-8 string.
269 	 *
270 	 * Returns: if the selection data contained a
271 	 *     recognized text type and it could be converted to UTF-8, a newly
272 	 *     allocated string containing the converted text, otherwise %NULL.
273 	 *     If the result is non-%NULL it must be freed with g_free().
274 	 */
275 	public string getText()
276 	{
277 		auto retStr = gtk_selection_data_get_text(gtkSelectionData);
278 
279 		scope(exit) Str.freeString(retStr);
280 		return Str.toString(retStr);
281 	}
282 
283 	/**
284 	 * Gets the contents of the selection data as array of URIs.
285 	 *
286 	 * Returns: if
287 	 *     the selection data contains a list of
288 	 *     URIs, a newly allocated %NULL-terminated string array
289 	 *     containing the URIs, otherwise %NULL. If the result is
290 	 *     non-%NULL it must be freed with g_strfreev().
291 	 *
292 	 * Since: 2.6
293 	 */
294 	public string[] getUris()
295 	{
296 		auto retStr = gtk_selection_data_get_uris(gtkSelectionData);
297 
298 		scope(exit) Str.freeStringArray(retStr);
299 		return Str.toStringArray(retStr);
300 	}
301 
302 	/**
303 	 * Stores new data into a #GtkSelectionData object. Should
304 	 * only be called from a selection handler callback.
305 	 * Zero-terminates the stored data.
306 	 *
307 	 * Params:
308 	 *     type = the type of selection data
309 	 *     format = format (number of bits in a unit)
310 	 *     data = pointer to the data (will be copied)
311 	 */
312 	public void set(GdkAtom type, int format, char[] data)
313 	{
314 		gtk_selection_data_set(gtkSelectionData, type, format, data.ptr, cast(int)data.length);
315 	}
316 
317 	/**
318 	 * Sets the contents of the selection from a #GdkPixbuf
319 	 * The pixbuf is converted to the form determined by
320 	 * @selection_data->target.
321 	 *
322 	 * Params:
323 	 *     pixbuf = a #GdkPixbuf
324 	 *
325 	 * Returns: %TRUE if the selection was successfully set,
326 	 *     otherwise %FALSE.
327 	 *
328 	 * Since: 2.6
329 	 */
330 	public bool setPixbuf(Pixbuf pixbuf)
331 	{
332 		return gtk_selection_data_set_pixbuf(gtkSelectionData, (pixbuf is null) ? null : pixbuf.getPixbufStruct()) != 0;
333 	}
334 
335 	/**
336 	 * Sets the contents of the selection from a UTF-8 encoded string.
337 	 * The string is converted to the form determined by
338 	 * @selection_data->target.
339 	 *
340 	 * Params:
341 	 *     str = a UTF-8 string
342 	 *     len = the length of @str, or -1 if @str is nul-terminated.
343 	 *
344 	 * Returns: %TRUE if the selection was successfully set,
345 	 *     otherwise %FALSE.
346 	 */
347 	public bool setText(string str, int len)
348 	{
349 		return gtk_selection_data_set_text(gtkSelectionData, Str.toStringz(str), len) != 0;
350 	}
351 
352 	/**
353 	 * Sets the contents of the selection from a list of URIs.
354 	 * The string is converted to the form determined by
355 	 * @selection_data->target.
356 	 *
357 	 * Params:
358 	 *     uris = a %NULL-terminated array of
359 	 *         strings holding URIs
360 	 *
361 	 * Returns: %TRUE if the selection was successfully set,
362 	 *     otherwise %FALSE.
363 	 *
364 	 * Since: 2.6
365 	 */
366 	public bool setUris(string[] uris)
367 	{
368 		return gtk_selection_data_set_uris(gtkSelectionData, Str.toStringzArray(uris)) != 0;
369 	}
370 
371 	/**
372 	 * Given a #GtkSelectionData object holding a list of targets,
373 	 * determines if any of the targets in @targets can be used to
374 	 * provide a #GdkPixbuf.
375 	 *
376 	 * Params:
377 	 *     writable = whether to accept only targets for which GTK+ knows
378 	 *         how to convert a pixbuf into the format
379 	 *
380 	 * Returns: %TRUE if @selection_data holds a list of targets,
381 	 *     and a suitable target for images is included, otherwise %FALSE.
382 	 *
383 	 * Since: 2.6
384 	 */
385 	public bool targetsIncludeImage(bool writable)
386 	{
387 		return gtk_selection_data_targets_include_image(gtkSelectionData, writable) != 0;
388 	}
389 
390 	/**
391 	 * Given a #GtkSelectionData object holding a list of targets,
392 	 * determines if any of the targets in @targets can be used to
393 	 * provide rich text.
394 	 *
395 	 * Params:
396 	 *     buffer = a #GtkTextBuffer
397 	 *
398 	 * Returns: %TRUE if @selection_data holds a list of targets,
399 	 *     and a suitable target for rich text is included,
400 	 *     otherwise %FALSE.
401 	 *
402 	 * Since: 2.10
403 	 */
404 	public bool targetsIncludeRichText(TextBuffer buffer)
405 	{
406 		return gtk_selection_data_targets_include_rich_text(gtkSelectionData, (buffer is null) ? null : buffer.getTextBufferStruct()) != 0;
407 	}
408 
409 	/**
410 	 * Given a #GtkSelectionData object holding a list of targets,
411 	 * determines if any of the targets in @targets can be used to
412 	 * provide text.
413 	 *
414 	 * Returns: %TRUE if @selection_data holds a list of targets,
415 	 *     and a suitable target for text is included, otherwise %FALSE.
416 	 */
417 	public bool targetsIncludeText()
418 	{
419 		return gtk_selection_data_targets_include_text(gtkSelectionData) != 0;
420 	}
421 
422 	/**
423 	 * Given a #GtkSelectionData object holding a list of targets,
424 	 * determines if any of the targets in @targets can be used to
425 	 * provide a list or URIs.
426 	 *
427 	 * Returns: %TRUE if @selection_data holds a list of targets,
428 	 *     and a suitable target for URI lists is included, otherwise %FALSE.
429 	 *
430 	 * Since: 2.10
431 	 */
432 	public bool targetsIncludeUri()
433 	{
434 		return gtk_selection_data_targets_include_uri(gtkSelectionData) != 0;
435 	}
436 
437 	/**
438 	 * Appends a specified target to the list of supported targets for a
439 	 * given widget and selection.
440 	 *
441 	 * Params:
442 	 *     widget = a #GtkWidget
443 	 *     selection = the selection
444 	 *     target = target to add.
445 	 *     info = A unsigned integer which will be passed back to the application.
446 	 */
447 	public static void addTarget(Widget widget, GdkAtom selection, GdkAtom target, uint info)
448 	{
449 		gtk_selection_add_target((widget is null) ? null : widget.getWidgetStruct(), selection, target, info);
450 	}
451 
452 	/**
453 	 * Prepends a table of targets to the list of supported targets
454 	 * for a given widget and selection.
455 	 *
456 	 * Params:
457 	 *     widget = a #GtkWidget
458 	 *     selection = the selection
459 	 *     targets = a table of targets to add
460 	 */
461 	public static void addTargets(Widget widget, GdkAtom selection, TargetEntry[] targets)
462 	{
463 		GtkTargetEntry[] targetsArray = new GtkTargetEntry[targets.length];
464 		for ( int i = 0; i < targets.length; i++ )
465 		{
466 			targetsArray[i] = *(targets[i].getTargetEntryStruct());
467 		}
468 
469 		gtk_selection_add_targets((widget is null) ? null : widget.getWidgetStruct(), selection, targetsArray.ptr, cast(uint)targets.length);
470 	}
471 
472 	/**
473 	 * Remove all targets registered for the given selection for the
474 	 * widget.
475 	 *
476 	 * Params:
477 	 *     widget = a #GtkWidget
478 	 *     selection = an atom representing a selection
479 	 */
480 	public static void clearTargets(Widget widget, GdkAtom selection)
481 	{
482 		gtk_selection_clear_targets((widget is null) ? null : widget.getWidgetStruct(), selection);
483 	}
484 
485 	/**
486 	 * Requests the contents of a selection. When received,
487 	 * a “selection-received” signal will be generated.
488 	 *
489 	 * Params:
490 	 *     widget = The widget which acts as requestor
491 	 *     selection = Which selection to get
492 	 *     target = Form of information desired (e.g., STRING)
493 	 *     time = Time of request (usually of triggering event)
494 	 *         In emergency, you could use #GDK_CURRENT_TIME
495 	 *
496 	 * Returns: %TRUE if requested succeeded. %FALSE if we could not process
497 	 *     request. (e.g., there was already a request in process for
498 	 *     this widget).
499 	 */
500 	public static bool convert(Widget widget, GdkAtom selection, GdkAtom target, uint time)
501 	{
502 		return gtk_selection_convert((widget is null) ? null : widget.getWidgetStruct(), selection, target, time) != 0;
503 	}
504 
505 	/**
506 	 * Claims ownership of a given selection for a particular widget,
507 	 * or, if @widget is %NULL, release ownership of the selection.
508 	 *
509 	 * Params:
510 	 *     widget = a #GtkWidget, or %NULL.
511 	 *     selection = an interned atom representing the selection to claim
512 	 *     time = timestamp with which to claim the selection
513 	 *
514 	 * Returns: %TRUE if the operation succeeded
515 	 */
516 	public static bool ownerSet(Widget widget, GdkAtom selection, uint time)
517 	{
518 		return gtk_selection_owner_set((widget is null) ? null : widget.getWidgetStruct(), selection, time) != 0;
519 	}
520 
521 	/**
522 	 * Claim ownership of a given selection for a particular widget, or,
523 	 * if @widget is %NULL, release ownership of the selection.
524 	 *
525 	 * Params:
526 	 *     display = the #GdkDisplay where the selection is set
527 	 *     widget = new selection owner (a #GtkWidget), or %NULL.
528 	 *     selection = an interned atom representing the selection to claim.
529 	 *     time = timestamp with which to claim the selection
530 	 *
531 	 * Returns: TRUE if the operation succeeded
532 	 *
533 	 * Since: 2.2
534 	 */
535 	public static bool ownerSetForDisplay(Display display, Widget widget, GdkAtom selection, uint time)
536 	{
537 		return gtk_selection_owner_set_for_display((display is null) ? null : display.getDisplayStruct(), (widget is null) ? null : widget.getWidgetStruct(), selection, time) != 0;
538 	}
539 
540 	/**
541 	 * Removes all handlers and unsets ownership of all
542 	 * selections for a widget. Called when widget is being
543 	 * destroyed. This function will not generally be
544 	 * called by applications.
545 	 *
546 	 * Params:
547 	 *     widget = a #GtkWidget
548 	 */
549 	public static void removeAll(Widget widget)
550 	{
551 		gtk_selection_remove_all((widget is null) ? null : widget.getWidgetStruct());
552 	}
553 }