File Coverage

blib/lib/Win32/AutoItX/Window.pm
Criterion Covered Total %
statement 18 84 21.4
branch 0 44 0.0
condition 0 21 0.0
subroutine 6 19 31.5
pod 11 11 100.0
total 35 179 19.5


line stmt bran cond sub pod time code
1             package Win32::AutoItX::Window;
2              
3             =head1 NAME
4              
5             Win32::AutoItX::Window - OO interface for Windows
6              
7             =head1 SYNOPSIS
8              
9             use Win32::AutoItX;
10              
11             my $a = Win32::AutoItX->new;
12              
13             my $pid = $a->Run('calc.exe');
14             my $window = $a->get_window('Calculator');
15             $window->wait;
16              
17             local $\ = "\n";
18             print "Handle: $window";
19             print "Title: ", $window->title;
20             print "PID: ", $window->process;
21             print "x = ", $window->x, " y = ", $window->y;
22             print "width = ", $window->width, " height = ", $window->height;
23             $window->maximize;
24             sleep 2;
25             $window->restore;
26             sleep 2;
27             $window->move($window->x + 50, $window->y + 50);
28              
29             my $control = $window->get_focus;
30             print "Focused control is $control with text = ", $control->text;
31              
32             # List all buttons
33             my @buttons = $window->find_controls(undef, class => 'Button');
34             foreach my $c (@buttons) {
35             print "Button '$c' has text ", $c->text;
36             }
37              
38             =head1 DESCRIPTION
39              
40             Win32::AutoItX::Window provides an object-oriented interface for AutoItX
41             methods to operate with Windows.
42              
43             =cut
44              
45 1     1   6 use strict;
  1         1  
  1         24  
46 1     1   4 use warnings;
  1         1  
  1         30  
47              
48             our $VERSION = '1.00';
49              
50 1     1   4 use Carp;
  1         2  
  1         73  
51 1     1   5 use Scalar::Util qw{ blessed };
  1         2  
  1         40  
52 1     1   314 use Win32::AutoItX::Control;
  1         2  
  1         45  
53              
54 1     1   8 use overload fallback => 1, '""' => sub { $_[0]->handle };
  1     0   3  
  1         8  
  0            
