FIJIUtils.pm 13.9 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 FIJIUtils.pm
# @brief Contains class \ref Tk::FIJIUtils

## @class Tk::FIJIUtils
# @brief Various helper functions for FIJI related to GUI handling

29
30
package Tk::FIJIUtils;

Stefan Tauner's avatar
Stefan Tauner committed
31
32
33
use strict;
use warnings;

34
use Scalar::Util 'blessed';
Stefan Tauner's avatar
Stefan Tauner committed
35
use Tk::Photo;
36
use Tk::FIJIModalDialog;
Stefan Tauner's avatar
Stefan Tauner committed
37
38
use Log::Log4perl qw(get_logger);

39
use FIJI qw(:fiji_version :fiji_dir :fiji_documentation_path :fiji_media_path);
Stefan Tauner's avatar
Stefan Tauner committed
40
my $about_file = File::Spec->catdir(FIJI_MEDIA_PATH, "about.txt");
Stefan Tauner's avatar
Stefan Tauner committed
41
my $about_text;
Stefan Tauner's avatar
Stefan Tauner committed
42
use constant SCROLL => 5;
Stefan Tauner's avatar
Stefan Tauner committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

# The next few functions abstract some commonly used image files.
# Due to some bug^Wfeature in the Perl implementation we cannot re-use
# the bitmaps and thus recreate them on every call.
# FIXME: we also would need to free their memory after they have been
# used with ->delete. However this would complicate matters even more.

sub logo_image_icon {
    my $logo_image_icon;
    my $mw = shift;
    if ($^O eq "MSWin32") {
        my $icon_base = File::Spec->rel2abs('fiji_logo', FIJI_MEDIA_PATH);
        $logo_image_icon = $mw->Photo(-file => "$icon_base.png", -format => 'PNG');
    } else {
        my $icon_base = File::Spec->rel2abs('fiji_logo_hires', FIJI_MEDIA_PATH);

        # Workaround for bug in Perl::Tk as seen on Fedora 20 and 21 (and other distributions):
        # Use XPM instead of PNG on Non-Windows:
        # $logo_image_icon = $mw->Photo(-file => "$icon_base.png", -format => 'PNG');
        $logo_image_icon = $mw->Photo(-file => "$icon_base.xpm", -format => 'XPM');

        # The .xbm is needed for transparency, generate with e.g. GIMP
        my $iconmask_path = "$icon_base-mask.xbm";
        $mw->iconmask("\@$iconmask_path") if -r $iconmask_path;
    }
    return $logo_image_icon;
}

sub logo_image {
    my $logo_image;
    if (!defined($logo_image)) {
        my $mw = shift;
Stefan Tauner's avatar
Stefan Tauner committed
75
        $logo_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'fiji_logo_hires.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
76
77
78
79
    }
    return $logo_image;
}

Christian Fibich's avatar
Christian Fibich committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
sub up_image {
    my $up_image;
    if (!defined($up_image)) {
        my $mw = shift;
        $up_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'up_24x24.xpm'),  -format => 'XPM');
    }
    return $up_image;
}


sub down_image {
    my $down_image;
    if (!defined($down_image)) {
        my $mw = shift;
        $down_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'down_24x24.xpm'),  -format => 'XPM');
    }
    return $down_image;
}

Stefan Tauner's avatar
Stefan Tauner committed
99
100
101
102
sub delete_image {
    my $delete_image;
    if (!defined($delete_image)) {
        my $mw = shift;
Stefan Tauner's avatar
Stefan Tauner committed
103
        $delete_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'delete_48x48.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
104
105
106
107
108
109
110
111
    }
    return $delete_image;
}

sub error_image {
    my $error_image;
    if (!defined($error_image)) {
        my $mw = shift;
Stefan Tauner's avatar
Stefan Tauner committed
112
        $error_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'stop_48x48.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
113
114
115
116
117
118
119
120
    }
    return $error_image;
}

sub alert_image {
    my $alert_image;
    if (!defined($alert_image)) {
        my $mw = shift;
Stefan Tauner's avatar
Stefan Tauner committed
121
        $alert_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'alert_48x48.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
122
123
124
125
    }
    return $alert_image;
}

