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
This commit is contained in:
Vadim Zeitlin
2007-09-26 16:48:46 +00:00
parent d6b9cc87c0
commit fa2f57be4d
9 changed files with 340 additions and 512 deletions

View File

@@ -41,44 +41,41 @@ public:
const wxValidator& validator = wxDefaultValidator, const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxTextCtrlNameStr); const wxString& name = wxTextCtrlNameStr);
// implement base class pure virtuals // overridden wxTextEntry methods
// ---------------------------------- // ------------------------------
virtual wxString GetValue() const; virtual wxString GetValue() const;
virtual wxString GetRange(long from, long to) const;
virtual bool IsEmpty() 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 int GetLineLength(long lineNo) const;
virtual wxString GetLineText(long lineNo) const; virtual wxString GetLineText(long lineNo) const;
virtual int GetNumberOfLines() const; virtual int GetNumberOfLines() const;
virtual bool IsModified() const;
virtual bool IsEditable() const;
virtual void GetSelection(long* from, long* to) const;
// operations
// ----------
// editing
virtual void Clear();
virtual void Replace(long from, long to, const wxString& value);
virtual void Remove(long from, long to);
// load the control's contents from the file
virtual bool DoLoadFile(const wxString& file, int fileType);
// clears the dirty flag
virtual void MarkDirty();
virtual void DiscardEdits();
virtual void SetMaxLength(unsigned long len); virtual void SetMaxLength(unsigned long len);
// writing text inserts it at the current position, appending always virtual void GetSelection(long *from, long *to) const;
// inserts it at the end
virtual void WriteText(const wxString& text); virtual void Redo();
virtual void AppendText(const wxString& text); virtual bool CanRedo() const;
virtual void SetInsertionPointEnd();
virtual long GetInsertionPoint() const;
virtual wxTextPos GetLastPosition() const;
// implement base class pure virtuals
// ----------------------------------
virtual bool DoLoadFile(const wxString& file, int fileType);
virtual bool IsModified() const;
virtual void MarkDirty();
virtual void DiscardEdits();
#ifdef __WIN32__ #ifdef __WIN32__
virtual bool EmulateKeyPress(const wxKeyEvent& event); virtual bool EmulateKeyPress(const wxKeyEvent& event);
@@ -107,33 +104,7 @@ public:
return wxTextCtrlBase::HitTest(pt, col, row); 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) // Caret handling (Windows only)
bool ShowNativeCaret(bool show = true); bool ShowNativeCaret(bool show = true);
bool HideNativeCaret() { return ShowNativeCaret(false); } bool HideNativeCaret() { return ShowNativeCaret(false); }
@@ -172,7 +143,7 @@ public:
virtual void AdoptAttributesFromHWND(); virtual void AdoptAttributesFromHWND();
virtual bool AcceptsFocus() const; virtual bool AcceptsFocusFromKeyboard() const;
// returns true if the platform should explicitly apply a theme border // returns true if the platform should explicitly apply a theme border
virtual bool CanApplyThemeBorder() const; virtual bool CanApplyThemeBorder() const;
@@ -227,6 +198,10 @@ protected:
virtual void DoSetValue(const wxString &value, int flags = 0); 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. // 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) // the limit is due to a previous call to SetMaxLength() and not built in)
bool HasSpaceLimit(unsigned int *len) const; bool HasSpaceLimit(unsigned int *len) const;
@@ -252,8 +227,8 @@ protected:
void DoWriteText(const wxString& text, void DoWriteText(const wxString& text,
int flags = SetValue_SendEvent | SetValue_SelectionOnly); int flags = SetValue_SendEvent | SetValue_SelectionOnly);
// set the selection possibly without scrolling the caret into view // set the selection (possibly without scrolling the caret into view)
void DoSetSelection(long from, long to, bool scrollCaret = true); void DoSetSelection(long from, long to, int flags);
// return true if there is a non empty selection in the control // return true if there is a non empty selection in the control
bool HasSelection() const; bool HasSelection() const;
@@ -278,6 +253,11 @@ protected:
// text ourselves: we want this to be exactly 1 // text ourselves: we want this to be exactly 1
int m_updatesCount; int m_updatesCount;
virtual void EnableTextChangedEvents(bool enable)
{
m_updatesCount = enable ? -1 : -2;
}
private: private:
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
DECLARE_DYNAMIC_CLASS_NO_COPY(wxTextCtrl) DECLARE_DYNAMIC_CLASS_NO_COPY(wxTextCtrl)
@@ -292,5 +272,4 @@ private:
}; };
#endif #endif // _WX_TEXTCTRL_H_
// _WX_TEXTCTRL_H_

