~ubuntu-branches/ubuntu/saucy/padre/saucy-proposed

« back to all changes in this revision

Viewing changes to lib/Padre/Wx/FindFast.pm

  • Committer: Package Import Robot
  • Author(s): Dominique Dumont, gregor herrmann, Dominique Dumont
  • Date: 2012-01-04 12:04:20 UTC
  • mfrom: (1.3.3)
  • Revision ID: package-import@ubuntu.com-20120104120420-i5oybqwf91m1d3il
Tags: 0.92.ds1-1
[ gregor herrmann ]
* Remove debian/source/local-options; abort-on-upstream-changes
  and unapply-patches are default in dpkg-source since 1.16.1.
* Swap order of alternative (build) dependencies after the perl
  5.14 transition.

[ Dominique Dumont ]
* Imported Upstream version 0.92.ds1
* removed fix-spelling patch (applied upstream)
* lintian-override: use wildcard to avoid listing a gazillion files
* updated size of some 'not-real-man-page' entries
* rules: remove dekstop cruft (replaced by a file provided in debian
  directory)
* control: removed Breaks statement. Add /me to uploaders. Updated
  dependencies
* rules: make sure that non-DFSG file (i.e. the cute butterfly, sigh)
  is not distributed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Padre::Wx::FindFast;
 
2
 
 
3
# Incremental search
 
4
 
 
5
use 5.008;
 
6
use strict;
 
7
use warnings;
 
8
use Padre::Current  ();
 
9
use Padre::Wx       ();
 
10
use Padre::Wx::Icon ();
 
11
 
 
12
our $VERSION = '0.92';
 
13
 
 
14
use constant GOOD => Wx::SystemSettings::GetColour( Wx::SYS_COLOUR_WINDOW );
 
15
use constant BAD  => Wx::Colour->new(
 
16
        GOOD->Red,
 
17
        int( GOOD->Green * 0.5 ),
 
18
        int( GOOD->Blue  * 0.5 ),
 
19
);
 
20
 
 
21
 
 
22
 
 
23
 
 
24
 
 
25
######################################################################
 
26
# Constructor
 
27
 
 
28
sub new {
 
29
        my $class = shift;
 
30
 
 
31
        my $self = bless {
 
32
                restart  => 1,
 
33
                backward => 0,
 
34
        }, $class;
 
35
 
 
36
        return $self;
 
37
}
 
38
 
 
39
sub find_term {
 
40
        my $self = shift;
 
41
        my $term = $self->{entry}->GetValue;
 
42
        return '' unless defined $term;
 
43
        return $term;
 
44
}
 
45
 
 
46
 
 
47
 
 
48
 
 
49
 
 
50
######################################################################
 
51
# Main Methods
 
52
 
 
53
#
 
54
# search($direction);
 
55
#
 
56
# initiate/continue searching in $direction.
 
57
#
 
58
sub search {
 
59
        my $self      = shift;
 
60
        my $direction = shift;
 
61
        my $current   = Padre::Current->new;
 
62
        my $editor    = $current->editor or return;
 
63
 
 
64
        $self->{backward} = $direction eq 'previous';
 
65
        unless ( $self->{panel} ) {
 
66
                $self->_create_panel;
 
67
        }
 
68
 
 
69
        # pane != panel
 
70
        my $pane = $current->main->aui->GetPane('find');
 
71
        if ( $pane->IsShown ) {
 
72
                $self->_find;
 
73
        } else {
 
74
                $self->_show_panel;
 
75
        }
 
76
}
 
77
 
 
78
# -- Private methods
 
