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
;
164 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
165 msStyle
|= WS_CLIPSIBLINGS
;
167 if ( m_windowStyle
& wxTE_MULTILINE
)
169 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
170 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
172 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
173 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
174 msStyle
|= WS_VSCROLL
;
175 m_windowStyle
|= wxTE_PROCESS_ENTER
;
178 msStyle
|= ES_AUTOHSCROLL
;
180 if (m_windowStyle
& wxHSCROLL
)
181 msStyle
|= (WS_HSCROLL
| ES_AUTOHSCROLL
);
183 if (m_windowStyle
& wxTE_READONLY
)
184 msStyle
|= ES_READONLY
;
186 if (m_windowStyle
& wxTE_PASSWORD
) // hidden input
187 msStyle
|= ES_PASSWORD
;
189 if (m_windowStyle
& wxTE_AUTO_SCROLL
)
190 msStyle
|= ES_AUTOHSCROLL
;
193 // we always want the characters and the arrows
194 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
196 // we may have several different cases:
197 // 1. normal case: both TAB and ENTER are used for dialog navigation
198 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
199 // control in the dialog
200 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
201 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
203 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
204 m_lDlgCode
|= DLGC_WANTMESSAGE
;
205 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
206 m_lDlgCode
|= DLGC_WANTTAB
;
208 // do create the control - either an EDIT or RICHEDIT
209 wxString windowClass
= wxT("EDIT");
212 if ( m_windowStyle
& wxTE_RICH
)
214 static bool s_errorGiven
= FALSE
; // MT-FIXME
216 // only give the error msg once if the DLL can't be loaded
219 // first try to load the RichEdit DLL (will do nothing if already
221 if ( !wxRichEditModule::Load() )
223 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
235 msStyle
|= ES_AUTOVSCROLL
;
238 int ver
= wxRichEditModule::GetLoadedVersion();
241 windowClass
= wxT("RICHEDIT");
245 #ifndef RICHEDIT_CLASS
246 wxString RICHEDIT_CLASS
;
247 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), ver
);
249 RICHEDIT_CLASS
+= _T('W');
251 RICHEDIT_CLASS
+= _T('A');
252 #endif // Unicode/ANSI
253 #endif // !RICHEDIT_CLASS
255 windowClass
= RICHEDIT_CLASS
;
261 #endif // wxUSE_RICHEDIT
264 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
266 // Even with extended styles, need to combine with WS_BORDER for them to
268 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
269 msStyle
|= WS_BORDER
;
271 // NB: don't use pos and size as CreateWindowEx arguments because they
272 // might be -1 in which case we should use the default values (and
273 // SetSize called below takes care of it)
274 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
,
284 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create text ctrl") );
289 Ctl3dSubclassCtl(GetHwnd());
297 // Have to enable events
298 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0,
299 ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
);
303 SubclassWin(GetHWND());
305 // set font, position, size and initial value
306 wxFont
& fontParent
= parent
->GetFont();
307 if ( fontParent
.Ok() )
313 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
316 // Causes a crash for Symantec C++ and WIN32 for some reason
317 #if !(defined(__SC__) && defined(__WIN32__))
318 if ( !value
.IsEmpty() )
327 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
332 // Make sure the window style (etc.) reflects the HWND style (roughly)
333 void wxTextCtrl::AdoptAttributesFromHWND()
335 wxWindow::AdoptAttributesFromHWND();
337 HWND hWnd
= GetHwnd();
338 long style
= GetWindowLong(hWnd
, GWL_STYLE
);
340 // retrieve the style to see whether this is an edit or richedit ctrl
344 GetClassName(hWnd
, buf
, WXSIZEOF(buf
));
346 if ( wxStricmp(buf
, wxT("EDIT")) == 0 )
350 #endif // wxUSE_RICHEDIT
352 if (style
& ES_MULTILINE
)
353 m_windowStyle
|= wxTE_MULTILINE
;
354 if (style
& ES_PASSWORD
)
355 m_windowStyle
|= wxTE_PASSWORD
;
356 if (style
& ES_READONLY
)
357 m_windowStyle
|= wxTE_READONLY
;
358 if (style
& ES_WANTRETURN
)
359 m_windowStyle
|= wxTE_PROCESS_ENTER
;
362 void wxTextCtrl::SetupColours()
364 wxColour bkgndColour
;
365 // if (IsEditable() || (m_windowStyle & wxTE_MULTILINE))
366 bkgndColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
368 // bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
370 SetBackgroundColour(bkgndColour
);
371 SetForegroundColour(GetParent()->GetForegroundColour());
374 // ----------------------------------------------------------------------------
375 // set/get the controls text
376 // ----------------------------------------------------------------------------
378 wxString
wxTextCtrl::GetValue() const
380 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
381 // retrieving more than 64Kb under Win9x
387 int len
= GetWindowTextLength(GetHwnd());
390 // alloc one extra WORD as needed by the control
391 wxChar
*p
= str
.GetWriteBuf(++len
);
394 textRange
.chrg
.cpMin
= 0;
395 textRange
.chrg
.cpMax
= -1;
396 textRange
.lpstrText
= p
;
398 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
400 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
401 // the newlines which is neither Unix nor Windows style (Win95 with
402 // riched20.dll shows this behaviour) - convert it to something
406 if ( *p
== _T('\r') )
412 //else: no text at all, leave the string empty
416 #endif // wxUSE_RICHEDIT
418 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
419 // same one as above for consitency
420 wxString str
= wxGetWindowText(GetHWND());
422 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
425 void wxTextCtrl::SetValue(const wxString
& value
)
427 // if the text is long enough, it's faster to just set it instead of first
428 // comparing it with the old one (chances are that it will be different
429 // anyhow, this comparison is there to avoid flicker for small single-line
430 // edit controls mostly)
431 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
433 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
435 SetWindowText(GetHwnd(), valueDos
.c_str());
437 // for compatibility with the GTK and because it is more logical, we
438 // move the cursor to the end of the text after SetValue()
440 // GRG, Jun/2000: Changed this back after a lot of discussion
441 // in the lists. wxWindows 2.2 will have a set of flags to
442 // customize this behaviour.
443 //SetInsertionPointEnd();
449 void wxTextCtrl::WriteText(const wxString
& value
)
451 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
453 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
458 void wxTextCtrl::AppendText(const wxString
& text
)
460 SetInsertionPointEnd();
464 void wxTextCtrl::Clear()
466 SetWindowText(GetHwnd(), wxT(""));
469 // ----------------------------------------------------------------------------
470 // Clipboard operations
471 // ----------------------------------------------------------------------------
473 void wxTextCtrl::Copy()
477 HWND hWnd
= GetHwnd();
478 SendMessage(hWnd
, WM_COPY
, 0, 0L);
482 void wxTextCtrl::Cut()
486 HWND hWnd
= GetHwnd();
487 SendMessage(hWnd
, WM_CUT
, 0, 0L);
491 void wxTextCtrl::Paste()
495 HWND hWnd
= GetHwnd();
496 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
500 bool wxTextCtrl::CanCopy() const
502 // Can copy if there's a selection
504 GetSelection(& from
, & to
);
505 return (from
!= to
) ;
508 bool wxTextCtrl::CanCut() const
510 // Can cut if there's a selection
512 GetSelection(& from
, & to
);
513 return (from
!= to
) && (IsEditable());
516 bool wxTextCtrl::CanPaste() const
521 int dataFormat
= 0; // 0 == any format
522 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
528 // Standard edit control: check for straight text on clipboard
529 bool isTextAvailable
= FALSE
;
530 if ( ::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
532 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
536 return isTextAvailable
;
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 void wxTextCtrl::SetEditable(bool editable
)
545 HWND hWnd
= GetHwnd();
546 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
549 void wxTextCtrl::SetInsertionPoint(long pos
)
551 HWND hWnd
= GetHwnd();
559 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
560 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
563 #endif // wxUSE_RICHEDIT
565 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
566 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
569 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
572 static const wxChar
*nothing
= _T("");
573 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
576 void wxTextCtrl::SetInsertionPointEnd()
578 long pos
= GetLastPosition();
579 SetInsertionPoint(pos
);
582 long wxTextCtrl::GetInsertionPoint() const
590 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
595 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
599 long wxTextCtrl::GetLastPosition() const
601 HWND hWnd
= GetHwnd();
603 // Will always return a number > 0 (according to docs)
604 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
606 // This gets the char index for the _beginning_ of the last line
607 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
609 // Get number of characters in the last line. We'll add this to the character
610 // index for the last line, 1st position.
611 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
613 return (long)(charIndex
+ lineLength
);
616 // If the return values from and to are the same, there is no
618 void wxTextCtrl::GetSelection(long* from
, long* to
) const
624 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
626 *from
= charRange
.cpMin
;
627 *to
= charRange
.cpMax
;
632 DWORD dwStart
, dwEnd
;
633 WPARAM wParam
= (WPARAM
) (DWORD
*) & dwStart
; // receives starting position
634 LPARAM lParam
= (LPARAM
) (DWORD
*) & dwEnd
; // receives ending position
636 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
642 bool wxTextCtrl::IsEditable() const
644 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
646 return ((style
& ES_READONLY
) == 0);
649 // ----------------------------------------------------------------------------
651 // ----------------------------------------------------------------------------
653 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
656 HWND hWnd
= GetHwnd();
657 long fromChar
= from
;
660 // Set selection and remove it
662 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
664 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
666 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
668 // Now replace with 'value', by pasting.
669 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const wxChar
*)value
, 0, 0);
671 // Paste into edit control
672 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
674 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
678 void wxTextCtrl::Remove(long from
, long to
)
680 HWND hWnd
= GetHwnd();
681 long fromChar
= from
;
684 // Cut all selected text
686 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
688 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
690 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
693 void wxTextCtrl::SetSelection(long from
, long to
)
695 HWND hWnd
= GetHwnd();
696 long fromChar
= from
;
699 // if from and to are both -1, it means (in wxWindows) that all text should
700 // be selected. Translate into Windows convention
701 if ((from
== -1) && (to
== -1))
708 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
709 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
711 // WPARAM is 0: selection is scrolled into view
712 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
716 bool wxTextCtrl::LoadFile(const wxString
& file
)
718 if ( wxTextCtrlBase::LoadFile(file
) )
720 // update the size limit if needed
729 bool wxTextCtrl::IsModified() const
731 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
734 // Makes 'unmodified'
735 void wxTextCtrl::DiscardEdits()
737 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
740 int wxTextCtrl::GetNumberOfLines() const
742 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
745 long wxTextCtrl::XYToPosition(long x
, long y
) const
747 HWND hWnd
= GetHwnd();
749 // This gets the char index for the _beginning_ of this line
750 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
751 return (long)(x
+ charIndex
);
754 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
756 HWND hWnd
= GetHwnd();
758 // This gets the line number containing the character
763 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
766 #endif // wxUSE_RICHEDIT
767 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
775 // This gets the char index for the _beginning_ of this line
776 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
777 if ( charIndex
== -1 )
782 // The X position must therefore be the different between pos and charIndex
784 *x
= (long)(pos
- charIndex
);
791 void wxTextCtrl::ShowPosition(long pos
)
793 HWND hWnd
= GetHwnd();
795 // To scroll to a position, we pass the number of lines and characters
796 // to scroll *by*. This means that we need to:
797 // (1) Find the line position of the current line.
798 // (2) Find the line position of pos.
799 // (3) Scroll by (pos - current).
800 // For now, ignore the horizontal scrolling.
802 // Is this where scrolling is relative to - the line containing the caret?
803 // Or is the first visible line??? Try first visible line.
804 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
806 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
808 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
810 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
812 if (linesToScroll
!= 0)
813 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
816 int wxTextCtrl::GetLineLength(long lineNo
) const
818 long charIndex
= XYToPosition(0, lineNo
);
819 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
823 wxString
wxTextCtrl::GetLineText(long lineNo
) const
825 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
827 // there must be at least enough place for the length WORD in the
832 wxChar
*buf
= str
.GetWriteBuf(len
);
834 *(WORD
*)buf
= (WORD
)len
;
835 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
838 str
.UngetWriteBuf(len
);
843 // ----------------------------------------------------------------------------
845 // ----------------------------------------------------------------------------
847 void wxTextCtrl::Undo()
851 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
855 void wxTextCtrl::Redo()
859 // Same as Undo, since Undo undoes the undo, i.e. a redo.
860 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
864 bool wxTextCtrl::CanUndo() const
866 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
869 bool wxTextCtrl::CanRedo() const
871 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
874 // ----------------------------------------------------------------------------
875 // implemenation details
876 // ----------------------------------------------------------------------------
878 void wxTextCtrl::Command(wxCommandEvent
& event
)
880 SetValue(event
.GetString());
881 ProcessCommand (event
);
884 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
886 // By default, load the first file into the text window.
887 if (event
.GetNumberOfFiles() > 0)
889 LoadFile(event
.GetFiles()[0]);
893 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
895 switch ( event
.KeyCode() )
898 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
900 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
901 InitCommandEvent(event
);
902 event
.SetString(GetValue());
903 if ( GetEventHandler()->ProcessEvent(event
) )
906 //else: multiline controls need Enter for themselves
911 // always produce navigation event - even if we process TAB
912 // ourselves the fact that we got here means that the user code
913 // decided to skip processing of this TAB - probably to let it
914 // do its default job.
916 wxNavigationKeyEvent eventNav
;
917 eventNav
.SetDirection(!event
.ShiftDown());
918 eventNav
.SetWindowChange(event
.ControlDown());
919 eventNav
.SetEventObject(this);
921 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
927 // no, we didn't process it
931 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
938 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
941 event
.SetEventObject( this );
942 GetEventHandler()->ProcessEvent(event
);
948 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
949 InitCommandEvent(event
);
950 event
.SetString(GetValue());
951 ProcessCommand(event
);
956 // the text size limit has been hit - increase it
960 // the other notification messages are not processed
974 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
980 WXUINT
WXUNUSED(message
),
981 WXWPARAM
WXUNUSED(wParam
),
982 WXLPARAM
WXUNUSED(lParam
)
989 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
990 return (WXHBRUSH
) hbrush
;
992 #endif // wxUSE_CTL3D
995 if (GetParent()->GetTransparentBackground())
996 SetBkMode(hdc
, TRANSPARENT
);
998 SetBkMode(hdc
, OPAQUE
);
1000 wxColour colBack
= GetBackgroundColour();
1002 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1003 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
1005 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1006 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1008 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1010 return (WXHBRUSH
)brush
->GetResourceHandle();
1013 // In WIN16, need to override normal erasing because
1014 // Ctl3D doesn't use the wxWindows background colour.
1016 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1018 wxColour
col(m_backgroundColour
);
1022 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1026 ::GetClientRect(GetHwnd(), &rect
);
1028 COLORREF ref
= PALETTERGB(col
.Red(),
1031 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1033 wxLogLastError(wxT("CreateSolidBrush"));
1035 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1037 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1039 ::FillRect(hdc
, &rect
, hBrush
);
1040 ::DeleteObject(hBrush
);
1041 ::SetMapMode(hdc
, mode
);
1046 void wxTextCtrl::AdjustSpaceLimit()
1049 unsigned int len
= ::GetWindowTextLength(GetHwnd()),
1050 limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1053 limit
= len
+ 0x8000; // 32Kb
1058 // as a nice side effect, this also allows passing limit > 64Kb
1059 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1062 #endif // wxUSE_RICHEDIT
1064 if ( limit
> 0xffff )
1066 // this will set it to a platform-dependent maximum (much more
1067 // than 64Kb under NT)
1071 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1077 bool wxTextCtrl::AcceptsFocus() const
1079 // we don't want focus if we can't be edited
1080 return IsEditable() && wxControl::AcceptsFocus();
1083 wxSize
wxTextCtrl::DoGetBestSize() const
1086 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1088 int wText
= DEFAULT_ITEM_WIDTH
;
1090 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1091 if ( m_windowStyle
& wxTE_MULTILINE
)
1093 hText
*= wxMax(GetNumberOfLines(), 5);
1095 //else: for single line control everything is ok
1097 return wxSize(wText
, hText
);
1100 // ----------------------------------------------------------------------------
1101 // standard handlers for standard edit menu events
1102 // ----------------------------------------------------------------------------
1104 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1109 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1114 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1119 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1124 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1129 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1131 event
.Enable( CanCut() );
1134 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1136 event
.Enable( CanCopy() );
1139 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1141 event
.Enable( CanPaste() );
1144 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1146 event
.Enable( CanUndo() );
1149 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1151 event
.Enable( CanRedo() );
1154 // ----------------------------------------------------------------------------
1155 // colour setting for the rich edit controls
1156 // ----------------------------------------------------------------------------
1160 // Watcom C++ doesn't define this
1162 #define SCF_ALL 0x0004
1165 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1167 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1169 // colour didn't really change
1175 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1176 // EM_SETBKGNDCOLOR additionally
1177 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1183 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1185 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1187 // colour didn't really change
1193 // change the colour of everything
1196 cf
.cbSize
= sizeof(cf
);
1197 cf
.dwMask
= CFM_COLOR
;
1198 cf
.crTextColor
= wxColourToRGB(colour
);
1199 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1205 #endif // wxUSE_RICHEDIT
1207 // ----------------------------------------------------------------------------
1209 // ----------------------------------------------------------------------------
1213 bool wxRichEditModule::OnInit()
1215 // don't do anything - we will load it when needed
1219 void wxRichEditModule::OnExit()
1223 FreeLibrary(ms_hRichEdit
);
1228 bool wxRichEditModule::Load(int version
)
1230 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1231 _T("incorrect richedit control version requested") );
1233 if ( version
<= ms_verRichEdit
)
1235 // we've already got this or better
1241 ::FreeLibrary(ms_hRichEdit
);
1244 // always try load riched20.dll first - like this we won't have to reload
1245 // it later if we're first asked for RE 1 and then for RE 2 or 3
1246 wxString dllname
= _T("riched20.dll");
1247 ms_hRichEdit
= ::LoadLibrary(dllname
);
1248 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1250 if ( !ms_hRichEdit
&& (version
== 1) )
1252 // fall back to RE 1
1253 dllname
= _T("riched32.dll");
1254 ms_hRichEdit
= ::LoadLibrary(dllname
);
1258 if ( !ms_hRichEdit
)
1260 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1262 ms_verRichEdit
= -1;
1270 #endif // wxUSE_RICHEDIT