Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
8.7 KiB

  1. package Win32::Clipboard;
  2. #######################################################################
  3. #
  4. # Win32::Clipboard - Interaction with the Windows clipboard
  5. #
  6. # Version: 0.51
  7. # Author: Aldo Calpini <[email protected]>
  8. #
  9. #######################################################################
  10. require Exporter; # to export the constants to the main:: space
  11. require DynaLoader; # to dynuhlode the module.
  12. @ISA = qw( Exporter DynaLoader );
  13. @EXPORT = qw(
  14. CF_TEXT
  15. CF_BITMAP
  16. CF_METAFILEPICT
  17. CF_SYLK
  18. CF_DIF
  19. CF_TIFF
  20. CF_OEMTEXT
  21. CF_DIB
  22. CF_PALETTE
  23. CF_PENDATA
  24. CF_RIFF
  25. CF_WAVE
  26. CF_UNICODETEXT
  27. CF_ENHMETAFILE
  28. CF_HDROP
  29. CF_LOCALE
  30. );
  31. #######################################################################
  32. # This AUTOLOAD is used to 'autoload' constants from the constant()
  33. # XS function. If a constant is not found then control is passed
  34. # to the AUTOLOAD in AutoLoader.
  35. #
  36. sub AUTOLOAD {
  37. my($constname);
  38. ($constname = $AUTOLOAD) =~ s/.*:://;
  39. #reset $! to zero to reset any current errors.
  40. $!=0;
  41. my $val = constant($constname, @_ ? $_[0] : 0);
  42. if ($! != 0) {
  43. if ($! =~ /Invalid/) {
  44. $AutoLoader::AUTOLOAD = $AUTOLOAD;
  45. goto &AutoLoader::AUTOLOAD;
  46. } else {
  47. my ($pack, $file, $line) = caller;
  48. die "Win32::Clipboard::$constname is not defined, used at $file line $line.";
  49. }
  50. }
  51. eval "sub $AUTOLOAD { $val }";
  52. goto &$AUTOLOAD;
  53. }
  54. #######################################################################
  55. # STATIC OBJECT PROPERTIES
  56. #
  57. $VERSION = "0.51";
  58. #######################################################################
  59. # FUNCTIONS
  60. #
  61. sub new {
  62. my($class, $value) = @_;
  63. my $self = "I'm the Clipboard!";
  64. Set($value) if defined($value);
  65. return bless(\$self);
  66. }
  67. sub Version {
  68. return $VERSION;
  69. }
  70. sub Get {
  71. if( IsBitmap() ) { return GetBitmap(); }
  72. elsif( IsFiles() ) { return GetFiles(); }
  73. else { return GetText(); }
  74. }
  75. sub TIESCALAR {
  76. my $class = shift;
  77. my $value = shift;
  78. Set($value) if defined $value;
  79. my $self = "I'm the Clipboard!";
  80. return bless \$self, $class;
  81. }
  82. sub FETCH { Get() }
  83. sub STORE { shift; Set(@_) }
  84. sub DESTROY {
  85. my($self) = @_;
  86. undef $self;
  87. StopClipboardViewer();
  88. }
  89. END {
  90. StopClipboardViewer();
  91. }
  92. #######################################################################
  93. # dynamically load in the Clipboard.pll module.
  94. #
  95. bootstrap Win32::Clipboard;
  96. #######################################################################
  97. # a little hack to use the module itself as a class.
  98. #
  99. sub main::Win32::Clipboard {
  100. my($value) = @_;
  101. my $self={};
  102. my $result = Win32::Clipboard::Set($value) if defined($value);
  103. return bless($self, "Win32::Clipboard");
  104. }
  105. 1;
  106. __END__
  107. =head1 NAME
  108. Win32::Clipboard - Interaction with the Windows clipboard
  109. =head1 SYNOPSIS
  110. use Win32::Clipboard;
  111. $CLIP = Win32::Clipboard();
  112. print "Clipboard contains: ", $CLIP->Get(), "\n";
  113. $CLIP->Set("some text to copy into the clipboard");
  114. $CLIP->Empty();
  115. $CLIP->WaitForChange();
  116. print "Clipboard has changed!\n";
  117. =head1 DESCRIPTION
  118. This module lets you interact with the Windows clipboard: you can get its content,
  119. set it, empty it, or let your script sleep until it changes.
  120. This version supports 3 formats for clipboard data:
  121. =over 4
  122. =item *
  123. text (C<CF_TEXT>)
  124. The clipboard contains some text; this is the B<only> format you can use to set
  125. clipboard data; you get it as a single string.
  126. Example:
  127. $text = Win32::Clipboard::GetText();
  128. print $text;
  129. =item *
  130. bitmap (C<CF_DIB>)
  131. The clipboard contains an image, either a bitmap or a picture copied in the
  132. clipboard from a graphic application. The data you get is a binary buffer
  133. ready to be written to a bitmap (BMP format) file.
  134. Example:
  135. $image = Win32::Clipboard::GetBitmap();
  136. open BITMAP, ">some.bmp";
  137. binmode BITMAP;
  138. print BITMAP $image;
  139. close BITMAP;
  140. =item *
  141. list of files (C<CF_HDROP>)
  142. The clipboard contains files copied or cutted from an Explorer-like
  143. application; you get a list of filenames.
  144. Example:
  145. @files = Win32::Clipboard::GetFiles();
  146. print join("\n", @files);
  147. =back
  148. =head2 REFERENCE
  149. All the functions can be used either with their full name (eg. B<Win32::Clipboard::Get>)
  150. or as methods of a C<Win32::Clipboard> object.
  151. For the syntax, refer to L</SYNOPSIS> above. Note also that you can create a clipboard
  152. object and set its content at the same time with:
  153. $CLIP = Win32::Clipboard("blah blah blah");
  154. or with the more common form:
  155. $CLIP = new Win32::Clipboard("blah blah blah");
  156. If you prefer, you can even tie the Clipboard to a variable like this:
  157. tie $CLIP, 'Win32::Clipboard';
  158. print "Clipboard content: $CLIP\n";
  159. $CLIP = "some text to copy to the clipboard...";
  160. In this case, you can still access other methods using the tied() function:
  161. tied($CLIP)->Empty;
  162. print "got the picture" if tied($CLIP)->IsBitmap;
  163. =over 4
  164. =item Empty()
  165. Empty the clipboard.
  166. =for html <P>
  167. =item EnumFormats()
  168. Returns an array of identifiers describing the format for the data currently in the
  169. clipboard. Formats can be standard ones (described in the L</CONSTANTS> section) or
  170. application-defined custom ones. See also IsFormatAvailable().
  171. =for html <P>
  172. =item Get()
  173. Returns the clipboard content; note that the result depends on the nature of
  174. clipboard data; to ensure that you get only the desired format, you should use
  175. GetText(), GetBitmap() or GetFiles() instead. Get() is in fact implemented as:
  176. if( IsBitmap() ) { return GetBitmap(); }
  177. elsif( IsFiles() ) { return GetFiles(); }
  178. else { return GetText(); }
  179. See also IsBitmap(), IsFiles(), IsText(), EnumFormats() and IsFormatAvailable()
  180. to check the clipboard format before getting data.
  181. =for html <P>
  182. =item GetAs(FORMAT)
  183. Returns the clipboard content in the desired FORMAT (can be one of the constants
  184. defined in the L</CONSTANTS> section or a custom format). Note that the only
  185. meaningful identifiers are C<CF_TEXT>, C<CF_DIB> and C<CF_HDROP>; any other
  186. format is treated as a string.
  187. =for html <P>
  188. =item GetBitmap()
  189. Returns the clipboard content as an image, or C<undef> on errors.
  190. =for html <P>
  191. =item GetFiles()
  192. Returns the clipboard content as a list of filenames, or C<undef> on errors.
  193. =for html <P>
  194. =item GetFormatName(FORMAT)
  195. Returns the name of the specified custom clipboard format, or C<undef> on errors;
  196. note that you cannot get the name of the standard formats (described in the
  197. L</CONSTANTS> section).
  198. =for html <P>
  199. =item GetText()
  200. Returns the clipboard content as a string, or C<undef> on errors.
  201. =for html <P>
  202. =item IsBitmap()
  203. Returns a boolean value indicating if the clipboard contains an image.
  204. See also GetBitmap().
  205. =for html <P>
  206. =item IsFiles()
  207. Returns a boolean value indicating if the clipboard contains a list of
  208. files. See also GetFiles().
  209. =for html <P>
  210. =item IsFormatAvailable(FORMAT)
  211. Checks if the clipboard data matches the specified FORMAT (one of the constants
  212. described in the L</CONSTANTS> section); returns zero if the data does not match,
  213. a nonzero value if it matches.
  214. =for html <P>
  215. =item IsText()
  216. Returns a boolean value indicating if the clipboard contains text.
  217. See also GetText().
  218. =for html <P>
  219. =item Set(VALUE)
  220. Set the clipboard content to the specified string.
  221. =for html <P>
  222. =item WaitForChange([TIMEOUT])
  223. This function halts the script until the clipboard content changes. If you specify
  224. a C<TIMEOUT> value (in milliseconds), the function will return when this timeout
  225. expires, even if the clipboard hasn't changed. If no value is given, it will wait
  226. indefinitely. Returns 1 if the clipboard has changed, C<undef> on errors.
  227. =back
  228. =head2 CONSTANTS
  229. These constants are the standard clipboard formats recognized by Win32::Clipboard:
  230. CF_TEXT 1
  231. CF_DIB 8
  232. CF_HDROP 15
  233. The following formats are B<not recognized> by Win32::Clipboard; they are,
  234. however, exported constants and can eventually be used with the EnumFormats(),
  235. IsFormatAvailable() and GetAs() functions:
  236. CF_BITMAP 2
  237. CF_METAFILEPICT 3
  238. CF_SYLK 4
  239. CF_DIF 5
  240. CF_TIFF 6
  241. CF_OEMTEXT 7
  242. CF_PALETTE 9
  243. CF_PENDATA 10
  244. CF_RIFF 11
  245. CF_WAVE 12
  246. CF_UNICODETEXT 13
  247. CF_ENHMETAFILE 14
  248. CF_LOCALE 16
  249. =head1 AUTHOR
  250. Aldo Calpini <F<[email protected]>>
  251. Original XS porting by Gurusamy Sarathy <F<[email protected]>>.
  252. =cut