blib/lib/ChordPro/Wx/Editor.pm untested

filter: Statementstmt 0.0% 0 / 195 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 43 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 318 CC 13 SCAR 15.1
CC
13
Cov
0%
SetViewLineNumbers
12.0
style_text
12.0
prepare_annotations
12.0
CRAP 4.5
1 #! perl
2  
3 use v5.26;
4 use feature 'signatures';
5 no warnings 'experimental::signatures';
6 use utf8;
7  
8 use Wx ':everything';
9  
10 package ChordPro::Wx::Editor;
11  
12 use ChordPro::Wx::Config;
13 use ChordPro::Wx::Utils;
14  
15 sub new( $class, $parent, $id ) {
16  
17 my $widget;
18 $::options->{stc} //= 1;
19 if ( $::options->{stc} && eval { require Wx::STC; 1 } ) {
20 # if ( $::options->{stc} && eval { require Wx::Scintilla; 1 } ) {
21 $widget = Wx::StyledTextCtrl->new($parent);
22 # $widget = Wx::Scintilla::TextCtrl->new($parent);
23 $state{have_stc} = 1;
24 return bless $widget => 'ChordPro::Wx::STCEditor';
25 }
26  
27 # Emergency fallback.
28 $state{have_stc} = 0;
29 ChordPro::Wx::TextEditor->new($parent);
30 }
31  
32 package ChordPro::Wx::STCEditor;
33  
34 use parent qw( -norequire Wx::StyledTextCtrl );
35 #use parent qw( -norequire Wx::Scintilla::TextCtrl );
36  
37 use Wx ':everything';
38 use ChordPro::Wx::Config;
39 use ChordPro::Wx::Utils;
40  
41  
42 sub refresh( $self, $prefs = undef ) {
43 my $stc = $self;
44  
45 $prefs //= \%preferences;
46  
47 # RTI loading is currently too slow.
48 # $state{rti} = ChordPro::runtime_info();
49 $state{rti}->{directive_abbrevs} = ChordPro::Song::_directive_abbrevs();
50  
51 $stc->SetLexer(wxSTC_LEX_CONTAINER);
52 $stc->SetKeyWords(0,
53 [qw( album arranger artist capo chord chorus
54 column_break columns comment comment_box
55 comment_italic composer copyright define
56 diagrams duration end_of_bridge end_of_chorus
57 end_of_grid end_of_tab end_of_verse grid
58 highlight image key lyricist meta new_page
59 new_physical_page new_song no_grid pagesize
60 pagetype sorttitle start_of_bridge
61 start_of_chorus start_of_grid start_of_tab
62 start_of_verse subtitle tempo time title
63 titles transpose year )
64 ]);
65  
66 Wx::Event::EVT_STC_STYLENEEDED( $stc, wxID_ANY,
67 sub { OnStyleNeeded($self, $_[1]) } );
68 Wx::Event::EVT_STC_CHANGE( $stc, wxID_ANY,
69 sub { OnChanged($self, $_[1]) } );
70 $self->SetModEventMask(0x01|0x02|0x10);
71 my $theme = $state{editortheme};
72 my $c = $prefs->{editcolour}{$theme};
73 my $fg = Wx::Colour->new($c->{fg});
74 my $bg = Wx::Colour->new($c->{bg});
75  
76 $stc->SetBackgroundColour($bg);
77 $stc->SetCaretForeground($fg);
78  
79 $stc->StyleSetForeground( wxSTC_STYLE_DEFAULT, $fg );
80 $stc->StyleSetBackground( wxSTC_STYLE_DEFAULT, $bg );
81 $stc->StyleClearAll;
82  
83 # 0 - basic
84 $stc->StyleSetForeground( 0, $fg );
85 $stc->StyleSetBackground( 0, $bg );
86 # 1 - comments (grey)
87 $stc->StyleSetForeground( 1, Wx::Colour->new($c->{s1}) );
88 $stc->StyleSetBackground( 1, $bg );
89 # 2 - Keywords (grey)
90 $stc->StyleSetForeground( 2, Wx::Colour->new($c->{s2}) );
91 $stc->StyleSetBackground( 2, $bg );
92 # 3 - Brackets (grey)
93 $stc->StyleSetForeground( 3, Wx::Colour->new($c->{s3}) );
94 $stc->StyleSetBackground( 3, $bg );
95 # 4 - Chords (red)
96 $stc->StyleSetForeground( 4, Wx::Colour->new($c->{s4}) );
97 $stc->StyleSetBackground( 4, $bg );
98 # 5 - Directives (blue, same as status label colour)
99 $stc->StyleSetForeground( 5, Wx::Colour->new($c->{s5}) );
100 $stc->StyleSetBackground( 5, $bg );
101 # 6 - Directive arguments (orange, same as toolbar icon colour)
102 $stc->StyleSetForeground( 6, Wx::Colour->new($c->{s6}) );
103 $stc->StyleSetBackground( 6, $bg );
104 # 7 - Errors
105 $stc->StyleSetForeground( 7, $fg );
106 $stc->StyleSetBackground( 7, wxRED );
107  
108 # For linenumbers.
109 $stc->StyleSetForeground( wxSTC_STYLE_LINENUMBER,
110 Wx::Colour->new( $c->{numfg} ) );
111 $stc->StyleSetBackground( wxSTC_STYLE_LINENUMBER,
112 Wx::Colour->new( $c->{numbg} ) );
113  
114 # For annotations.
115 $self->{astyle} //= 1 + wxSTC_STYLE_LASTPREDEFINED;
116 $stc->StyleSetBackground( $self->{astyle}, Wx::Colour->new($c->{annbg}) );
117 $stc->StyleSetForeground( $self->{astyle}, Wx::Colour->new($c->{annfg}) );
118  
119 $stc->SetFont( Wx::Font->new($prefs->{editfont}) );
120  
121 # Wrapping.
122 if ( $prefs->{editorwrap} ) {
123 $stc->SetWrapMode(3); # wxSTC_WRAP_WHITESPACE );
124 $stc->SetWrapStartIndent( $prefs->{editorwrapindent} );
125 }
126 else {
127 $stc->SetWrapMode(0); # wxSTC_WRAP_NONE );
128 }
129  
130 $self->style_text;
131 # Expert...
132 $stc->SetViewEOL( $state{vieweol} );
133 $stc->SetViewWhiteSpace( $state{viewws} );
134 $stc->SetViewLineNumbers(1);
135  
136 # Free Ctrl-Shift-U for iBus input (doesn't work).
137 # $stc->CmdKeyClear( ord('U'), 3 ); # Ctrl-Shift-U
138 }
139  
140 sub SetViewLineNumbers( $self, $b ) {
141 $self->SetMarginWidth( 0, $b ? 40 : 0 ); # TODO length
142 if ( $self->can("SetMarginBackground") ) { # wxPerl 3.005
143 $self->SetMarginType( 1, wxSTC_MARGIN_SYMBOL|wxSTC_MARGIN_COLOUR );
144 $self->SetMarginWidth( 1, 3 );
145 my $theme = $state{editortheme};
146 my $c = $preferences{editcolour}{$theme};
147 my $bg = Wx::Colour->new($c->{bg});
148 $self->SetMarginBackground( 1, $bg );
149 }
150 else {
151 $self->SetMarginWidth( 1, 0 );
152 }
153 }
154  
155 sub style_text( $self ) {
156 my $stc = $self;
157  
158 # Scintilla uses byte indices.
159 require Encode;
160 my $text = Encode::encode_utf8($stc->GetText);
161  
162 my $style = sub {
163 my ( $re, @styles ) = @_;
164 pos($text) = 0;
165 while ( $text =~ m/$re/g ) {
166 my @s = @styles;
167 die("!!! ", scalar(@{^CAPTURE}), ' ', scalar(@s)) unless @s == @{^CAPTURE};
168 my $end = pos($text);
169 my $start = $end - length($&);
170 my $group = 0;
171 while ( $start < $end ) {
172 my $l = length(${^CAPTURE[$group++]});
173 if ( $Wx::VERSION < 3.006 ) {
174 $stc->StartStyling( $start, 0 );
175 }
176 else {
177 $stc->StartStyling( $start );
178 }
179 $stc->SetStyling( $l, shift(@s) );
180 $start += $l;
181 }
182 }
183 };
184  
185 # Comments/
186 $style->( qr/^(#.*)/m, 1 );
187 # Directives.
188 $style->( qr/^([ \t]*)(\{)([-\w!]+)(.*)(\})/m, 7, 3, 5, 6, 3 );
189 $style->( qr/^([ \t]*)(\{)([-\w!]+)([: ])(.*)(\})/m, 7, 3, 5, 3, 6, 3 );
190 # Chords.
191 $style->( qr/(\[)([^\[\]\s]*)(\])/m, 3, 4, 3 );
192  
193 # For later. Much later...
194 #$self->MarkerDefine($_,$_,wxGREEN,wxBLACK) for 0..31;
195 #$self->MarkerAdd( 6, 8 );
196 #$self->MarkerAdd( 7, 9 );
197 #$self->MarkerAdd( 8, 10 );
198 }
199  
200 sub prepare_annotations( $self ) {
201  
202 return unless $state{have_stc};
203 my $stc = $self;
204  
205 $stc->AnnotationClearAll;
206 $stc->AnnotationSetVisible(wxSTC_ANNOTATION_BOXED);
207  
208 if ( $stc->can("StyleGetSizeFractional") ) { # Wx 3.002
209 $stc->StyleSetSizeFractional # size * 100
210 ( $self->{astyle},
211 ( $stc->StyleGetSizeFractional
212 ( wxSTC_STYLE_DEFAULT ) * 4 ) / 5 );
213 }
214  
215 return 1;
216 }
217  
218 sub add_annotation( $self, $line, $message ) {
219  
220 return unless $state{have_stc};
221 my $stc = $self;
222  
223 $stc->AnnotationSetText( $line, $message );
224 $stc->AnnotationSetStyle( $line, $self->{astyle} );
225 }
226  
227 unless ( __PACKAGE__->can("IsModified") ) {
228 *IsModified = sub($self) {
229 $self->{_modified} || $self->CanUndo;
230 };
231 }
232 unless ( __PACKAGE__->can("DiscardEdits") ) {
233 *DiscardEdits = sub($self) {
234 $self->EmptyUndoBuffer;
235 $self->{_modified} = 0;
236 };
237 }
238  
239 sub SetModified( $self, $mod ) {
240 if ( $mod ) {
241 $self->{_modified} = 1;
242 }
243 else {
244 $self->DiscardEdits;
245 }
246 }
247  
248 sub SetFont( $self, $font ) {
249 die("XXX\n") unless $font->IsOk;
250 $self->StyleSetFont( $_, $font ) for 0..7;
251 $self->{font} = $font;
252 }
253  
254 sub GetFont( $self ) {
255 $self->{font} // $self->StyleGetFont(0);
256 }
257  
258 sub OSXDisableAllSmartSubstitutions( $self ) {
259 }
260  
261 sub OnStyleNeeded( $self, $event ) { # scintilla
262 $self->style_text;
263 }
264  
265 sub OnChanged( $self, $event ) { # scintilla
266 $state{editchanged}++;
267 }
268  
269 sub Replace( $self, $from=-1, $to=-1, $text="" ) {
270 # We will only call this to replace the selection.
271 $self->ReplaceSelection($text);
272 }
273  
274 # STC::Remove is missing...
275 sub Remove( $self, $from=-1, $to=-1 ) {
276 $self->Replace( $from, $to, "" );
277 }
278  
279 ################ Methods ################
280  
281 package ChordPro::Wx::TextEditor;
282  
283 use parent qw( -norequire Wx::TextCtrl );
284  
285 use Wx ':everything';
286 use ChordPro::Files;
287 use ChordPro::Wx::Config;
288 use ChordPro::Wx::Utils;
289  
290 sub new( $class, $parent, $id=undef ) {
291  
292 my $self = $class->SUPER::new( $parent, wxID_ANY, "",
293 wxDefaultPosition, wxDefaultSize,
294 wxHSCROLL|wxTE_MULTILINE );
295  
296 return $self;
297 }
298  
299 sub refresh( $self, $prefs = undef ) {
300 my $ctrl = $self;
301 $prefs //= \%preferences;
302  
303 my $mod = $self->IsModified;
304  
305 # TextCtrl only supports background colour and font.
306 my $theme = $prefs->{editortheme};
307 my $c = $prefs->{editcolour}{$theme};
308 my $bgcol = Wx::Colour->new( $c->{bg} );
309 my $fgcol = Wx::Colour->new( $c->{fg} );
310 $ctrl->SetBackgroundColour($bgcol);
311 $ctrl->SetStyle( 0, $ctrl->GetLastPosition,
312 Wx::TextAttr->new( $fgcol, $bgcol ) );
313 $ctrl->SetFont( Wx::Font->new($prefs->{editfont}) );
314  
315 $ctrl->SetModified($mod);
316 }
317  
318 sub AddText( $self, $text ) {
319 $self->WriteText($text);
320 }
321  
322 sub GetLineCount( $self ) {
323 $self->GetNumberOfLines;
324 }
325  
326 sub GetSelectedText( $self ) {
327 $self->GetStringSelection;
328 }
329  
330 sub GetText( $self ) {
331 $self->GetValue;
332 }
333  
334 sub SetText( $self, $text ) {
335 $self->SetValue($text);
336 }
337  
338 sub SetColour( $self, $colour ) {
339 $self->SetStyle( 0, $self->GetLastPosition,
340 Wx::TextAttr->new( Wx::Colour->new($colour) ) );
341 }
342  
343 sub SetViewLineNumbers( $self, $b ) {
344 }
345  
346 sub EmptyUndoBuffer($self) {
347 }
348  
349 sub OSXDisableAllSmartSubstitutions( $self ) {
350 return unless is_macos;
351 $self->SUPER::OSXDisableAllSmartSubstitutions;
352 }
353  
354 ################
355  
356 1;