79
 
 
80
sub _find {
 
81
        my $self    = shift;
 
82
        my $current = Padre::Current->new;
 
83
        my $editor  = $current->editor or return;
 
84
        my $lock    = $self->lock_update;
 
85
 
 
86
        # Reset the dialog status
 
87
        $self->_status(1);
 
88
 
 
89
        # Build the search expression
 
90
        my $find_term = $self->{entry}->GetValue;
 
91
        if ( length $find_term ) {
 
92
                require Padre::Search;
 
93
                my $search = Padre::Search->new(
 
94
                        find_case    => $self->{case}->GetValue,
 
95
                        find_regex   => 0,
 
96
                        find_reverse => $self->{backward},
 
97
                        find_term    => $find_term,
 
98
                );
 
99
 
 
100
                # Handle restarting the search
 
101
                if ( $self->{restart} ) {
 
102
                        $editor->SetSelection( 0, 0 );
 
103
                        $self->{restart} = 0;
 
104
                }
 
105
 
 
106
                # Run the search
 
107
                unless ( $current->main->search_next($search) ) {
 
108
                        $self->_status(0);
 
109
                }
 
110
                $self->{entry}->SetFocus;
 
111
        }
 
112
 
 
113
        return;
 
114
}
 
115
 
 
116
# -- GUI related subs
 
117
 
 
118
#
 
119
# _create_panel();
 
120
#
 
121
# create find panel in aui manager.
 
122
#
 
