| 1 | ############################################################ | ||
| 2 | # | ||
| 3 | # $Header: /var/lib/cvs/Tk_Multi/Multi/Toplevel.pm,v 1.9 2004/10/11 14:54:00 domi Exp $ | ||
| 4 | # | ||
| 5 | # $Source: /var/lib/cvs/Tk_Multi/Multi/Toplevel.pm,v $ | ||
| 6 | # $Revision: 1.9 $ | ||
| 7 | # $Locker: $ | ||
| 8 | # | ||
| 9 | ############################################################ | ||
| 10 | |||
| 11 | package Tk::Multi::Toplevel ; | ||
| 12 | |||
| 13 | use Carp ; | ||
| 14 | |||
| 15 | use strict ; | ||
| 16 | use Tk::Multi::Any ; | ||
| 17 | require Tk::Toplevel; | ||
| 18 | require Tk::Derived; | ||
| 19 | |||
| 20 | use vars qw(@ISA $VERSION) ; | ||
| 21 | $VERSION = sprintf "%d.%03d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/; | ||
| 22 | |||
| 23 | @ISA = qw(Tk::Derived Tk::Toplevel); | ||
| 24 | |||
| 25 | Tk::Widget->Construct('MultiTop') ; | ||
| 26 | |||
| 27 | |||
| 28 | sub Populate | ||
| 29 | { | ||
| 30 | my ($cw,$args) = @_ ; | ||
| 31 | Tk::Multi::Any::normalize($cw,$args) ; | ||
| 32 | |||
| 33 | require Tk::Multi::Manager ; | ||
| 34 | require Tk::Multi::Frame ; | ||
| 35 | require Tk::Multi::Text ; | ||
| 36 | require Tk::ObjScanner ; | ||
| 37 | |||
| 38 | $cw->{manager} = delete $args->{-manager} || $cw ; | ||
| 39 | $cw->{podName} = delete $args->{-podName} ; | ||
| 40 | $cw->{podSection} = delete $args->{-podSection} ; | ||
| 41 | |||
| 42 | my $showDebug = sub | ||
| 43 | { | ||
| 44 | # must not create 2 scanner windows | ||
| 45 | my $t = 'internals' ; | ||
| 46 | unless (defined $cw->getSlave($t)) | ||
| 47 | { | ||
| 48 | my $f = $cw -> newSlave(type => 'MultiFrame', | ||
| 49 | title => $t, | ||
| 50 | destroyable => 1); | ||
| 51 | $f -> ObjScanner('caller' => $cw->{manager}, destroyable => 0) | ||
| 52 | -> pack(-expand => 1); | ||
| 53 | } | ||
| 54 | } ; | ||
| 55 | |||
| 56 | # create common menu bar | ||
| 57 | my $w_menu = $cw -> | ||
| 58 | Frame(-relief => 'raised', -borderwidth => 2) -> pack(-fill => 'x'); | ||
| 59 | |||
| 60 | my $fmenu= $w_menu->Menubutton(-text => 'File', -underline => 0) ; | ||
| 61 | $fmenu-> pack(-side => 'left' ); | ||
| 62 | |||
| 63 | $cw->Advertise('fileMenu' => $fmenu->menu); | ||
| 64 | |||
| 65 | $fmenu->command | ||
| 66 | ( | ||
| 67 | -label => 'close', | ||
| 68 | -command => sub{$cw->destroy;} | ||
| 69 | ); | ||
| 70 | |||
| 71 | $fmenu->command | ||
| 72 | ( | ||
| 73 | -label => 'show internals...', | ||
| 74 | -command => $showDebug | ||
| 75 | ); | ||
| 76 | |||
| 77 | $cw->Advertise('menubar' => $w_menu); | ||
| 78 | |||
| 79 | # load MultiText::manager | ||
| 80 | my $mmgr = $cw -> MultiManager | ||
| 81 | ( | ||
| 82 | 'title' => 'windows' , | ||
| 83 | 'menu' => $w_menu , | ||
| 84 | 'help' => sub {$cw->showHelp() ;} | ||
| 85 | ) | ||
| 86 | -> pack (-expand => 1, -fill => 'both'); | ||
| 87 | |||
| 88 | $cw->Advertise('multiMgr' => $mmgr); | ||
| 89 | # bind dump info | ||
| 90 | #$self->{tk}{toplevel}->bind ('<Meta-d>', $showDebug); | ||
| 91 | |||
| 92 | |||
| 93 | $cw->ConfigSpecs( | ||
| 94 | '-relief' => [$cw], | ||
| 95 | '-borderwidth' => [$cw], | ||
| 96 | 'DEFAULT' => [$cw] | ||
| 97 | ) ; | ||
| 98 | $cw->Delegates | ||
| 99 | ( | ||
| 100 | newSlave => 'multiMgr', | ||
| 101 | getSlave => 'multiMgr', | ||
| 102 | hide => 'multiMgr', | ||
| 103 | show => 'multiMgr', | ||
| 104 | destroySlave => 'multiMgr', | ||
| 105 | 'add' => 'fileMenu', | ||
| 106 | 'delete' => 'fileMenu', | ||
| 107 | 'insert' => 'fileMenu', | ||
| 108 | DEFAULT => $cw | ||
| 109 | ) ; | ||
| 110 | |||
| 111 | # needed to avoid geometry problems with packAdjuster | ||
| 112 | #$cw->DoWhenIdle(sub{ $cw->packPropagate(0);}) ; | ||
| 113 | $cw->SUPER::Populate($args); | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub menuCommand | ||
| 118 | { | ||
| 119 | my $cw = shift ; | ||
| 120 | my %args = Tk::Multi::Any::normalize($cw,@_) ; | ||
| 121 | my $name = $args{-name}; | ||
| 122 | my $menu = $args{-menu} ; | ||
| 123 | |||
| 124 | unless (defined $cw->Subwidget($menu)) | ||
| 125 | { | ||
| 126 | my $mb = $cw->Subwidget('menubar') -> | ||
| 127 | Menubutton (-text => $menu) ; | ||
| 128 | $mb-> pack ( -fill => 'x' , -side => 'left'); | ||
| 129 | $cw->Advertise($menu => $mb ); | ||
| 130 | |||
| 131 | # first fill | ||
| 132 | $mb->command (-label => $name, -command => $args{-command}) ; | ||
| 133 | @{$cw->{menuItems}{$menu}} = ($name); | ||
| 134 | return ; | ||
| 135 | } | ||
| 136 | |||
| 137 | push @{$cw->{menuItems}{$menu}}, $name; | ||
| 138 | |||
| 139 | my %hash; | ||
| 140 | my $i = 1 ; | ||
| 141 | map($hash{$_}= $i++, sort @{$cw->{menuItems}{$menu}}) ; | ||
| 142 | |||
| 143 | my $pos = $hash{$name} == ($i-1) ? 'end' : $hash{$name} ; | ||
| 144 | $cw->Subwidget($menu) -> menu -> insert | ||
| 145 | ( | ||
| 146 | $pos,'command', | ||
| 147 | -label => $name, | ||
| 148 | -command => $args{-command} | ||
| 149 | ); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub menuRemove | ||
| 153 | { | ||
| 154 | my $cw = shift ; | ||
| 155 | my %args = Tk::Multi::Any::normalize($cw,@_) ; # name , menu | ||
| 156 | my $name = $args{-name}; # can be an array ref | ||
| 157 | my $menu = $args{-menu}; | ||
| 158 | |||
| 159 | my %hash; | ||
| 160 | my $i = 1; | ||
| 161 | map($hash{$_}= $i++, sort @{$cw->{menuItems}{$menu}}) ; | ||
| 162 | |||
| 163 | my @array = ref($name) ? @$name : ($name) ; | ||
| 164 | foreach (@array) | ||
| 165 | { | ||
| 166 | my $pos = $hash{$_} == ($i-1) ? 'end' : $hash{$_} ; | ||
| 167 | |||
| 168 | $cw->Subwidget($menu) -> menu ->delete($pos) ; | ||
| 169 | delete $hash{$_}; | ||
| 170 | @{$cw->{menuItems}{$menu}} = keys %hash ; # ugly | ||
| 171 | } | ||
| 172 | |||
| 173 | # cleanup | ||
| 174 | if (scalar @{$cw->{menuItems}{$menu}} == 0) | ||
| 175 | { | ||
| 176 | delete $cw->{menuItems}{$menu}; | ||
| 177 | $cw->Subwidget($menu)-> destroy ; | ||
| 178 | delete $cw->{SubWidget}{$menu}; # Tk::mega bug workaround | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | sub showHelp | ||
| 183 | { | ||
| 184 | my $cw = shift ; | ||
| 185 | my %args = $cw->normalize(@_) ; | ||
| 186 | my $podName = $args{-pod} ; | ||
| 187 | my $podSection = $args{-section} ; | ||
| 188 | |||
| 189 | require Tk::Pod::Text ; | ||
| 190 | require Tk::Pod ; | ||
| 191 | |||
| 192 | my $class = defined $podName ? $podName : | ||
| 193 | defined $cw->{podName} ? $cw->{podName} : ref($cw); | ||
| 194 | my $section = defined $podSection ? $podSection : | ||
| 195 | defined $cw->{podSection} ? $cw->{podSection} : 'DESCRIPTION' ; | ||
| 196 | |||
| 197 | my $podSpec = $class.'/"'.$section.'"' ; | ||
| 198 | |||
| 199 | my $topTk = $cw->MainWindow ; | ||
| 200 | |||
| 201 | #print "podW is ",ref($podWidget)," children ",$topTk->children,"\n"; | ||
| 202 | my ($pod) = grep (ref($_) eq 'Tk::Pod',$topTk->children) ; | ||
| 203 | #print "1 pod is $pod, ",ref($pod),"\n"; | ||
| 204 | |||
| 205 | unless (defined $pod) | ||
| 206 | { | ||
| 207 | #print "Creating Tk::Pod\n"; | ||
| 208 | $pod = $topTk->Pod() ; | ||
| 209 | } | ||
| 210 | |||
| 211 | #print "2 pod is $pod, ",ref($pod),"\n"; | ||
| 212 | |||
| 213 | # $podWidget = $topTk->Pod() | ||
| 214 | # unless (defined $podWidget and ref($podWidget) eq 'Tk::Pod' ); | ||
| 215 | |||
| 216 | # first param is 'reuse' or 'new'. | ||
| 217 | # Pod::Text cannot find a section befire it is displayed | ||
| 218 | #print $podSpec,"\n"; | ||
| 219 | $pod->Subwidget('pod')->Link('reuse',undef, $podSpec) | ||
| 220 | |||
| 221 | } | ||
| 222 | |||
| 223 | 1; | ||
| 224 | |||
| 225 | __END__ |