View File

@@ -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 <vadim@wxwindows.org>
// 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_

View File

@@ -411,9 +411,10 @@ public:
virtual bool EmulateKeyPress(const wxKeyEvent& event); virtual bool EmulateKeyPress(const wxKeyEvent& event);
// generate the wxEVT_COMMAND_TEXT_UPDATED event, like SetValue() does // generate the wxEVT_COMMAND_TEXT_UPDATED event, like SetValue() does and
static void SendTextUpdatedEvent(wxWindow *win); // return true if the event was processed
void SendTextUpdatedEvent() { SendTextUpdatedEvent(this); } static bool SendTextUpdatedEvent(wxWindow *win);
bool SendTextUpdatedEvent() { return SendTextUpdatedEvent(this); }
// do the window-specific processing after processing the update event // do the window-specific processing after processing the update event
virtual void DoUpdateWindowUI(wxUpdateUIEvent& event); virtual void DoUpdateWindowUI(wxUpdateUIEvent& event);

View File

@@ -180,6 +180,8 @@ private:
#ifdef __WXGTK20__ #ifdef __WXGTK20__
#include "wx/gtk/textentry.h" #include "wx/gtk/textentry.h"
#elif defined(__WXMSW__)
#include "wx/msw/textentry.h"
#else #else
// no platform-specific implementation of wxTextEntry yet // no platform-specific implementation of wxTextEntry yet
class WXDLLIMPEXP_CORE wxTextEntry : public wxTextEntryBase class WXDLLIMPEXP_CORE wxTextEntry : public wxTextEntryBase

View File

@@ -468,7 +468,7 @@ wxTextAreaBase::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(pos)) const
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/* static */ /* static */
void wxTextCtrlBase::SendTextUpdatedEvent(wxWindow *win) bool wxTextCtrlBase::SendTextUpdatedEvent(wxWindow *win)
{ {
wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, win->GetId()); wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, win->GetId());
@@ -479,7 +479,7 @@ void wxTextCtrlBase::SendTextUpdatedEvent(wxWindow *win)
//event.SetString(win->GetValue()); //event.SetString(win->GetValue());
event.SetEventObject(win); event.SetEventObject(win);
win->GetEventHandler()->ProcessEvent(event); return win->GetEventHandler()->ProcessEvent(event);
} }
#else // !wxUSE_TEXTCTRL #else // !wxUSE_TEXTCTRL

View File

@@ -29,6 +29,7 @@
#endif //WX_PRECOMP #endif //WX_PRECOMP
#include "wx/textentry.h" #include "wx/textentry.h"
#include "wx/clipbrd.h"
// ============================================================================ // ============================================================================
// wxTextEntryBase implementation // wxTextEntryBase implementation
@@ -97,7 +98,16 @@ bool wxTextEntryBase::CanCut() const
bool wxTextEntryBase::CanPaste() 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 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX

View File