Stefan Tauner's avatar
Stefan Tauner committed
126
127
128
129
130
131
132
133
134
sub info_image {
    my $info_image;
    if (!defined($info_image)) {
        my $mw = shift;
        $info_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'info_48x48.xpm'),  -format => 'XPM');
    }
    return $info_image;
}

135
136
137
138
139
140
141
142
143
144
sub rename_image {
    my $rename_image;
    if (!defined($rename_image)) {
        my $mw = shift;
        $rename_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'label_edit_48x48.xpm'),  -format => 'XPM');
    }
    return $rename_image;
}


Stefan Tauner's avatar
Stefan Tauner committed
145
146
147
148
sub save_image {
    my $save_image;
    if (!defined($save_image)) {
        my $mw = shift;
Stefan Tauner's avatar
Stefan Tauner committed
149
        $save_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'save_24x24.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
150
151
152
153
154
155
156
157
    }
    return $save_image;
}

sub dummy_image {
    my $dummy_image;
    if (!defined($dummy_image)) {
        my $mw = shift;
Stefan Tauner's avatar
Stefan Tauner committed
158
        $dummy_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'dummy_24x24.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
159
160
161
    }
    return $dummy_image;
}
162
163
164
165
166
167
168
169

sub cross_platform_open {
    my ($parent, $path) = @_;
    my $ret;
    if ($^O eq "MSWin32") {
        $ret = FIJI::Utils::system(1, "start " . $path);
    } elsif ($^O eq "darwin") { # OSX
        $ret = FIJI::Utils::system("open " . $path);
170
171
    } elsif ($^O eq "cygwin") {
        $ret = FIJI::Utils::system("cygstart " . $path);
172
173
174
175
176
    } else {
        # fall back to xdg-open that works on most(?) unices
        $ret = FIJI::Utils::system("xdg-open " . $path);
    }
    if (defined($ret)) {
Stefan Tauner's avatar
Stefan Tauner committed
177
        my $d = $parent->FIJIModalDialog(-image     => error_image($parent),
178
                                         -text      => $ret,
179
                                         -title     => "Could not open $path",);
180
181
182
183
184
185
        $d->Show();
    }
}

sub show_documentation {
    my $parent = shift;
186
187
188
189
190
191
192
193
    if (!defined FIJI_DOCUMENTATION_PATH) {
        my $d = $parent->FIJIModalDialog(-image     => error_image($parent),
                                         -text      => "FIJI Documentation missing",
                                         -title     => "Could not open documentation",);
        $d->Show();
    } else {
        return cross_platform_open($parent, FIJI_DOCUMENTATION_PATH);
    }
194
}
195

Stefan Tauner's avatar
Stefan Tauner committed
196
197
198
sub show_about {
    my $logger = get_logger("");
    my $parent = shift;
Christian Fibich's avatar
Christian Fibich committed
199

Stefan Tauner's avatar
Stefan Tauner committed
200
201
202
203
204
205
206
207
    if (!defined($about_text)) {
        my $about_fh;
        if (!defined(open($about_fh, '<', $about_file))) {
                $logger->error("Cannot open file $about_file");
                return 1;
        }{
            local $/;
            $about_text = <$about_fh>;
208
            $about_text =~ s/\$FIJI_VERSION\$/${\FIJI_VERSION}/g;
Stefan Tauner's avatar
Stefan Tauner committed
209
210
211
        }
        close($about_fh);
    }
212

Stefan Tauner's avatar
Stefan Tauner committed
213
214
215
    my $logo_image = logo_image($parent);
    my $d = $parent->FIJIModalDialog(
        -title   => "About",
216
        -buttons => ["~Close"],
Stefan Tauner's avatar
Stefan Tauner committed
217
218
        -image  => $logo_image,
        -markuptext => $about_text,
219
        -scrollable => 0,
Stefan Tauner's avatar
Stefan Tauner committed
220
221
222
223
224
225
    );

    $d->geometry("950x".($logo_image->height()+55)."+0+0");
    $d->minsize(950, $logo_image->height()+55);
    $d->Show();
}
226

