~ubuntu-branches/debian/sid/padre/sid

« back to all changes in this revision

Viewing changes to lib/Padre/Wx/Dialog/RegexEditor.pm

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov, gregor herrmann, Damyan Ivanov
  • Date: 2009-11-18 17:48:27 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20091118174827-srz62scl23gdl88v
Tags: 0.50.ds1-1
[ gregor herrmann ]
* debian/control: change dependency on "libclass-xsaccessor-array-perl
  (>= 1.02)" to "libclass-xsaccessor-perl (>= 1.05-2) |
  libclass-xsaccessor-array-perl (>= 1.02)".

[ Damyan Ivanov ]
* New upstream release
  + copyright: add a new translator
    - describe share/doc/perlopref copyright and licensing
  + update patches:
    - refresh disable-tcp-server.patch
    - drop fix-man-whatis.patch and fix-pod.patch (merged upstream)
    - drop fix-helpprovider-with-no-perlopref.patch: no longer necessary
* repack.sh: in 0.49 padre.exe moved from script/ to bin/
  + perlopref is now properly licensed
* add perl (>= 5.10.1) as a preferred alternative to versioned (build)
  dependencies on libtest-simple-perl and libpod-simple-perl
* update debian/not-real-manual.list for 0.50 (one file added, two files
  changed in size)
