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 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, value
) )
306 // have to enable events manually
307 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
309 if ( m_windowStyle
& wxTE_AUTO_URL
)
313 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
316 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
318 #endif // wxUSE_RICHEDIT
323 // Make sure the window style (etc.) reflects the HWND style (roughly)
324 void wxTextCtrl::AdoptAttributesFromHWND()
326 wxWindow::AdoptAttributesFromHWND();
328 HWND hWnd
= GetHwnd();
329 long style
= GetWindowLong(hWnd
, GWL_STYLE
);
331 // retrieve the style to see whether this is an edit or richedit ctrl
335 GetClassName(hWnd
, buf
, WXSIZEOF(buf
));
337 if ( wxStricmp(buf
, wxT("EDIT")) == 0 )
341 #endif // wxUSE_RICHEDIT
343 if (style
& ES_MULTILINE
)
344 m_windowStyle
|= wxTE_MULTILINE
;
345 if (style
& ES_PASSWORD
)
346 m_windowStyle
|= wxTE_PASSWORD
;
347 if (style
& ES_READONLY
)
348 m_windowStyle
|= wxTE_READONLY
;
349 if (style
& ES_WANTRETURN
)
350 m_windowStyle
|= wxTE_PROCESS_ENTER
;
353 // ----------------------------------------------------------------------------
354 // set/get the controls text
355 // ----------------------------------------------------------------------------
357 wxString
wxTextCtrl::GetValue() const
359 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
360 // retrieving more than 64Kb under Win9x
366 int len
= GetWindowTextLength(GetHwnd());
369 // alloc one extra WORD as needed by the control
370 wxChar
*p
= str
.GetWriteBuf(++len
);
373 textRange
.chrg
.cpMin
= 0;
374 textRange
.chrg
.cpMax
= -1;
375 textRange
.lpstrText
= p
;
377 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
379 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
380 // the newlines which is neither Unix nor Windows style (Win95 with
381 // riched20.dll shows this behaviour) - convert it to something
385 if ( *p
== _T('\r') )
391 //else: no text at all, leave the string empty
395 #endif // wxUSE_RICHEDIT
397 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
398 // same one as above for consitency
399 wxString str
= wxGetWindowText(GetHWND());
401 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
404 void wxTextCtrl::SetValue(const wxString
& value
)
406 // if the text is long enough, it's faster to just set it instead of first
407 // comparing it with the old one (chances are that it will be different
408 // anyhow, this comparison is there to avoid flicker for small single-line
409 // edit controls mostly)
410 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
412 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
414 SetWindowText(GetHwnd(), valueDos
.c_str());
416 // for compatibility with the GTK and because it is more logical, we
417 // move the cursor to the end of the text after SetValue()
419 // GRG, Jun/2000: Changed this back after a lot of discussion
420 // in the lists. wxWindows 2.2 will have a set of flags to
421 // customize this behaviour.
422 //SetInsertionPointEnd();
428 void wxTextCtrl::WriteText(const wxString
& value
)
430 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
433 // ensure that the new text will be in the default style
435 (m_defaultStyle
.HasFont() || m_defaultStyle
.HasTextColour()) )
438 GetSelection(&start
, &end
);
439 SetStyle(start
, end
, m_defaultStyle
);
441 #endif // wxUSE_RICHEDIT
443 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
448 void wxTextCtrl::AppendText(const wxString
& text
)
450 SetInsertionPointEnd();
454 void wxTextCtrl::Clear()
456 SetWindowText(GetHwnd(), wxT(""));
459 // ----------------------------------------------------------------------------
460 // Clipboard operations
461 // ----------------------------------------------------------------------------
463 void wxTextCtrl::Copy()
467 HWND hWnd
= GetHwnd();
468 SendMessage(hWnd
, WM_COPY
, 0, 0L);
472 void wxTextCtrl::Cut()
476 HWND hWnd
= GetHwnd();
477 SendMessage(hWnd
, WM_CUT
, 0, 0L);
481 void wxTextCtrl::Paste()
485 HWND hWnd
= GetHwnd();
486 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
490 bool wxTextCtrl::CanCopy() const
492 // Can copy if there's a selection
494 GetSelection(& from
, & to
);
495 return (from
!= to
) ;
498 bool wxTextCtrl::CanCut() const
500 // Can cut if there's a selection
502 GetSelection(& from
, & to
);
503 return (from
!= to
) && (IsEditable());
506 bool wxTextCtrl::CanPaste() const
511 int dataFormat
= 0; // 0 == any format
512 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
518 // Standard edit control: check for straight text on clipboard
519 bool isTextAvailable
= FALSE
;
520 if ( ::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
522 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
526 return isTextAvailable
;
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 void wxTextCtrl::SetEditable(bool editable
)
535 HWND hWnd
= GetHwnd();
536 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
539 void wxTextCtrl::SetInsertionPoint(long pos
)
541 HWND hWnd
= GetHwnd();
549 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
550 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
553 #endif // wxUSE_RICHEDIT
555 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
556 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
559 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
566 static const wxChar
*nothing
= _T("");
567 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
571 void wxTextCtrl::SetInsertionPointEnd()
573 long pos
= GetLastPosition();
574 SetInsertionPoint(pos
);
577 long wxTextCtrl::GetInsertionPoint() const
585 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
590 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
594 long wxTextCtrl::GetLastPosition() const
596 HWND hWnd
= GetHwnd();
598 // Will always return a number > 0 (according to docs)
599 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
601 // This gets the char index for the _beginning_ of the last line
602 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
604 // Get number of characters in the last line. We'll add this to the character
605 // index for the last line, 1st position.
606 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
608 return (long)(charIndex
+ lineLength
);
611 // If the return values from and to are the same, there is no
613 void wxTextCtrl::GetSelection(long* from
, long* to
) const
619 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
621 *from
= charRange
.cpMin
;
622 *to
= charRange
.cpMax
;
627 DWORD dwStart
, dwEnd
;
628 WPARAM wParam
= (WPARAM
) &dwStart
; // receives starting position
629 LPARAM lParam
= (LPARAM
) &dwEnd
; // receives ending position
631 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
638 bool wxTextCtrl::IsEditable() const
640 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
642 return ((style
& ES_READONLY
) == 0);
645 // ----------------------------------------------------------------------------
647 // ----------------------------------------------------------------------------
649 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
651 HWND hWnd
= GetHwnd();
652 long fromChar
= from
;
655 // Set selection and remove it
657 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
658 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)TRUE
, (LPARAM
)value
.c_str());
660 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
661 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)0, (LPARAM
)value
.c_str());
665 void wxTextCtrl::Remove(long from
, long to
)
667 HWND hWnd
= GetHwnd();
668 long fromChar
= from
;
671 // Cut all selected text
673 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
674 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)TRUE
, (LPARAM
)"");
676 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
677 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)0, (LPARAM
)"");
681 void wxTextCtrl::SetSelection(long from
, long to
)
683 HWND hWnd
= GetHwnd();
684 long fromChar
= from
;
687 // if from and to are both -1, it means (in wxWindows) that all text should
688 // be selected. Translate into Windows convention
689 if ((from
== -1) && (to
== -1))
696 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
697 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
699 // WPARAM is 0: selection is scrolled into view
700 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
704 bool wxTextCtrl::LoadFile(const wxString
& file
)
706 if ( wxTextCtrlBase::LoadFile(file
) )
708 // update the size limit if needed
717 bool wxTextCtrl::IsModified() const
719 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
722 // Makes 'unmodified'
723 void wxTextCtrl::DiscardEdits()
725 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
728 int wxTextCtrl::GetNumberOfLines() const
730 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
733 long wxTextCtrl::XYToPosition(long x
, long y
) const
735 HWND hWnd
= GetHwnd();
737 // This gets the char index for the _beginning_ of this line
738 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
739 return (long)(x
+ charIndex
);
742 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
744 HWND hWnd
= GetHwnd();
746 // This gets the line number containing the character
751 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
754 #endif // wxUSE_RICHEDIT
755 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
763 // This gets the char index for the _beginning_ of this line
764 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
765 if ( charIndex
== -1 )
770 // The X position must therefore be the different between pos and charIndex
772 *x
= (long)(pos
- charIndex
);
779 void wxTextCtrl::ShowPosition(long pos
)
781 HWND hWnd
= GetHwnd();
783 // To scroll to a position, we pass the number of lines and characters
784 // to scroll *by*. This means that we need to:
785 // (1) Find the line position of the current line.
786 // (2) Find the line position of pos.
787 // (3) Scroll by (pos - current).
788 // For now, ignore the horizontal scrolling.
790 // Is this where scrolling is relative to - the line containing the caret?
791 // Or is the first visible line??? Try first visible line.
792 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
794 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
796 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
798 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
800 if (linesToScroll
!= 0)
801 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
804 int wxTextCtrl::GetLineLength(long lineNo
) const
806 long charIndex
= XYToPosition(0, lineNo
);
807 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
811 wxString
wxTextCtrl::GetLineText(long lineNo
) const
813 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
815 // there must be at least enough place for the length WORD in the
820 wxChar
*buf
= str
.GetWriteBuf(len
);
822 *(WORD
*)buf
= (WORD
)len
;
823 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
826 str
.UngetWriteBuf(len
);
831 void wxTextCtrl::SetMaxLength(unsigned long len
)
833 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
836 // ----------------------------------------------------------------------------
838 // ----------------------------------------------------------------------------
840 void wxTextCtrl::Undo()
844 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
848 void wxTextCtrl::Redo()
852 // Same as Undo, since Undo undoes the undo, i.e. a redo.
853 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
857 bool wxTextCtrl::CanUndo() const
859 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
862 bool wxTextCtrl::CanRedo() const
864 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
867 // ----------------------------------------------------------------------------
868 // implemenation details
869 // ----------------------------------------------------------------------------
871 void wxTextCtrl::Command(wxCommandEvent
& event
)
873 SetValue(event
.GetString());
874 ProcessCommand (event
);
877 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
879 // By default, load the first file into the text window.
880 if (event
.GetNumberOfFiles() > 0)
882 LoadFile(event
.GetFiles()[0]);
886 // ----------------------------------------------------------------------------
887 // kbd input processing
888 // ----------------------------------------------------------------------------
890 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
892 MSG
*msg
= (MSG
*)pMsg
;
894 // check for our special keys here: if we don't do it and the parent frame
895 // uses them as accelerators, they wouldn't work at all, so we disable
896 // usual preprocessing for them
897 if ( msg
->message
== WM_KEYDOWN
)
899 WORD vkey
= msg
->wParam
;
900 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
902 if ( vkey
== VK_BACK
)
907 if ( wxIsCtrlDown() )
921 else if ( wxIsShiftDown() )
923 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
929 return wxControl::MSWShouldPreProcessMessage(pMsg
);
932 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
934 switch ( event
.KeyCode() )
937 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
939 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
940 InitCommandEvent(event
);
941 event
.SetString(GetValue());
942 if ( GetEventHandler()->ProcessEvent(event
) )
945 //else: multiline controls need Enter for themselves
950 // always produce navigation event - even if we process TAB
951 // ourselves the fact that we got here means that the user code
952 // decided to skip processing of this TAB - probably to let it
953 // do its default job.
955 wxNavigationKeyEvent eventNav
;
956 eventNav
.SetDirection(!event
.ShiftDown());
957 eventNav
.SetWindowChange(event
.ControlDown());
958 eventNav
.SetEventObject(this);
960 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
966 // no, we didn't process it
970 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
977 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
980 event
.SetEventObject( this );
981 GetEventHandler()->ProcessEvent(event
);
987 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
988 InitCommandEvent(event
);
989 event
.SetString(GetValue());
990 ProcessCommand(event
);
995 // the text size limit has been hit - increase it
996 if ( !AdjustSpaceLimit() )
998 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
999 InitCommandEvent(event
);
1000 event
.SetString(GetValue());
1001 ProcessCommand(event
);
1005 // the other notification messages are not processed
1019 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1025 WXUINT
WXUNUSED(message
),
1026 WXWPARAM
WXUNUSED(wParam
),
1027 WXLPARAM
WXUNUSED(lParam
)
1034 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1035 return (WXHBRUSH
) hbrush
;
1037 #endif // wxUSE_CTL3D
1040 if (GetParent()->GetTransparentBackground())
1041 SetBkMode(hdc
, TRANSPARENT
);
1043 SetBkMode(hdc
, OPAQUE
);
1045 wxColour colBack
= GetBackgroundColour();
1047 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1048 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
1050 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1051 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1053 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1055 return (WXHBRUSH
)brush
->GetResourceHandle();
1058 // In WIN16, need to override normal erasing because
1059 // Ctl3D doesn't use the wxWindows background colour.
1061 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1063 wxColour
col(m_backgroundColour
);
1067 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1071 ::GetClientRect(GetHwnd(), &rect
);
1073 COLORREF ref
= PALETTERGB(col
.Red(),
1076 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1078 wxLogLastError(wxT("CreateSolidBrush"));
1080 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1082 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1084 ::FillRect(hdc
, &rect
, hBrush
);
1085 ::DeleteObject(hBrush
);
1086 ::SetMapMode(hdc
, mode
);
1091 bool wxTextCtrl::AdjustSpaceLimit()
1094 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1096 // HACK: we try to automatically extend the limit for the amount of text
1097 // to allow (interactively) entering more than 64Kb of text under
1098 // Win9x but we shouldn't reset the text limit which was previously
1099 // set explicitly with SetMaxLength()
1101 // we could solve this by storing the limit we set in wxTextCtrl but
1102 // to save space we prefer to simply test here the actual limit
1103 // value: we consider that SetMaxLength() can only be called for
1105 if ( limit
< 0x8000 )
1107 // we've got more text than limit set by SetMaxLength()
1111 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1114 limit
= len
+ 0x8000; // 32Kb
1119 // as a nice side effect, this also allows passing limit > 64Kb
1120 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1123 #endif // wxUSE_RICHEDIT
1125 if ( limit
> 0xffff )
1127 // this will set it to a platform-dependent maximum (much more
1128 // than 64Kb under NT)
1132 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1137 // we changed the limit
1141 bool wxTextCtrl::AcceptsFocus() const
1143 // we don't want focus if we can't be edited
1144 return IsEditable() && wxControl::AcceptsFocus();
1147 wxSize
wxTextCtrl::DoGetBestSize() const
1150 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1152 int wText
= DEFAULT_ITEM_WIDTH
;
1154 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1155 if ( m_windowStyle
& wxTE_MULTILINE
)
1157 hText
*= wxMax(GetNumberOfLines(), 5);
1159 //else: for single line control everything is ok
1161 return wxSize(wText
, hText
);
1164 // ----------------------------------------------------------------------------
1165 // standard handlers for standard edit menu events
1166 // ----------------------------------------------------------------------------
1168 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1173 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1178 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1183 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1188 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1193 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1195 event
.Enable( CanCut() );
1198 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1200 event
.Enable( CanCopy() );
1203 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1205 event
.Enable( CanPaste() );
1208 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1210 event
.Enable( CanUndo() );
1213 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1215 event
.Enable( CanRedo() );
1218 // the rest of the file only deals with the rich edit controls
1221 // ----------------------------------------------------------------------------
1222 // EN_LINK processing
1223 // ----------------------------------------------------------------------------
1225 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1227 NMHDR
*hdr
= (NMHDR
* )lParam
;
1228 if ( hdr
->code
== EN_LINK
)
1230 ENLINK
*enlink
= (ENLINK
*)hdr
;
1232 switch ( enlink
->msg
)
1235 // ok, so it is hardcoded - do we really nee to customize it?
1236 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1241 case WM_LBUTTONDOWN
:
1243 case WM_LBUTTONDBLCLK
:
1244 case WM_RBUTTONDOWN
:
1246 case WM_RBUTTONDBLCLK
:
1247 // send a mouse event
1249 static const wxEventType eventsMouse
[] =
1260 // the event ids are consecutive
1262 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1264 InitMouseEvent(evtMouse
,
1265 GET_X_LPARAM(enlink
->lParam
),
1266 GET_Y_LPARAM(enlink
->lParam
),
1269 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1271 enlink
->chrg
.cpMax
);
1273 InitCommandEvent(event
);
1275 *result
= ProcessCommand(event
);
1287 // ----------------------------------------------------------------------------
1288 // colour setting for the rich edit controls
1289 // ----------------------------------------------------------------------------
1291 // Watcom C++ doesn't define this
1293 #define SCF_ALL 0x0004
1296 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1298 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1300 // colour didn't really change
1306 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1307 // EM_SETBKGNDCOLOR additionally
1308 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1314 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1316 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1318 // colour didn't really change
1324 // change the colour of everything
1327 cf
.cbSize
= sizeof(cf
);
1328 cf
.dwMask
= CFM_COLOR
;
1329 cf
.crTextColor
= wxColourToRGB(colour
);
1330 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1336 // ----------------------------------------------------------------------------
1337 // styling support for rich edit controls
1338 // ----------------------------------------------------------------------------
1340 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1344 // can't do it with normal text control
1348 // the rich text control doesn't handle setting background colour, so don't
1349 // even try if it's the only thing we want to change
1350 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1351 !style
.HasFont() && !style
.HasTextColour() )
1353 // nothing to do: return TRUE if there was really nothing to do and
1354 // FALSE if we failed to set bg colour
1355 return !style
.HasBackgroundColour();
1358 // order the range if needed
1366 // we can only change the format of the selection, so select the range we
1367 // want and restore the old selection later
1368 long startOld
, endOld
;
1369 GetSelection(&startOld
, &endOld
);
1371 // but do we really have to change the selection?
1372 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1375 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
) start
, (LPARAM
) end
);
1377 // initialize CHARFORMAT struct
1384 cf
.cbSize
= sizeof(cf
);
1386 if ( style
.HasFont() )
1388 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1389 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1391 // fill in data from LOGFONT but recalculate lfHeight because we need
1392 // the real height in twips and not the negative number which
1393 // wxFillLogFont() returns (this is correct in general and works with
1394 // the Windows font mapper, but not here)
1396 wxFillLogFont(&lf
, &style
.GetFont());
1397 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1398 cf
.bCharSet
= lf
.lfCharSet
;
1399 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1400 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1402 // also deal with underline/italic/bold attributes: note that we must
1403 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1404 // style to allow clearing it
1407 cf
.dwEffects
|= CFE_ITALIC
;
1410 if ( lf
.lfWeight
== FW_BOLD
)
1412 cf
.dwEffects
|= CFE_BOLD
;
1415 if ( lf
.lfUnderline
)
1417 cf
.dwEffects
|= CFE_UNDERLINE
;
1420 // strikeout fonts are not supported by wxWindows
1423 if ( style
.HasTextColour() )
1425 cf
.dwMask
|= CFM_COLOR
;
1426 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1430 if ( wxRichEditModule::GetLoadedVersion() > 1 && style
.HasBackgroundColour() )
1432 cf
.dwMask
|= CFM_BACKCOLOR
;
1433 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1435 #endif // wxUSE_RICHEDIT2
1437 // do format the selection
1438 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1439 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1442 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1447 // restore the original selection
1448 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
)startOld
, (LPARAM
)endOld
);
1454 // ----------------------------------------------------------------------------
1456 // ----------------------------------------------------------------------------
1458 bool wxRichEditModule::OnInit()
1460 // don't do anything - we will load it when needed
1464 void wxRichEditModule::OnExit()
1468 FreeLibrary(ms_hRichEdit
);
1473 bool wxRichEditModule::Load(int version
)
1475 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1476 _T("incorrect richedit control version requested") );
1478 if ( version
<= ms_verRichEdit
)
1480 // we've already got this or better
1486 ::FreeLibrary(ms_hRichEdit
);
1489 // always try load riched20.dll first - like this we won't have to reload
1490 // it later if we're first asked for RE 1 and then for RE 2 or 3
1491 wxString dllname
= _T("riched20.dll");
1492 ms_hRichEdit
= ::LoadLibrary(dllname
);
1493 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1495 if ( !ms_hRichEdit
&& (version
== 1) )
1497 // fall back to RE 1
1498 dllname
= _T("riched32.dll");
1499 ms_hRichEdit
= ::LoadLibrary(dllname
);
1503 if ( !ms_hRichEdit
)
1505 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1507 ms_verRichEdit
= -1;
1515 #endif // wxUSE_RICHEDIT
1517 #endif // wxUSE_TEXTCTRL