1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "textctrl.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
43 #include "wx/module.h"
46 #include "wx/clipbrd.h"
49 #include "wx/textfile.h"
53 #include "wx/msw/private.h"
57 #include <sys/types.h>
59 #if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__))
63 // old mingw32 doesn't define this
65 #define CFM_CHARSET 0x08000000
69 #define CFM_BACKCOLOR 0x04000000
72 // cygwin does not have these defined for richedit
74 #define ENM_LINK 0x04000000
77 #ifndef EM_AUTOURLDETECT
78 #define EM_AUTOURLDETECT (WM_USER + 91)
82 #define EN_LINK 0x070b
84 typedef struct _enlink
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
100 // this module initializes RichEdit DLL if needed
101 class wxRichEditModule
: public wxModule
104 virtual bool OnInit();
105 virtual void OnExit();
107 // get the version currently loaded, -1 if none
108 static int GetLoadedVersion() { return ms_verRichEdit
; }
110 // load the richedit DLL of at least of required version
111 static bool Load(int version
= 1);
114 // the handle to richedit DLL and the version of the DLL loaded
115 static HINSTANCE ms_hRichEdit
;
117 // the DLL version loaded or -1 if none
118 static int ms_verRichEdit
;
120 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
123 HINSTANCE
wxRichEditModule::ms_hRichEdit
= (HINSTANCE
)NULL
;
124 int wxRichEditModule::ms_verRichEdit
= -1;
126 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
128 #endif // wxUSE_RICHEDIT
130 // ----------------------------------------------------------------------------
131 // event tables and other macros
132 // ----------------------------------------------------------------------------
134 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
136 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
137 EVT_CHAR(wxTextCtrl::OnChar
)
138 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
140 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
141 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
142 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
143 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
144 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
146 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
147 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
148 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
149 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
150 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
152 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
156 // ============================================================================
158 // ============================================================================
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 wxTextCtrl::wxTextCtrl()
171 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
172 const wxString
& value
,
176 const wxValidator
& validator
,
177 const wxString
& name
)
179 // base initialization
180 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
184 parent
->AddChild(this);
186 // translate wxWin style flags to MSW ones, checking for consistency while
188 long msStyle
= ES_LEFT
| WS_TABSTOP
;
190 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
191 msStyle
|= WS_CLIPSIBLINGS
;
193 if ( m_windowStyle
& wxTE_MULTILINE
)
195 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
196 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
198 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
199 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
200 msStyle
|= WS_VSCROLL
;
201 m_windowStyle
|= wxTE_PROCESS_ENTER
;
205 // there is really no reason to not have this style for single line
207 msStyle
|= ES_AUTOHSCROLL
;
210 if ( m_windowStyle
& wxHSCROLL
)
211 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
213 if ( m_windowStyle
& wxTE_READONLY
)
214 msStyle
|= ES_READONLY
;
216 if ( m_windowStyle
& wxTE_PASSWORD
)
217 msStyle
|= ES_PASSWORD
;
219 if ( m_windowStyle
& wxTE_AUTO_SCROLL
)
220 msStyle
|= ES_AUTOHSCROLL
;
222 if ( m_windowStyle
& wxTE_NOHIDESEL
)
223 msStyle
|= ES_NOHIDESEL
;
225 // we always want the characters and the arrows
226 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
228 // we may have several different cases:
229 // 1. normal case: both TAB and ENTER are used for dialog navigation
230 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
231 // control in the dialog
232 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
233 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
235 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
236 m_lDlgCode
|= DLGC_WANTMESSAGE
;
237 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
238 m_lDlgCode
|= DLGC_WANTTAB
;
240 // do create the control - either an EDIT or RICHEDIT
241 wxString windowClass
= wxT("EDIT");
244 if ( m_windowStyle
& wxTE_RICH
)
246 static bool s_errorGiven
= FALSE
; // MT-FIXME
248 // only give the error msg once if the DLL can't be loaded
251 // first try to load the RichEdit DLL (will do nothing if already
253 if ( !wxRichEditModule::Load() )
255 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
267 msStyle
|= ES_AUTOVSCROLL
;
268 // Experimental: this seems to help with the scroll problem. See messages from Jekabs Andrushaitis <j.andrusaitis@konts.lv>
269 // wx-dev list, entitled "[wx-dev] wxMSW-EVT_KEY_DOWN and wxMSW-wxTextCtrl" and "[wx-dev] TextCtrl (RichEdit)"
270 // Unfortunately, showing the selection in blue when the control doesn't have
271 // the focus is non-standard behaviour, and we need to find another workaround.
272 //msStyle |= ES_NOHIDESEL ;
275 int ver
= wxRichEditModule::GetLoadedVersion();
278 windowClass
= wxT("RICHEDIT");
282 #ifndef RICHEDIT_CLASS
283 wxString RICHEDIT_CLASS
;
284 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), ver
);
286 RICHEDIT_CLASS
+= _T('W');
288 RICHEDIT_CLASS
+= _T('A');
289 #endif // Unicode/ANSI
290 #endif // !RICHEDIT_CLASS
292 windowClass
= RICHEDIT_CLASS
;
298 #endif // wxUSE_RICHEDIT
300 // we need to turn '\n's into "\r\n"s for the multiline controls
302 if ( m_windowStyle
& wxTE_MULTILINE
)
304 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
311 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
314 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
319 // have to enable events manually
320 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
322 if ( m_windowStyle
& wxTE_AUTO_URL
)
326 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
329 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
331 #endif // wxUSE_RICHEDIT
336 // Make sure the window style (etc.) reflects the HWND style (roughly)
337 void wxTextCtrl::AdoptAttributesFromHWND()
339 wxWindow::AdoptAttributesFromHWND();
341 HWND hWnd
= GetHwnd();
342 long style
= GetWindowLong(hWnd
, GWL_STYLE
);
344 // retrieve the style to see whether this is an edit or richedit ctrl
348 GetClassName(hWnd
, buf
, WXSIZEOF(buf
));
350 if ( wxStricmp(buf
, wxT("EDIT")) == 0 )
354 #endif // wxUSE_RICHEDIT
356 if (style
& ES_MULTILINE
)
357 m_windowStyle
|= wxTE_MULTILINE
;
358 if (style
& ES_PASSWORD
)
359 m_windowStyle
|= wxTE_PASSWORD
;
360 if (style
& ES_READONLY
)
361 m_windowStyle
|= wxTE_READONLY
;
362 if (style
& ES_WANTRETURN
)
363 m_windowStyle
|= wxTE_PROCESS_ENTER
;
366 // ----------------------------------------------------------------------------
367 // set/get the controls text
368 // ----------------------------------------------------------------------------
370 wxString
wxTextCtrl::GetValue() const
372 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
373 // retrieving more than 64Kb under Win9x
379 int len
= GetWindowTextLength(GetHwnd());
382 // alloc one extra WORD as needed by the control
383 wxChar
*p
= str
.GetWriteBuf(++len
);
386 textRange
.chrg
.cpMin
= 0;
387 textRange
.chrg
.cpMax
= -1;
388 textRange
.lpstrText
= p
;
390 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
392 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
393 // the newlines which is neither Unix nor Windows style (Win95 with
394 // riched20.dll shows this behaviour) - convert it to something
398 if ( *p
== _T('\r') )
404 //else: no text at all, leave the string empty
408 #endif // wxUSE_RICHEDIT
410 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
411 // same one as above for consitency
412 wxString str
= wxGetWindowText(GetHWND());
414 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
417 void wxTextCtrl::SetValue(const wxString
& value
)
419 // if the text is long enough, it's faster to just set it instead of first
420 // comparing it with the old one (chances are that it will be different
421 // anyhow, this comparison is there to avoid flicker for small single-line
422 // edit controls mostly)
423 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
425 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
427 SetWindowText(GetHwnd(), valueDos
.c_str());
429 // for compatibility with the GTK and because it is more logical, we
430 // move the cursor to the end of the text after SetValue()
432 // GRG, Jun/2000: Changed this back after a lot of discussion
433 // in the lists. wxWindows 2.2 will have a set of flags to
434 // customize this behaviour.
435 //SetInsertionPointEnd();
441 void wxTextCtrl::WriteText(const wxString
& value
)
443 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
446 // ensure that the new text will be in the default style
448 (m_defaultStyle
.HasFont() || m_defaultStyle
.HasTextColour()) )
451 GetSelection(&start
, &end
);
452 SetStyle(start
, end
, m_defaultStyle
);
454 #endif // wxUSE_RICHEDIT
456 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
461 void wxTextCtrl::AppendText(const wxString
& text
)
463 SetInsertionPointEnd();
467 void wxTextCtrl::Clear()
469 SetWindowText(GetHwnd(), wxT(""));
472 // ----------------------------------------------------------------------------
473 // Clipboard operations
474 // ----------------------------------------------------------------------------
476 void wxTextCtrl::Copy()
480 HWND hWnd
= GetHwnd();
481 SendMessage(hWnd
, WM_COPY
, 0, 0L);
485 void wxTextCtrl::Cut()
489 HWND hWnd
= GetHwnd();
490 SendMessage(hWnd
, WM_CUT
, 0, 0L);
494 void wxTextCtrl::Paste()
498 HWND hWnd
= GetHwnd();
499 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
503 bool wxTextCtrl::CanCopy() const
505 // Can copy if there's a selection
507 GetSelection(& from
, & to
);
508 return (from
!= to
) ;
511 bool wxTextCtrl::CanCut() const
513 // Can cut if there's a selection
515 GetSelection(& from
, & to
);
516 return (from
!= to
) && (IsEditable());
519 bool wxTextCtrl::CanPaste() const
524 int dataFormat
= 0; // 0 == any format
525 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
531 // Standard edit control: check for straight text on clipboard
532 bool isTextAvailable
= FALSE
;
533 if ( ::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
535 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
539 return isTextAvailable
;
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 void wxTextCtrl::SetEditable(bool editable
)
548 HWND hWnd
= GetHwnd();
549 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
552 void wxTextCtrl::SetInsertionPoint(long pos
)
554 HWND hWnd
= GetHwnd();
562 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
563 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
566 #endif // wxUSE_RICHEDIT
568 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
569 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
572 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
579 static const wxChar
*nothing
= _T("");
580 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
584 void wxTextCtrl::SetInsertionPointEnd()
586 long pos
= GetLastPosition();
587 SetInsertionPoint(pos
);
590 long wxTextCtrl::GetInsertionPoint() const
598 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
603 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
607 long wxTextCtrl::GetLastPosition() const
609 HWND hWnd
= GetHwnd();
611 // Will always return a number > 0 (according to docs)
612 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
614 // This gets the char index for the _beginning_ of the last line
615 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
617 // Get number of characters in the last line. We'll add this to the character
618 // index for the last line, 1st position.
619 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
621 return (long)(charIndex
+ lineLength
);
624 // If the return values from and to are the same, there is no
626 void wxTextCtrl::GetSelection(long* from
, long* to
) const
632 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
634 *from
= charRange
.cpMin
;
635 *to
= charRange
.cpMax
;
640 DWORD dwStart
, dwEnd
;
641 WPARAM wParam
= (WPARAM
) &dwStart
; // receives starting position
642 LPARAM lParam
= (LPARAM
) &dwEnd
; // receives ending position
644 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
651 wxString
wxTextCtrl::GetStringSelection() const
653 // the base class version works correctly for the rich text controls
654 // because there the lines are terminated with just '\r' which means that
655 // the string length is not changed in the result of the translations doen
656 // in GetValue() but for the normal ones when we replace "\r\n" with '\n'
657 // we break the indices
660 return wxTextCtrlBase::GetStringSelection();
661 #endif // wxUSE_RICHEDIT
664 GetSelection(&from
, &to
);
669 str
= wxGetWindowText(GetHWND()).Mid(from
, to
- from
);
671 // and now that we have the correct selection, convert it to the
673 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
679 bool wxTextCtrl::IsEditable() const
681 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
683 return ((style
& ES_READONLY
) == 0);
686 // ----------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------
690 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
692 HWND hWnd
= GetHwnd();
693 long fromChar
= from
;
696 // Set selection and remove it
698 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
699 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)TRUE
, (LPARAM
)value
.c_str());
701 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
702 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)0, (LPARAM
)value
.c_str());
706 void wxTextCtrl::Remove(long from
, long to
)
708 HWND hWnd
= GetHwnd();
709 long fromChar
= from
;
712 // Cut all selected text
714 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
715 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)TRUE
, (LPARAM
)"");
717 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
718 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)0, (LPARAM
)"");
722 void wxTextCtrl::SetSelection(long from
, long to
)
724 HWND hWnd
= GetHwnd();
725 long fromChar
= from
;
728 // if from and to are both -1, it means (in wxWindows) that all text should
729 // be selected. Translate into Windows convention
730 if ((from
== -1) && (to
== -1))
737 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
738 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
740 // WPARAM is 0: selection is scrolled into view
741 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
745 bool wxTextCtrl::LoadFile(const wxString
& file
)
747 if ( wxTextCtrlBase::LoadFile(file
) )
749 // update the size limit if needed
758 bool wxTextCtrl::IsModified() const
760 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
763 // Makes 'unmodified'
764 void wxTextCtrl::DiscardEdits()
766 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
769 int wxTextCtrl::GetNumberOfLines() const
771 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
774 long wxTextCtrl::XYToPosition(long x
, long y
) const
776 HWND hWnd
= GetHwnd();
778 // This gets the char index for the _beginning_ of this line
779 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
780 return (long)(x
+ charIndex
);
783 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
785 HWND hWnd
= GetHwnd();
787 // This gets the line number containing the character
792 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
795 #endif // wxUSE_RICHEDIT
796 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
804 // This gets the char index for the _beginning_ of this line
805 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
806 if ( charIndex
== -1 )
811 // The X position must therefore be the different between pos and charIndex
813 *x
= (long)(pos
- charIndex
);
820 void wxTextCtrl::ShowPosition(long pos
)
822 HWND hWnd
= GetHwnd();
824 // To scroll to a position, we pass the number of lines and characters
825 // to scroll *by*. This means that we need to:
826 // (1) Find the line position of the current line.
827 // (2) Find the line position of pos.
828 // (3) Scroll by (pos - current).
829 // For now, ignore the horizontal scrolling.
831 // Is this where scrolling is relative to - the line containing the caret?
832 // Or is the first visible line??? Try first visible line.
833 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
835 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
837 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
839 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
841 if (linesToScroll
!= 0)
842 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
845 int wxTextCtrl::GetLineLength(long lineNo
) const
847 long charIndex
= XYToPosition(0, lineNo
);
848 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
852 wxString
wxTextCtrl::GetLineText(long lineNo
) const
854 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
856 // there must be at least enough place for the length WORD in the
861 wxChar
*buf
= str
.GetWriteBuf(len
);
863 *(WORD
*)buf
= (WORD
)len
;
864 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
867 str
.UngetWriteBuf(len
);
872 void wxTextCtrl::SetMaxLength(unsigned long len
)
874 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
877 // ----------------------------------------------------------------------------
879 // ----------------------------------------------------------------------------
881 void wxTextCtrl::Undo()
885 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
889 void wxTextCtrl::Redo()
893 // Same as Undo, since Undo undoes the undo, i.e. a redo.
894 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
898 bool wxTextCtrl::CanUndo() const
900 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
903 bool wxTextCtrl::CanRedo() const
905 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
908 // ----------------------------------------------------------------------------
909 // implemenation details
910 // ----------------------------------------------------------------------------
912 void wxTextCtrl::Command(wxCommandEvent
& event
)
914 SetValue(event
.GetString());
915 ProcessCommand (event
);
918 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
920 // By default, load the first file into the text window.
921 if (event
.GetNumberOfFiles() > 0)
923 LoadFile(event
.GetFiles()[0]);
927 // ----------------------------------------------------------------------------
928 // kbd input processing
929 // ----------------------------------------------------------------------------
931 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
933 MSG
*msg
= (MSG
*)pMsg
;
935 // check for our special keys here: if we don't do it and the parent frame
936 // uses them as accelerators, they wouldn't work at all, so we disable
937 // usual preprocessing for them
938 if ( msg
->message
== WM_KEYDOWN
)
940 WORD vkey
= msg
->wParam
;
941 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
943 if ( vkey
== VK_BACK
)
948 if ( wxIsCtrlDown() )
962 else if ( wxIsShiftDown() )
964 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
970 return wxControl::MSWShouldPreProcessMessage(pMsg
);
973 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
975 switch ( event
.KeyCode() )
978 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
980 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
981 InitCommandEvent(event
);
982 event
.SetString(GetValue());
983 if ( GetEventHandler()->ProcessEvent(event
) )
986 //else: multiline controls need Enter for themselves
991 // always produce navigation event - even if we process TAB
992 // ourselves the fact that we got here means that the user code
993 // decided to skip processing of this TAB - probably to let it
994 // do its default job.
996 wxNavigationKeyEvent eventNav
;
997 eventNav
.SetDirection(!event
.ShiftDown());
998 eventNav
.SetWindowChange(event
.ControlDown());
999 eventNav
.SetEventObject(this);
1001 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1007 // no, we didn't process it
1011 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1018 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1021 event
.SetEventObject( this );
1022 GetEventHandler()->ProcessEvent(event
);
1028 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1029 InitCommandEvent(event
);
1030 event
.SetString(GetValue());
1031 ProcessCommand(event
);
1036 // the text size limit has been hit - increase it
1037 if ( !AdjustSpaceLimit() )
1039 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1040 InitCommandEvent(event
);
1041 event
.SetString(GetValue());
1042 ProcessCommand(event
);
1046 // the other notification messages are not processed
1060 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1066 WXUINT
WXUNUSED(message
),
1067 WXWPARAM
WXUNUSED(wParam
),
1068 WXLPARAM
WXUNUSED(lParam
)
1075 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1076 return (WXHBRUSH
) hbrush
;
1078 #endif // wxUSE_CTL3D
1081 if (GetParent()->GetTransparentBackground())
1082 SetBkMode(hdc
, TRANSPARENT
);
1084 SetBkMode(hdc
, OPAQUE
);
1086 wxColour colBack
= GetBackgroundColour();
1088 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1089 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
1091 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1092 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1094 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1096 return (WXHBRUSH
)brush
->GetResourceHandle();
1099 // In WIN16, need to override normal erasing because
1100 // Ctl3D doesn't use the wxWindows background colour.
1102 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1104 wxColour
col(m_backgroundColour
);
1108 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1112 ::GetClientRect(GetHwnd(), &rect
);
1114 COLORREF ref
= PALETTERGB(col
.Red(),
1117 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1119 wxLogLastError(wxT("CreateSolidBrush"));
1121 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1123 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1125 ::FillRect(hdc
, &rect
, hBrush
);
1126 ::DeleteObject(hBrush
);
1127 ::SetMapMode(hdc
, mode
);
1132 bool wxTextCtrl::AdjustSpaceLimit()
1135 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1137 // HACK: we try to automatically extend the limit for the amount of text
1138 // to allow (interactively) entering more than 64Kb of text under
1139 // Win9x but we shouldn't reset the text limit which was previously
1140 // set explicitly with SetMaxLength()
1142 // we could solve this by storing the limit we set in wxTextCtrl but
1143 // to save space we prefer to simply test here the actual limit
1144 // value: we consider that SetMaxLength() can only be called for
1146 if ( limit
< 0x8000 )
1148 // we've got more text than limit set by SetMaxLength()
1152 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1155 limit
= len
+ 0x8000; // 32Kb
1160 // as a nice side effect, this also allows passing limit > 64Kb
1161 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1164 #endif // wxUSE_RICHEDIT
1166 if ( limit
> 0xffff )
1168 // this will set it to a platform-dependent maximum (much more
1169 // than 64Kb under NT)
1173 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1178 // we changed the limit
1182 bool wxTextCtrl::AcceptsFocus() const
1184 // we don't want focus if we can't be edited
1185 return IsEditable() && wxControl::AcceptsFocus();
1188 wxSize
wxTextCtrl::DoGetBestSize() const
1191 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1193 int wText
= DEFAULT_ITEM_WIDTH
;
1195 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1196 if ( m_windowStyle
& wxTE_MULTILINE
)
1198 hText
*= wxMax(GetNumberOfLines(), 5);
1200 //else: for single line control everything is ok
1202 return wxSize(wText
, hText
);
1205 // ----------------------------------------------------------------------------
1206 // standard handlers for standard edit menu events
1207 // ----------------------------------------------------------------------------
1209 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1214 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1219 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1224 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1229 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1234 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1236 event
.Enable( CanCut() );
1239 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1241 event
.Enable( CanCopy() );
1244 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1246 event
.Enable( CanPaste() );
1249 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1251 event
.Enable( CanUndo() );
1254 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1256 event
.Enable( CanRedo() );
1259 // the rest of the file only deals with the rich edit controls
1262 // ----------------------------------------------------------------------------
1263 // EN_LINK processing
1264 // ----------------------------------------------------------------------------
1266 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1268 NMHDR
*hdr
= (NMHDR
* )lParam
;
1269 if ( hdr
->code
== EN_LINK
)
1271 ENLINK
*enlink
= (ENLINK
*)hdr
;
1273 switch ( enlink
->msg
)
1276 // ok, so it is hardcoded - do we really nee to customize it?
1277 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1282 case WM_LBUTTONDOWN
:
1284 case WM_LBUTTONDBLCLK
:
1285 case WM_RBUTTONDOWN
:
1287 case WM_RBUTTONDBLCLK
:
1288 // send a mouse event
1290 static const wxEventType eventsMouse
[] =
1301 // the event ids are consecutive
1303 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1305 InitMouseEvent(evtMouse
,
1306 GET_X_LPARAM(enlink
->lParam
),
1307 GET_Y_LPARAM(enlink
->lParam
),
1310 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1312 enlink
->chrg
.cpMax
);
1314 InitCommandEvent(event
);
1316 *result
= ProcessCommand(event
);
1328 // ----------------------------------------------------------------------------
1329 // colour setting for the rich edit controls
1330 // ----------------------------------------------------------------------------
1332 // Watcom C++ doesn't define this
1334 #define SCF_ALL 0x0004
1337 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1339 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1341 // colour didn't really change
1347 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1348 // EM_SETBKGNDCOLOR additionally
1349 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1355 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1357 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1359 // colour didn't really change
1365 // change the colour of everything
1368 cf
.cbSize
= sizeof(cf
);
1369 cf
.dwMask
= CFM_COLOR
;
1370 cf
.crTextColor
= wxColourToRGB(colour
);
1371 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1377 // ----------------------------------------------------------------------------
1378 // styling support for rich edit controls
1379 // ----------------------------------------------------------------------------
1381 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1385 // can't do it with normal text control
1389 // the rich text control doesn't handle setting background colour, so don't
1390 // even try if it's the only thing we want to change
1391 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1392 !style
.HasFont() && !style
.HasTextColour() )
1394 // nothing to do: return TRUE if there was really nothing to do and
1395 // FALSE if we failed to set bg colour
1396 return !style
.HasBackgroundColour();
1399 // order the range if needed
1407 // we can only change the format of the selection, so select the range we
1408 // want and restore the old selection later
1409 long startOld
, endOld
;
1410 GetSelection(&startOld
, &endOld
);
1412 // but do we really have to change the selection?
1413 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1416 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
) start
, (LPARAM
) end
);
1418 // initialize CHARFORMAT struct
1425 cf
.cbSize
= sizeof(cf
);
1427 if ( style
.HasFont() )
1429 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1430 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1432 // fill in data from LOGFONT but recalculate lfHeight because we need
1433 // the real height in twips and not the negative number which
1434 // wxFillLogFont() returns (this is correct in general and works with
1435 // the Windows font mapper, but not here)
1437 wxFillLogFont(&lf
, &style
.GetFont());
1438 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1439 cf
.bCharSet
= lf
.lfCharSet
;
1440 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1441 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1443 // also deal with underline/italic/bold attributes: note that we must
1444 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1445 // style to allow clearing it
1448 cf
.dwEffects
|= CFE_ITALIC
;
1451 if ( lf
.lfWeight
== FW_BOLD
)
1453 cf
.dwEffects
|= CFE_BOLD
;
1456 if ( lf
.lfUnderline
)
1458 cf
.dwEffects
|= CFE_UNDERLINE
;
1461 // strikeout fonts are not supported by wxWindows
1464 if ( style
.HasTextColour() )
1466 cf
.dwMask
|= CFM_COLOR
;
1467 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1471 if ( wxRichEditModule::GetLoadedVersion() > 1 && style
.HasBackgroundColour() )
1473 cf
.dwMask
|= CFM_BACKCOLOR
;
1474 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1476 #endif // wxUSE_RICHEDIT2
1478 // do format the selection
1479 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1480 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1483 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1488 // restore the original selection
1489 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
)startOld
, (LPARAM
)endOld
);
1495 // ----------------------------------------------------------------------------
1497 // ----------------------------------------------------------------------------
1499 bool wxRichEditModule::OnInit()
1501 // don't do anything - we will load it when needed
1505 void wxRichEditModule::OnExit()
1509 FreeLibrary(ms_hRichEdit
);
1514 bool wxRichEditModule::Load(int version
)
1516 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1517 _T("incorrect richedit control version requested") );
1519 if ( version
<= ms_verRichEdit
)
1521 // we've already got this or better
1527 ::FreeLibrary(ms_hRichEdit
);
1530 // always try load riched20.dll first - like this we won't have to reload
1531 // it later if we're first asked for RE 1 and then for RE 2 or 3
1532 wxString dllname
= _T("riched20.dll");
1533 ms_hRichEdit
= ::LoadLibrary(dllname
);
1534 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1536 if ( !ms_hRichEdit
&& (version
== 1) )
1538 // fall back to RE 1
1539 dllname
= _T("riched32.dll");
1540 ms_hRichEdit
= ::LoadLibrary(dllname
);
1544 if ( !ms_hRichEdit
)
1546 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1548 ms_verRichEdit
= -1;
1556 #endif // wxUSE_RICHEDIT
1558 #endif // wxUSE_TEXTCTRL