+// ----------------------------------------------------------------------------
+// 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
+{