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

filter: Statementstmt 0.0% 0 / 211 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 342 CC 19 SCAR 35.9
CC
19
Cov
0%
setup_menubar
132.0
restorewinpos
20.0
update_menubar
6.0
CRAP 36.3
1 #! perl
2  
3 package ChordPro::Wx::Utils;
4  
5 use v5.26;
6 use utf8;
7 use Carp;
8 use feature qw( signatures );
9 no warnings "experimental::signatures";
10  
11 use Exporter 'import';
12 our @EXPORT;
13  
14 use Wx ':everything';
15 use Wx::Locale gettext => '_T';
16 use ChordPro::Files;
17  
18 ################ Constants ################
19  
20 # Constants not (yet) in this version of Wx, and constants specific to us.
21  
22 my %const =
23 ( wxID_FULLSCREEN => Wx::NewId(), # for menu
24 wxICON_NONE => 0x00040000, # unused for now
25  
26 $Wx::VERSION < 3.006 ?
27 ( wxSTC_MARGIN_COLOUR => 6,
28 ) : (),
29  
30 $Wx::VERSION < 3.004 ?
31 ( wxEVT_COMMAND_FILEPICKER_CHANGED => 0x000027b8,
32 wxEVT_COMMAND_DIRPICKER_CHANGED => 0x000027b9,
33 ) : (),
34  
35 $Wx::VERSION < 3.003 ?
36 ( wxELLIPSIZE_FLAGS_DEFAULT => 3,
37 wxELLIPSIZE_NONE => 0,
38 wxELLIPSIZE_START => 1,
39 wxELLIPSIZE_MIDDLE => 2,
40 wxELLIPSIZE_END => 3,
41 ) : (),
42  
43 $Wx::VERSION < 3.000 ?
44 ( wxEXEC_HIDE_CONSOLE => 0x00000020,
45 # wxID_EXECUTE
46 # wxDIRP_SMALL
47 # wxFLP_SMALL
48 # wxPB_SMALL
49 # wxRESERVE_SPACE_EVEN_IF_HIDDEN
50 ) : (),
51  
52 );
53  
54 no strict 'refs';
55  
56 while ( my ( $sub, $value ) = each %const ) {
57 *$sub = sub () { $value };
58 push( @EXPORT, $sub );
59 }
60  
61 use strict 'refs';
62  
63 { no feature 'signatures';
64 sub Wx::Event::EVT_STC_CLIPBOARD_PASTE($$$) {
65 $_[0]->Connect( $_[1], -1, Wx::wxEVT_STC_AUTOCOMP_CHAR_DELETED()+3, $_[2] );
66 }
67 }
68  
69 ################ ################
70  
71 # Create / update menu bar.
72 #
73 # setup_menubar is called by main ctor, update_menubar by the refresh
74 # methods of main and panels.
75 # This is intended to be called by a panel, but the actual menu bar is
76 # attached to the top level frame. The callbacks are routed to the
77 # methods in the panel, if possible.
78  
79 use constant
80 { M_ALL => 0xff,
81 M_MAIN => 0x01,
82 M_EDITOR => 0x02,
83 M_SONGBOOK => 0x04,
84 };
85  
86 push( @EXPORT, qw( M_MAIN M_EDITOR M_SONGBOOK ) );
87  
88 my @swingers;
89  
90 sub update_menubar( $self, $sel ) {
91 die unless @swingers;
92  
93 for ( @swingers ) {
94 my ( $mi, $mask ) = @$_;
95 $mi->Enable( $mask & $sel );
96 }
97 }
98  
99 sub setup_menubar( $self ) {
100  
101 state $expert = $ChordPro::Wx::Config::state{preferences}{expert};
102 state $advanced = $ChordPro::Wx::Config::state{preferences}{advanced};
103  
104 state $ctl =
105 [ [ wxID_FILE,
106 [ [ wxID_HOME, M_EDITOR|M_SONGBOOK, "Start Screen",
107 "Return to the Start Screen.", "OnStart" ],
108 [],
109 [ wxID_NEW, M_ALL, "",
110 "Create another ChordPro document", "OnNew" ],
111 [ wxID_OPEN, M_ALL, "",
112 "Open an existing ChordPro document", "OnOpen" ],
113 [],
114 [ wxID_SAVE, M_EDITOR, "",
115 "Save the current ChordPro file", "OnSave" ],
116 [ wxID_SAVEAS, M_EDITOR, "Save &As...\tShift-Ctrl-S",
117 "Save under a different name", "OnSaveAs" ],
118 [],
119 [ wxID_ANY, M_EDITOR|M_SONGBOOK, "Export to PDF...",
120 "Save the preview to a PDF", "OnPreviewSave" ],
121 [],
122 [ wxID_EXIT, M_ALL, "",
123 "Close Window and Exit", "OnClose" ],
124 ]
125 ],
126 [ wxID_EDIT,
127 [ [ wxID_UNDO, M_EDITOR, "",
128 "Undo edits", "OnUndo" ],
129 [ wxID_REDO, M_EDITOR, "",
130 "Redo edits", "OnRedo" ],
131 [],
132 [ wxID_CUT, M_EDITOR|M_SONGBOOK, "", "OnCut" ],
133 [ wxID_COPY, M_EDITOR|M_SONGBOOK, "", "OnCopy" ],
134 [ wxID_PASTE, M_EDITOR|M_SONGBOOK, "", "OnPaste" ],
135 [ wxID_DELETE, M_EDITOR|M_SONGBOOK, "", "OnDelete" ],
136 [],
137 [ wxID_ANY, M_EDITOR,
138 "Clear Editor Warnings",
139 "Clear the warning messages in the edit window.",
140 "OnClearDiagnosticFlags" ],
141 [],
142 [ wxID_ANY, M_EDITOR,
143 "Convert Text to ChordPro format\tShift-Ctrl-A",
144 "Convert a text document (chords above the lyrics) to ChordPro format",
145 "OnA2Crd" ],
146 $ENV{VISUAL} ?
147 [ wxID_ANY, M_EDITOR,
148 "Use external editor\tShift-Ctrl-X",
149 "Use an external editing program to modify the song",
150 "OnExternalEditor" ] : (),
151 [],
152 [ wxID_PREFERENCES, M_ALL, "Settings...\tCtrl-R",
153 "Open the Settings dialog", "OnPreferences" ],
154 ]
155 ],
156 [ wxID_ANY, M_EDITOR|M_SONGBOOK, "View",
157 [ [ wxID_ANY, M_EDITOR|M_SONGBOOK, "Preview Panel",
158 "Hide or show the preview pane", 1, "OnWindowPreview" ],
159 [ wxID_ANY, M_EDITOR|M_SONGBOOK, "Messages Panel",
160 "Hide or show the messages pane", 1, "OnWindowMessages" ],
161 $expert ?
162 ( [],
163 [ wxID_ANY, M_ALL, "View Line Endings",
164 "Make line endings visible for debugging", 1,
165 "OnExpertLineEndings" ],
166 [ wxID_ANY, M_ALL, "View White Space",
167 "Make white space visible for debugging", 1,
168 "OnExpertWhiteSpace" ],
169 ) : (),
170 [],
171 [ wxID_FULLSCREEN(), M_ALL, "Zoom\tShift-Ctrl-Z",
172 "OnMaximize" ],
173 ]
174 ],
175 [ wxID_ANY, M_EDITOR, "&Insert",
176 [ [ wxID_ANY, M_EDITOR, "{&title}",
177 "Insert a {title} directive", "OnInsertTitle" ],
178 [ wxID_ANY, M_EDITOR, "{&subtitle}",
179 "Insert a {subtitle} directive", "OnInsertSubtitle" ],
180 [],
181 [ wxID_ANY, M_EDITOR, "&Verse section",
182 "Insert start/end of verse directive.", "OnInsertVerse" ],
183 [ wxID_ANY, M_EDITOR, "&Chorus section",
184 "Insert start/end of chorus directive.", "OnInsertChorus" ],
185 [ wxID_ANY, M_EDITOR, "&Tab section",
186 "Insert start/end of tab directive.", "OnInsertTab" ],
187 [ wxID_ANY, M_EDITOR, "&Grid section",
188 "Insert start/end of grid directive.", "OnInsertGrid" ],
189 [],
190 [ wxID_ANY, M_EDITOR, "Special symbol",
191 "Insert a special symbol.", "OnInsertSymbol" ],
192 ]
193 ],
194 [ wxID_ANY, M_EDITOR|M_SONGBOOK, "Tasks",
195 [ [ wxID_ANY, M_EDITOR|M_SONGBOOK, "Default Preview\tCtrl-P",
196 "Preview with default formatting", "OnPreview" ],
197 [ wxID_ANY, M_EDITOR|M_SONGBOOK, "No Chord Diagrams",
198 "Preview without chord diagrams", "OnPreviewNoDiagrams" ],
199 [ wxID_ANY, M_EDITOR|M_SONGBOOK, "Lyrics Only",
200 "Preview with just the lyrics", "OnPreviewLyricsOnly" ],
201 [ wxID_ANY, M_EDITOR|M_SONGBOOK, "More...",
202 "Transpose, transcode, and more", "OnPreviewMore" ],
203 [],
204 ]
205 ],
206 [ wxID_HELP,
207 [ [ wxID_ANY, M_ALL, "ChordPro File Format",
208 "Help about the ChordPro file format", "OnHelp_ChordPro" ],
209 [ wxID_ANY, M_ALL, "ChordPro Configuration Files",
210 "Help about the configuration files", "OnHelp_Config" ],
211 [],
212 [ wxID_ANY, M_ALL, "Enable Debugging Info in PDF",
213 "Add sources and configuration files to the PDF for debugging", 1,
214 "OnHelp_DebugInfo" ],
215 [],
216 [ wxID_ABOUT, M_ALL, "About ChordPro",
217 "About WxChordPro", "OnAbout" ],
218 ]
219 ]
220 ];
221  
222 my $target = $Wx::wxTheApp->GetTopWindow;
223  
224 my $mb = Wx::MenuBar->new;
225  
226 for ( @$ctl ) {
227  
228 # [ wxID_FILE, [ ... ] ]
229  
230 my @data = @$_;
231 my $id = shift(@data);
232 my $menu = pop(@data);
233 my $sels = shift(@data) // M_ALL;
234 my $text = shift(@data);
235  
236 $id = Wx::NewId if $id < 0;
237  
238 my $m = Wx::Menu->new;
239 for my $item ( @$menu ) {
240  
241 if ( !@$item ) {
242 # []
243 $m->AppendSeparator;
244 next;
245 }
246  
247 # [ wxID_NEW, "", "Create new", ..., "OnNew" ],
248  
249 my @data = @$item;
250 my $id = shift(@data);
251 my $cb = pop(@data);
252 my $sels = shift(@data) // M_ALL;
253 my $text = shift(@data);
254 my $tip = shift(@data);
255  
256 $id = Wx::NewId if $id < 0;
257  
258 my $mi = $m->Append( $id,
259 _T($text // Wx::GetStockLabel($id)),
260 $tip ? _T($tip) : "", @data );
261 push( @swingers, [ $mi, $sels ] ) unless $sels == M_ALL;
262  
263 if ( my $code = $target->can($cb) ) {
264 Wx::Event::EVT_MENU( $target, $id, $code );
265 }
266 else {
267 # $self->log("W", "No callback for $cb" );
268 Wx::Event::EVT_MENU
269 ( $target, $id,
270 sub {
271 if ( my $code = $ChordPro::Wx::Config::state{panel}->can($cb) ) {
272 &$code( $ChordPro::Wx::Config::state{panel}, $_[1] );
273 }
274 else {
275 $self->log("E", "No callback for $cb" );
276 }
277 } );
278 }
279 }
280  
281 # Add menu to menu bar.
282 $mb->Append( $m, _T($text // Wx::GetStockLabel($id)) );
283 }
284  
285 # Append the tasks.
286 my $menu = $mb->FindMenu("Tasks");
287 $menu = $mb->GetMenu($menu);
288  
289 my $tasks = $ChordPro::Wx::Config::state{presets}{tasks};
290 $menu->AppendSeparator if %{$tasks};
291  
292 while ( my ($title,$info) = each %{$tasks} ) {
293 my $file = $info->{file};
294 my $id = Wx::NewId();
295 # Append to the menu.
296 my $mi = $menu->Append( $id, $title, _T("Custom task: ").$title );
297 Wx::Event::EVT_MENU
298 ( $self, $id,
299 sub { $ChordPro::Wx::Config::state{panel}->preview( [ "--config", $file ] ) }
300 );
301 push( @swingers, [ $mi, M_ALL & ~M_MAIN ] );
302 }
303  
304 # Add menu bar.
305 $target->SetMenuBar($mb);
306  
307 return $mb;
308 }
309  
310 push( @EXPORT, "setup_menubar", "update_menubar" );
311  
312 ################ ################
313  
314 sub savewinpos( $win, $name ) {
315 return unless $name eq "main";
316 $ChordPro::Wx::Config::state{windows}->{$name} =
317 join( " ", $win->GetPositionXY, $win->GetSizeWH );
318 }
319  
320 sub restorewinpos( $win, $name ) {
321 return unless $name eq "main";
322 $win = $Wx::wxTheApp->GetTopWindow;
323  
324 my $t = $ChordPro::Wx::Config::state{windows}->{$name};
325 if ( $t ) {
326 my @a = split( ' ', $t );
327 if ( is_msw || is_macos ) {
328 $win->SetSizeXYWHF( $a[0],$a[1],$a[2],$a[3], 0 );
329 }
330 else {
331 # Linux WM usually prevent placement.
332 $win->SetSize( $a[2],$a[3] );
333 }
334 }
335 }
336  
337 push( @EXPORT, 'savewinpos', 'restorewinpos' );
338  
339 ################ ################
340  
341 sub panels() {
342 my @panels = qw( p_editor p_sbexport );
343 wantarray ? @panels : \@panels;
344 }
345  
346 push( @EXPORT, 'panels' );
347  
348 ################ ################
349  
350 sub ellipsize( $widget, %opts ) {
351 my $text = $opts{text} // $widget->GetText;
352 my $home = ChordPro::Paths->get->home;
353 $text =~ s/^\Q$home\E\/*/~\//;
354 my $width = ($widget->GetSizeWH)[0];
355  
356 $text = Wx::Control::Ellipsize( $text, Wx::WindowDC->new($widget),
357 $opts{type} // wxELLIPSIZE_END(),
358 $width-10, wxELLIPSIZE_FLAGS_DEFAULT() )
359 if Wx::Control->can("Ellipsize");
360  
361 # Change w/o triggering a EVT_TEXT event.
362 $widget->ChangeValue($text);
363 }
364  
365 push( @EXPORT, "ellipsize" );
366  
367 ################ ################
368  
369 sub kbdkey( $key ) {
370 $key =~ s/Shift-/⇧/;
371 $key =~ s/Alt-/⎇/;
372 $key =~ s/Option-/⌥/;
373 my $c = is_macos ? "⌘" : "Ctrl-";
374 $key =~ s/Ctrl-/$c/;
375 return $key;
376 }
377  
378 push( @EXPORT, "kbdkey" );
379  
380 use Storable();
381  
382 ################ ################
383  
384 sub clone( $struct ) { Storable::dclone($struct) }
385  
386 push( @EXPORT, "clone" );
387  
388 ################ ################
389  
390 sub Wx::ColourPickerCtrl::GetAsHTML( $self ) {
391 $self->GetColour->GetAsString(wxC2S_HTML_SYNTAX);
392 }
393  
394 ################ ################
395  
396 # Override Wx::Bitmap to use resource search.
397 unless ( $::wxbitmapnew ) {
398 $::wxbitmapnew = \&Wx::Bitmap::new;
399 no warnings 'redefine';
400 use File::Basename;
401 *Wx::Bitmap::new = sub {
402 # Only handle Wx::Bitmap->new(file, type) case.
403 goto &$::wxbitmapnew if @_ != 3 || fs_test( f => $_[1] );
404 my ($self, @rest) = @_;
405 $rest[0] = ChordPro::Paths->get->findres( basename($rest[0]),
406 class => "icons" );
407 $rest[0] ||= ChordPro::Paths->get->findres( "missing.png",
408 class => "icons" );
409 $::wxbitmapnew->($self, @rest);
410 };
411 }
412  
413 ################ ################
414  
415 sub has_appearance() {
416 Wx::SystemSettings->can("GetAppearance");
417 }
418  
419 push( @EXPORT, 'has_appearance' );
420  
421 ################ ################