2007-01-02 Mike Kestner <mkestner@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Clipboard.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //
25 //
26
27 // COMPLETE
28
29 using System;
30 using System.Drawing;
31 using System.IO;
32 using System.Runtime.InteropServices;
33
34 namespace System.Windows.Forms {
35         public sealed class Clipboard {
36                 #region Local Variables
37                 #endregion      // Local Variables
38
39                 #region Constructors
40                 private Clipboard() {
41                 }
42                 #endregion      // Constructors
43
44                 #region Private Methods
45                 private static bool ConvertToClipboardData(ref int type, object obj, out byte[] data) {
46                         data = null;
47                         return false;
48                 }
49
50                 private static bool ConvertFromClipboardData(int type, IntPtr data, out object obj) {
51                         obj = null;
52                         if (data == IntPtr.Zero) {
53                                 return false;
54                         }
55                         return false;
56                 }
57                 #endregion      // Private Methods
58
59                 #region Public Static Methods
60
61                 public static IDataObject GetDataObject ()
62                 {
63                         return GetDataObject (false);
64                 }
65
66                 internal static IDataObject GetDataObject (bool primary_selection)
67                 {
68                         DataObject              clipboard;
69                         IntPtr                  clipboard_handle;
70                         int[]                   native_formats;
71                         DataFormats.Format      item_format;
72                         object                  managed_clipboard_item;
73                         XplatUI.ClipboardToObject converter;
74
75                         converter = new XplatUI.ClipboardToObject(ConvertFromClipboardData);
76
77                         clipboard_handle = XplatUI.ClipboardOpen (primary_selection);
78                         native_formats = XplatUI.ClipboardAvailableFormats(clipboard_handle);
79                         if (native_formats == null) {
80                                 return null;    // Clipboard empty
81                         }
82
83                         // Build the IDataObject
84                         clipboard = new DataObject();
85                         for (int i = 0; i < native_formats.Length; i++) {
86                                 // We might get a format we don't understand or know
87                                 item_format = DataFormats.GetFormat(native_formats[i]);
88
89                                 if (item_format != null) {
90                                         managed_clipboard_item = XplatUI.ClipboardRetrieve(clipboard_handle, native_formats[i], converter);
91
92                                         if (managed_clipboard_item != null) {
93                                                 clipboard.SetData(item_format.Name, managed_clipboard_item);
94                                                 // We don't handle 'bitmap' since it involves handles, so we'll equate it to dib
95                                                 if (item_format.Name == DataFormats.Dib) {
96                                                         clipboard.SetData(DataFormats.Bitmap, managed_clipboard_item);
97                                                 }
98                                         }
99                                 }
100                         }
101
102                         XplatUI.ClipboardClose(clipboard_handle);
103
104                         return clipboard;
105                 }
106
107                 public static void SetDataObject(object data) {
108                         SetDataObject(data, true);
109                         
110                 }
111
112                 public static void SetDataObject(object data, bool copy) {
113                         IntPtr                  clipboard_handle;
114                         XplatUI.ObjectToClipboard converter;
115                         int                     native_format;
116                         DataFormats.Format      item_format;
117
118                         converter = new XplatUI.ObjectToClipboard(ConvertToClipboardData);
119
120                         clipboard_handle = XplatUI.ClipboardOpen(false);
121                         XplatUI.ClipboardStore(clipboard_handle, null, 0, null);        // Empty clipboard
122
123                         native_format = -1;
124
125                         if (data is IDataObject) {
126                                 string[] formats;
127
128                                 formats = ((IDataObject)data).GetFormats();
129                                 for (int i = 0; i < formats.Length; i++) {
130                                         item_format = DataFormats.GetFormat(formats[i]);
131                                         if ((item_format != null) && (item_format.Name != DataFormats.StringFormat)) {
132                                                 native_format = item_format.Id;
133                                         }
134
135                                         XplatUI.ClipboardStore(clipboard_handle, ((IDataObject)data).GetData(formats[i]), native_format, converter);
136                                 }
137                         } else {
138                                 item_format = DataFormats.Format.Find(data.GetType().FullName);
139                                 if ((item_format != null) && (item_format.Name != DataFormats.StringFormat)) {
140                                         native_format = item_format.Id;
141                                 }
142
143                                 XplatUI.ClipboardStore(clipboard_handle, data, native_format, converter);
144                         }
145                         XplatUI.ClipboardClose(clipboard_handle);
146                 }
147                 #endregion      // Public Static Methods
148         }
149 }