From 2bfca191bf2261e385594a369da9aa4e4441aa43 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 15 Jun 2008 00:59:38 +0000 Subject: [PATCH] derive wxSTC from wxTextAreaBase to provide wxTextCtrl-like methods (see #9114) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54225 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/stc/stc.h | 101 ++++++++++++++++++++++++++++++++++++++++--- src/stc/gen_iface.py | 23 +++++++--- src/stc/stc.cpp | 18 +++++--- src/stc/stc.cpp.in | 8 ++++ src/stc/stc.h.in | 91 +++++++++++++++++++++++++++++++++++++- 5 files changed, 221 insertions(+), 20 deletions(-) diff --git a/include/wx/stc/stc.h b/include/wx/stc/stc.h index e9cbffab90..c766ced458 100644 --- a/include/wx/stc/stc.h +++ b/include/wx/stc/stc.h @@ -39,6 +39,10 @@ #include "wx/dnd.h" #include "wx/stopwatch.h" +#if wxUSE_TEXTCTRL + #include "wx/textctrl.h" +#endif // wxUSE_TEXTCTRL + class WXDLLIMPEXP_FWD_CORE wxScrollBar; // SWIG can't handle "#if" type of conditionals, only "#ifdef" @@ -1981,6 +1985,9 @@ class WXDLLIMPEXP_FWD_STC wxStyledTextEvent; //---------------------------------------------------------------------- class WXDLLIMPEXP_STC wxStyledTextCtrl : public wxControl +#if wxUSE_TEXTCTRL + , public wxTextAreaBase +#endif // wxUSE_TEXTCTRL { public: @@ -2081,7 +2088,7 @@ public: void SetViewWhiteSpace(int viewWS); // Find the position from a point within the window. - int PositionFromPoint(wxPoint pt); + int PositionFromPoint(wxPoint pt) const; // Find the position from a point within the window but return // INVALID_POSITION if not close to text. @@ -2586,7 +2593,7 @@ public: int GetFirstVisibleLine() const; // Retrieve the contents of a line. - wxString GetLine(int line); + wxString GetLine(int line) const; // Returns the number of lines in the document. There is always at least one. int GetLineCount() const; @@ -2619,10 +2626,10 @@ public: void HideSelection(bool normal); // Retrieve the line containing a position. - int LineFromPosition(int pos); + int LineFromPosition(int pos) const; // Retrieve the position at the start of a line. - int PositionFromLine(int line); + int PositionFromLine(int line) const; // Scroll horizontally and vertically. void LineScroll(int columns, int lines); @@ -3087,7 +3094,7 @@ public: void MoveCaretInsideView(); // How many characters are on a line, not including end of line characters? - int LineLength(int line); + int LineLength(int line) const; // Highlight the characters at two positions. void BraceHighlight(int pos1, int pos2); @@ -3576,11 +3583,14 @@ public: bool GetLastKeydownProcessed() { return m_lastKeyDownConsumed; } void SetLastKeydownProcessed(bool val) { m_lastKeyDownConsumed = val; } + // if we derive from wxTextAreaBase it already provides these methods +#if !wxUSE_TEXTCTRL // Write the contents of the editor to filename bool SaveFile(const wxString& filename); // Load the contents of filename into the editor bool LoadFile(const wxString& filename); +#endif // !wxUSE_TEXTCTRL #ifdef STC_USE_DND // Allow for simulating a DnD DragOver @@ -3641,11 +3651,88 @@ public: #ifdef SWIG %pythoncode "_stc_utf8_methods.py" #endif -//---------------------------------------------------------------------- + // implement wxTextAreaBase pure virtual methods + // --------------------------------------------- + + virtual int GetLineLength(long n) const { return GetLine(n).length(); } + virtual wxString GetLineText(long n) const { return GetLine(n); } + virtual int GetNumberOfLines() const { return GetLineCount(); } + + virtual bool IsModified() const { return GetModify(); } + virtual void MarkDirty() { wxFAIL_MSG("not implemented"); } + virtual void DiscardEdits() { SetSavePoint(); } + + virtual bool SetStyle(long WXUNUSED(start), long WXUNUSED(end), + const wxTextAttr& WXUNUSED(style)) + { + wxFAIL_MSG("not implemented"); + + return false; + } + + virtual bool GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style)) + { + wxFAIL_MSG("not implemented"); + + return false; + } + + virtual bool SetDefaultStyle(const wxTextAttr& WXUNUSED(style)) + { + wxFAIL_MSG("not implemented"); + + return false; + } + + virtual long XYToPosition(long x, long y) const + { + long pos = PositionFromLine(y); + pos += x; + return pos; + } + + virtual bool PositionToXY(long pos, long *x, long *y) const + { + if ( x ) + *x = -1; // TODO + + if ( y ) + { + long l = LineFromPosition(pos); + if ( l == -1 ) + return false; + *y = l; + } + + return true; + } + + virtual void ShowPosition(long pos) + { + EnsureVisible(LineFromPosition(pos)); + } + + using wxWindow::HitTest; + + virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const + { + const long l = PositionFromPoint(pt); + if ( l == -1 ) + return wxTE_HT_BELOW; // we don't really know where it was + + if ( pos ) + *pos = l; + + return wxTE_HT_ON_TEXT; + } + #ifndef SWIG protected: + virtual bool DoLoadFile(const wxString& file, int fileType); + virtual bool DoSaveFile(const wxString& file, int fileType); + // Event handlers void OnPaint(wxPaintEvent& evt); void OnScrollWin(wxScrollWinEvent& evt); @@ -3689,7 +3776,7 @@ protected: friend class ScintillaWX; friend class Platform; -#endif +#endif // !SWIG }; //---------------------------------------------------------------------- diff --git a/src/stc/gen_iface.py b/src/stc/gen_iface.py index bde1a8447d..7dbc277c21 100755 --- a/src/stc/gen_iface.py +++ b/src/stc/gen_iface.py @@ -21,7 +21,7 @@ H_TEMPLATE = os.path.abspath('./stc.h.in') CPP_TEMPLATE = os.path.abspath('./stc.cpp.in') H_DEST = os.path.abspath('../../include/wx/stc/stc.h') CPP_DEST = os.path.abspath('./stc.cpp') -DOCSTR_DEST = os.path.abspath('../../../wxPython/contrib/stc/_stc_gendocs.i') +DOCSTR_DEST = '/dev/null' #os.path.abspath('../../../wxPython/contrib/stc/_stc_gendocs.i') # Value prefixes to convert @@ -144,9 +144,9 @@ methodOverrideMap = { 'PositionFromPoint' : (0, - 'int %s(wxPoint pt);', + 'int %s(wxPoint pt) const;', - '''int %s(wxPoint pt) { + '''int %s(wxPoint pt) const { return SendMsg(%s, pt.x, pt.y);''', 0), @@ -410,9 +410,9 @@ methodOverrideMap = { 'GetLine' : (0, - 'wxString %s(int line);', + 'wxString %s(int line) const;', - '''wxString %s(int line) { + '''wxString %s(int line) const { int len = LineLength(line); if (!len) return wxEmptyString; @@ -655,6 +655,16 @@ methodOverrideMap = { } +# all Scintilla getters are transformed into const member of wxSTC class but +# some non-getter methods are also logically const and this set contains their +# names (notice that it's useless to include here methods manually overridden +# above) +constNonGetterMethods = set(( + 'LineFromPosition', + 'PositionFromLine', + 'LineLength', +)) + #---------------------------------------------------------------------------- def processIface(iface, h_tmplt, cpp_tmplt, h_dest, cpp_dest, docstr_dest): @@ -904,7 +914,8 @@ def parseFun(line, methods, docs, values, is_const): if not FUNC_FOR_CMD: return - methods.append( (retType, name, number, param1, param2, tuple(docs), is_const) ) + methods.append( (retType, name, number, param1, param2, tuple(docs), + is_const or name in constNonGetterMethods) ) #---------------------------------------------------------------------------- diff --git a/src/stc/stc.cpp b/src/stc/stc.cpp index c91d46eb49..1b03a9725c 100644 --- a/src/stc/stc.cpp +++ b/src/stc/stc.cpp @@ -393,7 +393,7 @@ void wxStyledTextCtrl::SetViewWhiteSpace(int viewWS) } // Find the position from a point within the window. -int wxStyledTextCtrl::PositionFromPoint(wxPoint pt) { +int wxStyledTextCtrl::PositionFromPoint(wxPoint pt) const { return SendMsg(2022, pt.x, pt.y); } @@ -1439,7 +1439,7 @@ int wxStyledTextCtrl::GetFirstVisibleLine() const } // Retrieve the contents of a line. -wxString wxStyledTextCtrl::GetLine(int line) { +wxString wxStyledTextCtrl::GetLine(int line) const { int len = LineLength(line); if (!len) return wxEmptyString; @@ -1538,13 +1538,13 @@ void wxStyledTextCtrl::HideSelection(bool normal) } // Retrieve the line containing a position. -int wxStyledTextCtrl::LineFromPosition(int pos) +int wxStyledTextCtrl::LineFromPosition(int pos) const { return SendMsg(2166, pos, 0); } // Retrieve the position at the start of a line. -int wxStyledTextCtrl::PositionFromLine(int line) +int wxStyledTextCtrl::PositionFromLine(int line) const { return SendMsg(2167, line, 0); } @@ -2464,7 +2464,7 @@ void wxStyledTextCtrl::MoveCaretInsideView() } // How many characters are on a line, not including end of line characters? -int wxStyledTextCtrl::LineLength(int line) +int wxStyledTextCtrl::LineLength(int line) const { return SendMsg(2350, line, 0); } @@ -3513,7 +3513,11 @@ void wxStyledTextCtrl::ScrollToColumn(int column) { } +#if wxUSE_TEXTCTRL +bool wxStyledTextCtrl::DoSaveFile(const wxString& filename, int WXUNUSED(fileType)) +#else bool wxStyledTextCtrl::SaveFile(const wxString& filename) +#endif { wxFile file(filename, wxFile::write); @@ -3528,7 +3532,11 @@ bool wxStyledTextCtrl::SaveFile(const wxString& filename) return success; } +#if wxUSE_TEXTCTRL +bool wxStyledTextCtrl::DoLoadFile(const wxString& filename, int WXUNUSED(fileType)) +#else bool wxStyledTextCtrl::LoadFile(const wxString& filename) +#endif { bool success = false; wxFile file(filename, wxFile::read); diff --git a/src/stc/stc.cpp.in b/src/stc/stc.cpp.in index 47660f78f7..637f4c09bd 100644 --- a/src/stc/stc.cpp.in +++ b/src/stc/stc.cpp.in @@ -516,7 +516,11 @@ void wxStyledTextCtrl::ScrollToColumn(int column) { } +#if wxUSE_TEXTCTRL +bool wxStyledTextCtrl::DoSaveFile(const wxString& filename, int WXUNUSED(fileType)) +#else bool wxStyledTextCtrl::SaveFile(const wxString& filename) +#endif { wxFile file(filename, wxFile::write); @@ -531,7 +535,11 @@ bool wxStyledTextCtrl::SaveFile(const wxString& filename) return success; } +#if wxUSE_TEXTCTRL +bool wxStyledTextCtrl::DoLoadFile(const wxString& filename, int WXUNUSED(fileType)) +#else bool wxStyledTextCtrl::LoadFile(const wxString& filename) +#endif { bool success = false; wxFile file(filename, wxFile::read); diff --git a/src/stc/stc.h.in b/src/stc/stc.h.in index 344330b079..8a6abafc25 100644 --- a/src/stc/stc.h.in +++ b/src/stc/stc.h.in @@ -39,6 +39,10 @@ #include "wx/dnd.h" #include "wx/stopwatch.h" +#if wxUSE_TEXTCTRL + #include "wx/textctrl.h" +#endif // wxUSE_TEXTCTRL + class WXDLLIMPEXP_FWD_CORE wxScrollBar; // SWIG can't handle "#if" type of conditionals, only "#ifdef" @@ -80,6 +84,9 @@ class WXDLLIMPEXP_FWD_STC wxStyledTextEvent; //---------------------------------------------------------------------- class WXDLLIMPEXP_STC wxStyledTextCtrl : public wxControl +#if wxUSE_TEXTCTRL + , public wxTextAreaBase +#endif // wxUSE_TEXTCTRL { public: @@ -209,11 +216,14 @@ public: bool GetLastKeydownProcessed() { return m_lastKeyDownConsumed; } void SetLastKeydownProcessed(bool val) { m_lastKeyDownConsumed = val; } + // if we derive from wxTextAreaBase it already provides these methods +#if !wxUSE_TEXTCTRL // Write the contents of the editor to filename bool SaveFile(const wxString& filename); // Load the contents of filename into the editor bool LoadFile(const wxString& filename); +#endif // !wxUSE_TEXTCTRL #ifdef STC_USE_DND // Allow for simulating a DnD DragOver @@ -274,11 +284,88 @@ public: #ifdef SWIG %%pythoncode "_stc_utf8_methods.py" #endif -//---------------------------------------------------------------------- + // implement wxTextAreaBase pure virtual methods + // --------------------------------------------- + + virtual int GetLineLength(long n) const { return GetLine(n).length(); } + virtual wxString GetLineText(long n) const { return GetLine(n); } + virtual int GetNumberOfLines() const { return GetLineCount(); } + + virtual bool IsModified() const { return GetModify(); } + virtual void MarkDirty() { wxFAIL_MSG("not implemented"); } + virtual void DiscardEdits() { SetSavePoint(); } + + virtual bool SetStyle(long WXUNUSED(start), long WXUNUSED(end), + const wxTextAttr& WXUNUSED(style)) + { + wxFAIL_MSG("not implemented"); + + return false; + } + + virtual bool GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style)) + { + wxFAIL_MSG("not implemented"); + + return false; + } + + virtual bool SetDefaultStyle(const wxTextAttr& WXUNUSED(style)) + { + wxFAIL_MSG("not implemented"); + + return false; + } + + virtual long XYToPosition(long x, long y) const + { + long pos = PositionFromLine(y); + pos += x; + return pos; + } + + virtual bool PositionToXY(long pos, long *x, long *y) const + { + if ( x ) + *x = -1; // TODO + + if ( y ) + { + long l = LineFromPosition(pos); + if ( l == -1 ) + return false; + *y = l; + } + + return true; + } + + virtual void ShowPosition(long pos) + { + EnsureVisible(LineFromPosition(pos)); + } + + using wxWindow::HitTest; + + virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const + { + const long l = PositionFromPoint(pt); + if ( l == -1 ) + return wxTE_HT_BELOW; // we don't really know where it was + + if ( pos ) + *pos = l; + + return wxTE_HT_ON_TEXT; + } + #ifndef SWIG protected: + virtual bool DoLoadFile(const wxString& file, int fileType); + virtual bool DoSaveFile(const wxString& file, int fileType); + // Event handlers void OnPaint(wxPaintEvent& evt); void OnScrollWin(wxScrollWinEvent& evt); @@ -322,7 +409,7 @@ protected: friend class ScintillaWX; friend class Platform; -#endif +#endif // !SWIG }; //---------------------------------------------------------------------- -- 2.47.2