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

  1. # $Id: DialogBox.pm,v 1.4 1996/12/02 00:34:21 rsi Exp rsi $
  2. #
  3. # DialogBox is similar to Dialog except that it allows any widget
  4. # in the top frame. Widgets can be added with the add method. Currently
  5. # there exists no way of deleting a widget once it has been added.
  6.  
  7. package Tk::DialogBox;
  8.  
  9. use English;
  10. use Carp;
  11.  
  12. require Tk::Toplevel;
  13. require Tk::Frame;
  14.  
  15. @ISA = qw(Tk::Toplevel Tk::Frame);
  16.  
  17. Tk::Widget->Construct("DialogBox");
  18.  
  19. sub Populate {
  20.     my ($cw, $args) = @_;
  21.  
  22.     $cw->SUPER::Populate($args);
  23.     my $buttons = delete $args->{"-buttons"};
  24.     $buttons = ["OK"] unless defined $buttons;
  25.     my $default_button = delete $args->{"-default_button"};
  26.     $default_button = $buttons->[0] unless defined $default_button;
  27.  
  28.     $cw->{"selected_button"} = '';
  29.     $cw->withdraw;
  30.     $cw->protocol("WM_DELETE_WINDOW" => sub {});
  31.     $cw->transient($cw->toplevel);
  32.  
  33.     # create the two frames
  34.     my $top = $cw->Component(Frame, "top");
  35.     $top->configure(-relief => "raised", -bd => 1);
  36.     $top->pack(-side => "top", -fill => "both", -ipady => 3, -ipadx => 3);
  37.     my $bot = $cw->Component(Frame, "bottom");
  38.     $bot->configure(-relief => "raised", -bd => 1);
  39.     $bot->pack(-side => "top", -fill => "both", -ipady => 3, -ipadx => 3);
  40.  
  41.     # create a row of buttons in the bottom.
  42.     foreach $bl (@$buttons) {
  43.     $b = $bot->Button(-text => $bl,
  44.               -command => [ sub {
  45.                   $_[0]->{"selected_button"} = $_[1];
  46.               }, $cw, $bl]);
  47.     if ($bl eq $default_button) {
  48.         $db = $bot->Frame(-relief => "sunken", -bd => 1);
  49.         $b->raise($db);
  50.         $b->pack(-in => $db, -padx => "2", -pady => "2");
  51.         $db->pack(-side => "left", -expand => 1, -padx => 1, -pady => 1);
  52.         $cw->bind("<Return>" => [ sub {
  53.         $_[2]->flash;
  54.         $_[1]->{"selected_button"} = $_[3];
  55.         }, $cw, $b, $bl]);
  56.         $cw->{"default_button"} = $b;
  57.     } else {
  58.         $b->pack(-side => "left", -expand => 1,  -padx => 1, -pady => 1);
  59.     }
  60.     }
  61. }
  62.  
  63. sub add {
  64.     my ($cw, $wnam, %args) = @_;
  65.     my $w = $cw->Subwidget("top")->$wnam(%args);
  66.     $cw->Advertise("\L$wnam" => $w);
  67.     return $w;
  68. }
  69.  
  70. sub Show {
  71.     my ($cw, $grab) = @_;
  72.     croak "DialogBox: `Show' method requires at least 1 argument"
  73.     if scalar @_ < 1;
  74.     my $old_focus = $cw->focusSave;
  75.     my $old_grab = $cw->grabSave;
  76.  
  77.     $cw->Subwidget("top")->pack;
  78.     $cw->Subwidget("bottom")->pack;
  79.  
  80.     $cw->Popup();
  81.     if (defined $grab && length $grab && ($grab =~ /global/)) {
  82.     $cw->grabGlobal;
  83.     } else {
  84.     $cw->grab;
  85.     }
  86.     $cw->waitVisibility;
  87.     if (defined $cw->{"default_button"}) {
  88.     $cw->{"default_button"}->focus;
  89.     } else {
  90.     $cw->focus;
  91.     }
  92.     $cw->waitVariable(\$cw->{"selected_button"});
  93.     $cw->grabRelease;
  94.     $cw->withdraw;
  95.     &$old_focus;
  96.     &$old_grab;
  97.     return $cw->{"selected_button"};
  98. }
  99.  
  100. 1;
  101.  
  102. __END__
  103.  
  104. =head1 NAME
  105.  
  106. Tk::DialogBox - create and manipulate a dialog screen.
  107.  
  108. =head1 SYNOPSIS
  109.  
  110.     use Tk::DialogBox
  111.     ...
  112.     $d = $top->DialogBox(-title => "Title", -buttons => ["OK", "Cancel"]);
  113.     $w = $d->add(Widget, args);
  114.     ...
  115.     $button = $d->Show;
  116.  
  117. =head1 DESCRIPTION
  118.  
  119. B<DialogBox> is very similar to B<Dialog> except that it allows
  120. any widget in the top frame. B<DialogBox> creates two
  121. frames---"top" and "bottom". The bottom frame shows all the
  122. specified buttons, lined up from left to right. The top frame acts
  123. as a container for all other widgets that can be added with the
  124. B<add()> method. The non-standard options recognized by
  125. B<DialogBox> are as follows:
  126.  
  127. =over 4
  128.  
  129. =item B<-title>
  130.  
  131. Specify the title of the dialog box. If this is not set, then the
  132. name of the program is used.
  133.  
  134. =item B<-buttons>
  135.  
  136. The buttons to display in the bottom frame. This is a reference to
  137. an array of strings containing the text to put on each
  138. button. There is no default value for this. If you do not specify
  139. any buttons, no buttons will be displayed.
  140.  
  141. =item B<-default_button>
  142.  
  143. Specifies the default button that is considered invoked when user
  144. presses <Return> on the dialog box. This button is highlighted. If
  145. no default button is specified, then the first element of the
  146. array whose reference is passed to the B<-buttons> option is used
  147. as the default.
  148.  
  149. =back
  150.  
  151. =head1 METHODS
  152.  
  153. B<DialogBox> supports only two methods as of now:
  154.  
  155. =over 4
  156.  
  157. =item B<add(>I<widget>, I<options>B<)>
  158.  
  159. Add the widget indicated by I<widget>. I<Widget> can be the name
  160. of any Tk widget (standard or contributed). I<Options> are the
  161. options that the widget accepts. The widget is advertized as a
  162. subwidget of B<DialogBox>.
  163.  
  164. =item B<Show(>I<grab>B<)>
  165.  
  166. Display the dialog box, until user invokes one of the buttons in
  167. the bottom frame. If the grab type is specified in I<grab>, then
  168. B<Show> uses that grab; otherwise it uses a local grab. Returns
  169. the name of the button invoked.
  170.  
  171. =back
  172.  
  173. =head1 BUGS
  174.  
  175. There is no way of removing a widget once it has been added to the
  176. top frame.
  177.  
  178. There is no control over the appearance of the buttons in the
  179. bottom frame nor is there any way to control the placement of the
  180. two frames with respect to each other e.g. widgets to the left,
  181. buttons to the right instead of widgets on the top and buttons on
  182. the bottom always.
  183.  
  184. =head1 AUTHOR
  185.  
  186. B<Rajappa Iyer> rsi@earthling.net
  187.  
  188. This code is distributed under the same terms as Perl.
  189.  
  190. =cut
  191.