FIJIUtils.pm 13.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#-----------------------------------------------------------------------
# Fault InJection Instrumenter (FIJI)
# https://embsys.technikum-wien.at/projects/vecs/fiji
#
# 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.
#-----------------------------------------------------------------------

## @file FIJIUtils.pm
# @brief Contains class \ref Tk::FIJIUtils

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

24
25
package Tk::FIJIUtils;

Stefan Tauner's avatar
Stefan Tauner committed
26
27
28
use strict;
use warnings;

29
use Scalar::Util 'blessed';
Stefan Tauner's avatar
Stefan Tauner committed
30
use Tk::Photo;
31
use Tk::FIJIModalDialog;
Stefan Tauner's avatar
Stefan Tauner committed
32
33
use Log::Log4perl qw(get_logger);

34
use FIJI qw(:fiji_version :fiji_dir :fiji_documentation_path :fiji_media_path);
Stefan Tauner's avatar
Stefan Tauner committed
35
my $about_file = File::Spec->catdir(FIJI_MEDIA_PATH, "about.txt");
Stefan Tauner's avatar
Stefan Tauner committed
36
my $about_text;
Stefan Tauner's avatar
Stefan Tauner committed
37
use constant SCROLL => 5;
Stefan Tauner's avatar
Stefan Tauner committed
38
39
40
41
42
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

# 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
70
        $logo_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'fiji_logo_hires.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
71
72
73
74
    }
    return $logo_image;
}

Christian Fibich's avatar
Christian Fibich committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
94
95
96
97
sub delete_image {
    my $delete_image;
    if (!defined($delete_image)) {
        my $mw = shift;
Stefan Tauner's avatar
Stefan Tauner committed
98
        $delete_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'delete_48x48.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
99
100
101
102
103
104
105
106
    }
    return $delete_image;
}

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

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

Stefan Tauner's avatar
Stefan Tauner committed
121
122
123
124
125
126
127
128
129
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;
}

130
131
132
133
134
135
136
137
138
139
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
140
141
142
143
sub save_image {
    my $save_image;
    if (!defined($save_image)) {
        my $mw = shift;
Stefan Tauner's avatar
Stefan Tauner committed
144
        $save_image = $mw->Photo(-file => File::Spec->catfile(FIJI_MEDIA_PATH, 'save_24x24.xpm'),  -format => 'XPM');
Stefan Tauner's avatar
Stefan Tauner committed
145
146
147
148
149
150
151
152
    }
    return $save_image;
}

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

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);
165
166
    } elsif ($^O eq "cygwin") {
        $ret = FIJI::Utils::system("cygstart " . $path);
167
168
169
170
171
    } 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
172
        my $d = $parent->FIJIModalDialog(-image     => error_image($parent),
173
                                         -text      => $ret,
174
                                         -title     => "Could not open $path",);
175
176
177
178
179
180
        $d->Show();
    }
}

sub show_documentation {
    my $parent = shift;
181
182
183
184
185
186
187
188
    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);
    }
189
}
190

Stefan Tauner's avatar
Stefan Tauner committed
191
192
193
sub show_about {
    my $logger = get_logger("");
    my $parent = shift;
Christian Fibich's avatar
Christian Fibich committed
194

Stefan Tauner's avatar
Stefan Tauner committed
195
196
197
198
199
200
201
202
    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>;
203
            $about_text =~ s/\$FIJI_VERSION\$/${\FIJI_VERSION}/g;
Stefan Tauner's avatar
Stefan Tauner committed
204
205
206
        }
        close($about_fh);
    }
207

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

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

Stefan Tauner's avatar
Stefan Tauner committed
222
223
224
sub set_icon ($) {
    my $mw = shift;
    $mw->iconimage(logo_image_icon($mw));
225
226
}

227
228
229
230
231
232
sub bind_mousewheel {
    my ($mw, $widget) = @_;

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

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

237
238
239
240
241
242
243
244
245
246
247
248
249
250
    # 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;
    }

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

}

sub _wheel_handler {

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

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    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
282
                $scrolled->yview("scroll",(-$val/120)*SCROLL,'units');
283
            } else {
Christian Fibich's avatar
Christian Fibich committed
284
                $scrolled->yview("scroll",$val*SCROLL,'units');
285
286
            }
            goto END;
287
        }
288
    }
289
290
END:
    return;
291
292
}

293
294
295
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
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)) {
354
            $logger->debug("Could not find previous state of $widget!");
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
        } 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);
    }
}

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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);
    }
}

390
391
392
393
394
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
# 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]]);
}

422
423
424
sub show_warning {
    my ($widget, $msg) = @_;
    get_logger("")->warn($msg);
425
    my $dialog = $widget->MainWindow->FIJIModalDialog(
426
427
428
429
430
        -image     => Tk::FIJIUtils::alert_image($widget->MainWindow),
        -wraplength => $widget->MainWindow->screenwidth,
        -text      => "$msg",
        -title     => "Warning",
    );
431
432
433
434
435
436
437
438
439
440
441
442
443
    $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();
444
445
}

446
1;