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 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
78 // this module initializes RichEdit DLL if needed
79 class wxRichEditModule
: public wxModule
82 virtual bool OnInit();
83 virtual void OnExit();
85 // get the version currently loaded, -1 if none
86 static int GetLoadedVersion() { return ms_verRichEdit
; }
88 // load the richedit DLL of at least of required version
89 static bool Load(int version
= 1);
92 // the handle to richedit DLL and the version of the DLL loaded
93 static HINSTANCE ms_hRichEdit
;
95 // the DLL version loaded or -1 if none
96 static int ms_verRichEdit
;
98 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
101 HINSTANCE
wxRichEditModule::ms_hRichEdit
= (HINSTANCE
)NULL
;
102 int wxRichEditModule::ms_verRichEdit
= -1;
104 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
106 #endif // wxUSE_RICHEDIT
108 // ----------------------------------------------------------------------------
109 // event tables and other macros
110 // ----------------------------------------------------------------------------
112 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
114 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
115 EVT_CHAR(wxTextCtrl::OnChar
)
116 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
118 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
119 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
120 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
121 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
122 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
124 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
125 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
126 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
127 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
128 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
130 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 wxTextCtrl::wxTextCtrl()
149 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
150 const wxString
& value
,
154 const wxValidator
& validator
,
155 const wxString
& name
)
157 // base initialization
158 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
162 parent
->AddChild(this);
164 // translate wxWin style flags to MSW ones, checking for consistency while
166 long msStyle
= ES_LEFT
| WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
168 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
169 msStyle
|= WS_CLIPSIBLINGS
;
171 if ( m_windowStyle
& wxTE_MULTILINE
)
173 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
174 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
176 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
177 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
178 msStyle
|= WS_VSCROLL
;
179 m_windowStyle
|= wxTE_PROCESS_ENTER
;
182 msStyle
|= ES_AUTOHSCROLL
;
184 if (m_windowStyle
& wxHSCROLL
)
185 msStyle
|= (WS_HSCROLL
| ES_AUTOHSCROLL
);
187 if (m_windowStyle
& wxTE_READONLY
)
188 msStyle
|= ES_READONLY
;
190 if (m_windowStyle
& wxTE_PASSWORD
) // hidden input
191 msStyle
|= ES_PASSWORD
;
193 if (m_windowStyle
& wxTE_AUTO_SCROLL
)
194 msStyle
|= ES_AUTOHSCROLL
;
197 // we always want the characters and the arrows
198 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
200 // we may have several different cases:
201 // 1. normal case: both TAB and ENTER are used for dialog navigation
202 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
203 // control in the dialog
204 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
205 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
207 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
208 m_lDlgCode
|= DLGC_WANTMESSAGE
;
209 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
210 m_lDlgCode
|= DLGC_WANTTAB
;
212 // do create the control - either an EDIT or RICHEDIT
213 wxString windowClass
= wxT("EDIT");
216 if ( m_windowStyle
& wxTE_RICH
)
218 static bool s_errorGiven
= FALSE
; // MT-FIXME
220 // only give the error msg once if the DLL can't be loaded
223 // first try to load the RichEdit DLL (will do nothing if already
225 if ( !wxRichEditModule::Load() )
227 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
239 msStyle
|= ES_AUTOVSCROLL
;
242 int ver
= wxRichEditModule::GetLoadedVersion();
245 windowClass
= wxT("RICHEDIT");
249 #ifndef RICHEDIT_CLASS
250 wxString RICHEDIT_CLASS
;
251 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), ver
);
253 RICHEDIT_CLASS
+= _T('W');
255 RICHEDIT_CLASS
+= _T('A');
256 #endif // Unicode/ANSI
257 #endif // !RICHEDIT_CLASS
259 windowClass
= RICHEDIT_CLASS
;
265 #endif // wxUSE_RICHEDIT
268 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
270 // Even with extended styles, need to combine with WS_BORDER for them to
272 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
273 msStyle
|= WS_BORDER
;
275 // NB: don't use pos and size as CreateWindowEx arguments because they
276 // might be -1 in which case we should use the default values (and
277 // SetSize called below takes care of it)
278 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
,
288 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create text ctrl") );
293 Ctl3dSubclassCtl(GetHwnd());
301 // have to enable events manually
302 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
304 if ( m_windowStyle
& wxTE_AUTO_URL
)
308 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
311 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
313 #endif // wxUSE_RICHEDIT
315 SubclassWin(GetHWND());
317 // set font, position, size and initial value
318 wxFont
& fontParent
= parent
->GetFont();
319 if ( fontParent
.Ok() )
325 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
328 // Causes a crash for Symantec C++ and WIN32 for some reason
329 #if !(defined(__SC__) && defined(__WIN32__))
330 if ( !value
.IsEmpty() )
339 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
344 // Make sure the window style (etc.) reflects the HWND style (roughly)
345 void wxTextCtrl::AdoptAttributesFromHWND()
347 wxWindow::AdoptAttributesFromHWND();
349 HWND hWnd
= GetHwnd();
350 long style
= GetWindowLong(hWnd
, GWL_STYLE
);
352 // retrieve the style to see whether this is an edit or richedit ctrl
356 GetClassName(hWnd
, buf
, WXSIZEOF(buf
));
358 if ( wxStricmp(buf
, wxT("EDIT")) == 0 )
362 #endif // wxUSE_RICHEDIT
364 if (style
& ES_MULTILINE
)
365 m_windowStyle
|= wxTE_MULTILINE
;
366 if (style
& ES_PASSWORD
)
367 m_windowStyle
|= wxTE_PASSWORD
;
368 if (style
& ES_READONLY
)
369 m_windowStyle
|= wxTE_READONLY
;
370 if (style
& ES_WANTRETURN
)
371 m_windowStyle
|= wxTE_PROCESS_ENTER
;
374 void wxTextCtrl::SetupColours()
376 wxColour bkgndColour
;
377 // if (IsEditable() || (m_windowStyle & wxTE_MULTILINE))
378 bkgndColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
380 // bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
382 SetBackgroundColour(bkgndColour
);
383 SetForegroundColour(GetParent()->GetForegroundColour());
386 // ----------------------------------------------------------------------------
387 // set/get the controls text
388 // ----------------------------------------------------------------------------
390 wxString
wxTextCtrl::GetValue() const
392 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
393 // retrieving more than 64Kb under Win9x
399 int len
= GetWindowTextLength(GetHwnd());
402 // alloc one extra WORD as needed by the control
403 wxChar
*p
= str
.GetWriteBuf(++len
);
406 textRange
.chrg
.cpMin
= 0;
407 textRange
.chrg
.cpMax
= -1;
408 textRange
.lpstrText
= p
;
410 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
412 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
413 // the newlines which is neither Unix nor Windows style (Win95 with
414 // riched20.dll shows this behaviour) - convert it to something
418 if ( *p
== _T('\r') )
424 //else: no text at all, leave the string empty
428 #endif // wxUSE_RICHEDIT
430 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
431 // same one as above for consitency
432 wxString str
= wxGetWindowText(GetHWND());
434 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
437 void wxTextCtrl::SetValue(const wxString
& value
)
439 // if the text is long enough, it's faster to just set it instead of first
440 // comparing it with the old one (chances are that it will be different
441 // anyhow, this comparison is there to avoid flicker for small single-line
442 // edit controls mostly)
443 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
445 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
447 SetWindowText(GetHwnd(), valueDos
.c_str());
449 // for compatibility with the GTK and because it is more logical, we
450 // move the cursor to the end of the text after SetValue()
452 // GRG, Jun/2000: Changed this back after a lot of discussion
453 // in the lists. wxWindows 2.2 will have a set of flags to
454 // customize this behaviour.
455 //SetInsertionPointEnd();
461 void wxTextCtrl::WriteText(const wxString
& value
)
463 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
466 // ensure that the new text will be in the default style
468 (m_defaultStyle
.HasFont() || m_defaultStyle
.HasTextColour()) )
471 GetSelection(&start
, &end
);
472 SetStyle(start
, end
, m_defaultStyle
);
474 #endif // wxUSE_RICHEDIT
476 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
481 void wxTextCtrl::AppendText(const wxString
& text
)
483 SetInsertionPointEnd();
487 void wxTextCtrl::Clear()
489 SetWindowText(GetHwnd(), wxT(""));
492 // ----------------------------------------------------------------------------
493 // Clipboard operations
494 // ----------------------------------------------------------------------------
496 void wxTextCtrl::Copy()
500 HWND hWnd
= GetHwnd();
501 SendMessage(hWnd
, WM_COPY
, 0, 0L);
505 void wxTextCtrl::Cut()
509 HWND hWnd
= GetHwnd();
510 SendMessage(hWnd
, WM_CUT
, 0, 0L);
514 void wxTextCtrl::Paste()
518 HWND hWnd
= GetHwnd();
519 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
523 bool wxTextCtrl::CanCopy() const
525 // Can copy if there's a selection
527 GetSelection(& from
, & to
);
528 return (from
!= to
) ;
531 bool wxTextCtrl::CanCut() const
533 // Can cut if there's a selection
535 GetSelection(& from
, & to
);
536 return (from
!= to
) && (IsEditable());
539 bool wxTextCtrl::CanPaste() const
544 int dataFormat
= 0; // 0 == any format
545 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
551 // Standard edit control: check for straight text on clipboard
552 bool isTextAvailable
= FALSE
;
553 if ( ::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
555 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
559 return isTextAvailable
;
562 // ----------------------------------------------------------------------------
564 // ----------------------------------------------------------------------------
566 void wxTextCtrl::SetEditable(bool editable
)
568 HWND hWnd
= GetHwnd();
569 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
572 void wxTextCtrl::SetInsertionPoint(long pos
)
574 HWND hWnd
= GetHwnd();
582 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
583 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
586 #endif // wxUSE_RICHEDIT
588 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
589 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
592 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
595 static const wxChar
*nothing
= _T("");
596 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
599 void wxTextCtrl::SetInsertionPointEnd()
601 long pos
= GetLastPosition();
602 SetInsertionPoint(pos
);
605 long wxTextCtrl::GetInsertionPoint() const
613 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
618 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
622 long wxTextCtrl::GetLastPosition() const
624 HWND hWnd
= GetHwnd();
626 // Will always return a number > 0 (according to docs)
627 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
629 // This gets the char index for the _beginning_ of the last line
630 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
632 // Get number of characters in the last line. We'll add this to the character
633 // index for the last line, 1st position.
634 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
636 return (long)(charIndex
+ lineLength
);
639 // If the return values from and to are the same, there is no
641 void wxTextCtrl::GetSelection(long* from
, long* to
) const
647 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
649 *from
= charRange
.cpMin
;
650 *to
= charRange
.cpMax
;
655 DWORD dwStart
, dwEnd
;
656 WPARAM wParam
= (WPARAM
) &dwStart
; // receives starting position
657 LPARAM lParam
= (LPARAM
) &dwEnd
; // receives ending position
659 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
666 bool wxTextCtrl::IsEditable() const
668 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
670 return ((style
& ES_READONLY
) == 0);
673 // ----------------------------------------------------------------------------
675 // ----------------------------------------------------------------------------
677 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
679 HWND hWnd
= GetHwnd();
680 long fromChar
= from
;
683 // Set selection and remove it
685 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
686 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)TRUE
, (LPARAM
)value
.c_str());
688 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
689 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)0, (LPARAM
)value
.c_str());
693 void wxTextCtrl::Remove(long from
, long to
)
695 HWND hWnd
= GetHwnd();
696 long fromChar
= from
;
699 // Cut all selected text
701 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
702 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)TRUE
, (LPARAM
)"");
704 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
705 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)0, (LPARAM
)"");
709 void wxTextCtrl::SetSelection(long from
, long to
)
711 HWND hWnd
= GetHwnd();
712 long fromChar
= from
;
715 // if from and to are both -1, it means (in wxWindows) that all text should
716 // be selected. Translate into Windows convention
717 if ((from
== -1) && (to
== -1))
724 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
725 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
727 // WPARAM is 0: selection is scrolled into view
728 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
732 bool wxTextCtrl::LoadFile(const wxString
& file
)
734 if ( wxTextCtrlBase::LoadFile(file
) )
736 // update the size limit if needed
745 bool wxTextCtrl::IsModified() const
747 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
750 // Makes 'unmodified'
751 void wxTextCtrl::DiscardEdits()
753 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
756 int wxTextCtrl::GetNumberOfLines() const
758 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
761 long wxTextCtrl::XYToPosition(long x
, long y
) const
763 HWND hWnd
= GetHwnd();
765 // This gets the char index for the _beginning_ of this line
766 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
767 return (long)(x
+ charIndex
);
770 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
772 HWND hWnd
= GetHwnd();
774 // This gets the line number containing the character
779 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
782 #endif // wxUSE_RICHEDIT
783 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
791 // This gets the char index for the _beginning_ of this line
792 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
793 if ( charIndex
== -1 )
798 // The X position must therefore be the different between pos and charIndex
800 *x
= (long)(pos
- charIndex
);
807 void wxTextCtrl::ShowPosition(long pos
)
809 HWND hWnd
= GetHwnd();
811 // To scroll to a position, we pass the number of lines and characters
812 // to scroll *by*. This means that we need to:
813 // (1) Find the line position of the current line.
814 // (2) Find the line position of pos.
815 // (3) Scroll by (pos - current).
816 // For now, ignore the horizontal scrolling.
818 // Is this where scrolling is relative to - the line containing the caret?
819 // Or is the first visible line??? Try first visible line.
820 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
822 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
824 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
826 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
828 if (linesToScroll
!= 0)
829 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
832 int wxTextCtrl::GetLineLength(long lineNo
) const
834 long charIndex
= XYToPosition(0, lineNo
);
835 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
839 wxString
wxTextCtrl::GetLineText(long lineNo
) const
841 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
843 // there must be at least enough place for the length WORD in the
848 wxChar
*buf
= str
.GetWriteBuf(len
);
850 *(WORD
*)buf
= (WORD
)len
;
851 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
854 str
.UngetWriteBuf(len
);
859 // ----------------------------------------------------------------------------
861 // ----------------------------------------------------------------------------
863 void wxTextCtrl::Undo()
867 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
871 void wxTextCtrl::Redo()
875 // Same as Undo, since Undo undoes the undo, i.e. a redo.
876 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
880 bool wxTextCtrl::CanUndo() const
882 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
885 bool wxTextCtrl::CanRedo() const
887 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
890 // ----------------------------------------------------------------------------
891 // implemenation details
892 // ----------------------------------------------------------------------------
894 void wxTextCtrl::Command(wxCommandEvent
& event
)
896 SetValue(event
.GetString());
897 ProcessCommand (event
);
900 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
902 // By default, load the first file into the text window.
903 if (event
.GetNumberOfFiles() > 0)
905 LoadFile(event
.GetFiles()[0]);
909 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
911 switch ( event
.KeyCode() )
914 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
916 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
917 InitCommandEvent(event
);
918 event
.SetString(GetValue());
919 if ( GetEventHandler()->ProcessEvent(event
) )
922 //else: multiline controls need Enter for themselves
927 // always produce navigation event - even if we process TAB
928 // ourselves the fact that we got here means that the user code
929 // decided to skip processing of this TAB - probably to let it
930 // do its default job.
932 wxNavigationKeyEvent eventNav
;
933 eventNav
.SetDirection(!event
.ShiftDown());
934 eventNav
.SetWindowChange(event
.ControlDown());
935 eventNav
.SetEventObject(this);
937 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
943 // no, we didn't process it
947 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
954 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
957 event
.SetEventObject( this );
958 GetEventHandler()->ProcessEvent(event
);
964 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
965 InitCommandEvent(event
);
966 event
.SetString(GetValue());
967 ProcessCommand(event
);
972 // the text size limit has been hit - increase it
976 // the other notification messages are not processed
990 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
996 WXUINT
WXUNUSED(message
),
997 WXWPARAM
WXUNUSED(wParam
),
998 WXLPARAM
WXUNUSED(lParam
)
1005 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1006 return (WXHBRUSH
) hbrush
;
1008 #endif // wxUSE_CTL3D
1011 if (GetParent()->GetTransparentBackground())
1012 SetBkMode(hdc
, TRANSPARENT
);
1014 SetBkMode(hdc
, OPAQUE
);
1016 wxColour colBack
= GetBackgroundColour();
1018 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1019 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
1021 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1022 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1024 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1026 return (WXHBRUSH
)brush
->GetResourceHandle();
1029 // In WIN16, need to override normal erasing because
1030 // Ctl3D doesn't use the wxWindows background colour.
1032 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1034 wxColour
col(m_backgroundColour
);
1038 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1042 ::GetClientRect(GetHwnd(), &rect
);
1044 COLORREF ref
= PALETTERGB(col
.Red(),
1047 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1049 wxLogLastError(wxT("CreateSolidBrush"));
1051 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1053 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1055 ::FillRect(hdc
, &rect
, hBrush
);
1056 ::DeleteObject(hBrush
);
1057 ::SetMapMode(hdc
, mode
);
1062 void wxTextCtrl::AdjustSpaceLimit()
1065 unsigned int len
= ::GetWindowTextLength(GetHwnd()),
1066 limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1069 limit
= len
+ 0x8000; // 32Kb
1074 // as a nice side effect, this also allows passing limit > 64Kb
1075 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1078 #endif // wxUSE_RICHEDIT
1080 if ( limit
> 0xffff )
1082 // this will set it to a platform-dependent maximum (much more
1083 // than 64Kb under NT)
1087 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1093 bool wxTextCtrl::AcceptsFocus() const
1095 // we don't want focus if we can't be edited
1096 return IsEditable() && wxControl::AcceptsFocus();
1099 wxSize
wxTextCtrl::DoGetBestSize() const
1102 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1104 int wText
= DEFAULT_ITEM_WIDTH
;
1106 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1107 if ( m_windowStyle
& wxTE_MULTILINE
)
1109 hText
*= wxMax(GetNumberOfLines(), 5);
1111 //else: for single line control everything is ok
1113 return wxSize(wText
, hText
);
1116 // ----------------------------------------------------------------------------
1117 // standard handlers for standard edit menu events
1118 // ----------------------------------------------------------------------------
1120 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1125 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1130 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1135 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1140 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1145 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1147 event
.Enable( CanCut() );
1150 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1152 event
.Enable( CanCopy() );
1155 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1157 event
.Enable( CanPaste() );
1160 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1162 event
.Enable( CanUndo() );
1165 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1167 event
.Enable( CanRedo() );
1170 // the rest of the file only deals with the rich edit controls
1173 // ----------------------------------------------------------------------------
1174 // EN_LINK processing
1175 // ----------------------------------------------------------------------------
1177 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1179 NMHDR
*hdr
= (NMHDR
* )lParam
;
1180 if ( hdr
->code
== EN_LINK
)
1182 ENLINK
*enlink
= (ENLINK
*)hdr
;
1184 switch ( enlink
->msg
)
1187 // ok, so it is hardcoded - do we really nee to customize it?
1188 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1193 case WM_LBUTTONDOWN
:
1195 case WM_LBUTTONDBLCLK
:
1196 case WM_RBUTTONDOWN
:
1198 case WM_RBUTTONDBLCLK
:
1199 // send a mouse event
1201 static const wxEventType eventsMouse
[] =
1212 // the event ids are consecutive
1214 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1216 InitMouseEvent(evtMouse
,
1217 GET_X_LPARAM(enlink
->lParam
),
1218 GET_Y_LPARAM(enlink
->lParam
),
1221 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1223 enlink
->chrg
.cpMax
);
1225 InitCommandEvent(event
);
1227 *result
= ProcessCommand(event
);
1239 // ----------------------------------------------------------------------------
1240 // colour setting for the rich edit controls
1241 // ----------------------------------------------------------------------------
1243 // Watcom C++ doesn't define this
1245 #define SCF_ALL 0x0004
1248 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1250 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1252 // colour didn't really change
1258 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1259 // EM_SETBKGNDCOLOR additionally
1260 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1266 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1268 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1270 // colour didn't really change
1276 // change the colour of everything
1279 cf
.cbSize
= sizeof(cf
);
1280 cf
.dwMask
= CFM_COLOR
;
1281 cf
.crTextColor
= wxColourToRGB(colour
);
1282 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1288 // ----------------------------------------------------------------------------
1289 // styling support for rich edit controls
1290 // ----------------------------------------------------------------------------
1292 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1296 // can't do it with normal text control
1300 // the rich text control doesn't handle setting background colour, so don't
1301 // even try if it's the only thing we want to change
1302 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1303 !style
.HasFont() && !style
.HasTextColour() )
1305 // nothing to do: return TRUE if there was really nothing to do and
1306 // FALSE if we failed to set bg colour
1307 return !style
.HasBackgroundColour();
1310 // order the range if needed
1318 // we can only change the format of the selection, so select the range we
1319 // want and restore the old selection later
1320 long startOld
, endOld
;
1321 GetSelection(&startOld
, &endOld
);
1323 // but do we really have to change the selection?
1324 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1327 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
) start
, (LPARAM
) end
);
1329 // initialize CHARFORMAT struct
1336 cf
.cbSize
= sizeof(cf
);
1338 if ( style
.HasFont() )
1340 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1341 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1343 // fill in data from LOGFONT but recalculate lfHeight because we need
1344 // the real height in twips and not the negative number which
1345 // wxFillLogFont() returns (this is correct in general and works with
1346 // the Windows font mapper, but not here)
1348 wxFillLogFont(&lf
, &style
.GetFont());
1349 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1350 cf
.bCharSet
= lf
.lfCharSet
;
1351 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1352 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1354 // also deal with underline/italic/bold attributes: note that we must
1355 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1356 // style to allow clearing it
1359 cf
.dwEffects
|= CFE_ITALIC
;
1362 if ( lf
.lfWeight
== FW_BOLD
)
1364 cf
.dwEffects
|= CFE_BOLD
;
1367 if ( lf
.lfUnderline
)
1369 cf
.dwEffects
|= CFE_UNDERLINE
;
1372 // strikeout fonts are not supported by wxWindows
1375 if ( style
.HasTextColour() )
1377 cf
.dwMask
|= CFM_COLOR
;
1378 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1382 if ( wxRichEditModule::GetLoadedVersion() > 1 && style
.HasBackgroundColour() )
1384 cf
.dwMask
|= CFM_BACKCOLOR
;
1385 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1387 #endif // wxUSE_RICHEDIT2
1389 // do format the selection
1390 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1391 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1394 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1399 // restore the original selection
1400 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
)startOld
, (LPARAM
)endOld
);
1406 // ----------------------------------------------------------------------------
1408 // ----------------------------------------------------------------------------
1410 bool wxRichEditModule::OnInit()
1412 // don't do anything - we will load it when needed
1416 void wxRichEditModule::OnExit()
1420 FreeLibrary(ms_hRichEdit
);
1425 bool wxRichEditModule::Load(int version
)
1427 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1428 _T("incorrect richedit control version requested") );
1430 if ( version
<= ms_verRichEdit
)
1432 // we've already got this or better
1438 ::FreeLibrary(ms_hRichEdit
);
1441 // always try load riched20.dll first - like this we won't have to reload
1442 // it later if we're first asked for RE 1 and then for RE 2 or 3
1443 wxString dllname
= _T("riched20.dll");
1444 ms_hRichEdit
= ::LoadLibrary(dllname
);
1445 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1447 if ( !ms_hRichEdit
&& (version
== 1) )
1449 // fall back to RE 1
1450 dllname
= _T("riched32.dll");
1451 ms_hRichEdit
= ::LoadLibrary(dllname
);
1455 if ( !ms_hRichEdit
)
1457 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1459 ms_verRichEdit
= -1;
1467 #endif // wxUSE_RICHEDIT
1469 #endif // wxUSE_TEXTCTRL