FIJIModalDialog.pm 4.69 KB
Newer Older
1
2
package Tk::FIJIModalDialog;

3
4
5
use strict;
use warnings;

6
7
use Log::Log4perl qw(get_logger);
use Tk;
8
use Tk::widgets qw(LabFrame Label Entry Button DialogBoxUL);
9
10
11
use Tk::PNG;
use File::Spec;

12
use base qw(Tk::DialogBoxUL);
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
29
    my $markuptext      = delete $args->{'-markuptext'};
30
31
32
33
    my $wraplength      = delete $args->{'-wraplength'};

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

34
35
    my $buttons         = $self->buttons();

36
    Tk::FIJIUtils::set_icon($self);
Stefan Tauner's avatar
Stefan Tauner committed
37

38
    my $esc_button;
39
40
    if (@$buttons == 1) {
        $esc_button = @$buttons[0];
41
    } else {
42
        foreach my $b (@$buttons) {
43
44
45
46
47
48
            if ($b eq "Cancel") {
                $esc_button = $b;
                last;
            }
        }
    }
49
    if (defined($esc_button)) {
50
        $logger->trace("Binding escape to $esc_button");
51
52
        $self->bind('<Escape>' => [$self->Subwidget("B_$esc_button"), 'Invoke']);
    }
53

Stefan Tauner's avatar
Stefan Tauner committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    $self->add('Label', -image => $image)->pack(-side => 'left') if (defined $image);

    if (defined $markuptext) {
        # For markup txt we use Tk::Text.
        # This allows us to set custom fonts and even add an URL handler
        my $t = $self->add('Text',
            -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,
            -pady => 15
        )->pack(
            -side => 'right',
69
            -fill => 'both',
Stefan Tauner's avatar
Stefan Tauner committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
            -expand => 1
        );
        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) {
        my $lbl = $self->add('Label', -text  => $text, -justify => 'left', )->pack(-side => 'right');
        $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);
    }
133
134
135
136
}

sub Show {
    my ($self) = @_;
137
138
139
140
141
142
143
144
145
146
    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;
            });
        }
147
    }
148
    # then show the dialog
149
    my $rv = $self->SUPER::Show();
150
    # and finally reset the original action to be performed when the main window is closed
151
152
    if (defined($old_action)) {
        $parent->protocol('WM_DELETE_WINDOW' => $old_action);
153
154
155
    }
    return $rv;
}