1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-23 07:40:05 +02:00

Rewrite QuickFix button (Dark Audacity) without SetClientData...

... Instead, it's easier to capture data in a lambda and use Bind.

Eliminates client data, casting, and use of the fields of the event object.
This commit is contained in:
Paul Licameli 2020-03-10 17:05:19 -04:00
parent 47fa55c4b7
commit 9dfa8f205c

View File

@ -99,18 +99,19 @@ void ShowDiagnostics(
class QuickFixDialog : public wxDialogWrapper class QuickFixDialog : public wxDialogWrapper
{ {
public: public:
using PrefSetter = std::function< void() > ;
QuickFixDialog(wxWindow * pParent, AudacityProject &project); QuickFixDialog(wxWindow * pParent, AudacityProject &project);
void Populate(); void Populate();
void PopulateOrExchange(ShuttleGui & S); void PopulateOrExchange(ShuttleGui & S);
void AddStuck( ShuttleGui & S, bool & bBool, wxString Pref, void AddStuck( ShuttleGui & S, bool & bBool,
const PrefSetter &prefSetter,
const TranslatableString &Prompt, wxString Help ); const TranslatableString &Prompt, wxString Help );
void OnOk(wxCommandEvent &event); void OnOk(wxCommandEvent &event);
void OnCancel(wxCommandEvent &event); void OnCancel(wxCommandEvent &event);
void OnHelp(wxCommandEvent &event); void OnHelp(const wxString &Str);
void OnFix(wxCommandEvent &event); void OnFix(const PrefSetter &setter, wxWindowID id);
wxString StringFromEvent( wxCommandEvent &event );
AudacityProject &mProject; AudacityProject &mProject;
@ -121,16 +122,13 @@ public:
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
#define FixButtonID 7001 #define FixButtonID 7001
#define HelpButtonID 7011 #define HelpButtonID 7011
#define FakeButtonID 7021
BEGIN_EVENT_TABLE(QuickFixDialog, wxDialogWrapper) BEGIN_EVENT_TABLE(QuickFixDialog, wxDialogWrapper)
EVT_BUTTON(wxID_OK, QuickFixDialog::OnOk) EVT_BUTTON(wxID_OK, QuickFixDialog::OnOk)
EVT_BUTTON(wxID_CANCEL, QuickFixDialog::OnCancel) EVT_BUTTON(wxID_CANCEL, QuickFixDialog::OnCancel)
EVT_BUTTON(wxID_HELP, QuickFixDialog::OnHelp)
EVT_COMMAND_RANGE(FixButtonID, HelpButtonID-1, wxEVT_BUTTON, QuickFixDialog::OnFix)
EVT_COMMAND_RANGE(HelpButtonID, FakeButtonID-1, wxEVT_BUTTON, QuickFixDialog::OnHelp)
END_EVENT_TABLE(); END_EVENT_TABLE();
QuickFixDialog::QuickFixDialog(wxWindow * pParent, AudacityProject &project) : QuickFixDialog::QuickFixDialog(wxWindow * pParent, AudacityProject &project) :
@ -160,16 +158,24 @@ QuickFixDialog::QuickFixDialog(wxWindow * pParent, AudacityProject &project) :
Center(); Center();
} }
void QuickFixDialog::AddStuck( void QuickFixDialog::AddStuck( ShuttleGui & S, bool & bBool,
ShuttleGui & S, bool & bBool, wxString Pref, const PrefSetter &prefSetter,
const TranslatableString &Prompt, wxString Help ) const TranslatableString &Prompt, wxString Help )
{ {
mItem++; mItem++;
wxWindowID id = FixButtonID + mItem;
if( !bBool) if( !bBool)
return; return;
S.AddFixedText( Prompt );
S.Id(FixButtonID + mItem).AddButton( XXO("Fix") )->SetClientObject( S
safenew wxStringClientData(Pref)); .AddFixedText( Prompt );
S
.Id( id )
.AddButton( XXO("Fix") )
->Bind( wxEVT_BUTTON, [this, prefSetter, id](wxCommandEvent&){
OnFix( prefSetter, id );
} );
{ {
// Replace standard Help button with smaller icon button. // Replace standard Help button with smaller icon button.
@ -177,7 +183,9 @@ void QuickFixDialog::AddStuck(
auto b = safenew wxBitmapButton(S.GetParent(), HelpButtonID+mItem, theTheme.Bitmap( bmpHelpIcon )); auto b = safenew wxBitmapButton(S.GetParent(), HelpButtonID+mItem, theTheme.Bitmap( bmpHelpIcon ));
b->SetToolTip( _("Help") ); b->SetToolTip( _("Help") );
b->SetLabel(_("Help")); // for screen readers b->SetLabel(_("Help")); // for screen readers
b->SetClientObject( safenew wxStringClientData( Help )); b->Bind( wxEVT_BUTTON, [this, Help](const wxCommandEvent&){
OnHelp( Help );
} );
S.AddWindow( b ); S.AddWindow( b );
} }
} }
@ -200,13 +208,38 @@ void QuickFixDialog::PopulateOrExchange(ShuttleGui & S)
{ {
mItem = -1; mItem = -1;
auto defaultAction =
[](AudacityProject *pProject, const wxString &path){ return
[pProject, path]{
gPrefs->Write(path, 0);
gPrefs->Flush();
// This is overkill (aka slow), as all preferences are
// reloaded and all
// toolbars recreated.
// Overkill probably doesn't matter, as this command is
// infrequently used.
DoReloadPreferences( *pProject );
};
};
// Use # in the URLs to ensure we go to the online version of help. // Use # in the URLs to ensure we go to the online version of help.
// Local help may well not be installed. // Local help may well not be installed.
AddStuck( S, mbSyncLocked, "/GUI/SyncLockTracks", auto pProject = &mProject;
AddStuck( S, mbSyncLocked,
defaultAction( pProject, "/GUI/SyncLockTracks" ),
XO("Clocks on the Tracks"), "Quick_Fix#sync_lock" ); XO("Clocks on the Tracks"), "Quick_Fix#sync_lock" );
AddStuck( S, mbInSnapTo, "/SnapTo", AddStuck( S, mbInSnapTo,
[pProject] {
gPrefs->Write( "/SnapTo", 0 );
gPrefs->Flush();
// Sadly SnapTo has to be handled specially,
// as it is not part of the standard
// preference dialogs.
ProjectSelectionManager::Get( *pProject ).AS_SetSnapTo( 0 );
},
XO("Can't select precisely"), "Quick_Fix#snap_to" ); XO("Can't select precisely"), "Quick_Fix#snap_to" );
AddStuck( S, mbSoundActivated, "/AudioIO/SoundActivatedRecord", AddStuck( S, mbSoundActivated,
defaultAction( pProject, "/AudioIO/SoundActivatedRecord" ),
XO("Recording stops and starts"), XO("Recording stops and starts"),
"Quick_Fix#sound_activated_recording" ); "Quick_Fix#sound_activated_recording" );
} }
@ -222,8 +255,9 @@ void QuickFixDialog::PopulateOrExchange(ShuttleGui & S)
wxButton * pBtn = (wxButton*)FindWindowById( wxID_HELP ); wxButton * pBtn = (wxButton*)FindWindowById( wxID_HELP );
if( pBtn ) if( pBtn )
pBtn->SetClientObject( safenew wxStringClientData( "Quick_Fix#" )); pBtn->Bind( wxEVT_BUTTON, [this]( const wxCommandEvent & ){
OnHelp( "Quick_Fix#" );
} );
} }
void QuickFixDialog::OnOk(wxCommandEvent &event) void QuickFixDialog::OnOk(wxCommandEvent &event)
@ -238,55 +272,18 @@ void QuickFixDialog::OnCancel(wxCommandEvent &event)
EndModal(wxID_CANCEL); EndModal(wxID_CANCEL);
} }
wxString QuickFixDialog::StringFromEvent( wxCommandEvent &event ) void QuickFixDialog::OnHelp(const wxString &Str)
{ {
wxButton * pBtn = (wxButton*)event.GetEventObject(); HelpSystem::ShowHelp(this, Str, true);
if( !pBtn ){
wxFAIL_MSG( "Event Object not found");
return "";
}
wxStringClientData * pStrCd = (wxStringClientData*)(pBtn->GetClientObject());
if( !pStrCd ){
wxFAIL_MSG( "Client Data not found");
return "";
}
wxString Str = pStrCd->GetData();
if( Str.empty()){
wxFAIL_MSG( "String data empty");
return "";
}
return Str;
} }
void QuickFixDialog::OnHelp(wxCommandEvent &event) void QuickFixDialog::OnFix(const PrefSetter &setter, wxWindowID id)
{ {
HelpSystem::ShowHelp(this, StringFromEvent( event ), true); if ( setter )
} setter();
void QuickFixDialog::OnFix(wxCommandEvent &event)
{
wxString Str = StringFromEvent( event );
gPrefs->Write( Str, 0);
gPrefs->Flush();
{
// Sadly SnapTo has to be handled specially, as it is not part of the standard
// preference dialogs.
if( Str == "/SnapTo" )
{
ProjectSelectionManager::Get( mProject ).AS_SetSnapTo( 0 );
}
else
{
// This is overkill (aka slow), as all preferences are reloaded and all
// toolbars recreated.
// Overkill probably doesn't matter, as this command is infrequently used.
DoReloadPreferences( mProject );
}
}
// Change the label after doing the fix, as the fix may take a second or two. // Change the label after doing the fix, as the fix may take a second or two.
wxButton * pBtn = (wxButton*)event.GetEventObject(); auto pBtn = FindWindow(id);
if( pBtn ) if( pBtn )
pBtn->SetLabel( _("Fixed") ); pBtn->SetLabel( _("Fixed") );