55              
56             my %Helper_Methods = qw{
57             handle WinGetHandle
58             process WinGetProcess
59             state WinGetState
60             exists WinExists
61             is_active WinActive
62             text WinGetText
63             title WinGetTitle
64             x WinGetPosX
65             y WinGetPosY
66             width WinGetPosWidth
67             height WinGetPosHeight
68             client_width WinGetClientSizeWidth
69             client_height WinGetClientSizeHeight
70             activate WinActivate
71             close WinClose
72             kill WinKill
73             select_menu_item WinMenuSelectItem
74             move WinMove
75             set_on_top WinSetOnTop
76             set_state WinSetState
77             set_title WinSetTitle
78             set_transparency WinSetTrans
79             wait WinWait
80             wait_active WinWaitActive
81             wait_not_active WinWaitNotActive
82             wait_close WinWaitClose
83             };
84              
85             =head1 METHODS
86              
87             =head2 new
88              
89             $window = Win32::AutoItX::Window->new($autoitx, $window_title, $window_text)
90              
91             creates the window object.
92              
93             =cut
94              
95             sub new {
96 0     0 1   my $class = shift;
97 0           my %self;
98 0           $self{autoitx} = shift;
99 0           $self{title} = shift;
100 0           $self{text} = shift;
101 0 0         $self{text} = '' unless defined $self{text};
102             croak "The first argument should be a Win32::AutoItX object"
103 0 0 0       unless blessed $self{autoitx} and $self{autoitx}->isa('Win32::AutoItX');
104 0           return bless \%self, $class;
105             }
106             #-------------------------------------------------------------------------------
107              
108             =head2 get_class_list
109              
110             @class_list = $window->get_class_list()
111              
112             retrieves list with classes from the window.
113              
114             =cut
115              
116             sub get_class_list {
117 0     0 1   my $self = shift;
118             my @classes = split /\r?\n/,
119 0           $self->WinGetClassList($self->{title}, $self->{text});
120 0           return @classes;
121             }
122             #-------------------------------------------------------------------------------
123              
124             =head2 get_control
125              
126             $control = $window->get_control($control_id)
127              
128             returns a L object for the control specified by id.
129              
130             =cut
131              
132             sub get_control {
133 0     0 1   my $self = shift;
134 0           my $control_id = shift;
135             return Win32::AutoItX::Control->new(
136 0           $self->{autoitx}, $self->{title}, $self->{text}, $control_id
137             );
138             }
139             #-------------------------------------------------------------------------------
140              
141             =head2 get_focus
142              
143             $control = $window->get_focus()
144              
145             returns a L object for the control that has keyboard
146             focus within the window.
147              
148             =cut
149              
150             sub get_focus {
151 0     0 1   my $self = shift;
152             return Win32::AutoItX::Control->new(
153             $self->{autoitx}, $self->{title}, $self->{text},
154             $self->ControlGetFocus($self->{title}, $self->{text})
155 0           );
156             }
157             #-------------------------------------------------------------------------------
158              
159             =head2 find_controls
160              
161             $control = $window->find_controls($text, %options)
162             @controls = $window->find_controls($text, %options)
163              
164             returns a L object (or a list of objects in the list
165             context) for matched contols. C<$text> is a raw string or a C.
166              
167             Available options to filter controls:
168              
169             =over
170              
171             =item class
172              
173             a raw string or a C to filter by Control's class.
174              
175             =item visible
176              
177             get visible controls only (set by default).
178              
179             =item enabled
180              
181             get enabled controls only.
182              
183             =back
184              
185             =cut
186              
187             sub find_controls {
188 0     0 1   my $self = shift;
189 0           my $text = shift;
190 0           my %args = (
191             class => undef,
192             visible => 1,
193             enabled => 0,
194             @_
195             );
196              
197 0           my %by_classes;
198             my @controls;
199 0           foreach my $class ($self->get_class_list) {
200 0 0         if (defined $args{class}) {
201 0 0 0       if (ref $args{class} and ref $args{class} eq 'Regexp') {
202 0 0         next if $class !~ /$args{class}/;
203             } else {
204 0 0         next if $class ne $args{class};
205             }
206             }
207 0           $by_classes{$class}++;
208 0           my $control = $self->get_control(
209             "[CLASS:$class; INSTANCE:$by_classes{$class}]"
210             );
211 0 0 0       next if $args{visible} and not $control->is_visible;
212 0 0 0       next if $args{enabled} and not $control->is_enabled;
213 0 0         if (defined $text) {
214 0           my $control_text = $control->text;
215 0 0         next unless defined $control_text;
216 0 0 0       if (ref $text and ref $text eq 'Regexp') {
217 0 0         next if $control_text !~ /$text/;
218             } else {
219 0 0         next if $control_text ne $text;
220             }
221             }
222 0 0         return $control unless wantarray;
223 0           push @controls, $control;
224             }
225 0           return @controls;
226             }
227             #-------------------------------------------------------------------------------
228              
229             =head2 wait_control
230              
231             $control = $w->wait_control(%options)
232              
233             waits until the control will be visible and enabled (optionally) and returns a
234             L object.
235              
236             Optional arguments:
237              
238             =over
239              
240             =item control
241              
242             the control id
243              
244             =item class
245              
246             a raw string or a C to filter controls by class
247              
248             =item text
249              
250             a raw string or a C to filter controls by text
251              
252             =item enabled
253              
254             if true wait until the control will be enabled
255              
256             =item timeout
257              
258             timeout in seconds to wait the control (60 by default)
259              
260             =back
261              
262             =cut
263              
264             sub wait_control {
265 0     0 1   my $self = shift;
266 0           my %args = (
267             control => undef,
268             text => undef,
269             class => undef,
270             timeout => undef,
271             enabled => 0,
272             @_
273             );
274 0 0         $args{timeout} = 60 unless defined $args{timeout};
275 0           my $control;
276 0 0         $control = $self->get_control($args{control}) if defined $args{control};
277              
278 0           my $start = time;
279 0           while (time - $start < $args{timeout}) {
280 0 0         if (defined $control) {
281             return $control if $control->is_visible
282 0 0 0       and (not $args{enabled} or $control->is_enabled);
      0        
283             } else {
284 0 0         if (my $c = $self->find_controls($args{text},
285             class => $args{class},
286             visible => 1,
287             enabled => $args{enabled},
288             )) {
289 0           return $c;
290             }
291             }
292 0 0         sleep 1 if time - $start < $args{timeout};
293             }
294 0           croak "Timed out ($args{timeout} seconds)";
295             }
296             #-------------------------------------------------------------------------------
297              
298             =head2 handle
299              
300             $handle = $window->handle()
301              
302             retrieves the internal handle of the window.
303              
304             =head2 process
305              
306             $process = $window->process()
307              
308             retrieves the Process ID (PID) associated with the window.
309              
310             =head2 state
311              
312             $state = $window->state()
313              
314             returns a value indicating the state of the window. Multiple values are added
315             together so use biwise Bitwise And (C<&>) to examine the part you are interested
316             in:
317              
318             1 = Window exists
319             2 = Window is visible
320             4 = Windows is enabled
321             8 = Window is active
322             16 = Window is minimized
323             32 = Windows is maximized
324              
325             For example:
326              
327             print "Window is visible and enabled." if $window->state() & 6;
328              
329             =head2 exists
330              
331             $boolean = $window->exists()
332              
333             checks to see if the specified window exists.
334              
335             =head2 is_active
336              
337             $boolean = $window->is_active()
338              
339             checks to see if the window exists and is currently active.
340              
341             =head2 text
342              
343             $text = $window->text()
344              
345             retrieves the text from the window. Up to 64KB of window text can be retrieved.
346             It works on minimized windows, but only works on hidden windows if you've set
347            
348             $window->AutoItSetOption(WinDetectHiddenText => 1)
349              
350             =head2 title
351              
352             $title = $window->title()
353              
354             retrieves the full title from the window.
355              
356             =head2 x
357              
358             $x = $window->x()
359              
360             retrieves the X coordinate of the window.
361              
362             =head2 y
363              
364             $y = $window->y()
365              
366             retrieves the Y coordinate of the window.
367              
368             =head2 width
369              
370             $width = $window->width()
371              
372             retrieves the width of the window.
373              
374             =head2 height
375              
376             $height = $window->height()
377              
378             retrieves the height of the window.
379              
380             =head2 client_width
381              
382             $client_width = $window->client_width()
383              
384             retrieves the width of the window's client area.
385              
386             =head2 client_height
387              
388             $client_height = $window->client_height()
389              
390             retrieves the height of the window's client area.
391              
392             =head2 activate
393              
394             $window->activate()
395              
396             activates (gives focus to) the window.
397              
398             =head2 close
399              
400             $window->close()
401              
402             closes the window.
403              
404             =head2 kill
405            
406             $window->kill()
407              
408             forces the window to close.
409              
410             =head2 select_menu_item
411              
412             $window->select_menu_item($item1, $item2, ..., $item7)
413              
414             invokes a menu item of the window. You should note that underlined menu items
415             actually contain a & character to indicate the underlining. You can access menu
416             items up to six levels deep; and the window can be inactive, minimized, and/or
417             even hidden. It will only work on standard menus. Unfortunately, many menus in
418             use today are actually custom written or toolbars "pretending" to be menus.
419             This is true for most Microsoft applications.
420              
421             =head2 move
422              
423             $window->move($x, $y)
424             $window->move($x, $y, $width, $height)
425              
426             moves and/or resizes the window. It has no effect on minimized windows, but it
427             works on hidden windows. If very width and height are small (or negative), the
428             window will go no smaller than 112 x 27 pixels. If width and height are large,
429             the window will go no larger than approximately C<12+DesktopWidth> x
430             C<12+DesktopHeight> pixels. Negative values are allowed for the x and y
431             coordinates. In fact, you can move a window off screen; and if the window's
432             program is one that remembers its last window position, the window will appear
433             in the corner (but fully on-screen) the next time you launch the program.
434              
435             =head2 set_on_top
436              
437             $window->set_on_top($flag)
438              
439             changes the window's "Always On Top" attribute. C<$flag> determines whether the
440             window should have the "TOPMOST" flag set: 1=set on top flag, 0 = remove on top
441             flag.
442              
443             =head2 hide
444              
445             $window->hide()
446              
447             hides the window.
448              
449             =cut
450              
451 0     0 1   sub hide { $_[0]->set_state($_[0]->SW_HIDE) }
452              
453             =head2 show
454              
455             $window->show()
456              
457             shows the previously hidden window.
458              
459             =cut
460              
461 0     0 1   sub show { $_[0]->set_state($_[0]->SW_SHOW) }
462              
463             =head2 minimize
464              
465             $window->minimize()
466              
467             minimizes the window.
468              
469             =cut
470              
471 0     0 1   sub minimize { $_[0]->set_state($_[0]->SW_MINIMIZE) }
472              
473             =head2 maximize
474              
475             $window->maximize()
476              
477             maximizes the window.
478              
479             =cut
480              
481 0     0 1   sub maximize { $_[0]->set_state($_[0]->SW_MAXIMIZE) }
482              
483             =head2 restore
484              
485             $window->restore()
486              
487             undoes the window minimization or maximization.
488              
489             =cut
490              
491 0     0 1   sub restore { $_[0]->set_state($_[0]->SW_RESTORE) }
492              
493             =head2 set_title
494              
495             $window->set_title($title)
496              
497             changes the title of the window.
498              
499             =head2 set_transparency
500              
501             $window->set_transparency($transparency)
502              
503             sets the transparency of the window: a number in the range 0 - 255. The larger
504             the number, the more transparent the window will become.
505              
506             =head2 wait
507              
508             $window->wait()
509             $window->wait($timeout)
510              
511             pauses execution until the window exists. C<$timeout> in seconds.
512              
513             =head2 wait_active
514              
515             $window->wait_active()
516             $window->wait_active($timeout)
517              
518             pauses execution until the window is active. C<$timeout> in seconds.
519              
520             =head2 wait_not_active
521              
522             $window->wait_not_active()
523             $window->wait_not_active($timeout)
524              
525             pauses execution until the window is not active. C<$timeout> in seconds.
526              
527             =head2 wait_close
528              
529             $window->wait_close()
530             $window->wait_close($timeout)
531              
532             pauses execution until the window does not exist. C<$timeout> in seconds.
533              
534             =head2 AutoItX native methods
535              
536             This module also autoloads all AutoItX methods. For example:
537              
538             $window->WinActivate($win_title) unless $window->WinActive($win_title);
539              
540             Please see AutoItX Help file for documenation of all available methods.
541              
542             =cut
543              
544             sub AUTOLOAD {
545 0     0     my $self = shift;
546 0           my $method = our $AUTOLOAD;
547 0           $method =~ s/.*:://;
548              
549 0           my @params = @_;
550 0 0         if (exists $Helper_Methods{$method}) {
551 0           $method = $Helper_Methods{$method};
552 0           unshift @params, $self->{title}, $self->{text};
553             }
554             print "Call AutoItX method $method with params: @params\n"
555 0 0         if $self->{autoitx}->debug;
556 0           $self->{autoitx}->{autoit}->$method(@params);
557             }
558             #-------------------------------------------------------------------------------
559              
560             =head1 SEE ALSO
561              
562             =over
563              
564             =item L
565              
566             =item L
567              
568             =item AutoItX Help
569              
570             =back
571              
572             =head1 AUTHOR
573              
574             Mikhail Telnov EMikhail.Telnov@gmail.comE
575              
576             =head1 COPYRIGHT
577              
578             This software is copyright (c) 2017 by Mikhail Telnov.
579              
580             This library is free software; you may redistribute and/or modify it
581             under the same terms as Perl itself.
582              
583             =cut
584              
585             1;