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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/TransferDataSource.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
// TransferDataSource.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
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Linq;
 
30
using System.IO;
 
31
using System.Runtime.Serialization.Formatters.Binary;
 
32
using Xwt.Drawing;
 
33
using Xwt.Backends;
 
34
using Xwt.Engine;
 
35
 
 
36
namespace Xwt
 
37
{
 
38
        /// <summary>
 
39
        /// A collection of data to be transferred through drag and drop or the clipboard
 
40
        /// </summary>
 
41
        public sealed class TransferDataSource
 
42
        {
 
43
                Dictionary<TransferDataType,object> data = new Dictionary<TransferDataType,object> ();
 
44
 
 
45
                /// <summary>
 
46
                /// Gets or sets the data request callback.
 
47
                /// </summary>
 
48
                /// <value>
 
49
                /// The data request callback.
 
50
                /// </value>
 
51
                /// <remarks>
 
52
                /// This callback can be used in combination with the AddType method to
 
53
                /// generate the data on demand. In some scenarios, the drop/paste
 
54
                /// side of a drag&drop or clipboard operation can decide if a drop/paste
 
55
                /// is allowed or not by checking the available data type in this
 
56
                /// data source. Once the operation is accepted, the DataRequestCallback
 
57
                /// callback will be invoked to get the data for the type.
 
58
                /// </remarks>
 
59
                public DataRequestDelegate DataRequestCallback { get; set; }
 
60
                
 
61
                /// <summary>
 
62
                /// Adds a value to the data source
 
63
                /// </summary>
 
64
                /// <param name='value'>
 
65
                /// Value.
 
66
                /// </param>
 
67
                public void AddValue<T> (T value) where T : class
 
68
                {
 
69
                        if (value == null)
 
70
                                throw new ArgumentNullException ("value");
 
71
                        data [TransferDataType.FromType (typeof (T))] = value;
 
72
                }
 
73
                
 
74
                /// <summary>
 
75
                /// Registers that the data store contains data of the provided type
 
76
                /// </summary>
 
77
                /// <param name='type'>
 
78
                /// The transfer data type
 
79
                /// </param>
 
80
                /// <remarks>
 
81
                /// This method can be used in combination with DataRequestCallback to
 
82
                /// generate the data on demand. In some scenarios, the drop/paste
 
83
                /// side of a drag&drop or clipboard operation can decide if a drop/paste
 
84
                /// is allowed or not by checking the available data type in this
 
85
                /// data source. Once the operation is accepted, the DataRequestCallback
 
86
                /// callback will be invoked to get the data for the type.
 
87
                /// </remarks>
 
88
                public void AddType (TransferDataType type)
 
89
                {
 
90
                        data [type] = null;
 
91
                }
 
92
                
 
93
                /// <summary>
 
94
                /// Registers that the data store contains data of the provided type
 
95
                /// </summary>
 
96
                /// <param name='type'>
 
97
                /// A type
 
98
                /// </param>
 
99
                /// <remarks>
 
100
                /// This method can be used in combination with DataRequestCallback to
 
101
                /// generate the data on demand. In some scenarios, the drop/paste
 
102
                /// side of a drag&drop or clipboard operation can decide if a drop/paste
 
103
                /// is allowed or not by checking the available data type in this
 
104
                /// data source. Once the operation is accepted, the DataRequestCallback
 
105
                /// callback will be invoked to get the data for the type.
 
106
                /// </remarks>
 
107
                public void AddType (Type type)
 
108
                {
 
109
                        data [TransferDataType.FromType (type)] = null;
 
110
                }
 
111
                
 
112
                /// <summary>
 
113
                /// Gets the types included in this data source
 
114
                /// </summary>
 
115
                public TransferDataType[] DataTypes {
 
116
                        get {
 
117
                                return data.Keys.ToArray ();
 
118
                        }
 
119
                }
 
120
 
 
121
                /// <summary>
 
122
                /// Gets the value for a specific type
 
123
                /// </summary>
 
124
                /// <returns>
 
125
                /// The value, or null if there is not value for this type
 
126
                /// </returns>
 
127
                /// <param name='type'>
 
128
                /// A type.
 
129
                /// </param>
 
130
                public object GetValue (TransferDataType type)
 
131
                {
 
132
                        object val;
 
133
                        if (data.TryGetValue (type, out val)) {
 
134
                                if (val != null)
 
135
                                        return val;
 
136
                                if (DataRequestCallback != null)
 
137
                                        return DataRequestCallback (type);
 
138
                        }
 
139
                        return null;
 
140
                }
 
141
                
 
142
                public static byte[] SerializeValue (object val)
 
143
                {
 
144
                        using (MemoryStream ms = new MemoryStream ()) {
 
145
                                BinaryFormatter bf = new BinaryFormatter ();
 
146
                                bf.Serialize (ms, val);
 
147
                                return ms.ToArray ();
 
148
                        }
 
149
                }
 
150
                
 
151
                public static object DeserializeValue (byte[] data)
 
152
                {
 
153
                        using (MemoryStream ms = new MemoryStream (data)) {
 
154
                                BinaryFormatter bf = new BinaryFormatter ();
 
155
                                return bf.Deserialize (ms);
 
156
                        }
 
157
                }
 
158
        }
 
159
        
 
160
        public delegate object DataRequestDelegate (TransferDataType type);
 
161
}