+
+ if ( !::MultiByteToWideChar(codepage, 0, value, -1,
+ (wchar_t *)(const wchar_t *)wchBuf, len) )
+ {
+ wxLogLastError(_T("MultiByteToWideChar"));
+ }
+
+ // finally, stream it in the control
+ const wchar_t *wpc = wchBuf;
+#endif // wxUSE_UNICODE_MSLU
+
+ EDITSTREAM eds;
+ wxZeroMemory(eds);
+ eds.dwCookie = (DWORD)&wpc;
+ // the cast below is needed for broken (very) old mingw32 headers
+ eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn;
+
+ // we're going to receive 2 EN_CHANGE notifications if we got any selection
+ // (same problem as in DoWriteText())
+ if ( selectionOnly && HasSelection() )
+ {
+ // so suppress one of them
+ m_suppressNextUpdate = TRUE;
+ }
+
+ if ( !::SendMessage(GetHwnd(), EM_STREAMIN,
+ SF_TEXT |
+ SF_UNICODE |
+ (selectionOnly ? SFF_SELECTION : 0),
+ (LPARAM)&eds) || eds.dwError )
+ {
+ wxLogLastError(_T("EM_STREAMIN"));
+ }
+
+#if !wxUSE_WCHAR_T
+ free(wchBuf);
+#endif // !wxUSE_WCHAR_T
+
+ return TRUE;
+}
+
+#endif // wxUSE_RICHEDIT
+
+void wxTextCtrl::WriteText(const wxString& value)
+{
+ DoWriteText(value);
+}
+
+void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly)
+{
+ 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 )
+ {
+ 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 below which is confusing for the client code and so should be
+ // avoided
+ //
+ // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
+ // and there is a non empty selection currently and (b) rich text
+ // controls in any case
+ if (
+#if wxUSE_RICHEDIT
+ IsRich() ||
+#endif // wxUSE_RICHEDIT
+ (selectionOnly && HasSelection()) )
+ {
+ m_suppressNextUpdate = TRUE;
+ }
+
+ ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT,
+ 0, (LPARAM)valueDos.c_str());
+
+ // OTOH, non rich text controls don't generate any events at all when
+ // we use WM_SETTEXT -- have to emulate them here
+ if ( !selectionOnly
+#if wxUSE_RICHEDIT
+ && !IsRich()
+#endif // wxUSE_RICHEDIT
+ )
+ {
+ // Windows already sends an update event for single-line
+ // controls.
+ if ( m_windowStyle & wxTE_MULTILINE )
+ SendUpdateEvent();
+ }
+ }
+
+ AdjustSpaceLimit();
+}
+
+void wxTextCtrl::AppendText(const wxString& text)
+{
+ SetInsertionPointEnd();
+
+ WriteText(text);
+
+#if wxUSE_RICHEDIT
+ if ( 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(), wxT(""));
+
+#if wxUSE_RICHEDIT
+ if ( !IsRich() )
+#endif // wxUSE_RICHEDIT
+ {
+ // 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();
+ }
+}
+
+#ifdef __WIN32__
+
+bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event)
+{
+ SetFocus();
+
+ size_t lenOld = GetValue().length();
+
+ wxUint32 code = event.GetRawKeyCode();
+ ::keybd_event(code, 0, 0 /* key press */, 0);
+ ::keybd_event(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__
+
+// ----------------------------------------------------------------------------
+// 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()
+{
+ 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 = GetLastPosition();
+ }
+
+ SetInsertionPoint(pos);
+}
+
+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
+
+ DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
+ return Pos & 0xFFFF;
+}
+
+long wxTextCtrl::GetLastPosition() const
+{
+ int numLines = GetNumberOfLines();
+ long posStartLastLine = XYToPosition(0, numLines - 1);
+
+ long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine);
+
+ return posStartLastLine + lenLastLine;
+}
+
+// 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
+ {
+ DWORD dwStart, dwEnd;
+ ::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
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::SetSelection(long from, long to)
+{
+ // if from and to are both -1, it means (in wxWindows) 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();
+
+#ifdef __WIN32__
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ CHARRANGE range;
+ range.cpMin = from;
+ range.cpMax = to;
+ SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to);
+ }
+
+ if ( scrollCaret )
+ {
+#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
+ if ( GetRichVersion() > 1 )
+ {
+ if ( !HasFlag(wxTE_NOHIDESEL) )
+ {
+ ::SendMessage(GetHwnd(), EM_SETOPTIONS,
+ ECOOP_OR, ECO_NOHIDESEL);
+ }
+ //else: everything is already ok
+ }
+#endif // wxUSE_RICHEDIT
+
+ SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)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);
+ }
+#endif // wxUSE_RICHEDIT
+ }
+#else // Win16
+ // WPARAM is 0: selection is scrolled into view
+ SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(from, to));
+#endif // Win32/16
+}
+
+// ----------------------------------------------------------------------------
+// Editing
+// ----------------------------------------------------------------------------
+
+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 */);
+
+ SendMessage(GetHwnd(), EM_REPLACESEL,
+#ifdef __WIN32__
+ TRUE,