2
// TransferDataStore.cs
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
28
using System.Collections.Generic;
31
using System.Runtime.Serialization.Formatters.Binary;
36
namespace Xwt.Backends
39
/// A collection of data that has been transferred through drag & drop or the clipboard
41
public class TransferDataStore: ITransferData
43
Dictionary<TransferDataType,object> data = new Dictionary<TransferDataType,object> ();
45
public void AddText (string text)
47
data [TransferDataType.Text] = text;
50
public void AddImage (Xwt.Drawing.Image image)
52
data [TransferDataType.Image] = image;
55
public void AddUris (Uri[] uris)
57
data [TransferDataType.Uri] = uris;
60
public void AddValue (TransferDataType type, byte[] value)
62
Type t = Type.GetType (type.Id);
64
data [type] = TransferDataSource.DeserializeValue (value);
69
public void AddValue (TransferDataType type, object value)
74
object GetValue (TransferDataType type)
77
if (data.TryGetValue (type, out val)) {
84
object ITransferData.GetValue (TransferDataType type)
86
return GetValue (type);
89
T ITransferData.GetValue<T> ()
91
object ob = GetValue (TransferDataType.FromType (typeof(T)));
92
if (ob == null || ob.GetType () == typeof(Type))
95
T val = (T) TransferDataSource.DeserializeValue ((byte[])ob);
96
data[TransferDataType.FromType (typeof(T))] = val;
102
bool ITransferData.HasType (TransferDataType type)
104
return data.ContainsKey (type);
107
string ITransferData.Text {
109
return (string) GetValue (TransferDataType.Text);
113
Uri[] ITransferData.Uris {
115
var u = (Uri[]) GetValue (TransferDataType.Uri);
116
return u ?? new Uri [0];
120
Xwt.Drawing.Image ITransferData.Image {
122
return (Xwt.Drawing.Image) GetValue (TransferDataType.Image);