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
+37 -58
View File
@@ -41,44 +41,41 @@ 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 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);
// 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);
virtual void GetSelection(long *from, long *to) const;
virtual void Redo();
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__
virtual bool EmulateKeyPress(const wxKeyEvent& event);
@@ -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_
+67
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_
+4 -3
View File
@@ -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);
+2
View File
@@ -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
+2 -2
View File
@@ -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
+11 -1
View File
@@ -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
+4 -219
View File
@@ -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();
}
// ----------------------------------------------------------------------------
+59 -229
View File
File diff suppressed because it is too large Load Diff
+154
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);
}