DialogBoxUL.pm 6.14 KB
Newer Older
1
2
3
4
#-----------------------------------------------------------------------
# Fault InJection Instrumenter (FIJI)
# https://embsys.technikum-wien.at/projects/vecs/fiji
#
5
6
7
8
# The creation of this file has been supported by the publicly funded
# R&D project Josef Ressel Center for Verification of Embedded Computing
# Systems (VECS) managed by the Christian Doppler Gesellschaft (CDG).
#
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright (C) 2017 Christian Fibich <fibich@technikum-wien.at>
# Copyright (C) 2017 Stefan Tauner <tauner@technikum-wien.at>
#
# This module is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the LICENSE file for more details.
#-----------------------------------------------------------------------
21
22
23
24
#
# DialogBox is similar to Dialog except that it allows any widget
# in the top frame. Widgets can be added with the add method. Currently
# there exists no way of deleting a widget once it has been added.
25
# DialogBoxUL adds support for a default button and keyboard accelerators.
26

27
package Tk::DialogBoxUL;
28
29
30
31
32

use strict;
use Carp;

use vars qw($VERSION);
33
$VERSION = '4.017'; # was: sprintf '4.%03d', q$Revision: #13 $ =~ /\D(\d+)\s*$/;
34
35
36

use base  qw(Tk::Toplevel);

37
Tk::Widget->Construct('DialogBoxUL');
38
39
40
41
42
43

sub Populate {
    my ($cw, $args) = @_;

    $cw->SUPER::Populate($args);
    my $buttons = delete $args->{'-buttons'};
44
45
    $buttons = ['~OK'] unless defined $buttons;
    my $bl0 = ($buttons->[0] =~ tr/~//dr);
46
    my $default_button = delete $args->{'-default_button'};
47
    $default_button = $bl0 unless defined $default_button;
48
49
    my $cancel_button = delete $args->{'-cancel_button'};
    if (!$cancel_button && @$buttons == 1) {
50
	$cancel_button = $bl0;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    }

    $cw->{'selected_button'} = '';
    $cw->transient($cw->Parent->toplevel);
    $cw->withdraw;
    if ($cancel_button) {
	$cw->protocol('WM_DELETE_WINDOW' => sub { $cw->{'cancel_button'}->invoke });
    } else {
	$cw->protocol('WM_DELETE_WINDOW' => sub { $cw->{'selected_button'} = undef });
    }
    # Make sure waitVariable exits if a waiting dialog is destroyed
    $cw->OnDestroy(sub { $cw->{'selected_button'} = $cw->{'selected_button'} });

    # create the two frames
    my $top = $cw->Component('Frame', 'top');
66
    $top->configure(-relief => 'raised', -bd => 1) unless $Tk::platform eq 'MSWin32'; # FIXME: document
67
68
69
70
71
72
    my $bot = $cw->Component('Frame', 'bottom');
    $bot->configure(-relief => 'raised', -bd => 1) unless $Tk::platform eq 'MSWin32';
    $bot->pack(qw/-side bottom -fill both -ipady 3 -ipadx 3/);
    $top->pack(qw/-side top -fill both -ipady 3 -ipadx 3 -expand 1/);

    # create a row of buttons in the bottom.
73
74
    my $bo;  # foreach my $var: perl > 5.003_08
    foreach $bo (@$buttons)
75
     {
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	my $bl = ($bo =~ tr/~//dr); # Remove all ~ characters
	my $underline = rindex($bo, '~');
	my $accel;
	if ($underline >= 0) {
	    if (substr($bo, $underline + 1, 1) !~ /[[:alpha:]]/) {
		warn "First tilde must precede an alphanumeric character.";
	    } elsif ($underline >= 0) {
		$accel = lc(substr($bo, $underline + 1, 1));
	    }
	}
	my $b = $bot->Button(
	    -text => $bl,
	    -command => sub { $cw->{'selected_button'} = "$bl" },
	    -underline => (defined($accel)) ? $underline : -1
	);
91
92
	$b->bind('<Return>' => [ $b, 'Invoke']);
	$cw->Advertise("B_$bl" => $b);
93
        if ($Tk::platform eq 'MSWin32') # FIXME: document
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
         {
          $b->configure(-width => 10, -pady => 0);
         }
	if ($bl eq $default_button) {
	    my $db = $bot->Frame(-relief => 'sunken', -bd => 1);
	    $b->raise($db);
	    $b->pack(-in => $db, -padx => '2', -pady => '2');
	    $db->pack(-side => 'left', -expand => 1, -padx => 1, -pady => 1);
	    $cw->{'default_button'} = $b;
	    $cw->bind('<Return>' => [ $b, 'Invoke']);
	} else {
	    $b->pack(-side => 'left', -expand => 1,  -padx => 1, -pady => 1);
	}
	if (defined $cancel_button && $bl eq $cancel_button) {
	    $cw->{'cancel_button'} = $b;
	}
110
111
112
	if (defined($accel)) {
	    $cw->bind("<Alt-$accel>" => [ $b, 'Invoke']);
	}
113
	push(@{$cw->{'buttons'}}, $bl);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    }
    if (defined $default_button && !$cw->{'default_button'}) {
	warn "Default button `$default_button' does not exist.\n";
    }
    if (defined $cancel_button && !$cw->{'cancel_button'}) {
	warn "Cancel button `$cancel_button' does not exist.\n";
    }
    $cw->ConfigSpecs(-command    => ['CALLBACK', undef, undef, undef ],
                     -foreground => ['DESCENDANTS', 'foreground','Foreground', 'black'],
                     -background => ['DESCENDANTS', 'background','Background',  undef],
		     -focus	 => ['PASSIVE', undef, undef, undef],
		     -showcommand => ['CALLBACK', undef, undef, undef],
                    );
    $cw->Delegates('Construct',$top);
}

130
131
132
133
sub buttons {
    return \@{shift->{'buttons'}};
}

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
sub add {
    my ($cw, $wnam, @args) = @_;
    my $w = $cw->Subwidget('top')->$wnam(@args);
    $cw->Advertise("\L$wnam" => $w);
    return $w;
}

sub Wait
{
 my $cw = shift;
 $cw->Callback(-showcommand => $cw);
 $cw->waitVariable(\$cw->{'selected_button'});
 $cw->grabRelease if Tk::Exists($cw);
 $cw->withdraw if Tk::Exists($cw);
 $cw->Callback(-command => $cw->{'selected_button'});
}

sub Show {

153
    croak 'DialogBoxUL: "Show" method requires at least 1 argument'
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
	if scalar @_ < 1;
    my $cw = shift;
    my ($grab) = @_;
    my $old_focus = $cw->focusSave;
    my $old_grab = $cw->grabSave;

    shift if defined $grab && length $grab && ($grab =~ /global/);
    $cw->Popup(@_);

    Tk::catch {
    if (defined $grab && length $grab && ($grab =~ /global/)) {
	$cw->grabGlobal;
    } else {
	$cw->grab;
    }
    };
    if (my $focusw = $cw->cget(-focus)) {
	$focusw->focus;
    } elsif (defined $cw->{'default_button'}) {
	$cw->{'default_button'}->focus;
    } else {
	$cw->focus;
    }
    $cw->Wait;
    &$old_focus;
    &$old_grab;
    return $cw->{'selected_button'};
}

sub SelectButton
{
 my ($w, $button_text) = @_;
 $w->{'selected_button'} = $button_text;
}

sub Exit
{
 my $cw = shift;
192
 #kill the DialogBoxUL, by faking a 'DONE'
193
194
195
196
 $cw->{'selected_button'} = $cw->{'default_button'}->cget(-text);
}

1;