-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathPACGlobalVarEntry.pm
269 lines (202 loc) · 7.39 KB
/
PACGlobalVarEntry.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package PACGlobalVarEntry;
##################################################################
# This file is part of PAC( Perl Auto Connector)
#
# Copyright (C) 2010-2016 David Torrejon Vaquerizas
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###################################################################
$|++;
###################################################################
# Import Modules
# Standard
use strict;
use warnings;
use FindBin qw ( $RealBin $Bin $Script );
# GTK2
use Gtk2 '-init';
# PAC modules
use PACUtils;
# END: Import Modules
###################################################################
###################################################################
# Define GLOBAL CLASS variables
# END: Define GLOBAL CLASS variables
###################################################################
###################################################################
# START: Public class methods
sub new {
my $class = shift;
my $self = {};
$self -> {cfg} = shift;
$self -> {container} = undef;
$self -> {frame} = {};
$self -> {list} = [];
_buildVarGUI( $self );
defined $self -> {cfg} and PACGlobalVarEntry::update( $self -> {cfg} );
bless( $self, $class );
return $self;
}
sub update {
my $self = shift;
my $cfg = shift;
defined $cfg and $$self{cfg} = $cfg;
# Destroy previous widgets
$$self{frame}{vbvar} -> foreach( sub { $_[0] -> destroy(); } );
# Empty parent widgets' list
$$self{list} = [];
# Now, add the -new?- widgets
foreach my $var ( sort { $a cmp $b } keys %{ $$self{cfg} } ) { _buildVar( $self, $var, $$self{cfg}{$var}{'value'}, $$self{cfg}{$var}{'hidden'} ); }
return 1;
}
sub get_cfg {
my $self = shift;
my %hash;
foreach my $w ( @{ $self -> {list} } ) {
next if $$w{var} -> get_chars( 0, -1 ) eq '';
$hash{ $$w{var} -> get_chars( 0, -1 ) }{'value'} = $$w{val} -> get_chars( 0, -1 );
$hash{ $$w{var} -> get_chars( 0, -1 ) }{'hidden'} = $$w{hide} -> get_active;
}
return \%hash;
}
# END: Public class methods
###################################################################
###################################################################
# START: Private functions definitions
sub _buildVarGUI {
my $self = shift;
my $cfg = $self -> {cfg};
my %w;
# Build a vbox for:buttons, separator and expect widgets
$w{vbox} = Gtk2::VBox -> new( 0, 0 );
# Build a hbuttonbox for widgets actions (add, etc.)
$w{bbox} = Gtk2::HButtonBox -> new();
$w{vbox} -> pack_start( $w{bbox}, 0, 1, 0 );
$w{bbox} -> set_layout( 'GTK_BUTTONBOX_START' );
# Build 'add' button
$w{btnadd} = Gtk2::Button -> new_from_stock( 'gtk-add' );
$w{bbox} -> add( $w{btnadd} );
# Build a separator
$w{sep} = Gtk2::HSeparator -> new();
$w{vbox} -> pack_start( $w{sep}, 0, 1, 5 );
# Build a scrolled window
$w{sw} = Gtk2::ScrolledWindow -> new();
$w{vbox} -> pack_start( $w{sw}, 1, 1, 0 );
$w{sw} -> set_policy( 'automatic', 'automatic' );
$w{sw} -> set_shadow_type( 'none' );
$w{vp} = Gtk2::Viewport -> new();
$w{sw} -> add( $w{vp} );
$w{vp} -> set_property( 'border-width', 5 );
$w{vp} -> set_shadow_type( 'none' );
# Build and add the vbox that will contain the expect widgets
$w{vbvar} = Gtk2::VBox -> new( 0, 0 );
$w{vp} -> add( $w{vbvar} );
$$self{container} = $w{vbox};
$$self{frame} = \%w;
# Button(s) callback(s)
$w{btnadd} -> signal_connect( 'clicked', sub {
# Save current cfg
$$self{cfg} = $self -> get_cfg();
# Append an empty var entry to cfg
$$self{cfg}{' _variable'}{'value'} = '_value';
$$self{cfg}{' _variable'}{'hidden'} = 0;
# Update gui
$self -> update();
# Set keyboard focus on first created entry
$$self{list}[0]{var} -> grab_focus();
return 1;
} );
return 1;
}
sub _buildVar {
my $self = shift;
my $var = shift;
my $val = shift;
my $hide = shift;
my @undo;
my $undoing = 0;
my %w;
$w{position} = scalar @{ $$self{list} };
# Make an HBox to contain label, entry and del button
$w{hbox} = Gtk2::HBox -> new( 0, 0 );
# Build label
$w{lbl1} = Gtk2::Label -> new( 'Variable:' );
$w{hbox} -> pack_start( $w{lbl1}, 0, 1, 0 );
# Build entry
$w{var} = Gtk2::Entry -> new;
$w{hbox} -> pack_start( $w{var}, 0, 1, 0 );
$w{var} -> set_text( $var );
# Build label
$w{lbl2} = Gtk2::Label -> new( ' Value:' );
$w{hbox} -> pack_start( $w{lbl2}, 0, 1, 0 );
# Build entry
$w{val} = Gtk2::Entry -> new;
$w{hbox} -> pack_start( $w{val}, 1, 1, 0 );
$w{val} -> set_text( $val );
$w{hide} = Gtk2::CheckButton -> new( 'Hide' );
$w{hbox} -> pack_start( $w{hide}, 0, 1, 0 );
$w{hide} -> set_active( $hide );
$w{hide} -> signal_connect( toggled => sub { $w{val} -> set_visibility( ! $w{hide} -> get_active ); } );
$w{val} -> set_visibility( ! $w{hide} -> get_active );
# Build delete button
$w{btn} = Gtk2::Button -> new_from_stock( 'gtk-delete' );
$w{hbox} -> pack_start( $w{btn}, 0, 1, 0 );
# Add built control to main container
$$self{frame}{vbvar} -> pack_start( $w{hbox}, 0, 1, 0 );
$$self{frame}{vbvar} -> show_all;
$$self{list}[$w{position}] = \%w;
# Setup some callbacks
# Asign a callback for deleting entry
$w{btn} -> signal_connect( 'clicked' => sub {
$$self{cfg} = $self -> get_cfg();
splice( @{ $$self{list} }, $w{position}, 1 );
delete $$self{cfg}{ $w{var} -> get_chars( 0, -1 ) } ;
$self -> update();
return 1;
} );
$w{var} -> signal_connect( 'delete_text' => sub { ! $undoing and push( @undo, $w{var} -> get_chars( 0, -1 ) ); return $_[1], $_[3]; } );
$w{var} -> signal_connect( 'insert_text' => sub { ! $undoing and push( @undo, $w{var} -> get_chars( 0, -1 ) ); return $_[1], $_[3]; } );
$w{var} -> signal_connect( 'key_press_event' => sub {
my ( $widget, $event ) = @_;
my $keyval = '' . ( $event -> keyval );
my $state = '' . ( $event -> state );
# Check if <Ctrl>z is pushed
if ( ( $event -> state == [ qw( control-mask ) ] ) && ( chr( $keyval ) eq 'z' ) && ( scalar @undo ) ) {
$undoing = 1;
$w{var} -> set_text( pop( @undo ) );
$undoing = 0;
return 1;
}
return 0;
} );
$w{val} -> signal_connect( 'delete_text' => sub { ! $undoing and push( @undo, $w{val} -> get_chars( 0, -1 ) ); return $_[1], $_[3]; } );
$w{val} -> signal_connect( 'insert_text' => sub { ! $undoing and push( @undo, $w{val} -> get_chars( 0, -1 ) ); return $_[1], $_[3]; } );
$w{val} -> signal_connect( 'key_press_event' => sub {
my ( $widget, $event ) = @_;
my $keyval = '' . ( $event -> keyval );
my $state = '' . ( $event -> state );
# Check if <Ctrl>z is pushed
if ( ( $event -> state == [ qw( control-mask ) ] ) && ( chr( $keyval ) eq 'z' ) && ( scalar @undo ) ) {
$undoing = 1;
$w{val} -> set_text( pop( @undo ) );
$undoing = 0;
return 1;
}
return 0;
} );
return %w;
}
# END: Private functions definitions
###################################################################
1;