diff --git a/ZRCola/zrcolacomppnl.cpp b/ZRCola/zrcolacomppnl.cpp index e7014fa..61efcdd 100644 --- a/ZRCola/zrcolacomppnl.cpp +++ b/ZRCola/zrcolacomppnl.cpp @@ -41,7 +41,34 @@ wxZRColaComposerPanel::wxZRColaComposerPanel(wxWindow* parent) : m_timer = new wxTimer(this, wxID_TIMER); // Restore the previously saved state (if exists). - wxPersistentZRColaComposerPanel(this).Restore(); + wxString fileName(GetStateFileName()); + if (wxFileExists(fileName)) { + wxFFile file(fileName, wxT("rb")); + if (file.IsOpened()) { + // Load decomposed text. + unsigned __int64 n; + file.Read(&n, sizeof(n)); + if (!file.Error()) { + wxString decomposed; + file.Read(wxStringBuffer(decomposed, n), sizeof(wchar_t)*n); + if (!file.Error()) { + // Load composed text. + file.Read(&n, sizeof(n)); + if (!file.Error()) { + wxString composed; + file.Read(wxStringBuffer(composed, n), sizeof(wchar_t)*n); + if (!file.Error()) { + // Restore state. + m_progress = true; + m_decomposed->SetValue(decomposed); + m_composed->SetValue(composed); + m_progress = false; + } + } + } + } + } + } } @@ -53,7 +80,9 @@ wxZRColaComposerPanel::~wxZRColaComposerPanel() m_decomposed->PopEventHandler(); // This is a controlled exit. Purge saved state. - wxPersistentZRColaComposerPanel(this).Clear(); + wxString fileName(GetStateFileName()); + if (wxFileExists(fileName)) + wxRemoveFile(fileName); } @@ -174,12 +203,64 @@ void wxZRColaComposerPanel::OnComposedText(wxCommandEvent& event) void wxZRColaComposerPanel::OnTimerTimeout(wxTimerEvent& event) { - wxPersistentZRColaComposerPanel(this).Save(); + wxString fileName(GetStateFileName()); + wxFFile file(fileName, wxT("wb")); + if (file.IsOpened()) { + // Save decomposed text. + { +#ifdef __WINDOWS__ + // Use Windows GetWindowText() function to avoid line ending conversion incompletely imposed by wxWidgets. + WXHWND hWnd = m_decomposed->GetHWND(); + std::vector text((std::vector::size_type)::GetWindowTextLengthW(hWnd) + 1); + ::GetWindowTextW(hWnd, text.data(), text.size()); + unsigned __int64 n = text.size() - 1; +#else + wxString text(m_decomposed->GetValue()); + unsigned __int64 n = text.size(); +#endif + file.Write(&n, sizeof(n)); + file.Write(text.data(), sizeof(wchar_t)*n); + } + + // Save composed text. + { +#ifdef __WINDOWS__ + // Use Windows GetWindowText() function to avoid line ending conversion incompletely imposed by wxWidgets. + WXHWND hWnd = m_composed->GetHWND(); + std::vector text((std::vector::size_type)::GetWindowTextLengthW(hWnd) + 1); + ::GetWindowTextW(hWnd, text.data(), text.size()); + unsigned __int64 n = text.size() - 1; +#else + wxString text(m_composed->GetValue()); + unsigned __int64 n = text.size(); +#endif + file.Write(&n, sizeof(n)); + file.Write(text.data(), sizeof(wchar_t)*n); + } + } event.Skip(); } +wxString wxZRColaComposerPanel::GetStateFileName() const +{ + wxString path; + + path = wxFileName::GetTempDir(); + if (!wxEndsWithPathSeparator(path)) + path += wxFILE_SEP_PATH; + + if (!wxDirExists(path)) + wxMkdir(path); + + wxString fileName(path); + fileName += wxT("ZRColaComposerPanel-state.tmp"); + + return fileName; +} + + ////////////////////////////////////////////////////////////////////////// // wxPersistentZRColaComposerPanel ////////////////////////////////////////////////////////////////////////// @@ -197,111 +278,13 @@ wxString wxPersistentZRColaComposerPanel::GetKind() const void wxPersistentZRColaComposerPanel::Save() const { - wxString fileName(GetStateFileName()); - wxFFile file(fileName, wxT("wb")); - if (!file.IsOpened()) - return; - const wxZRColaComposerPanel * const wnd = static_cast(GetWindow()); - - // Save decomposed text. - { -#ifdef __WINDOWS__ - // Use Windows GetWindowText() function to avoid line ending conversion incompletely imposed by wxWidgets. - WXHWND hWnd = wnd->m_decomposed->GetHWND(); - std::vector text((std::vector::size_type)::GetWindowTextLengthW(hWnd) + 1); - ::GetWindowTextW(hWnd, text.data(), text.size()); - unsigned __int64 n = text.size() - 1; -#else - wxString text(m_decomposed->GetValue()); - unsigned __int64 n = text.size(); -#endif - file.Write(&n, sizeof(n)); - file.Write(text.data(), sizeof(wchar_t)*n); - } - - // Save composed text. - { -#ifdef __WINDOWS__ - // Use Windows GetWindowText() function to avoid line ending conversion incompletely imposed by wxWidgets. - WXHWND hWnd = wnd->m_composed->GetHWND(); - std::vector text((std::vector::size_type)::GetWindowTextLengthW(hWnd) + 1); - ::GetWindowTextW(hWnd, text.data(), text.size()); - unsigned __int64 n = text.size() - 1; -#else - wxString text(m_composed->GetValue()); - unsigned __int64 n = text.size(); -#endif - file.Write(&n, sizeof(n)); - file.Write(text.data(), sizeof(wchar_t)*n); - } } bool wxPersistentZRColaComposerPanel::Restore() { - wxString fileName(GetStateFileName()); - - if (!wxFileExists(fileName)) - return false; - - wxFFile file(fileName, wxT("rb")); - if (!file.IsOpened()) - return false; - wxZRColaComposerPanel * const wnd = static_cast(GetWindow()); - // Load decomposed text. - unsigned __int64 n; - file.Read(&n, sizeof(n)); - if (!file.Error()) { - wxString decomposed; - file.Read(wxStringBuffer(decomposed, n), sizeof(wchar_t)*n); - if (!file.Error()) { - // Load composed text. - file.Read(&n, sizeof(n)); - if (!file.Error()) { - wxString composed; - file.Read(wxStringBuffer(composed, n), sizeof(wchar_t)*n); - if (!file.Error()) { - // Restore state. - wnd->m_progress = true; - wnd->m_decomposed->SetValue(decomposed); - wnd->m_composed->SetValue(composed); - wnd->m_progress = false; - return true; - } - } - } - } - - return false; -} - - -void wxPersistentZRColaComposerPanel::Clear() const -{ - wxString fileName(GetStateFileName()); - - if (wxFileExists(fileName)) - wxRemoveFile(fileName); -} - - -wxString wxPersistentZRColaComposerPanel::GetStateFileName() const -{ - wxString path; - - path = wxFileName::GetTempDir(); - if (!wxEndsWithPathSeparator(path)) - path += wxFILE_SEP_PATH; - - if (!wxDirExists(path)) - wxMkdir(path); - - wxString fileName(path); - fileName += GetKind(); - fileName += wxT("-state.tmp"); - - return fileName; + return true; } diff --git a/ZRCola/zrcolacomppnl.h b/ZRCola/zrcolacomppnl.h index 0f356e2..8912d58 100644 --- a/ZRCola/zrcolacomppnl.h +++ b/ZRCola/zrcolacomppnl.h @@ -57,6 +57,8 @@ protected: virtual void OnTimerTimeout(wxTimerEvent& event); DECLARE_EVENT_TABLE() + wxString GetStateFileName() const; + protected: bool m_progress; ///< Boolean flag to avoid recursive updates of composed and decomposed text controls ZRCola::mapping_vector m_mapping1; ///< Character index mapping vector between decomposed and normalized text @@ -80,10 +82,6 @@ public: virtual wxString GetKind() const; virtual void Save() const; virtual bool Restore(); - virtual void Clear() const; - -protected: - wxString GetStateFileName() const; }; diff --git a/ZRCola/zrcolafrm.cpp b/ZRCola/zrcolafrm.cpp index 8b64f15..f9aa5a8 100644 --- a/ZRCola/zrcolafrm.cpp +++ b/ZRCola/zrcolafrm.cpp @@ -76,6 +76,9 @@ wxZRColaFrame::wxZRColaFrame() : // Set focus. m_panel->m_decomposed->SetFocus(); + // Arrange composer panel persistence. + wxPersistentRegisterAndRestore(m_panel); + // Register global hotkey(s). if (!RegisterHotKey(wxZRColaHKID_INVOKE_COMPOSE, wxMOD_WIN, VK_F5)) wxMessageBox(_("ZRCola keyboard shortcut Win+F5 could not be registered. Some functionality will not be available."), _("Warning"), wxOK | wxICON_WARNING);