home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _65bdf4058652a329314ae9000adde471 < prev    next >
Text File  |  2004-06-01  |  9KB  |  364 lines

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