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, checking for consistency while
200 long msStyle
= ES_LEFT
| WS_TABSTOP
;
202 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
203 msStyle
|= WS_CLIPSIBLINGS
;
205 if ( m_windowStyle
& wxTE_MULTILINE
)
207 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
208 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
210 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
211 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
212 msStyle
|= WS_VSCROLL
;
213 m_windowStyle
|= wxTE_PROCESS_ENTER
;
217 // there is really no reason to not have this style for single line
219 msStyle
|= ES_AUTOHSCROLL
;
222 if ( m_windowStyle
& wxHSCROLL
)
223 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
225 if ( m_windowStyle
& wxTE_READONLY
)
226 msStyle
|= ES_READONLY
;
228 if ( m_windowStyle
& wxTE_PASSWORD
)
229 msStyle
|= ES_PASSWORD
;
231 if ( m_windowStyle
& wxTE_AUTO_SCROLL
)
232 msStyle
|= ES_AUTOHSCROLL
;
234 if ( m_windowStyle
& wxTE_NOHIDESEL
)
235 msStyle
|= ES_NOHIDESEL
;
237 // do create the control - either an EDIT or RICHEDIT
238 wxString windowClass
= wxT("EDIT");
241 if ( m_windowStyle
& wxTE_AUTO_URL
)
243 // automatic URL detection only works in RichEdit 2.0+
244 m_windowStyle
|= wxTE_RICH2
;
247 if ( m_windowStyle
& wxTE_RICH2
)
249 // using richedit 2.0 implies using wxTE_RICH
250 m_windowStyle
|= wxTE_RICH
;
253 // we need to load the richedit DLL before creating the rich edit control
254 if ( m_windowStyle
& wxTE_RICH
)
256 static bool s_errorGiven
= FALSE
;// MT-FIXME
258 // Which version do we need? Use 1.0 by default because it is much more
259 // like the the standard EDIT or 2.0 if explicitly requested, but use
260 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
262 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
263 // (and thus EDIT) much better than RichEdit 2.0 so we probably
264 // should use 3.0 if available as it is the best of both worlds -
265 // but as I can't test it right now I don't do it (VZ)
267 const int verRichEdit
= 2;
268 #else // !wxUSE_UNICODE
269 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
270 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
272 // only give the error msg once if the DLL can't be loaded
275 // try to load the RichEdit DLL (will do nothing if already done)
276 if ( !wxRichEditModule::Load(verRichEdit
) )
279 // try another version?
280 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
282 if ( !wxRichEditModule::Load(verRichEdit
) )
283 #endif // wxUSE_UNICODE
285 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
292 // have we managed to load any richedit version?
295 msStyle
|= ES_AUTOVSCROLL
;
297 m_verRichEdit
= verRichEdit
;
298 if ( m_verRichEdit
== 1 )
300 windowClass
= wxT("RICHEDIT");
304 #ifndef RICHEDIT_CLASS
305 wxString RICHEDIT_CLASS
;
306 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
308 RICHEDIT_CLASS
+= _T('W');
310 RICHEDIT_CLASS
+= _T('A');
311 #endif // Unicode/ANSI
312 #endif // !RICHEDIT_CLASS
314 windowClass
= RICHEDIT_CLASS
;
318 #endif // wxUSE_RICHEDIT
320 // we need to turn '\n's into "\r\n"s for the multiline controls
322 if ( m_windowStyle
& wxTE_MULTILINE
)
324 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
331 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
334 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
339 // have to enable events manually
340 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
342 if ( m_windowStyle
& wxTE_AUTO_URL
)
346 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
349 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
351 #endif // wxUSE_RICHEDIT
356 // Make sure the window style (etc.) reflects the HWND style (roughly)
357 void wxTextCtrl::AdoptAttributesFromHWND()
359 wxWindow::AdoptAttributesFromHWND();
361 HWND hWnd
= GetHwnd();
362 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
364 // retrieve the style to see whether this is an edit or richedit ctrl
366 wxString classname
= wxGetWindowClass(GetHWND());
368 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
375 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
377 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
382 #endif // wxUSE_RICHEDIT
384 if (style
& ES_MULTILINE
)
385 m_windowStyle
|= wxTE_MULTILINE
;
386 if (style
& ES_PASSWORD
)
387 m_windowStyle
|= wxTE_PASSWORD
;
388 if (style
& ES_READONLY
)
389 m_windowStyle
|= wxTE_READONLY
;
390 if (style
& ES_WANTRETURN
)
391 m_windowStyle
|= wxTE_PROCESS_ENTER
;
394 // ----------------------------------------------------------------------------
395 // set/get the controls text
396 // ----------------------------------------------------------------------------
398 wxString
wxTextCtrl::GetValue() const
400 // range 0..-1 is special for GetRange() and means to retrieve all text
401 return GetRange(0, -1);
404 wxString
wxTextCtrl::GetRange(long from
, long to
) const
408 if ( from
>= to
&& to
!= -1 )
410 // nothing to retrieve
417 int len
= GetWindowTextLength(GetHwnd());
420 // alloc one extra WORD as needed by the control
421 wxChar
*p
= str
.GetWriteBuf(++len
);
424 textRange
.chrg
.cpMin
= from
;
425 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
426 textRange
.lpstrText
= p
;
428 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
430 if ( m_verRichEdit
> 1 )
432 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
433 // neither Unix nor Windows style - convert it to something
437 if ( *p
== _T('\r') )
444 if ( m_verRichEdit
== 1 )
446 // convert to the canonical form - see comment below
447 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
450 //else: no text at all, leave the string empty
453 #endif // wxUSE_RICHEDIT
456 str
= wxGetWindowText(GetHWND());
458 // need only a range?
461 str
= str
.Mid(from
, to
- from
);
464 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
465 // canonical one (same one as above) for consistency with the other kinds
466 // of controls and, more importantly, with the other ports
467 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
473 void wxTextCtrl::SetValue(const wxString
& value
)
475 // if the text is long enough, it's faster to just set it instead of first
476 // comparing it with the old one (chances are that it will be different
477 // anyhow, this comparison is there to avoid flicker for small single-line
478 // edit controls mostly)
479 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
481 DoWriteText(value
, FALSE
/* not selection only */);
483 // mark the control as being not dirty - we changed its text, not the
487 // for compatibility, don't move the cursor when doing SetValue()
488 SetInsertionPoint(0);
492 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
494 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
498 wchar_t *wbuf
= (wchar_t *)buf
;
499 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
504 cb
-= sizeof(wchar_t);
505 (*pcb
) += sizeof(wchar_t);
508 *(const wchar_t **)dwCookie
= wpc
;
513 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from utils.cpp
515 #if wxUSE_UNICODE_MSLU
516 bool wxTextCtrl::StreamIn(const wxString
& value
,
517 wxFontEncoding
WXUNUSED(encoding
),
520 const wchar_t *wpc
= value
.c_str();
521 #else // !wxUSE_UNICODE_MSLU
522 bool wxTextCtrl::StreamIn(const wxString
& value
,
523 wxFontEncoding encoding
,
526 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
527 // text in the non default charset - otherwise it thinks it knows better
528 // than we do and always shows it in the default one
530 // first get the Windows code page for this encoding
531 long codepage
= wxEncodingToCodepage(encoding
);
532 if ( codepage
== -1 )
538 // next translate to Unicode using this code page
539 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
542 wxWCharBuffer
wchBuf(len
);
544 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
547 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
548 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
550 wxLogLastError(_T("MultiByteToWideChar"));
553 // finally, stream it in the control
554 const wchar_t *wpc
= wchBuf
;
555 #endif // wxUSE_UNICODE_MSLU
559 eds
.dwCookie
= (DWORD
)&wpc
;
560 // the cast below is needed for broken (very) old mingw32 headers
561 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
563 if ( !::SendMessage(GetHwnd(), EM_STREAMIN
,
566 (selectionOnly
? SFF_SELECTION
: 0),
567 (LPARAM
)&eds
) || eds
.dwError
)
569 wxLogLastError(_T("EM_STREAMIN"));
574 #endif // !wxUSE_WCHAR_T
579 #endif // wxUSE_RICHEDIT
581 void wxTextCtrl::WriteText(const wxString
& value
)
586 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
589 if ( m_windowStyle
& wxTE_MULTILINE
)
590 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
595 // there are several complications with the rich edit controls here
599 // first, ensure that the new text will be in the default style
600 if ( !m_defaultStyle
.IsDefault() )
603 GetSelection(&start
, &end
);
604 SetStyle(start
, end
, m_defaultStyle
);
607 #if wxUSE_UNICODE_MSLU
608 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
609 // but EM_STREAMIN works
610 if ( wxGetOsVersion() == wxWIN95
&& GetRichVersion() > 1 )
612 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
614 #endif // wxUSE_UNICODE_MSLU
617 // next check if the text we're inserting must be shown in a non
618 // default charset -- this only works for RichEdit > 1.0
619 if ( GetRichVersion() > 1 )
621 wxFont font
= m_defaultStyle
.GetFont();
627 wxFontEncoding encoding
= font
.GetEncoding();
628 if ( encoding
!= wxFONTENCODING_SYSTEM
)
630 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
634 #endif // !wxUSE_UNICODE
638 #endif // wxUSE_RICHEDIT
640 if ( !selectionOnly
)
642 SetSelection(-1, -1);
645 ::SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
651 void wxTextCtrl::AppendText(const wxString
& text
)
653 SetInsertionPointEnd();
658 void wxTextCtrl::Clear()
660 ::SetWindowText(GetHwnd(), wxT(""));
663 // ----------------------------------------------------------------------------
664 // Clipboard operations
665 // ----------------------------------------------------------------------------
667 void wxTextCtrl::Copy()
671 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
675 void wxTextCtrl::Cut()
679 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
683 void wxTextCtrl::Paste()
687 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
691 bool wxTextCtrl::CanCopy() const
693 // Can copy if there's a selection
695 GetSelection(&from
, &to
);
699 bool wxTextCtrl::CanCut() const
701 return CanCopy() && IsEditable();
704 bool wxTextCtrl::CanPaste() const
712 UINT cf
= 0; // 0 == any format
714 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
716 #endif // wxUSE_RICHEDIT
718 // Standard edit control: check for straight text on clipboard
719 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
722 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
725 return isTextAvailable
;
728 // ----------------------------------------------------------------------------
730 // ----------------------------------------------------------------------------
732 void wxTextCtrl::SetEditable(bool editable
)
734 HWND hWnd
= GetHwnd();
735 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
738 void wxTextCtrl::SetInsertionPoint(long pos
)
740 DoSetSelection(pos
, pos
);
743 void wxTextCtrl::SetInsertionPointEnd()
748 if ( m_verRichEdit
== 1 )
750 // we don't have to waste time calling GetLastPosition() in this case
753 else // !RichEdit 1.0
754 #endif // wxUSE_RICHEDIT
756 pos
= GetLastPosition();
759 SetInsertionPoint(pos
);
762 long wxTextCtrl::GetInsertionPoint() const
770 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
773 #endif // wxUSE_RICHEDIT
775 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
779 long wxTextCtrl::GetLastPosition() const
781 int numLines
= GetNumberOfLines();
782 long posStartLastLine
= XYToPosition(0, numLines
- 1);
784 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
786 return posStartLastLine
+ lenLastLine
;
789 // If the return values from and to are the same, there is no
791 void wxTextCtrl::GetSelection(long* from
, long* to
) const
797 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
799 *from
= charRange
.cpMin
;
800 *to
= charRange
.cpMax
;
803 #endif // !wxUSE_RICHEDIT
805 DWORD dwStart
, dwEnd
;
806 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
813 bool wxTextCtrl::IsEditable() const
815 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
817 return (style
& ES_READONLY
) == 0;
820 // ----------------------------------------------------------------------------
822 // ----------------------------------------------------------------------------
824 void wxTextCtrl::SetSelection(long from
, long to
)
826 // if from and to are both -1, it means (in wxWindows) that all text should
827 // be selected - translate into Windows convention
828 if ( (from
== -1) && (to
== -1) )
834 DoSetSelection(from
, to
);
837 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
839 HWND hWnd
= GetHwnd();
848 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
851 #endif // wxUSE_RICHEDIT
853 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
858 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
861 // WPARAM is 0: selection is scrolled into view
862 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
866 // ----------------------------------------------------------------------------
868 // ----------------------------------------------------------------------------
870 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
872 // Set selection and remove it
873 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
875 SendMessage(GetHwnd(), EM_REPLACESEL
,
881 (LPARAM
)value
.c_str());
884 void wxTextCtrl::Remove(long from
, long to
)
886 Replace(from
, to
, _T(""));
889 bool wxTextCtrl::LoadFile(const wxString
& file
)
891 if ( wxTextCtrlBase::LoadFile(file
) )
893 // update the size limit if needed
902 bool wxTextCtrl::IsModified() const
904 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
907 // Makes 'unmodified'
908 void wxTextCtrl::DiscardEdits()
910 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
913 int wxTextCtrl::GetNumberOfLines() const
915 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
918 long wxTextCtrl::XYToPosition(long x
, long y
) const
920 // This gets the char index for the _beginning_ of this line
921 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
923 return charIndex
+ x
;
926 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
928 HWND hWnd
= GetHwnd();
930 // This gets the line number containing the character
935 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
938 #endif // wxUSE_RICHEDIT
940 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
949 // This gets the char index for the _beginning_ of this line
950 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
951 if ( charIndex
== -1 )
956 // The X position must therefore be the different between pos and charIndex
958 *x
= pos
- charIndex
;
965 void wxTextCtrl::ShowPosition(long pos
)
967 HWND hWnd
= GetHwnd();
969 // To scroll to a position, we pass the number of lines and characters
970 // to scroll *by*. This means that we need to:
971 // (1) Find the line position of the current line.
972 // (2) Find the line position of pos.
973 // (3) Scroll by (pos - current).
974 // For now, ignore the horizontal scrolling.
976 // Is this where scrolling is relative to - the line containing the caret?
977 // Or is the first visible line??? Try first visible line.
978 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
980 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
982 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
984 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
986 if (linesToScroll
!= 0)
987 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
990 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
992 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
995 int wxTextCtrl::GetLineLength(long lineNo
) const
997 long pos
= XYToPosition(0, lineNo
);
999 return GetLengthOfLineContainingPos(pos
);
1002 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1004 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1006 // there must be at least enough place for the length WORD in the
1008 len
+= sizeof(WORD
);
1011 wxChar
*buf
= str
.GetWriteBuf(len
);
1013 *(WORD
*)buf
= (WORD
)len
;
1014 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1017 str
.UngetWriteBuf(len
);
1022 void wxTextCtrl::SetMaxLength(unsigned long len
)
1024 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1027 // ----------------------------------------------------------------------------
1029 // ----------------------------------------------------------------------------
1031 void wxTextCtrl::Undo()
1035 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1039 void wxTextCtrl::Redo()
1043 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1044 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1048 bool wxTextCtrl::CanUndo() const
1050 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1053 bool wxTextCtrl::CanRedo() const
1055 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1058 // ----------------------------------------------------------------------------
1059 // implemenation details
1060 // ----------------------------------------------------------------------------
1062 void wxTextCtrl::Command(wxCommandEvent
& event
)
1064 SetValue(event
.GetString());
1065 ProcessCommand (event
);
1068 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1070 // By default, load the first file into the text window.
1071 if (event
.GetNumberOfFiles() > 0)
1073 LoadFile(event
.GetFiles()[0]);
1077 // ----------------------------------------------------------------------------
1078 // kbd input processing
1079 // ----------------------------------------------------------------------------
1081 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1083 MSG
*msg
= (MSG
*)pMsg
;
1085 // check for our special keys here: if we don't do it and the parent frame
1086 // uses them as accelerators, they wouldn't work at all, so we disable
1087 // usual preprocessing for them
1088 if ( msg
->message
== WM_KEYDOWN
)
1090 WORD vkey
= (WORD
) msg
->wParam
;
1091 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1093 if ( vkey
== VK_BACK
)
1098 if ( wxIsCtrlDown() )
1112 else if ( wxIsShiftDown() )
1114 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1120 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1123 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1125 switch ( event
.KeyCode() )
1128 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1130 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1131 InitCommandEvent(event
);
1132 event
.SetString(GetValue());
1133 if ( GetEventHandler()->ProcessEvent(event
) )
1136 //else: multiline controls need Enter for themselves
1141 // always produce navigation event - even if we process TAB
1142 // ourselves the fact that we got here means that the user code
1143 // decided to skip processing of this TAB - probably to let it
1144 // do its default job.
1146 wxNavigationKeyEvent eventNav
;
1147 eventNav
.SetDirection(!event
.ShiftDown());
1148 eventNav
.SetWindowChange(event
.ControlDown());
1149 eventNav
.SetEventObject(this);
1151 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1157 // no, we didn't process it
1161 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1163 // we always want the characters and the arrows
1164 if ( nMsg
== WM_GETDLGCODE
)
1166 // we always want the chars and the arrows
1167 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1169 // we may have several different cases:
1170 // 1. normal case: both TAB and ENTER are used for dialog navigation
1171 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
1172 // control in the dialog
1173 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
1174 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
1176 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
1177 lDlgCode
|= DLGC_WANTMESSAGE
;
1178 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
1179 lDlgCode
|= DLGC_WANTTAB
;
1184 return wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1187 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1194 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1197 event
.SetEventObject( this );
1198 GetEventHandler()->ProcessEvent(event
);
1204 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1205 InitCommandEvent(event
);
1206 event
.SetString(GetValue());
1207 ProcessCommand(event
);
1212 // the text size limit has been hit - increase it
1213 if ( !AdjustSpaceLimit() )
1215 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1216 InitCommandEvent(event
);
1217 event
.SetString(GetValue());
1218 ProcessCommand(event
);
1222 // the other notification messages are not processed
1236 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1242 WXUINT
WXUNUSED(message
),
1243 WXWPARAM
WXUNUSED(wParam
),
1244 WXLPARAM
WXUNUSED(lParam
)
1251 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1252 return (WXHBRUSH
) hbrush
;
1254 #endif // wxUSE_CTL3D
1257 if (GetParent()->GetTransparentBackground())
1258 SetBkMode(hdc
, TRANSPARENT
);
1260 SetBkMode(hdc
, OPAQUE
);
1262 wxColour colBack
= GetBackgroundColour();
1264 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1265 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1267 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1268 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1270 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1272 return (WXHBRUSH
)brush
->GetResourceHandle();
1275 // In WIN16, need to override normal erasing because
1276 // Ctl3D doesn't use the wxWindows background colour.
1278 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1280 wxColour
col(m_backgroundColour
);
1284 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1288 ::GetClientRect(GetHwnd(), &rect
);
1290 COLORREF ref
= wxColourToRGB(col
);
1291 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1293 wxLogLastError(wxT("CreateSolidBrush"));
1295 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1297 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1299 ::FillRect(hdc
, &rect
, hBrush
);
1300 ::DeleteObject(hBrush
);
1301 ::SetMapMode(hdc
, mode
);
1306 bool wxTextCtrl::AdjustSpaceLimit()
1309 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1311 // HACK: we try to automatically extend the limit for the amount of text
1312 // to allow (interactively) entering more than 64Kb of text under
1313 // Win9x but we shouldn't reset the text limit which was previously
1314 // set explicitly with SetMaxLength()
1316 // we could solve this by storing the limit we set in wxTextCtrl but
1317 // to save space we prefer to simply test here the actual limit
1318 // value: we consider that SetMaxLength() can only be called for
1320 if ( limit
< 0x8000 )
1322 // we've got more text than limit set by SetMaxLength()
1326 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1329 limit
= len
+ 0x8000; // 32Kb
1334 // as a nice side effect, this also allows passing limit > 64Kb
1335 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1338 #endif // wxUSE_RICHEDIT
1340 if ( limit
> 0xffff )
1342 // this will set it to a platform-dependent maximum (much more
1343 // than 64Kb under NT)
1347 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1352 // we changed the limit
1356 bool wxTextCtrl::AcceptsFocus() const
1358 // we don't want focus if we can't be edited
1359 return IsEditable() && wxControl::AcceptsFocus();
1362 wxSize
wxTextCtrl::DoGetBestSize() const
1365 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1367 int wText
= DEFAULT_ITEM_WIDTH
;
1369 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1370 if ( m_windowStyle
& wxTE_MULTILINE
)
1372 hText
*= wxMax(GetNumberOfLines(), 5);
1374 //else: for single line control everything is ok
1376 return wxSize(wText
, hText
);
1379 // ----------------------------------------------------------------------------
1380 // standard handlers for standard edit menu events
1381 // ----------------------------------------------------------------------------
1383 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1388 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1393 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1398 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1403 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1408 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1410 event
.Enable( CanCut() );
1413 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1415 event
.Enable( CanCopy() );
1418 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1420 event
.Enable( CanPaste() );
1423 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1425 event
.Enable( CanUndo() );
1428 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1430 event
.Enable( CanRedo() );
1433 // the rest of the file only deals with the rich edit controls
1436 // ----------------------------------------------------------------------------
1437 // EN_LINK processing
1438 // ----------------------------------------------------------------------------
1440 bool wxTextCtrl::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
*result
)
1442 NMHDR
*hdr
= (NMHDR
* )lParam
;
1443 if ( hdr
->code
== EN_LINK
)
1445 ENLINK
*enlink
= (ENLINK
*)hdr
;
1447 switch ( enlink
->msg
)
1450 // ok, so it is hardcoded - do we really nee to customize it?
1451 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1456 case WM_LBUTTONDOWN
:
1458 case WM_LBUTTONDBLCLK
:
1459 case WM_RBUTTONDOWN
:
1461 case WM_RBUTTONDBLCLK
:
1462 // send a mouse event
1464 static const wxEventType eventsMouse
[] =
1475 // the event ids are consecutive
1477 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1479 InitMouseEvent(evtMouse
,
1480 GET_X_LPARAM(enlink
->lParam
),
1481 GET_Y_LPARAM(enlink
->lParam
),
1484 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1486 enlink
->chrg
.cpMax
);
1488 InitCommandEvent(event
);
1490 *result
= ProcessCommand(event
);
1502 // ----------------------------------------------------------------------------
1503 // colour setting for the rich edit controls
1504 // ----------------------------------------------------------------------------
1506 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1508 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1510 // colour didn't really change
1516 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1517 // EM_SETBKGNDCOLOR additionally
1518 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1524 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1526 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1528 // colour didn't really change
1534 // change the colour of everything
1537 cf
.cbSize
= sizeof(cf
);
1538 cf
.dwMask
= CFM_COLOR
;
1539 cf
.crTextColor
= wxColourToRGB(colour
);
1540 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1546 // ----------------------------------------------------------------------------
1547 // styling support for rich edit controls
1548 // ----------------------------------------------------------------------------
1550 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1554 // can't do it with normal text control
1558 // the richedit 1.0 doesn't handle setting background colour, so don't
1559 // even try to do anything if it's the only thing we want to change
1560 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() )
1562 // nothing to do: return TRUE if there was really nothing to do and
1563 // FALSE if we failed to set bg colour
1564 return !style
.HasBackgroundColour();
1567 // order the range if needed
1575 // we can only change the format of the selection, so select the range we
1576 // want and restore the old selection later
1577 long startOld
, endOld
;
1578 GetSelection(&startOld
, &endOld
);
1580 // but do we really have to change the selection?
1581 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1585 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1588 // initialize CHARFORMAT struct
1597 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1598 // CHARFORMAT in that case
1600 if ( m_verRichEdit
== 1 )
1602 // this is the only thing the control is going to grok
1603 cf
.cbSize
= sizeof(CHARFORMAT
);
1608 // CHARFORMAT or CHARFORMAT2
1609 cf
.cbSize
= sizeof(cf
);
1612 if ( style
.HasFont() )
1614 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1615 // but using it doesn't seem to hurt neither so leaving it for now
1617 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1618 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1620 // fill in data from LOGFONT but recalculate lfHeight because we need
1621 // the real height in twips and not the negative number which
1622 // wxFillLogFont() returns (this is correct in general and works with
1623 // the Windows font mapper, but not here)
1625 wxFillLogFont(&lf
, &style
.GetFont());
1626 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1627 cf
.bCharSet
= lf
.lfCharSet
;
1628 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1629 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1631 // also deal with underline/italic/bold attributes: note that we must
1632 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1633 // style to allow clearing it
1636 cf
.dwEffects
|= CFE_ITALIC
;
1639 if ( lf
.lfWeight
== FW_BOLD
)
1641 cf
.dwEffects
|= CFE_BOLD
;
1644 if ( lf
.lfUnderline
)
1646 cf
.dwEffects
|= CFE_UNDERLINE
;
1649 // strikeout fonts are not supported by wxWindows
1652 if ( style
.HasTextColour() )
1654 cf
.dwMask
|= CFM_COLOR
;
1655 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1659 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1661 cf
.dwMask
|= CFM_BACKCOLOR
;
1662 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1664 #endif // wxUSE_RICHEDIT2
1666 // do format the selection
1667 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1668 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1671 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1676 // restore the original selection
1677 DoSetSelection(startOld
, endOld
, FALSE
);
1683 // ----------------------------------------------------------------------------
1685 // ----------------------------------------------------------------------------
1687 bool wxRichEditModule::OnInit()
1689 // don't do anything - we will load it when needed
1693 void wxRichEditModule::OnExit()
1695 for ( int i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
1697 if ( ms_hRichEdit
[i
] )
1699 ::FreeLibrary(ms_hRichEdit
[i
]);
1705 bool wxRichEditModule::Load(int version
)
1707 // we don't support loading richedit 3.0 as I don't know how to distinguish
1708 // it from 2.0 anyhow
1709 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
1710 _T("incorrect richedit control version requested") );
1712 // make it the index in the array
1715 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
1717 // we had already tried to load it and failed
1721 if ( ms_hRichEdit
[version
] )
1723 // we've already got this one
1727 wxString dllname
= version
? _T("riched20") : _T("riched32");
1728 dllname
+= _T(".dll");
1730 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
1732 if ( !ms_hRichEdit
[version
] )
1734 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1736 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
1744 #endif // wxUSE_RICHEDIT
1746 #endif // wxUSE_TEXTCTRL