~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/ClipboardBackend.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ClipboardBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin Inc
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Linq;
 
28
using Xwt.Backends;
 
29
using System.Collections.Generic;
 
30
using System.Threading;
 
31
 
 
32
namespace Xwt.GtkBackend
 
33
{
 
34
        public class ClipboardBackend: IClipboardBackend
 
35
        {
 
36
//              Gtk.Clipboard primaryClipboard;
 
37
                Gtk.Clipboard clipboard;
 
38
                
 
39
                public ClipboardBackend ()
 
40
                {
 
41
                        clipboard = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
 
42
//                      primaryClipboard = Gtk.Clipboard.Get (Gdk.Atom.Intern ("PRIMARY", false));
 
43
                }
 
44
 
 
45
                #region IClipboardBackend implementation
 
46
                public void Clear ()
 
47
                {
 
48
                        clipboard.Clear ();
 
49
                }
 
50
 
 
51
                public void SetData (TransferDataType type, Func<object> dataSource)
 
52
                {
 
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);
 
56
                                if (ttype == type)
 
57
                                        Util.SetSelectionData (data, data.Target.Name, dataSource ());
 
58
                        },
 
59
                        delegate {
 
60
                        });
 
61
                }
 
62
 
 
63
                public bool IsTypeAvailable (TransferDataType type)
 
64
                {
 
65
                        if (type == TransferDataType.Text)
 
66
                                return clipboard.WaitIsTextAvailable ();
 
67
                        if (type == TransferDataType.Image)
 
68
                                return clipboard.WaitIsImageAvailable ();
 
69
                        
 
70
                        foreach (var at in GetAtomsForType (type)) {
 
71
                                if (clipboard.WaitIsTargetAvailable (at))
 
72
                                        return true;
 
73
                        }
 
74
                        return false;
 
75
                }
 
76
                
 
77
                IEnumerable<Gdk.Atom> GetAtomsForType (TransferDataType type)
 
78
                {
 
79
                        foreach (Gtk.TargetEntry te in (Gtk.TargetEntry[])Util.BuildTargetTable (new TransferDataType[] { type }))
 
80
                                yield return Gdk.Atom.Intern (te.Target, false);
 
81
                }
 
82
 
 
83
                public object GetData (TransferDataType type)
 
84
                {
 
85
                        if (type == TransferDataType.Text)
 
86
                                return clipboard.WaitForText ();
 
87
                        if (type == TransferDataType.Text)
 
88
                                return clipboard.WaitForImage ();
 
89
                        
 
90
                        TransferDataStore store = new TransferDataStore ();
 
91
                        
 
92
                        foreach (var at in GetAtomsForType (type)) {
 
93
                                var data = clipboard.WaitForContents (at);
 
94
                                Util.GetSelectionData (data, store);
 
95
                        }
 
96
                        return ((ITransferData)store).GetValue (type);
 
97
                }
 
98
 
 
99
                public IAsyncResult BeginGetData (TransferDataType type, AsyncCallback callback, object state)
 
100
                {
 
101
                        var atts = GetAtomsForType (type).ToArray ();
 
102
                        return new DataRequest (clipboard, callback, state, type, atts);
 
103
                }
 
104
 
 
105
                public object EndGetData (IAsyncResult ares)
 
106
                {
 
107
                        return ((DataRequest)ares).Result;
 
108
                }
 
109
                
 
110
                #endregion
 
111
        }
 
112
        
 
113
        class DataRequest: IAsyncResult
 
114
        {
 
115
                Gtk.Clipboard clipboard;
 
116
                Gdk.Atom[] atoms;
 
117
                int index = 0;
 
118
                ManualResetEvent doneEvent;
 
119
                bool complete;
 
120
                TransferDataType type;
 
121
                AsyncCallback callback;
 
122
                
 
123
                public DataRequest (Gtk.Clipboard clipboard, AsyncCallback callback, object state, TransferDataType type, Gdk.Atom[] atoms)
 
124
                {
 
125
                        this.callback = callback;
 
126
                        this.type = type;
 
127
                        AsyncState = state;
 
128
                        this.atoms = atoms;
 
129
                        this.clipboard = clipboard;
 
130
                        RequestData ();
 
131
                }
 
132
                
 
133
                public object Result { get; set; }
 
134
                
 
135
                void RequestData ()
 
136
                {
 
137
                        clipboard.RequestContents (atoms[index], DataReceived);
 
138
                }
 
139
                
 
140
                void DataReceived (Gtk.Clipboard cb, Gtk.SelectionData data)
 
141
                {
 
142
                        TransferDataStore store = new TransferDataStore ();
 
143
                        if (Util.GetSelectionData (data, store)) {
 
144
                                Result = ((ITransferData)store).GetValue (type);
 
145
                                SetComplete ();
 
146
                        } else {
 
147
                                if (++index < atoms.Length)
 
148
                                        RequestData ();
 
149
                                else
 
150
                                        SetComplete ();
 
151
                        }
 
152
                }
 
153
                
 
154
                void SetComplete ()
 
155
                {
 
156
                        lock (atoms) {
 
157
                                complete = true;
 
158
                                if (doneEvent != null)
 
159
                                        doneEvent.Set ();
 
160
                        }
 
161
                        if (callback != null) {
 
162
                                Application.Invoke (delegate {
 
163
                                        callback (this);
 
164
                                });
 
165
                        }
 
166
                }
 
167
                
 
168
                #region IAsyncResult implementation
 
169
                public object AsyncState { get; set; }
 
170
 
 
171
                public WaitHandle AsyncWaitHandle {
 
172
                        get {
 
173
                                lock (atoms) {
 
174
                                        if (doneEvent == null)
 
175
                                                doneEvent = new ManualResetEvent (complete);
 
176
                                }
 
177
                                return doneEvent;
 
178
                        }
 
179
                }
 
180
 
 
181
                public bool CompletedSynchronously {
 
182
                        get {
 
183
                                return false;
 
184
                        }
 
185
                }
 
186
 
 
187
                public bool IsCompleted {
 
188
                        get {
 
189
                                return complete;
 
190
                        }
 
191
                }
 
192
                #endregion
 
193
        }
 
194
}
 
195