Debian Codegraph @

Tk/MainWindow.pm

raw
1 # Copyright (c) 1995-2003 Nick Ing-Simmons. All rights reserved.
2 # This program is free software; you can redistribute it and/or
3 # modify it under the same terms as Perl itself.
4 package Tk::MainWindow;
5 use base qw(Tk::Toplevel);
6 BEGIN { @MainWindow::ISA = 'Tk::MainWindow' }
7
8 use strict;
9
10 use vars qw($VERSION);
11 $VERSION = '4.015'; # was: sprintf '4.%03d', q$Revision: #12 $ =~ /\D(\d+)\s*$/;
12
13 use Tk::CmdLine;
14 use Tk qw(catch);
15 require Tk::Toplevel;
16
17 use Carp;
18
19 $| = 1;
20
21 my %Windows = ();
22
23 sub CreateArgs
24 {
25 my ($class,$args) = @_;
26 my $cmd = Tk::CmdLine->CreateArgs();
27 my $key;
28 foreach $key (keys %$cmd)
29 {
30 $args->{$key} = $cmd->{$key} unless exists $args->{$key};
31 }
32 my %result = $class->SUPER::CreateArgs(undef,$args);
33 my $name = delete($args->{'-name'});
34 unless (Tk::tainting)
35 {
36 $ENV{'DISPLAY'} = ':0' unless (exists $ENV{'DISPLAY'});
37 $result{'-screen'} = $ENV{'DISPLAY'} unless exists $result{'-screen'};
38 }
39 return (-name => "\l$name",%result);
40 }
41
42 sub new
43 {
44 my $package = shift;
45 if (@_ > 0 && $_[0] =~ /:\d+(\.\d+)?$/)
46 {
47 carp "Usage $package->new(-screen => '$_[0]' ...)" if $^W;
48 unshift(@_,'-screen');
49 }
50 croak('Odd number of args'."$package->new(" . join(',',@_) .')') if @_ % 2;
51 my %args = @_;
52
53 my $top = eval { bless Create($package->CreateArgs(\%args)), $package };
54 croak($@ . "$package->new(" . join(',',@_) .')') if ($@);
55 $top->apply_command_line;
56 $top->InitBindings;
57 $top->SetBindtags;
58 $top->InitObject(\%args);
59 eval { $top->configure(%args) };
60 croak "$@" if ($@);
61 if (($top->positionfrom||'') ne 'user' and ($top->sizefrom||'') ne 'user') {
62 my $geometry = $top->optionGet(qw(geometry Geometry));
63 if ($geometry) {
64 $top->geometry($geometry);
65 }
66 }
67 $Windows{$top} = $top;
68 return $top;
69 }
70
71 sub _Destroyed
72 {
73 my $top = shift;
74 $top->SUPER::_Destroyed;
75 delete $Windows{$top};
76 }
77
78 sub InitBindings
79 {
80 my $mw = shift;
81 $mw->bind('all','<Tab>','focusNext');
82 # <<LeftTab>> is named <<PrevWindow>> in Tcl/Tk
83 $mw->eventAdd(qw[<<LeftTab>> <Shift-Tab>]);
84 # This is needed for XFree86 systems
85 catch { $mw->eventAdd(qw[<<LeftTab>> <ISO_Left_Tab>]) };
86 # This seems to be correct on *some* HP systems.
87 catch { $mw->eventAdd(qw[<<LeftTab>> <hpBackTab>]) };
88 $mw->bind('all','<<LeftTab>>','focusPrev');
89 if ($mw->windowingsystem eq 'x11')
90 {
91 $mw->eventAdd(qw[<<Cut>> <Control-Key-x> <Lock-Control-Key-X> <Key-F20> <Meta-Key-w>]);
92 $mw->eventAdd(qw[<<Copy>> <Control-Key-c> <Lock-Control-Key-C> <Key-F16> <Control-Key-w>]);
93 $mw->eventAdd(qw[<<Paste>> <Control-Key-v> <Lock-Control-Key-V> <Key-F18> <Control-Key-y>]);
94 $mw->eventAdd(qw[<<PasteSelection>> <ButtonRelease-2>]);
95 $mw->eventAdd(qw[<<Undo>> <Control-Key-z> <Key-Undo> <Key-F14>
96 <Control-Key-underscore>]);
97 $mw->eventAdd(qw[<<Redo>> <Control-Key-y> <Shift-Key-Undo> <Key-F12> <Shift-Key-F14>]);
98 }
99 elsif ($mw->windowingsystem eq 'win32')
100 {
101 $mw->eventAdd(qw[<<Cut>> <Control-Key-x> <Shift-Key-Delete>]);
102 $mw->eventAdd(qw[<<Copy>> <Control-Key-c> <Control-Key-Insert>]);
103 $mw->eventAdd(qw[<<Paste>> <Control-Key-v> <Shift-Key-Insert>]);
104 $mw->eventAdd(qw[<<Undo>> <Control-Key-z>]);
105 $mw->eventAdd(qw[<<Redo>> <Control-Key-y>]);
106 }
107 elsif ($mw->windowingsystem eq 'aqua')
108 {
109 $mw->eventAdd(qw[<<Cut>> <Command-Key-x> <Key-F2>]);
110 $mw->eventAdd(qw[<<Copy>> <Command-Key-c> <Key-F3>]);
111 $mw->eventAdd(qw[<<Paste>> <Command-Key-v> <Key-F4>]);
112 $mw->eventAdd(qw[<<PasteSelection>> <ButtonRelease-2>]);
113 $mw->eventAdd(qw[<<Clear>> <Clear>]);
114 $mw->eventAdd(qw[<<Undo>> <Command-Key-z>]);
115 $mw->eventAdd(qw[<<Redo>> <Command-Key-y>]);
116 }
117 elsif ($mw->windowingsystem eq 'classic')
118 {
119 $mw->eventAdd(qw[<<Cut>> <Control-Key-x> <Key-F2>]);
120 $mw->eventAdd(qw[<<Copy>> <Control-Key-c> <Key-F3>]);
121 $mw->eventAdd(qw[<<Paste>> <Control-Key-v> <Key-F4>]);
122 $mw->eventAdd(qw[<<PasteSelection>> <ButtonRelease-2>]);
123 $mw->eventAdd(qw[<<Clear>> <Clear>]);
124 $mw->eventAdd(qw[<<Undo>> <Control-Key-z> <Key-F1>]);
125 $mw->eventAdd(qw[<<Redo>> <Control-Key-Z>]);
126 }
127
128 # FIXME - Should these move to Menubutton ?
129 my $c = ($Tk::platform eq 'unix') ? 'all' : 'Tk::Menubutton';
130 $mw->bind($c,'<Alt-KeyPress>',['TraverseToMenu',Tk::Ev('K')]);
131 $mw->bind($c,'<F10>','FirstMenu');
132 }
133
134 sub Existing
135 {
136 my @Windows;
137 foreach my $name (keys %Windows)
138 {
139 my $obj = $Windows{$name};
140 if (Tk::Exists($obj))
141 {
142 push(@Windows,$obj);
143 }
144 else
145 {
146 delete $Windows{$name};
147 }
148 }
149 return @Windows;
150 }
151
152 END
153 {
154 if (Tk::IsParentProcess())
155 {
156 foreach my $top (values %Windows)
157 {
158 if ($top->IsWidget)
159 {
160 # Tk data structuctures are still in place
161 # this can occur if non-callback perl code did a 'die'.
162 # It will also handle some cases of non-Tk 'exit' being called
163 # Destroy this mainwindow and hence is descendants ...
164 $top->destroy;
165 }
166 }
167 }
168 }
169
170 sub CmdLine { return shift->command }
171
172 sub WMSaveYourself
173 {
174 my $mw = shift;
175 my @args = @{$mw->command};
176 # warn 'preWMSaveYourself:'.join(' ',@args)."\n";
177 @args = ($0) unless (@args);
178 my $i = 1;
179 while ($i < @args)
180 {
181 if ($args[$i] eq '-iconic')
182 {
183 splice(@args,$i,1);
184 }
185 elsif ($args[$i] =~ /^-(geometry|iconposition)$/)
186 {
187 splice(@args,$i,2);
188 }
189 }
190
191 my @ip = $mw->wm('iconposition');
192 # print 'ip ',join(',',@ip),"\n";
193 my $icon = $mw->iconwindow;
194 if (defined($icon))
195 {
196 @ip = $icon->geometry =~ /\d+x\d+([+-]\d+)([+-]\d+)/;
197 }
198 splice(@args,1,0,'-iconposition' => join(',',@ip)) if (@ip == 2);
199
200 splice(@args,1,0,'-iconic') if ($mw->state() eq 'iconic');
201
202 splice(@args,1,0,'-geometry' => $mw->geometry);
203 # warn 'postWMSaveYourself:'.join(' ',@args)."\n";
204 $mw->command([@args]);
205 }
206
207 1;
208
209 __END__
210