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

« back to all changes in this revision

Viewing changes to lib/Padre/Wx/Outline.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:
3
3
use 5.008;
4
4
use strict;
5
5
use warnings;
6
 
use Scalar::Util          ();
7
 
use Params::Util          ();
8
 
use Padre::Role::Task     ();
9
 
use Padre::Wx::Role::View ();
10
 
use Padre::Wx::Role::Main ();
11
 
use Padre::Wx             ();
 
6
use Scalar::Util            ();
 
7
use Params::Util            ();
 
8
use Padre::Feature          ();
 
9
use Padre::Role::Task       ();
 
10
use Padre::Wx::Role::View   ();
 
11
use Padre::Wx::Role::Main   ();
 
12
use Padre::Wx               ();
 
13
use Padre::Wx::FBP::Outline ();
12
14
use Padre::Logger;
13
15
 
14
 
our $VERSION = '0.90';
 
16
our $VERSION = '0.92';
15
17
our @ISA     = qw{
16
18
        Padre::Role::Task
17
19
        Padre::Wx::Role::View
18
20
        Padre::Wx::Role::Main
19
 
        Wx::TreeCtrl
 
21
        Padre::Wx::FBP::Outline
20
22
};
21
23
 
22
24
 
30
32
        my $class = shift;
31
33
        my $main  = shift;
32
34
        my $panel = shift || $main->right;
 
35
        my $self  = $class->SUPER::new($panel);
33
36
 
34
37
        # This tool is just a single tree control
35
 
        my $self = $class->SUPER::new(
36
 
                $panel,
37
 
                -1,
38
 
                Wx::wxDefaultPosition,
39
 
                Wx::wxDefaultSize,
40
 
                Wx::wxTR_HIDE_ROOT | Wx::wxTR_SINGLE | Wx::wxTR_HAS_BUTTONS | Wx::wxTR_LINES_AT_ROOT
41
 
        );
42
 
        $self->SetIndent(10);
