Pasteboard.cs 4.2 KB

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