root/trunk/Padre-Plugin-Perl6/lib/Padre/Plugin/Perl6/Perl6HelpDialog.pm @ 5994

Revision 5994, 6.0 KB (checked in by azawawi, 14 months ago)

[Perl 6] Extended Grok in an unofficial way to support perl6_table_index.pod

Line 
1package Padre::Plugin::Perl6::Perl6HelpDialog;
2
3use warnings;
4use strict;
5
6# package exports and version
7our $VERSION = '0.49';
8our @EXPORT_OK = ();
9
10# module imports
11use Padre::Wx ();
12
13# is a subclass of Wx::Dialog
14use base 'Wx::Dialog';
15
16# accessors
17use Class::XSAccessor accessors => {
18        _hbox              => '_hbox',               # horizontal box sizer
19        _vbox              => '_vbox',               # vertical box sizer
20        _search_text       => '_search_text',        # search text control
21        _list              => '_list',               # matches list
22        _targets_index     => '_targets_index',      # targets index
23        _help_viewer       => '_help_viewer',        # HTML Help Viewer
24        _plugin            => '_plugin',             # plugin object
25        _topic             => '_topic',              # default help topic
26        _grok              => '_grok',               # Perl 6 documentation reader instance
27};
28
29# -- constructor
30sub new {
31        my ($class, $plugin, %opt) = @_;
32
33        my $main = $plugin->main;
34       
35        # create object
36        my $self = $class->SUPER::new(
37                $main,
38                -1,
39                Wx::gettext('Perl 6 Help (Powered by grok)'),
40                Wx::wxDefaultPosition,
41                Wx::wxDefaultSize,
42                Wx::wxDEFAULT_FRAME_STYLE|Wx::wxTAB_TRAVERSAL,
43        );
44
45        $self->SetIcon( Wx::GetWxPerlIcon() );
46        $self->_plugin($plugin);
47        $self->_topic($opt{topic} // '');
48
49        # create dialog
50        $self->_create;
51       
52        # fit and center the dialog
53        $self->Fit;
54        $self->CentreOnParent;
55       
56        return $self;
57}
58
59
60# -- event handler
61
62#
63# Fetches the current selection's help HTML via App::Grok
64#
65sub display_help_in_viewer {
66        my $self = shift;
67
68        my $help_html;
69        my $selection = $self->_list->GetSelection();
70        if($selection != -1) {
71                my $help_target = $self->_list->GetClientData($selection);
72
73                if($help_target) {
74                        eval {
75                                $help_html = $self->_grok->render_target($help_target,'xhtml');
76                        };
77                }
78        }
79       
80        if(not $help_html) {
81                $help_html = '<b>No Help found</b>';
82        }
83       
84        $self->_help_viewer->SetPage($help_html);
85
86        return;
87}
88
89# -- private methods
90
91#
92# create the dialog itself.
93#
94sub _create {
95        my $self = shift;
96
97        # create sizer that will host all controls
98        $self->_hbox( Wx::BoxSizer->new( Wx::wxHORIZONTAL ) );
99        $self->_vbox( Wx::BoxSizer->new( Wx::wxVERTICAL ) );
100
101        # create the controls
102        $self->_create_controls;
103        $self->_create_buttons;
104
105        # wrap everything in a box to add some padding
106        $self->SetMinSize( [ 640, 480 ] );
107        $self->SetSizer( $self->_hbox );
108       
109        # focus on the search text box
110        $self->_search_text->SetFocus();
111       
112        $self->_search_text->SetValue( $self->_topic );
113        $self->_update_list_box;
114}
115
116#
117# create the buttons pane.
118#
119sub _create_buttons {
120        my $self = shift;
121
122        my $close_button = Wx::Button->new( $self, Wx::wxID_CANCEL, Wx::gettext('Close') );
123        $self->_vbox->Add($close_button, 0, Wx::wxALL|Wx::wxALIGN_LEFT, 5 );
124}
125
126#
127# create controls in the dialog
128#
129sub _create_controls {
130        my $self = shift;
131
132        # search textbox
133        my $search_label = Wx::StaticText->new( $self, -1,
134                Wx::gettext('&Type a help topic to read:') );
135        $self->_search_text( Wx::TextCtrl->new( $self, -1, '' ) );
136       
137        # matches result list
138        my $matches_label = Wx::StaticText->new( $self, -1,
139                Wx::gettext('&Matching Help Topics:') );
140        $self->_list(
141                Wx::ListBox->new(
142                        $self,
143                        -1,
144                        Wx::wxDefaultPosition,
145                        Wx::wxDefaultSize,
146                        [],
147                        Wx::wxLB_SINGLE
148                )
149        );
150               
151        # HTML Help Viewer
152        require Padre::Wx::HtmlWindow;
153        $self->_help_viewer(
154                Padre::Wx::HtmlWindow->new(
155                        $self,
156                        -1,
157                        Wx::wxDefaultPosition,
158                        Wx::wxDefaultSize,
159                        Wx::wxBORDER_STATIC
160                )
161        );
162        $self->_help_viewer->SetPage('');
163
164       
165        $self->_vbox->Add( $search_label, 0, Wx::wxALL|Wx::wxEXPAND, 2 );
166        $self->_vbox->Add( $self->_search_text, 0, Wx::wxALL|Wx::wxEXPAND, 2 );
167        $self->_vbox->Add( $matches_label, 0, Wx::wxALL|Wx::wxEXPAND, 2 );
168        $self->_vbox->Add( $self->_list, 1, Wx::wxALL|Wx::wxEXPAND, 2 );
169        $self->_hbox->Add( $self->_vbox, 0, Wx::wxALL|Wx::wxEXPAND, 2 );
170        $self->_hbox->Add( $self->_help_viewer, 1,
171                Wx::wxALL | Wx::wxALIGN_TOP | Wx::wxALIGN_CENTER_HORIZONTAL | Wx::wxEXPAND, 1 );
172
173        $self->_setup_events();
174       
175        return;
176}
177
178#
179# Adds various events
180#
181sub _setup_events {
182        my $self = shift;
183       
184        Wx::Event::EVT_CHAR( $self->_search_text, sub {
185                my $this  = shift;
186                my $event = shift;
187                my $code  = $event->GetKeyCode;
188
189                if ( $code == Wx::WXK_DOWN || $code == Wx::WXK_PAGEDOWN) {
190                        $self->_list->SetFocus();
191                }
192
193                $event->Skip(1);               
194        });
195
196        Wx::Event::EVT_TEXT( $self, $self->_search_text, sub {
197
198                $self->_update_list_box;
199               
200                return;
201        });
202       
203        Wx::Event::EVT_LISTBOX( $self, $self->_list, sub {
204                $self->display_help_in_viewer;
205        });
206       
207}
208
209#
210# Search for files and cache result
211#
212sub _search() {
213        my $self = shift;
214       
215        # Generate a sorted file-list based on filename
216        eval {
217                require App::Grok;
218                $self->_grok(App::Grok->new);
219                my @targets_index = sort $self->_grok->target_index();
220                $self->_targets_index( \@targets_index );
221
222                #extend grok so it knows about Perl 6 table index
223                my $filename = File::Spec->catdir(
224                        Cwd::realpath( File::Basename::dirname(__FILE__) ),
225                        'perl6_table_index.pod' );
226                open FILE, $filename or die "Cannot open $filename\n";   
227                until (<FILE> =~ /=head1 Table index/) {}
228                my $item = undef;
229                while(my $line = <FILE>) {
230                        if($line =~ /^=head2\s+(.+?)$/i) {
231                                $item = $1;
232                        } elsif($item) {
233                                if(not $self->_grok->{functions}{$item}) {
234                                        my @empty = ($item,'');
235                                        $self->_grok->{functions}{$item} = \@empty;
236                                }
237                                $self->_grok->{functions}{$item}[1] .= $line;
238                        }
239                }
240                close FILE;
241               
242                @targets_index = sort $self->_grok->target_index();
243                $self->_targets_index( \@targets_index );
244        };
245       
246        return;
247}
248
249#
250# Update matches list box from matched files list
251#
252sub _update_list_box() {
253        my $self = shift;
254       
255        if(not $self->_targets_index) {
256                $self->_search();
257        }
258
259        my $search_expr = $self->_search_text->GetValue();
260        $search_expr = quotemeta $search_expr;
261
262        #Populate the list box now
263        $self->_list->Clear();
264        my $pos = 0;
265        foreach my $target (@{$self->_targets_index}) {
266                if($target =~ /$search_expr/i) {
267                        $self->_list->Insert($target, $pos, $target);
268                        $pos++;
269                }
270        }
271        if($pos > 0) {
272                $self->_list->Select(0);
273        }
274        $self->display_help_in_viewer;
275                       
276        return;
277}
278
279
2801;
Note: See TracBrowser for help on using the browser.