Merge pull request #2057 from directhex/monolite-on-jenkins
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / DataFormats.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.Collections;
31 using System.Text;
32
33 namespace System.Windows.Forms
34 {
35         public class DataFormats
36         {
37                 public class Format
38                 {
39                         static readonly object lockobj = new object ();
40                         
41                         private static Format   formats;
42                         private string          name;
43                         private int             id;
44                         private Format          next;
45                         internal bool           is_serializable;
46
47                         public Format (string name, int id)
48                         {
49                                 this.name = name;
50                                 this.id = id;
51                                 
52                                 lock (lockobj) {
53                                         if (formats == null)
54                                                 formats = this;
55                                         else {
56                                                 Format f = formats;
57                                                 while (f.next != null)
58                                                         f = f.next;
59                                                 f.next = this;
60                                         }
61                                 }
62                         }
63
64                         #region Public Instance Properties
65                         public int Id {
66                                 get {
67                                         return this.id;
68                                 }
69                         }
70
71                         public string Name {
72                                 get {
73                                         return this.name;
74                                 }
75                         }
76
77                         internal Format Next {
78                                 get {
79                                         return this.next;
80                                 }
81                         }
82                         #endregion      // Public Instance Properties
83
84                         #region Private Methods
85                         internal static Format Add (string name)
86                         {
87                                 Format f;
88
89                                 f = Find (name);
90                                 if (f == null) {
91                                         IntPtr cliphandle;
92
93                                         cliphandle = XplatUI.ClipboardOpen (false);
94                                         f = new Format (name, XplatUI.ClipboardGetID (cliphandle, name));
95                                         XplatUI.ClipboardClose (cliphandle);
96                                 }
97                                 return f;
98                         }
99
100                         internal static Format Add (int id) {
101                                 Format f;
102
103                                 f = Find (id);
104                                 if (f == null)
105                                         f = new Format("Format" + id.ToString(), id);
106                                 return f;
107                         }
108
109                         internal static Format Find (int id) {
110                                 Format f;
111
112                                 f = formats;
113                                 while ((f != null) && (f.Id != id))
114                                         f = f.next;
115                                 return f;
116                         }
117
118                         internal static Format Find (string name) {
119                                 Format f;
120
121                                 f = formats;
122                                 while ((f != null) && (!f.Name.Equals(name)))
123                                         f = f.next;
124                                 return f;
125                         }
126
127                         internal static Format List {
128                                 get {
129                                         return formats;
130                                 }
131                         }
132                         #endregion      // Private Methods
133                 }
134                 
135                 private DataFormats ()
136                 {
137                 }
138                 
139                 #region Public Static Fields
140                 public static readonly string Bitmap                    = "Bitmap";
141                 public static readonly string CommaSeparatedValue       = "Csv";
142                 public static readonly string Dib                       = "DeviceIndependentBitmap";
143                 public static readonly string Dif                       = "DataInterchangeFormat";
144                 public static readonly string EnhancedMetafile          = "EnhancedMetafile";
145                 public static readonly string FileDrop                  = "FileDrop";
146                 public static readonly string Html                      = "HTML Format";
147                 public static readonly string Locale                    = "Locale";
148                 public static readonly string MetafilePict              = "MetaFilePict";
149                 public static readonly string OemText                   = "OEMText";
150                 public static readonly string Palette                   = "Palette";
151                 public static readonly string PenData                   = "PenData";
152                 public static readonly string Riff                      = "RiffAudio";
153                 public static readonly string Rtf                       = "Rich Text Format";
154                 public static readonly string Serializable              = "WindowsForms10PersistentObject";
155                 public static readonly string StringFormat              = "System.String";
156                 public static readonly string SymbolicLink              = "SymbolicLink";
157                 public static readonly string Text                      = "Text";
158                 public static readonly string Tiff                      = "Tiff";
159                 public static readonly string UnicodeText               = "UnicodeText";
160                 public static readonly string WaveAudio                 = "WaveAudio";
161                 #endregion      // Public Static Fields
162
163                 private static object lock_object = new object ();
164                 private static bool initialized;
165
166                 // we don't want to force the creation of a new format
167                 internal static bool ContainsFormat (int id)
168                 {
169                         lock (lock_object) {
170                                 if (!initialized)
171                                         Init ();
172
173                                 return Format.Find (id) != null;
174                         }
175                 }
176
177                 public static Format GetFormat (int id)
178                 {
179                         lock (lock_object) {
180                                 if (!initialized)
181                                         Init ();
182                                 return Format.Find (id);
183                         }
184                 }
185
186                 public static Format GetFormat (string format)
187                 {
188                         lock (lock_object) {
189                                 if (!initialized)
190                                         Init ();
191                                 return Format.Add (format);
192                         }
193                 }
194
195                 // Assumes we are locked on the lock_object when it is called
196                 private static void Init ()
197                 {
198                         if (initialized)
199                                 return;
200                         IntPtr cliphandle = XplatUI.ClipboardOpen(false);
201
202                         new Format (Text, XplatUI.ClipboardGetID (cliphandle, Text));
203                         new Format (Bitmap, XplatUI.ClipboardGetID (cliphandle, Bitmap));
204                         new Format (MetafilePict, XplatUI.ClipboardGetID (cliphandle, MetafilePict));
205                         new Format (SymbolicLink, XplatUI.ClipboardGetID (cliphandle, SymbolicLink));
206                         new Format (Dif, XplatUI.ClipboardGetID (cliphandle, Dif)) ;
207                         new Format (Tiff, XplatUI.ClipboardGetID (cliphandle, Tiff));
208                         new Format (OemText, XplatUI.ClipboardGetID (cliphandle, OemText));
209                         new Format (Dib, XplatUI.ClipboardGetID (cliphandle, Dib));
210                         new Format (Palette, XplatUI.ClipboardGetID (cliphandle, Palette));
211                         new Format (PenData, XplatUI.ClipboardGetID (cliphandle, PenData));
212                         new Format (Riff, XplatUI.ClipboardGetID (cliphandle, Riff));
213                         new Format (WaveAudio, XplatUI.ClipboardGetID (cliphandle, WaveAudio));
214                         new Format (UnicodeText, XplatUI.ClipboardGetID (cliphandle, UnicodeText));
215                         new Format (EnhancedMetafile, XplatUI.ClipboardGetID (cliphandle, EnhancedMetafile));
216                         new Format (FileDrop, XplatUI.ClipboardGetID (cliphandle, FileDrop));
217                         new Format (Locale, XplatUI.ClipboardGetID (cliphandle, Locale));
218                         new Format (CommaSeparatedValue, XplatUI.ClipboardGetID (cliphandle, CommaSeparatedValue));
219                         new Format (Html, XplatUI.ClipboardGetID (cliphandle, Html));
220                         new Format (Rtf, XplatUI.ClipboardGetID (cliphandle, Rtf));
221                         new Format (Serializable, XplatUI.ClipboardGetID (cliphandle, Serializable));
222                         new Format (StringFormat, XplatUI.ClipboardGetID (cliphandle, StringFormat));
223
224                         XplatUI.ClipboardClose (cliphandle);
225
226                         initialized = true;
227                 }
228         }
229 }