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
)
129 // ============================================================================
131 // ============================================================================
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 wxTextCtrl::wxTextCtrl()
144 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
145 const wxString
& value
,
149 const wxValidator
& validator
,
150 const wxString
& name
)
152 // base initialization
153 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
157 parent
->AddChild(this);
159 // translate wxWin style flags to MSW ones, checking for consistency while
161 long msStyle
= ES_LEFT
| WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
163 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
164 msStyle
|= WS_CLIPSIBLINGS
;
166 if ( m_windowStyle
& wxTE_MULTILINE
)
168 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
169 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
171 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
172 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
173 msStyle
|= WS_VSCROLL
;
174 m_windowStyle
|= wxTE_PROCESS_ENTER
;
177 msStyle
|= ES_AUTOHSCROLL
;
179 if (m_windowStyle
& wxHSCROLL
)
180 msStyle
|= (WS_HSCROLL
| ES_AUTOHSCROLL
);
182 if (m_windowStyle
& wxTE_READONLY
)
183 msStyle
|= ES_READONLY
;
185 if (m_windowStyle
& wxTE_PASSWORD
) // hidden input
186 msStyle
|= ES_PASSWORD
;
188 if (m_windowStyle
& wxTE_AUTO_SCROLL
)
189 msStyle
|= ES_AUTOHSCROLL
;
192 // we always want the characters and the arrows
193 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
195 // we may have several different cases:
196 // 1. normal case: both TAB and ENTER are used for dialog navigation
197 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
198 // control in the dialog
199 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
200 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
202 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
203 m_lDlgCode
|= DLGC_WANTMESSAGE
;
204 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
205 m_lDlgCode
|= DLGC_WANTTAB
;
207 // do create the control - either an EDIT or RICHEDIT
208 wxString windowClass
= wxT("EDIT");
211 if ( m_windowStyle
& wxTE_RICH
)
213 static bool s_errorGiven
= FALSE
; // MT-FIXME
215 // only give the error msg once if the DLL can't be loaded
218 // first try to load the RichEdit DLL (will do nothing if already
220 if ( !wxRichEditModule::Load() )
222 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
234 msStyle
|= ES_AUTOVSCROLL
;
237 int ver
= wxRichEditModule::GetLoadedVersion();
240 windowClass
= wxT("RICHEDIT");
244 #ifndef RICHEDIT_CLASS
245 wxString RICHEDIT_CLASS
;
246 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), ver
);
248 RICHEDIT_CLASS
+= _T('W');
250 RICHEDIT_CLASS
+= _T('A');
251 #endif // Unicode/ANSI
252 #endif // !RICHEDIT_CLASS
254 windowClass
= RICHEDIT_CLASS
;
260 #endif // wxUSE_RICHEDIT
263 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
265 // Even with extended styles, need to combine with WS_BORDER for them to
267 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
268 msStyle
|= WS_BORDER
;
270 // NB: don't use pos and size as CreateWindowEx arguments because they
271 // might be -1 in which case we should use the default values (and
272 // SetSize called below takes care of it)
273 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
,
283 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create text ctrl") );
288 Ctl3dSubclassCtl(GetHwnd());
296 // Have to enable events
297 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0,
298 ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
);
302 SubclassWin(GetHWND());
304 // set font, position, size and initial value
305 wxFont
& fontParent
= parent
->GetFont();
306 if ( fontParent
.Ok() )
312 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
315 // Causes a crash for Symantec C++ and WIN32 for some reason
316 #if !(defined(__SC__) && defined(__WIN32__))
317 if ( !value
.IsEmpty() )
326 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
331 // Make sure the window style (etc.) reflects the HWND style (roughly)
332 void wxTextCtrl::AdoptAttributesFromHWND()
334 wxWindow::AdoptAttributesFromHWND();
336 HWND hWnd
= GetHwnd();
337 long style
= GetWindowLong(hWnd
, GWL_STYLE
);
339 // retrieve the style to see whether this is an edit or richedit ctrl
343 GetClassName(hWnd
, buf
, WXSIZEOF(buf
));
345 if ( wxStricmp(buf
, wxT("EDIT")) == 0 )
349 #endif // wxUSE_RICHEDIT
351 if (style
& ES_MULTILINE
)
352 m_windowStyle
|= wxTE_MULTILINE
;
353 if (style
& ES_PASSWORD
)
354 m_windowStyle
|= wxTE_PASSWORD
;
355 if (style
& ES_READONLY
)
356 m_windowStyle
|= wxTE_READONLY
;
357 if (style
& ES_WANTRETURN
)
358 m_windowStyle
|= wxTE_PROCESS_ENTER
;
361 void wxTextCtrl::SetupColours()
363 wxColour bkgndColour
;
364 // if (IsEditable() || (m_windowStyle & wxTE_MULTILINE))
365 bkgndColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
367 // bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
369 SetBackgroundColour(bkgndColour
);
370 SetForegroundColour(GetParent()->GetForegroundColour());
373 // ----------------------------------------------------------------------------
374 // set/get the controls text
375 // ----------------------------------------------------------------------------
377 wxString
wxTextCtrl::GetValue() const
379 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
380 // retrieving more than 64Kb under Win9x
386 int len
= GetWindowTextLength(GetHwnd());
389 // alloc one extra WORD as needed by the control
390 wxChar
*p
= str
.GetWriteBuf(++len
);
393 textRange
.chrg
.cpMin
= 0;
394 textRange
.chrg
.cpMax
= -1;
395 textRange
.lpstrText
= p
;
397 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
399 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
400 // the newlines which is neither Unix nor Windows style (Win95 with
401 // riched20.dll shows this behaviour) - convert it to something
405 if ( *p
== _T('\r') )
411 //else: no text at all, leave the string empty
415 #endif // wxUSE_RICHEDIT
417 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
418 // same one as above for consitency
419 wxString str
= wxGetWindowText(GetHWND());
421 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
424 void wxTextCtrl::SetValue(const wxString
& value
)
426 // if the text is long enough, it's faster to just set it instead of first
427 // comparing it with the old one (chances are that it will be different
428 // anyhow, this comparison is there to avoid flicker for small single-line
429 // edit controls mostly)
430 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
432 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
434 SetWindowText(GetHwnd(), valueDos
.c_str());
436 // for compatibility with the GTK and because it is more logical, we
437 // move the cursor to the end of the text after SetValue()
439 // GRG, Jun/2000: Changed this back after a lot of discussion
440 // in the lists. wxWindows 2.2 will have a set of flags to
441 // customize this behaviour.
442 //SetInsertionPointEnd();
448 void wxTextCtrl::WriteText(const wxString
& value
)
450 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
453 // ensure that the new text will be in the default style
455 (m_defaultStyle
.HasFont() || m_defaultStyle
.HasTextColour()) )
458 GetSelection(&start
, &end
);
459 SetStyle(start
, end
, m_defaultStyle
);
461 #endif // wxUSE_RICHEDIT
463 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
468 void wxTextCtrl::AppendText(const wxString
& text
)
470 SetInsertionPointEnd();
474 void wxTextCtrl::Clear()
476 SetWindowText(GetHwnd(), wxT(""));
479 // ----------------------------------------------------------------------------
480 // Clipboard operations
481 // ----------------------------------------------------------------------------
483 void wxTextCtrl::Copy()
487 HWND hWnd
= GetHwnd();
488 SendMessage(hWnd
, WM_COPY
, 0, 0L);
492 void wxTextCtrl::Cut()
496 HWND hWnd
= GetHwnd();
497 SendMessage(hWnd
, WM_CUT
, 0, 0L);
501 void wxTextCtrl::Paste()
505 HWND hWnd
= GetHwnd();
506 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
510 bool wxTextCtrl::CanCopy() const
512 // Can copy if there's a selection
514 GetSelection(& from
, & to
);
515 return (from
!= to
) ;
518 bool wxTextCtrl::CanCut() const
520 // Can cut if there's a selection
522 GetSelection(& from
, & to
);
523 return (from
!= to
) && (IsEditable());
526 bool wxTextCtrl::CanPaste() const
531 int dataFormat
= 0; // 0 == any format
532 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
538 // Standard edit control: check for straight text on clipboard
539 bool isTextAvailable
= FALSE
;
540 if ( ::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
542 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
546 return isTextAvailable
;
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
553 void wxTextCtrl::SetEditable(bool editable
)
555 HWND hWnd
= GetHwnd();
556 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
559 void wxTextCtrl::SetInsertionPoint(long pos
)
561 HWND hWnd
= GetHwnd();
569 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
570 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
573 #endif // wxUSE_RICHEDIT
575 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
576 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
579 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
582 static const wxChar
*nothing
= _T("");
583 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
586 void wxTextCtrl::SetInsertionPointEnd()
588 long pos
= GetLastPosition();
589 SetInsertionPoint(pos
);
592 long wxTextCtrl::GetInsertionPoint() const
600 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
605 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
609 long wxTextCtrl::GetLastPosition() const
611 HWND hWnd
= GetHwnd();
613 // Will always return a number > 0 (according to docs)
614 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
616 // This gets the char index for the _beginning_ of the last line
617 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
619 // Get number of characters in the last line. We'll add this to the character
620 // index for the last line, 1st position.
621 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
623 return (long)(charIndex
+ lineLength
);
626 // If the return values from and to are the same, there is no
628 void wxTextCtrl::GetSelection(long* from
, long* to
) const
634 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
636 *from
= charRange
.cpMin
;
637 *to
= charRange
.cpMax
;
642 DWORD dwStart
, dwEnd
;
643 WPARAM wParam
= (WPARAM
) &dwStart
; // receives starting position
644 LPARAM lParam
= (LPARAM
) &dwEnd
; // receives ending position
646 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
653 bool wxTextCtrl::IsEditable() const
655 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
657 return ((style
& ES_READONLY
) == 0);
660 // ----------------------------------------------------------------------------
662 // ----------------------------------------------------------------------------
664 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
667 HWND hWnd
= GetHwnd();
668 long fromChar
= from
;
671 // Set selection and remove it
673 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
675 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
677 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
679 // Now replace with 'value', by pasting.
680 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const wxChar
*)value
, 0, 0);
682 // Paste into edit control
683 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
685 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
689 void wxTextCtrl::Remove(long from
, long to
)
691 HWND hWnd
= GetHwnd();
692 long fromChar
= from
;
695 // Cut all selected text
697 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
699 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
701 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
704 void wxTextCtrl::SetSelection(long from
, long to
)
706 HWND hWnd
= GetHwnd();
707 long fromChar
= from
;
710 // if from and to are both -1, it means (in wxWindows) that all text should
711 // be selected. Translate into Windows convention
712 if ((from
== -1) && (to
== -1))
719 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
720 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
722 // WPARAM is 0: selection is scrolled into view
723 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
727 bool wxTextCtrl::LoadFile(const wxString
& file
)
729 if ( wxTextCtrlBase::LoadFile(file
) )
731 // update the size limit if needed
740 bool wxTextCtrl::IsModified() const
742 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
745 // Makes 'unmodified'
746 void wxTextCtrl::DiscardEdits()
748 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
751 int wxTextCtrl::GetNumberOfLines() const
753 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
756 long wxTextCtrl::XYToPosition(long x
, long y
) const
758 HWND hWnd
= GetHwnd();
760 // This gets the char index for the _beginning_ of this line
761 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
762 return (long)(x
+ charIndex
);
765 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
767 HWND hWnd
= GetHwnd();
769 // This gets the line number containing the character
774 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
777 #endif // wxUSE_RICHEDIT
778 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
786 // This gets the char index for the _beginning_ of this line
787 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
788 if ( charIndex
== -1 )
793 // The X position must therefore be the different between pos and charIndex
795 *x
= (long)(pos
- charIndex
);
802 void wxTextCtrl::ShowPosition(long pos
)
804 HWND hWnd
= GetHwnd();
806 // To scroll to a position, we pass the number of lines and characters
807 // to scroll *by*. This means that we need to:
808 // (1) Find the line position of the current line.
809 // (2) Find the line position of pos.
810 // (3) Scroll by (pos - current).
811 // For now, ignore the horizontal scrolling.
813 // Is this where scrolling is relative to - the line containing the caret?
814 // Or is the first visible line??? Try first visible line.
815 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
817 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
819 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
821 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
823 if (linesToScroll
!= 0)
824 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
827 int wxTextCtrl::GetLineLength(long lineNo
) const
829 long charIndex
= XYToPosition(0, lineNo
);
830 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
834 wxString
wxTextCtrl::GetLineText(long lineNo
) const
836 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
838 // there must be at least enough place for the length WORD in the
843 wxChar
*buf
= str
.GetWriteBuf(len
);
845 *(WORD
*)buf
= (WORD
)len
;
846 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
849 str
.UngetWriteBuf(len
);
854 // ----------------------------------------------------------------------------
856 // ----------------------------------------------------------------------------
858 void wxTextCtrl::Undo()
862 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
866 void wxTextCtrl::Redo()
870 // Same as Undo, since Undo undoes the undo, i.e. a redo.
871 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
875 bool wxTextCtrl::CanUndo() const
877 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
880 bool wxTextCtrl::CanRedo() const
882 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
885 // ----------------------------------------------------------------------------
886 // implemenation details
887 // ----------------------------------------------------------------------------
889 void wxTextCtrl::Command(wxCommandEvent
& event
)
891 SetValue(event
.GetString());
892 ProcessCommand (event
);
895 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
897 // By default, load the first file into the text window.
898 if (event
.GetNumberOfFiles() > 0)
900 LoadFile(event
.GetFiles()[0]);
904 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
906 switch ( event
.KeyCode() )
909 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
911 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
912 InitCommandEvent(event
);
913 event
.SetString(GetValue());
914 if ( GetEventHandler()->ProcessEvent(event
) )
917 //else: multiline controls need Enter for themselves
922 // always produce navigation event - even if we process TAB
923 // ourselves the fact that we got here means that the user code
924 // decided to skip processing of this TAB - probably to let it
925 // do its default job.
927 wxNavigationKeyEvent eventNav
;
928 eventNav
.SetDirection(!event
.ShiftDown());
929 eventNav
.SetWindowChange(event
.ControlDown());
930 eventNav
.SetEventObject(this);
932 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
938 // no, we didn't process it
942 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
949 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
952 event
.SetEventObject( this );
953 GetEventHandler()->ProcessEvent(event
);
959 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
960 InitCommandEvent(event
);
961 event
.SetString(GetValue());
962 ProcessCommand(event
);
967 // the text size limit has been hit - increase it
971 // the other notification messages are not processed
985 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
991 WXUINT
WXUNUSED(message
),
992 WXWPARAM
WXUNUSED(wParam
),
993 WXLPARAM
WXUNUSED(lParam
)
1000 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1001 return (WXHBRUSH
) hbrush
;
1003 #endif // wxUSE_CTL3D
1006 if (GetParent()->GetTransparentBackground())
1007 SetBkMode(hdc
, TRANSPARENT
);
1009 SetBkMode(hdc
, OPAQUE
);
1011 wxColour colBack
= GetBackgroundColour();
1013 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1014 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
1016 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1017 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1019 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1021 return (WXHBRUSH
)brush
->GetResourceHandle();
1024 // In WIN16, need to override normal erasing because
1025 // Ctl3D doesn't use the wxWindows background colour.
1027 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1029 wxColour
col(m_backgroundColour
);
1033 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1037 ::GetClientRect(GetHwnd(), &rect
);
1039 COLORREF ref
= PALETTERGB(col
.Red(),
1042 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1044 wxLogLastError(wxT("CreateSolidBrush"));
1046 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1048 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1050 ::FillRect(hdc
, &rect
, hBrush
);
1051 ::DeleteObject(hBrush
);
1052 ::SetMapMode(hdc
, mode
);
1057 void wxTextCtrl::AdjustSpaceLimit()
1060 unsigned int len
= ::GetWindowTextLength(GetHwnd()),
1061 limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1064 limit
= len
+ 0x8000; // 32Kb
1069 // as a nice side effect, this also allows passing limit > 64Kb
1070 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1073 #endif // wxUSE_RICHEDIT
1075 if ( limit
> 0xffff )
1077 // this will set it to a platform-dependent maximum (much more
1078 // than 64Kb under NT)
1082 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1088 bool wxTextCtrl::AcceptsFocus() const
1090 // we don't want focus if we can't be edited
1091 return IsEditable() && wxControl::AcceptsFocus();
1094 wxSize
wxTextCtrl::DoGetBestSize() const
1097 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1099 int wText
= DEFAULT_ITEM_WIDTH
;
1101 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1102 if ( m_windowStyle
& wxTE_MULTILINE
)
1104 hText
*= wxMax(GetNumberOfLines(), 5);
1106 //else: for single line control everything is ok
1108 return wxSize(wText
, hText
);
1111 // ----------------------------------------------------------------------------
1112 // standard handlers for standard edit menu events
1113 // ----------------------------------------------------------------------------
1115 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1120 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1125 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1130 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1135 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1140 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1142 event
.Enable( CanCut() );
1145 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1147 event
.Enable( CanCopy() );
1150 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1152 event
.Enable( CanPaste() );
1155 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1157 event
.Enable( CanUndo() );
1160 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1162 event
.Enable( CanRedo() );
1165 // the rest of the file only deals with the rich edit controls
1168 // ----------------------------------------------------------------------------
1169 // colour setting for the rich edit controls
1170 // ----------------------------------------------------------------------------
1172 // Watcom C++ doesn't define this
1174 #define SCF_ALL 0x0004
1177 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1179 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1181 // colour didn't really change
1187 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1188 // EM_SETBKGNDCOLOR additionally
1189 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1195 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1197 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1199 // colour didn't really change
1205 // change the colour of everything
1208 cf
.cbSize
= sizeof(cf
);
1209 cf
.dwMask
= CFM_COLOR
;
1210 cf
.crTextColor
= wxColourToRGB(colour
);
1211 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1217 // ----------------------------------------------------------------------------
1218 // styling support for rich edit controls
1219 // ----------------------------------------------------------------------------
1221 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1225 // can't do it with normal text control
1229 // the rich text control doesn't handle setting background colour, so don't
1230 // even try if it's the only thing we want to change
1231 if ( !style
.HasFont() && !style
.HasTextColour() )
1233 // nothing to do: return TRUE if there was really nothing to doand
1234 // FALSE fi we failed to set bg colour
1235 return !style
.HasBackgroundColour();
1238 // order the range if needed
1246 // we can only change the format of the selection, so select the range we
1247 // want and restore the old selection later
1248 long startOld
, endOld
;
1249 GetSelection(&startOld
, &endOld
);
1251 // but do we really have to change the selection?
1252 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1255 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
) start
, (LPARAM
) end
);
1257 // initialize CHARFORMAT struct
1260 cf
.cbSize
= sizeof(cf
);
1262 if ( style
.HasFont() )
1264 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
;
1266 // fill in data from LOGFONT but recalculate lfHeight because we need
1267 // the real height in twips and not the negative number which
1268 // wxFillLogFont() returns (this is correct in general and works with
1269 // the Windows font mapper, but not here)
1271 wxFillLogFont(&lf
, &style
.GetFont());
1272 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1273 cf
.bCharSet
= lf
.lfCharSet
;
1274 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1275 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1277 // also deal with underline/italic/bold attributes
1280 cf
.dwMask
|= CFM_ITALIC
;
1281 cf
.dwEffects
|= CFE_ITALIC
;
1284 if ( lf
.lfWeight
== FW_BOLD
)
1286 cf
.dwMask
|= CFM_BOLD
;
1287 cf
.dwEffects
|= CFE_BOLD
;
1290 if ( lf
.lfUnderline
)
1292 cf
.dwMask
|= CFM_UNDERLINE
;
1293 cf
.dwEffects
|= CFE_UNDERLINE
;
1296 // strikeout fonts are not supported by wxWindows
1299 if ( style
.HasTextColour() )
1301 cf
.dwMask
|= CFM_COLOR
;
1302 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1305 // do format the selection
1306 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1307 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1310 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1315 // restore the original selection
1316 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
)startOld
, (LPARAM
)endOld
);
1322 // ----------------------------------------------------------------------------
1324 // ----------------------------------------------------------------------------
1326 bool wxRichEditModule::OnInit()
1328 // don't do anything - we will load it when needed
1332 void wxRichEditModule::OnExit()
1336 FreeLibrary(ms_hRichEdit
);
1341 bool wxRichEditModule::Load(int version
)
1343 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1344 _T("incorrect richedit control version requested") );
1346 if ( version
<= ms_verRichEdit
)
1348 // we've already got this or better
1354 ::FreeLibrary(ms_hRichEdit
);
1357 // always try load riched20.dll first - like this we won't have to reload
1358 // it later if we're first asked for RE 1 and then for RE 2 or 3
1359 wxString dllname
= _T("riched20.dll");
1360 ms_hRichEdit
= ::LoadLibrary(dllname
);
1361 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1363 if ( !ms_hRichEdit
&& (version
== 1) )
1365 // fall back to RE 1
1366 dllname
= _T("riched32.dll");
1367 ms_hRichEdit
= ::LoadLibrary(dllname
);
1371 if ( !ms_hRichEdit
)
1373 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1375 ms_verRichEdit
= -1;
1383 #endif // wxUSE_RICHEDIT