source: OFFICIAL/FCM_V1.3/bin/fcm_gui_internal @ 7

Last change on this file since 7 was 1, checked in by fcm, 15 years ago

creation de larborescence

File size: 6.9 KB
Line 
1#!/usr/bin/perl
2# ------------------------------------------------------------------------------
3# NAME
4#   fcm_gui_internal
5#
6# SYNOPSIS
7#   fcm_gui_internal POS FUNCTION
8#
9# DESCRIPTION
10#   The fcm_gui_internal command is part of a simple graphical user interface
11#   for some of the commands of the FCM system. The argument POS is a geometry
12#   string used by the &Main::get_input method to determine the location of the
13#   pop up window. The argument FUNCTION must be a keyword recognised by the
14#   &Fcm::Cm::cm_command function.
15#
16# COPYRIGHT
17#   (C) Crown copyright Met Office. All rights reserved.
18#   For further details please refer to the file COPYRIGHT.txt
19#   which you should have received as part of this distribution.
20# ------------------------------------------------------------------------------
21
22# Standard pragmas
23use warnings;
24use strict;
25
26# Standard modules
27use Tk;
28use File::Basename;
29use File::Spec::Functions;
30
31# FCM component modules:
32use lib catfile (dirname (dirname ($0)), 'lib');
33use Fcm::Cm;
34use Fcm::Config;
35
36# ------------------------------------------------------------------------------
37
38# Get configuration settings
39my $config = Fcm::Config->new ();
40$config->get_config ();
41
42my $pos      = shift @ARGV;
43my $function = shift @ARGV;
44cm_command $function;
45
46# ------------------------------------------------------------------------------
47# SYNOPSIS
48#   $cfg = &main::cfg ();
49#
50# DESCRIPTION
51#   Return the $config variable.
52# ------------------------------------------------------------------------------
53
54sub cfg {
55  return $config;
56}
57
58# ------------------------------------------------------------------------------
59# SYNOPSIS
60#   $ans = &main::get_input (
61#     TITLE   => $title,
62#     MESSAGE => $mesg,
63#     TYPE    => $type,
64#     DEFAULT => $def,
65#   );
66#
67# DESCRIPTION
68#   Get an input string from the user and return it as $ans. If TYPE is 'YN', a
69#   'YesNo' type message box will be displayed to prompt the user to click
70#   either the 'yes' or 'no' button. If TYPE is 'YNA', then an 'all' button is
71#   provided as a third option. Otherwise, a dialog box with an entry box for
72#   the user to type in a string will be displayed. TITLE is the title of the
73#   dialog box, and MESSAGE is the main message of the dialog box. If DEFAULT is
74#   set, $ans is set to the default value when the dialog box is invoked.
75# ------------------------------------------------------------------------------
76
77sub get_input {
78  my %args  = @_;
79  my $title = exists $args{TITLE}   ? $args{TITLE}   : '';
80  my $mesg  = exists $args{MESSAGE} ? $args{MESSAGE} : '';
81  my $type  = exists $args{TYPE}    ? $args{TYPE}    : '';
82  my $def   = exists $args{DEFAULT} ? $args{DEFAULT} : '';
83  my $ans   = '';
84
85  # Flush output
86  my $flush = $|;
87  $| = 1;
88
89  # Create a main window
90  my $mw = MainWindow->new;
91  $mw->title ($title);
92
93  # Define the default which applies if the dialog box is just closed or
94  # the user selects 'cancel'
95  $ans = $def ? $def : '';
96
97  if ($type =~ /^yn/i) {
98    # Create a yes-no(-all) dialog box
99
100    # If TYPE is YNA then add a third button: 'all'
101    my $buttons;
102    if ($type =~ /a$/i) {
103      $buttons = 3;
104
105    } else {
106      $buttons = 2;
107    }
108
109    # Message of the dialog box
110    $mw->Label ('-text' => $mesg)->grid (
111      '-row'        => 0,
112      '-column'     => 0,
113      '-columnspan' => $buttons,
114      '-padx'       => 10,
115      '-pady'       => 10,
116    );
117
118    # The "yes" button
119    my $y_b = $mw->Button (
120      '-text'      => 'Yes',
121      '-underline' => 0,
122      '-command'   => sub {$ans = 'y'; $mw->destroy},
123    )->grid (
124      '-row'    => 1,
125      '-column' => 0,
126      '-padx'   => 5,
127      '-pady'   => 5,
128    );
129
130    # The "no" button
131    my $n_b = $mw->Button (
132      '-text'      => 'No',
133      '-underline' => 0,
134      '-command'   => sub {$ans = 'n'; $mw->destroy},
135    )->grid (
136      '-row'    => 1,
137      '-column' => 1,
138      '-padx'   => 5,
139      '-pady'   => 5,
140    );
141
142    # The "all" button
143    my $a_b;
144    if ($buttons == 3) {
145      $a_b = $mw->Button (
146        '-text'      => 'All',
147        '-underline' => 0,
148        '-command'   => sub {$ans = 'a'; $mw->destroy},
149      )->grid (
150        '-row'    => 1,
151        '-column' => 2,
152        '-padx'   => 5,
153        '-pady'   => 5,
154      );
155    }
156
157    # Keyboard binding
158    if ($buttons == 3) {
159      $mw->bind ('<Key>' => sub {
160        if ($Tk::event->K eq 'Y' or $Tk::event->K eq 'y') {
161          $y_b->invoke;
162
163        } elsif ($Tk::event->K eq 'N' or $Tk::event->K eq 'n') {
164          $n_b->invoke;
165
166        } elsif ($Tk::event->K eq 'A' or $Tk::event->K eq 'a') {
167          $a_b->invoke;
168        }
169      });
170
171    } else {
172      $mw->bind ('<Key>' => sub {
173        if ($Tk::event->K eq 'Y' or $Tk::event->K eq 'y') {
174          $y_b->invoke;
175
176        } elsif ($Tk::event->K eq 'N' or $Tk::event->K eq 'n') {
177          $n_b->invoke;
178        }
179      });
180    }
181
182    # Handle the situation when the user attempts to quit the window
183    $mw->protocol ('WM_DELETE_WINDOW', sub {
184      $ans = $def if $def;
185      $mw->destroy;
186    });
187
188  } else {
189    # Create a dialog box to obtain an input string
190
191    # Message of the dialog box
192    $mw->Label ('-text' => $mesg)->grid (
193      '-row'    => 0,
194      '-column' => 0,
195      '-padx'   => 5,
196      '-pady'   => 5,
197    );
198
199    # Entry box for the user to type in the input string
200    my $entry   = $ans;
201    my $input_e = $mw->Entry (
202      '-textvariable'    => \$entry,
203      '-width'           => 40,
204    )->grid (
205      '-row'    => 0,
206      '-column' => 1,
207      '-sticky' => 'ew',
208      '-padx'   => 5,
209      '-pady'   => 5,
210    );
211
212    my $b_f = $mw->Frame->grid (
213      '-row'        => 1,
214      '-column'     => 0,
215      '-columnspan' => 2,
216      '-sticky'     => 'e',
217    );
218
219    # An OK button to accept the input string
220    my $ok_b = $b_f->Button (
221      '-text' => 'OK',
222      '-command' => sub {$ans = $entry; $mw->destroy},
223    )->grid (
224      '-row'        => 0,
225      '-column'     => 0,
226      '-padx'       => 5,
227      '-pady'       => 5,
228    );
229
230    # A Cancel button to reject the input string
231    my $cancel_b = $b_f->Button (
232      '-text' => 'Cancel',
233      '-command' => sub {$ans = undef; $mw->destroy},
234    )->grid (
235      '-row'        => 0,
236      '-column'     => 1,
237      '-padx'       => 5,
238      '-pady'       => 5,
239    );
240
241    # Keyboard binding
242    $mw->bind ('<Key>' => sub {
243      if ($Tk::event->K eq 'Return' or $Tk::event->K eq 'KP_Enter') {
244        $ok_b->invoke;
245
246      } elsif ($Tk::event->K eq 'Escape') {
247        $cancel_b->invoke;
248      }
249    });
250
251    # Allow the entry box to expand
252    $mw->gridColumnconfigure (1, '-weight' => 1);
253
254    # Set initial focus on the entry box
255    $input_e->focus;
256    $input_e->icursor ('end');
257  }
258
259  $mw->geometry ($pos);
260
261  # Switch on "always on top" property for $mw
262  $mw->property (
263    qw/set _NET_WM_STATE ATOM/,
264    32,
265    ['_NET_WM_STATE_STAYS_ON_TOP'],
266    ($mw->toplevel->wrapper)[0],
267  );
268
269  MainLoop;
270
271  # Reset flush flag
272  $| = $flush;
273
274  return $ans;
275}
276
277# ------------------------------------------------------------------------------
278
279__END__
Note: See TracBrowser for help on using the repository browser.