2008-05-10 Geoff Norton <gnorton@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.CarbonInternal / Pasteboard.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) 2007 Novell, Inc.
21 //
22 // Authors:
23 //      Geoff Norton (gnorton@novell.com)
24 //
25 //
26
27
28 using System;
29 using System.Runtime.InteropServices;
30 using System.Windows.Forms;
31
32 namespace System.Windows.Forms.CarbonInternal {
33         internal class Pasteboard {
34                 private static IntPtr primary_pbref;
35                 private static IntPtr app_pbref;
36
37                 private static IntPtr internal_format;
38
39                 static Pasteboard () {
40                         PasteboardCreate (XplatUICarbon.__CFStringMakeConstantString("com.apple.pasteboard.clipboard"), ref primary_pbref);
41                         PasteboardCreate (IntPtr.Zero, ref app_pbref);
42                         internal_format = XplatUICarbon.__CFStringMakeConstantString ("com.novell.mono.mwf.pasteboard");
43                 }
44
45                 internal static object Retrieve (IntPtr pbref, int key) {
46                         UInt32 count = 0;
47
48                         key = (int)internal_format;
49
50                         PasteboardGetItemCount (pbref, ref count);
51                         for (int i = 1; i <= count; i++) {
52                                 UInt32 itemid = 0;
53
54                                 PasteboardGetItemIdentifier (pbref, (UInt32)i, ref itemid);
55                                 //FIXME: We should get all the flavors and enumerate but we're cheating for now
56                                 if (itemid == 0xFACE) {
57                                         IntPtr pbdata = IntPtr.Zero;
58
59                                         PasteboardCopyItemFlavorData (pbref, (UInt32)0xFACE, (UInt32)key, ref pbdata);
60                                         if (pbdata != IntPtr.Zero) {
61                                                 GCHandle handle = (GCHandle) Marshal.ReadIntPtr (CFDataGetBytePtr (pbdata));
62                                                 
63                                                 return handle.Target;
64                                         }
65                                 }
66                         }
67                         return null;
68                 }
69
70                 internal static void Store (IntPtr pbref, object data, int key) {
71                         IntPtr gcdata = (IntPtr) GCHandle.Alloc (data);
72                         IntPtr pbdata = CFDataCreate (IntPtr.Zero, ref gcdata, Marshal.SizeOf (typeof (IntPtr)));
73
74                         key = (int)internal_format;
75
76                         PasteboardClear (pbref);
77                         PasteboardPutItemFlavor (pbref, (UInt32)0xFACE, (UInt32)key, pbdata, 0);
78                 }
79
80                 internal static IntPtr Primary {
81                         get { return primary_pbref; }
82                 }
83                 
84                 internal static IntPtr Application {
85                         get { return app_pbref; }
86                 }
87
88                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
89                 static extern IntPtr CFDataCreate (IntPtr allocator, ref IntPtr buf, Int32 length);
90                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
91                 static extern IntPtr CFDataGetBytePtr (IntPtr data);
92
93                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
94                 static extern int PasteboardClear (IntPtr pbref);
95                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
96                 static extern int PasteboardCreate (IntPtr str, ref IntPtr pbref);
97                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
98                 static extern int PasteboardCopyItemFlavorData (IntPtr pbref, UInt32 itemid, UInt32 key, ref IntPtr data);
99                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
100                 static extern int PasteboardGetItemCount (IntPtr pbref, ref UInt32 count);
101                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
102                 static extern int PasteboardGetItemIdentifier (IntPtr pbref, UInt32 itemindex, ref UInt32 itemid);
103                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
104                 static extern int PasteboardPutItemFlavor (IntPtr pbref, UInt32 itemid, UInt32 key, IntPtr data, UInt32 flags);
105         }
106 }