+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, TRUE /* selection only */);
+}
+
+void wxTextCtrl::Remove(long from, long to)
+{
+ Replace(from, to, wxEmptyString);
+}
+
+bool wxTextCtrl::LoadFile(const wxString& file)
+{
+ if ( wxTextCtrlBase::LoadFile(file) )
+ {
+ // update the size limit if needed
+ AdjustSpaceLimit();
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+bool wxTextCtrl::IsModified() const
+{
+ return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0;
+}
+
+void wxTextCtrl::MarkDirty()
+{
+ SendMessage(GetHwnd(), EM_SETMODIFY, TRUE, 0L);
+}
+
+void wxTextCtrl::DiscardEdits()
+{
+ SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
+}
+
+int wxTextCtrl::GetNumberOfLines() const
+{
+ return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)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, (WPARAM)y, (LPARAM)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, (LPARAM)pos);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ lineNo = SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)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, (WPARAM)lineNo, (LPARAM)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;
+}
+
+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, (WPARAM)-1, (LPARAM)0L);
+
+ int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
+
+ int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
+
+ int linesToScroll = specifiedLineLineNo - currentLineLineNo;
+
+ if (linesToScroll != 0)
+ (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
+}
+
+long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const
+{
+ return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)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') )
+ {
+ buf[len - 2] = _T('\n');
+ len--;
+ }
+ }
+#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)
+{
+ ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0);
+}
+
+// ----------------------------------------------------------------------------
+// Undo/redo
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::Undo()
+{
+ if (CanUndo())
+ {
+ ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
+ }
+}
+
+void wxTextCtrl::Redo()
+{
+ if (CanRedo())
+ {
+ // Same as Undo, since Undo undoes the undo, i.e. a redo.
+ ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
+ }
+}
+
+bool wxTextCtrl::CanUndo() const
+{
+ return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
+}
+
+bool wxTextCtrl::CanRedo() const
+{
+ return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
+}
+
+// ----------------------------------------------------------------------------
+// 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/showh 2 times before)
+ return false;
+ }
+
+ m_isNativeCaretShown = show;
+ }
+
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// implemenation 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* pMsg)
+{
+ MSG *msg = (MSG *)pMsg;
+
+ // 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 )
+ {
+ WORD vkey = (WORD) msg->wParam;
+ if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN )
+ {
+ 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:
+ 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(pMsg);
+}
+
+void wxTextCtrl::OnChar(wxKeyEvent& event)
+{
+ switch ( event.GetKeyCode() )
+ {
+ case WXK_RETURN:
+ if ( !(m_windowStyle & wxTE_MULTILINE) )
+ {
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
+ InitCommandEvent(event);
+ event.SetString(GetValue());
+ if ( GetEventHandler()->ProcessEvent(event) )
+ return;
+ }
+ //else: multiline controls need Enter for themselves
+
+ break;
+
+ case WXK_TAB:
+ // always produce navigation event - even if we process TAB
+ // ourselves the fact that we got here means that the user code
+ // decided to skip processing of this TAB - probably to let it
+ // do its default job.
+ {
+ wxNavigationKeyEvent eventNav;
+ eventNav.SetDirection(!event.ShiftDown());
+ eventNav.SetWindowChange(event.ControlDown());
+ eventNav.SetEventObject(this);
+
+ if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
+ return;
+ }
+ break;
+ }
+
+ // no, we didn't process it
+ event.Skip();
+}
+
+long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
+{
+ long lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
+
+ if ( nMsg == 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 case: both TAB and ENTER are used for dlg navigation
+ // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
+ // next control in the dialog
+ // 3. ctrl which wants ENTER for itself: TAB is used for dialog
+ // navigation
+ // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
+ // to the next control
+
+ // the multiline edit control should always get <Return> for itself
+ 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 is this 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;
+ }
+ }
+
+ return lRc;
+}
+
+// ----------------------------------------------------------------------------
+// text control event processing
+// ----------------------------------------------------------------------------
+
+bool wxTextCtrl::SendUpdateEvent()
+{
+ // is event reporting suspended?
+ if ( m_suppressNextUpdate )
+ {
+ // do process the next one
+ m_suppressNextUpdate = FALSE;
+
+ return FALSE;
+ }
+
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
+ InitCommandEvent(event);
+ event.SetString(GetValue());
+
+ return ProcessCommand(event);
+}
+
+bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
+{
+ switch ( param )
+ {
+ case EN_SETFOCUS:
+ case EN_KILLFOCUS:
+ {
+ wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
+ : wxEVT_SET_FOCUS,
+ m_windowId);
+ event.SetEventObject(this);
+ GetEventHandler()->ProcessEvent(event);
+ }
+ break;
+
+ 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
+ default:
+ return FALSE;
+ }
+
+ // processed
+ return TRUE;
+}
+
+WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
+#if wxUSE_CTL3D
+ WXUINT message,
+ WXWPARAM wParam,
+ WXLPARAM lParam
+#else
+ WXUINT WXUNUSED(message),
+ WXWPARAM WXUNUSED(wParam),
+ WXLPARAM WXUNUSED(lParam)
+#endif
+ )
+{
+#if wxUSE_CTL3D
+ if ( m_useCtl3D )
+ {
+ HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
+ return (WXHBRUSH) hbrush;
+ }
+#endif // wxUSE_CTL3D
+
+ HDC hdc = (HDC)pDC;
+ wxColour colBack = GetBackgroundColour();
+
+ if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
+ colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
+
+ ::SetBkColor(hdc, wxColourToRGB(colBack));
+ ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
+
+ wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
+
+ return (WXHBRUSH)brush->GetResourceHandle();
+}
+
+// In WIN16, need to override normal erasing because
+// Ctl3D doesn't use the wxWindows background colour.
+#ifdef __WIN16__
+void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
+{
+ wxColour col(m_backgroundColour);
+
+#if wxUSE_CTL3D
+ if (m_useCtl3D)
+ col = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
+#endif
+
+ RECT rect;
+ ::GetClientRect(GetHwnd(), &rect);
+
+ COLORREF ref = wxColourToRGB(col);
+ HBRUSH hBrush = ::CreateSolidBrush(ref);
+ if ( !hBrush )
+ wxLogLastError(wxT("CreateSolidBrush"));
+
+ HDC hdc = (HDC)event.GetDC()->GetHDC();
+
+ int mode = ::SetMapMode(hdc, MM_TEXT);
+
+ ::FillRect(hdc, &rect, hBrush);
+ ::DeleteObject(hBrush);
+ ::SetMapMode(hdc, mode);
+
+}
+#endif // Win16
+
+bool wxTextCtrl::AdjustSpaceLimit()
+{
+#ifndef __WIN16__
+ unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
+
+ // 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()
+ //
+ // 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
+ // values < 32Kb
+ if ( limit < 0x8000 )
+ {
+ // we've got more text than limit set by SetMaxLength()
+ return FALSE;
+ }
+
+ unsigned int len = ::GetWindowTextLength(GetHwnd());
+ if ( len >= limit )
+ {
+ limit = len + 0x8000; // 32Kb
+
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ // as a nice side effect, this also allows passing limit > 64Kb
+ ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ if ( limit > 0xffff )
+ {
+ // this will set it to a platform-dependent maximum (much more
+ // than 64Kb under NT)
+ limit = 0;
+ }
+
+ ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
+ }
+ }
+#endif // !Win16
+
+ // we changed the limit
+ return TRUE;
+}
+
+bool wxTextCtrl::AcceptsFocus() 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 = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
+ if ( m_windowStyle & wxTE_MULTILINE )
+ {
+ hText *= wxMax(GetNumberOfLines(), 5);
+ }
+ //else: for single line control everything is ok
+
+ return wxSize(wText, hText);
+}
+
+// ----------------------------------------------------------------------------
+// standard handlers for standard edit menu events
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
+{
+ Cut();
+}
+
+void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
+{
+ Copy();
+}
+
+void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
+{
+ Paste();
+}
+
+void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
+{
+ Undo();
+}
+
+void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
+{
+ Redo();
+}
+
+void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
+{
+ long from, to;
+ GetSelection(& from, & to);
+ if (from != -1 && to != -1)
+ Remove(from, to);
+}
+
+void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
+{
+ SetSelection(-1, -1);
+}
+
+void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
+{
+ event.Enable( CanCut() );
+}
+
+void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
+{
+ event.Enable( CanCopy() );
+}
+
+void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
+{
+ event.Enable( CanPaste() );
+}
+
+void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
+{
+ event.Enable( CanUndo() );
+}
+
+void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
+{
+ event.Enable( CanRedo() );
+}
+
+void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
+{
+ long from, to;
+ GetSelection(& from, & to);
+ event.Enable(from != -1 && to != -1 && from != to && IsEditable()) ;
+}
+
+void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
+{
+ event.Enable(GetLastPosition() > 0);
+}
+
+void wxTextCtrl::OnRightClick(wxMouseEvent& event)
+{
+#if wxUSE_RICHEDIT
+ if (IsRich())
+ {
+ if (!m_privateContextMenu)
+ {
+ m_privateContextMenu = new wxMenu;
+ m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
+ m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
+ m_privateContextMenu->AppendSeparator();
+ m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
+ m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
+ m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
+ m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
+ m_privateContextMenu->AppendSeparator();
+ m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
+ }
+ PopupMenu(m_privateContextMenu, event.GetPosition());
+ return;
+ }
+ else
+#endif
+ event.Skip();