* TreeView.cs: Don't draw the selected node when we lose
[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                 public static IDataObject GetDataObject() {
61                         DataObject              clipboard;
62                         IntPtr                  clipboard_handle;
63                         int[]                   native_formats;
64                         DataFormats.Format      item_format;
65                         object                  managed_clipboard_item;
66                         XplatUI.ClipboardToObject converter;
67
68                         converter = new XplatUI.ClipboardToObject(ConvertFromClipboardData);
69
70                         clipboard_handle = XplatUI.ClipboardOpen();
71                         native_formats = XplatUI.ClipboardAvailableFormats(clipboard_handle);
72                         if (native_formats == null) {
73                                 return null;    // Clipboard empty
74                         }
75
76                         // Build the IDataObject
77                         clipboard = new DataObject();
78                         for (int i = 0; i < native_formats.Length; i++) {
79                                 // We might get a format we don't understand or know
80                                 item_format = DataFormats.GetFormat(native_formats[i]);
81                                 if (item_format != null) {
82                                         managed_clipboard_item = XplatUI.ClipboardRetrieve(clipboard_handle, native_formats[i], converter);
83
84                                         if (managed_clipboard_item != null) {
85                                                 clipboard.SetData(item_format.Name, managed_clipboard_item);
86                                                 // We don't handle 'bitmap' since it involves handles, so we'll equate it to dib
87                                                 if (item_format.Name == DataFormats.Dib) {
88                                                         clipboard.SetData(DataFormats.Bitmap, managed_clipboard_item);
89                                                 }
90                                         }
91                                 }
92                         }
93
94                         XplatUI.ClipboardClose(clipboard_handle);
95
96                         return clipboard;
97                 }
98
99                 public static void SetDataObject(object data) {
100                         SetDataObject(data, true);
101                         
102                 }
103
104                 public static void SetDataObject(object data, bool copy) {
105                         IntPtr                  clipboard_handle;
106                         XplatUI.ObjectToClipboard converter;
107                         int                     native_format;
108                         DataFormats.Format      item_format;
109
110                         converter = new XplatUI.ObjectToClipboard(ConvertToClipboardData);
111
112                         clipboard_handle = XplatUI.ClipboardOpen();
113
114                         native_format = -1;
115
116                         item_format = DataFormats.Format.Find(data.GetType().FullName);
117                         if ((item_format != null) && (item_format.Name != DataFormats.StringFormat)) {
118                                 native_format = item_format.Id;
119                         }
120
121                         XplatUI.ClipboardStore(clipboard_handle, data, native_format, converter);
122                         XplatUI.ClipboardClose(clipboard_handle);
123                 }
124                 #endregion      // Public Static Methods
125         }
126 }