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