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

@@ -1985,11 +1985,12 @@ class WXDLLIMPEXP_FWD_STC wxStyledTextEvent;
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class WXDLLIMPEXP_STC wxStyledTextCtrl : public wxControl class WXDLLIMPEXP_STC wxStyledTextCtrl : public wxControl,
, public wxTextEntryBase
#if wxUSE_TEXTCTRL #if wxUSE_TEXTCTRL
, public wxTextAreaBase public wxTextCtrlIface
#endif // wxUSE_TEXTCTRL #else // !wxUSE_TEXTCTRL
public wxTextEntryBase
#endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL
{ {
public: public:

View File

@@ -582,11 +582,13 @@ public:
virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt,
wxTextCoord *col, wxTextCoord *col,
wxTextCoord *row) const; wxTextCoord *row) const;
virtual wxString GetValue() const = 0;
virtual void SetValue(const wxString& value) = 0;
protected: protected:
// implementation of loading/saving // implementation of loading/saving
virtual bool DoLoadFile(const wxString& file, int fileType) = 0; virtual bool DoLoadFile(const wxString& file, int fileType);
virtual bool DoSaveFile(const wxString& file, int fileType) = 0; virtual bool DoSaveFile(const wxString& file, int fileType);
// the name of the last file loaded with LoadFile() which will be used by // the name of the last file loaded with LoadFile() which will be used by
@@ -611,6 +613,16 @@ class WXDLLIMPEXP_CORE wxTextCtrlIface : public wxTextAreaBase,
public: public:
wxTextCtrlIface() { } wxTextCtrlIface() { }
// wxTextAreaBase overrides
virtual wxString GetValue() const
{
return wxTextEntryBase::GetValue();
}
virtual void SetValue(const wxString& value)
{
wxTextEntryBase::SetValue(value);
}
private: private:
wxDECLARE_NO_COPY_CLASS(wxTextCtrlIface); wxDECLARE_NO_COPY_CLASS(wxTextCtrlIface);
}; };
@@ -683,6 +695,16 @@ public:
virtual bool GetStyle(long position, wxTextAttr& style); virtual bool GetStyle(long position, wxTextAttr& style);
virtual bool SetDefaultStyle(const wxTextAttr& style); virtual bool SetDefaultStyle(const wxTextAttr& style);
// wxTextAreaBase overrides
virtual wxString GetValue() const
{
return wxTextEntry::GetValue();
}
virtual void SetValue(const wxString& value)
{
wxTextEntry::SetValue(value);
}
protected: protected:
// override streambuf method // override streambuf method
#if wxHAS_TEXT_WINDOW_STREAM #if wxHAS_TEXT_WINDOW_STREAM

View File

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

View File

