| 1 | package Tk::Multi::Manager; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw($VERSION @ISA $errno); | ||
| 5 | |||
| 6 | use Carp ; | ||
| 7 | use Tk::Derived; | ||
| 8 | use Tk::Frame; | ||
| 9 | use Tk::Adjuster ; | ||
| 10 | use Tie::IxHash ; | ||
| 11 | use Tk::Multi::Any ; | ||
| 12 | |||
| 13 | @ISA = qw(Tk::Derived Tk::Frame); | ||
| 14 | # Items to export into callers namespace by default. Note: do not export | ||
| 15 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 16 | # Do not simply export all your public functions/methods/constants. | ||
| 17 | |||
| 18 | $VERSION = sprintf "%d.%03d", q$Revision: 2.5 $ =~ /(\d+)\.(\d+)/; | ||
| 19 | |||
| 20 | Tk::Widget->Construct('MultiManager'); | ||
| 21 | |||
| 22 | my $title_num = 0; | ||
| 23 | |||
| 24 | sub Populate | ||
| 25 | { | ||
| 26 | my ($cw,$args) = @_ ; | ||
| 27 | Tk::Multi::Any::normalize($cw,$args) ; | ||
| 28 | |||
| 29 | require Tk::Menubutton; | ||
| 30 | |||
| 31 | my $title = delete $args->{'-title'} || 'display'; | ||
| 32 | |||
| 33 | $cw->{trace} = delete $args->{'-trace'}; | ||
| 34 | |||
| 35 | my $userMenu = delete $args->{-menu} ; | ||
| 36 | |||
| 37 | unless (defined $userMenu) | ||
| 38 | { | ||
| 39 | $userMenu = $cw->Frame(-relief => 'raised', -borderwidth => 2); | ||
| 40 | $userMenu->pack(-fill => 'x'); | ||
| 41 | } | ||
| 42 | |||
| 43 | $cw->{menu}= $userMenu -> Menubutton (-text => $title) | ||
| 44 | -> pack(-side => 'left' ); | ||
| 45 | |||
| 46 | # add help menu on the right side | ||
| 47 | $cw->{help}= $userMenu -> Menubutton (-text => 'Help') | ||
| 48 | -> pack(-side => 'right') ; | ||
| 49 | |||
| 50 | # add global help if defined | ||
| 51 | my $help = delete $args->{'-help'} || | ||
| 52 | "If you read this text and if the help menu has no other entry ". | ||
| 53 | "than 'global', it means that the user of Tk::Multi did not provide ". | ||
| 54 | "any help for the application you're using. Shame on him."; | ||
| 55 | |||
| 56 | $cw->addHelp('global', $help); | ||
| 57 | |||
| 58 | #$cw->{slave} = Tie::IxHash -> new () ; | ||
| 59 | my %sHash ; | ||
| 60 | $cw->{tiedSlave} = tie %sHash, 'Tie::IxHash' ; | ||
| 61 | $cw->{slave} = \%sHash ; | ||
| 62 | |||
| 63 | #my $obj = $cw->{windowFrame} = $cw -> Frame(bg => 'red') | ||
| 64 | # ->pack(qw(-fill both)); | ||
| 65 | |||
| 66 | $cw->ConfigSpecs(DEFAULT => [$cw], | ||
| 67 | '-width' => ['SELF'], | ||
| 68 | '-height' => ['SELF'], | ||
| 69 | ) ; | ||
| 70 | #$cw->Delegates(DEFAULT => 'SELF' ) ; | ||
| 71 | #print "args: ",keys %args,"\n"; | ||
| 72 | $cw->SUPER::Populate($args) ; | ||
| 73 | } | ||
| 74 | |||
| 75 | # may add a new Note to the note book | ||
| 76 | # or create a separate sub-window to see windows side by side | ||
| 77 | # or move a Note to a separate window | ||
| 78 | # and vice-versa ? | ||
| 79 | sub newSlave | ||
| 80 | { | ||
| 81 | my $cw = shift ; | ||
| 82 | my %args = Tk::Multi::Any::normalize($cw,@_) ; | ||
| 83 | |||
| 84 | my $title = $args{'-title'} ||= $cw->Class . "-" . $title_num++ ; | ||
| 85 | |||
| 86 | my $slaveType = delete $args{'-type'}; | ||
| 87 | croak("No type specified\n") unless defined $slaveType ; | ||
| 88 | |||
| 89 | # add button if it doesn't exist | ||
| 90 | if (defined $cw->{slave}{$title}) | ||
| 91 | { | ||
| 92 | # display error message | ||
| 93 | $cw->BackTrace("Window $title already exists\n"); | ||
| 94 | } | ||
| 95 | elsif (defined $args{-before}) | ||
| 96 | { | ||
| 97 | my $bef = delete $args{-before} ; | ||
| 98 | my $idx = $cw->{tiedSlave}->Indices($bef); | ||
| 99 | # create the new entry in the tied hash and place it before | ||
| 100 | # $bef widget | ||
| 101 | $cw->{tiedSlave}->Splice($idx,0,$title => {}) | ||
| 102 | } | ||
| 103 | elsif (defined $args{-side} and delete $args{-side} eq 'top') | ||
| 104 | { | ||
| 105 | # create the new entry in the tied hash and put it on top | ||
| 106 | $cw->{tiedSlave}->Unshift($title => {}) ; | ||
| 107 | } | ||
| 108 | else | ||
| 109 | { | ||
| 110 | # create the new entry in the tied hash and put it on bottom | ||
| 111 | $cw->{tiedSlave}->Push($title => {}) ; | ||
| 112 | } | ||
| 113 | |||
| 114 | $cw->{slave}{$title}{show} = 1 ; | ||
| 115 | |||
| 116 | if (defined $args{'-hidden'} ) | ||
| 117 | { | ||
| 118 | $cw->{slave}{$title}{'show'} = 0 if $args{'-hidden'} == 1 ; | ||
| 119 | delete $args{'-hidden'} ; | ||
| 120 | } | ||
| 121 | |||
| 122 | # add help button if help is defined | ||
| 123 | $cw->addHelp('on '.$title, delete $args{-help}) if defined $args{'-help'} ; | ||
| 124 | |||
| 125 | # add control button | ||
| 126 | my $topmenu = $cw->{menu}->menu ; | ||
| 127 | $topmenu ->insert($cw->{tiedSlave}->Indices($title) + 1, | ||
| 128 | 'cascade', -label => $title ) ; | ||
| 129 | |||
| 130 | my $menu = $topmenu->Menu; | ||
| 131 | $topmenu->entryconfigure($title, -menu => $menu); | ||
| 132 | |||
| 133 | # checkButton does not work if I get the ref from inside the tied hash | ||
| 134 | # hence the use of the intermediate reference | ||
| 135 | my $var = $cw->{slave}{$title}{'show'} ; | ||
| 136 | $cw->{slave}{$title}{'showRef'} = \$var ; | ||
| 137 | $menu->checkbutton | ||
| 138 | ( | ||
| 139 | -label => 'show', | ||
| 140 | -variable => \$var, | ||
| 141 | -command => sub {$cw->updateVisi($title) ;} | ||
| 142 | ); | ||
| 143 | |||
| 144 | $cw->{slave}{$title}{submenu} = $menu ; | ||
| 145 | |||
| 146 | my $destroyable = delete $args{'-destroyable'} ; | ||
| 147 | |||
| 148 | # add widget and adjuster | ||
| 149 | my $wd = $cw->{slave}{$title}{widget} = | ||
| 150 | $cw -> $slaveType ('menu_button' => $menu, | ||
| 151 | qw/-relief raised -borderwidth 4/, | ||
| 152 | %args); | ||
| 153 | $cw->{slave}{$title}{adjuster} = $cw -> Adjuster (-widget => $wd); | ||
| 154 | |||
| 155 | |||
| 156 | if (defined $destroyable and $destroyable) | ||
| 157 | { | ||
| 158 | $menu->command(-label=>'destroy', | ||
| 159 | -command => sub{$cw->destroySlave($title);} ); | ||
| 160 | } | ||
| 161 | |||
| 162 | # bottom slave must not use the packAdjust method | ||
| 163 | $cw->updateVisi($title); | ||
| 164 | |||
| 165 | return $cw->{slave}{$title}{widget} ; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub getSlave | ||
| 169 | { | ||
| 170 | my $cw = shift ; | ||
| 171 | my $title = shift ; | ||
| 172 | return defined $cw->{slave}{$title} ? $cw->{slave}{$title}{widget} : undef; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub addHelp | ||
| 176 | { | ||
| 177 | my $cw = shift ; | ||
| 178 | my $label = shift ; | ||
| 179 | my $help = shift ; | ||
| 180 | |||
| 181 | my $sub = ref($help) eq 'CODE' ? $help : | ||
| 182 | sub | ||
| 183 | { | ||
| 184 | require Tk::Dialog ; | ||
| 185 | $cw ->Dialog('-title'=> "$label help", -text => $help ) -> Show(); | ||
| 186 | } ; | ||
| 187 | |||
| 188 | $cw->{'help'} -> command (-label => $label, -command => $sub); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub hide | ||
| 192 | { | ||
| 193 | my $cw = shift ; | ||
| 194 | my $title = shift ; | ||
| 195 | $cw-> updateVisi($title,0) ; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub show | ||
| 199 | { | ||
| 200 | my $cw = shift ; | ||
| 201 | my $title = shift ; | ||
| 202 | $cw-> updateVisi($title,1) ; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub updateVisi | ||
| 206 | { | ||
| 207 | my $cw = shift ; | ||
| 208 | my $title = shift ; | ||
| 209 | my $show = shift ; | ||
| 210 | |||
| 211 | my $ref = $cw->{slave}{$title}{'showRef'}; | ||
| 212 | if (defined $show) | ||
| 213 | { | ||
| 214 | # update value used by the check Button | ||
| 215 | $$ref = $cw->{slave}{$title}{'show'} = $show ; | ||
| 216 | } | ||
| 217 | else | ||
| 218 | { | ||
| 219 | $show = $cw->{slave}{$title}{'show'} = $$ref ; | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | my $slave = $cw->{slave}{$title} ; | ||
| 224 | # NB tiedSlave and slave are a tied IxHash | ||
| 225 | my $idx = $cw->{tiedSlave} -> Indices($title) ; | ||
| 226 | my $nextShowed ; | ||
| 227 | my $prevShowed ; | ||
| 228 | my $l = $cw->{tiedSlave}->Length() ; | ||
| 229 | print "$title index is $idx out of $l\n" if $cw->{trace} ; | ||
| 230 | for (my $i=0; $i<$l; $i++) | ||
| 231 | { | ||
| 232 | my $item = $cw->{tiedSlave}-> Values($i) ; | ||
| 233 | $prevShowed = $i if ($i<$idx && $item->{'show'}) ; | ||
| 234 | if ($i>$idx && $item->{'show'}) | ||
| 235 | { | ||
| 236 | $nextShowed = $i ; | ||
| 237 | last ; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | print "$title show is $show\n" if $cw->{trace} ; | ||
| 242 | print "$title previous showed is index $prevShowed\n" if $cw->{trace} ; | ||
| 243 | print "$title next showed is index $nextShowed\n" if $cw->{trace} ; | ||
| 244 | |||
| 245 | my @pargs = qw/-fill both -expand 1 -anchor n/; | ||
| 246 | my $currentW = $cw->{slave}{$title}{widget}; | ||
| 247 | |||
| 248 | # so that the master window update its size when a slave is hidden | ||
| 249 | # or shown | ||
| 250 | $cw->packPropagate(1); | ||
| 251 | |||
| 252 | if ($show) | ||
| 253 | { | ||
| 254 | if (defined $nextShowed) | ||
| 255 | { | ||
| 256 | print "$title uses packAdjust\n" if $cw->{trace} ; | ||
| 257 | |||
| 258 | my $nextWidget = $cw->{tiedSlave}-> Values($nextShowed)->{widget} ; | ||
| 259 | $currentW->pack(@pargs, -before => $nextWidget ); | ||
| 260 | $cw->{slave}{$title}{adjuster}-> packAfter($currentW ); | ||
| 261 | } | ||
| 262 | else | ||
| 263 | { | ||
| 264 | print "$title uses pack\n" if $cw->{trace} ; | ||
| 265 | if (defined $prevShowed) | ||
| 266 | { | ||
| 267 | print "index $prevShowed uses packAjust\n" if $cw->{trace} ; | ||
| 268 | my $prevWidget = | ||
| 269 | $cw->{tiedSlave}-> Values($prevShowed)->{widget} ; | ||
| 270 | |||
| 271 | $cw->{tiedSlave}-> Values($prevShowed)->{adjuster}-> | ||
| 272 | packAfter($prevWidget ); | ||
| 273 | } | ||
| 274 | $currentW -> pack(@pargs) ; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | else | ||
| 278 | { | ||
| 279 | if (defined $prevShowed and not defined $nextShowed) | ||
| 280 | { | ||
| 281 | print "index $prevShowed uses pack\n" if $cw->{trace} ; | ||
| 282 | my $w = $cw->{tiedSlave}-> Values($prevShowed)->{adjuster} ; | ||
| 283 | $w->packForget(1); | ||
| 284 | $cw->{tiedSlave}-> Values($prevShowed)->{widget}-> | ||
| 285 | pack(@pargs); | ||
| 286 | } | ||
| 287 | #hide it | ||
| 288 | print "$title uses packForget\n" if $cw->{trace} ; | ||
| 289 | $cw->{slave}{$title}{adjuster} -> packForget(1) ; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | sub destroySlave | ||
| 294 | { | ||
| 295 | my $cw = shift ; | ||
| 296 | my $title = shift ; | ||
| 297 | |||
| 298 | die "Slave $title does not exist\n" | ||
| 299 | unless defined $cw->{slave}{$title}{widget} ; | ||
| 300 | |||
| 301 | # first remove the display from the window | ||
| 302 | $cw->updateVisi($title,0); | ||
| 303 | |||
| 304 | # retrieve actual menu object from the MenuButtom | ||
| 305 | my $cm = $cw->{menu} -> cget(-menu); | ||
| 306 | |||
| 307 | $cw->{slave}{$title}{widget}->destroy; | ||
| 308 | $cw->{slave}{$title}{submenu}->destroy; | ||
| 309 | |||
| 310 | my $idx = $cw->{tiedSlave}->Indices($title); | ||
| 311 | $cw->{tiedSlave}->Splice($idx,1); | ||
| 312 | |||
| 313 | # delete the actual Menu entry from topmenu | ||
| 314 | $cm -> delete($title) ; | ||
| 315 | |||
| 316 | #delete $cw->{slave}{$title}; | ||
| 317 | } | ||
| 318 | |||
| 319 | 1; | ||
| 320 | |||
| 321 | __END__ |