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"
32 #include "wx/textctrl.h"
33 #include "wx/settings.h"
41 #include "wx/module.h"
44 #include "wx/clipbrd.h"
47 #include "wx/textfile.h"
51 #include "wx/msw/private.h"
55 #include <sys/types.h>
63 #if wxUSE_RICHEDIT && !defined(__GNUWIN32_OLD__)
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
73 // this module initializes RichEdit DLL if needed
74 class wxRichEditModule
: public wxModule
77 virtual bool OnInit();
78 virtual void OnExit();
80 // get the version currently loaded, -1 if none
81 static int GetLoadedVersion() { return ms_verRichEdit
; }
83 // load the richedit DLL of at least of required version
84 static bool Load(int version
= 1);
87 // the handle to richedit DLL and the version of the DLL loaded
88 static HINSTANCE ms_hRichEdit
;
90 // the DLL version loaded or -1 if none
91 static int ms_verRichEdit
;
93 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
96 HINSTANCE
wxRichEditModule::ms_hRichEdit
= (HINSTANCE
)NULL
;
97 int wxRichEditModule::ms_verRichEdit
= -1;
99 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
101 #endif // wxUSE_RICHEDIT
103 // ----------------------------------------------------------------------------
104 // event tables and other macros
105 // ----------------------------------------------------------------------------
107 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
109 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
110 EVT_CHAR(wxTextCtrl::OnChar
)
111 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
113 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
114 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
115 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
116 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
117 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
119 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
120 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
121 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
122 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
123 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
125 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
130 // ============================================================================
132 // ============================================================================
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxTextCtrl::wxTextCtrl()
145 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
146 const wxString
& value
,
150 const wxValidator
& validator
,
151 const wxString
& name
)
153 // base initialization
154 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
158 parent
->AddChild(this);
160 // translate wxWin style flags to MSW ones, checking for consistency while
162 long msStyle
= ES_LEFT
| WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
/* | WS_CLIPSIBLINGS */ ;
163 if ( m_windowStyle
& wxTE_MULTILINE
)
165 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
166 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
168 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
169 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
170 msStyle
|= WS_VSCROLL
;
171 m_windowStyle
|= wxTE_PROCESS_ENTER
;
174 msStyle
|= ES_AUTOHSCROLL
;
176 if (m_windowStyle
& wxHSCROLL
)
177 msStyle
|= (WS_HSCROLL
| ES_AUTOHSCROLL
);
179 if (m_windowStyle
& wxTE_READONLY
)
180 msStyle
|= ES_READONLY
;
182 if (m_windowStyle
& wxTE_PASSWORD
) // hidden input
183 msStyle
|= ES_PASSWORD
;
185 if (m_windowStyle
& wxTE_AUTO_SCROLL
)
186 msStyle
|= ES_AUTOHSCROLL
;
189 // we always want the characters and the arrows
190 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
192 // we may have several different cases:
193 // 1. normal case: both TAB and ENTER are used for dialog navigation
194 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
195 // control in the dialog
196 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
197 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
199 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
200 m_lDlgCode
|= DLGC_WANTMESSAGE
;
201 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
202 m_lDlgCode
|= DLGC_WANTTAB
;
204 // do create the control - either an EDIT or RICHEDIT
205 wxString windowClass
= wxT("EDIT");
208 if ( m_windowStyle
& wxTE_RICH
)
210 static bool s_errorGiven
= FALSE
; // MT-FIXME
212 // only give the error msg once if the DLL can't be loaded
215 // first try to load the RichEdit DLL (will do nothing if already
217 if ( !wxRichEditModule::Load() )
219 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
231 msStyle
|= ES_AUTOVSCROLL
;
234 int ver
= wxRichEditModule::GetLoadedVersion();
237 windowClass
= wxT("RICHEDIT");
241 #ifndef RICHEDIT_CLASS
242 wxString RICHEDIT_CLASS
;
243 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), ver
);
245 RICHEDIT_CLASS
+= _T('W');
247 RICHEDIT_CLASS
+= _T('A');
248 #endif // Unicode/ANSI
249 #endif // !RICHEDIT_CLASS
251 windowClass
= RICHEDIT_CLASS
;
257 #endif // wxUSE_RICHEDIT
260 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
262 // Even with extended styles, need to combine with WS_BORDER for them to
264 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
265 msStyle
|= WS_BORDER
;
267 // NB: don't use pos and size as CreateWindowEx arguments because they
268 // might be -1 in which case we should use the default values (and
269 // SetSize called below takes care of it)
270 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
,
280 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create text ctrl") );
285 Ctl3dSubclassCtl(GetHwnd());
293 // Have to enable events
294 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0,
295 ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
);
299 SubclassWin(GetHWND());
301 // set font, position, size and initial value
302 wxFont
& fontParent
= parent
->GetFont();
303 if ( fontParent
.Ok() )
309 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
312 // Causes a crash for Symantec C++ and WIN32 for some reason
313 #if !(defined(__SC__) && defined(__WIN32__))
314 if ( !value
.IsEmpty() )
323 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
328 // Make sure the window style (etc.) reflects the HWND style (roughly)
329 void wxTextCtrl::AdoptAttributesFromHWND()
331 wxWindow::AdoptAttributesFromHWND();
333 HWND hWnd
= GetHwnd();
334 long style
= GetWindowLong(hWnd
, GWL_STYLE
);
336 // retrieve the style to see whether this is an edit or richedit ctrl
340 GetClassName(hWnd
, buf
, WXSIZEOF(buf
));
342 if ( wxStricmp(buf
, wxT("EDIT")) == 0 )
346 #endif // wxUSE_RICHEDIT
348 if (style
& ES_MULTILINE
)
349 m_windowStyle
|= wxTE_MULTILINE
;
350 if (style
& ES_PASSWORD
)
351 m_windowStyle
|= wxTE_PASSWORD
;
352 if (style
& ES_READONLY
)
353 m_windowStyle
|= wxTE_READONLY
;
354 if (style
& ES_WANTRETURN
)
355 m_windowStyle
|= wxTE_PROCESS_ENTER
;
358 void wxTextCtrl::SetupColours()
360 wxColour bkgndColour
;
361 // if (IsEditable() || (m_windowStyle & wxTE_MULTILINE))
362 bkgndColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
364 // bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
366 SetBackgroundColour(bkgndColour
);
367 SetForegroundColour(GetParent()->GetForegroundColour());
370 // ----------------------------------------------------------------------------
371 // set/get the controls text
372 // ----------------------------------------------------------------------------
374 wxString
wxTextCtrl::GetValue() const
376 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
377 // retrieving more than 64Kb under Win9x
383 int len
= GetWindowTextLength(GetHwnd());
386 // alloc one extra WORD as needed by the control
387 wxChar
*p
= str
.GetWriteBuf(++len
);
390 textRange
.chrg
.cpMin
= 0;
391 textRange
.chrg
.cpMax
= -1;
392 textRange
.lpstrText
= p
;
394 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
396 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
397 // the newlines which is neither Unix nor Windows style (Win95 with
398 // riched20.dll shows this behaviour) - convert it to something
402 if ( *p
== _T('\r') )
408 //else: no text at all, leave the string empty
412 #endif // wxUSE_RICHEDIT
414 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
415 // same one as above for consitency
416 wxString str
= wxGetWindowText(GetHWND());
418 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
421 void wxTextCtrl::SetValue(const wxString
& value
)
423 // if the text is long enough, it's faster to just set it instead of first
424 // comparing it with the old one (chances are that it will be different
425 // anyhow, this comparison is there to avoid flicker for small single-line
426 // edit controls mostly)
427 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
429 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
431 SetWindowText(GetHwnd(), valueDos
.c_str());
433 // for compatibility with the GTK and because it is more logical, we
434 // move the cursor to the end of the text after SetValue()
436 // GRG, Jun/2000: Changed this back after a lot of discussion
437 // in the lists. wxWindows 2.2 will have a set of flags to
438 // customize this behaviour.
439 //SetInsertionPointEnd();
445 void wxTextCtrl::WriteText(const wxString
& value
)
447 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
449 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
454 void wxTextCtrl::AppendText(const wxString
& text
)
456 SetInsertionPointEnd();
460 void wxTextCtrl::Clear()
462 SetWindowText(GetHwnd(), wxT(""));
465 // ----------------------------------------------------------------------------
466 // Clipboard operations
467 // ----------------------------------------------------------------------------
469 void wxTextCtrl::Copy()
473 HWND hWnd
= GetHwnd();
474 SendMessage(hWnd
, WM_COPY
, 0, 0L);
478 void wxTextCtrl::Cut()
482 HWND hWnd
= GetHwnd();
483 SendMessage(hWnd
, WM_CUT
, 0, 0L);
487 void wxTextCtrl::Paste()
491 HWND hWnd
= GetHwnd();
492 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
496 bool wxTextCtrl::CanCopy() const
498 // Can copy if there's a selection
500 GetSelection(& from
, & to
);
501 return (from
!= to
) ;
504 bool wxTextCtrl::CanCut() const
506 // Can cut if there's a selection
508 GetSelection(& from
, & to
);
509 return (from
!= to
) && (IsEditable());
512 bool wxTextCtrl::CanPaste() const
517 int dataFormat
= 0; // 0 == any format
518 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
524 // Standard edit control: check for straight text on clipboard
525 bool isTextAvailable
= FALSE
;
526 if ( ::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
528 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
532 return isTextAvailable
;
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 void wxTextCtrl::SetEditable(bool editable
)
541 HWND hWnd
= GetHwnd();
542 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
545 void wxTextCtrl::SetInsertionPoint(long pos
)
547 HWND hWnd
= GetHwnd();
555 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
556 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
559 #endif // wxUSE_RICHEDIT
561 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
562 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
565 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
568 static const wxChar
*nothing
= _T("");
569 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
572 void wxTextCtrl::SetInsertionPointEnd()
574 long pos
= GetLastPosition();
575 SetInsertionPoint(pos
);
578 long wxTextCtrl::GetInsertionPoint() const
586 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
591 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
595 long wxTextCtrl::GetLastPosition() const
597 HWND hWnd
= GetHwnd();
599 // Will always return a number > 0 (according to docs)
600 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
602 // This gets the char index for the _beginning_ of the last line
603 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
605 // Get number of characters in the last line. We'll add this to the character
606 // index for the last line, 1st position.
607 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
609 return (long)(charIndex
+ lineLength
);
612 // If the return values from and to are the same, there is no
614 void wxTextCtrl::GetSelection(long* from
, long* to
) const
620 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
622 *from
= charRange
.cpMin
;
623 *to
= charRange
.cpMax
;
628 DWORD dwStart
, dwEnd
;
629 WPARAM wParam
= (WPARAM
) (DWORD
*) & dwStart
; // receives starting position
630 LPARAM lParam
= (LPARAM
) (DWORD
*) & dwEnd
; // receives ending position
632 ::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
)
652 HWND hWnd
= GetHwnd();
653 long fromChar
= from
;
656 // Set selection and remove it
658 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
660 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
662 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
664 // Now replace with 'value', by pasting.
665 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const wxChar
*)value
, 0, 0);
667 // Paste into edit control
668 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
670 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
674 void wxTextCtrl::Remove(long from
, long to
)
676 HWND hWnd
= GetHwnd();
677 long fromChar
= from
;
680 // Cut all selected text
682 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
684 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
686 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
689 void wxTextCtrl::SetSelection(long from
, long to
)
691 HWND hWnd
= GetHwnd();
692 long fromChar
= from
;
695 // if from and to are both -1, it means (in wxWindows) that all text should
696 // be selected. Translate into Windows convention
697 if ((from
== -1) && (to
== -1))
704 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
705 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
707 // WPARAM is 0: selection is scrolled into view
708 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
712 bool wxTextCtrl::LoadFile(const wxString
& file
)
714 if ( wxTextCtrlBase::LoadFile(file
) )
716 // update the size limit if needed
725 bool wxTextCtrl::IsModified() const
727 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
730 // Makes 'unmodified'
731 void wxTextCtrl::DiscardEdits()
733 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
736 int wxTextCtrl::GetNumberOfLines() const
738 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
741 long wxTextCtrl::XYToPosition(long x
, long y
) const
743 HWND hWnd
= GetHwnd();
745 // This gets the char index for the _beginning_ of this line
746 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
747 return (long)(x
+ charIndex
);
750 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
752 HWND hWnd
= GetHwnd();
754 // This gets the line number containing the character
759 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
762 #endif // wxUSE_RICHEDIT
763 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
771 // This gets the char index for the _beginning_ of this line
772 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
773 if ( charIndex
== -1 )
778 // The X position must therefore be the different between pos and charIndex
780 *x
= (long)(pos
- charIndex
);
787 void wxTextCtrl::ShowPosition(long pos
)
789 HWND hWnd
= GetHwnd();
791 // To scroll to a position, we pass the number of lines and characters
792 // to scroll *by*. This means that we need to:
793 // (1) Find the line position of the current line.
794 // (2) Find the line position of pos.
795 // (3) Scroll by (pos - current).
796 // For now, ignore the horizontal scrolling.
798 // Is this where scrolling is relative to - the line containing the caret?
799 // Or is the first visible line??? Try first visible line.
800 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
802 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
804 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
806 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
808 if (linesToScroll
!= 0)
809 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
812 int wxTextCtrl::GetLineLength(long lineNo
) const
814 long charIndex
= XYToPosition(0, lineNo
);
815 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
819 wxString
wxTextCtrl::GetLineText(long lineNo
) const
821 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
823 // there must be at least enough place for the length WORD in the
828 wxChar
*buf
= str
.GetWriteBuf(len
);
831 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
834 str
.UngetWriteBuf(len
);
839 // ----------------------------------------------------------------------------
841 // ----------------------------------------------------------------------------
843 void wxTextCtrl::Undo()
847 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
851 void wxTextCtrl::Redo()
855 // Same as Undo, since Undo undoes the undo, i.e. a redo.
856 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
860 bool wxTextCtrl::CanUndo() const
862 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
865 bool wxTextCtrl::CanRedo() const
867 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
870 // ----------------------------------------------------------------------------
871 // implemenation details
872 // ----------------------------------------------------------------------------
874 void wxTextCtrl::Command(wxCommandEvent
& event
)
876 SetValue(event
.GetString());
877 ProcessCommand (event
);
880 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
882 // By default, load the first file into the text window.
883 if (event
.GetNumberOfFiles() > 0)
885 LoadFile(event
.GetFiles()[0]);
889 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
891 switch ( event
.KeyCode() )
894 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
896 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
897 InitCommandEvent(event
);
898 event
.SetString(GetValue());
899 if ( GetEventHandler()->ProcessEvent(event
) )
902 //else: multiline controls need Enter for themselves
907 // always produce navigation event - even if we process TAB
908 // ourselves the fact that we got here means that the user code
909 // decided to skip processing of this TAB - probably to let it
910 // do its default job.
912 wxNavigationKeyEvent eventNav
;
913 eventNav
.SetDirection(!event
.ShiftDown());
914 eventNav
.SetWindowChange(event
.ControlDown());
915 eventNav
.SetEventObject(this);
917 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
923 // no, we didn't process it
927 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
934 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
937 event
.SetEventObject( this );
938 GetEventHandler()->ProcessEvent(event
);
944 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
945 InitCommandEvent(event
);
946 event
.SetString(GetValue());
947 ProcessCommand(event
);
952 // the text size limit has been hit - increase it
956 // the other notification messages are not processed
970 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
978 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
979 return (WXHBRUSH
) hbrush
;
981 #endif // wxUSE_CTL3D
984 if (GetParent()->GetTransparentBackground())
985 SetBkMode(hdc
, TRANSPARENT
);
987 SetBkMode(hdc
, OPAQUE
);
989 wxColour colBack
= GetBackgroundColour();
991 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
992 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
994 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
995 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
997 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
999 return (WXHBRUSH
)brush
->GetResourceHandle();
1002 // In WIN16, need to override normal erasing because
1003 // Ctl3D doesn't use the wxWindows background colour.
1005 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1007 wxColour
col(m_backgroundColour
);
1011 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1015 ::GetClientRect(GetHwnd(), &rect
);
1017 COLORREF ref
= PALETTERGB(col
.Red(),
1020 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1022 wxLogLastError(wxT("CreateSolidBrush"));
1024 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1026 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1028 ::FillRect(hdc
, &rect
, hBrush
);
1029 ::DeleteObject(hBrush
);
1030 ::SetMapMode(hdc
, mode
);
1035 void wxTextCtrl::AdjustSpaceLimit()
1038 unsigned int len
= ::GetWindowTextLength(GetHwnd()),
1039 limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1042 limit
= len
+ 0x8000; // 32Kb
1047 // as a nice side effect, this also allows passing limit > 64Kb
1048 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1051 #endif // wxUSE_RICHEDIT
1053 if ( limit
> 0xffff )
1055 // this will set it to a platform-dependent maximum (much more
1056 // than 64Kb under NT)
1060 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1066 bool wxTextCtrl::AcceptsFocus() const
1068 // we don't want focus if we can't be edited
1069 return IsEditable() && wxControl::AcceptsFocus();
1072 wxSize
wxTextCtrl::DoGetBestSize() const
1075 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1077 int wText
= DEFAULT_ITEM_WIDTH
;
1079 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1080 if ( m_windowStyle
& wxTE_MULTILINE
)
1082 hText
*= wxMax(GetNumberOfLines(), 5);
1084 //else: for single line control everything is ok
1086 return wxSize(wText
, hText
);
1089 // ----------------------------------------------------------------------------
1090 // standard handlers for standard edit menu events
1091 // ----------------------------------------------------------------------------
1093 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1098 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1103 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1108 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1113 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1118 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1120 event
.Enable( CanCut() );
1123 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1125 event
.Enable( CanCopy() );
1128 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1130 event
.Enable( CanPaste() );
1133 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1135 event
.Enable( CanUndo() );
1138 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1140 event
.Enable( CanRedo() );
1143 // ----------------------------------------------------------------------------
1144 // colour setting for the rich edit controls
1145 // ----------------------------------------------------------------------------
1149 // Watcom C++ doesn't define this
1151 #define SCF_ALL 0x0004
1154 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1156 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1158 // colour didn't really change
1164 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1165 // EM_SETBKGNDCOLOR additionally
1166 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1172 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1174 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1176 // colour didn't really change
1182 // change the colour of everything
1185 cf
.cbSize
= sizeof(cf
);
1186 cf
.dwMask
= CFM_COLOR
;
1187 cf
.crTextColor
= wxColourToRGB(colour
);
1188 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1194 #endif // wxUSE_RICHEDIT
1196 // ----------------------------------------------------------------------------
1198 // ----------------------------------------------------------------------------
1202 bool wxRichEditModule::OnInit()
1204 // don't do anything - we will load it when needed
1208 void wxRichEditModule::OnExit()
1212 FreeLibrary(ms_hRichEdit
);
1217 bool wxRichEditModule::Load(int version
)
1219 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1220 _T("incorrect richedit control version requested") );
1222 if ( version
<= ms_verRichEdit
)
1224 // we've already got this or better
1230 ::FreeLibrary(ms_hRichEdit
);
1233 // always try load riched20.dll first - like this we won't have to reload
1234 // it later if we're first asked for RE 1 and then for RE 2 or 3
1235 wxString dllname
= _T("riched20.dll");
1236 ms_hRichEdit
= ::LoadLibrary(dllname
);
1237 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1239 if ( !ms_hRichEdit
&& (version
== 1) )
1241 // fall back to RE 1
1242 dllname
= _T("riched32.dll");
1243 ms_hRichEdit
= ::LoadLibrary(dllname
);
1247 if ( !ms_hRichEdit
)
1249 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1251 ms_verRichEdit
= -1;
1259 #endif // wxUSE_RICHEDIT