@@ -47,7 +47,7 @@
#include "wx/tokenzr.h" #include "wx/tokenzr.h"
#include "wx/mstream.h" #include "wx/mstream.h"
#include "wx/image.h" #include "wx/image.h"
#include "wx/file.h" #include "wx/ffile.h"
#include "ScintillaWX.h" #include "ScintillaWX.h"
@@ -3500,74 +3500,57 @@ void wxStyledTextCtrl::ScrollToColumn(int column) {
#if wxUSE_TEXTCTRL #if wxUSE_TEXTCTRL
bool wxStyledTextCtrl::DoSaveFile(const wxString& filename, int WXUNUSED(fileType)) bool wxStyledTextCtrl::DoSaveFile(const wxString& filename, int fileType)
{
bool ok = wxTextAreaBase::DoSaveFile(filename, fileType);
#else #else
bool wxStyledTextCtrl::SaveFile(const wxString& filename) bool wxStyledTextCtrl::SaveFile(const wxString& filename)
#endif
{ {
wxFile file(filename, wxFile::write); #if wxUSE_FFILE
wxFFile file(filename, wxT("w"));
if (!file.IsOpened()) bool ok = file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
return false; #else
bool ok = false;
bool success = file.Write(GetText(), *wxConvCurrent); #endif // wxUSE_FFILE
#endif
if (success) if (ok)
{
SetSavePoint(); SetSavePoint();
}
return success; return ok;
} }
#if wxUSE_TEXTCTRL #if wxUSE_TEXTCTRL
bool wxStyledTextCtrl::DoLoadFile(const wxString& filename, int WXUNUSED(fileType)) bool wxStyledTextCtrl::DoLoadFile(const wxString& filename, int fileType)
{
bool ok = wxTextAreaBase::DoLoadFile(filename, fileType);
#else #else
bool wxStyledTextCtrl::LoadFile(const wxString& filename) bool wxStyledTextCtrl::LoadFile(const wxString& filename)
#endif
{ {
bool success = false; #if wxUSE_FFILE
wxFile file(filename, wxFile::read); wxFFile file(filename);
bool ok = file.IsOpened();
if (file.IsOpened()) if (ok)
{ {
wxString contents; wxString text;
// get the file size (assume it is not huge file...) ok = file.ReadAll(&text, *wxConvCurrent);
ssize_t len = (ssize_t)file.Length(); if (ok)
if (len > 0)
{ {
#if wxUSE_UNICODE SetValue(text);
wxMemoryBuffer buffer(len+1);
success = (file.Read(buffer.GetData(), len) == len);
if (success) {
((char*)buffer.GetData())[len] = 0;
contents = wxString(buffer, *wxConvCurrent, len);
}
#else
wxString buffer;
success = (file.Read(wxStringBuffer(buffer, len), len) == len);
contents = buffer;
#endif
}
else
{
if (len == 0)
success = true; // empty file is ok
else
success = false; // len == wxInvalidOffset
}
if (success)
{
SetText(contents);
EmptyUndoBuffer();
SetSavePoint();
} }
} }
#else
return success; bool ok = false;
#endif // wxUSE_FFILE
#endif
if (ok)
{
EmptyUndoBuffer();
SetSavePoint();
}
return ok;
} }
#if wxUSE_DRAG_AND_DROP #if wxUSE_DRAG_AND_DROP
wxDragResult wxStyledTextCtrl::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) { wxDragResult wxStyledTextCtrl::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
return m_swx->DoDragOver(x, y, def); return m_swx->DoDragOver(x, y, def);

View File

@@ -47,7 +47,7 @@
#include "wx/tokenzr.h" #include "wx/tokenzr.h"
#include "wx/mstream.h" #include "wx/mstream.h"
#include "wx/image.h" #include "wx/image.h"
#include "wx/file.h" #include "wx/ffile.h"
#include "ScintillaWX.h" #include "ScintillaWX.h"
@@ -509,74 +509,57 @@ void wxStyledTextCtrl::ScrollToColumn(int column) {
#if wxUSE_TEXTCTRL #if wxUSE_TEXTCTRL
bool wxStyledTextCtrl::DoSaveFile(const wxString& filename, int WXUNUSED(fileType)) bool wxStyledTextCtrl::DoSaveFile(const wxString& filename, int fileType)
{
bool ok = wxTextAreaBase::DoSaveFile(filename, fileType);
#else #else
bool wxStyledTextCtrl::SaveFile(const wxString& filename) bool wxStyledTextCtrl::SaveFile(const wxString& filename)
#endif
{ {
wxFile file(filename, wxFile::write); #if wxUSE_FFILE
wxFFile file(filename, wxT("w"));
if (!file.IsOpened()) bool ok = file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
return false; #else
bool ok = false;
bool success = file.Write(GetText(), *wxConvCurrent); #endif // wxUSE_FFILE
#endif
if (success) if (ok)
{
SetSavePoint(); SetSavePoint();
}
return success; return ok;
} }
#if wxUSE_TEXTCTRL #if wxUSE_TEXTCTRL
bool wxStyledTextCtrl::DoLoadFile(const wxString& filename, int WXUNUSED(fileType)) bool wxStyledTextCtrl::DoLoadFile(const wxString& filename, int fileType)
{
bool ok = wxTextAreaBase::DoLoadFile(filename, fileType);
#else #else
bool wxStyledTextCtrl::LoadFile(const wxString& filename) bool wxStyledTextCtrl::LoadFile(const wxString& filename)
#endif
{ {
bool success = false; #if wxUSE_FFILE
wxFile file(filename, wxFile::read); wxFFile file(filename);
bool ok = file.IsOpened();
if (file.IsOpened()) if (ok)
{ {
wxString contents; wxString text;
// get the file size (assume it is not huge file...) ok = file.ReadAll(&text, *wxConvCurrent);
ssize_t len = (ssize_t)file.Length(); if (ok)
if (len > 0)
{ {
#if wxUSE_UNICODE SetValue(text);
wxMemoryBuffer buffer(len+1);
success = (file.Read(buffer.GetData(), len) == len);
if (success) {
((char*)buffer.GetData())[len] = 0;
contents = wxString(buffer, *wxConvCurrent, len);
}
#else
wxString buffer;
success = (file.Read(wxStringBuffer(buffer, len), len) == len);
contents = buffer;
#endif
}
else
{
if (len == 0)
success = true; // empty file is ok
else
success = false; // len == wxInvalidOffset
}
if (success)
{
SetText(contents);
EmptyUndoBuffer();
SetSavePoint();
} }
} }
#else
return success; bool ok = false;
#endif // wxUSE_FFILE
#endif
if (ok)
{
EmptyUndoBuffer();
SetSavePoint();
}
return ok;
} }
#if wxUSE_DRAG_AND_DROP #if wxUSE_DRAG_AND_DROP
wxDragResult wxStyledTextCtrl::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) { wxDragResult wxStyledTextCtrl::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
return m_swx->DoDragOver(x, y, def); return m_swx->DoDragOver(x, y, def);

View File

@@ -84,11 +84,12 @@ class WXDLLIMPEXP_FWD_STC wxStyledTextEvent;
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class WXDLLIMPEXP_STC wxStyledTextCtrl : public wxControl class WXDLLIMPEXP_STC wxStyledTextCtrl : public wxControl,
, public wxTextEntryBase
#if wxUSE_TEXTCTRL #if wxUSE_TEXTCTRL
, public wxTextAreaBase public wxTextCtrlIface
#endif // wxUSE_TEXTCTRL #else // !wxUSE_TEXTCTRL
public wxTextEntryBase
#endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL
{ {
public: public: