1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "textctrl.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
43 #include "wx/module.h"
46 #include "wx/clipbrd.h"
49 #include "wx/textfile.h"
53 #include "wx/msw/private.h"
57 #include <sys/types.h>
59 #if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__))
63 // old mingw32 doesn't define this
65 #define CFM_CHARSET 0x08000000
69 #define CFM_BACKCOLOR 0x04000000
72 // cygwin does not have these defined for richedit
74 #define ENM_LINK 0x04000000
77 #ifndef EM_AUTOURLDETECT
78 #define EM_AUTOURLDETECT (WM_USER + 91)
82 #define EN_LINK 0x070b
84 typedef struct _enlink
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
100 // this module initializes RichEdit DLL if needed
101 class wxRichEditModule
: public wxModule
104 virtual bool OnInit();
105 virtual void OnExit();
107 // get the version currently loaded, -1 if none
108 static int GetLoadedVersion() { return ms_verRichEdit
; }
110 // load the richedit DLL of at least of required version
111 static bool Load(int version
= 1);
114 // the handle to richedit DLL and the version of the DLL loaded
115 static HINSTANCE ms_hRichEdit
;
117 // the DLL version loaded or -1 if none
118 static int ms_verRichEdit
;
120 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
123 HINSTANCE
wxRichEditModule::ms_hRichEdit
= (HINSTANCE
)NULL
;
124 int wxRichEditModule::ms_verRichEdit
= -1;
126 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
128 #endif // wxUSE_RICHEDIT
130 // ----------------------------------------------------------------------------
131 // event tables and other macros
132 // ----------------------------------------------------------------------------
134 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
136 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
137 EVT_CHAR(wxTextCtrl::OnChar
)
138 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
140 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
141 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
142 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
143 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
144 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
146 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
147 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
148 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
149 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
150 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
152 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
156 // ============================================================================
158 // ============================================================================
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 wxTextCtrl::wxTextCtrl()
171 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
172 const wxString
& value
,
176 const wxValidator
& validator
,
177 const wxString
& name
)
179 // base initialization
180 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
184 parent
->AddChild(this);
186 // translate wxWin style flags to MSW ones, checking for consistency while
188 long msStyle
= ES_LEFT
| WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
190 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
191 msStyle
|= WS_CLIPSIBLINGS
;
193 if ( m_windowStyle
& wxTE_MULTILINE
)
195 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
196 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
198 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
199 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
200 msStyle
|= WS_VSCROLL
;
201 m_windowStyle
|= wxTE_PROCESS_ENTER
;
204 msStyle
|= ES_AUTOHSCROLL
;
206 if (m_windowStyle
& wxHSCROLL
)
207 msStyle
|= (WS_HSCROLL
| ES_AUTOHSCROLL
);
209 if (m_windowStyle
& wxTE_READONLY
)
210 msStyle
|= ES_READONLY
;
212 if (m_windowStyle
& wxTE_PASSWORD
) // hidden input
213 msStyle
|= ES_PASSWORD
;
215 if (m_windowStyle
& wxTE_AUTO_SCROLL
)
216 msStyle
|= ES_AUTOHSCROLL
;
219 // we always want the characters and the arrows
220 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
222 // we may have several different cases:
223 // 1. normal case: both TAB and ENTER are used for dialog navigation
224 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
225 // control in the dialog
226 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
227 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
229 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
230 m_lDlgCode
|= DLGC_WANTMESSAGE
;
231 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
232 m_lDlgCode
|= DLGC_WANTTAB
;
234 // do create the control - either an EDIT or RICHEDIT
235 wxString windowClass
= wxT("EDIT");
238 if ( m_windowStyle
& wxTE_RICH
)
240 static bool s_errorGiven
= FALSE
; // MT-FIXME
242 // only give the error msg once if the DLL can't be loaded
245 // first try to load the RichEdit DLL (will do nothing if already
247 if ( !wxRichEditModule::Load() )
249 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
261 msStyle
|= ES_AUTOVSCROLL
;
262 // Experimental: this seems to help with the scroll problem. See messages from Jekabs Andrushaitis <j.andrusaitis@konts.lv>
263 // wx-dev list, entitled "[wx-dev] wxMSW-EVT_KEY_DOWN and wxMSW-wxTextCtrl" and "[wx-dev] TextCtrl (RichEdit)"
264 // Unfortunately, showing the selection in blue when the control doesn't have
265 // the focus is non-standard behaviour, and we need to find another workaround.
266 //msStyle |= ES_NOHIDESEL ;
269 int ver
= wxRichEditModule::GetLoadedVersion();
272 windowClass
= wxT("RICHEDIT");
276 #ifndef RICHEDIT_CLASS
277 wxString RICHEDIT_CLASS
;
278 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), ver
);
280 RICHEDIT_CLASS
+= _T('W');
282 RICHEDIT_CLASS
+= _T('A');
283 #endif // Unicode/ANSI
284 #endif // !RICHEDIT_CLASS
286 windowClass
= RICHEDIT_CLASS
;
292 #endif // wxUSE_RICHEDIT
295 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
297 // Even with extended styles, need to combine with WS_BORDER for them to
299 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
300 msStyle
|= WS_BORDER
;
302 // NB: don't use pos and size as CreateWindowEx arguments because they
303 // might be -1 in which case we should use the default values (and
304 // SetSize called below takes care of it)
305 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
,
315 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create text ctrl") );
320 Ctl3dSubclassCtl(GetHwnd());
328 // have to enable events manually
329 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
331 if ( m_windowStyle
& wxTE_AUTO_URL
)
335 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
338 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
340 #endif // wxUSE_RICHEDIT
342 SubclassWin(GetHWND());
344 // set font, position, size and initial value
345 wxFont
& fontParent
= parent
->GetFont();
346 if ( fontParent
.Ok() )
352 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
355 // Causes a crash for Symantec C++ and WIN32 for some reason
356 #if !(defined(__SC__) && defined(__WIN32__))
357 if ( !value
.IsEmpty() )
366 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
371 // Make sure the window style (etc.) reflects the HWND style (roughly)
372 void wxTextCtrl::AdoptAttributesFromHWND()
374 wxWindow::AdoptAttributesFromHWND();
376 HWND hWnd
= GetHwnd();
377 long style
= GetWindowLong(hWnd
, GWL_STYLE
);
379 // retrieve the style to see whether this is an edit or richedit ctrl
383 GetClassName(hWnd
, buf
, WXSIZEOF(buf
));
385 if ( wxStricmp(buf
, wxT("EDIT")) == 0 )
389 #endif // wxUSE_RICHEDIT
391 if (style
& ES_MULTILINE
)
392 m_windowStyle
|= wxTE_MULTILINE
;
393 if (style
& ES_PASSWORD
)
394 m_windowStyle
|= wxTE_PASSWORD
;
395 if (style
& ES_READONLY
)
396 m_windowStyle
|= wxTE_READONLY
;
397 if (style
& ES_WANTRETURN
)
398 m_windowStyle
|= wxTE_PROCESS_ENTER
;
401 void wxTextCtrl::SetupColours()
403 wxColour bkgndColour
;
404 // if (IsEditable() || (m_windowStyle & wxTE_MULTILINE))
405 bkgndColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
407 // bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
409 SetBackgroundColour(bkgndColour
);
410 SetForegroundColour(GetParent()->GetForegroundColour());
413 // ----------------------------------------------------------------------------
414 // set/get the controls text
415 // ----------------------------------------------------------------------------
417 wxString
wxTextCtrl::GetValue() const
419 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
420 // retrieving more than 64Kb under Win9x
426 int len
= GetWindowTextLength(GetHwnd());
429 // alloc one extra WORD as needed by the control
430 wxChar
*p
= str
.GetWriteBuf(++len
);
433 textRange
.chrg
.cpMin
= 0;
434 textRange
.chrg
.cpMax
= -1;
435 textRange
.lpstrText
= p
;
437 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
439 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
440 // the newlines which is neither Unix nor Windows style (Win95 with
441 // riched20.dll shows this behaviour) - convert it to something
445 if ( *p
== _T('\r') )
451 //else: no text at all, leave the string empty
455 #endif // wxUSE_RICHEDIT
457 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
458 // same one as above for consitency
459 wxString str
= wxGetWindowText(GetHWND());
461 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
464 void wxTextCtrl::SetValue(const wxString
& value
)
466 // if the text is long enough, it's faster to just set it instead of first
467 // comparing it with the old one (chances are that it will be different
468 // anyhow, this comparison is there to avoid flicker for small single-line
469 // edit controls mostly)
470 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
472 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
474 SetWindowText(GetHwnd(), valueDos
.c_str());
476 // for compatibility with the GTK and because it is more logical, we
477 // move the cursor to the end of the text after SetValue()
479 // GRG, Jun/2000: Changed this back after a lot of discussion
480 // in the lists. wxWindows 2.2 will have a set of flags to
481 // customize this behaviour.
482 //SetInsertionPointEnd();
488 void wxTextCtrl::WriteText(const wxString
& value
)
490 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
493 // ensure that the new text will be in the default style
495 (m_defaultStyle
.HasFont() || m_defaultStyle
.HasTextColour()) )
498 GetSelection(&start
, &end
);
499 SetStyle(start
, end
, m_defaultStyle
);
501 #endif // wxUSE_RICHEDIT
503 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
508 void wxTextCtrl::AppendText(const wxString
& text
)
510 SetInsertionPointEnd();
514 void wxTextCtrl::Clear()
516 SetWindowText(GetHwnd(), wxT(""));
519 // ----------------------------------------------------------------------------
520 // Clipboard operations
521 // ----------------------------------------------------------------------------
523 void wxTextCtrl::Copy()
527 HWND hWnd
= GetHwnd();
528 SendMessage(hWnd
, WM_COPY
, 0, 0L);
532 void wxTextCtrl::Cut()
536 HWND hWnd
= GetHwnd();
537 SendMessage(hWnd
, WM_CUT
, 0, 0L);
541 void wxTextCtrl::Paste()
545 HWND hWnd
= GetHwnd();
546 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
550 bool wxTextCtrl::CanCopy() const
552 // Can copy if there's a selection
554 GetSelection(& from
, & to
);
555 return (from
!= to
) ;
558 bool wxTextCtrl::CanCut() const
560 // Can cut if there's a selection
562 GetSelection(& from
, & to
);
563 return (from
!= to
) && (IsEditable());
566 bool wxTextCtrl::CanPaste() const
571 int dataFormat
= 0; // 0 == any format
572 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
578 // Standard edit control: check for straight text on clipboard
579 bool isTextAvailable
= FALSE
;
580 if ( ::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
582 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
586 return isTextAvailable
;
589 // ----------------------------------------------------------------------------
591 // ----------------------------------------------------------------------------
593 void wxTextCtrl::SetEditable(bool editable
)
595 HWND hWnd
= GetHwnd();
596 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
599 void wxTextCtrl::SetInsertionPoint(long pos
)
601 HWND hWnd
= GetHwnd();
609 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
610 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
613 #endif // wxUSE_RICHEDIT
615 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
616 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
619 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
626 static const wxChar
*nothing
= _T("");
627 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
631 void wxTextCtrl::SetInsertionPointEnd()
633 long pos
= GetLastPosition();
634 SetInsertionPoint(pos
);
637 long wxTextCtrl::GetInsertionPoint() const
645 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
650 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
654 long wxTextCtrl::GetLastPosition() const
656 HWND hWnd
= GetHwnd();
658 // Will always return a number > 0 (according to docs)
659 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
661 // This gets the char index for the _beginning_ of the last line
662 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
664 // Get number of characters in the last line. We'll add this to the character
665 // index for the last line, 1st position.
666 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
668 return (long)(charIndex
+ lineLength
);
671 // If the return values from and to are the same, there is no
673 void wxTextCtrl::GetSelection(long* from
, long* to
) const
679 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
681 *from
= charRange
.cpMin
;
682 *to
= charRange
.cpMax
;
687 DWORD dwStart
, dwEnd
;
688 WPARAM wParam
= (WPARAM
) &dwStart
; // receives starting position
689 LPARAM lParam
= (LPARAM
) &dwEnd
; // receives ending position
691 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
698 bool wxTextCtrl::IsEditable() const
700 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
702 return ((style
& ES_READONLY
) == 0);
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
711 HWND hWnd
= GetHwnd();
712 long fromChar
= from
;
715 // Set selection and remove it
717 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
718 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)TRUE
, (LPARAM
)value
.c_str());
720 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
721 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)0, (LPARAM
)value
.c_str());
725 void wxTextCtrl::Remove(long from
, long to
)
727 HWND hWnd
= GetHwnd();
728 long fromChar
= from
;
731 // Cut all selected text
733 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
734 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)TRUE
, (LPARAM
)"");
736 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
737 SendMessage(hWnd
, EM_REPLACESEL
, (WPARAM
)0, (LPARAM
)"");
741 void wxTextCtrl::SetSelection(long from
, long to
)
743 HWND hWnd
= GetHwnd();
744 long fromChar
= from
;
747 // if from and to are both -1, it means (in wxWindows) that all text should
748 // be selected. Translate into Windows convention
749 if ((from
== -1) && (to
== -1))
756 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
757 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
759 // WPARAM is 0: selection is scrolled into view
760 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
764 bool wxTextCtrl::LoadFile(const wxString
& file
)
766 if ( wxTextCtrlBase::LoadFile(file
) )
768 // update the size limit if needed
777 bool wxTextCtrl::IsModified() const
779 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
782 // Makes 'unmodified'
783 void wxTextCtrl::DiscardEdits()
785 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
788 int wxTextCtrl::GetNumberOfLines() const
790 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
793 long wxTextCtrl::XYToPosition(long x
, long y
) const
795 HWND hWnd
= GetHwnd();
797 // This gets the char index for the _beginning_ of this line
798 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
799 return (long)(x
+ charIndex
);
802 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
804 HWND hWnd
= GetHwnd();
806 // This gets the line number containing the character
811 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
814 #endif // wxUSE_RICHEDIT
815 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
823 // This gets the char index for the _beginning_ of this line
824 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
825 if ( charIndex
== -1 )
830 // The X position must therefore be the different between pos and charIndex
832 *x
= (long)(pos
- charIndex
);
839 void wxTextCtrl::ShowPosition(long pos
)
841 HWND hWnd
= GetHwnd();
843 // To scroll to a position, we pass the number of lines and characters
844 // to scroll *by*. This means that we need to:
845 // (1) Find the line position of the current line.
846 // (2) Find the line position of pos.
847 // (3) Scroll by (pos - current).
848 // For now, ignore the horizontal scrolling.
850 // Is this where scrolling is relative to - the line containing the caret?
851 // Or is the first visible line??? Try first visible line.
852 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
854 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
856 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
858 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
860 if (linesToScroll
!= 0)
861 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
864 int wxTextCtrl::GetLineLength(long lineNo
) const
866 long charIndex
= XYToPosition(0, lineNo
);
867 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
871 wxString
wxTextCtrl::GetLineText(long lineNo
) const
873 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
875 // there must be at least enough place for the length WORD in the
880 wxChar
*buf
= str
.GetWriteBuf(len
);
882 *(WORD
*)buf
= (WORD
)len
;
883 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
886 str
.UngetWriteBuf(len
);
891 void wxTextCtrl::SetMaxLength(unsigned long len
)
893 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
896 // ----------------------------------------------------------------------------
898 // ----------------------------------------------------------------------------
900 void wxTextCtrl::Undo()
904 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
908 void wxTextCtrl::Redo()
912 // Same as Undo, since Undo undoes the undo, i.e. a redo.
913 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
917 bool wxTextCtrl::CanUndo() const
919 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
922 bool wxTextCtrl::CanRedo() const
924 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
927 // ----------------------------------------------------------------------------
928 // implemenation details
929 // ----------------------------------------------------------------------------
931 void wxTextCtrl::Command(wxCommandEvent
& event
)
933 SetValue(event
.GetString());
934 ProcessCommand (event
);
937 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
939 // By default, load the first file into the text window.
940 if (event
.GetNumberOfFiles() > 0)
942 LoadFile(event
.GetFiles()[0]);
946 // ----------------------------------------------------------------------------
947 // kbd input processing
948 // ----------------------------------------------------------------------------
950 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
952 MSG
*msg
= (MSG
*)pMsg
;
954 // check for our special keys here: if we don't do it and the parent frame
955 // uses them as accelerators, they wouldn't work at all, so we disable
956 // usual preprocessing for them
957 if ( msg
->message
== WM_KEYDOWN
)
959 WORD vkey
= msg
->wParam
;
960 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
962 if ( vkey
== VK_BACK
)
967 if ( wxIsCtrlDown() )
981 else if ( wxIsShiftDown() )
983 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
989 return wxControl::MSWShouldPreProcessMessage(pMsg
);
992 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
994 switch ( event
.KeyCode() )
997 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
999 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1000 InitCommandEvent(event
);
1001 event
.SetString(GetValue());
1002 if ( GetEventHandler()->ProcessEvent(event
) )
1005 //else: multiline controls need Enter for themselves
1010 // always produce navigation event - even if we process TAB
1011 // ourselves the fact that we got here means that the user code
1012 // decided to skip processing of this TAB - probably to let it
1013 // do its default job.
1015 wxNavigationKeyEvent eventNav
;
1016 eventNav
.SetDirection(!event
.ShiftDown());
1017 eventNav
.SetWindowChange(event
.ControlDown());
1018 eventNav
.SetEventObject(this);
1020 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1026 // no, we didn't process it
1030 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1037 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1040 event
.SetEventObject( this );
1041 GetEventHandler()->ProcessEvent(event
);
1047 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1048 InitCommandEvent(event
);
1049 event
.SetString(GetValue());
1050 ProcessCommand(event
);
1055 // the text size limit has been hit - increase it
1056 if ( !AdjustSpaceLimit() )
1058 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1059 InitCommandEvent(event
);
1060 event
.SetString(GetValue());
1061 ProcessCommand(event
);
1065 // the other notification messages are not processed
1079 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1085 WXUINT
WXUNUSED(message
),
1086 WXWPARAM
WXUNUSED(wParam
),
1087 WXLPARAM
WXUNUSED(lParam
)
1094 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1095 return (WXHBRUSH
) hbrush
;
1097 #endif // wxUSE_CTL3D
1100 if (GetParent()->GetTransparentBackground())
1101 SetBkMode(hdc
, TRANSPARENT
);
1103 SetBkMode(hdc
, OPAQUE
);
1105 wxColour colBack
= GetBackgroundColour();
1107 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1108 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
1110 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1111 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1113 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1115 return (WXHBRUSH
)brush
->GetResourceHandle();
1118 // In WIN16, need to override normal erasing because
1119 // Ctl3D doesn't use the wxWindows background colour.
1121 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1123 wxColour
col(m_backgroundColour
);
1127 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1131 ::GetClientRect(GetHwnd(), &rect
);
1133 COLORREF ref
= PALETTERGB(col
.Red(),
1136 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1138 wxLogLastError(wxT("CreateSolidBrush"));
1140 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1142 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1144 ::FillRect(hdc
, &rect
, hBrush
);
1145 ::DeleteObject(hBrush
);
1146 ::SetMapMode(hdc
, mode
);
1151 bool wxTextCtrl::AdjustSpaceLimit()
1154 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1156 // HACK: we try to automatically extend the limit for the amount of text
1157 // to allow (interactively) entering more than 64Kb of text under
1158 // Win9x but we shouldn't reset the text limit which was previously
1159 // set explicitly with SetMaxLength()
1161 // we could solve this by storing the limit we set in wxTextCtrl but
1162 // to save space we prefer to simply test here the actual limit
1163 // value: we consider that SetMaxLength() can only be called for
1165 if ( limit
< 0x8000 )
1167 // we've got more text than limit set by SetMaxLength()
1171 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1174 limit
= len
+ 0x8000; // 32Kb
1179 // as a nice side effect, this also allows passing limit > 64Kb
1180 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1183 #endif // wxUSE_RICHEDIT
1185 if ( limit
> 0xffff )
1187 // this will set it to a platform-dependent maximum (much more
1188 // than 64Kb under NT)
1192 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1197 // we changed the limit
1201 bool wxTextCtrl::AcceptsFocus() const
1203 // we don't want focus if we can't be edited
1204 return IsEditable() && wxControl::AcceptsFocus();
1207 wxSize
wxTextCtrl::DoGetBestSize() const
1210 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1212 int wText
= DEFAULT_ITEM_WIDTH
;
1214 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1215 if ( m_windowStyle
& wxTE_MULTILINE
)
1217 hText
*= wxMax(GetNumberOfLines(), 5);
1219 //else: for single line control everything is ok
1221 return wxSize(wText
, hText
);
1224 // ----------------------------------------------------------------------------
1225 // standard handlers for standard edit menu events
1226 // ----------------------------------------------------------------------------
1228 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1233 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1238 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1243 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1248 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1253 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1255 event
.Enable( CanCut() );
1258 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1260 event
.Enable( CanCopy() );
1263 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1265 event
.Enable( CanPaste() );
1268 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1270 event
.Enable( CanUndo() );
1273 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1275 event
.Enable( CanRedo() );
1278 // the rest of the file only deals with the rich edit controls
1281 // ----------------------------------------------------------------------------
1282 // EN_LINK processing
1283 // ----------------------------------------------------------------------------
1285 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1287 NMHDR
*hdr
= (NMHDR
* )lParam
;
1288 if ( hdr
->code
== EN_LINK
)
1290 ENLINK
*enlink
= (ENLINK
*)hdr
;
1292 switch ( enlink
->msg
)
1295 // ok, so it is hardcoded - do we really nee to customize it?
1296 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1301 case WM_LBUTTONDOWN
:
1303 case WM_LBUTTONDBLCLK
:
1304 case WM_RBUTTONDOWN
:
1306 case WM_RBUTTONDBLCLK
:
1307 // send a mouse event
1309 static const wxEventType eventsMouse
[] =
1320 // the event ids are consecutive
1322 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1324 InitMouseEvent(evtMouse
,
1325 GET_X_LPARAM(enlink
->lParam
),
1326 GET_Y_LPARAM(enlink
->lParam
),
1329 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1331 enlink
->chrg
.cpMax
);
1333 InitCommandEvent(event
);
1335 *result
= ProcessCommand(event
);
1347 // ----------------------------------------------------------------------------
1348 // colour setting for the rich edit controls
1349 // ----------------------------------------------------------------------------
1351 // Watcom C++ doesn't define this
1353 #define SCF_ALL 0x0004
1356 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1358 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1360 // colour didn't really change
1366 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1367 // EM_SETBKGNDCOLOR additionally
1368 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1374 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1376 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1378 // colour didn't really change
1384 // change the colour of everything
1387 cf
.cbSize
= sizeof(cf
);
1388 cf
.dwMask
= CFM_COLOR
;
1389 cf
.crTextColor
= wxColourToRGB(colour
);
1390 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1396 // ----------------------------------------------------------------------------
1397 // styling support for rich edit controls
1398 // ----------------------------------------------------------------------------
1400 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1404 // can't do it with normal text control
1408 // the rich text control doesn't handle setting background colour, so don't
1409 // even try if it's the only thing we want to change
1410 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1411 !style
.HasFont() && !style
.HasTextColour() )
1413 // nothing to do: return TRUE if there was really nothing to do and
1414 // FALSE if we failed to set bg colour
1415 return !style
.HasBackgroundColour();
1418 // order the range if needed
1426 // we can only change the format of the selection, so select the range we
1427 // want and restore the old selection later
1428 long startOld
, endOld
;
1429 GetSelection(&startOld
, &endOld
);
1431 // but do we really have to change the selection?
1432 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1435 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
) start
, (LPARAM
) end
);
1437 // initialize CHARFORMAT struct
1444 cf
.cbSize
= sizeof(cf
);
1446 if ( style
.HasFont() )
1448 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1449 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1451 // fill in data from LOGFONT but recalculate lfHeight because we need
1452 // the real height in twips and not the negative number which
1453 // wxFillLogFont() returns (this is correct in general and works with
1454 // the Windows font mapper, but not here)
1456 wxFillLogFont(&lf
, &style
.GetFont());
1457 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1458 cf
.bCharSet
= lf
.lfCharSet
;
1459 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1460 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1462 // also deal with underline/italic/bold attributes: note that we must
1463 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1464 // style to allow clearing it
1467 cf
.dwEffects
|= CFE_ITALIC
;
1470 if ( lf
.lfWeight
== FW_BOLD
)
1472 cf
.dwEffects
|= CFE_BOLD
;
1475 if ( lf
.lfUnderline
)
1477 cf
.dwEffects
|= CFE_UNDERLINE
;
1480 // strikeout fonts are not supported by wxWindows
1483 if ( style
.HasTextColour() )
1485 cf
.dwMask
|= CFM_COLOR
;
1486 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1490 if ( wxRichEditModule::GetLoadedVersion() > 1 && style
.HasBackgroundColour() )
1492 cf
.dwMask
|= CFM_BACKCOLOR
;
1493 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1495 #endif // wxUSE_RICHEDIT2
1497 // do format the selection
1498 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1499 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1502 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1507 // restore the original selection
1508 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
)startOld
, (LPARAM
)endOld
);
1514 // ----------------------------------------------------------------------------
1516 // ----------------------------------------------------------------------------
1518 bool wxRichEditModule::OnInit()
1520 // don't do anything - we will load it when needed
1524 void wxRichEditModule::OnExit()
1528 FreeLibrary(ms_hRichEdit
);
1533 bool wxRichEditModule::Load(int version
)
1535 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1536 _T("incorrect richedit control version requested") );
1538 if ( version
<= ms_verRichEdit
)
1540 // we've already got this or better
1546 ::FreeLibrary(ms_hRichEdit
);
1549 // always try load riched20.dll first - like this we won't have to reload
1550 // it later if we're first asked for RE 1 and then for RE 2 or 3
1551 wxString dllname
= _T("riched20.dll");
1552 ms_hRichEdit
= ::LoadLibrary(dllname
);
1553 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1555 if ( !ms_hRichEdit
&& (version
== 1) )
1557 // fall back to RE 1
1558 dllname
= _T("riched32.dll");
1559 ms_hRichEdit
= ::LoadLibrary(dllname
);
1563 if ( !ms_hRichEdit
)
1565 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1567 ms_verRichEdit
= -1;
1575 #endif // wxUSE_RICHEDIT
1577 #endif // wxUSE_TEXTCTRL