43
 
 
44
 
        Wx::Event::EVT_COMMAND_SET_FOCUS(
45
 
                $self, $self,
 
38
        my $tree = $self->{tree};
 
39
        $self->disable;
 
40
        $tree->SetIndent(10);
 
41
 
 
42
        # Prepare the available images
 
43
        my $images = Wx::ImageList->new( 16, 16 );
 
44
        $self->{images} = {
 
45
                folder => $images->Add(
 
46
                        Wx::ArtProvider::GetBitmap(
 
47
                                'wxART_FOLDER',
 
48
                                'wxART_OTHER_C',
 
49
                                [ 16, 16 ],
 
50
                        ),
 
51
                ),
 
52
                file => $images->Add(
 
53
                        Wx::ArtProvider::GetBitmap(
 
54
                                'wxART_NORMAL_FILE',
 
55
                                'wxART_OTHER_C',
 
56
                                [ 16, 16 ],
 
57
                        ),
 
58
                ),
 
59
        };
 
60
        $tree->AssignImageList($images);
 
61
 
 
62
        Wx::Event::EVT_TEXT(
 
63
                $self,
 
64
                $self->{search},
46
65
                sub {
47
 
                        $self->on_tree_item_set_focus( $_[1] );
 
66
                        $self->render;
48
67
                },
49
68
        );
50
69
 
51
 
        # Double-click a function name
52
 
        Wx::Event::EVT_TREE_ITEM_ACTIVATED(
53
 
                $self, $self,
 
70
        # Handle char events in search box
 
71
        Wx::Event::EVT_CHAR(
 
72
                $self->{search},
54
73
                sub {
55
 
                        $self->on_tree_item_activated( $_[1] );
 
74
                        my ( $this, $event ) = @_;
 
75
 
 
76
                        my $code = $event->GetKeyCode;
 
77
                        if ( $code == Wx::K_DOWN or $code == Wx::K_UP or $code == Wx::K_RETURN ) {
 
78
 
 
79
                                # Up/Down and return keys focus on the functions lists
 
80
                                my $tree = $self->{tree};
 
81
                                $tree->SetFocus;
 
82
                                my $selection = $tree->GetSelection;
 
83
                                if ( $selection == -1 and $tree->GetCount > 0 ) {
 
84
                                        $selection = 0;
 
85
                                }
 
86
                                $tree->SelectItem($selection);
 
87
                        } elsif ( $code == Wx::K_ESCAPE ) {
 
88
 
 
89
                                # Escape key clears search and returns focus
 
90
                                # to the editor
 
91
                                $self->{search}->SetValue('');
 
92
                                my $editor = $self->current->editor;
 
93
                                $editor->SetFocus if $editor;
 
94
                        }
 
95
 
 
96
                        $event->Skip(1);
 
97
                        return;
56
98
                }
57
99
        );
58
100
 
59
 
        $self->Hide;
60
 
 
61
 
        # Cache document metadata for use when changing documents.
62
 
        # By substituting old metadata before we scan for new metadata,
63
 
        # we can make the widget APPEAR to be faster than it is and
64
 
        # offset the cost of doing the PPI parse in the background.
65
 
        # $self->{cache} = {};
 
101
        if (Padre::Feature::STYLE_GUI) {
 
102
                $self->main->theme->apply($self);
 
103
        }
66
104
 
67
105
        return $self;
68
106
}
79
117
        my $event  = shift;
80
118
        my $show   = 0;
81
119
        my $menu   = Wx::Menu->new;
82
 
        my $pldata = $self->GetPlData( $event->GetItem );
 
120
        my $tree   = $self->{tree};
 
121
        my $pldata = $tree->GetPlData( $event->GetItem );
83
122
 
84
123
        if ( defined($pldata) && defined( $pldata->{line} ) && $pldata->{line} > 0 ) {
85
124
                my $goto = $menu->Append( -1, Wx::gettext('&Go to Element') );
116
155
        if ( $show > 0 ) {
117
156
                my $x = $event->GetPoint->x;
118
157
                my $y = $event->GetPoint->y;
119
 
                $self->PopupMenu( $menu, $x, $y );
 
158
                $tree->PopupMenu( $menu, $x, $y );
120
159
        }
121
160
 
122
161
        return;
130
169
sub on_tree_item_set_focus {
131
170
        my $self      = shift;
132
171
        my $event     = shift;
133
 
        my $selection = $self->GetSelection;
 
172
        my $tree      = $self->{tree};
 
173
        my $selection = $tree->GetSelection;
134
174
        if ( $selection and $selection->IsOk ) {
135
 
                my $item = $self->GetPlData($selection);
 
175
                my $item = $tree->GetPlData($selection);
136
176
                if ( defined $item ) {
137
177
                        $self->select_line_in_editor( $item->{line} );
138
178
                }
175
215
        my $self = shift;
176
216
        my $task = shift;
177
217
        my $data = Params::Util::_ARRAY( $task->{data} ) or return;
178
 
        my $lock = $self->main->lock('UPDATE');
 
218
        my $lock = $self->lock_update;
 
219
 
 
220
        # Cache data model for faster searches
 
221
        $self->{data} = $data;
 
222
 
 
223
        # And render it
 
224
        $self->render;
 
225
 
 
226
        return 1;
 
227
}
 
228
 
 
229
sub render {
 
230
        my $self        = shift;
 
231
        my $data        = $self->{data};
 
232
        my $search_term = quotemeta $self->{search}->GetValue;
179
233
 
180
234
        # Clear any old content
181
235
        $self->clear;
182
236
 
183
237
        # Add the hidden unused root
184
 
        my $root = $self->AddRoot(
 
238
        my $tree   = $self->{tree};
 
239
        my $images = $self->{images};
 
240
        my $root   = $tree->AddRoot(
185
241
                Wx::gettext('Outline'),
186
242
                -1,
187
243
                -1,
190
246
 
191
247
        # Add the package trees
192
248
        foreach my $pkg (@$data) {
193
 
                my $branch = $self->AppendItem(
 
249
                my $branch = $tree->AppendItem(
194
250
                        $root,
195
251
                        $pkg->{name},
196
252
                        -1, -1,
201
257
                                }
202
258
                        )
203
259
                );
 
260
                $tree->SetItemImage( $branch, $images->{folder} );
 
261
 
204
262
                my @types = qw(classes grammars packages pragmata modules
205
263
                        attributes methods events roles regexes);
206
264
                foreach my $type (@types) {
207
265
                        $self->add_subtree( $pkg, $type, $branch );
208
266
                }
209
 
                $self->Expand($branch);
 
267
                $tree->Expand($branch);
210
268
        }
211
269
 
212
270
        # Set MIME type specific event handler
213
271
        Wx::Event::EVT_TREE_ITEM_RIGHT_CLICK(
214
 
                $self, $self,
 
272
                $tree, $tree,
215
273
                sub {
216
 
                        $_[0]->on_tree_item_right_click( $_[1] );
 
274
                        $self->on_tree_item_right_click( $_[1] );
217
275
                },
218
276
        );
219
277
 
220
 
        # TO DO Expanding all is not acceptable: We need to keep the state
221
 
        # (i.e., keep the pragmata subtree collapsed if it was collapsed
222
 
        # by the user)
223
 
        #$self->ExpandAll;
224
278
        $self->GetBestSize;
225
279
 
226
 
        return 1;
 
280
        return;
227
281
}
228
282
 
229
283
 
233
287
######################################################################
234
288
# General Methods
235
289
 
 
290
# Sets the focus on the search field
 
291
sub focus_on_search {
 
292
        $_[0]->{search}->SetFocus;
 
293
}
 
294
 
236
295
sub gettext_label {
237
296
        Wx::gettext('Outline');
238
297
}
239
298
 
240
299
sub clear {
241
 
        $_[0]->DeleteAllItems;
 
300
        $_[0]->{tree}->DeleteAllItems;
242
301
}
243
302
 
244
303
sub refresh {
245
304
        TRACE( $_[0] ) if DEBUG;
246
 
        my $self = shift;
 
305
        my $self     = shift;
 
306
        my $current  = shift or return;
 
307
        my $document = $current->document;
 
308
        my $lock     = $self->lock_update;
 
309
        my $tree     = $self->{tree};
247
310
 
248
311
        # Cancel any existing outline task
249
312
        $self->task_reset;
250
313
 
251
 
        # Shortcut if the document is empty
252
 
        my $document = $self->current->document;
253
 
        unless ( $document and not $document->is_unused ) {
254
 
                $self->clear;
255
 
                return 1;
 
314
        # Always clear! :)
 
315
        $self->clear;
 
316
 
 
317
        # Hide the widgets when no files are open
 
318
        unless ($document) {
 
319
                $self->disable;
 
320
                return;
256
321
        }
257
322
 
258
323
        # Is there an outline task for this document type
259
324
        my $task = $document->task_outline;
260
325
        unless ($task) {
261
 
                $self->clear;
262
 
                return;
263
 
        }
 
326
                $self->disable;
 
327
                return;
 
328
        }
 
329
 
 
330
        # Shortcut if there is nothing to search for
 
331
        if ( $document->is_unused ) {
 
332
                $self->disable;
 
333
                return;
 
334
        }
 
335
 
 
336
        # Ensure the search box and tree are visible
 
337
        $self->enable;
264
338
 
265
339
        # Trigger the task to fetch the refresh data
266
340
        $self->task_request(
269
343
        );
270
344
}
271
345
 
 
346
sub disable {
 
347
        $_[0]->{search}->Hide;
 
348
        $_[0]->{tree}->Hide;
 
349
}
 
350
 
 
351
sub enable {
 
352
        my $self = shift;
 
353
 
 
354
        $self->{search}->Show;
 
355
        $self->{tree}->Show;
 
356
 
 
357
        # Recalculate our layout in case the view geometry
 
358
        # has changed from when we were hidden.
 
359
        $self->Layout;
 
360
}
 
361
 
272
362
sub add_subtree {
273
363
        my ( $self, $pkg, $type, $root ) = @_;
 
364
        my $tree        = $self->{tree};
 
365
        my $search_term = quotemeta $self->{search}->GetValue;
 
366
        my $images      = $self->{images};
274
367
 
275
368
        my %type_caption = (
276
369
                pragmata   => Wx::gettext('Pragmata'),
288
381
                        warn "Type not translated: $type_caption\n";
289
382
                }
290
383
 
291
 
                $type_elem = $self->AppendItem(
 
384
                $type_elem = $tree->AppendItem(
292
385
                        $root,
293
386
                        $type_caption,
294
387
                        -1,
295
388
                        -1,
296
389
                        Wx::TreeItemData->new
297
390
                );
 
391
                $tree->SetItemImage( $type_elem, $images->{folder} );
298
392
 
299
393
                my @sorted_entries = ();
300
394
                if ( $type eq 'methods' ) {
319
413
                }
320
414
 
321
415
                foreach my $item (@sorted_entries) {
322
 
                        $self->AppendItem(
 
416
                        my $name = $item->{name};
 
417
                        next if $name !~ /$search_term/;
 
418
                        my $item = $tree->AppendItem(
323
419
                                $type_elem,
324
 
                                $item->{name},
325
 
                                -1, -1,
 
420
                                $name, -1, -1,
326
421
                                Wx::TreeItemData->new(
327
422
                                        {   line => $item->{line},
328
 
                                                name => $item->{name},
 
423
                                                name => $name,
329
424
                                                type => $type,
330
425
                                        }
331
426
                                )
332
427
                        );
 
428
                        $tree->SetItemImage( $item, $images->{file} );
 
429
 
333
430
                }
334
431
        }
335
432
        if ( defined $type_elem ) {
336
 
                if ( $type eq 'methods' ) {
337
 
                        $self->Expand($type_elem);
 
433
                if ( length $search_term > 0 ) {
 
434
                        $tree->Expand($type_elem);
338
435
                } else {
339
 
                        if ( $self->IsExpanded($type_elem) ) {
340
 
                                $self->Collapse($type_elem);
 
436
                        if ( $type eq 'methods' ) {
 
437
                                $tree->Expand($type_elem);
 
438
                        } else {
 
439
                                if ( $tree->IsExpanded($type_elem) ) {
 
440
                                        $tree->Collapse($type_elem);
 
441
                                }
341
442
                        }
342
443
                }
343
444
        }