Code to Save & Restore text moved to wxZRColaComposerPanel to avoid confusion with GUI persistence

This commit is contained in:
Simon Rozman 2016-04-22 14:24:17 +02:00
parent 82d2fc42bd
commit 7a424da3fc
3 changed files with 90 additions and 106 deletions

View File

@ -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<wchar_t> text((std::vector<wchar_t>::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<wchar_t> text((std::vector<wchar_t>::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<const wxZRColaComposerPanel*>(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<wchar_t> text((std::vector<wchar_t>::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<wchar_t> text((std::vector<wchar_t>::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<wxZRColaComposerPanel*>(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;
}

View File

@ -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;
};

View File

@ -76,6 +76,9 @@ wxZRColaFrame::wxZRColaFrame() :
// Set focus.
m_panel->m_decomposed->SetFocus();
// Arrange composer panel persistence.
wxPersistentRegisterAndRestore<wxZRColaComposerPanel>(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);