* rules: remove cruft from padre.deb
  + license etc from perlopref directory (already present in d/copyright)
  + remove .po files from the .deb (there are .mo files installed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Padre::Wx::Dialog::RegexEditor;
 
2
 
 
3
# The Regex Editor for Padre
 
4
 
 
5
use 5.008;
 
6
use strict;
 
7
use warnings;
 
8
use Carp            ();
 
9
use Padre::Wx       ();
 
10
use Padre::Wx::Icon ();
 
11
 
 
12
our $VERSION = '0.50';
 
13
our @ISA     = 'Wx::Dialog';
 
14
 
 
15
 
 
16
######################################################################
 
17
# Constructor
 
18
 
 
19
sub new {
 
20
        my $class  = shift;
 
21
        my $parent = shift;
 
22
 
 
23
        # Create the basic object
 
24
        my $self = $class->SUPER::new(
 
25
                $parent,
 
26
                -1,
 
27
                Wx::gettext('Regex Editor'),
 
28
                Wx::wxDefaultPosition,
 
29
                Wx::wxDefaultSize,
 
30
                Wx::wxDEFAULT_FRAME_STYLE,
 
31
        );
 
32
 
 
33
        # Set basic dialog properties
 
34
        $self->SetIcon(Padre::Wx::Icon::PADRE);
 
35
        $self->SetMinSize( [ 750, 550 ] );
 
36
 
 
37
        # Dialog Controls
 
38
 
 
39
        $self->{regex} = Wx::TextCtrl->new(
 
40
                $self, -1, '', Wx::wxDefaultPosition, Wx::wxDefaultSize,
 
41
                Wx::wxTE_MULTILINE | Wx::wxNO_FULL_REPAINT_ON_RESIZE
 
42
        );
 
43
 
 
44
        $self->{substitute} = Wx::TextCtrl->new(
 
45
                $self, -1, '', Wx::wxDefaultPosition, Wx::wxDefaultSize,
 
46
                Wx::wxTE_MULTILINE | Wx::wxNO_FULL_REPAINT_ON_RESIZE
 
47
        );
 
48
 
 
49
        $self->{original_text} = Wx::TextCtrl->new(
 
50
                $self, -1, '', Wx::wxDefaultPosition, Wx::wxDefaultSize,
 
51
                Wx::wxTE_MULTILINE | Wx::wxNO_FULL_REPAINT_ON_RESIZE
 
52
        );
 
53
 
 
54
        $self->{matched_text} = Wx::TextCtrl->new(
 
55
                $self, -1, '', Wx::wxDefaultPosition, Wx::wxDefaultSize,
 
56
                Wx::wxTE_MULTILINE | Wx::wxNO_FULL_REPAINT_ON_RESIZE
 
57
        );
 
58
 
 
59
        Wx::Event::EVT_TEXT(
 
60
                $self,
 
61
                $self->{regex},
 
62
                sub { $_[0]->run; },
 
63
        );
 
64
        Wx::Event::EVT_TEXT(
 
65
                $self,
 
66
                $self->{substitute},
 
67
                sub { $_[0]->run; },
 
68
        );
 
69
        Wx::Event::EVT_TEXT(
 
70
                $self,
 
71
                $self->{original_text},
 
72
                sub { $_[0]->run; },
 
73
        );
 
74
 
 
75
        # Modifiers
 
76
        my %m = _modifiers();
 
77
        foreach my $name ( keys %m ) {
 
78
                $self->{$name} = Wx::CheckBox->new(
 
79
                        $self,
 
80
                        -1,
 
81
                        $m{$name}{name},
 
82
                );
 
83
                Wx::Event::EVT_CHECKBOX(
 
84
                        $self,
 
85
                        $self->{$name},
 
86
                        sub {
 
87
                                $_[0]->box_clicked($name);
 
88
                        },
 
89
                );
 
90
        }
 
91
 
 
92
        $self->{matching} = Wx::RadioButton->new(
 
93
                $self,
 
94
                -1,
 
95
                'Matching',
 
96
        );
 
97
        Wx::Event::EVT_RADIOBUTTON( $self, $self->{matching}, sub { $_[0]->run; } );
 
98
        $self->{substituting} = Wx::RadioButton->new(
 
99
                $self,
 
100
                -1,
 
101
                'Substituting',
 
102
        );
 
103
        Wx::Event::EVT_RADIOBUTTON( $self, $self->{substituting}, sub { $_[0]->run; } );
 
104
 
 
105
        # Buttons
 
106
        #       $self->{button_match} = Wx::Button->new(
 
107
        #               $self,
 
108
        #               -1,
 
109
        #               Wx::gettext('&Match'),
 
110
        #       );
 
111
        #       Wx::Event::EVT_BUTTON(
 
112
        #               $self,
 
113
        #               $self->{button_match},
 
114
        #               sub {
 
115
        #                       $_[0]->button_match;
 
116
        #               },
 
117
        #       );
 
118
        #
 
119
        #       # Preferences Button
 
120
        #       $self->{button_replace} = Wx::Button->new(
 
121
        #               $self,
 
122
        #               -1,
 
123
        #               Wx::gettext('&Replace'),
 
124
        #       );
 
125
        #       Wx::Event::EVT_BUTTON(
 
126
        #               $self,
 
127
        #               $self->{button_replace},
 
128
        #               sub {
 
129
        #                       $_[0]->button_replace;
 
130
        #               },
 
131
        #       );
 
132
 
 
133
        # Close Button
 
134
        $self->{button_close} = Wx::Button->new(
 
135
                $self,
 
136
                Wx::wxID_CANCEL,
 
137
                Wx::gettext('&Close'),
 
138
        );
 
139
        Wx::Event::EVT_BUTTON(
 
140
                $self,
 
141
                $self->{button_close},
 
142
                sub {
 
143
                        $_[0]->button_close;
 
144
                },
 
145
        );
 
146
 
 
147
        # Dialog Layout
 
148
 
 
149
        # Horizontal button sizer
 
150
        my $buttons = Wx::BoxSizer->new(Wx::wxHORIZONTAL);
 
151
        $buttons->AddStretchSpacer;
 
152
        $buttons->Add( $self->{button_match},   0, Wx::wxALL, 1 );
 
153
        $buttons->Add( $self->{button_replace}, 0, Wx::wxALL, 1 );
 
154
        $buttons->AddStretchSpacer;
 
155
        $buttons->Add( $self->{button_close}, 0, Wx::wxALL, 1 );
 
156
        $buttons->AddStretchSpacer;
 
157
 
 
158
 
 
159
        my $modifiers = Wx::BoxSizer->new(Wx::wxHORIZONTAL);
 
160
        $modifiers->AddStretchSpacer;
 
161
        $modifiers->Add( $self->{ignore_case}, 0, Wx::wxALL, 1 );
 
162
        $modifiers->Add( $self->{single_line}, 0, Wx::wxALL, 1 );
 
163
        $modifiers->Add( $self->{multi_line},  0, Wx::wxALL, 1 );
 
164
        $modifiers->Add( $self->{extended},    0, Wx::wxALL, 1 );
 
165
 
 
166
        my $operation = Wx::BoxSizer->new(Wx::wxHORIZONTAL);
 
167
        $operation->AddStretchSpacer;
 
168
        $operation->Add( $self->{matching},     0, Wx::wxALL, 1 );
 
169
        $operation->Add( $self->{substituting}, 0, Wx::wxALL, 1 );
 
170
 
 
171
        # Vertical layout of the left hand side
 
172
        my $left = Wx::BoxSizer->new(Wx::wxVERTICAL);
 
173
        $left->Add( $modifiers, 0, Wx::wxALL | Wx::wxEXPAND, 1 );
 
174
        $left->Add( $operation, 0, Wx::wxALL | Wx::wxEXPAND, 1 );
 
175
        $left->Add(
 
176
                $self->{regex},
 
177
                1,
 
178
                Wx::wxALL | Wx::wxALIGN_TOP | Wx::wxALIGN_CENTER_HORIZONTAL | Wx::wxEXPAND,
 
179
                1
 
180
        );
 
181
        $left->Add(
 
182
                $self->{substitute},
 
183
                1,
 
184
                Wx::wxALL | Wx::wxALIGN_TOP | Wx::wxALIGN_CENTER_HORIZONTAL | Wx::wxEXPAND,
 
185
                1
 
186
        );
 
187
 
 
188
 
 
189
        # Vertical layout of the right hand side
 
190
        my $right = Wx::BoxSizer->new(Wx::wxVERTICAL);
 
191
        $right->Add(
 
192
                $self->{original_text},
 
193
                1,
 
194
                Wx::wxALL | Wx::wxALIGN_TOP | Wx::wxALIGN_CENTER_HORIZONTAL | Wx::wxEXPAND,
 
195
                1
 
196
        );
 
197
        $right->Add(
 
198
                $self->{matched_text},
 
199
                1,
 
200
                Wx::wxALL | Wx::wxALIGN_TOP | Wx::wxALIGN_CENTER_HORIZONTAL | Wx::wxEXPAND,
 
201
                1
 
202
        );
 
203
        $right->Add( $buttons, 0, Wx::wxALL | Wx::wxEXPAND, 1 );
 
204
 
 
205
 
 
206
 
 
207
        # Main sizer
 
208
        my $sizer = Wx::BoxSizer->new(Wx::wxHORIZONTAL);
 
209
        $sizer->Add( $left,  0, Wx::wxALL | Wx::wxEXPAND, 1 );
 
210
        $sizer->Add( $right, 1, Wx::wxALL | Wx::wxEXPAND, 1 );
 
211
 
 
212
        # Tune the size and position it appears
 
213
        $self->SetSizer($sizer);
 
214
        $self->Fit;
 
215
        $self->CentreOnParent;
 
216
 
 
217
        return $self;
 
218
}
 
219
 
 
220
 
 
221
sub _modifiers {
 
222
        return (
 
223
                ignore_case => { mod => 'i', name => sprintf( Wx::gettext('Ignore case (%s)'), 'i' ) },
 
224
                single_line => { mod => 's', name => sprintf( Wx::gettext('Single-line (%s)'), 's' ) },
 
225
                multi_line  => { mod => 'm', name => sprintf( Wx::gettext('Multi-line (%s)'),  'm' ) },
 
226
                extended    => { mod => 'x', name => sprintf( Wx::gettext('Extended (%s)'),    'x' ) },
 
227
        );
 
228
}
 
229
 
 
230
 
 
231
# -- public methods
 
232
 
 
233
sub show {
 
234
        my $self = shift;
 
235
 
 
236
        $self->{regex}->AppendText("regex");
 
237
        $self->{substitute}->AppendText("substitute");
 
238
        $self->{original_text}->AppendText("Original text");
 
239
        $self->{matching}->SetValue(1);
 
240
 
 
241
        $self->Show;
 
242
}
 
243
 
 
244
#
 
245
# $self->button_match;
 
246
#
 
247
# handler called when the Match button has been clicked.
 
248
#
 
249
sub button_match {
 
250
        my $self = shift;
 
251
        $self->run();
 
252
        return;
 
253
}
 
254
 
 
255
sub run {
 
256
        my $self = shift;
 
257
 
 
258
        my $regex = $self->{regex}->GetRange( 0, $self->{regex}->GetLastPosition );
 
259
        my $original_text = $self->{original_text}->GetRange( 0, $self->{original_text}->GetLastPosition );
 
260
        my $substitute = $self->{substitute}->GetRange( 0, $self->{substitute}->GetLastPosition );
 
261
 
 
262
 
 
263
        my $start = '';
 
264
        my $end   = '';
 
265
        my %m     = _modifiers();
 
266
        foreach my $name ( keys %m ) {
 
267
                if ( $self->{$name}->IsChecked ) {
 
268
                        $start .= $m{$name}{mod};
 
269
                } else {
 
270
                        $end .= $m{$name}{mod};
 
271
                }
 
272
        }
 
273
        my $xism = "$start-$end";
 
274
 
 
275
        $self->{matched_text}->Clear;
 
276
 
 
277
        if ( $self->{matching}->GetValue ) {
 
278
                my $match;
 
279
                eval {
 
280
                        if ( $original_text =~ /(?$xism:$regex)/ )
 
281
                        {
 
282
                                $match = substr( $original_text, $-[0], $+[0] - $-[0] );
 
283
                        }
 
284
                };
 
285
                if ($@) {
 
286
                        my $main = Padre->ide->wx->main;
 
287
 
 
288
                        #$main->message("Match failure in $regex:  $@");
 
289
                        $self->{matched_text}->AppendText("Match failure in $regex:  $@");
 
290
                        return;
 
291
                }
 
292
 
 
293
                if ( defined $match ) {
 
294
                        $self->{matched_text}->AppendText("Matched '$match'");
 
295
                } else {
 
296
                        $self->{matched_text}->AppendText("No match");
 
297
                }
 
298
        } else {
 
299
                $self->{matched_text}->AppendText("Substitute not yet implemented");
 
300
        }
 
301
 
 
302
        return;
 
303
}
 
304
 
 
305
#
 
306
# $self->button_replace;
 
307
#
 
308
# handler called when the Match button has been clicked.
 
309
#
 
310
sub button_replace {
 
311
        my $self = shift;
 
312
        my $main = Padre->ide->wx->main;
 
313
        $main->message("Replace");
 
314
        return;
 
315
}
 
316
 
 
317
 
 
318
sub box_clicked {
 
319
        my $self = shift;
 
320
 
 
321
        #my $box  = shift;
 
322
        $self->run();
 
323
 
 
324
        #my $main = Padre->ide->wx->main;
 
325
        #$main->message("Box $box");
 
326
        return;
 
327
 
 
328
}
 
329
 
 
330
#
 
331
# $self->button_close;
 
332
#
 
333
# handler called when the close button has been clicked.
 
334
#
 
335
sub button_close {
 
336
        $_[0]->Destroy;
 
337
}
 
338
 
 
339
1;
 
340
 
 
341
__END__
 
342
 
 
343
=pod
 
344
 
 
345
=head1 NAME
 
346
 
 
347
Padre::Wx::Dialog::RegexEditor - dialog to make it easy to creat a Regular expression
 
348
 
 
349
=head1 DESCRIPTION
 
350
 
 
351
 
 
352
The Regex Editor provides an interface to easily create Regular 
 
353
expressions used in Perl.
 
354
 
 
355
The user can insert a regex - the surrounding / characters are not
 
356
needed and a text. The Regex Editor will automatically display the matching 
 
357
text in the bottom right window.
 
358
 
 
359
 
 
360
At the top of the window the user can select any of the four
 
361
regex modifiers:
 
362
 
 
363
Ignore case (i)
 
364
 
 
365
Single-line (s)
 
366
 
 
367
Multi-line (m)
 
368
 
 
369
Extended (x)
 
370
 
 
371
 
 
372
=head1 TODO
 
373
 
 
374
Implement substitute as well
 
375
 
 
376
Global match
 
377
 
 
378
Allow the change/replacement of the // around the regex
 
379
 
 
380
Highlight the match in the source text instead of in 
 
381
a separate window
 
382
 
 
383
Display the captured groups in a tree hierarchy similar to Rx ?
 
384
 
 
385
  Group                  Span (character) Value
 
386
  Match 0 (Group 0)      4-7              the actual match
 
387
 
 
388
Display the various Perl variable containing the relevant values
 
389
e.g. the @- and @+ arrays, the %+ hash
 
390
$1, $2 ..
 
391
 
 
392
point out what to use instead of $@ and $' and $`
 
393
 
 
394
English explanation of the regex
 
395
 
 
396
=head1 COPYRIGHT & LICENSE
 
397
 
 
398
Copyright 2008-2009 The Padre development team as listed in Padre.pm.
 
399
 
 
400
This program is free software; you can redistribute it and/or modify it
 
401
under the same terms as Perl 5 itself.
 
402
 
 
403
=cut
 
404
 
 
405
# Copyright 2008-2009 The Padre development team as listed in Padre.pm.
 
406
# LICENSE
 
407
# This program is free software; you can redistribute it and/or
 
408
# modify it under the same terms as Perl 5 itself.