From fa2f57be4db838388659fe48dbf5546daf5364f8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 26 Sep 2007 16:48:46 +0000 Subject: [PATCH] refactor wxMSW code to extract parts common to wxTextCtrl and wxComboBox into wxTextEntry git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48952 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/textctrl.h | 83 ++++------- include/wx/msw/textentry.h | 67 +++++++++ include/wx/textctrl.h | 7 +- include/wx/textentry.h | 2 + src/common/textcmn.cpp | 4 +- src/common/textentrycmn.cpp | 12 +- src/msw/combobox.cpp | 223 +--------------------------- src/msw/textctrl.cpp | 288 ++++++++---------------------------- src/msw/textentry.cpp | 154 +++++++++++++++++++ 9 files changed, 334 insertions(+), 506 deletions(-) create mode 100644 include/wx/msw/textentry.h create mode 100644 src/msw/textentry.cpp diff --git a/include/wx/msw/textctrl.h b/include/wx/msw/textctrl.h index a56a3ff7d6..f5fc2fb35b 100644 --- a/include/wx/msw/textctrl.h +++ b/include/wx/msw/textctrl.h @@ -41,45 +41,42 @@ public: const wxValidator& validator = wxDefaultValidator, const wxString& name = wxTextCtrlNameStr); - // implement base class pure virtuals - // ---------------------------------- + // overridden wxTextEntry methods + // ------------------------------ virtual wxString GetValue() const; + virtual wxString GetRange(long from, long to) const; + virtual bool IsEmpty() const; - virtual wxString GetRange(long from, long to) const; + virtual void WriteText(const wxString& text); + virtual void AppendText(const wxString& text); + virtual void Clear(); virtual int GetLineLength(long lineNo) const; virtual wxString GetLineText(long lineNo) const; virtual int GetNumberOfLines() const; - virtual bool IsModified() const; - virtual bool IsEditable() const; + virtual void SetMaxLength(unsigned long len); - virtual void GetSelection(long* from, long* to) const; + virtual void GetSelection(long *from, long *to) const; - // operations - // ---------- + virtual void Redo(); + virtual bool CanRedo() const; - // editing - virtual void Clear(); - virtual void Replace(long from, long to, const wxString& value); - virtual void Remove(long from, long to); + virtual void SetInsertionPointEnd(); + virtual long GetInsertionPoint() const; + virtual wxTextPos GetLastPosition() const; + + // implement base class pure virtuals + // ---------------------------------- - // load the control's contents from the file virtual bool DoLoadFile(const wxString& file, int fileType); - // clears the dirty flag + virtual bool IsModified() const; virtual void MarkDirty(); virtual void DiscardEdits(); - virtual void SetMaxLength(unsigned long len); - - // writing text inserts it at the current position, appending always - // inserts it at the end - virtual void WriteText(const wxString& text); - virtual void AppendText(const wxString& text); - #ifdef __WIN32__ virtual bool EmulateKeyPress(const wxKeyEvent& event); #endif // __WIN32__ @@ -107,33 +104,7 @@ public: return wxTextCtrlBase::HitTest(pt, col, row); } - // Clipboard operations - virtual void Copy(); - virtual void Cut(); - virtual void Paste(); - - virtual bool CanCopy() const; - virtual bool CanCut() const; - virtual bool CanPaste() const; - - // Undo/redo - virtual void Undo(); - virtual void Redo(); - - virtual bool CanUndo() const; - virtual bool CanRedo() const; - - // Insertion point - virtual void SetInsertionPoint(long pos); - virtual void SetInsertionPointEnd(); - virtual long GetInsertionPoint() const; - virtual wxTextPos GetLastPosition() const; - - virtual void SetSelection(long from, long to); - virtual void SetEditable(bool editable); - // Caret handling (Windows only) - bool ShowNativeCaret(bool show = true); bool HideNativeCaret() { return ShowNativeCaret(false); } @@ -172,7 +143,7 @@ public: virtual void AdoptAttributesFromHWND(); - virtual bool AcceptsFocus() const; + virtual bool AcceptsFocusFromKeyboard() const; // returns true if the platform should explicitly apply a theme border virtual bool CanApplyThemeBorder() const; @@ -227,6 +198,10 @@ protected: virtual void DoSetValue(const wxString &value, int flags = 0); + // implement wxTextEntry pure virtual: it implements all the operations for + // the simple EDIT controls + virtual WXHWND GetEditHWND() const { return m_hWnd; } + // return true if this control has a user-set limit on amount of text (i.e. // the limit is due to a previous call to SetMaxLength() and not built in) bool HasSpaceLimit(unsigned int *len) const; @@ -252,8 +227,8 @@ protected: void DoWriteText(const wxString& text, int flags = SetValue_SendEvent | SetValue_SelectionOnly); - // set the selection possibly without scrolling the caret into view - void DoSetSelection(long from, long to, bool scrollCaret = true); + // set the selection (possibly without scrolling the caret into view) + void DoSetSelection(long from, long to, int flags); // return true if there is a non empty selection in the control bool HasSelection() const; @@ -278,6 +253,11 @@ protected: // text ourselves: we want this to be exactly 1 int m_updatesCount; + virtual void EnableTextChangedEvents(bool enable) + { + m_updatesCount = enable ? -1 : -2; + } + private: DECLARE_EVENT_TABLE() DECLARE_DYNAMIC_CLASS_NO_COPY(wxTextCtrl) @@ -292,5 +272,4 @@ private: }; -#endif - // _WX_TEXTCTRL_H_ +#endif // _WX_TEXTCTRL_H_ diff --git a/include/wx/msw/textentry.h b/include/wx/msw/textentry.h new file mode 100644 index 0000000000..a39139df66 --- /dev/null +++ b/include/wx/msw/textentry.h @@ -0,0 +1,67 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: wx/msw/textentry.h +// Purpose: wxMSW-specific wxTextEntry implementation +// Author: Vadim Zeitlin +// Created: 2007-09-26 +// RCS-ID: $Id: textentry.h 48944 2007-09-26 00:30:22Z VZ $ +// Copyright: (c) 2007 Vadim Zeitlin +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_MSW_TEXTENTRY_H_ +#define _WX_MSW_TEXTENTRY_H_ + +// ---------------------------------------------------------------------------- +// wxTextEntry: common part of wxComboBox and (single line) wxTextCtrl +// ---------------------------------------------------------------------------- + +class WXDLLIMPEXP_CORE wxTextEntry : public wxTextEntryBase +{ +public: + wxTextEntry() { } + + // implement wxTextEntryBase pure virtual methods + virtual void WriteText(const wxString& text); + virtual wxString GetValue() const; + virtual void Remove(long from, long to); + + virtual void Copy(); + virtual void Cut(); + virtual void Paste(); + + virtual void Undo(); + virtual void Redo(); + virtual bool CanUndo() const; + virtual bool CanRedo() const; + + virtual void SetInsertionPoint(long pos); + virtual long GetInsertionPoint() const; + virtual long GetLastPosition() const; + + virtual void SetSelection(long from, long to) + { DoSetSelection(from, to); } + virtual void GetSelection(long *from, long *to) const; + + virtual bool IsEditable() const; + virtual void SetEditable(bool editable); + + virtual void SetMaxLength(unsigned long len); + +protected: + // this is really a hook for multiline text controls as the single line + // ones don't need to ever scroll to show the selection but having it here + // allows us to put Remove() in the base class + enum + { + SetSel_NoScroll = 0, // don't do anything special + SetSel_Scroll = 1 // default: scroll to make the selection visible + }; + virtual void DoSetSelection(long from, long to, int flags = SetSel_Scroll); + +private: + // implement this to return the HWND of the EDIT control + virtual WXHWND GetEditHWND() const = 0; +}; + +#endif // _WX_MSW_TEXTENTRY_H_ + diff --git a/include/wx/textctrl.h b/include/wx/textctrl.h index 7bdf27a74b..7c8be51720 100644 --- a/include/wx/textctrl.h +++ b/include/wx/textctrl.h @@ -411,9 +411,10 @@ public: virtual bool EmulateKeyPress(const wxKeyEvent& event); - // generate the wxEVT_COMMAND_TEXT_UPDATED event, like SetValue() does - static void SendTextUpdatedEvent(wxWindow *win); - void SendTextUpdatedEvent() { SendTextUpdatedEvent(this); } + // generate the wxEVT_COMMAND_TEXT_UPDATED event, like SetValue() does and + // return true if the event was processed + static bool SendTextUpdatedEvent(wxWindow *win); + bool SendTextUpdatedEvent() { return SendTextUpdatedEvent(this); } // do the window-specific processing after processing the update event virtual void DoUpdateWindowUI(wxUpdateUIEvent& event); diff --git a/include/wx/textentry.h b/include/wx/textentry.h index d7ba22c10b..3050074282 100644 --- a/include/wx/textentry.h +++ b/include/wx/textentry.h @@ -180,6 +180,8 @@ private: #ifdef __WXGTK20__ #include "wx/gtk/textentry.h" +#elif defined(__WXMSW__) + #include "wx/msw/textentry.h" #else // no platform-specific implementation of wxTextEntry yet class WXDLLIMPEXP_CORE wxTextEntry : public wxTextEntryBase diff --git a/src/common/textcmn.cpp b/src/common/textcmn.cpp index 0e7f869b63..8fa4ad28c9 100644 --- a/src/common/textcmn.cpp +++ b/src/common/textcmn.cpp @@ -468,7 +468,7 @@ wxTextAreaBase::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(pos)) const // ---------------------------------------------------------------------------- /* static */ -void wxTextCtrlBase::SendTextUpdatedEvent(wxWindow *win) +bool wxTextCtrlBase::SendTextUpdatedEvent(wxWindow *win) { wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, win->GetId()); @@ -479,7 +479,7 @@ void wxTextCtrlBase::SendTextUpdatedEvent(wxWindow *win) //event.SetString(win->GetValue()); event.SetEventObject(win); - win->GetEventHandler()->ProcessEvent(event); + return win->GetEventHandler()->ProcessEvent(event); } #else // !wxUSE_TEXTCTRL diff --git a/src/common/textentrycmn.cpp b/src/common/textentrycmn.cpp index 67443dcdad..956d5d5914 100644 --- a/src/common/textentrycmn.cpp +++ b/src/common/textentrycmn.cpp @@ -29,6 +29,7 @@ #endif //WX_PRECOMP #include "wx/textentry.h" +#include "wx/clipbrd.h" // ============================================================================ // wxTextEntryBase implementation @@ -97,7 +98,16 @@ bool wxTextEntryBase::CanCut() const bool wxTextEntryBase::CanPaste() const { - return IsEditable(); + if ( IsEditable() ) + { +#if wxUSE_CLIPBOARD + // check if there is any text on the clipboard + if ( wxTheClipboard->IsSupported(wxDF_TEXT) ) + return true; +#endif // wxUSE_CLIPBOARD + } + + return false; } #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX diff --git a/src/msw/combobox.cpp b/src/msw/combobox.cpp index 60aa76a568..5b1fc63868 100644 --- a/src/msw/combobox.cpp +++ b/src/msw/combobox.cpp @@ -395,7 +395,7 @@ WXHWND wxComboBox::GetEditHWND() const { // this function should not be called for wxCB_READONLY controls, it is // the callers responsability to check this - wxASSERT_MSG( !(GetWindowStyle() & wxCB_READONLY), + wxASSERT_MSG( !HasFlag(wxCB_READONLY), _T("read-only combobox doesn't have any edit control") ); POINT pt; @@ -403,7 +403,7 @@ WXHWND wxComboBox::GetEditHWND() const HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt); if ( !hwndEdit || hwndEdit == GetHwnd() ) { - wxFAIL_MSG(_T("not read only combobox without edit control?")); + wxFAIL_MSG(_T("combobox without edit control?")); } return (WXHWND)hwndEdit; @@ -502,232 +502,17 @@ WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const // wxComboBox text control-like methods // ---------------------------------------------------------------------------- -wxString wxComboBox::GetValue() const -{ - return wxGetWindowText(m_hWnd); -} - void wxComboBox::SetValue(const wxString& value) { if ( HasFlag(wxCB_READONLY) ) SetStringSelection(value); else - SetWindowText(GetHwnd(), value.c_str()); -} - -// Clipboard operations -void wxComboBox::Copy() -{ - SendMessage(GetHwnd(), WM_COPY, 0, 0L); -} - -void wxComboBox::Cut() -{ - SendMessage(GetHwnd(), WM_CUT, 0, 0L); -} - -void wxComboBox::Paste() -{ - SendMessage(GetHwnd(), WM_PASTE, 0, 0L); -} - -void wxComboBox::Undo() -{ - if (CanUndo()) - { - HWND hEditWnd = (HWND) GetEditHWND(); - if ( hEditWnd ) - ::SendMessage(hEditWnd, EM_UNDO, 0, 0); - } -} - -void wxComboBox::Redo() -{ - if (CanUndo()) - { - // Same as Undo, since Undo undoes the undo, i.e. a redo. - HWND hEditWnd = (HWND) GetEditHWND(); - if ( hEditWnd ) - ::SendMessage(hEditWnd, EM_UNDO, 0, 0); - } -} - -void wxComboBox::SelectAll() -{ - SetSelection(0, GetLastPosition()); -} - -bool wxComboBox::CanUndo() const -{ - if (!IsEditable()) - return false; - - HWND hEditWnd = (HWND) GetEditHWND(); - if ( hEditWnd ) - return ::SendMessage(hEditWnd, EM_CANUNDO, 0, 0) != 0; - else - return false; -} - -bool wxComboBox::CanRedo() const -{ - if (!IsEditable()) - return false; - - HWND hEditWnd = (HWND) GetEditHWND(); - if ( hEditWnd ) - return ::SendMessage(hEditWnd, EM_CANUNDO, 0, 0) != 0; - else - return false; -} - -bool wxComboBox::HasSelection() const -{ - long from, to; - GetSelection(&from, &to); - return from != to; -} - -bool wxComboBox::CanCopy() const -{ - // Can copy if there's a selection - return HasSelection(); -} - -bool wxComboBox::CanCut() const -{ - return IsEditable() && CanCopy(); -} - -bool wxComboBox::CanPaste() const -{ - if ( !IsEditable() ) - return false; - - // Standard edit control: check for straight text on clipboard - if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) - return false; - - bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0; - ::CloseClipboard(); - - return isTextAvailable; + wxTextEntry::SetValue(value); } bool wxComboBox::IsEditable() const { - return !HasFlag(wxCB_READONLY); -} - -void wxComboBox::SetEditable(bool editable) -{ - HWND hWnd = (HWND)GetEditHWND(); - if ( !::SendMessage(hWnd, EM_SETREADONLY, !editable, 0) ) - { - wxLogLastError(_T("SendMessage(EM_SETREADONLY)")); - } -} - -void wxComboBox::SetInsertionPoint(long pos) -{ - if ( GetWindowStyle() & wxCB_READONLY ) - return; - - HWND hWnd = GetHwnd(); - ::SendMessage(hWnd, CB_SETEDITSEL, 0, MAKELPARAM(pos, pos)); - HWND hEditWnd = (HWND) GetEditHWND(); - if ( hEditWnd ) - { - // Scroll insertion point into view - SendMessage(hEditWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); - // Why is this necessary? (Copied from wxTextCtrl::SetInsertionPoint) - SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM) wxEmptyString); - } -} - -void wxComboBox::SetInsertionPointEnd() -{ - // setting insertion point doesn't make sense for read only comboboxes - if ( !(GetWindowStyle() & wxCB_READONLY) ) - { - wxTextPos pos = GetLastPosition(); - SetInsertionPoint(pos); - } -} - -long wxComboBox::GetInsertionPoint() const -{ - // CB_GETEDITSEL returns the index of the first character of the selection in - // its low-order word - DWORD pos= (DWORD)::SendMessage(GetHwnd(), CB_GETEDITSEL, 0, 0L); - return LOWORD(pos); -} - -wxTextPos wxComboBox::GetLastPosition() const -{ - HWND hEditWnd = (HWND) GetEditHWND(); - - // Get number of characters in the last (only) line. We'll add this to the character - // index for the last line, 1st position. - wxTextPos lineLength = (wxTextPos)SendMessage(hEditWnd, EM_LINELENGTH, (WPARAM) 0, (LPARAM)0L); - - return lineLength; -} - -void wxComboBox::Replace(long from, long to, const wxString& value) -{ -#if wxUSE_CLIPBOARD - Remove(from, to); - - // Now replace with 'value', by pasting. - wxSetClipboardData(wxDF_TEXT, (wxObject *)value.wx_str(), 0, 0); - - // Paste into edit control - SendMessage(GetHwnd(), WM_PASTE, (WPARAM)0, (LPARAM)0L); -#else - wxUnusedVar(from); - wxUnusedVar(to); - wxUnusedVar(value); -#endif -} - -void wxComboBox::Remove(long from, long to) -{ - // Set selection and remove it - SetSelection(from, to); - SendMessage(GetHwnd(), WM_CUT, (WPARAM)0, (LPARAM)0); -} - -void wxComboBox::SetSelection(long from, long to) -{ - // if from and to are both -1, it means (in wxWidgets) that all text should - // be selected, translate this into Windows convention - if ( (from == -1) && (to == -1) ) - { - from = 0; - } - - if ( SendMessage(GetHwnd(), CB_SETEDITSEL, - 0, (LPARAM)MAKELONG(from, to)) == CB_ERR ) - { - wxLogDebug(_T("CB_SETEDITSEL failed")); - } -} - -void wxComboBox::GetSelection(long* from, long* to) const -{ - DWORD dwStart, dwEnd; - if ( ::SendMessage(GetHwnd(), CB_GETEDITSEL, - (WPARAM)&dwStart, (LPARAM)&dwEnd) == CB_ERR ) - { - *from = - *to = 0; - } - else - { - *from = dwStart; - *to = dwEnd; - } + return !HasFlag(wxCB_READONLY) && wxTextEntry::IsEditable(); } // ---------------------------------------------------------------------------- diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 7218b339f2..d57e3eca0b 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -590,7 +590,7 @@ WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const { long msStyle = wxControl::MSWGetStyle(style, exstyle); - // styles which we alaways add by default + // styles which we always add by default if ( style & wxTE_MULTILINE ) { msStyle |= ES_MULTILINE | ES_WANTRETURN; @@ -1172,9 +1172,7 @@ void wxTextCtrl::DoWriteText(const wxString& value, int flags) void wxTextCtrl::AppendText(const wxString& text) { - SetInsertionPointEnd(); - - WriteText(text); + wxTextEntry::AppendText(text); #if wxUSE_RICHEDIT // don't do this if we're frozen, saves some time @@ -1191,18 +1189,12 @@ void wxTextCtrl::Clear() { ::SetWindowText(GetHwnd(), wxEmptyString); -#if wxUSE_RICHEDIT - if ( !IsRich() ) -#endif // wxUSE_RICHEDIT + if ( IsMultiLine() && !IsRich() ) { // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves // but the normal ones don't -- make Clear() behaviour consistent by // always sending this event - - // Windows already sends an update event for single-line - // controls. - if ( m_windowStyle & wxTE_MULTILINE ) - SendUpdateEvent(); + SendUpdateEvent(); } } @@ -1225,91 +1217,10 @@ bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event) #endif // __WIN32__ -// ---------------------------------------------------------------------------- -// Clipboard operations -// ---------------------------------------------------------------------------- - -void wxTextCtrl::Copy() -{ - if (CanCopy()) - { - ::SendMessage(GetHwnd(), WM_COPY, 0, 0L); - } -} - -void wxTextCtrl::Cut() -{ - if (CanCut()) - { - ::SendMessage(GetHwnd(), WM_CUT, 0, 0L); - } -} - -void wxTextCtrl::Paste() -{ - if (CanPaste()) - { - ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L); - } -} - -bool wxTextCtrl::HasSelection() const -{ - long from, to; - GetSelection(&from, &to); - return from != to; -} - -bool wxTextCtrl::CanCopy() const -{ - // Can copy if there's a selection - return HasSelection(); -} - -bool wxTextCtrl::CanCut() const -{ - return CanCopy() && IsEditable(); -} - -bool wxTextCtrl::CanPaste() const -{ - if ( !IsEditable() ) - return false; - -#if wxUSE_RICHEDIT - if ( IsRich() ) - { - UINT cf = 0; // 0 == any format - - return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0; - } -#endif // wxUSE_RICHEDIT - - // Standard edit control: check for straight text on clipboard - if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) - return false; - - bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0; - ::CloseClipboard(); - - return isTextAvailable; -} - // ---------------------------------------------------------------------------- // Accessors // ---------------------------------------------------------------------------- -void wxTextCtrl::SetEditable(bool editable) -{ - HWND hWnd = GetHwnd(); - ::SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); -} - -void wxTextCtrl::SetInsertionPoint(long pos) -{ - DoSetSelection(pos, pos); -} - void wxTextCtrl::SetInsertionPointEnd() { // we must not do anything if the caret is already there because calling @@ -1323,21 +1234,7 @@ void wxTextCtrl::SetInsertionPointEnd() return; } - long pos; - -#if wxUSE_RICHEDIT - if ( m_verRichEdit == 1 ) - { - // we don't have to waste time calling GetLastPosition() in this case - pos = -1; - } - else // !RichEdit 1.0 -#endif // wxUSE_RICHEDIT - { - pos = lastPosition; - } - - SetInsertionPoint(pos); + SetInsertionPoint(lastPosition); } long wxTextCtrl::GetInsertionPoint() const @@ -1353,23 +1250,27 @@ long wxTextCtrl::GetInsertionPoint() const } #endif // wxUSE_RICHEDIT - DWORD Pos = (DWORD)::SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); - return Pos & 0xFFFF; + return wxTextEntry::GetInsertionPoint(); } wxTextPos wxTextCtrl::GetLastPosition() const { - int numLines = GetNumberOfLines(); - long posStartLastLine = XYToPosition(0, numLines - 1); + if ( IsMultiLine() ) + { + int numLines = GetNumberOfLines(); + long posStartLastLine = XYToPosition(0, numLines - 1); - long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine); + long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine); - return posStartLastLine + lenLastLine; + return posStartLastLine + lenLastLine; + } + + return wxTextEntry::GetLastPosition(); } // If the return values from and to are the same, there is no // selection. -void wxTextCtrl::GetSelection(long* from, long* to) const +void wxTextCtrl::GetSelection(long *from, long *to) const { #if wxUSE_RICHEDIT if ( IsRich() ) @@ -1383,45 +1284,15 @@ void wxTextCtrl::GetSelection(long* from, long* to) const else #endif // !wxUSE_RICHEDIT { - DWORD dwStart, dwEnd; - ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); - - *from = dwStart; - *to = dwEnd; + wxTextEntry::GetSelection(from, to); } } -bool wxTextCtrl::IsEditable() const -{ - // strangely enough, we may be called before the control is created: our - // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls - // us - if ( !m_hWnd ) - return true; - - long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); - - return (style & ES_READONLY) == 0; -} - // ---------------------------------------------------------------------------- // selection // ---------------------------------------------------------------------------- -void wxTextCtrl::SetSelection(long from, long to) -{ - // if from and to are both -1, it means (in wxWidgets) that all text should - // be selected - translate into Windows convention - if ( (from == -1) && (to == -1) ) - { - from = 0; - to = -1; - } - - DoSetSelection(from, to); -} - -void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) +void wxTextCtrl::DoSetSelection(long from, long to, int flags) { HWND hWnd = GetHwnd(); @@ -1431,15 +1302,15 @@ void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) CHARRANGE range; range.cpMin = from; range.cpMax = to; - ::SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); + ::SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM)&range); } else #endif // wxUSE_RICHEDIT { - ::SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to); + ::SendMessage(hWnd, EM_SETSEL, from, to); } - if ( scrollCaret && !IsFrozen() ) + if ( (flags & SetSel_Scroll) && !IsFrozen() ) { #if wxUSE_RICHEDIT // richedit 3.0 (i.e. the version living in riched20.dll distributed @@ -1473,7 +1344,7 @@ void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) } #endif // wxUSE_RICHEDIT - ::SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); + ::SendMessage(hWnd, EM_SCROLLCARET, 0, (LPARAM)0); #if wxUSE_RICHEDIT // restore ECO_NOHIDESEL if we changed it @@ -1506,22 +1377,9 @@ bool wxTextCtrl::DoLoadFile(const wxString& file, int fileType) } // ---------------------------------------------------------------------------- -// Editing +// dirty status // ---------------------------------------------------------------------------- -void wxTextCtrl::Replace(long from, long to, const wxString& value) -{ - // Set selection and remove it - DoSetSelection(from, to, false /* don't scroll caret into view */); - - DoWriteText(value); -} - -void wxTextCtrl::Remove(long from, long to) -{ - Replace(from, to, wxEmptyString); -} - bool wxTextCtrl::IsModified() const { return ::SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; @@ -1529,27 +1387,27 @@ bool wxTextCtrl::IsModified() const void wxTextCtrl::MarkDirty() { - ::SendMessage(GetHwnd(), EM_SETMODIFY, TRUE, 0L); + ::SendMessage(GetHwnd(), EM_SETMODIFY, TRUE, 0); } void wxTextCtrl::DiscardEdits() { - ::SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); -} - -int wxTextCtrl::GetNumberOfLines() const -{ - return (int)::SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); + ::SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0); } // ---------------------------------------------------------------------------- // Positions <-> coords // ---------------------------------------------------------------------------- +int wxTextCtrl::GetNumberOfLines() const +{ + return (int)::SendMessage(GetHwnd(), EM_GETLINECOUNT, 0, 0); +} + long wxTextCtrl::XYToPosition(long x, long y) const { // This gets the char index for the _beginning_ of this line - long charIndex = ::SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0); + long charIndex = ::SendMessage(GetHwnd(), EM_LINEINDEX, y, 0); return charIndex + x; } @@ -1563,12 +1421,12 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const #if wxUSE_RICHEDIT if ( IsRich() ) { - lineNo = ::SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos); + lineNo = ::SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, pos); } else #endif // wxUSE_RICHEDIT { - lineNo = ::SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0); + lineNo = ::SendMessage(hWnd, EM_LINEFROMCHAR, pos, 0); } if ( lineNo == -1 ) @@ -1578,7 +1436,7 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const } // This gets the char index for the _beginning_ of this line - long charIndex = ::SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); + long charIndex = ::SendMessage(hWnd, EM_LINEINDEX, lineNo, 0); if ( charIndex == -1 ) { return false; @@ -1696,21 +1554,21 @@ void wxTextCtrl::ShowPosition(long pos) // Is this where scrolling is relative to - the line containing the caret? // Or is the first visible line??? Try first visible line. -// int currentLineLineNo1 = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L); +// int currentLineLineNo1 = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, -1, 0L); - int currentLineLineNo = (int)::SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L); + int currentLineLineNo = (int)::SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, 0, 0); - int specifiedLineLineNo = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L); + int specifiedLineLineNo = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, pos, 0); int linesToScroll = specifiedLineLineNo - currentLineLineNo; if (linesToScroll != 0) - (void)::SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); + ::SendMessage(hWnd, EM_LINESCROLL, 0, linesToScroll); } long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const { - return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0); + return ::SendMessage(GetHwnd(), EM_LINELENGTH, pos, 0); } int wxTextCtrl::GetLineLength(long lineNo) const @@ -1780,14 +1638,7 @@ void wxTextCtrl::SetMaxLength(unsigned long len) else #endif // wxUSE_RICHEDIT { - if ( len >= 0xffff ) - { - // this will set it to a platform-dependent maximum (much more - // than 64Kb under NT) - len = 0; - } - - ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0); + wxTextEntry::SetMaxLength(len); } } @@ -1795,41 +1646,27 @@ void wxTextCtrl::SetMaxLength(unsigned long len) // Undo/redo // ---------------------------------------------------------------------------- -void wxTextCtrl::Undo() -{ - if (CanUndo()) - { - ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); - } -} - void wxTextCtrl::Redo() { - if (CanRedo()) - { #if wxUSE_RICHEDIT - if (GetRichVersion() > 1) - ::SendMessage(GetHwnd(), EM_REDO, 0, 0); - else -#endif - // Same as Undo, since Undo undoes the undo, i.e. a redo. - ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); + if ( GetRichVersion() > 1 ) + { + ::SendMessage(GetHwnd(), EM_REDO, 0, 0); + return; } -} +#endif // wxUSE_RICHEDIT -bool wxTextCtrl::CanUndo() const -{ - return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; + wxTextEntry::Redo(); } bool wxTextCtrl::CanRedo() const { #if wxUSE_RICHEDIT - if (GetRichVersion() > 1) + if ( GetRichVersion() > 1 ) return ::SendMessage(GetHwnd(), EM_CANREDO, 0, 0) != 0; - else -#endif - return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; +#endif // wxUSE_RICHEDIT + + return wxTextEntry::CanRedo(); } // ---------------------------------------------------------------------------- @@ -1843,7 +1680,7 @@ bool wxTextCtrl::ShowNativeCaret(bool show) if ( !(show ? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) ) { // not an error, may simply indicate that it's not shown/hidden - // yet (i.e. it had been hidden/showh 2 times before) + // yet (i.e. it had been hidden/shown 2 times before) return false; } @@ -1854,7 +1691,7 @@ bool wxTextCtrl::ShowNativeCaret(bool show) } // ---------------------------------------------------------------------------- -// implemenation details +// implementation details // ---------------------------------------------------------------------------- void wxTextCtrl::Command(wxCommandEvent & event) @@ -2084,10 +1921,7 @@ bool wxTextCtrl::SendUpdateEvent() return false; } - wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); - InitCommandEvent(event); - - return ProcessCommand(event); + return SendTextUpdatedEvent(); } bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) @@ -2173,7 +2007,7 @@ bool wxTextCtrl::AdjustSpaceLimit() return true; } -bool wxTextCtrl::AcceptsFocus() const +bool wxTextCtrl::AcceptsFocusFromKeyboard() const { // we don't want focus if we can't be edited unless we're a multiline // control because then it might be still nice to get focus from keyboard @@ -2500,8 +2334,6 @@ bool wxTextCtrl::SetForegroundColour(const wxColour& colour) // styling support for rich edit controls // ---------------------------------------------------------------------------- -#if wxUSE_RICHEDIT - bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) { if ( !IsRich() ) @@ -2539,7 +2371,7 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) if ( changeSel ) { - DoSetSelection(start, end, false /* don't scroll caret into view */); + DoSetSelection(start, end, SetSel_NoScroll); } // initialize CHARFORMAT struct @@ -2717,7 +2549,7 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) if ( changeSel ) { // restore the original selection - DoSetSelection(startOld, endOld, false); + DoSetSelection(startOld, endOld, SetSel_NoScroll); } return ok; @@ -2780,7 +2612,7 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) if ( changeSel ) { - DoSetSelection(position, position+1, false /* don't scroll caret into view */); + DoSetSelection(position, position + 1, SetSel_NoScroll); } // get the selection formatting @@ -2880,14 +2712,12 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) if ( changeSel ) { // restore the original selection - DoSetSelection(startOld, endOld, false); + DoSetSelection(startOld, endOld, SetSel_NoScroll); } return true; } -#endif - // ---------------------------------------------------------------------------- // wxRichEditModule // ---------------------------------------------------------------------------- @@ -2967,7 +2797,7 @@ bool wxRichEditModule::LoadInkEdit() wxLogNull logNull; return ms_inkEditLib.Load(wxT("inked")); } -#endif +#endif // wxUSE_INKEDIT #endif // wxUSE_RICHEDIT diff --git a/src/msw/textentry.cpp b/src/msw/textentry.cpp new file mode 100644 index 0000000000..152098e35b --- /dev/null +++ b/src/msw/textentry.cpp @@ -0,0 +1,154 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: src/msw/textentry.cpp +// Purpose: wxTextEntry implementation for wxMSW +// Author: Vadim Zeitlin +// Created: 2007-09-26 +// RCS-ID: $Id: wxhead.cpp,v 1.7 2007/01/09 16:22:45 zeitlin Exp $ +// Copyright: (c) 2007 Vadim Zeitlin +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +// for compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP +#endif // WX_PRECOMP + +#include "wx/textentry.h" + +#include "wx/msw/private.h" + +#define GetEditHwnd() ((HWND)(GetEditHWND())) + +// ============================================================================ +// wxTextEntry implementation +// ============================================================================ + +void wxTextEntry::WriteText(const wxString& text) +{ + ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str()); +} + +wxString wxTextEntry::GetValue() const +{ + return wxGetWindowText(GetEditHWND()); +} + +void wxTextEntry::Remove(long from, long to) +{ + DoSetSelection(from, to, SetSel_NoScroll); + WriteText(wxString()); +} + +void wxTextEntry::Copy() +{ + ::SendMessage(GetEditHwnd(), WM_COPY, 0, 0); +} + +void wxTextEntry::Cut() +{ + ::SendMessage(GetEditHwnd(), WM_CUT, 0, 0); +} + +void wxTextEntry::Paste() +{ + ::SendMessage(GetEditHwnd(), WM_PASTE, 0, 0); +} + +void wxTextEntry::Undo() +{ + ::SendMessage(GetEditHwnd(), EM_UNDO, 0, 0); +} + +void wxTextEntry::Redo() +{ + // same as Undo, since Undo undoes the undo + return Undo(); +} + +bool wxTextEntry::CanUndo() const +{ + return ::SendMessage(GetEditHwnd(), EM_CANUNDO, 0, 0) != 0; +} + +bool wxTextEntry::CanRedo() const +{ + // see comment in Redo() + return CanUndo(); +} + +void wxTextEntry::SetInsertionPoint(long pos) +{ + // be careful to call DoSetSelection() which is overridden in wxTextCtrl + // and not just SetSelection() here + DoSetSelection(pos, pos); +} + +long wxTextEntry::GetInsertionPoint() const +{ + long from; + GetSelection(&from, NULL); + return from; +} + +long wxTextEntry::GetLastPosition() const +{ + return ::SendMessage(GetEditHwnd(), EM_LINELENGTH, 0, 0); +} + +void wxTextEntry::DoSetSelection(long from, long to, int WXUNUSED(flags)) +{ + // if from and to are both -1, it means (in wxWidgets) that all text should + // be selected, translate this into Windows convention + if ( (from == -1) && (to == -1) ) + { + from = 0; + } + + ::SendMessage(GetEditHwnd(), EM_SETSEL, from, to); +} + +void wxTextEntry::GetSelection(long *from, long *to) const +{ + DWORD dwStart, dwEnd; + ::SendMessage(GetEditHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); + + if ( from ) + *from = dwStart; + if ( to ) + *to = dwEnd; +} + +bool wxTextEntry::IsEditable() const +{ + return (::GetWindowLong(GetEditHwnd(), GWL_STYLE) & ES_READONLY) != 0; +} + +void wxTextEntry::SetEditable(bool editable) +{ + ::SendMessage(GetEditHwnd(), EM_SETREADONLY, !editable, 0); +} + +void wxTextEntry::SetMaxLength(unsigned long len) +{ + if ( len >= 0xffff ) + { + // this will set it to a platform-dependent maximum (much more + // than 64Kb under NT) + len = 0; + } + + ::SendMessage(GetEditHwnd(), EM_LIMITTEXT, len, 0); +} -- 2.45.2