| 1 | package Padre::Wx::FindResult; |
|---|
| 2 | =pod |
|---|
| 3 | |
|---|
| 4 | =head1 NAME |
|---|
| 5 | |
|---|
| 6 | Padre::Wx::FindResult - Find and list all occurrences |
|---|
| 7 | |
|---|
| 8 | =head1 DESCRIPTION |
|---|
| 9 | |
|---|
| 10 | C<Padre::Wx::FindResult> Displays a list of all the occurrences of term |
|---|
| 11 | in a file. Clicking on an item in the list will go to the line in that editor. |
|---|
| 12 | |
|---|
| 13 | =cut |
|---|
| 14 | |
|---|
| 15 | use 5.008; |
|---|
| 16 | use strict; |
|---|
| 17 | use warnings; |
|---|
| 18 | use Params::Util qw{_INSTANCE}; |
|---|
| 19 | use Padre::Wx ; |
|---|
| 20 | use Wx::Event qw( EVT_BUTTON ); |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | our $VERSION = '0.50'; |
|---|
| 24 | our @ISA = 'Wx::ListView'; |
|---|
| 25 | my $LineCount; # Global fid count so it can be used in the label |
|---|
| 26 | |
|---|
| 27 | =pod |
|---|
| 28 | |
|---|
| 29 | =head3 new |
|---|
| 30 | |
|---|
| 31 | Create the new Find results Panel. |
|---|
| 32 | |
|---|
| 33 | =cut |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | sub new { |
|---|
| 37 | my ($class, $main, $lines,$editor) = @_; |
|---|
| 38 | $LineCount = scalar(@$lines); |
|---|
| 39 | #ensure the bottom aui is present. |
|---|
| 40 | $main->show_output(1); |
|---|
| 41 | |
|---|
| 42 | # Create the underlying object |
|---|
| 43 | my $self = $class->SUPER::new( |
|---|
| 44 | Padre::Current->main->bottom, |
|---|
| 45 | -1, |
|---|
| 46 | Wx::wxDefaultPosition, |
|---|
| 47 | Wx::wxDefaultSize, |
|---|
| 48 | Wx::wxLC_REPORT | Wx::wxLC_SINGLE_SEL |
|---|
| 49 | ); |
|---|
| 50 | |
|---|
| 51 | $self->InsertColumn( $_, _get_title($_) ) for 0 .. 1; |
|---|
| 52 | |
|---|
| 53 | Wx::Event::EVT_LIST_ITEM_ACTIVATED( |
|---|
| 54 | $self, $self, |
|---|
| 55 | sub { |
|---|
| 56 | $self->on_list_item_activated( $_[1] ,$main,$editor); |
|---|
| 57 | }, |
|---|
| 58 | ); |
|---|
| 59 | Wx::Event::EVT_RIGHT_DOWN( |
|---|
| 60 | $self, \&on_right_down, |
|---|
| 61 | ); |
|---|
| 62 | |
|---|
| 63 | $self->populate_list($lines); |
|---|
| 64 | $self->set_column_widths; |
|---|
| 65 | Padre::Current->main->bottom->show($self); |
|---|
| 66 | |
|---|
| 67 | return $self; |
|---|
| 68 | |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | =pod |
|---|
| 72 | |
|---|
| 73 | =head3 new |
|---|
| 74 | |
|---|
| 75 | gettext_label |
|---|
| 76 | |
|---|
| 77 | Sets the label of the tab. called automatically when the obejct is created. |
|---|
| 78 | |
|---|
| 79 | =cut |
|---|
| 80 | |
|---|
| 81 | sub gettext_label { |
|---|
| 82 | Wx::gettext('Find Results ('.$LineCount.')'); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | =pod |
|---|
| 87 | |
|---|
| 88 | =head3 set_column_widths |
|---|
| 89 | |
|---|
| 90 | $self->set_column_widths() |
|---|
| 91 | |
|---|
| 92 | works out the correct column widths for the |
|---|
| 93 | list columns. |
|---|
| 94 | |
|---|
| 95 | =cut |
|---|
| 96 | |
|---|
| 97 | sub set_column_widths { |
|---|
| 98 | my $self = shift; |
|---|
| 99 | |
|---|
| 100 | my $width0_default = $self->GetCharWidth * length( Wx::gettext('Line No') ) + 16; |
|---|
| 101 | |
|---|
| 102 | $self->SetColumnWidth( 0,$width0_default ); |
|---|
| 103 | $self->SetColumnWidth( 1, Wx::wxLIST_AUTOSIZE); |
|---|
| 104 | |
|---|
| 105 | return; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | #### |
|---|
| 110 | |
|---|
| 111 | =pod |
|---|
| 112 | |
|---|
| 113 | =head3 on_list_item_activated |
|---|
| 114 | |
|---|
| 115 | On double click event go to the selectd line in the editor |
|---|
| 116 | |
|---|
| 117 | =cut |
|---|
| 118 | |
|---|
| 119 | sub on_list_item_activated { |
|---|
| 120 | my ($self, $event,$main, $editor) =@_; |
|---|
| 121 | |
|---|
| 122 | #If the user has closed the editor the search was origionaly performed |
|---|
| 123 | #on |
|---|
| 124 | if (!$main->find_id_of_editor($editor)){ |
|---|
| 125 | $self->DeleteAllItems; |
|---|
| 126 | my $message_item->[0]->{line} = Wx::gettext('Related Editor Has been Closed'); |
|---|
| 127 | $message_item->[0]->{lineNumber} = '*'; |
|---|
| 128 | $self->populate_list($message_item); |
|---|
| 129 | return; |
|---|
| 130 | } |
|---|
| 131 | my $line = $event->GetItem->GetText; |
|---|
| 132 | |
|---|
| 133 | if ( not defined($line) |
|---|
| 134 | or $line !~ /^\d+$/o |
|---|
| 135 | or $editor->GetLineCount < $line ) |
|---|
| 136 | { |
|---|
| 137 | return; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | $self->select_line( $line - 1 ,$editor); |
|---|
| 141 | |
|---|
| 142 | return; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | =pod |
|---|
| 146 | |
|---|
| 147 | =head3 select_line |
|---|
| 148 | |
|---|
| 149 | $self->select_line($lineNumber, $editor); |
|---|
| 150 | |
|---|
| 151 | Sets the focus to the selected line. |
|---|
| 152 | |
|---|
| 153 | =cut |
|---|
| 154 | sub select_line { |
|---|
| 155 | my ( $self, $line,$editor ) = @_; |
|---|
| 156 | |
|---|
| 157 | return if not $editor; |
|---|
| 158 | |
|---|
| 159 | $editor->EnsureVisible($line); |
|---|
| 160 | $editor->goto_pos_centerize( $editor->GetLineIndentPosition($line) ); |
|---|
| 161 | $editor->SetFocus; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | =pod |
|---|
| 165 | |
|---|
| 166 | =head3 _get_title |
|---|
| 167 | |
|---|
| 168 | $self->_get_title(); |
|---|
| 169 | |
|---|
| 170 | set the column headings to the list. |
|---|
| 171 | |
|---|
| 172 | =cut |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | sub _get_title { |
|---|
| 177 | my $c = shift; |
|---|
| 178 | |
|---|
| 179 | return Wx::gettext('Line No') if $c == 0; |
|---|
| 180 | return Wx::gettext('Line') if $c == 1; |
|---|
| 181 | |
|---|
| 182 | die "invalid value '$c'"; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | =pod |
|---|
| 186 | |
|---|
| 187 | =head3 relocale |
|---|
| 188 | |
|---|
| 189 | $self->relocale(); |
|---|
| 190 | |
|---|
| 191 | Reset the column headings if locales are changed, |
|---|
| 192 | |
|---|
| 193 | =cut |
|---|
| 194 | |
|---|
| 195 | sub relocale { |
|---|
| 196 | my $self = shift; |
|---|
| 197 | |
|---|
| 198 | for my $i ( 0 .. 1 ) { |
|---|
| 199 | my $col = $self->GetColumn($i); |
|---|
| 200 | $col->SetText( _get_title($i) ); |
|---|
| 201 | $self->SetColumn( $i, $col ); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | return; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | =pod |
|---|
| 208 | |
|---|
| 209 | =head3 on_right_down |
|---|
| 210 | |
|---|
| 211 | Called when the user presses a right click or a context menu key (on win32) |
|---|
| 212 | |
|---|
| 213 | =cut |
|---|
| 214 | |
|---|
| 215 | sub on_right_down { |
|---|
| 216 | my ( $self, $event ) = @_; |
|---|
| 217 | |
|---|
| 218 | return if $self->GetItemCount == 0; |
|---|
| 219 | |
|---|
| 220 | # Create the popup menu |
|---|
| 221 | my $menu = Wx::Menu->new; |
|---|
| 222 | |
|---|
| 223 | if ( $self->GetFirstSelected != -1 ) { |
|---|
| 224 | |
|---|
| 225 | # Copy selected |
|---|
| 226 | Wx::Event::EVT_MENU( |
|---|
| 227 | $self, |
|---|
| 228 | $menu->Append( -1, Wx::gettext("Copy &Selected") ), |
|---|
| 229 | sub { |
|---|
| 230 | |
|---|
| 231 | # Get selected message |
|---|
| 232 | my $msg = ''; |
|---|
| 233 | my $selection = $self->GetFirstSelected; |
|---|
| 234 | if ( $selection != -1 ) { |
|---|
| 235 | my $text = $self->GetItem( $selection, 1 )->GetText || ''; |
|---|
| 236 | $msg = "$text\n"; |
|---|
| 237 | |
|---|
| 238 | # And copy it to clipboard |
|---|
| 239 | if ( ( length $msg > 0 ) and Wx::wxTheClipboard->Open() ) { |
|---|
| 240 | Wx::wxTheClipboard->SetData( Wx::TextDataObject->new($msg) ); |
|---|
| 241 | Wx::wxTheClipboard->Close(); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | ); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | # Copy all |
|---|
| 249 | Wx::Event::EVT_MENU( |
|---|
| 250 | $self, |
|---|
| 251 | $menu->Append( -1, Wx::gettext("Copy &All") ), |
|---|
| 252 | sub { |
|---|
| 253 | |
|---|
| 254 | # Append messages in one string |
|---|
| 255 | my $msg = ''; |
|---|
| 256 | foreach my $i ( 0 .. $self->GetItemCount - 1 ) { |
|---|
| 257 | |
|---|
| 258 | my $text = $self->GetItem( $i, 0 )->GetText || ''; |
|---|
| 259 | $msg .= "$text\n"; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | # And copy it to clipboard |
|---|
| 263 | if ( ( length $msg > 0 ) and Wx::wxTheClipboard->Open() ) { |
|---|
| 264 | Wx::wxTheClipboard->SetData( Wx::TextDataObject->new($msg) ); |
|---|
| 265 | Wx::wxTheClipboard->Close(); |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | ); |
|---|
| 269 | |
|---|
| 270 | if ( $event->isa('Wx::MouseEvent') ) { |
|---|
| 271 | $self->PopupMenu( $menu, $event->GetX, $event->GetY ); |
|---|
| 272 | } else { #Wx::CommandEvent |
|---|
| 273 | $self->PopupMenu( $menu, 50, 50 ); # TODO better location |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | =pod |
|---|
| 278 | |
|---|
| 279 | =head3 populate_list |
|---|
| 280 | |
|---|
| 281 | my $entry->[0]->{lineNumber} = 10; |
|---|
| 282 | $entry->[0]->{line} = ' this is at line 10'; |
|---|
| 283 | $self->populate_list($entry); |
|---|
| 284 | |
|---|
| 285 | Populate the list with the line number and text. |
|---|
| 286 | |
|---|
| 287 | =cut |
|---|
| 288 | # populates the list |
|---|
| 289 | |
|---|
| 290 | sub populate_list |
|---|
| 291 | { |
|---|
| 292 | my ($self, $lines) = @_; |
|---|
| 293 | |
|---|
| 294 | for (my $i = (scalar(@$lines)-1); $i >= 0; $i--) |
|---|
| 295 | { |
|---|
| 296 | |
|---|
| 297 | my $item = $self->InsertStringItem(0,$lines->[$i]->{lineNumber}); |
|---|
| 298 | $self->SetItem($item,1, $lines->[$i]->{line}); |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | 1; |
|---|
| 304 | |
|---|
| 305 | # Copyright 2008-2009 The Padre development team as listed in Padre.pm. |
|---|
| 306 | # LICENSE |
|---|
| 307 | # This program is free software; you can redistribute it and/or |
|---|
| 308 | # modify it under the same terms as Perl 5 itself. |
|---|