Stefan Tauner's avatar
Stefan Tauner committed
227
228
229
sub set_icon ($) {
    my $mw = shift;
    $mw->iconimage(logo_image_icon($mw));
230
231
}

232
233
234
235
236
237
sub bind_mousewheel {
    my ($mw, $widget) = @_;

    eval { $widget->Subwidget('scrolled') };

    if ($@ || !blessed($widget) || !$widget->isa("Tk::Widget")) {
238
239
        my $logger = get_logger("");
        $logger->error("Could not bind mouse wheel to wrong widget (".$widget->class().")\n($@)");
240
241
    }

242
243
244
245
246
247
248
249
250
251
252
253
254
255
    # add to the list of scrollable widgets in the main window
    if (!defined $mw->{'scrollable_widgets'}) {
        $mw->{'scrollable_widgets'} = [$widget];
    } else {
        my $in_list = 0;
        for my $scrollable_widget (@{$mw->{'scrollable_widgets'}}) {
            if ($scrollable_widget == $widget) {
                $in_list = 1;
                last;
            }
        }
        push @{$mw->{'scrollable_widgets'}}, $widget unless $in_list;
    }

256
257
258
    # bind the mouse wheel
    if ($^O eq "MSWin32") {
        # Windows passes the 'delta' itself
Christian Fibich's avatar
Christian Fibich committed
259
        $mw->bind('<MouseWheel>' => [\&_wheel_handler, $mw, Tk::Ev('D')]);
260
    } else {
Christian Fibich's avatar
Christian Fibich committed
261
262
        $mw->bind('<4>' => [\&_wheel_handler, $mw, -1]);
        $mw->bind('<5>' => [\&_wheel_handler, $mw, 1]);
263
264
265
266
267
268
    }

}

sub _wheel_handler {

Christian Fibich's avatar
Christian Fibich committed
269
    my ($event_widget, $mw, $val) = @_;
270

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    my $container;

    # search through hierarchy of widget causing the event
    # for the nearest ancestor in the list of widgets to be scrolled
    for ($container = $event_widget; defined $container; $container = $container->parent) {
        # search the list of the widgets to be scrolled
        for (my $index = 0; $index < @{$mw->{'scrollable_widgets'}}; $index++) {
            my $widget_to_scroll = @{$mw->{'scrollable_widgets'}}[$index];
            # remove already deleted widgets from list
            unless (Tk::Exists($widget_to_scroll)) {
                delete @{$mw->{'scrollable_widgets'}}[$index];
                next;
            }
            next if $container != @{$mw->{'scrollable_widgets'}}[$index];
            my $scrolled = $widget_to_scroll->Subwidget('scrolled');
            if ($^O eq "MSWin32") {
Christian Fibich's avatar
Christian Fibich committed
287
                $scrolled->yview("scroll",(-$val/120)*SCROLL,'units');
288
            } else {
Christian Fibich's avatar
Christian Fibich committed
289
                $scrolled->yview("scroll",$val*SCROLL,'units');
290
291
            }
            goto END;
292
        }
293
    }
294
295
END:
    return;
296
297
}

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
sub _getstate_all_widgets_rec {
    my $logger = get_logger("");
    my ($widget, $state_ref) = @_;

    if (grep { '-state' eq $_->[0] } $widget->configure()) {
        my $old_state = $widget->cget('-state');
        $state_ref->{$widget} = $old_state;
    } elsif ($widget->isa("Tk::NoteBook")) {
        # NoteBook is special (of course there is no generic solution in Perl Tk)...
        foreach my $page ($widget->pages()) {
            my $page_state = $widget->pagecget($page, '-state');
            $state_ref->{$widget . $page} = $page_state;
        }
    }
    foreach my $child ($widget->children()) {
        _getstate_all_widgets_rec($child, $state_ref);
    }
}

