Refactor wxStyledTextCtrl to share common file save/load code.

Keep the code for saving and loading text contents from files in a single
place instead of doing it differently in wxTextCtrl and wxStyledTextCtrl.

This required adding Set/GetValue() methods to wxTextAreaBase just so that its
DoLoad/SaveFile() could use them, even if they are the same as wxTextEntryBase
methods and are overridden in wxTextCtrlBase to be implemented in terms of the
latter.

Notice that wxRichTextCtrl might need to be refactored to use this code too in
the future.

Also notice that this reverts the change of r62081 which replaced SetValue()
with ChangeValue() in DoLoadFile() as wxTextAreaBase only has SetValue() and
it's not worth adding ChangeValue() to it too just to preserve this recent
change in behaviour.

Closes #10715.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62139 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-09-26 13:26:16 +00:00
parent 65702d2fe9
commit 3396739da1
6 changed files with 132 additions and 132 deletions

View File

@@ -736,7 +736,7 @@ bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr& style)
// file IO functions
// ----------------------------------------------------------------------------
bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType))
bool wxTextAreaBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType))
{
#if wxUSE_FFILE
wxFFile file(filename);
@@ -745,22 +745,38 @@ bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType)
wxString text;
if ( file.ReadAll(&text) )
{
ChangeValue(text);
DiscardEdits();
m_filename = filename;
SetValue(text);
return true;
}
}
wxLogError(_("File couldn't be loaded."));
#endif // wxUSE_FFILE
return false;
}
bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int fileType)
{
if ( wxTextAreaBase::DoLoadFile(filename, fileType) )
{
DiscardEdits();
m_filename = filename;
return true;
}
wxLogError(_("File couldn't be loaded."));
return false;
}
bool wxTextAreaBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
{
#if wxUSE_FFILE
wxFFile file(filename, wxT("w"));
return file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
#else
return false;
#endif // wxUSE_FFILE
}
bool wxTextAreaBase::SaveFile(const wxString& filename, int fileType)
{
wxString filenameToUse = filename.empty() ? m_filename : filename;
@@ -775,11 +791,9 @@ bool wxTextAreaBase::SaveFile(const wxString& filename, int fileType)
return DoSaveFile(filenameToUse, fileType);
}
bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int fileType)
{
#if wxUSE_FFILE
wxFFile file(filename, wxT("w"));
if ( file.IsOpened() && file.Write(GetValue()) )
if ( wxTextAreaBase::DoSaveFile(filename, fileType) )
{
// if it worked, save for future calls
m_filename = filename;
@@ -789,10 +803,6 @@ bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType)
return true;
}
#endif // wxUSE_FFILE
wxLogError(_("The text couldn't be saved."));
return false;
}