From 891bd624fc577083b173ff3824dfde3a7dccd62e Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 13 Oct 2016 10:32:20 +0200 Subject: [PATCH] Code clean-up --- ZRCola/zrcolachrcatpnl.cpp | 13 ++++++------- ZRCola/zrcolachrgrid.cpp | 2 +- ZRCola/zrcolachrreq.cpp | 4 ++-- ZRCola/zrcolachrslct.cpp | 24 ++++++++++++------------ ZRCola/zrcolacomppnl.cpp | 10 +++++----- ZRCola/zrcolafrm.cpp | 14 ++++++-------- ZRCola/zrcolakeyhndlr.cpp | 8 ++++---- ZRCola/zrcolasettings.cpp | 13 ++++++------- ZRCola/zrcolaupdater.cpp | 4 ++-- 9 files changed, 44 insertions(+), 48 deletions(-) diff --git a/ZRCola/zrcolachrcatpnl.cpp b/ZRCola/zrcolachrcatpnl.cpp index 680ef74..f67f4c4 100644 --- a/ZRCola/zrcolachrcatpnl.cpp +++ b/ZRCola/zrcolachrcatpnl.cpp @@ -31,7 +31,7 @@ END_EVENT_TABLE() wxZRColaCharacterCatalogPanel::wxZRColaCharacterCatalogPanel(wxWindow* parent) : wxZRColaCharacterCatalogPanelBase(parent) { - std::fstream dat((LPCTSTR)((ZRColaApp*)wxTheApp)->GetDatabaseFilePath(), std::ios_base::in | std::ios_base::binary); + std::fstream dat((LPCTSTR)dynamic_cast(wxTheApp)->GetDatabaseFilePath(), std::ios_base::in | std::ios_base::binary); if (dat.good()) { if (stdex::idrec::find(dat, ZRCOLA_DB_ID, sizeof(ZRCola::recordid_t))) { ZRCola::recordsize_t size; @@ -96,7 +96,7 @@ void wxZRColaCharacterCatalogPanel::OnChoice(wxCommandEvent& event) void wxZRColaCharacterCatalogPanel::OnGridClick(wxGridEvent& event) { - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); if (app->m_mainWnd) { app->m_mainWnd->m_panel->m_decomposed->WriteText(m_grid->GetCellValue(event.GetRow(), event.GetCol())); app->m_mainWnd->m_panel->m_decomposed->SetFocus(); @@ -111,7 +111,7 @@ void wxZRColaCharacterCatalogPanel::OnGridKeyDown(wxKeyEvent& event) switch (event.GetKeyCode()) { case WXK_RETURN: case WXK_NUMPAD_ENTER: - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); if (app->m_mainWnd) { app->m_mainWnd->m_panel->m_decomposed->WriteText(m_grid->GetCellValue(m_grid->GetCursorRow(), m_grid->GetCursorColumn())); app->m_mainWnd->m_panel->m_decomposed->SetFocus(); @@ -137,7 +137,7 @@ void wxZRColaCharacterCatalogPanel::OnShowAll(wxCommandEvent& event) void wxZRColaCharacterCatalogPanel::OnFocusDecomposed(wxCommandEvent& event) { - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); if (app->m_mainWnd) { app->m_mainWnd->m_panel->m_decomposed->SetFocus(); @@ -193,7 +193,7 @@ wxString wxPersistentZRColaCharacterCatalogPanel::GetKind() const void wxPersistentZRColaCharacterCatalogPanel::Save() const { - const wxZRColaCharacterCatalogPanel * const wnd = static_cast(GetWindow()); + auto wnd = static_cast(GetWindow()); // dynamic_cast is not reliable as we are typically called late in the wxTopLevelWindowMSW destructor. SaveValue(wxT("charGroup"), wnd->m_cg_id); SaveValue(wxT("showAll" ), wnd->m_show_all->GetValue()); @@ -202,8 +202,7 @@ void wxPersistentZRColaCharacterCatalogPanel::Save() const bool wxPersistentZRColaCharacterCatalogPanel::Restore() { - wxZRColaCharacterCatalogPanel * const wnd = static_cast(GetWindow()); - + auto wnd = dynamic_cast(GetWindow()); bool update = false; // Restore selected character group. diff --git a/ZRCola/zrcolachrgrid.cpp b/ZRCola/zrcolachrgrid.cpp index 1854d46..2f6f33d 100644 --- a/ZRCola/zrcolachrgrid.cpp +++ b/ZRCola/zrcolachrgrid.cpp @@ -97,7 +97,7 @@ wxString wxZRColaCharGrid::GetToolTipText(int idx) { wxASSERT_MSG(idx < (int)m_chars.Length(), wxT("index out of bounds")); - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); // See if this character has a key sequence registered. char ks[sizeof(ZRCola::keyseq_db::keyseq)] = {}; diff --git a/ZRCola/zrcolachrreq.cpp b/ZRCola/zrcolachrreq.cpp index 5e7259e..d758639 100644 --- a/ZRCola/zrcolachrreq.cpp +++ b/ZRCola/zrcolachrreq.cpp @@ -80,7 +80,7 @@ void wxPersistentZRColaCharRequest::Save() const { wxPersistentDialog::Save(); - const wxZRColaCharRequest * const wnd = static_cast(GetWindow()); + auto wnd = static_cast(GetWindow()); // dynamic_cast is not reliable as we are typically called late in the wxTopLevelWindowMSW destructor. SaveValue(wxT("character"), wnd->m_character->GetValue()); SaveValue(wxT("context"), wnd->m_context->GetValue()); @@ -89,7 +89,7 @@ void wxPersistentZRColaCharRequest::Save() const bool wxPersistentZRColaCharRequest::Restore() { - wxZRColaCharRequest * const wnd = static_cast(GetWindow()); + auto wnd = dynamic_cast(GetWindow()); wxString str; if (RestoreValue(wxT("character"), &str)) diff --git a/ZRCola/zrcolachrslct.cpp b/ZRCola/zrcolachrslct.cpp index 94c9c75..5118f2b 100644 --- a/ZRCola/zrcolachrslct.cpp +++ b/ZRCola/zrcolachrslct.cpp @@ -41,7 +41,7 @@ wxZRColaCharSelect::wxZRColaCharSelect(wxWindow* parent) : m_unicode->SetValidator(wxHexValidator(&m_char, wxNUM_VAL_DEFAULT, 4)); // Fill categories. - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) { const auto &cc = app->m_cc_db.idxRnk[i]; int idx = m_categories->Insert(wxGetTranslation(wxString(cc.name, cc.name_len), wxT("ZRCola-zrcdb")), i); @@ -74,7 +74,7 @@ void wxZRColaCharSelect::OnIdle(wxIdleEvent& event) if (m_unicodeChanged) { if (m_unicode->GetValidator()->TransferFromWindow()) { - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); m_gridPreview->SetCellValue(wxString(1, m_char), 0, 0); @@ -137,7 +137,7 @@ void wxZRColaCharSelect::OnIdle(wxIdleEvent& event) wxString val(m_search->GetValue()); if (!val.IsEmpty()) { - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); m_searchThread = new SearchThread(this); @@ -191,7 +191,7 @@ void wxZRColaCharSelect::OnCategoriesAll(wxHyperlinkEvent& event) { event.StopPropagation(); - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) m_categories->Check(i, true); @@ -203,7 +203,7 @@ void wxZRColaCharSelect::OnCategoriesNone(wxHyperlinkEvent& event) { event.StopPropagation(); - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) m_categories->Check(i, false); @@ -215,7 +215,7 @@ void wxZRColaCharSelect::OnCategoriesInvert(wxHyperlinkEvent& event) { event.StopPropagation(); - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) m_categories->Check(i, !m_categories->IsChecked(i)); @@ -408,7 +408,7 @@ void wxZRColaCharSelect::OnOKButtonClick(wxCommandEvent& event) void wxZRColaCharSelect::ResetResults() { // Fill the results. - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); size_t i, n = app->m_chr_db.idxChr.size(); wxString val; val.reserve(n); @@ -500,7 +500,7 @@ wxZRColaCharSelect::SearchThread::SearchThread(wxZRColaCharSelect *parent) : wxThread::ExitCode wxZRColaCharSelect::SearchThread::Entry() { - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); std::map hits; if (TestDestroy()) return (wxThread::ExitCode)1; @@ -580,11 +580,11 @@ void wxPersistentZRColaCharSelect::Save() const { wxPersistentDialog::Save(); - const wxZRColaCharSelect * const wnd = static_cast(GetWindow()); + auto wnd = static_cast(GetWindow()); // dynamic_cast is not reliable as we are typically called late in the wxTopLevelWindowMSW destructor. + auto app = dynamic_cast(wxTheApp); SaveValue(wxT("recentChars"), wnd->m_gridRecent->GetCharacters()); - ZRColaApp *app = (ZRColaApp*)wxTheApp; for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) { const auto &cc = app->m_cc_db.idxRnk[i]; wxString name(wxT("category")); @@ -598,13 +598,13 @@ void wxPersistentZRColaCharSelect::Save() const bool wxPersistentZRColaCharSelect::Restore() { - wxZRColaCharSelect * const wnd = static_cast(GetWindow()); + auto wnd = dynamic_cast(GetWindow()); + auto app = dynamic_cast(wxTheApp); wxString recent; if (RestoreValue(wxT("recentChars"), &recent)) wnd->m_gridRecent->SetCharacters(recent); - ZRColaApp *app = (ZRColaApp*)wxTheApp; for (size_t i = 0, n = app->m_cc_db.idxRnk.size(); i < n; i++) { const auto &cc = app->m_cc_db.idxRnk[i]; wxString name(wxT("category")); diff --git a/ZRCola/zrcolacomppnl.cpp b/ZRCola/zrcolacomppnl.cpp index 6dd5ccc..4dcfe21 100644 --- a/ZRCola/zrcolacomppnl.cpp +++ b/ZRCola/zrcolacomppnl.cpp @@ -86,14 +86,15 @@ void wxZRColaComposerPanel::SynchronizePanels() if (m_decomposedChanged) { m_timerSave.Stop(); + auto app = dynamic_cast(wxTheApp); wxString src; size_t len = GetValue(m_decomposed, src); std::wstring norm; - ((ZRColaApp*)wxTheApp)->m_t_db.Decompose(src.data(), len, norm, &m_mapping1); + app->m_t_db.Decompose(src.data(), len, norm, &m_mapping1); std::wstring dst; - ((ZRColaApp*)wxTheApp)->m_t_db.Compose(norm.data(), norm.size(), dst, &m_mapping2); + app->m_t_db.Compose(norm.data(), norm.size(), dst, &m_mapping2); m_decomposed->GetSelection(&m_selDecomposed.first, &m_selDecomposed.second); @@ -115,7 +116,6 @@ void wxZRColaComposerPanel::SynchronizePanels() wxString src; size_t len = GetValue(m_composed, src); - ZRColaApp *app = (ZRColaApp*)wxTheApp; std::wstring dst; wxZRColaFrame *mainWnd = dynamic_cast(wxGetActiveWindow()); if (mainWnd) @@ -382,7 +382,7 @@ wxString wxPersistentZRColaComposerPanel::GetKind() const void wxPersistentZRColaComposerPanel::Save() const { - const wxZRColaComposerPanel * const wnd = static_cast(GetWindow()); + auto const wnd = static_cast(GetWindow()); // dynamic_cast is not reliable as we are typically called late in the wxTopLevelWindowMSW destructor. SaveValue(wxT("splitDecomposed"), wnd->m_splitterDecomposed->GetSashPosition()); SaveValue(wxT("splitComposed" ), wnd->m_splitterComposed ->GetSashPosition()); @@ -391,7 +391,7 @@ void wxPersistentZRColaComposerPanel::Save() const bool wxPersistentZRColaComposerPanel::Restore() { - wxZRColaComposerPanel * const wnd = static_cast(GetWindow()); + auto wnd = dynamic_cast(GetWindow()); int sashVal; diff --git a/ZRCola/zrcolafrm.cpp b/ZRCola/zrcolafrm.cpp index 46ffa57..d9a1981 100644 --- a/ZRCola/zrcolafrm.cpp +++ b/ZRCola/zrcolafrm.cpp @@ -365,7 +365,7 @@ void wxZRColaFrame::OnHelpShortcuts(wxCommandEvent& event) #endif // Search and try to launch local PDF copy. - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); pdf_path = app->GetDatabasePath(); pdf_path += _T("ZRCola_keyboard.pdf"); if (wxFileExists(pdf_path) && @@ -549,23 +549,21 @@ wxPersistentZRColaFrame::wxPersistentZRColaFrame(wxZRColaFrame *wnd) : wxPersist void wxPersistentZRColaFrame::Save() const { - const wxZRColaFrame * const wnd = static_cast(GetWindow()); + wxPersistentTLW::Save(); + + auto wnd = static_cast(GetWindow()); // dynamic_cast is not reliable as we are typically called late in the wxTopLevelWindowMSW destructor. wxPersistentZRColaComposerPanel(wnd->m_panel).Save(); wxPersistentZRColaCharacterCatalogPanel(wnd->m_panelChrCat).Save(); - - wxPersistentTLW::Save(); } bool wxPersistentZRColaFrame::Restore() { - const bool r = wxPersistentTLW::Restore(); - - wxZRColaFrame * const wnd = static_cast(GetWindow()); + auto wnd = dynamic_cast(GetWindow()); wxPersistentZRColaCharacterCatalogPanel(wnd->m_panelChrCat).Restore(); wxPersistentZRColaComposerPanel(wnd->m_panel).Restore(); - return r; + return wxPersistentTLW::Restore(); } diff --git a/ZRCola/zrcolakeyhndlr.cpp b/ZRCola/zrcolakeyhndlr.cpp index 68816d5..de83069 100644 --- a/ZRCola/zrcolakeyhndlr.cpp +++ b/ZRCola/zrcolakeyhndlr.cpp @@ -39,7 +39,7 @@ bool wxZRColaKeyHandler::ProcessEvent(wxEvent& event) if (e.GetKeyCode() == WXK_INSERT) { // Insert key has been pressed. m_is_insert = true; - wxFrame *pFrame = wxDynamicCast(((ZRColaApp*)wxTheApp)->m_mainWnd, wxFrame); + wxFrame *pFrame = wxDynamicCast(dynamic_cast(wxTheApp)->m_mainWnd, wxFrame); if (pFrame && pFrame->GetStatusBar()) pFrame->SetStatusText(_("INS key is pressed. Type the Unicode code of desired character now (up to four hexadecimal digits: 0-9, A-F), then release INS.")); } else if (m_is_insert) { @@ -49,7 +49,7 @@ bool wxZRColaKeyHandler::ProcessEvent(wxEvent& event) if (WXK_NUMPAD0 <= key && key <= WXK_NUMPAD9) chr = '0' + (key - WXK_NUMPAD0); } - wxFrame *pFrame = wxDynamicCast(((ZRColaApp*)wxTheApp)->m_mainWnd, wxFrame); + wxFrame *pFrame = wxDynamicCast(dynamic_cast(wxTheApp)->m_mainWnd, wxFrame); if (('0' <= chr && chr <= '9' || 'A' <= chr && chr <= 'F') && m_insert_seq.size() < 4) { // A hex-digit pressed. Save it. m_insert_seq.push_back((char)chr); @@ -73,7 +73,7 @@ bool wxZRColaKeyHandler::ProcessEvent(wxEvent& event) #endif ) { - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); ZRCola::keyseq_db::indexKey::size_type start; bool found; wxFrame *pFrame = wxDynamicCast(app->m_mainWnd, wxFrame); @@ -143,7 +143,7 @@ bool wxZRColaKeyHandler::ProcessEvent(wxEvent& event) wxKeyEvent &e = (wxKeyEvent&)event; if (m_is_insert && e.GetKeyCode() == WXK_INSERT) { // Insert key has been depressed. - wxFrame *pFrame = wxDynamicCast(((ZRColaApp*)wxTheApp)->m_mainWnd, wxFrame); + wxFrame *pFrame = wxDynamicCast(dynamic_cast(wxTheApp)->m_mainWnd, wxFrame); if (pFrame && pFrame->GetStatusBar()) pFrame->SetStatusText(wxEmptyString); diff --git a/ZRCola/zrcolasettings.cpp b/ZRCola/zrcolasettings.cpp index 4fb76f9..bef2786 100644 --- a/ZRCola/zrcolasettings.cpp +++ b/ZRCola/zrcolasettings.cpp @@ -29,7 +29,7 @@ wxZRColaSettings::wxZRColaSettings(wxWindow* parent) : m_lang(ZRCola::langid_t::blank), wxZRColaSettingsBase(parent) { - ZRColaApp *app = ((ZRColaApp*)wxTheApp); + auto app = dynamic_cast(wxTheApp); m_languages->Clear(); for (size_t i = 0, n = app->m_lang_db.idxLng.size(); i < n; i++) { const auto &lang = app->m_lang_db.idxLng[i]; @@ -57,7 +57,7 @@ void wxZRColaSettings::OnInitDialog(wxInitDialogEvent& event) m_languages->Enable(!m_lang_auto); (m_lang_auto ? m_langAuto : m_langManual)->SetValue(true); - ZRColaApp *app = ((ZRColaApp*)wxTheApp); + auto app = dynamic_cast(wxTheApp); char l[sizeof(ZRCola::language_db::language)] = {}; ((ZRCola::language_db::language*)l)->id = m_lang; ZRCola::language_db::indexLang::size_type start; @@ -121,7 +121,7 @@ void wxZRColaSettings::OnApplyButtonClick(wxCommandEvent& event) } else { m_lang_auto = false; - ZRColaApp *app = ((ZRColaApp*)wxTheApp); + auto app = dynamic_cast(wxTheApp); const auto &lang = app->m_lang_db.idxLng[m_languages->GetSelection()]; if (m_lang != lang.id) { @@ -155,7 +155,7 @@ void wxPersistentZRColaSettings::Save() const { wxPersistentDialog::Save(); - const wxZRColaSettings * const wnd = static_cast(GetWindow()); + auto wnd = static_cast(GetWindow()); // dynamic_cast is not reliable as we are typically called late in the wxTopLevelWindowMSW destructor. SaveValue(wxT("langAuto"), wnd->m_lang_auto); SaveValue(wxT("lang" ), wxString::FromAscii(wnd->m_lang.data, _countof(wnd->m_lang.data))); @@ -164,9 +164,8 @@ void wxPersistentZRColaSettings::Save() const bool wxPersistentZRColaSettings::Restore() { - wxZRColaSettings * const wnd = static_cast(GetWindow()); - - ZRColaApp *app = ((ZRColaApp*)wxTheApp); + auto wnd = dynamic_cast(GetWindow()); + auto app = dynamic_cast(wxTheApp); wxString lang; // Restore automatic language detection setting first. diff --git a/ZRCola/zrcolaupdater.cpp b/ZRCola/zrcolaupdater.cpp index 7571c9e..5f7ef57 100644 --- a/ZRCola/zrcolaupdater.cpp +++ b/ZRCola/zrcolaupdater.cpp @@ -40,7 +40,7 @@ wxZRColaUpdater::wxZRColaUpdater(wxWindow* parent) : //Connect(wxID_ANY, wxEVT_UPDATER_CHECK_COMPLETE, wxThreadEventHandler(wxZRColaUpdater::OnCheckComplete), NULL, this); // Prepare Updater. - ZRColaApp *app = (ZRColaApp*)wxTheApp; + auto app = dynamic_cast(wxTheApp); m_updater = new wxUpdCheckThread(app->m_locale.GetCanonicalName(), this); //if (m_updater->Run() != wxTHREAD_NO_ERROR) { // wxFAIL_MSG(wxT("Can't create the thread!")); @@ -95,7 +95,7 @@ void wxZRColaUpdater::OnUpdate(wxCommandEvent& event) m_updater->LaunchUpdate(); Close(true); - ((ZRColaApp*)wxTheApp)->m_mainWnd->Close(); + dynamic_cast(wxTheApp)->m_mainWnd->Close(); } } }