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
95 #define SF_UNICODE 0x0010
98 // Watcom C++ doesn't define this
100 #define SCF_ALL 0x0004
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
109 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
111 #endif // wxUSE_RICHEDIT
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
119 // this module initializes RichEdit DLL(s) if needed
120 class wxRichEditModule
: public wxModule
123 virtual bool OnInit();
124 virtual void OnExit();
126 // load the richedit DLL of at least of required version
127 static bool Load(int version
= 1);
130 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
131 static HINSTANCE ms_hRichEdit
[2];
133 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
136 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
138 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
140 #endif // wxUSE_RICHEDIT
142 // ----------------------------------------------------------------------------
143 // event tables and other macros
144 // ----------------------------------------------------------------------------
146 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
148 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
149 EVT_CHAR(wxTextCtrl::OnChar
)
150 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
152 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
153 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
154 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
155 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
156 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
158 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
159 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
160 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
161 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
162 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
164 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
168 // ============================================================================
170 // ============================================================================
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void wxTextCtrl::Init()
183 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
184 const wxString
& value
,
188 const wxValidator
& validator
,
189 const wxString
& name
)
191 // base initialization
192 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
196 parent
->AddChild(this);
198 // translate wxWin style flags to MSW ones
199 WXDWORD msStyle
= MSWGetCreateWindowFlags();
201 // do create the control - either an EDIT or RICHEDIT
202 wxString windowClass
= wxT("EDIT");
205 if ( m_windowStyle
& wxTE_AUTO_URL
)
207 // automatic URL detection only works in RichEdit 2.0+
208 m_windowStyle
|= wxTE_RICH2
;
211 if ( m_windowStyle
& wxTE_RICH2
)
213 // using richedit 2.0 implies using wxTE_RICH
214 m_windowStyle
|= wxTE_RICH
;
217 // we need to load the richedit DLL before creating the rich edit control
218 if ( m_windowStyle
& wxTE_RICH
)
220 static bool s_errorGiven
= FALSE
;// MT-FIXME
222 // Which version do we need? Use 1.0 by default because it is much more
223 // like the the standard EDIT or 2.0 if explicitly requested, but use
224 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
226 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
227 // (and thus EDIT) much better than RichEdit 2.0 so we probably
228 // should use 3.0 if available as it is the best of both worlds -
229 // but as I can't test it right now I don't do it (VZ)
231 const int verRichEdit
= 2;
232 #else // !wxUSE_UNICODE
233 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
234 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
236 // only give the error msg once if the DLL can't be loaded
239 // try to load the RichEdit DLL (will do nothing if already done)
240 if ( !wxRichEditModule::Load(verRichEdit
) )
243 // try another version?
244 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
246 if ( !wxRichEditModule::Load(verRichEdit
) )
247 #endif // wxUSE_UNICODE
249 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
256 // have we managed to load any richedit version?
259 msStyle
|= ES_AUTOVSCROLL
;
261 m_verRichEdit
= verRichEdit
;
262 if ( m_verRichEdit
== 1 )
264 windowClass
= wxT("RICHEDIT");
268 #ifndef RICHEDIT_CLASS
269 wxString RICHEDIT_CLASS
;
270 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
272 RICHEDIT_CLASS
+= _T('W');
274 RICHEDIT_CLASS
+= _T('A');
275 #endif // Unicode/ANSI
276 #endif // !RICHEDIT_CLASS
278 windowClass
= RICHEDIT_CLASS
;
282 #endif // wxUSE_RICHEDIT
284 // we need to turn '\n's into "\r\n"s for the multiline controls
286 if ( m_windowStyle
& wxTE_MULTILINE
)
288 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
295 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
298 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
303 // enable the events we're interested in: we want to get EN_CHANGE and
304 // EN_UPDATE as for the normal controls
305 LPARAM mask
= ENM_CHANGE
| ENM_UPDATE
;
307 if ( GetRichVersion() == 1 )
309 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
310 // explained in its handler
311 mask
|= ENM_MOUSEEVENTS
;
313 else if ( m_windowStyle
& wxTE_AUTO_URL
)
317 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
320 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
322 #endif // wxUSE_RICHEDIT
327 // Make sure the window style (etc.) reflects the HWND style (roughly)
328 void wxTextCtrl::AdoptAttributesFromHWND()
330 wxWindow::AdoptAttributesFromHWND();
332 HWND hWnd
= GetHwnd();
333 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
335 // retrieve the style to see whether this is an edit or richedit ctrl
337 wxString classname
= wxGetWindowClass(GetHWND());
339 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
346 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
348 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
353 #endif // wxUSE_RICHEDIT
355 if (style
& ES_MULTILINE
)
356 m_windowStyle
|= wxTE_MULTILINE
;
357 if (style
& ES_PASSWORD
)
358 m_windowStyle
|= wxTE_PASSWORD
;
359 if (style
& ES_READONLY
)
360 m_windowStyle
|= wxTE_READONLY
;
361 if (style
& ES_WANTRETURN
)
362 m_windowStyle
|= wxTE_PROCESS_ENTER
;
365 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
367 // default border for the text controls is the sunken one
368 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
370 style
|= wxBORDER_SUNKEN
;
373 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
378 if ( style
& wxTE_MULTILINE
)
380 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
381 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
383 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
384 if ( !(style
& wxTE_NO_VSCROLL
) )
385 msStyle
|= WS_VSCROLL
;
387 style
|= wxTE_PROCESS_ENTER
;
391 // there is really no reason to not have this style for single line
393 msStyle
|= ES_AUTOHSCROLL
;
396 if ( style
& wxHSCROLL
)
397 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
399 if ( style
& wxTE_READONLY
)
400 msStyle
|= ES_READONLY
;
402 if ( style
& wxTE_PASSWORD
)
403 msStyle
|= ES_PASSWORD
;
405 if ( style
& wxTE_AUTO_SCROLL
)
406 msStyle
|= ES_AUTOHSCROLL
;
408 if ( style
& wxTE_NOHIDESEL
)
409 msStyle
|= ES_NOHIDESEL
;
414 void wxTextCtrl::SetWindowStyleFlag(long style
)
417 // we have to deal with some styles separately because they can't be
418 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
419 // using richedit-specific EM_SETOPTIONS
421 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
423 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
425 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
426 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
428 #endif // wxUSE_RICHEDIT
430 wxControl::SetWindowStyleFlag(style
);
433 // ----------------------------------------------------------------------------
434 // set/get the controls text
435 // ----------------------------------------------------------------------------
437 wxString
wxTextCtrl::GetValue() const
439 // range 0..-1 is special for GetRange() and means to retrieve all text
440 return GetRange(0, -1);
443 wxString
wxTextCtrl::GetRange(long from
, long to
) const
447 if ( from
>= to
&& to
!= -1 )
449 // nothing to retrieve
456 int len
= GetWindowTextLength(GetHwnd());
459 // alloc one extra WORD as needed by the control
460 wxChar
*p
= str
.GetWriteBuf(++len
);
463 textRange
.chrg
.cpMin
= from
;
464 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
465 textRange
.lpstrText
= p
;
467 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
469 if ( m_verRichEdit
> 1 )
471 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
472 // neither Unix nor Windows style - convert it to something
476 if ( *p
== _T('\r') )
483 if ( m_verRichEdit
== 1 )
485 // convert to the canonical form - see comment below
486 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
489 //else: no text at all, leave the string empty
492 #endif // wxUSE_RICHEDIT
495 str
= wxGetWindowText(GetHWND());
497 // need only a range?
500 str
= str
.Mid(from
, to
- from
);
503 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
504 // canonical one (same one as above) for consistency with the other kinds
505 // of controls and, more importantly, with the other ports
506 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
512 void wxTextCtrl::SetValue(const wxString
& value
)
514 // if the text is long enough, it's faster to just set it instead of first
515 // comparing it with the old one (chances are that it will be different
516 // anyhow, this comparison is there to avoid flicker for small single-line
517 // edit controls mostly)
518 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
520 DoWriteText(value
, FALSE
/* not selection only */);
522 // mark the control as being not dirty - we changed its text, not the
526 // for compatibility, don't move the cursor when doing SetValue()
527 SetInsertionPoint(0);
531 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
533 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
537 wchar_t *wbuf
= (wchar_t *)buf
;
538 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
543 cb
-= sizeof(wchar_t);
544 (*pcb
) += sizeof(wchar_t);
547 *(const wchar_t **)dwCookie
= wpc
;
552 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from utils.cpp
554 #if wxUSE_UNICODE_MSLU
555 bool wxTextCtrl::StreamIn(const wxString
& value
,
556 wxFontEncoding
WXUNUSED(encoding
),
559 const wchar_t *wpc
= value
.c_str();
560 #else // !wxUSE_UNICODE_MSLU
561 bool wxTextCtrl::StreamIn(const wxString
& value
,
562 wxFontEncoding encoding
,
565 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
566 // text in the non default charset - otherwise it thinks it knows better
567 // than we do and always shows it in the default one
569 // first get the Windows code page for this encoding
570 long codepage
= wxEncodingToCodepage(encoding
);
571 if ( codepage
== -1 )
577 // next translate to Unicode using this code page
578 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
581 wxWCharBuffer
wchBuf(len
);
583 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
586 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
587 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
589 wxLogLastError(_T("MultiByteToWideChar"));
592 // finally, stream it in the control
593 const wchar_t *wpc
= wchBuf
;
594 #endif // wxUSE_UNICODE_MSLU
598 eds
.dwCookie
= (DWORD
)&wpc
;
599 // the cast below is needed for broken (very) old mingw32 headers
600 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
602 if ( !::SendMessage(GetHwnd(), EM_STREAMIN
,
605 (selectionOnly
? SFF_SELECTION
: 0),
606 (LPARAM
)&eds
) || eds
.dwError
)
608 wxLogLastError(_T("EM_STREAMIN"));
613 #endif // !wxUSE_WCHAR_T
618 #endif // wxUSE_RICHEDIT
620 void wxTextCtrl::WriteText(const wxString
& value
)
625 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
628 if ( m_windowStyle
& wxTE_MULTILINE
)
629 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
634 // there are several complications with the rich edit controls here
638 // first, ensure that the new text will be in the default style
639 if ( !m_defaultStyle
.IsDefault() )
642 GetSelection(&start
, &end
);
643 SetStyle(start
, end
, m_defaultStyle
);
646 #if wxUSE_UNICODE_MSLU
647 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
648 // but EM_STREAMIN works
649 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
651 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
653 #endif // wxUSE_UNICODE_MSLU
656 // next check if the text we're inserting must be shown in a non
657 // default charset -- this only works for RichEdit > 1.0
658 if ( GetRichVersion() > 1 )
660 wxFont font
= m_defaultStyle
.GetFont();
666 wxFontEncoding encoding
= font
.GetEncoding();
667 if ( encoding
!= wxFONTENCODING_SYSTEM
)
669 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
673 #endif // !wxUSE_UNICODE
677 #endif // wxUSE_RICHEDIT
679 if ( !selectionOnly
)
681 //SetSelection(-1, -1);
682 // This eliminates an annoying flashing effect
683 // when replacing all text.
687 ::SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
693 void wxTextCtrl::AppendText(const wxString
& text
)
695 SetInsertionPointEnd();
700 void wxTextCtrl::Clear()
702 ::SetWindowText(GetHwnd(), wxT(""));
707 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
711 size_t lenOld
= GetValue().length();
713 wxUint32 code
= event
.GetRawKeyCode();
714 ::keybd_event(code
, 0, 0 /* key press */, 0);
715 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
717 // assume that any alphanumeric key changes the total number of characters
718 // in the control - this should work in 99% of cases
719 return GetValue().length() != lenOld
;
724 // ----------------------------------------------------------------------------
725 // Clipboard operations
726 // ----------------------------------------------------------------------------
728 void wxTextCtrl::Copy()
732 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
736 void wxTextCtrl::Cut()
740 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
744 void wxTextCtrl::Paste()
748 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
752 bool wxTextCtrl::CanCopy() const
754 // Can copy if there's a selection
756 GetSelection(&from
, &to
);
760 bool wxTextCtrl::CanCut() const
762 return CanCopy() && IsEditable();
765 bool wxTextCtrl::CanPaste() const
773 UINT cf
= 0; // 0 == any format
775 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
777 #endif // wxUSE_RICHEDIT
779 // Standard edit control: check for straight text on clipboard
780 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
783 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
786 return isTextAvailable
;
789 // ----------------------------------------------------------------------------
791 // ----------------------------------------------------------------------------
793 void wxTextCtrl::SetEditable(bool editable
)
795 HWND hWnd
= GetHwnd();
796 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
799 void wxTextCtrl::SetInsertionPoint(long pos
)
801 DoSetSelection(pos
, pos
);
804 void wxTextCtrl::SetInsertionPointEnd()
809 if ( m_verRichEdit
== 1 )
811 // we don't have to waste time calling GetLastPosition() in this case
814 else // !RichEdit 1.0
815 #endif // wxUSE_RICHEDIT
817 pos
= GetLastPosition();
820 SetInsertionPoint(pos
);
823 long wxTextCtrl::GetInsertionPoint() const
831 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
834 #endif // wxUSE_RICHEDIT
836 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
840 long wxTextCtrl::GetLastPosition() const
842 int numLines
= GetNumberOfLines();
843 long posStartLastLine
= XYToPosition(0, numLines
- 1);
845 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
847 return posStartLastLine
+ lenLastLine
;
850 // If the return values from and to are the same, there is no
852 void wxTextCtrl::GetSelection(long* from
, long* to
) const
858 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
860 *from
= charRange
.cpMin
;
861 *to
= charRange
.cpMax
;
864 #endif // !wxUSE_RICHEDIT
866 DWORD dwStart
, dwEnd
;
867 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
874 bool wxTextCtrl::IsEditable() const
876 // strangely enough, we may be called before the control is created: our
877 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
882 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
884 return (style
& ES_READONLY
) == 0;
887 // ----------------------------------------------------------------------------
889 // ----------------------------------------------------------------------------
891 void wxTextCtrl::SetSelection(long from
, long to
)
893 // if from and to are both -1, it means (in wxWindows) that all text should
894 // be selected - translate into Windows convention
895 if ( (from
== -1) && (to
== -1) )
901 DoSetSelection(from
, to
);
904 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
906 HWND hWnd
= GetHwnd();
915 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
918 #endif // wxUSE_RICHEDIT
920 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
925 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
928 // WPARAM is 0: selection is scrolled into view
929 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
933 // ----------------------------------------------------------------------------
935 // ----------------------------------------------------------------------------
937 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
939 // Set selection and remove it
940 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
942 SendMessage(GetHwnd(), EM_REPLACESEL
,
948 (LPARAM
)value
.c_str());
951 void wxTextCtrl::Remove(long from
, long to
)
953 Replace(from
, to
, _T(""));
956 bool wxTextCtrl::LoadFile(const wxString
& file
)
958 if ( wxTextCtrlBase::LoadFile(file
) )
960 // update the size limit if needed
969 bool wxTextCtrl::IsModified() const
971 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
974 // Makes 'unmodified'
975 void wxTextCtrl::DiscardEdits()
977 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
980 int wxTextCtrl::GetNumberOfLines() const
982 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
985 long wxTextCtrl::XYToPosition(long x
, long y
) const
987 // This gets the char index for the _beginning_ of this line
988 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
990 return charIndex
+ x
;
993 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
995 HWND hWnd
= GetHwnd();
997 // This gets the line number containing the character
1002 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1005 #endif // wxUSE_RICHEDIT
1007 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1016 // This gets the char index for the _beginning_ of this line
1017 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1018 if ( charIndex
== -1 )
1023 // The X position must therefore be the different between pos and charIndex
1025 *x
= pos
- charIndex
;
1032 void wxTextCtrl::ShowPosition(long pos
)
1034 HWND hWnd
= GetHwnd();
1036 // To scroll to a position, we pass the number of lines and characters
1037 // to scroll *by*. This means that we need to:
1038 // (1) Find the line position of the current line.
1039 // (2) Find the line position of pos.
1040 // (3) Scroll by (pos - current).
1041 // For now, ignore the horizontal scrolling.
1043 // Is this where scrolling is relative to - the line containing the caret?
1044 // Or is the first visible line??? Try first visible line.
1045 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1047 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1049 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1051 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1053 if (linesToScroll
!= 0)
1054 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1057 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1059 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1062 int wxTextCtrl::GetLineLength(long lineNo
) const
1064 long pos
= XYToPosition(0, lineNo
);
1066 return GetLengthOfLineContainingPos(pos
);
1069 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1071 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1073 // there must be at least enough place for the length WORD in the
1075 len
+= sizeof(WORD
);
1078 wxChar
*buf
= str
.GetWriteBuf(len
);
1080 *(WORD
*)buf
= (WORD
)len
;
1081 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1084 str
.UngetWriteBuf(len
);
1089 void wxTextCtrl::SetMaxLength(unsigned long len
)
1091 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1094 // ----------------------------------------------------------------------------
1096 // ----------------------------------------------------------------------------
1098 void wxTextCtrl::Undo()
1102 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1106 void wxTextCtrl::Redo()
1110 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1111 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1115 bool wxTextCtrl::CanUndo() const
1117 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1120 bool wxTextCtrl::CanRedo() const
1122 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1125 // ----------------------------------------------------------------------------
1126 // implemenation details
1127 // ----------------------------------------------------------------------------
1129 void wxTextCtrl::Command(wxCommandEvent
& event
)
1131 SetValue(event
.GetString());
1132 ProcessCommand (event
);
1135 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1137 // By default, load the first file into the text window.
1138 if (event
.GetNumberOfFiles() > 0)
1140 LoadFile(event
.GetFiles()[0]);
1144 // ----------------------------------------------------------------------------
1145 // kbd input processing
1146 // ----------------------------------------------------------------------------
1148 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1150 MSG
*msg
= (MSG
*)pMsg
;
1152 // check for our special keys here: if we don't do it and the parent frame
1153 // uses them as accelerators, they wouldn't work at all, so we disable
1154 // usual preprocessing for them
1155 if ( msg
->message
== WM_KEYDOWN
)
1157 WORD vkey
= (WORD
) msg
->wParam
;
1158 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1160 if ( vkey
== VK_BACK
)
1165 if ( wxIsCtrlDown() )
1179 else if ( wxIsShiftDown() )
1181 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1187 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1190 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1192 switch ( event
.KeyCode() )
1195 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1197 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1198 InitCommandEvent(event
);
1199 event
.SetString(GetValue());
1200 if ( GetEventHandler()->ProcessEvent(event
) )
1203 //else: multiline controls need Enter for themselves
1208 // always produce navigation event - even if we process TAB
1209 // ourselves the fact that we got here means that the user code
1210 // decided to skip processing of this TAB - probably to let it
1211 // do its default job.
1213 wxNavigationKeyEvent eventNav
;
1214 eventNav
.SetDirection(!event
.ShiftDown());
1215 eventNav
.SetWindowChange(event
.ControlDown());
1216 eventNav
.SetEventObject(this);
1218 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1224 // no, we didn't process it
1228 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1230 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1232 if ( nMsg
== WM_GETDLGCODE
)
1234 // we always want the chars and the arrows
1235 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1237 // we may have several different cases:
1238 // 1. normal case: both TAB and ENTER are used for dialog navigation
1239 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1240 // next control in the dialog
1241 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1243 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass
1244 // to the next control
1246 // the multiline edit control should always get <Return> for itself
1247 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1248 lDlgCode
|= DLGC_WANTMESSAGE
;
1250 if ( HasFlag(wxTE_PROCESS_TAB
) )
1251 lDlgCode
|= DLGC_WANTTAB
;
1259 // ----------------------------------------------------------------------------
1260 // text control event processing
1261 // ----------------------------------------------------------------------------
1263 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1270 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1273 event
.SetEventObject( this );
1274 GetEventHandler()->ProcessEvent(event
);
1280 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1281 InitCommandEvent(event
);
1282 event
.SetString(GetValue());
1283 ProcessCommand(event
);
1288 // the text size limit has been hit - increase it
1289 if ( !AdjustSpaceLimit() )
1291 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1292 InitCommandEvent(event
);
1293 event
.SetString(GetValue());
1294 ProcessCommand(event
);
1298 // the other notification messages are not processed
1313 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1319 WXUINT
WXUNUSED(message
),
1320 WXWPARAM
WXUNUSED(wParam
),
1321 WXLPARAM
WXUNUSED(lParam
)
1328 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1329 return (WXHBRUSH
) hbrush
;
1331 #endif // wxUSE_CTL3D
1334 if (GetParent()->GetTransparentBackground())
1335 SetBkMode(hdc
, TRANSPARENT
);
1337 SetBkMode(hdc
, OPAQUE
);
1339 wxColour colBack
= GetBackgroundColour();
1341 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1342 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1344 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1345 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1347 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1349 return (WXHBRUSH
)brush
->GetResourceHandle();
1352 // In WIN16, need to override normal erasing because
1353 // Ctl3D doesn't use the wxWindows background colour.
1355 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1357 wxColour
col(m_backgroundColour
);
1361 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1365 ::GetClientRect(GetHwnd(), &rect
);
1367 COLORREF ref
= wxColourToRGB(col
);
1368 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1370 wxLogLastError(wxT("CreateSolidBrush"));
1372 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1374 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1376 ::FillRect(hdc
, &rect
, hBrush
);
1377 ::DeleteObject(hBrush
);
1378 ::SetMapMode(hdc
, mode
);
1383 bool wxTextCtrl::AdjustSpaceLimit()
1386 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1388 // HACK: we try to automatically extend the limit for the amount of text
1389 // to allow (interactively) entering more than 64Kb of text under
1390 // Win9x but we shouldn't reset the text limit which was previously
1391 // set explicitly with SetMaxLength()
1393 // we could solve this by storing the limit we set in wxTextCtrl but
1394 // to save space we prefer to simply test here the actual limit
1395 // value: we consider that SetMaxLength() can only be called for
1397 if ( limit
< 0x8000 )
1399 // we've got more text than limit set by SetMaxLength()
1403 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1406 limit
= len
+ 0x8000; // 32Kb
1411 // as a nice side effect, this also allows passing limit > 64Kb
1412 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1415 #endif // wxUSE_RICHEDIT
1417 if ( limit
> 0xffff )
1419 // this will set it to a platform-dependent maximum (much more
1420 // than 64Kb under NT)
1424 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1429 // we changed the limit
1433 bool wxTextCtrl::AcceptsFocus() const
1435 // we don't want focus if we can't be edited
1436 return IsEditable() && wxControl::AcceptsFocus();
1439 wxSize
wxTextCtrl::DoGetBestSize() const
1442 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1444 int wText
= DEFAULT_ITEM_WIDTH
;
1446 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1447 if ( m_windowStyle
& wxTE_MULTILINE
)
1449 hText
*= wxMax(GetNumberOfLines(), 5);
1451 //else: for single line control everything is ok
1453 return wxSize(wText
, hText
);
1456 // ----------------------------------------------------------------------------
1457 // standard handlers for standard edit menu events
1458 // ----------------------------------------------------------------------------
1460 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1465 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1470 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1475 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1480 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1485 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1487 event
.Enable( CanCut() );
1490 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1492 event
.Enable( CanCopy() );
1495 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1497 event
.Enable( CanPaste() );
1500 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1502 event
.Enable( CanUndo() );
1505 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1507 event
.Enable( CanRedo() );
1510 // the rest of the file only deals with the rich edit controls
1513 // ----------------------------------------------------------------------------
1514 // EN_LINK processing
1515 // ----------------------------------------------------------------------------
1517 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1519 NMHDR
*hdr
= (NMHDR
* )lParam
;
1520 switch ( hdr
->code
)
1524 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1525 UINT msg
= msgf
->msg
;
1527 // this is a bit crazy but richedit 1.0 sends us all mouse
1528 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1529 // generate the wxWin events for this message manually
1531 // NB: in fact, this is still not totally correct as it does
1532 // send us WM_LBUTTONUP if the selection was cleared by the
1533 // last click -- so currently we get 2 events in this case,
1534 // but as I don't see any obvious way to check for this I
1535 // leave this code in place because it's still better than
1536 // not getting left up events at all
1537 if ( msg
== WM_LBUTTONUP
)
1539 WXUINT flags
= msgf
->wParam
;
1540 int x
= GET_X_LPARAM(msgf
->lParam
),
1541 y
= GET_Y_LPARAM(msgf
->lParam
);
1543 HandleMouseEvent(msg
, x
, y
, flags
);
1547 // return TRUE to process the event (and FALSE to ignore it)
1552 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1554 switch ( enlink
->msg
)
1557 // ok, so it is hardcoded - do we really nee to
1559 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1564 case WM_LBUTTONDOWN
:
1566 case WM_LBUTTONDBLCLK
:
1567 case WM_RBUTTONDOWN
:
1569 case WM_RBUTTONDBLCLK
:
1570 // send a mouse event
1572 static const wxEventType eventsMouse
[] =
1583 // the event ids are consecutive
1585 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1587 InitMouseEvent(evtMouse
,
1588 GET_X_LPARAM(enlink
->lParam
),
1589 GET_Y_LPARAM(enlink
->lParam
),
1592 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1594 enlink
->chrg
.cpMax
);
1596 InitCommandEvent(event
);
1598 *result
= ProcessCommand(event
);
1606 // not processed, leave it to the base class
1607 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1610 // ----------------------------------------------------------------------------
1611 // colour setting for the rich edit controls
1612 // ----------------------------------------------------------------------------
1614 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1616 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1618 // colour didn't really change
1624 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1625 // EM_SETBKGNDCOLOR additionally
1626 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1632 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1634 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1636 // colour didn't really change
1642 // change the colour of everything
1645 cf
.cbSize
= sizeof(cf
);
1646 cf
.dwMask
= CFM_COLOR
;
1647 cf
.crTextColor
= wxColourToRGB(colour
);
1648 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1654 // ----------------------------------------------------------------------------
1655 // styling support for rich edit controls
1656 // ----------------------------------------------------------------------------
1658 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1662 // can't do it with normal text control
1666 // the richedit 1.0 doesn't handle setting background colour, so don't
1667 // even try to do anything if it's the only thing we want to change
1668 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() )
1670 // nothing to do: return TRUE if there was really nothing to do and
1671 // FALSE if we failed to set bg colour
1672 return !style
.HasBackgroundColour();
1675 // order the range if needed
1683 // we can only change the format of the selection, so select the range we
1684 // want and restore the old selection later
1685 long startOld
, endOld
;
1686 GetSelection(&startOld
, &endOld
);
1688 // but do we really have to change the selection?
1689 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1693 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1696 // initialize CHARFORMAT struct
1705 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1706 // CHARFORMAT in that case
1708 if ( m_verRichEdit
== 1 )
1710 // this is the only thing the control is going to grok
1711 cf
.cbSize
= sizeof(CHARFORMAT
);
1716 // CHARFORMAT or CHARFORMAT2
1717 cf
.cbSize
= sizeof(cf
);
1720 if ( style
.HasFont() )
1722 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1723 // but using it doesn't seem to hurt neither so leaving it for now
1725 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1726 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1728 // fill in data from LOGFONT but recalculate lfHeight because we need
1729 // the real height in twips and not the negative number which
1730 // wxFillLogFont() returns (this is correct in general and works with
1731 // the Windows font mapper, but not here)
1733 wxFillLogFont(&lf
, &style
.GetFont());
1734 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1735 cf
.bCharSet
= lf
.lfCharSet
;
1736 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1737 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1739 // also deal with underline/italic/bold attributes: note that we must
1740 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1741 // style to allow clearing it
1744 cf
.dwEffects
|= CFE_ITALIC
;
1747 if ( lf
.lfWeight
== FW_BOLD
)
1749 cf
.dwEffects
|= CFE_BOLD
;
1752 if ( lf
.lfUnderline
)
1754 cf
.dwEffects
|= CFE_UNDERLINE
;
1757 // strikeout fonts are not supported by wxWindows
1760 if ( style
.HasTextColour() )
1762 cf
.dwMask
|= CFM_COLOR
;
1763 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1767 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1769 cf
.dwMask
|= CFM_BACKCOLOR
;
1770 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1772 #endif // wxUSE_RICHEDIT2
1774 // do format the selection
1775 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1776 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1779 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1784 // restore the original selection
1785 DoSetSelection(startOld
, endOld
, FALSE
);
1791 // ----------------------------------------------------------------------------
1793 // ----------------------------------------------------------------------------
1795 bool wxRichEditModule::OnInit()
1797 // don't do anything - we will load it when needed
1801 void wxRichEditModule::OnExit()
1803 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
1805 if ( ms_hRichEdit
[i
] )
1807 ::FreeLibrary(ms_hRichEdit
[i
]);
1813 bool wxRichEditModule::Load(int version
)
1815 // we don't support loading richedit 3.0 as I don't know how to distinguish
1816 // it from 2.0 anyhow
1817 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
1818 _T("incorrect richedit control version requested") );
1820 // make it the index in the array
1823 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
1825 // we had already tried to load it and failed
1829 if ( ms_hRichEdit
[version
] )
1831 // we've already got this one
1835 wxString dllname
= version
? _T("riched20") : _T("riched32");
1836 dllname
+= _T(".dll");
1838 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
1840 if ( !ms_hRichEdit
[version
] )
1842 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1844 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
1852 #endif // wxUSE_RICHEDIT
1854 #endif // wxUSE_TEXTCTRL