1 |
package CFClient::BindingEditor; |
2 |
|
3 |
use CFClient::UI; |
4 |
|
5 |
our @ISA = CFClient::UI::FancyFrame::; |
6 |
|
7 |
sub new { |
8 |
my $class = shift; |
9 |
|
10 |
my $self = $class->SUPER::new ( |
11 |
binding => [], |
12 |
commands => [], |
13 |
title => "Macro/Keybinding Recorder", |
14 |
@_ |
15 |
); |
16 |
|
17 |
$self->add (my $vb = new CFClient::UI::VBox); |
18 |
|
19 |
$vb->add ($self->{rec_btn} = new CFClient::UI::Button |
20 |
text => "start recording", |
21 |
tooltip => "Start/Stops recording of actions." |
22 |
."All subsequent actions after the recording started will be captured." |
23 |
."The actions are displayed after the record was stopped." |
24 |
."To bind the action you have to click on the 'Bind' button", |
25 |
on_activate => sub { |
26 |
unless ($self->{recording}) { |
27 |
$self->start; |
28 |
} else { |
29 |
$self->stop; |
30 |
} |
31 |
}); |
32 |
|
33 |
$vb->add (new CFClient::UI::Label text => "Actions:"); |
34 |
$vb->add ($self->{cmdbox} = new CFClient::UI::VBox); |
35 |
|
36 |
$vb->add (new CFClient::UI::Label text => "Bound to: "); |
37 |
$vb->add (my $hb = new CFClient::UI::HBox); |
38 |
$hb->add ($self->{keylbl} = new CFClient::UI::Label expand => 1); |
39 |
$hb->add (new CFClient::UI::Button |
40 |
text => "bind", |
41 |
tooltip => "This opens a query where you have to press the key combination to bind the recorded actions", |
42 |
on_activate => sub { |
43 |
$self->ask_for_bind; |
44 |
}); |
45 |
|
46 |
$vb->add (my $hb = new CFClient::UI::HBox); |
47 |
$hb->add (new CFClient::UI::Button |
48 |
text => "ok", |
49 |
expand => 1, |
50 |
tooltip => "This closes the binding editor and saves the binding", |
51 |
on_activate => sub { |
52 |
$self->hide; |
53 |
$self->commit; |
54 |
}); |
55 |
|
56 |
$hb->add (new CFClient::UI::Button |
57 |
text => "cancel", |
58 |
expand => 1, |
59 |
tooltip => "This closes the binding editor without saving", |
60 |
on_activate => sub { |
61 |
$self->hide; |
62 |
$self->{binding_cancel}->() |
63 |
if $self->{binding_cancel}; |
64 |
}); |
65 |
|
66 |
$self->update_binding_widgets; |
67 |
|
68 |
$self |
69 |
} |
70 |
|
71 |
sub cfg_bind { |
72 |
my ($self, $mod, $sym, $cmds) = @_; |
73 |
$::CFG->{profile}{default}{bindings}{$mod}{$sym} = $cmds; |
74 |
::update_bindings (); |
75 |
} |
76 |
|
77 |
sub cfg_unbind { |
78 |
my ($self, $mod, $sym, $cmds) = @_; |
79 |
delete $::CFG->{profile}{default}{bindings}{$mod}{$sym}; |
80 |
::update_bindings (); |
81 |
} |
82 |
|
83 |
sub commit { |
84 |
my ($self) = @_; |
85 |
my ($mod, $sym, $cmds) = $self->get_binding; |
86 |
if ($sym != 0 && @$cmds > 0) { |
87 |
$::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym) |
88 |
."'. Don't forget 'Save Config'!"); |
89 |
$self->{binding_change}->($mod, $sym, $cmds) |
90 |
if $self->{binding_change}; |
91 |
} else { |
92 |
$::STATUSBOX->add ("No action bound, no key or action specified!"); |
93 |
$self->{binding_cancel}->() |
94 |
if $self->{binding_cancel}; |
95 |
} |
96 |
} |
97 |
|
98 |
sub start { |
99 |
my ($self) = @_; |
100 |
|
101 |
$self->{rec_btn}->set_text ("stop recording"); |
102 |
$self->{recording} = 1; |
103 |
$self->clear_command_list; |
104 |
$::CONN->start_record if $::CONN; |
105 |
} |
106 |
|
107 |
sub stop { |
108 |
my ($self) = @_; |
109 |
|
110 |
$self->{rec_btn}->set_text ("start recording"); |
111 |
$self->{recording} = 0; |
112 |
|
113 |
my $rec; |
114 |
$rec = $::CONN->stop_record if $::CONN; |
115 |
return unless ref $rec eq 'ARRAY'; |
116 |
$self->set_command_list ($rec); |
117 |
} |
118 |
|
119 |
|
120 |
sub ask_for_bind_and_commit { |
121 |
my ($self) = @_; |
122 |
$self->ask_for_bind (1); |
123 |
} |
124 |
|
125 |
sub ask_for_bind { |
126 |
my ($self, $commit, $end_cb) = @_; |
127 |
|
128 |
CFClient::Binder::open_binding_dialog (sub { |
129 |
my ($mod, $sym) = @_; |
130 |
$self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak? |
131 |
$self->update_binding_widgets; |
132 |
$self->commit if $commit; |
133 |
$end_cb->() if $end_cb; |
134 |
}); |
135 |
} |
136 |
|
137 |
# $mod and $sym are the modifiers and key symbol |
138 |
# $cmds is a array ref of strings (the commands) |
139 |
# $cb is the callback that is executed on OK |
140 |
# $ccb is the callback that is executed on CANCEL and |
141 |
# when the binding was unsuccessful on OK |
142 |
sub set_binding { |
143 |
my ($self, $mod, $sym, $cmds, $cb, $ccb) = @_; |
144 |
|
145 |
$self->clear_command_list; |
146 |
$self->{recording} = 0; |
147 |
$self->{rec_btn}->set_text ("start recording"); |
148 |
|
149 |
$self->{binding} = [$mod, $sym]; |
150 |
$self->{commands} = $cmds; |
151 |
|
152 |
$self->{binding_change} = $cb; |
153 |
$self->{binding_cancel} = $ccb; |
154 |
|
155 |
$self->update_binding_widgets; |
156 |
} |
157 |
|
158 |
# this is a shortcut method that asks for a binding |
159 |
# and then just binds it. |
160 |
sub do_quick_binding { |
161 |
my ($self, $cmds, $end_cb) = @_; |
162 |
$self->set_binding (undef, undef, $cmds, sub { $self->cfg_bind (@_) }); |
163 |
$self->ask_for_bind (1, $end_cb); |
164 |
} |
165 |
|
166 |
sub update_binding_widgets { |
167 |
my ($self) = @_; |
168 |
my ($mod, $sym, $cmds) = $self->get_binding; |
169 |
$self->{keylbl}->set_text (CFClient::Binder::keycombo_to_name ($mod, $sym)); |
170 |
$self->set_command_list ($cmds); |
171 |
} |
172 |
|
173 |
sub get_binding { |
174 |
my ($self) = @_; |
175 |
return ( |
176 |
$self->{binding}->[0], |
177 |
$self->{binding}->[1], |
178 |
[ grep { defined $_ } @{$self->{commands}} ] |
179 |
); |
180 |
} |
181 |
|
182 |
sub clear_command_list { |
183 |
my ($self) = @_; |
184 |
$self->{cmdbox}->clear (); |
185 |
} |
186 |
|
187 |
sub set_command_list { |
188 |
my ($self, $cmds) = @_; |
189 |
|
190 |
$self->{cmdbox}->clear (); |
191 |
$self->{commands} = $cmds; |
192 |
|
193 |
my $idx = 0; |
194 |
|
195 |
for (@$cmds) { |
196 |
$self->{cmdbox}->add (my $hb = new CFClient::UI::HBox); |
197 |
|
198 |
my $i = $idx; |
199 |
$hb->add (new CFClient::UI::Label text => $_); |
200 |
$hb->add (new CFClient::UI::Button |
201 |
text => "delete", |
202 |
tooltip => "Deletes the action from the record", |
203 |
on_activate => sub { |
204 |
$self->{cmdbox}->remove ($hb); |
205 |
$cmds->[$i] = undef; |
206 |
}); |
207 |
|
208 |
|
209 |
$idx++ |
210 |
} |
211 |
} |
212 |
|
213 |
1 |