win32-window 0.1.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ ## win32-window
2
+ ============
3
+
4
+ Ruby interface to the Win32 window management APIs.
@@ -0,0 +1 @@
1
+ $version = '0.1.0.pre'
@@ -0,0 +1,188 @@
1
+ require 'win32/api'
2
+ include Win32
3
+
4
+ module Win32
5
+ class Rect < Struct.new(:left, :top, :right, :bottom)
6
+ end
7
+
8
+ class Point < Struct.new(:x, :y)
9
+ end
10
+
11
+ class Size < Struct.new(:width, :height)
12
+ end
13
+
14
+ class Window
15
+ def initialize(handle)
16
+ @handle = handle
17
+ end
18
+
19
+ def self.find(opts = {})
20
+ matches = []
21
+
22
+ callback = API::Callback.new('LP', 'I') { |handle|
23
+ match = true
24
+ match &&= opts[:title].nil? || (get_title(handle) =~ opts[:title])
25
+ match &&= opts[:pid].nil? || (get_pid(handle) == opts[:pid])
26
+
27
+ if match
28
+ matches << Window.new(handle)
29
+ end
30
+
31
+ 1
32
+ }
33
+
34
+ EnumWindows.call(callback, nil)
35
+
36
+ return matches
37
+ end
38
+
39
+ def self.desktop
40
+ Window.new(GetDesktopWindow.call())
41
+ end
42
+
43
+ def self.foreground
44
+ Window.new(GetForegroundWindow.call())
45
+ end
46
+
47
+ def title
48
+ Window.get_title(@handle)
49
+ end
50
+
51
+ def topmost=(topmost)
52
+ SetWindowPos.call(@handle, topmost ? HWND_TOPMOST : HWND_NOTTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE)
53
+ end
54
+
55
+ def minimize
56
+ CloseWindow.call(@handle)
57
+ end
58
+
59
+ def minimized?
60
+ IsIconic.call(@handle) != 0
61
+ end
62
+
63
+ def visible?
64
+ IsWindowVisible.call(@handle) != 0
65
+ end
66
+
67
+ def parent
68
+ parent = GetParent.call(@handle)
69
+ if parent == 0
70
+ nil
71
+ else
72
+ Window.new(parent)
73
+ end
74
+ end
75
+
76
+ def pid
77
+ Window.get_pid(@handle)
78
+ end
79
+
80
+ def move(x, y)
81
+ SetWindowPos.call(@handle, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER)
82
+ end
83
+
84
+ def resize(width, height)
85
+ SetWindowPos.call(@handle, 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER)
86
+ end
87
+
88
+ def location
89
+ g = geometry()
90
+ Point.new(g.left, g.top)
91
+ end
92
+
93
+ def size
94
+ g = geometry()
95
+ Size.new(g.right - g.left, g.bottom - g.top)
96
+ end
97
+
98
+ def geometry
99
+ rect = Window.buffer(4 * 4)
100
+ GetWindowRect.call(@handle, rect)
101
+ left, top, right, bottom = rect.unpack('LLLL')
102
+ return Rect.new(left, top, right, bottom)
103
+ end
104
+
105
+ def client_geometry
106
+ rect = Window.buffer(4 * 4)
107
+ GetClientRect.call(@handle, rect)
108
+ left, top, right, bottom = rect.unpack('LLLL')
109
+
110
+ width = right - left
111
+ height = bottom - top
112
+
113
+ point = point(left, top)
114
+ ClientToScreen.call(@handle, point)
115
+ screen_left, screen_top = point.unpack('LL')
116
+
117
+ return Rect.new(screen_left, screen_top, screen_left + width, screen_top + height)
118
+ end
119
+
120
+ private
121
+ def self.get_title(handle)
122
+ if GetWindowTextW.call(handle, buffer = "\0" * 1024, buffer.length) == 0
123
+ nil
124
+ else
125
+ buffer.force_encoding('utf-16LE').encode('utf-8').rstrip
126
+ end
127
+ end
128
+
129
+ def self.get_pid(handle)
130
+ buf = buffer(4)
131
+ GetWindowThreadProcessId.call(handle, buf)
132
+ buf.unpack('L')[0]
133
+ end
134
+
135
+ def self.buffer(bytes)
136
+ ' ' * bytes
137
+ end
138
+
139
+ def point(x, y)
140
+ [x, y].pack('LL')
141
+ end
142
+ end
143
+
144
+ EnumWindows = API.new('EnumWindows', 'KP', 'L', 'user32')
145
+ GetWindowTextW = API.new('GetWindowTextW', 'LPI', 'I', 'user32')
146
+
147
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633519(v=vs.85).aspx
148
+ GetWindowRect = API.new('GetWindowRect', 'IP', 'L', 'user32')
149
+
150
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633503(v=vs.85).aspx
151
+ GetClientRect = API.new('GetClientRect', 'IP', 'L', 'user32')
152
+
153
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/dd183434(v=vs.85).aspx
154
+ ClientToScreen = API.new('ClientToScreen', 'IP', 'L', 'user32')
155
+
156
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx
157
+ SetWindowPos = API.new('SetWindowPos', 'IIIIIII', 'L', 'user32')
158
+
159
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx
160
+ GetWindowThreadProcessId = API.new('GetWindowThreadProcessId', 'IP', 'L', 'user32')
161
+
162
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632678(v=vs.85).aspx
163
+ CloseWindow = API.new('CloseWindow', 'I', 'L', 'user32')
164
+
165
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633504(v=vs.85).aspx
166
+ GetDesktopWindow = API.new('GetDesktopWindow', '', 'I', 'user32')
167
+
168
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx
169
+ GetForegroundWindow = API.new('GetForegroundWindow', '', 'I', 'user32')
170
+
171
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633510(v=vs.85).aspx
172
+ GetParent = API.new('GetParent', 'I', 'I', 'user32')
173
+
174
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633527(v=vs.85).aspx
175
+ IsIconic = API.new('IsIconic', 'I', 'I', 'user32')
176
+
177
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633530(v=vs.85).aspx
178
+ IsWindowVisible = API.new('IsWindowVisible', 'I', 'I', 'user32')
179
+
180
+ HWND_BOTTOM = 1
181
+ HWND_NOTTOPMOST = -2
182
+ HWND_TOP = 0
183
+ HWND_TOPMOST = -1
184
+
185
+ SWP_NOSIZE = 0x0001
186
+ SWP_NOZORDER = 0x0004
187
+ SWP_NOMOVE = 0x0002
188
+ end
@@ -0,0 +1,2 @@
1
+ require 'win32/window/window'
2
+ require 'win32/window/version'
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: win32-window
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Chris Schmich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.2.2
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.2.2
30
+ description: ! " A Ruby library to work with Microsoft Windows' Win32 window management
31
+ APIs,\n including search, enumeration, and window manipulation.\n"
32
+ email: schmch@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/win32/window/version.rb
38
+ - lib/win32/window/window.rb
39
+ - lib/win32/window.rb
40
+ - README.md
41
+ homepage: https://github.com/schmich/win32-window
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>'
57
+ - !ruby/object:Gem::Version
58
+ version: 1.3.1
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Ruby interface to the Win32 window management APIs.
65
+ test_files: []
66
+ has_rdoc: