blib/lib/Padre/Plugin/Encode.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Padre::Plugin::Encode;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use version; our $VERSION = qv('0.1.3');
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use base 'Padre::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use Wx ':everything';
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Wx::Event ':everything';
12 use Wx::Locale qw(:default);
13  
14 use Padre::Wx ();
15 use Padre::Wx::Dialog ();
16 use Padre::Locale ();
17  
18 my @ENCODINGS = qw(
19 cp932
20 cp949
21 euc-jp
22 euc-kr
23 shift-jis
24 utf-8
25 );
26  
27 sub padre_interfaces {
28 'Padre::Plugin' => '0.24',
29 }
30  
31 sub menu_plugins_simple {
32 'Convert Encoding' => [
33 Wx::gettext('Encode document to System Default') => \&encode_document_to_system_default,
34 Wx::gettext('Encode document to utf-8') => \&encode_document_to_utf8,
35 Wx::gettext('Encode document to ...') => \&encode_document_to,
36 ];
37 }
38  
39 sub encode_document_to_system_default {
40 my ( $window, $event ) = @_;
41  
42 my $doc = $window->current->document;
43 $doc->{encoding} = Padre::Locale::encoding_system_default || 'utf-8';
44 $doc->save_file if $doc->filename;
45 $window->refresh;
46  
47 my $string = 'Encode document to System Default('.$doc->{encoding}.')';
48 my $output_panel = $window->{gui}->{output_panel};
49 $output_panel->clear;
50 $output_panel->AppendText( $string . $/ );
51 }
52  
53 sub encode_document_to_utf8 {
54 my ( $window, $event ) = @_;
55  
56 my $doc = $window->current->document;
57 $doc->{encoding} = 'utf-8';
58 $doc->save_file if $doc->filename;
59 $window->refresh;
60  
61 my $string = 'Encode document to '.$doc->{encoding};
62 my $output_panel = $window->{gui}->{output_panel};
63 $output_panel->clear;
64 $output_panel->AppendText( $string . $/ );
65 }
66  
67 sub encode_document_to {
68 my ( $window, $event ) = @_;
69  
70 my @layout = (
71 [
72 [ 'Wx::StaticText', undef, Wx::gettext('Encode to:') ],
73 [ 'Wx::ComboBox', '_encoding_', 'utf-8', \@ENCODINGS, wxCB_READONLY ],
74 ],
75 [
76 [ 'Wx::Button', '_ok_', Wx::wxID_OK ],
77 [ 'Wx::Button', '_cancel_', Wx::wxID_CANCEL ],
78 ],
79 );
80  
81 my $dialog = Padre::Wx::Dialog->new(
82 parent => $window,
83 title => gettext(
84 "Encode document to..."
85 ),
86 layout => \@layout,
87 width => [ 100, 200 ],
88 bottom => 20,
89 );
90 $dialog->{_widgets_}{_ok_}->SetDefault;
91 Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}{_ok_}, \&ok_clicked );
92 Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}{_cancel_}, \&cancel_clicked );
93  
94 $dialog->{_widgets_}{_encoding_}->SetFocus;
95 $dialog->Show(1);
96  
97 return 1;
98 }
99  
100 sub cancel_clicked {
101 my ( $dialog, $event ) = @_;
102  
103 $dialog->Destroy;
104 }
105  
106 sub ok_clicked {
107 my ( $dialog, $event ) = @_;
108  
109 my $window = $dialog->GetParent;
110 my $data = $dialog->get_data;
111 $dialog->Destroy;
112  
113 my $doc = $window->current->document;
114 $doc->{encoding} = $data->{_encoding_};
115 $doc->save_file if $doc->filename;
116 $window->refresh;
117  
118 my $string = 'Encode document to '.$doc->{encoding};
119 my $output_panel = $window->{gui}->{output_panel};
120 $output_panel->clear;
121 $output_panel->AppendText( $string . $/ );
122 }
123  
124 1; # Magic true value required at end of module
125 __END__
↑ Summary