Wrangler/Wx/Dialog/ShortcutCollector.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 123 CC 14 SCAR 39.3
CC
14
Cov
0%
Calculate
90.0
new
42.0
BEGIN
2.0
CRAP 51.1
1 package Wrangler::Wx::Dialog::ShortcutCollector;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Wx::Dialog';
7 use Wx qw(:id :misc :sizer :dialog :keycode);
8 use Wx::Event qw(EVT_TEXT EVT_BUTTON EVT_TOGGLEBUTTON EVT_CHAR);
9 use Encode;
10  
11 sub new {
12 my $class = shift;
13 my $parent = shift;
14 my $ref = shift;
15  
16 # Set up the dialog
17 my $self = $class->SUPER::new($parent, -1, "Define a Shortcut", wxDefaultPosition, [-1,170], wxDEFAULT_DIALOG_STYLE);
18  
19 ## keyboard buttons
20 $self->{shift} = Wx::ToggleButton->new($self, -1, 'SHIFT' );
21 $self->{ctrl} = Wx::ToggleButton->new($self, -1, 'CTRL' );
22 $self->{alt} = Wx::ToggleButton->new($self, -1, 'ALT' );
23 $self->{key} = Wx::TextCtrl->new($self, -1, '', wxDefaultPosition, wxDefaultSize);
24 # $self->{key}->SetMaxLength(1);
25  
26 my $topsizer = Wx::FlexGridSizer->new(2, 4, 1, 1); # rows, cols, vgap, hgap
27 $topsizer->Add($self->{shift}, 0, wxALL, 5);
28 $topsizer->Add(Wx::Panel->new($self), 0, wxALL, 5);
29 $topsizer->Add(Wx::Panel->new($self), 0, wxALL, 5);
30 $topsizer->Add(Wx::Panel->new($self), 0, wxALL, 5);
31 $topsizer->Add($self->{ctrl}, 0, wxALL, 5);
32 $topsizer->Add($self->{alt}, 0, wxALL, 5);
33 $topsizer->Add(Wx::StaticText->new($self, -1, '+', wxDefaultPosition, wxDefaultSize), 0, wxALL, 5);
34 $topsizer->Add($self->{key}, 0, wxALL, 5);
35  
36 # a results display
37 $self->{result} = Wx::TextCtrl->new($self, -1, '', wxDefaultPosition, wxDefaultSize);
38 $self->{result}->SetEditable(0);
39 $self->{result}->SetBackgroundColour( Wx::Colour->new( 222, 222, 222 ) );
40  
41 ## control buttons
42 # my $capture = Wx::ToggleButton->new($self, -1, 'Capture');
43  
44 my $button_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
45 # $button_sizer->Add($capture, 0, wxALL, 2);
46 $button_sizer->Add(Wx::Button->new($self, wxID_OK, 'OK'), 0, wxALL, 2);
47 $button_sizer->Add(Wx::Button->new($self, wxID_CANCEL, 'Cancel'), 0, wxALL, 2);
48  
49 my $sizer = Wx::BoxSizer->new(wxVERTICAL);
50 $sizer->Add($topsizer, 0, wxALL|wxGROW, 5);
51 $sizer->Add($self->{result}, 0, wxALL|wxGROW, 5);
52 $sizer->Add($button_sizer, 0, wxALL|wxALIGN_RIGHT, 5);
53  
54 $self->SetSizer($sizer);
55 $self->Layout();
56 $self->Centre();
57 # $capture->SetFocus(); # so this modal dialog starts catching the keyboard; compare OnChar, as events are based on this $button
58  
59 EVT_TOGGLEBUTTON($self, $self->{shift}, sub { Wrangler::debug("ShortcutCollector: SHIFT: @_"); $self->{keycodes}->[0] = $self->{keycodes}->[0] ? undef : 3; $self->Calculate(); });
60 EVT_TOGGLEBUTTON($self, $self->{ctrl}, sub {Wrangler::debug("ShortcutCollector: CTRL: @_"); $self->{keycodes}->[1] = $self->{keycodes}->[1] ? undef : 2; $self->Calculate(); });
61 EVT_TOGGLEBUTTON($self, $self->{alt}, sub { Wrangler::debug("ShortcutCollector: ALT: @_"); $self->{keycodes}->[2] = $self->{keycodes}->[2] ? undef : 1; $self->Calculate(); });
62 EVT_TEXT($self, $self->{key}, sub {
63 $_[0]->{key}->ChangeValue(substr($_[0]->{key}->GetValue(),1,1)) if length($_[0]->{key}->GetValue()) > 1;
64 if(my $content = $_[0]->{key}->GetValue()){
65 $_[0]->{key}->ChangeValue(uc($content));
66 }
67 $self->{keycodes}->[3] = $_[0]->{last_keycode};
68 $self->{'keys'}->[3] = $_[0]->{key}->GetValue();
69 $self->Calculate();
70 });
71 # EVT_TOGGLEBUTTON($self, $capture, sub { print "OnCapture: @_ \n"; $_[0]->{capturing} = $_[0]->{capturing} ? undef : 1; });
72 # EVT_CHAR($capture, \&OnChar );
73 EVT_CHAR($self->{key}, sub { Wrangler::debug("ShortcutCollector: TextCtrl OnChar"); $_[0]->GetParent()->{last_keycode} = $_[1]->GetKeyCode(); $_[1]->Skip(1); });
74  
75 return $self;
76 }
77  
78 # sub OnChar {
79 # my $button = shift; # shift is $button, because we use the "capture" button for tapping into the onchar events
80 # my $dialog = $button->GetParent();
81 # my $event = shift;
82 #
83 # print "OnChar: @_ \n";
84 #
85 # if($dialog->{capturing}){
86 # my $keycode = $event->GetKeyCode();
87 # print " capturing: $keycode \n";
88 # $event->Skip(0);
89 # return;
90 # }
91 #
92 # $event->Skip(1);
93 # };
94  
95 sub Calculate {
96 my $dialog = shift;
97  
98 # simple mapping instead of WXK constants XORing
99 my $result = '';
100 $result .= 'ALT + ' if $dialog->{keycodes}->[2];
101 $result .= 'CTRL + ' if $dialog->{keycodes}->[1];
102 $result .= 'SHIFT + ' if $dialog->{keycodes}->[0];
103 $result .= uc($dialog->{'keys'}->[3]) if $dialog->{'keys'}->[3];
104 $dialog->{result_human} = $result;
105  
106 $dialog->{result_keycodes} = '';
107 $dialog->{result_keycodes} .= $dialog->{keycodes}->[2].'-' if $dialog->{keycodes}->[2];
108 $dialog->{result_keycodes} .= $dialog->{keycodes}->[1].'-' if $dialog->{keycodes}->[1];
109 $dialog->{result_keycodes} .= $dialog->{keycodes}->[0].'-' if $dialog->{keycodes}->[0];
110 $dialog->{result_keycodes} .= $dialog->{keycodes}->[3] if $dialog->{keycodes}->[3];
111  
112 $dialog->{result}->SetValue($result);
113 }
114  
115 1;