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 gdk.Clipboard;
26 
27 private import gdk.ContentFormats;
28 private import gdk.ContentProvider;
29 private import gdk.Display;
30 private import gdk.Texture;
31 private import gdk.c.functions;
32 public  import gdk.c.types;
33 private import gio.AsyncResultIF;
34 private import gio.Cancellable;
35 private import gio.InputStream;
36 private import glib.ErrorG;
37 private import glib.GException;
38 private import glib.Str;
39 private import glib.c.functions;
40 private import gobject.ObjectG;
41 private import gobject.Signals;
42 private import gobject.Value;
43 private import std.algorithm;
44 
45 
46 /**
47  * The `GdkClipboard` object represents data shared between applications or
48  * inside an application.
49  * 
50  * To get a `GdkClipboard` object, use [method@Gdk.Display.get_clipboard] or
51  * [method@Gdk.Display.get_primary_clipboard]. You can find out about the data
52  * that is currently available in a clipboard using
53  * [method@Gdk.Clipboard.get_formats].
54  * 
55  * To make text or image data available in a clipboard, use
56  * [method@Gdk.Clipboard.set_text] or [method@Gdk.Clipboard.set_texture].
57  * For other data, you can use [method@Gdk.Clipboard.set_content], which
58  * takes a [class@Gdk.ContentProvider] object.
59  * 
60  * To read textual or image data from a clipboard, use
61  * [method@Gdk.Clipboard.read_text_async] or
62  * [method@Gdk.Clipboard.read_texture_async]. For other data, use
63  * [method@Gdk.Clipboard.read_async], which provides a `GInputStream` object.
64  */
65 public class Clipboard : ObjectG
66 {
67 	/** the main Gtk struct */
68 	protected GdkClipboard* gdkClipboard;
69 
70 	/** Get the main Gtk struct */
71 	public GdkClipboard* getClipboardStruct(bool transferOwnership = false)
72 	{
73 		if (transferOwnership)
74 			ownedRef = false;
75 		return gdkClipboard;
76 	}
77 
78 	/** the main Gtk struct as a void* */
79 	protected override void* getStruct()
80 	{
81 		return cast(void*)gdkClipboard;
82 	}
83 
84 	/**
85 	 * Sets our main struct and passes it to the parent class.
86 	 */
87 	public this (GdkClipboard* gdkClipboard, bool ownedRef = false)
88 	{
89 		this.gdkClipboard = gdkClipboard;
90 		super(cast(GObject*)gdkClipboard, ownedRef);
91 	}
92 
93 
94 	/** */
95 	public static GType getType()
96 	{
97 		return gdk_clipboard_get_type();
98 	}
99 
100 	/**
101 	 * Returns the `GdkContentProvider` currently set on @clipboard.
102 	 *
103 	 * If the @clipboard is empty or its contents are not owned by the
104 	 * current process, %NULL will be returned.
105 	 *
106 	 * Returns: The content of a clipboard or %NULL
107 	 *     if the clipboard does not maintain any content.
108 	 */
109 	public ContentProvider getContent()
110 	{
111 		auto __p = gdk_clipboard_get_content(gdkClipboard);
112 
113 		if(__p is null)
114 		{
115 			return null;
116 		}
117 
118 		return ObjectG.getDObject!(ContentProvider)(cast(GdkContentProvider*) __p);
119 	}
120 
121 	/**
122 	 * Gets the `GdkDisplay` that the clipboard was created for.
123 	 *
124 	 * Returns: a `GdkDisplay`
125 	 */
126 	public Display getDisplay()
127 	{
128 		auto __p = gdk_clipboard_get_display(gdkClipboard);
129 
130 		if(__p is null)
131 		{
132 			return null;
133 		}
134 
135 		return ObjectG.getDObject!(Display)(cast(GdkDisplay*) __p);
136 	}
137 
138 	/**
139 	 * Gets the formats that the clipboard can provide its current contents in.
140 	 *
141 	 * Returns: The formats of the clipboard
142 	 */
143 	public ContentFormats getFormats()
144 	{
145 		auto __p = gdk_clipboard_get_formats(gdkClipboard);
146 
147 		if(__p is null)
148 		{
149 			return null;
150 		}
151 
152 		return ObjectG.getDObject!(ContentFormats)(cast(GdkContentFormats*) __p);
153 	}
154 
155 	/**
156 	 * Returns if the clipboard is local.
157 	 *
158 	 * A clipboard is considered local if it was last claimed
159 	 * by the running application.
160 	 *
161 	 * Note that [method@Gdk.Clipboard.get_content] may return %NULL
162 	 * even on a local clipboard. In this case the clipboard is empty.
163 	 *
164 	 * Returns: %TRUE if the clipboard is local
165 	 */
166 	public bool isLocal()
167 	{
168 		return gdk_clipboard_is_local(gdkClipboard) != 0;
169 	}
170 
171 	/**
172 	 * Asynchronously requests an input stream to read the @clipboard's
173 	 * contents from.
174 	 *
175 	 * When the operation is finished @callback will be called. You must then
176 	 * call [method@Gdk.Clipboard.read_finish] to get the result of the operation.
177 	 *
178 	 * The clipboard will choose the most suitable mime type from the given list
179 	 * to fulfill the request, preferring the ones listed first.
180 	 *
181 	 * Params:
182 	 *     mimeTypes = a %NULL-terminated array of mime types to choose from
183 	 *     ioPriority = the I/O priority of the request
184 	 *     cancellable = optional `GCancellable` object, %NULL to ignore.
185 	 *     callback = callback to call when the request is satisfied
186 	 *     userData = the data to pass to callback function
187 	 */
188 	public void readAsync(string[] mimeTypes, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
189 	{
190 		gdk_clipboard_read_async(gdkClipboard, Str.toStringzArray(mimeTypes), ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
191 	}
192 
193 	/**
194 	 * Finishes an asynchronous clipboard read.
195 	 *
196 	 * See [method@Gdk.Clipboard.read_async].
197 	 *
198 	 * Params:
199 	 *     result = a `GAsyncResult`
200 	 *     outMimeType = pointer to store
201 	 *         the chosen mime type in or %NULL
202 	 *
203 	 * Returns: a `GInputStream` or %NULL on error.
204 	 *
205 	 * Throws: GException on failure.
206 	 */
207 	public InputStream readFinish(AsyncResultIF result, out string outMimeType)
208 	{
209 		char* outoutMimeType = null;
210 		GError* err = null;
211 
212 		auto __p = gdk_clipboard_read_finish(gdkClipboard, (result is null) ? null : result.getAsyncResultStruct(), &outoutMimeType, &err);
213 
214 		if (err !is null)
215 		{
216 			throw new GException( new ErrorG(err) );
217 		}
218 
219 		outMimeType = Str.toString(outoutMimeType);
220 
221 		if(__p is null)
222 		{
223 			return null;
224 		}
225 
226 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) __p, true);
227 	}
228 
229 	/**
230 	 * Asynchronously request the @clipboard contents converted to a string.
231 	 *
232 	 * When the operation is finished @callback will be called. You must then
233 	 * call [method@Gdk.Clipboard.read_text_finish] to get the result.
234 	 *
235 	 * This is a simple wrapper around [method@Gdk.Clipboard.read_value_async].
236 	 * Use that function or [method@Gdk.Clipboard.read_async] directly if you
237 	 * need more control over the operation.
238 	 *
239 	 * Params:
240 	 *     cancellable = optional `GCancellable` object, %NULL to ignore
241 	 *     callback = callback to call when the request is satisfied
242 	 *     userData = the data to pass to callback function
243 	 */
244 	public void readTextAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
245 	{
246 		gdk_clipboard_read_text_async(gdkClipboard, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
247 	}
248 
249 	/**
250 	 * Finishes an asynchronous clipboard read.
251 	 *
252 	 * See [method@Gdk.Clipboard.read_text_async].
253 	 *
254 	 * Params:
255 	 *     result = a `GAsyncResult`
256 	 *
257 	 * Returns: a new string or %NULL on error
258 	 *
259 	 * Throws: GException on failure.
260 	 */
261 	public string readTextFinish(AsyncResultIF result)
262 	{
263 		GError* err = null;
264 
265 		auto retStr = gdk_clipboard_read_text_finish(gdkClipboard, (result is null) ? null : result.getAsyncResultStruct(), &err);
266 
267 		if (err !is null)
268 		{
269 			throw new GException( new ErrorG(err) );
270 		}
271 
272 		scope(exit) Str.freeString(retStr);
273 		return Str.toString(retStr);
274 	}
275 
276 	/**
277 	 * Asynchronously request the @clipboard contents converted to a `GdkPixbuf`.
278 	 *
279 	 * When the operation is finished @callback will be called. You must then
280 	 * call [method@Gdk.Clipboard.read_texture_finish] to get the result.
281 	 *
282 	 * This is a simple wrapper around [method@Gdk.Clipboard.read_value_async].
283 	 * Use that function or [methos@Gdk.Clipboard.read_async] directly if you
284 	 * need more control over the operation.
285 	 *
286 	 * Params:
287 	 *     cancellable = optional `GCancellable` object, %NULL to ignore.
288 	 *     callback = callback to call when the request is satisfied
289 	 *     userData = the data to pass to callback function
290 	 */
291 	public void readTextureAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
292 	{
293 		gdk_clipboard_read_texture_async(gdkClipboard, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
294 	}
295 
296 	/**
297 	 * Finishes an asynchronous clipboard read.
298 	 *
299 	 * See [method@Gdk.Clipboard.read_texture_async].
300 	 *
301 	 * Params:
302 	 *     result = a `GAsyncResult`
303 	 *
304 	 * Returns: a new `GdkTexture` or %NULL on error
305 	 *
306 	 * Throws: GException on failure.
307 	 */
308 	public Texture readTextureFinish(AsyncResultIF result)
309 	{
310 		GError* err = null;
311 
312 		auto __p = gdk_clipboard_read_texture_finish(gdkClipboard, (result is null) ? null : result.getAsyncResultStruct(), &err);
313 
314 		if (err !is null)
315 		{
316 			throw new GException( new ErrorG(err) );
317 		}
318 
319 		if(__p is null)
320 		{
321 			return null;
322 		}
323 
324 		return ObjectG.getDObject!(Texture)(cast(GdkTexture*) __p, true);
325 	}
326 
327 	/**
328 	 * Asynchronously request the @clipboard contents converted to the given
329 	 * @type.
330 	 *
331 	 * When the operation is finished @callback will be called. You must then call
332 	 * [method@Gdk.Clipboard.read_value_finish] to get the resulting `GValue`.
333 	 *
334 	 * For local clipboard contents that are available in the given `GType`,
335 	 * the value will be copied directly. Otherwise, GDK will try to use
336 	 * [func@content_deserialize_async] to convert the clipboard's data.
337 	 *
338 	 * Params:
339 	 *     type = a `GType` to read
340 	 *     ioPriority = the I/O priority of the request
341 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
342 	 *     callback = callback to call when the request is satisfied
343 	 *     userData = the data to pass to callback function
344 	 */
345 	public void readValueAsync(GType type, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
346 	{
347 		gdk_clipboard_read_value_async(gdkClipboard, type, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
348 	}
349 
350 	/**
351 	 * Finishes an asynchronous clipboard read.
352 	 *
353 	 * See [method@Gdk.Clipboard.read_value_async].
354 	 *
355 	 * Params:
356 	 *     result = a `GAsyncResult`
357 	 *
358 	 * Returns: a `GValue` containing the result.
359 	 *
360 	 * Throws: GException on failure.
361 	 */
362 	public Value readValueFinish(AsyncResultIF result)
363 	{
364 		GError* err = null;
365 
366 		auto __p = gdk_clipboard_read_value_finish(gdkClipboard, (result is null) ? null : result.getAsyncResultStruct(), &err);
367 
368 		if (err !is null)
369 		{
370 			throw new GException( new ErrorG(err) );
371 		}
372 
373 		if(__p is null)
374 		{
375 			return null;
376 		}
377 
378 		return ObjectG.getDObject!(Value)(cast(GValue*) __p);
379 	}
380 
381 	/**
382 	 * Sets a new content provider on @clipboard.
383 	 *
384 	 * The clipboard will claim the `GdkDisplay`'s resources and advertise
385 	 * these new contents to other applications.
386 	 *
387 	 * In the rare case of a failure, this function will return %FALSE. The
388 	 * clipboard will then continue reporting its old contents and ignore
389 	 * @provider.
390 	 *
391 	 * If the contents are read by either an external application or the
392 	 * @clipboard's read functions, @clipboard will select the best format to
393 	 * transfer the contents and then request that format from @provider.
394 	 *
395 	 * Params:
396 	 *     provider = the new contents of @clipboard or
397 	 *         %NULL to clear the clipboard
398 	 *
399 	 * Returns: %TRUE if setting the clipboard succeeded
400 	 */
401 	public bool setContent(ContentProvider provider)
402 	{
403 		return gdk_clipboard_set_content(gdkClipboard, (provider is null) ? null : provider.getContentProviderStruct()) != 0;
404 	}
405 
406 	/**
407 	 * Puts the given @text into the clipboard.
408 	 *
409 	 * Params:
410 	 *     text = Text to put into the clipboard
411 	 */
412 	public void setText(string text)
413 	{
414 		gdk_clipboard_set_text(gdkClipboard, Str.toStringz(text));
415 	}
416 
417 	/**
418 	 * Puts the given @texture into the clipboard.
419 	 *
420 	 * Params:
421 	 *     texture = a `GdkTexture` to put into the clipboard
422 	 */
423 	public void setTexture(Texture texture)
424 	{
425 		gdk_clipboard_set_texture(gdkClipboard, (texture is null) ? null : texture.getTextureStruct());
426 	}
427 
428 	/**
429 	 * Sets the clipboard to contain the value collected from the given @args.
430 	 *
431 	 * Params:
432 	 *     type = type of value to set
433 	 *     args = varargs containing the value of @type
434 	 */
435 	public void setValist(GType type, void* args)
436 	{
437 		gdk_clipboard_set_valist(gdkClipboard, type, args);
438 	}
439 
440 	/**
441 	 * Sets the @clipboard to contain the given @value.
442 	 *
443 	 * Params:
444 	 *     value = a `GValue` to set
445 	 */
446 	public void setValue(Value value)
447 	{
448 		gdk_clipboard_set_value(gdkClipboard, (value is null) ? null : value.getValueStruct());
449 	}
450 
451 	/**
452 	 * Asynchronously instructs the @clipboard to store its contents remotely.
453 	 *
454 	 * If the clipboard is not local, this function does nothing but report success.
455 	 *
456 	 * The @callback must call [method@Gdk.Clipboard.store_finish].
457 	 *
458 	 * The purpose of this call is to preserve clipboard contents beyond the
459 	 * lifetime of an application, so this function is typically called on
460 	 * exit. Depending on the platform, the functionality may not be available
461 	 * unless a "clipboard manager" is running.
462 	 *
463 	 * This function is called automatically when a [class@Gtk.Application] is
464 	 * shut down, so you likely don't need to call it.
465 	 *
466 	 * Params:
467 	 *     ioPriority = the I/O priority of the request.
468 	 *     cancellable = optional `GCancellable` object, %NULL to ignore.
469 	 *     callback = callback to call when the request is satisfied
470 	 *     userData = the data to pass to callback function
471 	 */
472 	public void storeAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
473 	{
474 		gdk_clipboard_store_async(gdkClipboard, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
475 	}
476 
477 	/**
478 	 * Finishes an asynchronous clipboard store.
479 	 *
480 	 * See [method@Gdk.Clipboard.store_async].
481 	 *
482 	 * Params:
483 	 *     result = a `GAsyncResult`
484 	 *
485 	 * Returns: %TRUE if storing was successful.
486 	 *
487 	 * Throws: GException on failure.
488 	 */
489 	public bool storeFinish(AsyncResultIF result)
490 	{
491 		GError* err = null;
492 
493 		auto __p = gdk_clipboard_store_finish(gdkClipboard, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
494 
495 		if (err !is null)
496 		{
497 			throw new GException( new ErrorG(err) );
498 		}
499 
500 		return __p;
501 	}
502 
503 	/**
504 	 * Emitted when the clipboard changes ownership.
505 	 */
506 	gulong addOnChanged(void delegate(Clipboard) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
507 	{
508 		return Signals.connect(this, "changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
509 	}
510 }