5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
using System.Collections.Generic;
30
using System.Threading;
32
namespace Xwt.GtkBackend
34
public class ClipboardBackend: IClipboardBackend
36
// Gtk.Clipboard primaryClipboard;
37
Gtk.Clipboard clipboard;
39
public ClipboardBackend ()
41
clipboard = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
42
// primaryClipboard = Gtk.Clipboard.Get (Gdk.Atom.Intern ("PRIMARY", false));
45
#region IClipboardBackend implementation
51
public void SetData (TransferDataType type, Func<object> dataSource)
53
clipboard.SetWithData ((Gtk.TargetEntry[])Util.BuildTargetTable (new TransferDataType[] { type }),
54
delegate (Gtk.Clipboard cb, Gtk.SelectionData data, uint id) {
55
TransferDataType ttype = Util.AtomToType (data.Target.Name);
57
Util.SetSelectionData (data, data.Target.Name, dataSource ());
63
public bool IsTypeAvailable (TransferDataType type)
65
if (type == TransferDataType.Text)
66
return clipboard.WaitIsTextAvailable ();
67
if (type == TransferDataType.Image)
68
return clipboard.WaitIsImageAvailable ();
70
foreach (var at in GetAtomsForType (type)) {
71
if (clipboard.WaitIsTargetAvailable (at))
77
IEnumerable<Gdk.Atom> GetAtomsForType (TransferDataType type)
79
foreach (Gtk.TargetEntry te in (Gtk.TargetEntry[])Util.BuildTargetTable (new TransferDataType[] { type }))
80
yield return Gdk.Atom.Intern (te.Target, false);
83
public object GetData (TransferDataType type)
85
if (type == TransferDataType.Text)
86
return clipboard.WaitForText ();
87
if (type == TransferDataType.Text)
88
return clipboard.WaitForImage ();
90
TransferDataStore store = new TransferDataStore ();
92
foreach (var at in GetAtomsForType (type)) {
93
var data = clipboard.WaitForContents (at);
94
Util.GetSelectionData (data, store);
96
return ((ITransferData)store).GetValue (type);
99
public IAsyncResult BeginGetData (TransferDataType type, AsyncCallback callback, object state)
101
var atts = GetAtomsForType (type).ToArray ();
102
return new DataRequest (clipboard, callback, state, type, atts);
105
public object EndGetData (IAsyncResult ares)
107
return ((DataRequest)ares).Result;
113
class DataRequest: IAsyncResult
115
Gtk.Clipboard clipboard;
118
ManualResetEvent doneEvent;
120
TransferDataType type;
121
AsyncCallback callback;
123
public DataRequest (Gtk.Clipboard clipboard, AsyncCallback callback, object state, TransferDataType type, Gdk.Atom[] atoms)
125
this.callback = callback;
129
this.clipboard = clipboard;
133
public object Result { get; set; }
137
clipboard.RequestContents (atoms[index], DataReceived);
140
void DataReceived (Gtk.Clipboard cb, Gtk.SelectionData data)
142
TransferDataStore store = new TransferDataStore ();
143
if (Util.GetSelectionData (data, store)) {
144
Result = ((ITransferData)store).GetValue (type);
147
if (++index < atoms.Length)
158
if (doneEvent != null)
161
if (callback != null) {
162
Application.Invoke (delegate {
168
#region IAsyncResult implementation
169
public object AsyncState { get; set; }
171
public WaitHandle AsyncWaitHandle {
174
if (doneEvent == null)
175
doneEvent = new ManualResetEvent (complete);
181
public bool CompletedSynchronously {
187
public bool IsCompleted {