win32-clipboard 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,39 @@
1
+ == 0.4.0 - 24-Feb-2006
2
+ * Removed the C version entirely and replaced it with a pure Ruby version
3
+ that uses the Win32API package.
4
+ * Added a gemspec.
5
+ * Added a install.rb file.
6
+
7
+ == 0.3.1 - 2-Feb-2006
8
+ * Unicode fix to handle characters with null bytes. Thanks go to Brian
9
+ Marick for the spot.
10
+
11
+ == 0.3.0 - 26-May-2005
12
+ * Changed the Clipboard.data= method to Clipboard.set_data because it does,
13
+ in fact, take up to two arguments. This API change is the main reason for
14
+ the VERSION bump.
15
+ * Added Clipboard.get_data method as an alias for Clipboard.data.
16
+ * Removed the clipboard.rd file. The Clipboard.txt file is now rdoc friendly.
17
+ * Now Unicode friendly.
18
+ * Added more tests, including Unicode test.
19
+ * General code cleanup.
20
+
21
+ == 0.2.1 - 28-Feb-2005
22
+ * Moved the 'examples' directory to the toplevel directory.
23
+ * Renamed the sample script to 'clipboard_test.rb'.
24
+ * Made this document and README rdoc friendly.
25
+
26
+ == 0.2.0 - 12-Jul-2004
27
+ * Made Clipboard a class instead of a module.
28
+ * Moved the ClipboardError class under the Win32 module.
29
+ * Added the formats() class method.
30
+ * Added the register_format class method.
31
+ * Added the format_available? class method.
32
+ * Added the format_name class method.
33
+ * Replaced all instances (1) of the deprecated STR2CSTR() function with the
34
+ StringValuePtr() function. This means that as of version 0.2.0 this package
35
+ requires Ruby 1.8.0 or later.
36
+ * Moved the sample script to doc/examples.
37
+
38
+ == 0.1.0 - 19-Nov-2003
39
+ * Initial release
@@ -0,0 +1,11 @@
1
+ MANIFEST
2
+ CHANGES
3
+ README
4
+
5
+ doc/clipboard.txt
6
+
7
+ examples/clipboard_test.rb
8
+
9
+ lib/win32/clipboard.rb
10
+
11
+ test/tc_clipboard.rb
data/README ADDED
@@ -0,0 +1,106 @@
1
+ == Description
2
+ win32-clipboard - a Ruby package for interacting with the Win32 clipboard.
3
+
4
+ == Synopsis
5
+ require "win32/clipboard"
6
+ include Win32
7
+
8
+ puts "Data on clipboard: " + Clipboard.data
9
+ puts "Number of available formats: " + Clipboard.num_formats
10
+
11
+ puts "Setting data to 'foobar'"
12
+ Clipboard.data = "foobar"
13
+
14
+ puts "Clipboard now contains: " + Clipboard.data
15
+
16
+ puts "Clearing clipboard"
17
+ Clipboard.empty
18
+
19
+ == Class Methods
20
+ Clipboard.data(format=nil)
21
+ Clipboard.get_data(format=nil)
22
+ Returns the data currently in the clipboard. If 'format' is
23
+ specified, it will attempt to retrieve the data in that format. The
24
+ default is Clipboard::TEXT. See the 'Constants' section for
25
+ the supported formats.
26
+
27
+ Note that this method does implicit conversions on formats with regards
28
+ to text data. See the MSDN documentation for more details.
29
+
30
+ Clipboard.set_data(data, format=nil)
31
+ Sets the clipboard contents to the data that you specify. You may
32
+ optionally specify a clipboard format. The default is Clipboard::TEXT.
33
+
34
+ See the 'Constants' section for a list of the supported formats.
35
+
36
+ Clipboard.empty
37
+ Empty the contents of the clipboard
38
+
39
+ Clipboard.format_available?(format_number)
40
+ Returns true if 'format_number' is currently available on the clipboard,
41
+ false otherwise.
42
+
43
+ Clipboard.format_name(format_number)
44
+ Returns the corresponding name for the given 'format_number', or nil
45
+ if it does not exist.
46
+
47
+ Clipboard.formats
48
+ Returns a hash of all the current formats, with the format number as the
49
+ key and the format name as the value for that key.
50
+
51
+ Clipboard.num_formats
52
+ Returns the number of different data formats currently on the clipboard.
53
+
54
+ Clipboard.register_format(format)
55
+ Registers the given 'format' (a String) as a clipboard format, which
56
+ can then be used as a valid clipboard format.
57
+
58
+ If a registered format with the specified name already exists, a new
59
+ format is not registered and the return value identifies the existing
60
+ format. This enables more than one application to copy and paste data
61
+ using the same registered clipboard format. Note that the format name
62
+ comparison is case-insensitive.
63
+
64
+ Registered clipboard formats are identified by values in the range 0xC000
65
+ through 0xFFFF.
66
+
67
+ == Error Classes
68
+ ClipboardError
69
+ Typically only raised if the clipboard fails to open or if you attempt to
70
+ register an invalid format.
71
+
72
+ == Constants
73
+ === Standard Constants
74
+ VERSION
75
+ Returns the current version number of this package, as a String.
76
+
77
+ === Clipboard Formats
78
+ Clipboard::OEMTEXT
79
+ Text format containing characters in the OEM character set. Each line ends
80
+ with a carriage return/linefeed (CR-LF) combination. A null character
81
+ signals the end of the data.
82
+
83
+ Clipboard::TEXT
84
+ Text format. Each line ends with a carriage return/linefeed (CR-LF)
85
+ combination. A null character signals the end of the data. Use this format
86
+ for ANSI text.
87
+
88
+ Clipboard::UNICODETEXT
89
+ Unicode text format. Each line ends with a carriage return/linefeed
90
+ (CR-LF) combination. A null character signals the end of the data. This
91
+ format is only supported on NT/2000/XP.
92
+
93
+ == Future Plans
94
+ Add more formatting option contants and related methods.
95
+
96
+ == License
97
+ Ruby's
98
+
99
+ == Copyright
100
+ (C) 2003-2006 Daniel J. Berger
101
+ All Rights Reserved
102
+
103
+ == Author
104
+ Daniel J. Berger
105
+ djberg96 at gmail dot com
106
+ imperator/mok on IRC
@@ -0,0 +1,39 @@
1
+ ##########################################################################
2
+ # clipboard_test.rb (win32-clipboard)
3
+ #
4
+ # Generic test script for those without TestUnit installed, or for
5
+ # general futzing.
6
+ ##########################################################################
7
+ base = File.basename(Dir.pwd)
8
+ if base == "examples" || base =~ /win32-clipboard/
9
+ require "ftools"
10
+ Dir.chdir("..") if base == "examples"
11
+ Dir.mkdir("win32") unless File.exists?("win32")
12
+ File.copy("clipboard.so","win32")
13
+ $LOAD_PATH.unshift Dir.pwd
14
+ Dir.chdir("examples") if base =~ /win32-clipboard/
15
+ end
16
+
17
+ require "win32/clipboard"
18
+ require "pp"
19
+ include Win32
20
+
21
+ puts "VERSION: " + Clipboard::VERSION
22
+
23
+ pp Clipboard.formats
24
+ pp Clipboard.data(Clipboard::DIB)
25
+ pp Clipboard.format_available?(49161)
26
+ pp Clipboard.format_name(999999999)
27
+ pp Clipboard.format_available?(9999999)
28
+
29
+ puts "Data was: [" + Clipboard.data + "]"
30
+
31
+ Clipboard.set_data("foobar")
32
+
33
+ puts "Data is now: [" + Clipboard.data + "]"
34
+
35
+ puts "Number of available formats: " + Clipboard.num_formats.to_s
36
+
37
+ Clipboard.empty
38
+
39
+ puts "Clipboard emptied"
@@ -0,0 +1,182 @@
1
+ require "Win32API"
2
+
3
+ module Win32
4
+ class ClipboardError < StandardError; end
5
+ class Clipboard
6
+ VERSION = '0.4.0'
7
+
8
+ # Clipboard data types
9
+ TEXT = 1
10
+ OEMTEXT = 7
11
+ UNICODETEXT = 13
12
+
13
+ # Alloc constants
14
+ GMEM_MOVEABLE = 0x0002
15
+ GMEM_ZEROINIT = 0x0040
16
+ GHND = 0x0042
17
+
18
+ # Clipboard specific functions
19
+ @@OpenClipboard = Win32API.new('user32', 'OpenClipboard', 'L', 'I')
20
+ @@CloseClipboard = Win32API.new('user32', 'CloseClipboard', 'V', 'I')
21
+ @@GetClipboardData = Win32API.new('user32', 'GetClipboardData', 'I', 'P')
22
+ @@SetClipboardData = Win32API.new('user32', 'SetClipboardData', 'II', 'I')
23
+ @@EmptyClipboard = Win32API.new('user32', 'EmptyClipboard', 'V', 'I')
24
+
25
+ @@CountClipboardFormats = Win32API.new(
26
+ 'user32', 'CountClipboardFormats', 'V', 'I'
27
+ )
28
+
29
+ @@IsClipboardFormatAvailable = Win32API.new(
30
+ 'user32', 'IsClipboardFormatAvailable', 'I', 'I'
31
+ )
32
+
33
+ @@GetClipboardFormatName = Win32API.new(
34
+ 'user32', 'GetClipboardFormatName', 'IPI', 'I'
35
+ )
36
+
37
+ @@EnumClipboardFormats = Win32API.new(
38
+ 'user32', 'EnumClipboardFormats', 'I', 'I'
39
+ )
40
+
41
+ @@RegisterClipboardFormat = Win32API.new(
42
+ 'user32', 'RegisterClipboardFormat', 'P', 'I'
43
+ )
44
+
45
+ # Generic Win32 functions
46
+ @@GlobalAlloc = Win32API.new('kernel32', 'GlobalAlloc', 'II' ,'I')
47
+ @@GlobalLock = Win32API.new('kernel32', 'GlobalLock', 'I' ,'I')
48
+ @@GlobalFree = Win32API.new('kernel32', 'GlobalFree', 'I' ,'I')
49
+ @@Memcpy = Win32API.new('msvcrt', 'memcpy', 'IPI', 'I')
50
+
51
+ # Sets the clipboard contents to the data that you specify. You may
52
+ # optionally specify a clipboard format. The default is Clipboard::TEXT.
53
+ def self.set_data(clip_data, format = TEXT)
54
+ self.open
55
+ @@EmptyClipboard.call
56
+
57
+ # NULL terminate text
58
+ case format
59
+ when TEXT, OEMTEXT, UNICODETEXT
60
+ clip_data << "\0"
61
+ end
62
+
63
+ # Global Allocate a movable piece of memory.
64
+ hmem = @@GlobalAlloc.call(GHND, clip_data.length + 4)
65
+ mem = @@GlobalLock.call(hmem)
66
+ @@Memcpy.call(mem, clip_data, clip_data.length)
67
+
68
+ # Set the new data
69
+ if @@SetClipboardData.call(format, hmem) == 0
70
+ @@GlobalFree.call(hmem)
71
+ self.close
72
+ raise ClipboardError, "SetClipboardData() failed"
73
+ end
74
+
75
+ @@GlobalFree.call(hmem)
76
+ self.close
77
+ self
78
+ end
79
+
80
+ # Returns the data currently in the clipboard. If 'format' is
81
+ # specified, it will attempt to retrieve the data in that format. The
82
+ # default is Clipboard::TEXT.
83
+ def self.data(format = TEXT)
84
+ clipdata = ""
85
+ self.open
86
+ begin
87
+ clipdata = @@GetClipboardData.call(format)
88
+ rescue ArgumentError
89
+ # Assume failure is caused by no data in clipboard
90
+ end
91
+ self.close
92
+ clipdata
93
+ end
94
+
95
+ # An alias for Clipboard.data.
96
+ def self.get_data(format = TEXT)
97
+ self.data(format)
98
+ end
99
+
100
+ # Empties the contents of the clipboard.
101
+ def self.empty
102
+ self.open
103
+ @@EmptyClipboard.call
104
+ self.close
105
+ self
106
+ end
107
+
108
+ # Returns the number of different data formats currently on the
109
+ # clipboard.
110
+ def self.num_formats
111
+ count = 0
112
+ self.open
113
+ count = @@CountClipboardFormats.call
114
+ self.close
115
+ count
116
+ end
117
+
118
+ # Returns whether or not +format+ (an int) is currently available.
119
+ def self.format_available?(format)
120
+ @@IsClipboardFormatAvailable.call(format) == 0 ? false : true
121
+ end
122
+
123
+ # Returns the corresponding name for the given 'format_number', or nil
124
+ # if it does not exist. You cannot specify any of the predefined
125
+ # clipboard formats (or nil is returned), only registered formats.
126
+ def self.format_name(format)
127
+ val = nil
128
+ buf = 0.chr * 80
129
+ self.open
130
+ if @@GetClipboardFormatName.call(format, buf, buf.length) != 0
131
+ val = buf
132
+ end
133
+ self.close
134
+ val.split(0.chr).first rescue nil
135
+ end
136
+
137
+ # Returns a hash of all the current formats, with the format number as
138
+ # the key and the format name as the value for that key.
139
+ def self.formats
140
+ formats = {}
141
+ format = 0
142
+
143
+ self.open
144
+ while 0 != (format = @@EnumClipboardFormats.call(format))
145
+ buf = 0.chr * 80
146
+ @@GetClipboardFormatName.call(format, buf, buf.length)
147
+ formats[format] = buf.split(0.chr).first
148
+ end
149
+ self.close
150
+ formats
151
+ end
152
+
153
+ # Registers the given 'format' (a String) as a clipboard format, which
154
+ # can then be used as a valid clipboard format.
155
+ #
156
+ # If a registered format with the specified name already exists, a new
157
+ # format is not registered and the return value identifies the existing
158
+ # format. This enables more than one application to copy and paste data
159
+ # using the same registered clipboard format. Note that the format name
160
+ # comparison is case-insensitive.
161
+ #
162
+ # Registered clipboard formats are identified by values in the range 0xC000
163
+ # through 0xFFFF.
164
+ def self.register_format(format)
165
+ if @@RegisterClipboardFormat.call(format) == 0
166
+ raise ClipboardError, "RegisterClipboardFormat() call failed"
167
+ end
168
+ end
169
+
170
+ private
171
+
172
+ def self.open
173
+ if 0 == @@OpenClipboard.call(0)
174
+ raise ClipboardError, "OpenClipboard() failed"
175
+ end
176
+ end
177
+
178
+ def self.close
179
+ @@CloseClipboard.call
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,94 @@
1
+ ###########################################################################
2
+ # tc_clipboard.rb
3
+ #
4
+ # Test suite for the win32-clipboard package. This will copy and remove
5
+ # data from your clipboard. If your current clipboard data is crucial to
6
+ # you, please save it first.
7
+ ###########################################################################
8
+ base = File.basename(Dir.pwd)
9
+ if base == 'test' || base =~ /win32-clipboard/
10
+ Dir.chdir('..') if base == 'test'
11
+ $LOAD_PATH.unshift(Dir.pwd + '/lib')
12
+ Dir.chdir('test') if base =~ /win32-clipboard/
13
+ end
14
+
15
+ require "win32/clipboard"
16
+ require "test/unit"
17
+ include Win32
18
+
19
+ class TC_Win32ClipBoard < Test::Unit::TestCase
20
+ def test_version
21
+ assert_equal('0.4.0', Clipboard::VERSION)
22
+ end
23
+
24
+ def test_data
25
+ assert_respond_to(Clipboard, :data)
26
+ assert_nothing_raised{ Clipboard.data }
27
+ assert_kind_of(String, Clipboard.data, 'bad data type')
28
+ assert_raises(NameError){ Clipboard.data(CF_FOO) }
29
+ end
30
+
31
+ def test_set_data
32
+ assert_respond_to(Clipboard, :set_data)
33
+ assert_nothing_raised{ Clipboard.set_data("foo") }
34
+ assert_nothing_raised{
35
+ Clipboard.set_data('Ηελλας', Clipboard::UNICODETEXT)
36
+ }
37
+ assert_raises(NameError){ Clipboard.set_data('foo', CF_FOO) }
38
+ end
39
+
40
+ def test_set_and_get_ascii
41
+ assert_nothing_raised{ Clipboard.set_data('foobar') }
42
+ assert_equal('foobar', Clipboard.data)
43
+ end
44
+
45
+ def test_set_and_get_unicode
46
+ assert_nothing_raised{
47
+ Clipboard.set_data('Ηελλας', Clipboard::UNICODETEXT)
48
+ }
49
+ assert_equal('Ηελλας', Clipboard.data(Clipboard::UNICODETEXT))
50
+ end
51
+
52
+ def test_empty
53
+ assert_respond_to(Clipboard, :empty)
54
+ assert_nothing_raised{ Clipboard.empty }
55
+ end
56
+
57
+ def test_num_formats
58
+ assert_respond_to(Clipboard, :num_formats)
59
+ assert_nothing_raised{ Clipboard.num_formats }
60
+ assert_kind_of(Fixnum, Clipboard.num_formats)
61
+ end
62
+
63
+ # This TypeError check causes a segfault when using Win32API in 1.8.4 or
64
+ # earlier.
65
+ def test_register_format
66
+ assert_respond_to(Clipboard,:register_format)
67
+ assert_nothing_raised{ Clipboard.register_format('foo') }
68
+ #assert_raises(TypeError){ Clipboard.register_format(1) }
69
+ end
70
+
71
+ def test_formats
72
+ assert_respond_to(Clipboard, :formats)
73
+ assert_nothing_raised{ Clipboard.formats }
74
+ assert_kind_of(Hash, Clipboard.formats)
75
+ end
76
+
77
+ def test_format_available
78
+ assert_respond_to(Clipboard, :format_available?)
79
+ assert_nothing_raised{ Clipboard.format_available?(1) }
80
+ end
81
+
82
+ def test_format_name
83
+ assert_respond_to(Clipboard, :format_name)
84
+ assert_nothing_raised{ Clipboard.format_name(1) }
85
+ assert_nil(Clipboard.format_name(9999999))
86
+ assert_raises(TypeError){ Clipboard.format_name('foo') }
87
+ end
88
+
89
+ def test_constants
90
+ assert_not_nil(Clipboard::TEXT)
91
+ assert_not_nil(Clipboard::OEMTEXT)
92
+ assert_not_nil(Clipboard::UNICODETEXT)
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: win32-clipboard
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.4.0
7
+ date: 2006-02-24 00:00:00 -07:00
8
+ summary: A package for interacting with the Windows clipboard
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/win32utils
13
+ rubyforge_project: win32utils
14
+ description: A package for interacting with the Windows clipboard
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Daniel J. Berger
30
+ files:
31
+ - examples/clipboard_test.rb
32
+ - lib/win32/clipboard.rb
33
+ - test/tc_clipboard.rb
34
+ - CHANGES
35
+ - MANIFEST
36
+ - README
37
+ test_files:
38
+ - test/tc_clipboard.rb
39
+ rdoc_options: []
40
+
41
+ extra_rdoc_files:
42
+ - CHANGES
43
+ - README
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ requirements: []
49
+
50
+ dependencies: []
51
+