FIJIModalDialog.pm 6.65 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
# Authors:
# Christian Fibich <fibich@technikum-wien.at>
# Stefan Tauner <tauner@technikum-wien.at>
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
# 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.
#-----------------------------------------------------------------------

## @file
# @brief Contains class \ref Tk::FIJIModalDialog

## @class Tk::FIJIModalDialog
# @brief Tk Widget showing a modal dialog window based on Tk::DialogBoxUL

29
30
package Tk::FIJIModalDialog;

31
32
33
use strict;
use warnings;

34
35
use Log::Log4perl qw(get_logger);
use Tk;
36
use Tk::widgets qw(LabFrame Label Entry Button DialogBoxUL);
37
38
39
use Tk::PNG;
use File::Spec;

40
use base qw(Tk::DialogBoxUL);
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Construct Tk::Widget 'FIJIModalDialog';

sub ClassInit {
    my ($class, $mw) = @_;
    $class->SUPER::ClassInit($mw);

    my $self = bless {}, $class;
    return $self;
}


sub Populate {
    my $logger = get_logger("");
    my ($self, $args) = @_;
    my $image           = delete $args->{'-image'};
    my $text            = delete $args->{'-text'};
Stefan Tauner's avatar
Stefan Tauner committed
57
    my $markuptext      = delete $args->{'-markuptext'};
58
    my $scrollable      = delete $args->{'-scrollable'};
59
    my $wraplength      = delete $args->{'-wraplength'};
60
    my $resizeable_y    = delete $args->{'-resizeable_y'};
61
62
63

    $self->SUPER::Populate($args);

64
    my $buttons         = $self->buttons();
65
    $scrollable = 1 if !defined($scrollable);
66
67
    # Enable resizability if not overridden by user for scrollable dialogs
    $resizeable_y = 1 if !defined($resizeable_y) && $scrollable;
68

69
    Tk::FIJIUtils::set_icon($self);
Stefan Tauner's avatar
Stefan Tauner committed
70

71
    my $esc_button;
72
73
    if (@$buttons == 1) {
        $esc_button = @$buttons[0];
74
    } else {
75
        foreach my $b (@$buttons) {
76
77
78
79
80
81
            if ($b eq "Cancel") {
                $esc_button = $b;
                last;
            }
        }
    }
82
    if (defined($esc_button)) {
83
        $logger->trace("Binding escape to $esc_button");
84
85
        $self->bind('<Escape>' => [$self->Subwidget("B_$esc_button"), 'Invoke']);
    }
86

Stefan Tauner's avatar
Stefan Tauner committed
87
88
    $self->add('Label', -image => $image)->pack(-side => 'left') if (defined $image);

89
90
91
92
93
94
95
96
97
98
99
100
101
102
    my $msg_parent;
    if ($scrollable) {
        $msg_parent = $self->Scrolled('Pane',
            '-scrollbars' => 'oe',
            '-sticky' => 'nwse',
            );
        Tk::FIJIUtils::bind_mousewheel($self, $msg_parent);
        $msg_parent->pack(
            '-expand' => 1,
            '-fill'   => 'both',
            '-anchor' => 'nw',
            '-side'   => 'top',
        );

103
        $self->resizable(0, $resizeable_y);
104
105
    } else {
        $msg_parent = $self;
106
        $self->resizable(0, $resizeable_y);
107
    }
Stefan Tauner's avatar
Stefan Tauner committed
108
109
110
    if (defined $markuptext) {
        # For markup txt we use Tk::Text.
        # This allows us to set custom fonts and even add an URL handler
111
        my $t = $msg_parent->Text(
Stefan Tauner's avatar
Stefan Tauner committed
112
113
114
115
116
117
            -wrap => 'word',
            # relief = flat would be ideal but there is a bug in Perl::Tk
            # on Linux printing that draws a light line left of the widget.
            -relief => 'groove',
            -borderwidth => 2,
            -padx => 15,
118
            -pady => 15,
Stefan Tauner's avatar
Stefan Tauner committed
119
120
        )->pack(
            -side => 'right',
121
            -fill => 'both',
122
            -expand => 1,
Stefan Tauner's avatar
Stefan Tauner committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        );
        my $tag = "tag000";
        foreach ($markuptext) {
            chomp;
            my (@items) = split (/(https?:\S+)/,$_);

            foreach (@items) {
                $t->tagConfigure($tag, -font => "Verdana 13");
                $t->insert( 'end', $_, $tag );
                if (/(https?:\S+)/) {
                    $t->tagConfigure( $tag, -foreground => 'blue' );
                    $t->tagBind(
                        $tag,
                        '<Any-Enter>' => [ \&manipulate_link, $tag, 'raised', 'hand2' ],
                    );
                    $t->tagBind(
                        $tag,
                        '<Any-Leave>' => [ \&manipulate_link, $tag, 'flat', 'xterm' ] );
                    $t->tagBind(
                        $tag,
                        '<Button-1>' => [ \&manipulate_link, $tag, 'sunken' ]
                    );
                    $t->tagBind(
                        $tag,
                        '<ButtonRelease-1>' => [ \&manipulate_link, $tag, 'raised', undef, sub { Tk::FIJIUtils::cross_platform_open($self, $_[1]); } ]
                    );
                }
                $tag++;
            }
            $t->insert( 'end', "\n" );
        }
        $t->configure(-state => 'disabled');
    } elsif (defined $text) {
156
        my $lbl = $msg_parent->Label(
157
158
159
160
161
162
163
            -text => $text,
            -justify => 'left',
        )->pack(
            -side => 'right',
            -fill => 'both',
            -expand => 1
        );
Stefan Tauner's avatar
Stefan Tauner committed
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
        $lbl->configure(-wraplength => $wraplength) if (defined $wraplength);
    } else {
        $logger->debug("Neither text nor markuptext given for modal dialog");
    }
}


sub manipulate_link {

    # manipulate the link as you press the mouse key

    my ($a)      = shift;
    my ($tag)    = shift;
    my ($relief) = shift;
    my ($cursor) = shift;
    my ($after)  = shift;

    # by configuring the relief (to simulate a button press)
    $a->tagConfigure( $tag, -relief => $relief, -borderwidth => 1 );

    # by changing the cursor between hand and xterm
    $a->configure( -cursor => $cursor ) if ($cursor);

    # and by scheduling the specified action to run "soon"
    if ($after) {
        my ($s) = $a->get( $a->tagRanges($tag) );
        $a->after( 50, [ $after, $a, $s, $tag, @_ ] ) if ($after);
    }
192
193
194
195
}

sub Show {
    my ($self) = @_;
196
197
198
199
200
201
202
203
204
205
    my $parent = $self->parent;
    my $old_action;
    if (defined($parent)) {
        # first set action to perform when the main window is closed to be ignored
        $old_action = $parent->protocol('WM_DELETE_WINDOW');
        if (defined($old_action)) {
            $parent->protocol('WM_DELETE_WINDOW' => sub {
                return 1;
            });
        }
206
    }
207
    # then show the dialog
208
    my $rv = $self->SUPER::Show();
209
    # and finally reset the original action to be performed when the main window is closed
210
211
    if (defined($old_action)) {
        $parent->protocol('WM_DELETE_WINDOW' => $old_action);
212
213
214
    }
    return $rv;
}