sub _disable_all_widgets_rec {
    my $logger = get_logger("");
    my ($widget, $state_ref) = @_;

    # The logging needs to continue even with all other widgets disabled.
    # To that end we need to make sure that the ROText but also its master
    # frame don't get disabled.
    if ($widget->isa("Tk::Frame")) {
        my $subwidget = $widget->Subwidget('scrolled');
        if (defined($subwidget) && $subwidget->isa("Tk::ROText")) {
            return;
        }
    }

    if (grep { '-state' eq $_->[0] } $widget->configure()) {
        $widget->configure('-state', "disabled");
    } elsif ($widget->isa("Tk::NoteBook")) {
        # Tk::NoteBook is special (of course there is no generic solution in Perl Tk)...
        foreach my $page ($widget->pages()) {
            $widget->pageconfigure($page, '-state' => "disabled");
        }
    }
    foreach my $child ($widget->children()) {
        _disable_all_widgets_rec($child, $state_ref);
    }
}

sub disable_all_widgets {
    my $logger = get_logger("");
    my ($widget, $state_ref) = @_;

    _getstate_all_widgets_rec($widget, $state_ref);
    _disable_all_widgets_rec($widget, $state_ref);
}

sub reenable_all_widgets {
    my $logger = get_logger("");
    my ($widget, $prev_state_ref) = @_;

    if (grep { '-state' eq $_->[0] } $widget->configure()) {
        my $prev_state = $prev_state_ref->{$widget};
        if (!defined($prev_state)) {
359
            $logger->debug("Could not find previous state of $widget!");
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
        } else {
            $widget->configure('-state', $prev_state);
        }
    } else {
        if ($widget->isa("Tk::NoteBook")) {
            foreach my $page ($widget->pages()) {
                my $prev_state = $prev_state_ref->{$widget . $page};
                $widget->pageconfigure($page, '-state', $prev_state);
            }
        }
    }
    foreach my $child ($widget->children()) {
        reenable_all_widgets($child, $prev_state_ref);
    }
}

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
sub highlight_widget ($$$) {
    my ($widget, $enable, $widget_background) = @_;

    my $state = $widget->cget('-state');
    my $parameter = ($state eq 'normal') ? '-bg' : '-disabledbackground';
    if ($enable) {
        $widget->configure($parameter => 'orange red');
    } else {

        # the 3rd element returned for '-bg' is the default background, usually.
        # Apparently it is something darker on Linux so the following does
        # not work as intended. :(
        # $widget->configure('-bg' => ($widget->configure('-bg'))[3]);
        # Work around: store the entry background color at creation time and
        # use that instead.
        $widget->configure($parameter => $widget_background);
    }
}

395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# Sets up the desired bindings for Entry widgets
# that differ from default behavior
sub entry_rebind($) {
    my $entry = shift;

    # select all
    $entry->bind(
        '<Control-a>',
        sub {
            my $w = shift;
            $w->selectionRange(0, 'end');
            $w->icursor('end');
            $w->break;
        },
    );

    # overwrite selection when pasting
    $entry->bind(
        '<<Paste>>',
        sub {
                my ($w, $d) = @_;
                $w->deleteSelected;
                $w->insert("insert", $w->clipboardGet);
                $w->SeeInsert if $w->can('SeeInsert');
                $w->break;
            },
        );

    my @tags = $entry->bindtags();
    $entry->bindtags([@tags[1, 0, 2, 3]]);
}

427
428
429
sub show_warning {
    my ($widget, $msg) = @_;
    get_logger("")->warn($msg);
430
    my $dialog = $widget->MainWindow->FIJIModalDialog(
431
432
433
434
435
        -image     => Tk::FIJIUtils::alert_image($widget->MainWindow),
        -wraplength => $widget->MainWindow->screenwidth,
        -text      => "$msg",
        -title     => "Warning",
    );
436
437
438
439
440
441
442
443
444
445
446
447
448
    $dialog->Show();
}

sub show_error {
    my ($widget, $msg) = @_;
    get_logger("")->error($msg);
    my $dialog = $widget->MainWindow->FIJIModalDialog(
        -image     => Tk::FIJIUtils::error_image($widget->MainWindow),
        -wraplength => $widget->MainWindow->screenwidth,
        -text      => "$msg",
        -title     => "Error",
    );
    $dialog->Show();
449
450
}

451
1;