| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.26; | ||
| 4 | use Object::Pad; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | role ChordPro::Wx::PanelRole; | ||
| 8 | |||
| 9 | use Wx qw[:everything]; | ||
| 10 | use Wx::Locale gettext => '_T'; | ||
| 11 | |||
| 12 | use ChordPro::Files; | ||
| 13 | use ChordPro::Paths; | ||
| 14 | use ChordPro::Utils qw( demarkup plural :xp ); | ||
| 15 | use ChordPro::Wx::Config; | ||
| 16 | use ChordPro::Wx::Utils; | ||
| 17 | |||
| 18 | # Either Wx::WebView or Wx::StaticText. | ||
| 19 | field $wv :accessor; | ||
| 20 | |||
| 21 | # Wx::WebView. | ||
| 22 | field $prv :mutator; | ||
| 23 | |||
| 24 | ################ API Functions ################ | ||
| 25 | |||
| 26 | # The logger is set up by the refresh call! | ||
| 27 | |||
| 28 | method setup_logger() { | ||
| 29 | return if $state{logstderr}; | ||
| 30 | Wx::Log::SetActiveTarget( Wx::LogTextCtrl->new( $self->{t_messages} ) ); | ||
| 31 | } | ||
| 32 | |||
| 33 | method log( $level, @msg ) { | ||
| 34 | wxTheApp->GetTopWindow->log( $level, @msg ); | ||
| 35 | $self->alert(1) if $level =~ /WEF/; | ||
| 36 | } | ||
| 37 | |||
| 38 | method alert( $severity, $message = "Click Messages to see diagnostic information" ) { | ||
| 39 | return if $self->{sw_tb}->IsSplit; | ||
| 40 | state $id = wxID_ANY; | ||
| 41 | if ( $id == wxID_ANY ) { | ||
| 42 | $id = Wx::NewId; | ||
| 43 | $self->{w_infobar}->AddButton( $id, "Messages"); | ||
| 44 | $self->{w_infobar}->AddButton( wxID_CLOSE ); | ||
| 45 | Wx::Event::EVT_BUTTON( $self->{w_infobar}, $id, | ||
| 46 | sub { $self->OnWindowMessages($_[1]) } ); | ||
| 47 | } | ||
| 48 | $self->{w_infobar}->ShowMessage( $message, wxICON_INFORMATION); | ||
| 49 | } | ||
| 50 | |||
| 51 | method setup_webview() { | ||
| 52 | |||
| 53 | my $try; | ||
| 54 | $wv = $self->{webview}; | ||
| 55 | return unless eval { require Wx::WebView; 1 }; | ||
| 56 | |||
| 57 | # WebView can only handle PDF on Windows with Edge backend. | ||
| 58 | # Wx::WebView::IsBackendAvailable requires Wx 3.002. | ||
| 59 | return if is_msw | ||
| 60 | && ( $Wx::VERSION < 3.002 || !Wx::WebView::IsBackendAvailable("wxWebViewEdge") ); | ||
| 61 | |||
| 62 | $state{have_webview} = 1; # Note: too early | ||
| 63 | $wv = Wx::WebView::New( $self->{p_right}, | ||
| 64 | wxID_ANY, "", | ||
| 65 | wxDefaultPosition, wxDefaultSize, | ||
| 66 | is_msw ? "wxWebViewEdge" : () | ||
| 67 | ); | ||
| 68 | $self->{sz_preview}->Replace( $self->{webview}, $wv, 1 ); | ||
| 69 | $self->{webview}->Destroy; | ||
| 70 | $self->{webview} = $wv; | ||
| 71 | $self->{sz_preview}->Layout; | ||
| 72 | } | ||
| 73 | |||
| 74 | method setup_messages_ctxmenu() { | ||
| 75 | |||
| 76 | # Context menu for message area. | ||
| 77 | my $menu = Wx::Menu->new; | ||
| 78 | my $id = Wx::NewId; | ||
| 79 | $menu->Append( $id, "Clear the message area", "", Wx::wxITEM_NORMAL ); | ||
| 80 | Wx::Event::EVT_MENU( $self, $id, $self->can("OnMessagesClear") ); | ||
| 81 | $id = Wx::NewId; | ||
| 82 | $menu->Append( $id, "Save the messages to a file", "", Wx::wxITEM_NORMAL ); | ||
| 83 | Wx::Event::EVT_MENU( $self, $id, $self->can("OnMessagesSave") ); | ||
| 84 | Wx::Event::EVT_CONTEXT_MENU( $self->{t_messages}, | ||
| 85 | sub { $_[0]->PopupMenu( $menu, | ||
| 86 | Wx::wxDefaultPosition ) } ); | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | method unsplit() { | ||
| 91 | $self->{sw_lr}->Unsplit(undef); | ||
| 92 | $self->{sw_tb}->Unsplit(undef); | ||
| 93 | } | ||
| 94 | |||
| 95 | method prepare_annotations() { | ||
| 96 | return unless $state{have_stc}; | ||
| 97 | $self->{t_editor}->prepare_annotations; | ||
| 98 | } | ||
| 99 | |||
| 100 | method add_annotation( $line, $msg ) { | ||
| 101 | return unless $state{have_stc}; | ||
| 102 | $self->{t_editor}->add_annotation( $line, $msg ); | ||
| 103 | } | ||
| 104 | |||
| 105 | method refresh_messages { | ||
| 106 | $self->{t_messages}->SetFont( Wx::Font->new($preferences{msgsfont}) ); | ||
| 107 | } | ||
| 108 | |||
| 109 | ################ Virtual Methods ################ | ||
| 110 | |||
| 111 | method name(); | ||
| 112 | method check_source_saved(); | ||
| 113 | method check_preview_saved(); | ||
| 114 | method save_preferences(); | ||
| 115 | method update_preferences(); | ||
| 116 | |||
| 117 | ################ Event Handlers (alphabetic order) ################ | ||
| 118 | |||
| 119 | method OnHelp_DebugInfo($event) { | ||
| 120 | $state{debuginfo} = wxTheApp->GetTopWindow->GetMenuBar->FindItem($event->GetId)->IsChecked; | ||
| 121 | } | ||
| 122 | |||
| 123 | #method OnNew($event) { | ||
| 124 | # return unless $self->GetParent->check_saved; | ||
| 125 | # $self->GetParent->select_mode("initial"); | ||
| 126 | #} | ||
| 127 | |||
| 128 | method OnOpen($event) { | ||
| 129 | # Let the parent handle this one. | ||
| 130 | # I would have expected a Skip to be sufficient, but then nothing happens. | ||
| 131 | # Explicitly posting an event, e.g. | ||
| 132 | # Wx::PostEvent( wxTheApp->GetTopWindow, | ||
| 133 | # Wx::CommandEvent->new( wxEVT_COMMAND_MENU_SELECTED, | ||
| 134 | # wxID_OPEN ) ); | ||
| 135 | # ends up here, not in the parent, so we have a nice loop. | ||
| 136 | # This is the only place where we violate our principle to not call | ||
| 137 | # event handlers explicitly... | ||
| 138 | $self->GetParent->OnOpen($event); | ||
| 139 | } | ||
| 140 | |||
| 141 | method OnPreferences($event) { | ||
| 142 | # Dispatch to Main. | ||
| 143 | Wx::PostEvent( $self->GetParent, | ||
| 144 | Wx::CommandEvent->new | ||
| 145 | ( wxEVT_COMMAND_MENU_SELECTED, wxID_PREFERENCES) ); | ||
| 146 | } | ||
| 147 | |||
| 148 | method OnPreview($event) { # for menu | ||
| 149 | $self->preview( [] ); | ||
| 150 | $self->previewtooltip; | ||
| 151 | } | ||
| 152 | |||
| 153 | method OnPreviewClose($event) { | ||
| 154 | return unless $self->{sw_lr}->IsSplit; | ||
| 155 | $self->{sw_lr}->Unsplit(undef); | ||
| 156 | $self->previewtooltip; | ||
| 157 | } | ||
| 158 | |||
| 159 | method OnPreviewLyricsOnly($event) { | ||
| 160 | $self->preview( [ '--lyrics-only' ] ); | ||
| 161 | $self->previewtooltip; | ||
| 162 | } | ||
| 163 | |||
| 164 | method OnPreviewMore($event) { | ||
| 165 | |||
| 166 | # C D E F G A B C | ||
| 167 | state @xpmap = qw( 0 1 1 2 3 3 4 5 6 6 7 8 8 9 10 10 11 12 ); | ||
| 168 | state @sfmap = qw( 0 7 -5 2 9 -3 4 -1 6 -6 1 8 -4 3 10 -2 5 0 ); | ||
| 169 | |||
| 170 | unless ( $self->{d_render} ) { | ||
| 171 | require ChordPro::Wx::RenderDialog; | ||
| 172 | $self->{d_render} = ChordPro::Wx::RenderDialog->new | ||
| 173 | ( $self, wxID_ANY, "Tasks" ); | ||
| 174 | restorewinpos( $self->{d_render}, "render" ); | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | $self->{d_render} ->refresh; | ||
| 178 | } | ||
| 179 | |||
| 180 | my $d = $self->{d_render}; | ||
| 181 | my $ret = $d->ShowModal; | ||
| 182 | return unless $ret == wxID_OK; | ||
| 183 | |||
| 184 | my @args; | ||
| 185 | if ( $d->{cb_task_no_diagrams}->IsChecked ) { | ||
| 186 | push( @args, "--no-chord-grids" ); | ||
| 187 | } | ||
| 188 | if ( $d->{cb_task_lyrics_only}->IsChecked ) { | ||
| 189 | push( @args, "--lyrics-only", | ||
| 190 | "--define=delegates.abc.omit=1", | ||
| 191 | "--define=delegates.ly.omit=1" ); | ||
| 192 | } | ||
| 193 | if ( $d->{cb_task_decapo}->IsChecked ) { | ||
| 194 | push( @args, "--decapo" ); | ||
| 195 | } | ||
| 196 | |||
| 197 | # Transpose. See also Preview.pm. | ||
| 198 | $state{"xpose_$_"} ||= 0 | ||
| 199 | for qw( enabled semitones accidentals ); | ||
| 200 | if ( $state{xpose_enabled} ) { | ||
| 201 | my $pfx; | ||
| 202 | if ( $state{xpose_accidentals} == XP_SHARP ) { | ||
| 203 | $pfx = "s" | ||
| 204 | } | ||
| 205 | elsif ( $state{xpose_accidentals} == XP_FLAT ) { | ||
| 206 | $pfx = "f" | ||
| 207 | } | ||
| 208 | else { | ||
| 209 | $pfx = ""; | ||
| 210 | } | ||
| 211 | push( @ARGV, '--transpose', $state{xpose_semitones} . $pfx ); | ||
| 212 | } | ||
| 213 | |||
| 214 | my $i = 0; | ||
| 215 | while ( exists $d->{"cb_customtask_$i"} ) { | ||
| 216 | if ( $d->{"cb_customtask_$i"}->IsChecked ) { | ||
| 217 | push( @args, "--config", | ||
| 218 | $state{presets}{tasks}{lc $d->{"cb_customtask_$i"}->GetLabel}->{file} ); | ||
| 219 | } | ||
| 220 | $i++; | ||
| 221 | } | ||
| 222 | $self->preview( \@args ); | ||
| 223 | $self->previewtooltip; | ||
| 224 | } | ||
| 225 | |||
| 226 | method OnPreviewNoDiagrams($event) { | ||
| 227 | $self->preview( [ '--no-chord-grids' ] ); | ||
| 228 | $self->previewtooltip; | ||
| 229 | } | ||
| 230 | |||
| 231 | method OnPreviewSave($event) { | ||
| 232 | if ( $self->prv && $self->prv->have_preview ) { | ||
| 233 | return $self->prv->save; | ||
| 234 | } | ||
| 235 | Wx::MessageDialog->new( $self, | ||
| 236 | "No preview to save", | ||
| 237 | "No Preview", | ||
| 238 | wxOK | wxICON_ERROR )->ShowModal; | ||
| 239 | } | ||
| 240 | |||
| 241 | method OnSashLRChanged($event) { | ||
| 242 | $state{sash}{$self->panel."_lr"} = $self->{sw_lr}->GetSashPosition; | ||
| 243 | } | ||
| 244 | |||
| 245 | method OnSashTBChanged($event) { | ||
| 246 | $state{sash}{$self->panel."_tb"} = $self->{sw_tb}->GetSashPosition; | ||
| 247 | } | ||
| 248 | |||
| 249 | method OnShowMessages($event) { | ||
| 250 | $self->OnWindowMessages($event); | ||
| 251 | } | ||
| 252 | |||
| 253 | method OnShowPreview($event) { # for button | ||
| 254 | $self->{sw_lr}->IsSplit | ||
| 255 | ? $self->OnPreviewClose($event) | ||
| 256 | : $self->OnPreview($event); | ||
| 257 | } | ||
| 258 | |||
| 259 | method OnWindowMessages($event) { | ||
| 260 | if ( $self->{sw_tb}->IsSplit ) { | ||
| 261 | $state{sash}{$self->panel."_tb"} = $self->{sw_tb}->GetSashPosition; | ||
| 262 | $self->{sw_tb}->Unsplit(undef); | ||
| 263 | } | ||
| 264 | else { | ||
| 265 | $self->{bmb_messages}->SetBackgroundColour(wxNullColour); | ||
| 266 | $self->{sw_tb}->SplitHorizontally( $self->{p_top}, | ||
| 267 | $self->{p_bottom}, | ||
| 268 | $state{sash}{$self->panel."_tb"} // 0 ); | ||
| 269 | } | ||
| 270 | $self->messagestooltip; | ||
| 271 | } | ||
| 272 | |||
| 273 | method OnMessagesClear($event) { | ||
| 274 | $self->{t_messages}->Clear; | ||
| 275 | } | ||
| 276 | |||
| 277 | method OnMessagesRuntimeInfo($event) { | ||
| 278 | $self->log( 'I', "---- Runtime Information ----\n" . $self->GetParent->aboutmsg ); | ||
| 279 | $self->log( 'I', "---- End of Runtime Information ----\n" ); | ||
| 280 | } | ||
| 281 | |||
| 282 | method OnMessagesSave($event) { | ||
| 283 | my $conf = Wx::ConfigBase::Get; | ||
| 284 | my $file = $state{messages}{savedas} // ""; | ||
| 285 | |||
| 286 | # Starting the dialog and cancel it is now the official way to get | ||
| 287 | # the runtime info into the log messages :). | ||
| 288 | $self->OnMessagesRuntimeInfo($event); | ||
| 289 | |||
| 290 | my $fd = Wx::FileDialog->new | ||
| 291 | ( $self, | ||
| 292 | _T("Choose file to save in"), | ||
| 293 | fn_dirname($file), fn_basename($file), | ||
| 294 | "*", | ||
| 295 | wxFD_SAVE|wxFD_OVERWRITE_PROMPT ); | ||
| 296 | |||
| 297 | my $ret = $fd->ShowModal; | ||
| 298 | if ( $ret == wxID_OK ) { | ||
| 299 | $file = $fd->GetPath; | ||
| 300 | $self->{t_messages}->SaveFile($file); | ||
| 301 | $self->log( 'S', "Messages saved." ); | ||
| 302 | $state{messages}{savedas} = $file; | ||
| 303 | } | ||
| 304 | |||
| 305 | $fd->Destroy; | ||
| 306 | return $ret; | ||
| 307 | } | ||
| 308 | |||
| 309 | method OnWindowPreview($event) { | ||
| 310 | if ( $self->{sw_lr}->IsSplit ) { | ||
| 311 | $state{sash}{$self->panel."_lr"} = $self->{sw_lr}->GetSashPosition; | ||
| 312 | $self->{sw_lr}->Unsplit(undef); | ||
| 313 | } | ||
| 314 | else { | ||
| 315 | return $self->OnPreview($event) | ||
| 316 | unless $self->prv && $self->prv->have_preview; | ||
| 317 | $self->{sw_lr}->SplitVertically( $self->{p_left}, | ||
| 318 | $self->{p_right}, | ||
| 319 | $state{sash}{$self->panel."_lr"} // 0 ); | ||
| 320 | } | ||
| 321 | $self->previewtooltip; | ||
| 322 | } | ||
| 323 | |||
| 324 | method previewtooltip() { | ||
| 325 | my $mb = wxTheApp->GetTopWindow->GetMenuBar; | ||
| 326 | my $mi = $mb->FindItem($mb->FindMenuItem("View","Preview Panel")); | ||
| 327 | if ( $self->{sw_lr}->IsSplit ) { | ||
| 328 | $self->{bmb_preview}->SetToolTip(_T("Hide the preview\nUse ". | ||
| 329 | kbdkey("Ctrl-P"). | ||
| 330 | " to refresh the preview")); | ||
| 331 | $mi->Check(1); | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | $self->{bmb_preview}->SetToolTip(_T("Generate and show a new preview")); | ||
| 335 | $mi->Check(0); | ||
| 336 | } | ||
| 337 | $self->panel_focus; | ||
| 338 | } | ||
| 339 | |||
| 340 | method messagestooltip() { | ||
| 341 | my $mb = wxTheApp->GetTopWindow->GetMenuBar; | ||
| 342 | my $mi = $mb->FindItem($mb->FindMenuItem("View","Messages Panel")); | ||
| 343 | if ( $self->{sw_tb}->IsSplit ) { | ||
| 344 | $self->{bmb_messages}->SetToolTip(_T("Hide the messages")); | ||
| 345 | $mi->Check(1); | ||
| 346 | } | ||
| 347 | else { | ||
| 348 | $self->{bmb_messages}->SetToolTip(_T("Show the messages")); | ||
| 349 | $mi->Check(0); | ||
| 350 | } | ||
| 351 | $self->{w_infobar}->Dismiss if $self->{w_infobar}->IsShown; | ||
| 352 | $self->panel_focus; | ||
| 353 | $self->panel_linenums( $mi->IsChecked ); | ||
| 354 | } | ||
| 355 | |||
| 356 | method panel_focus() { | ||
| 357 | return unless UNIVERSAL::can( $state{panel}, "set_focus" ); | ||
| 358 | $state{panel}->set_focus; | ||
| 359 | } | ||
| 360 | |||
| 361 | method panel_linenums( $b ) { | ||
| 362 | return unless UNIVERSAL::can( $state{panel}, "showlinenumbers" ); | ||
| 363 | $state{panel}->showlinenumbers($b); | ||
| 364 | } | ||
| 365 | |||
| 366 | 1; | ||
| 367 |