@@ -395,7 +395,7 @@ WXHWND wxComboBox::GetEditHWND() const
{ {
// this function should not be called for wxCB_READONLY controls, it is // this function should not be called for wxCB_READONLY controls, it is
// the callers responsability to check this // 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") ); _T("read-only combobox doesn't have any edit control") );
POINT pt; POINT pt;
@@ -403,7 +403,7 @@ WXHWND wxComboBox::GetEditHWND() const
HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt); HWND hwndEdit = ::ChildWindowFromPoint(GetHwnd(), pt);
if ( !hwndEdit || hwndEdit == GetHwnd() ) if ( !hwndEdit || hwndEdit == GetHwnd() )
{ {
wxFAIL_MSG(_T("not read only combobox without edit control?")); wxFAIL_MSG(_T("combobox without edit control?"));
} }
return (WXHWND)hwndEdit; return (WXHWND)hwndEdit;
@@ -502,232 +502,17 @@ WXDWORD wxComboBox::MSWGetStyle(long style, WXDWORD *exstyle) const
// wxComboBox text control-like methods // wxComboBox text control-like methods
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxString wxComboBox::GetValue() const
{
return wxGetWindowText(m_hWnd);
}
void wxComboBox::SetValue(const wxString& value) void wxComboBox::SetValue(const wxString& value)
{ {
if ( HasFlag(wxCB_READONLY) ) if ( HasFlag(wxCB_READONLY) )
SetStringSelection(value); SetStringSelection(value);
else else
SetWindowText(GetHwnd(), value.c_str()); wxTextEntry::SetValue(value);
}
// 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;
} }
bool wxComboBox::IsEditable() const bool wxComboBox::IsEditable() const
{ {
return !HasFlag(wxCB_READONLY); return !HasFlag(wxCB_READONLY) && wxTextEntry::IsEditable();
}
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;
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -590,7 +590,7 @@ WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
{ {
long msStyle = wxControl::MSWGetStyle(style, exstyle); long msStyle = wxControl::MSWGetStyle(style, exstyle);
// styles which we alaways add by default // styles which we always add by default
if ( style & wxTE_MULTILINE ) if ( style & wxTE_MULTILINE )
{ {
msStyle |= ES_MULTILINE | ES_WANTRETURN; msStyle |= ES_MULTILINE | ES_WANTRETURN;
@@ -1172,9 +1172,7 @@ void wxTextCtrl::DoWriteText(const wxString& value, int flags)
void wxTextCtrl::AppendText(const wxString& text) void wxTextCtrl::AppendText(const wxString& text)
{ {
SetInsertionPointEnd(); wxTextEntry::AppendText(text);
WriteText(text);
#if wxUSE_RICHEDIT #if wxUSE_RICHEDIT
// don't do this if we're frozen, saves some time // don't do this if we're frozen, saves some time
@@ -1191,17 +1189,11 @@ void wxTextCtrl::Clear()
{ {
::SetWindowText(GetHwnd(), wxEmptyString); ::SetWindowText(GetHwnd(), wxEmptyString);
#if wxUSE_RICHEDIT if ( IsMultiLine() && !IsRich() )
if ( !IsRich() )
#endif // wxUSE_RICHEDIT
{ {
// rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
// but the normal ones don't -- make Clear() behaviour consistent by // but the normal ones don't -- make Clear() behaviour consistent by
// always sending this event // 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__ #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 // 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() void wxTextCtrl::SetInsertionPointEnd()
{ {
// we must not do anything if the caret is already there because calling // we must not do anything if the caret is already there because calling
@@ -1323,21 +1234,7 @@ void wxTextCtrl::SetInsertionPointEnd()
return; return;
} }
long pos; SetInsertionPoint(lastPosition);
#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);
} }
long wxTextCtrl::GetInsertionPoint() const long wxTextCtrl::GetInsertionPoint() const
@@ -1353,23 +1250,27 @@ long wxTextCtrl::GetInsertionPoint() const
} }
#endif // wxUSE_RICHEDIT #endif // wxUSE_RICHEDIT
DWORD Pos = (DWORD)::SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); return wxTextEntry::GetInsertionPoint();
return Pos & 0xFFFF;
} }
wxTextPos wxTextCtrl::GetLastPosition() const wxTextPos wxTextCtrl::GetLastPosition() const
{ {
if ( IsMultiLine() )
{
int numLines = GetNumberOfLines(); int numLines = GetNumberOfLines();
long posStartLastLine = XYToPosition(0, numLines - 1); 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 // If the return values from and to are the same, there is no
// selection. // selection.
void wxTextCtrl::GetSelection(long* from, long* to) const void wxTextCtrl::GetSelection(long *from, long *to) const
{ {
#if wxUSE_RICHEDIT #if wxUSE_RICHEDIT
if ( IsRich() ) if ( IsRich() )
@@ -1383,45 +1284,15 @@ void wxTextCtrl::GetSelection(long* from, long* to) const
else else
#endif // !wxUSE_RICHEDIT #endif // !wxUSE_RICHEDIT
{ {
DWORD dwStart, dwEnd; wxTextEntry::GetSelection(from, to);
::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
*from = dwStart;
*to = dwEnd;
} }
} }
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 // selection
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxTextCtrl::SetSelection(long from, long to) void wxTextCtrl::DoSetSelection(long from, long to, int flags)
{
// 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)
{ {
HWND hWnd = GetHwnd(); HWND hWnd = GetHwnd();
@@ -1431,15 +1302,15 @@ void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret)
CHARRANGE range; CHARRANGE range;
range.cpMin = from; range.cpMin = from;
range.cpMax = to; range.cpMax = to;
::SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); ::SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM)&range);
} }
else else
#endif // wxUSE_RICHEDIT #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 #if wxUSE_RICHEDIT
// richedit 3.0 (i.e. the version living in riched20.dll distributed // 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 #endif // wxUSE_RICHEDIT
::SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); ::SendMessage(hWnd, EM_SCROLLCARET, 0, (LPARAM)0);
#if wxUSE_RICHEDIT #if wxUSE_RICHEDIT
// restore ECO_NOHIDESEL if we changed it // 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 bool wxTextCtrl::IsModified() const
{ {
return ::SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; return ::SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0;
@@ -1529,27 +1387,27 @@ bool wxTextCtrl::IsModified() const
void wxTextCtrl::MarkDirty() void wxTextCtrl::MarkDirty()
{ {
::SendMessage(GetHwnd(), EM_SETMODIFY, TRUE, 0L); ::SendMessage(GetHwnd(), EM_SETMODIFY, TRUE, 0);
} }
void wxTextCtrl::DiscardEdits() void wxTextCtrl::DiscardEdits()
{ {
::SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); ::SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0);
}
int wxTextCtrl::GetNumberOfLines() const
{
return (int)::SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Positions <-> coords // Positions <-> coords
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
int wxTextCtrl::GetNumberOfLines() const
{
return (int)::SendMessage(GetHwnd(), EM_GETLINECOUNT, 0, 0);
}
long wxTextCtrl::XYToPosition(long x, long y) const long wxTextCtrl::XYToPosition(long x, long y) const
{ {
// This gets the char index for the _beginning_ of this line // 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; return charIndex + x;
} }
@@ -1563,12 +1421,12 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
#if wxUSE_RICHEDIT #if wxUSE_RICHEDIT
if ( IsRich() ) if ( IsRich() )
{ {
lineNo = ::SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos); lineNo = ::SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, pos);
} }
else else
#endif // wxUSE_RICHEDIT #endif // wxUSE_RICHEDIT
{ {
lineNo = ::SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0); lineNo = ::SendMessage(hWnd, EM_LINEFROMCHAR, pos, 0);
} }
if ( lineNo == -1 ) 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 // 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 ) if ( charIndex == -1 )
{ {
return false; return false;
@@ -1696,21 +1554,21 @@ void wxTextCtrl::ShowPosition(long pos)
// Is this where scrolling is relative to - the line containing the caret? // Is this where scrolling is relative to - the line containing the caret?
// Or is the first visible line??? Try first visible line. // 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; int linesToScroll = specifiedLineLineNo - currentLineLineNo;
if (linesToScroll != 0) if (linesToScroll != 0)
(void)::SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); ::SendMessage(hWnd, EM_LINESCROLL, 0, linesToScroll);
} }
long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const 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 int wxTextCtrl::GetLineLength(long lineNo) const
@@ -1780,14 +1638,7 @@ void wxTextCtrl::SetMaxLength(unsigned long len)
else else
#endif // wxUSE_RICHEDIT #endif // wxUSE_RICHEDIT
{ {
if ( len >= 0xffff ) wxTextEntry::SetMaxLength(len);
{
// this will set it to a platform-dependent maximum (much more
// than 64Kb under NT)
len = 0;
}
::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0);
} }
} }
@@ -1795,41 +1646,27 @@ void wxTextCtrl::SetMaxLength(unsigned long len)
// Undo/redo // Undo/redo
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxTextCtrl::Undo()
{
if (CanUndo())
{
::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
}
}
void wxTextCtrl::Redo() void wxTextCtrl::Redo()
{ {
if (CanRedo())
{
#if wxUSE_RICHEDIT #if wxUSE_RICHEDIT
if (GetRichVersion() > 1) if ( GetRichVersion() > 1 )
{
::SendMessage(GetHwnd(), EM_REDO, 0, 0); ::SendMessage(GetHwnd(), EM_REDO, 0, 0);
else return;
#endif
// Same as Undo, since Undo undoes the undo, i.e. a redo.
::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
} }
} #endif // wxUSE_RICHEDIT
bool wxTextCtrl::CanUndo() const wxTextEntry::Redo();
{
return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
} }
bool wxTextCtrl::CanRedo() const bool wxTextCtrl::CanRedo() const
{ {
#if wxUSE_RICHEDIT #if wxUSE_RICHEDIT
if (GetRichVersion() > 1) if ( GetRichVersion() > 1 )
return ::SendMessage(GetHwnd(), EM_CANREDO, 0, 0) != 0; return ::SendMessage(GetHwnd(), EM_CANREDO, 0, 0) != 0;
else #endif // wxUSE_RICHEDIT
#endif
return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; return wxTextEntry::CanRedo();
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -1843,7 +1680,7 @@ bool wxTextCtrl::ShowNativeCaret(bool show)
if ( !(show ? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) ) if ( !(show ? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) )
{ {
// not an error, may simply indicate that it's not shown/hidden // 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; return false;
} }
@@ -1854,7 +1691,7 @@ bool wxTextCtrl::ShowNativeCaret(bool show)
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// implemenation details // implementation details
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxTextCtrl::Command(wxCommandEvent & event) void wxTextCtrl::Command(wxCommandEvent & event)
@@ -2084,10 +1921,7 @@ bool wxTextCtrl::SendUpdateEvent()
return false; return false;
} }
wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); return SendTextUpdatedEvent();
InitCommandEvent(event);
return ProcessCommand(event);
} }
bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
@@ -2173,7 +2007,7 @@ bool wxTextCtrl::AdjustSpaceLimit()
return true; 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 // 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 // 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 // styling support for rich edit controls
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#if wxUSE_RICHEDIT
bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
{ {
if ( !IsRich() ) if ( !IsRich() )
@@ -2539,7 +2371,7 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
if ( changeSel ) if ( changeSel )
{ {
DoSetSelection(start, end, false /* don't scroll caret into view */); DoSetSelection(start, end, SetSel_NoScroll);
} }
// initialize CHARFORMAT struct // initialize CHARFORMAT struct
@@ -2717,7 +2549,7 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
if ( changeSel ) if ( changeSel )
{ {
// restore the original selection // restore the original selection
DoSetSelection(startOld, endOld, false); DoSetSelection(startOld, endOld, SetSel_NoScroll);
} }
return ok; return ok;
@@ -2780,7 +2612,7 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
if ( changeSel ) if ( changeSel )
{ {
DoSetSelection(position, position+1, false /* don't scroll caret into view */); DoSetSelection(position, position + 1, SetSel_NoScroll);
} }
// get the selection formatting // get the selection formatting
@@ -2880,14 +2712,12 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
if ( changeSel ) if ( changeSel )
{ {
// restore the original selection // restore the original selection
DoSetSelection(startOld, endOld, false); DoSetSelection(startOld, endOld, SetSel_NoScroll);
} }
return true; return true;
} }
#endif
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxRichEditModule // wxRichEditModule
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -2967,7 +2797,7 @@ bool wxRichEditModule::LoadInkEdit()
wxLogNull logNull; wxLogNull logNull;
return ms_inkEditLib.Load(wxT("inked")); return ms_inkEditLib.Load(wxT("inked"));
} }
#endif #endif // wxUSE_INKEDIT
#endif // wxUSE_RICHEDIT #endif // wxUSE_RICHEDIT

154
src/msw/textentry.cpp Normal file
View File

@@ -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 <vadim@wxwindows.org>
// 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);
}