123
sub _create_panel {
 
124
        my $self = shift;
 
125
        my $main = Padre::Current->main;
 
126
 
 
127
        # The panel and the boxsizer to place controls
 
128
        $self->{outer} = Wx::BoxSizer->new(Wx::HORIZONTAL);
 
129
        $self->{panel} = Wx::Panel->new( $main, -1, Wx::DefaultPosition, Wx::DefaultSize );
 
130
        $self->{hbox}  = Wx::BoxSizer->new(Wx::HORIZONTAL);
 
131
 
 
132
        # Close button
 
133
        $self->{close} = Wx::BitmapButton->new(
 
134
                $self->{panel}, -1,
 
135
                Padre::Wx::Icon::find('actions/x-document-close'),
 
136
                Wx::Point->new( -1, -1 ),
 
137
                Wx::Size->new( -1, -1 ),
 
138
                Wx::BORDER_NONE,
 
139
        );
 
140
        Wx::Event::EVT_BUTTON( $main, $self->{close}, sub { $self->_hide_panel } );
 
141
 
 
142
        # Search area
 
143
        $self->{label} = Wx::StaticText->new( $self->{panel}, -1, Wx::gettext('Find:') );
 
144
        $self->{entry} = Wx::TextCtrl->new( $self->{panel}, -1, '' );
 
145
        $self->{entry}->SetMinSize( Wx::Size->new( 25 * $self->{entry}->GetCharWidth, -1 ) );
 
146
        Wx::Event::EVT_CHAR( $self->{entry}, sub { $self->_on_key_pressed( $_[1] ) } );
 
147
        Wx::Event::EVT_TEXT( $main, $self->{entry}, sub { $self->_on_entry_changed( $_[1] ) } );
 
148
 
 
149
        # Previous button
 
150
        $self->{previous} = Wx::BitmapButton->new(
 
151
                $self->{panel}, -1,
 
152
                Padre::Wx::Icon::find('actions/go-previous'),
 
153
                Wx::Point->new( -1, -1 ),
 
154
                Wx::Size->new( -1, -1 ),
 
155
                Wx::BORDER_NONE
 
156
        );
 
157
 
 
158
        Wx::Event::EVT_BUTTON( $main, $self->{previous}, sub { $self->search('previous') } );
 
159
        $self->{previous_text} = Wx::Button->new(
 
160
                $self->{panel}, -1,
 
161
                Wx::gettext('Previ&ous'),
 
162
                Wx::Point->new( -1, -1 ),
 
163
                Wx::Size->new( -1, -1 ),
 
164
                Wx::BORDER_NONE,
 
165
        );
 
166
        Wx::Event::EVT_BUTTON( $main, $self->{previous_text}, sub { $self->search('previous') } );
 
167
 
 
168
        # Previous button
 
169
        $self->{next} = Wx::BitmapButton->new(
 
170
                $self->{panel}, -1,
 
171
                Padre::Wx::Icon::find('actions/go-next'),
 
172
                Wx::Point->new( -1, -1 ),
 
173
                Wx::Size->new( -1, -1 ),
 
174
                Wx::BORDER_NONE,
 
175
        );
 
176
        Wx::Event::EVT_BUTTON( $main, $self->{next}, sub { $self->search('next') } );
 
177
        $self->{next_text} = Wx::Button->new(
 
178
                $self->{panel}, -1,
 
179
                Wx::gettext('&Next'),
 
180
                Wx::Point->new( -1, -1 ),
 
181
                Wx::Size->new( -1, -1 ),
 
182
                Wx::BORDER_NONE,
 
183
        );
 
184
        Wx::Event::EVT_BUTTON( $main, $self->{next_text}, sub { $self->search('next') } );
 
185
 
 
186
        # Case sensitivity
 
187
        $self->{case} = Wx::CheckBox->new( $self->{panel}, -1, Wx::gettext('Case &sensitive') );
 
188
        Wx::Event::EVT_CHECKBOX( $main, $self->{case}, sub { $self->_on_case_checked } );
 
189
 
 
190
        # Place all controls
 
191
        $self->{hbox}->Add( $self->{close},         0, Wx::ALIGN_CENTER_VERTICAL | Wx::ALL,  5 );
 
192
        $self->{hbox}->Add( $self->{label},         0, Wx::ALIGN_CENTER_VERTICAL | Wx::LEFT, 10 );
 
193
        $self->{hbox}->Add( $self->{entry},         0, Wx::ALIGN_CENTER_VERTICAL | Wx::ALL,  5 );
 
194
        $self->{hbox}->Add( $self->{previous},      0, Wx::ALIGN_CENTER_VERTICAL | Wx::ALL,  5 );
 
195
        $self->{hbox}->Add( $self->{previous_text}, 0, Wx::ALIGN_CENTER_VERTICAL | Wx::ALL,  5 );
 
196
        $self->{hbox}->Add( $self->{next},          0, Wx::ALIGN_CENTER_VERTICAL | Wx::ALL,  5 );
 
197
        $self->{hbox}->Add( $self->{next_text},     0, Wx::ALIGN_CENTER_VERTICAL | Wx::ALL,  5 );
 
198
        $self->{hbox}->Add( $self->{case},          0, Wx::ALIGN_CENTER_VERTICAL | Wx::ALL,  5 );
 
199
        $self->{hbox}->Add( 0,                      1, Wx::EXPAND,                           5 );
 
200
 
 
201
        $self->{panel}->SetSizer( $self->{hbox} );
 
202
        $self->{panel}->Layout;
 
203
        $self->{hbox}->Fit( $self->{panel} );
 
204
 
 
205
        $self->{outer}->Add( $self->{panel}, 1, Wx::ALIGN_LEFT | Wx::ALL | Wx::EXPAND, 5 );
 
206
 
 
207
        my $width  = $main->current->editor->GetSize->GetWidth;
 
208
        my $height = $self->{panel}->GetSize->GetHeight;
 
209
        my $size   = Wx::Size->new( $width, $height );
 
210
        $self->{panel}->SetSize($size);
 
211
 
 
212
        # manage the pane in aui
 
213
        $main->aui->AddPane(
 
214
                $self->{panel},
 
215
                Padre::Wx->aui_pane_info(
 
216
                        Name           => 'find',
 
217
                        CaptionVisible => 0,
 
218
                        Layer          => 1,
 
219
                        PaneBorder     => 0,
 
220
                        )->Bottom->Fixed->Hide,
 
221
        );
 
222
 
 
223
        return 1;
 
224
}
 
225
 
 
226
sub _hide_panel {
 
227
        my $self = shift;
 
228
 
 
229
        # pane != panel
 
230
        my $auimngr = Padre->ide->wx->main->aui;
 
231
        $auimngr->GetPane('find')->Hide;
 
232
        $auimngr->Update;
 
233
 
 
234
        $self->{visible} = 0;
 
235
 
 
236
        Padre::Current->editor->SetFocus;
 
237
 
 
238
        return 1;
 
239
}
 
