home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Tk / NoteBook.pm < prev    next >
Encoding:
Perl POD Document  |  1997-08-10  |  9.2 KB  |  378 lines

  1. # $Id: NoteBook.pm,v 1.3 1997/02/08 19:11:39 rsi Exp $
  2. #
  3. # Implementation of NoteBook widget.
  4. # Derived from NoteBook.tcl in Tix 4.0
  5.  
  6. # Contributed by Rajappa Iyer <rsi@earthling.net>
  7. # Hack by Nick for 'menu' traversal.
  8.  
  9. package Tk::NoteBook;
  10.  
  11. use Tk qw(Ev);
  12.  
  13. use Carp;
  14. require Tk::Frame;
  15. require Tk::NBFrame;
  16. require Tk::VStack;
  17.  
  18. Tk::Widget->Construct("NoteBook");
  19.  
  20. @Tk::NoteBook::ISA = qw(Tk::VStack);
  21.  
  22. sub TraverseToNoteBook;
  23.  
  24. sub ClassInit {
  25.     my ($class,$mw) = @_;
  26.     if (0) {
  27.     # class binding does not work right due to extra level of
  28.     # widget hierachy
  29.     $mw->bind($class,"<ButtonPress-1>", ['MouseDown',Ev('x'),Ev('y')]);
  30.     $mw->bind($class,"<ButtonRelease-1>", ['MouseUp',Ev('x'),Ev('y')]);
  31.  
  32.     $mw->bind($class,"<B1-Motion>", ['MouseDown',Ev('x'),Ev('y')]);
  33.     $mw->bind($class,"<Left>", ['FocusNext','prev']);
  34.     $mw->bind($class,"<Right>", ['FocusNext','next']);
  35.  
  36.     $mw->bind($class,"<Return>", 'SetFocusByKey');
  37.     $mw->bind($class,"<space>", 'SetFocusByKey');
  38.     }
  39.     return $class;
  40. }
  41.  
  42. sub Populate {
  43.     my ($cw, $args) = @_;
  44.  
  45.     $cw->SUPER::Populate($args);
  46.     $cw->{"pad-x1"} = 0;
  47.     $cw->{"pad-x2"} = 0;
  48.     $cw->{"pad-y1"} = 20;
  49.     $cw->{"pad-y2"} = 0;
  50.  
  51.     my $f = $cw->Component("NBFrame" => "nbframe");
  52.  
  53.     if (1) {
  54.     # Should be class bindings
  55.     $f->bind("<ButtonPress-1>", sub {
  56.         $cw->MouseDown($f->XEvent->x, $f->XEvent->y);
  57.     });
  58.     $f->bind("<ButtonRelease-1>", sub {
  59.         $cw->MouseUp($f->XEvent->x, $f->XEvent->y);
  60.     });
  61.     $f->bind("<B1-Motion>", sub {
  62.         $cw->MouseDown($f->XEvent->x, $f->XEvent->y);
  63.     });
  64.     $f->bind("<Left>", sub {$cw->FocusNext("prev");});
  65.     $f->bind("<Right>", sub {$cw->FocusNext("next");});
  66.     $f->bind("<Return>", sub {$cw->SetFocusByKey;});
  67.     $f->bind("<space>", sub {$cw->SetFocusByKey;});
  68.      }
  69.  
  70.     $f->configure(-slave => 1, -takefocus => 1, -relief => "raised");
  71.     $cw->{"top"} = $f;
  72.     $cw->ConfigSpecs(-takefocus => ["SELF", "takeFocus", "TakeFocus", 0],
  73.              "DEFAULT" => [$f]);
  74. }
  75.  
  76. #---------------------------
  77. # Public methods
  78. #---------------------------
  79. sub add {
  80.     my ($w, $child, %args) = @_;
  81.     my $c = $w->SUPER::add($child, %args);
  82.     delete $args{-createcmd};
  83.     delete $args{-raisecmd};
  84.     if (keys %args) {
  85.     $w->{"top"}->add($child, %args);
  86.     }
  87.     return $c;
  88. }
  89.  
  90. sub raise {
  91.     my ($w, $child) = @_;
  92.     $w->SUPER::raise($child);
  93.     if ($w->{"top"}->pagecget($child, -state) eq "normal") {
  94.     $w->{"top"}->activate($child);
  95.     $w->{"top"}->focus($child);
  96.     }
  97. }
  98.  
  99. sub delete {
  100.     my ($w, $child) = @_;
  101.     $w->SUPER::delete($child);
  102.     if (defined $w->{"top"}->{$child}) {
  103.     $w->{"top"}->delete($child);
  104.     }
  105.     $w->Subwidget ( 'nbframe' )->delete ( $child );
  106. }
  107.  
  108. #---------------------------------------
  109. # Private methods
  110. #---------------------------------------
  111. sub Resize {
  112.     my ($w) = @_;
  113.  
  114.     my ($tW, $tH) = split(" ", $w->{"top"}->geometryinfo);
  115.     $w->{"pad-x1"} = 2;
  116.     $w->{"pad-x2"} = 2;
  117.     $w->{"pad-y1"} = $tH + (defined $w->{"-ipadx"} ? $w->{"-ipadx"} : 0) + 1;
  118.     $w->{"pad-y2"} = 2;
  119.     $w->{"minW"} = $tW;
  120.     $w->{"minH"} = $tH;
  121.     $w->SUPER::Resize;
  122. }
  123.  
  124. sub MouseDown {
  125.     my ($w, $x, $y) = @_;
  126.     my $name = $w->{"top"}->identify($x, $y);
  127.     $w->{"top"}->focus($name);
  128.     $w->{"down"} = $name;
  129. }
  130.  
  131. sub MouseUp {
  132.     my ($w, $x, $y) = @_;
  133.     my $name = $w->{"top"}->identify($x, $y);
  134.     if ((defined $name) &&
  135.     ($name eq $w->{"down"}) &&
  136.     ($w->{"top"}->pagecget($name, -state) eq "normal")) {
  137.     $w->{"top"}->activate($name);
  138.     $w->SUPER::raise($name);
  139.     } else {
  140.     $w->{"top"}->focus($name);
  141.     }
  142. }
  143.  
  144. sub FocusNext {
  145.     my ($w, $dir) = @_;
  146.     my $name;
  147.  
  148.     if (not defined $w->{"top"}->info("focus")) {
  149.     $name = $w->{"top"}->info("active");
  150.     $w->{"top"}->focus($name);
  151.     } else {
  152.     $name = $w->{"top"}->info("focus" . $dir);
  153.     $w->{"top"}->focus($name);
  154.     }
  155. }
  156.  
  157. sub SetFocusByKey {
  158.     my ($w) = @_;
  159.  
  160.     my $name = $w->{"top"}->info("focus");
  161.     if (defined $name) {
  162.     if ($w->{"top"}->pagecget($name, -state) eq "normal") {
  163.         $w->raise($name);
  164.         $w->{"top"}->activate($name);
  165.     }
  166.     }
  167. }
  168.  
  169. sub NoteBookFind {
  170.     my ($w, $char) = @_;
  171.  
  172.     foreach $page (@{$w->{"windows"}}) {
  173.     $i = $w->{"top"}->pagecget($page, -underline);
  174.     $c = substr($page, $i, 1);
  175.     if ($char =~ /$c/) {
  176.         if ($w->{"top"}->pagecget($page, -state) ne "disabled") {
  177.         return $page;
  178.         }
  179.     }
  180.     }
  181.     return undef;
  182. }
  183.  
  184. # This is called by TraveseToMenu when an <Alt-Keypress> occurs
  185. # See the code in Tk.pm
  186. sub FindMenu {
  187.     my ($w, $char) = @_;
  188.  
  189.     foreach $page (@{$w->{"windows"}}) {
  190.     $i = $w->{"top"}->pagecget($page, -underline);
  191.     my $l = $w->{"top"}->pagecget($page, -label);
  192.     next if (not defined $l);
  193.     $c = substr($l, $i, 1);
  194.     if ($char =~ /$c/i) {
  195.         if ($w->{"top"}->pagecget($page, -state) ne "disabled") {
  196.                 $w->{"keypage"} = $page;
  197.         return $w;
  198.         }
  199.     }
  200.     }
  201.     return undef;
  202. }
  203.  
  204. #
  205. # This is called to post the supposed 'menu'
  206. # when we have returned ourselves as a 'menu' matching
  207. # and <Alt-KeyPress>,  See the code in Tk.pm
  208. #
  209. sub PostFirst {
  210.     my ($w) = @_;
  211.     my $page = delete $w->{"keypage"};
  212.     if (defined $page) {
  213.     $w->raise($page);
  214.     }
  215. }
  216.  
  217. 1;
  218.  
  219. __END__
  220.  
  221. =head1 NAME
  222.  
  223. Tk::NoteBook - display several windows in limited space with notebook metaphor.
  224.  
  225. =head1 SYNOPSIS
  226.  
  227.   use Tk::NoteBook;
  228.   ...
  229.   $w = $frame->NoteBook();
  230.   $page1 = $w->add("page1", options);
  231.   $page2 = $w->add("page2", options);
  232.   ...
  233.  
  234. =head1 DESCRIPTION
  235.  
  236. The NoteBook widget provides a notebook metaphor to display several
  237. windows in limited space. The notebook is divided into a stack of pages
  238. of which only one is displayed at any time. The other pages can be
  239. selected by means of choosing the visual "tabs" at the top of the
  240. widget. Additionally, the <Tab> key may be used to traverse the pages.
  241. If B<-underline> is used, Alt- bindings will also work.
  242.  
  243. The widget takes all the options that a Frame does. In addition,
  244. it supports the following options:
  245.  
  246. =over 4
  247.  
  248. =item B<-dynamicgeometry>
  249.  
  250. If set to false (default and recommended), the size of the NoteBook
  251. will match the size of the largest page. Otherwise the size will
  252. match the size of the current page causing the NoteBook to change
  253. size when different pages of different sizes are selected.
  254.  
  255. =item B<-ipadx>
  256.  
  257. The amount of internal horizontal padding around the pages.
  258.  
  259. =item B<-ipady>
  260.  
  261. The amount of internal vertical padding around the pages.
  262.  
  263. =back
  264.  
  265. =head1 METHODS
  266.  
  267. The following methods may be used with a NoteBook object in addition
  268. to standard methods.
  269.  
  270. =over 4
  271.  
  272. =item B<add(>I<page>, I<options>B<)>
  273.  
  274. Adds a page with name I<page> to the notebook. Returns an object
  275. of type B<Frame>. The recognized I<options> are:
  276.  
  277. =over 4
  278.  
  279. =item B<-anchor>
  280.  
  281. Specifies how the information in a tab is to be displayed. Must be
  282. one of B<n>, B<ne>, B<e>, B<se>, B<s>, B<sw>, B<w>, B<nw> or
  283. B<center>.
  284.  
  285. =item B<-bitmap>
  286.  
  287. Specifies a bitmap to display on the tab of this page. The bitmap
  288. is displayed only if none of the B<-label> or B<-image> options
  289. are specified.
  290.  
  291. =item B<-image>
  292.  
  293. Specifies an image to display on the tab of this page. The image
  294. is displayed only if the B<-label> option is not specified.
  295.  
  296. =item B<-label>
  297.  
  298. Specifies the text string to display on the tab of this page.
  299.  
  300. =item B<-justify>
  301.  
  302. When there are multiple lines of text displayed in a tab, this
  303. option determines the justification of the lines.
  304.  
  305. =item B<-createcmd>
  306.  
  307. Specifies a Perl command to be called the first time the page is
  308. shown on the screen. This option can be used to delay the creation
  309. of the contents of a page until necessary. It can be useful in
  310. situations where there are a large number of pages in a NoteBook
  311. widget; with B<-createcmd> you do not have to make the user wait
  312. until all pages are constructed before displaying the first page.
  313.  
  314. =item B<-raisecmd>
  315.  
  316. Specifies a Perl command to be called whenever this page is raised
  317. by the user.
  318.  
  319. =item B<-state>
  320.  
  321. Specifies whether this page can be raised by the user. Must be
  322. either B<normal> or B<disabled>.
  323.  
  324. =item B<-underline>
  325.  
  326. Specifies the integer index of a character to underline in the
  327. tab. This option is used by the default bindings to implement
  328. keyboard traversal for menu buttons and menu entries. 0
  329. corresponds to the first character of text displayed on the
  330. widget, 1 to the next character and so on.
  331.  
  332. =item B<-wraplength>
  333.  
  334. This option specifies the maximum line length of the label string
  335. on this tab. If the line length of the label string exceeds this
  336. length, then it is wrapped onto the next line so that no line is
  337. longer than the specified length. The value may be specified in
  338. any standard forms for screen distances. If this value is less
  339. than or equal to 0, then no wrapping is done: lines will break
  340. only at newline characters in the text.
  341.  
  342. =back
  343.  
  344. =item B<delete(>I<page>B<)>
  345.  
  346. Deletes the page identified by I<page>.
  347.  
  348. =item B<pagecget(>I<page>, I<option>B<)>
  349.  
  350. Returns the current value of the configuration otion given by
  351. I<option> in the page given by I<page>. I<Option> may have any of
  352. the values accepted in the B<add> method.
  353.  
  354. =item B<pageconfigure(>I<page>, I<options>B<)>
  355.  
  356. Like configure for the page indicated by I<page>. I<Options> may
  357. be any of the options accepted by the B<add> method.
  358.  
  359. =item B<raise(>I<page>B<)>
  360.  
  361. Raise the page identified by I<page>.
  362.  
  363. =item B<raised()>
  364.  
  365. Returns the name of the currently raised page.
  366.  
  367. =back
  368.  
  369. =head1 AUTHOR
  370.  
  371. B<Rajappa Iyer> rsi@earthling.net
  372.  
  373. This code and documentation was derived from NoteBook.tcl in
  374. Tix4.0 written by Ioi Lam. It may be distributed under the same
  375. conditions as Perl itself.
  376.  
  377. =cut
  378.