+#if wxUSE_WCHAR_T
+ wxWCharBuffer wchBuf(len);
+ wchar_t *wpc = wchBuf.data();
+#else
+ wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t));
+ wchar_t *wpc = wchBuf;
+#endif
+
+ wxStreamOutData data;
+ data.wpc = wpc;
+ data.len = len;
+
+ EDITSTREAM eds;
+ wxZeroMemory(eds);
+ eds.dwCookie = (DWORD)&data;
+ eds.pfnCallback = wxRichEditStreamOut;
+
+ ::SendMessage
+ (
+ GetHwnd(),
+ EM_STREAMOUT,
+ SF_TEXT | SF_UNICODE | (selectionOnly ? SFF_SELECTION : 0),
+ (LPARAM)&eds
+ );
+
+ if ( eds.dwError )
+ {
+ wxLogLastError(_T("EM_STREAMOUT"));
+ }
+ else // streamed out ok
+ {
+ // NUL-terminate the string because its length could have been
+ // decreased by wxRichEditStreamOut
+ *(wchBuf.data() + data.len) = L'\0';
+
+ // now convert to the given encoding (this is a possibly lossful
+ // conversion but what else can we do)
+ wxCSConv conv(encoding);
+ size_t lenNeeded = conv.WC2MB(NULL, wchBuf, 0);
+
+ if ( lenNeeded != wxCONV_FAILED && lenNeeded++ )
+ {
+ conv.WC2MB(wxStringBuffer(out, lenNeeded), wchBuf, lenNeeded);
+ }
+ }
+
+#if !wxUSE_WCHAR_T
+ free(wchBuf);
+#endif // !wxUSE_WCHAR_T
+
+ return out;
+}
+
+#endif // !wxUSE_UNICODE_MSLU
+
+#endif // wxUSE_RICHEDIT
+
+void wxTextCtrl::WriteText(const wxString& value)
+{
+ DoWriteText(value);
+}
+
+void wxTextCtrl::DoWriteText(const wxString& value, int flags)
+{
+ bool selectionOnly = (flags & SetValue_SelectionOnly) != 0;
+ wxString valueDos;
+ if ( m_windowStyle & wxTE_MULTILINE )
+ valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
+ else
+ valueDos = value;
+
+#if wxUSE_RICHEDIT
+ // there are several complications with the rich edit controls here
+ bool done = false;
+ if ( IsRich() )
+ {
+ // first, ensure that the new text will be in the default style
+ if ( !m_defaultStyle.IsDefault() )
+ {
+ long start, end;
+ GetSelection(&start, &end);
+ SetStyle(start, end, m_defaultStyle);
+ }
+
+#if wxUSE_UNICODE_MSLU
+ // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
+ // but EM_STREAMIN works
+ if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
+ {
+ done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly);
+ }
+#endif // wxUSE_UNICODE_MSLU
+
+#if !wxUSE_UNICODE
+ // next check if the text we're inserting must be shown in a non
+ // default charset -- this only works for RichEdit > 1.0
+ if ( GetRichVersion() > 1 )
+ {
+ wxFont font = m_defaultStyle.GetFont();
+ if ( !font.Ok() )
+ font = GetFont();
+
+ if ( font.Ok() )
+ {
+ wxFontEncoding encoding = font.GetEncoding();
+ if ( encoding != wxFONTENCODING_SYSTEM )
+ {
+ // we have to use EM_STREAMIN to force richedit control 2.0+
+ // to show any text in the non default charset -- otherwise
+ // it thinks it knows better than we do and always shows it
+ // in the default one
+ done = StreamIn(valueDos, encoding, selectionOnly);
+ }
+ }
+ }
+#endif // !wxUSE_UNICODE
+ }
+
+ if ( !done )
+#endif // wxUSE_RICHEDIT
+ {
+ // in some cases we get 2 EN_CHANGE notifications after the SendMessage
+ // call (this happens for plain EDITs with EM_REPLACESEL and under some
+ // -- undetermined -- conditions with rich edit) and sometimes we don't
+ // get any events at all (plain EDIT with WM_SETTEXT), so ensure that
+ // we generate exactly one of them by ignoring all but the first one in
+ // SendUpdateEvent() and generating one ourselves if we hadn't got any
+ // notifications from Windows
+ if ( !(flags & SetValue_SendEvent) )
+ m_updatesCount = -2; // suppress any update event
+
+ UpdatesCountFilter ucf(m_updatesCount);
+
+ ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT,
+ // EM_REPLACESEL takes 1 to indicate the operation should be redoable
+ selectionOnly ? 1 : 0, (LPARAM)valueDos.wx_str());
+
+ if ( !ucf.GotUpdate() && (flags & SetValue_SendEvent) )
+ {
+ SendUpdateEvent();
+ }
+ }
+}
+
+void wxTextCtrl::AppendText(const wxString& text)
+{
+ wxTextEntry::AppendText(text);
+
+#if wxUSE_RICHEDIT
+ // don't do this if we're frozen, saves some time
+ if ( !IsFrozen() && IsMultiLine() && GetRichVersion() > 1 )
+ {
+ // setting the caret to the end and showing it simply doesn't work for
+ // RichEdit 2.0 -- force it to still do what we want
+ ::SendMessage(GetHwnd(), EM_LINESCROLL, 0, GetNumberOfLines());
+ }
+#endif // wxUSE_RICHEDIT
+}
+
+void wxTextCtrl::Clear()
+{
+ ::SetWindowText(GetHwnd(), wxEmptyString);
+
+ 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
+ SendUpdateEvent();
+ }
+}
+
+#ifdef __WIN32__
+
+bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event)
+{
+ SetFocus();
+
+ size_t lenOld = GetValue().length();
+
+ wxUint32 code = event.GetRawKeyCode();
+ ::keybd_event((BYTE)code, 0, 0 /* key press */, 0);
+ ::keybd_event((BYTE)code, 0, KEYEVENTF_KEYUP, 0);
+
+ // assume that any alphanumeric key changes the total number of characters
+ // in the control - this should work in 99% of cases
+ return GetValue().length() != lenOld;
+}
+
+#endif // __WIN32__
+
+// ----------------------------------------------------------------------------
+// Accessors
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::SetInsertionPointEnd()
+{
+ // we must not do anything if the caret is already there because calling
+ // SetInsertionPoint() thaws the controls if Freeze() had been called even
+ // if it doesn't actually move the caret anywhere and so the simple fact of
+ // doing it results in horrible flicker when appending big amounts of text
+ // to the control in a few chunks (see DoAddText() test in the text sample)
+ const wxTextPos lastPosition = GetLastPosition();
+ if ( GetInsertionPoint() == lastPosition )
+ {
+ return;
+ }
+
+ SetInsertionPoint(lastPosition);
+}
+
+long wxTextCtrl::GetInsertionPoint() const
+{
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ CHARRANGE range;
+ range.cpMin = 0;
+ range.cpMax = 0;
+ ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
+ return range.cpMin;
+ }
+#endif // wxUSE_RICHEDIT
+
+ return wxTextEntry::GetInsertionPoint();
+}
+
+wxTextPos wxTextCtrl::GetLastPosition() const
+{
+ if ( IsMultiLine() )
+ {
+ int numLines = GetNumberOfLines();
+ long posStartLastLine = XYToPosition(0, numLines - 1);
+
+ long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine);
+
+ 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
+{
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ CHARRANGE charRange;
+ ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange);
+
+ *from = charRange.cpMin;
+ *to = charRange.cpMax;
+ }
+ else
+#endif // !wxUSE_RICHEDIT
+ {
+ wxTextEntry::GetSelection(from, to);
+ }
+}
+
+// ----------------------------------------------------------------------------
+// selection
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::DoSetSelection(long from, long to, int flags)
+{
+ HWND hWnd = GetHwnd();
+
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ // 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;
+ }
+
+ CHARRANGE range;
+ range.cpMin = from;
+ range.cpMax = to;
+ ::SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM)&range);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ wxTextEntry::DoSetSelection(from, to, flags);
+ }
+
+ if ( (flags & SetSel_Scroll) && !IsFrozen() )
+ {
+#if wxUSE_RICHEDIT
+ // richedit 3.0 (i.e. the version living in riched20.dll distributed
+ // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
+ // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
+ // option is set (but it does work ok in richedit 1.0 mode...)
+ //
+ // so to make it work we either need to give focus to it here which
+ // will probably create many problems (dummy focus events; window
+ // containing the text control being brought to foreground
+ // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
+ // create other problems too -- and in fact it does because if we turn
+ // on/off this style while appending the text to the control, the
+ // vertical scrollbar never appears in it even if we append tons of
+ // text and to work around this the only solution I found was to use
+ // ES_DISABLENOSCROLL
+ //
+ // this is very ugly but I don't see any other way to make this work
+ long style = 0;
+ if ( GetRichVersion() > 1 )
+ {
+ if ( !HasFlag(wxTE_NOHIDESEL) )
+ {
+ // setting ECO_NOHIDESEL also sets WS_VISIBLE and possibly
+ // others, remember the style so we can reset it later if needed
+ style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
+ ::SendMessage(GetHwnd(), EM_SETOPTIONS,
+ ECOOP_OR, ECO_NOHIDESEL);
+ }
+ //else: everything is already ok
+ }
+#endif // wxUSE_RICHEDIT
+
+ ::SendMessage(hWnd, EM_SCROLLCARET, 0, (LPARAM)0);
+
+#if wxUSE_RICHEDIT
+ // restore ECO_NOHIDESEL if we changed it
+ if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL) )
+ {
+ ::SendMessage(GetHwnd(), EM_SETOPTIONS,
+ ECOOP_AND, ~ECO_NOHIDESEL);
+ if ( style != ::GetWindowLong(GetHwnd(), GWL_STYLE) )
+ ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
+ }
+#endif // wxUSE_RICHEDIT
+ }
+}
+
+// ----------------------------------------------------------------------------
+// Working with files
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::DoLoadFile(const wxString& file, int fileType)
+{
+ if ( wxTextCtrlBase::DoLoadFile(file, fileType) )
+ {
+ // update the size limit if needed
+ AdjustSpaceLimit();
+
+ return true;
+ }
+
+ return false;
+}
+
+// ----------------------------------------------------------------------------
+// dirty status
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::IsModified() const
+{
+ return ::SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0;
+}
+
+void wxTextCtrl::MarkDirty()
+{
+ ::SendMessage(GetHwnd(), EM_SETMODIFY, TRUE, 0);
+}
+
+void wxTextCtrl::DiscardEdits()
+{
+ ::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, y, 0);
+
+ return charIndex + x;
+}
+
+bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
+{
+ HWND hWnd = GetHwnd();
+
+ // This gets the line number containing the character
+ long lineNo;
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ lineNo = ::SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, pos);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ lineNo = ::SendMessage(hWnd, EM_LINEFROMCHAR, pos, 0);
+ }
+
+ if ( lineNo == -1 )
+ {
+ // no such line
+ return false;
+ }
+
+ // This gets the char index for the _beginning_ of this line
+ long charIndex = ::SendMessage(hWnd, EM_LINEINDEX, lineNo, 0);
+ if ( charIndex == -1 )
+ {
+ return false;
+ }
+
+ // The X position must therefore be the different between pos and charIndex
+ if ( x )
+ *x = pos - charIndex;
+ if ( y )
+ *y = lineNo;
+
+ return true;
+}
+
+wxTextCtrlHitTestResult
+wxTextCtrl::HitTest(const wxPoint& pt, long *posOut) const
+{
+ // first get the position from Windows
+ LPARAM lParam;
+
+#if wxUSE_RICHEDIT
+ POINTL ptl;
+ if ( IsRich() )
+ {
+ // for rich edit controls the position is passed iva the struct fields
+ ptl.x = pt.x;
+ ptl.y = pt.y;
+ lParam = (LPARAM)&ptl;
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ // for the plain ones, we are limited to 16 bit positions which are
+ // combined in a single 32 bit value
+ lParam = MAKELPARAM(pt.x, pt.y);
+ }
+
+ LRESULT pos = ::SendMessage(GetHwnd(), EM_CHARFROMPOS, 0, lParam);
+
+ if ( pos == -1 )
+ {
+ // this seems to indicate an error...
+ return wxTE_HT_UNKNOWN;
+ }
+
+#if wxUSE_RICHEDIT
+ if ( !IsRich() )
+#endif // wxUSE_RICHEDIT
+ {
+ // for plain EDIT controls the higher word contains something else
+ pos = LOWORD(pos);
+ }
+
+
+ // next determine where it is relatively to our point: EM_CHARFROMPOS
+ // always returns the closest character but we need to be more precise, so
+ // double check that we really are where it pretends
+ POINTL ptReal;
+
+#if wxUSE_RICHEDIT
+ // FIXME: we need to distinguish between richedit 2 and 3 here somehow but
+ // we don't know how to do it
+ if ( IsRich() )
+ {
+ ::SendMessage(GetHwnd(), EM_POSFROMCHAR, (WPARAM)&ptReal, pos);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ LRESULT lRc = ::SendMessage(GetHwnd(), EM_POSFROMCHAR, pos, 0);
+
+ if ( lRc == -1 )
+ {
+ // this is apparently returned when pos corresponds to the last
+ // position
+ ptReal.x =
+ ptReal.y = 0;
+ }
+ else
+ {
+ ptReal.x = LOWORD(lRc);
+ ptReal.y = HIWORD(lRc);
+ }
+ }
+
+ wxTextCtrlHitTestResult rc;
+
+ if ( pt.y > ptReal.y + GetCharHeight() )
+ rc = wxTE_HT_BELOW;
+ else if ( pt.x > ptReal.x + GetCharWidth() )
+ rc = wxTE_HT_BEYOND;
+ else
+ rc = wxTE_HT_ON_TEXT;
+
+ if ( posOut )
+ *posOut = pos;
+
+ return rc;
+}
+
+// ----------------------------------------------------------------------------
+//
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::ShowPosition(long pos)
+{
+ HWND hWnd = GetHwnd();
+
+ // To scroll to a position, we pass the number of lines and characters
+ // to scroll *by*. This means that we need to:
+ // (1) Find the line position of the current line.
+ // (2) Find the line position of pos.
+ // (3) Scroll by (pos - current).
+ // For now, ignore the horizontal scrolling.
+
+ // 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, -1, 0L);
+
+ int currentLineLineNo = (int)::SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, 0, 0);
+
+ int specifiedLineLineNo = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, pos, 0);
+
+ int linesToScroll = specifiedLineLineNo - currentLineLineNo;
+
+ if (linesToScroll != 0)
+ ::SendMessage(hWnd, EM_LINESCROLL, 0, linesToScroll);
+}
+
+long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const
+{
+ return ::SendMessage(GetHwnd(), EM_LINELENGTH, pos, 0);
+}
+
+int wxTextCtrl::GetLineLength(long lineNo) const
+{
+ long pos = XYToPosition(0, lineNo);
+
+ return GetLengthOfLineContainingPos(pos);
+}
+
+wxString wxTextCtrl::GetLineText(long lineNo) const
+{
+ size_t len = (size_t)GetLineLength(lineNo) + 1;
+
+ // there must be at least enough place for the length WORD in the
+ // buffer
+ len += sizeof(WORD);
+
+ wxString str;
+ {
+ wxStringBufferLength tmp(str, len);
+ wxChar *buf = tmp;
+
+ *(WORD *)buf = (WORD)len;
+ len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
+
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ // remove the '\r' returned by the rich edit control, the user code
+ // should never see it
+ if ( buf[len - 2] == _T('\r') && buf[len - 1] == _T('\n') )
+ {
+ // richedit 1.0 uses "\r\n" as line terminator, so remove "\r"
+ // here and "\n" below
+ buf[len - 2] = _T('\n');
+ len--;
+ }
+ else if ( buf[len - 1] == _T('\r') )
+ {
+ // richedit 2.0+ uses only "\r", replace it with "\n"
+ buf[len - 1] = _T('\n');
+ }
+ }
+#endif // wxUSE_RICHEDIT
+
+ // remove the '\n' at the end, if any (this is how this function is
+ // supposed to work according to the docs)
+ if ( buf[len - 1] == _T('\n') )
+ {
+ len--;
+ }
+
+ buf[len] = 0;
+ tmp.SetLength(len);
+ }
+
+ return str;
+}
+
+void wxTextCtrl::SetMaxLength(unsigned long len)
+{
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, len ? len : 0x7fffffff);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ wxTextEntry::SetMaxLength(len);
+ }
+}
+
+// ----------------------------------------------------------------------------
+// Undo/redo
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::Redo()
+{
+#if wxUSE_RICHEDIT
+ if ( GetRichVersion() > 1 )
+ {
+ ::SendMessage(GetHwnd(), EM_REDO, 0, 0);
+ return;
+ }
+#endif // wxUSE_RICHEDIT
+
+ wxTextEntry::Redo();
+}
+
+bool wxTextCtrl::CanRedo() const
+{
+#if wxUSE_RICHEDIT
+ if ( GetRichVersion() > 1 )
+ return ::SendMessage(GetHwnd(), EM_CANREDO, 0, 0) != 0;
+#endif // wxUSE_RICHEDIT
+
+ return wxTextEntry::CanRedo();
+}
+
+// ----------------------------------------------------------------------------
+// caret handling (Windows only)
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::ShowNativeCaret(bool show)
+{
+ if ( show != m_isNativeCaretShown )
+ {
+ 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/shown 2 times before)
+ return false;
+ }
+
+ m_isNativeCaretShown = show;
+ }
+
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// implementation details
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::Command(wxCommandEvent & event)
+{
+ SetValue(event.GetString());
+ ProcessCommand (event);
+}
+
+void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
+{
+ // By default, load the first file into the text window.
+ if (event.GetNumberOfFiles() > 0)
+ {
+ LoadFile(event.GetFiles()[0]);
+ }
+}
+
+// ----------------------------------------------------------------------------
+// kbd input processing
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
+{
+ // check for our special keys here: if we don't do it and the parent frame
+ // uses them as accelerators, they wouldn't work at all, so we disable
+ // usual preprocessing for them
+ if ( msg->message == WM_KEYDOWN )
+ {
+ const WPARAM vkey = msg->wParam;
+ if ( HIWORD(msg->lParam) & KF_ALTDOWN )
+ {
+ // Alt-Backspace is accelerator for "Undo"
+ if ( vkey == VK_BACK )
+ return false;
+ }
+ else // no Alt
+ {
+ // we want to process some Ctrl-foo and Shift-bar but no key
+ // combinations without either Ctrl or Shift nor with both of them
+ // pressed
+ const int ctrl = wxIsCtrlDown(),
+ shift = wxIsShiftDown();
+ switch ( ctrl + shift )
+ {
+ default:
+ wxFAIL_MSG( _T("how many modifiers have we got?") );
+ // fall through
+
+ case 0:
+ if ( IsMultiLine() && vkey == VK_RETURN )
+ return false;
+ // fall through
+ case 2:
+ break;
+
+ case 1:
+ // either Ctrl or Shift pressed
+ if ( ctrl )
+ {
+ switch ( vkey )
+ {
+ case 'C':
+ case 'V':
+ case 'X':
+ case VK_INSERT:
+ case VK_DELETE:
+ case VK_HOME:
+ case VK_END:
+ return false;
+ }
+ }
+ else // Shift is pressed
+ {
+ if ( vkey == VK_INSERT || vkey == VK_DELETE )
+ return false;
+ }
+ }
+ }
+ }
+
+ return wxControl::MSWShouldPreProcessMessage(msg);
+}
+
+void wxTextCtrl::OnChar(wxKeyEvent& event)
+{
+ switch ( event.GetKeyCode() )
+ {
+ case WXK_RETURN:
+ {
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
+ InitCommandEvent(event);
+ event.SetString(GetValue());
+ if ( HandleWindowEvent(event) )
+ if ( !HasFlag(wxTE_MULTILINE) )
+ return;
+ //else: multiline controls need Enter for themselves
+ }
+ break;
+
+ case WXK_TAB:
+ // ok, so this is getting absolutely ridiculous but I don't see
+ // any other way to fix this bug: when a multiline text control is
+ // inside a wxFrame, we need to generate the navigation event as
+ // otherwise nothing happens at all, but when the same control is
+ // created inside a dialog, IsDialogMessage() *does* switch focus
+ // all by itself and so if we do it here as well, it is advanced
+ // twice and goes to the next control... to prevent this from
+ // happening we're doing this ugly check, the logic being that if
+ // we don't have focus then it had been already changed to the next
+ // control
+ //
+ // the right thing to do would, of course, be to understand what
+ // the hell is IsDialogMessage() doing but this is beyond my feeble
+ // forces at the moment unfortunately
+ if ( !(m_windowStyle & wxTE_PROCESS_TAB))
+ {
+ if ( FindFocus() == this )
+ {
+ int flags = 0;
+ if (!event.ShiftDown())
+ flags |= wxNavigationKeyEvent::IsForward ;
+ if (event.ControlDown())
+ flags |= wxNavigationKeyEvent::WinChange ;
+ if (Navigate(flags))
+ return;
+ }
+ }
+ else
+ {
+ // Insert tab since calling the default Windows handler
+ // doesn't seem to do it
+ WriteText(wxT("\t"));
+ return;
+ }
+ break;
+ }
+
+ // no, we didn't process it
+ event.Skip();
+}
+
+void wxTextCtrl::OnKeyDown(wxKeyEvent& event)
+{
+ // richedit control doesn't send WM_PASTE, WM_CUT and WM_COPY messages
+ // when Ctrl-V, X or C is pressed and this prevents wxClipboardTextEvent
+ // from working. So we work around it by intercepting these shortcuts
+ // ourselves and emitting clipboard events (which richedit will handle,
+ // so everything works as before, including pasting of rich text):
+ if ( event.GetModifiers() == wxMOD_CONTROL && IsRich() )
+ {
+ switch ( event.GetKeyCode() )
+ {
+ case 'C':
+ Copy();
+ return;
+ case 'X':
+ Cut();
+ return;
+ case 'V':
+ Paste();
+ return;
+ default:
+ break;
+ }
+ }
+
+ // no, we didn't process it
+ event.Skip();
+}
+
+WXLRESULT wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
+{
+ WXLRESULT lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
+
+ switch ( nMsg )
+ {
+ case WM_GETDLGCODE:
+ {
+ // we always want the chars and the arrows: the arrows for
+ // navigation and the chars because we want Ctrl-C to work even
+ // in a read only control
+ long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
+
+ if ( IsEditable() )
+ {
+ // we may have several different cases:
+ // 1. normal: both TAB and ENTER are used for navigation
+ // 2. ctrl wants TAB for itself: ENTER is used to pass to
+ // the next control in the dialog
+ // 3. ctrl wants ENTER for itself: TAB is used for dialog
+ // navigation
+ // 4. ctrl wants both TAB and ENTER: Ctrl-ENTER is used to
+ // go to the next control (we need some way to do it)
+
+ // multiline controls should always get ENTER for themselves
+ if ( HasFlag(wxTE_PROCESS_ENTER) || HasFlag(wxTE_MULTILINE) )
+ lDlgCode |= DLGC_WANTMESSAGE;
+
+ if ( HasFlag(wxTE_PROCESS_TAB) )
+ lDlgCode |= DLGC_WANTTAB;
+
+ lRc |= lDlgCode;
+ }
+ else // !editable
+ {
+ // NB: use "=", not "|=" as the base class version returns
+ // the same flags in the disabled state as usual (i.e.
+ // including DLGC_WANTMESSAGE). This is strange (how
+ // does it work in the native Win32 apps?) but for now
+ // live with it.
+ lRc = lDlgCode;
+ }
+ }
+ break;
+ }
+
+ return lRc;
+}
+
+// ----------------------------------------------------------------------------
+// text control event processing
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::SendUpdateEvent()
+{
+ switch ( m_updatesCount )
+ {
+ case 0:
+ // remember that we've got an update
+ m_updatesCount++;
+ break;
+
+ case 1:
+ // we had already sent one event since the last control modification
+ return false;
+
+ default:
+ wxFAIL_MSG( _T("unexpected wxTextCtrl::m_updatesCount value") );
+ // fall through
+
+ case -1:
+ // we hadn't updated the control ourselves, this event comes from
+ // the user, don't need to ignore it nor update the count
+ break;
+
+ case -2:
+ // the control was updated programmatically and we do NOT want to
+ // send events
+ return false;
+ }
+
+ return SendTextUpdatedEvent();
+}
+
+bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
+{
+ switch ( param )
+ {
+ case EN_CHANGE:
+ SendUpdateEvent();
+ break;
+
+ case EN_MAXTEXT:
+ // the text size limit has been hit -- try to increase it
+ if ( !AdjustSpaceLimit() )
+ {
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId);
+ InitCommandEvent(event);
+ event.SetString(GetValue());
+ ProcessCommand(event);
+ }
+ break;
+
+ // the other edit notification messages are not processed (or, in
+ // the case of EN_{SET/KILL}FOCUS were already handled at WM_SET/
+ // KILLFOCUS level)
+ default:
+ return false;
+ }
+
+ // processed
+ return true;
+}
+
+WXHBRUSH wxTextCtrl::MSWControlColor(WXHDC hDC, WXHWND hWnd)
+{
+ if ( !IsEnabled() && !HasFlag(wxTE_MULTILINE) )
+ return MSWControlColorDisabled(hDC);
+
+ return wxTextCtrlBase::MSWControlColor(hDC, hWnd);
+}
+
+bool wxTextCtrl::HasSpaceLimit(unsigned int *len) const
+{
+ // HACK: we try to automatically extend the limit for the amount of text
+ // to allow (interactively) entering more than 64Kb of text under
+ // Win9x but we shouldn't reset the text limit which was previously
+ // set explicitly with SetMaxLength()
+ //
+ // Unfortunately there is no EM_GETLIMITTEXTSETBYUSER and so we don't
+ // know the limit we set (if any). We could solve this by storing the
+ // limit we set in wxTextCtrl but to save space we prefer to simply
+ // test here the actual limit value: we consider that SetMaxLength()
+ // can only be called for small values while EN_MAXTEXT is only sent
+ // for large values (in practice the default limit seems to be 30000
+ // but make it smaller just to be on the safe side)
+ *len = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
+ return *len < 10001;
+
+}
+
+bool wxTextCtrl::AdjustSpaceLimit()
+{
+ unsigned int limit;
+ if ( HasSpaceLimit(&limit) )
+ return false;
+
+ unsigned int len = ::GetWindowTextLength(GetHwnd());
+ if ( len >= limit )
+ {
+ // increment in 32Kb chunks
+ SetMaxLength(len + 0x8000);
+ }
+
+ // we changed the limit
+ return true;
+}
+
+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
+ // to be able to scroll it without mouse
+ return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
+}
+
+wxSize wxTextCtrl::DoGetBestSize() const
+{
+ int cx, cy;
+ wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
+
+ int wText = DEFAULT_ITEM_WIDTH;
+
+ int hText = cy;
+ if ( m_windowStyle & wxTE_MULTILINE )
+ {
+ hText *= wxMax(wxMin(GetNumberOfLines(), 10), 2);
+ }
+ //else: for single line control everything is ok