240
 
 
241
sub _show_panel {
 
242
        my $self = shift;
 
243
 
 
244
        # Create the panel if needed
 
245
        unless ( $self->{panel} ) {
 
246
                $self->_create_panel;
 
247
        }
 
248
 
 
249
        # Show the panel; pane != panel
 
250
        my $auimngr = Padre->ide->wx->main->aui;
 
251
        $auimngr->GetPane('find')->Show(1);
 
252
        $auimngr->Update;
 
253
 
 
254
        # Reset the form
 
255
        $self->_status(1);
 
256
        $self->{case}->SetValue(0);
 
257
        $self->{entry}->SetValue('');
 
258
        $self->{entry}->SetFocus;
 
259
        $self->{visible} = 1;
 
260
 
 
261
        return 1;
 
262
}
 
263
 
 
264
sub visible {
 
265
        $_[0]->{visible} || 0;
 
266
}
 
267
 
 
268
# -- Event handlers
 
269
 
 
270
#
 
271
# _on_case_checked()
 
272
#
 
273
# called when the "case insensitive" checkbox has changed value. in that
 
274
# case, we'll restart searching from the start of the document.
 
275
#
 
276
sub _on_case_checked {
 
277
        my $self = shift;
 
278
        $self->{restart} = 1;
 
279
        $self->{entry}->SetFocus;
 
280
        $self->_find;
 
281
}
 
282
 
 
283
#
 
284
# _on_entry_changed()
 
285
#
 
286
# called when the entry content has changed (keyboard or other mean). in that
 
287
# case, we'll start searching from the start of the document.
 
288
#
 
289
sub _on_entry_changed {
 
290
        my $self = shift;
 
291
        $self->{restart} = 1;
 
292
        $self->_find;
 
293
}
 
294
 
 
295
#
 
296
# _on_key_pressed()
 
297
#
 
298
# called when a key is pressed in the entry. used to trap
 
299
#               escape so we abort
 
300
#               return = find again
 
301
# search, otherwise dispatch event up-stack.
 
302
#
 
303
sub _on_key_pressed {
 
304
        my $self  = shift;
 
305
        my $event = shift;
 
306
        my $mod   = $event->GetModifiers || 0;
 
307
        my $code  = $event->GetKeyCode;
 
308
 
 
309
        # Remove the bit ( Wx::MOD_META) set by Num Lock being pressed on Linux
 
310
        $mod = $mod & ( Wx::MOD_ALT + Wx::MOD_CMD + Wx::MOD_SHIFT );
 
311
 
 
312
        if ( $code == Wx::K_ESCAPE ) {
 
313
                $self->_hide_panel;
 
314
                return;
 
315
        }
 
316
        if ( $code == Wx::K_RETURN ) {
 
317
                $self->_find;
 
318
                return;
 
319
        }
 
320
 
 
321
        $event->Skip(1);
 
322
}
 
323
 
 
324
# Set the status visuals as good/bad
 
325
sub _status {
 
326
        $_[0]->{entry}->SetBackgroundColour( $_[1] ? GOOD : BAD );
 
327
}
 
328
 
 
329
sub lock_update {
 
330
        my $self   = shift;
 
331
        my $lock   = Wx::WindowUpdateLocker->new( $self->{entry} );
 
332
        my $editor = Padre::Current->editor;
 
333
        if ($editor) {
 
334
                $lock = [ $lock, $editor->lock_update ];
 
335
        }
 
336
        return $lock;
 
337
}
 
338
 
 
339
1;
 
340
 
 
341
# Copyright 2008-2011 The Padre development team as listed in Padre.pm.
 
342
# LICENSE
 
343
# This program is free software; you can redistribute it and/or
 
344
# modify it under the same terms as Perl 5 itself.