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 // have to enable events manually
304 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
306 if ( m_windowStyle
& wxTE_AUTO_URL
)
310 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
313 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
315 #endif // wxUSE_RICHEDIT
320 // Make sure the window style (etc.) reflects the HWND style (roughly)
321 void wxTextCtrl::AdoptAttributesFromHWND()
323 wxWindow::AdoptAttributesFromHWND();
325 HWND hWnd
= GetHwnd();
326 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
328 // retrieve the style to see whether this is an edit or richedit ctrl
330 wxString classname
= wxGetWindowClass(GetHWND());
332 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
339 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
341 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
346 #endif // wxUSE_RICHEDIT
348 if (style
& ES_MULTILINE
)
349 m_windowStyle
|= wxTE_MULTILINE
;
350 if (style
& ES_PASSWORD
)
351 m_windowStyle
|= wxTE_PASSWORD
;
352 if (style
& ES_READONLY
)
353 m_windowStyle
|= wxTE_READONLY
;
354 if (style
& ES_WANTRETURN
)
355 m_windowStyle
|= wxTE_PROCESS_ENTER
;
358 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
360 // default border for the text controls is the sunken one
361 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
363 style
|= wxBORDER_SUNKEN
;
366 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
371 if ( style
& wxTE_MULTILINE
)
373 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
374 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
376 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
377 if ( !(style
& wxTE_NO_VSCROLL
) )
378 msStyle
|= WS_VSCROLL
;
380 style
|= wxTE_PROCESS_ENTER
;
384 // there is really no reason to not have this style for single line
386 msStyle
|= ES_AUTOHSCROLL
;
389 if ( style
& wxHSCROLL
)
390 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
392 if ( style
& wxTE_READONLY
)
393 msStyle
|= ES_READONLY
;
395 if ( style
& wxTE_PASSWORD
)
396 msStyle
|= ES_PASSWORD
;
398 if ( style
& wxTE_AUTO_SCROLL
)
399 msStyle
|= ES_AUTOHSCROLL
;
401 if ( style
& wxTE_NOHIDESEL
)
402 msStyle
|= ES_NOHIDESEL
;
407 void wxTextCtrl::SetWindowStyleFlag(long style
)
410 // we have to deal with some styles separately because they can't be
411 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
412 // using richedit-specific EM_SETOPTIONS
414 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
416 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
418 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
419 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
421 #endif // wxUSE_RICHEDIT
423 wxControl::SetWindowStyleFlag(style
);
426 // ----------------------------------------------------------------------------
427 // set/get the controls text
428 // ----------------------------------------------------------------------------
430 wxString
wxTextCtrl::GetValue() const
432 // range 0..-1 is special for GetRange() and means to retrieve all text
433 return GetRange(0, -1);
436 wxString
wxTextCtrl::GetRange(long from
, long to
) const
440 if ( from
>= to
&& to
!= -1 )
442 // nothing to retrieve
449 int len
= GetWindowTextLength(GetHwnd());
452 // alloc one extra WORD as needed by the control
453 wxChar
*p
= str
.GetWriteBuf(++len
);
456 textRange
.chrg
.cpMin
= from
;
457 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
458 textRange
.lpstrText
= p
;
460 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
462 if ( m_verRichEdit
> 1 )
464 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
465 // neither Unix nor Windows style - convert it to something
469 if ( *p
== _T('\r') )
476 if ( m_verRichEdit
== 1 )
478 // convert to the canonical form - see comment below
479 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
482 //else: no text at all, leave the string empty
485 #endif // wxUSE_RICHEDIT
488 str
= wxGetWindowText(GetHWND());
490 // need only a range?
493 str
= str
.Mid(from
, to
- from
);
496 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
497 // canonical one (same one as above) for consistency with the other kinds
498 // of controls and, more importantly, with the other ports
499 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
505 void wxTextCtrl::SetValue(const wxString
& value
)
507 // if the text is long enough, it's faster to just set it instead of first
508 // comparing it with the old one (chances are that it will be different
509 // anyhow, this comparison is there to avoid flicker for small single-line
510 // edit controls mostly)
511 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
513 DoWriteText(value
, FALSE
/* not selection only */);
515 // mark the control as being not dirty - we changed its text, not the
519 // for compatibility, don't move the cursor when doing SetValue()
520 SetInsertionPoint(0);
524 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
526 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
530 wchar_t *wbuf
= (wchar_t *)buf
;
531 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
536 cb
-= sizeof(wchar_t);
537 (*pcb
) += sizeof(wchar_t);
540 *(const wchar_t **)dwCookie
= wpc
;
545 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from utils.cpp
547 #if wxUSE_UNICODE_MSLU
548 bool wxTextCtrl::StreamIn(const wxString
& value
,
549 wxFontEncoding
WXUNUSED(encoding
),
552 const wchar_t *wpc
= value
.c_str();
553 #else // !wxUSE_UNICODE_MSLU
554 bool wxTextCtrl::StreamIn(const wxString
& value
,
555 wxFontEncoding encoding
,
558 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
559 // text in the non default charset - otherwise it thinks it knows better
560 // than we do and always shows it in the default one
562 // first get the Windows code page for this encoding
563 long codepage
= wxEncodingToCodepage(encoding
);
564 if ( codepage
== -1 )
570 // next translate to Unicode using this code page
571 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
574 wxWCharBuffer
wchBuf(len
);
576 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
579 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
580 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
582 wxLogLastError(_T("MultiByteToWideChar"));
585 // finally, stream it in the control
586 const wchar_t *wpc
= wchBuf
;
587 #endif // wxUSE_UNICODE_MSLU
591 eds
.dwCookie
= (DWORD
)&wpc
;
592 // the cast below is needed for broken (very) old mingw32 headers
593 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
595 if ( !::SendMessage(GetHwnd(), EM_STREAMIN
,
598 (selectionOnly
? SFF_SELECTION
: 0),
599 (LPARAM
)&eds
) || eds
.dwError
)
601 wxLogLastError(_T("EM_STREAMIN"));
606 #endif // !wxUSE_WCHAR_T
611 #endif // wxUSE_RICHEDIT
613 void wxTextCtrl::WriteText(const wxString
& value
)
618 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
621 if ( m_windowStyle
& wxTE_MULTILINE
)
622 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
627 // there are several complications with the rich edit controls here
631 // first, ensure that the new text will be in the default style
632 if ( !m_defaultStyle
.IsDefault() )
635 GetSelection(&start
, &end
);
636 SetStyle(start
, end
, m_defaultStyle
);
639 #if wxUSE_UNICODE_MSLU
640 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
641 // but EM_STREAMIN works
642 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
644 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
646 #endif // wxUSE_UNICODE_MSLU
649 // next check if the text we're inserting must be shown in a non
650 // default charset -- this only works for RichEdit > 1.0
651 if ( GetRichVersion() > 1 )
653 wxFont font
= m_defaultStyle
.GetFont();
659 wxFontEncoding encoding
= font
.GetEncoding();
660 if ( encoding
!= wxFONTENCODING_SYSTEM
)
662 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
666 #endif // !wxUSE_UNICODE
670 #endif // wxUSE_RICHEDIT
672 if ( !selectionOnly
)
674 SetSelection(-1, -1);
677 ::SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
683 void wxTextCtrl::AppendText(const wxString
& text
)
685 SetInsertionPointEnd();
690 void wxTextCtrl::Clear()
692 ::SetWindowText(GetHwnd(), wxT(""));
695 // ----------------------------------------------------------------------------
696 // Clipboard operations
697 // ----------------------------------------------------------------------------
699 void wxTextCtrl::Copy()
703 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
707 void wxTextCtrl::Cut()
711 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
715 void wxTextCtrl::Paste()
719 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
723 bool wxTextCtrl::CanCopy() const
725 // Can copy if there's a selection
727 GetSelection(&from
, &to
);
731 bool wxTextCtrl::CanCut() const
733 return CanCopy() && IsEditable();
736 bool wxTextCtrl::CanPaste() const
744 UINT cf
= 0; // 0 == any format
746 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
748 #endif // wxUSE_RICHEDIT
750 // Standard edit control: check for straight text on clipboard
751 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
754 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
757 return isTextAvailable
;
760 // ----------------------------------------------------------------------------
762 // ----------------------------------------------------------------------------
764 void wxTextCtrl::SetEditable(bool editable
)
766 HWND hWnd
= GetHwnd();
767 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
770 void wxTextCtrl::SetInsertionPoint(long pos
)
772 DoSetSelection(pos
, pos
);
775 void wxTextCtrl::SetInsertionPointEnd()
780 if ( m_verRichEdit
== 1 )
782 // we don't have to waste time calling GetLastPosition() in this case
785 else // !RichEdit 1.0
786 #endif // wxUSE_RICHEDIT
788 pos
= GetLastPosition();
791 SetInsertionPoint(pos
);
794 long wxTextCtrl::GetInsertionPoint() const
802 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
805 #endif // wxUSE_RICHEDIT
807 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
811 long wxTextCtrl::GetLastPosition() const
813 int numLines
= GetNumberOfLines();
814 long posStartLastLine
= XYToPosition(0, numLines
- 1);
816 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
818 return posStartLastLine
+ lenLastLine
;
821 // If the return values from and to are the same, there is no
823 void wxTextCtrl::GetSelection(long* from
, long* to
) const
829 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
831 *from
= charRange
.cpMin
;
832 *to
= charRange
.cpMax
;
835 #endif // !wxUSE_RICHEDIT
837 DWORD dwStart
, dwEnd
;
838 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
845 bool wxTextCtrl::IsEditable() const
847 // strangely enough, we may be called before the control is created: our
848 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
853 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
855 return (style
& ES_READONLY
) == 0;
858 // ----------------------------------------------------------------------------
860 // ----------------------------------------------------------------------------
862 void wxTextCtrl::SetSelection(long from
, long to
)
864 // if from and to are both -1, it means (in wxWindows) that all text should
865 // be selected - translate into Windows convention
866 if ( (from
== -1) && (to
== -1) )
872 DoSetSelection(from
, to
);
875 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
877 HWND hWnd
= GetHwnd();
886 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
889 #endif // wxUSE_RICHEDIT
891 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
896 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
899 // WPARAM is 0: selection is scrolled into view
900 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
904 // ----------------------------------------------------------------------------
906 // ----------------------------------------------------------------------------
908 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
910 // Set selection and remove it
911 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
913 SendMessage(GetHwnd(), EM_REPLACESEL
,
919 (LPARAM
)value
.c_str());
922 void wxTextCtrl::Remove(long from
, long to
)
924 Replace(from
, to
, _T(""));
927 bool wxTextCtrl::LoadFile(const wxString
& file
)
929 if ( wxTextCtrlBase::LoadFile(file
) )
931 // update the size limit if needed
940 bool wxTextCtrl::IsModified() const
942 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
945 // Makes 'unmodified'
946 void wxTextCtrl::DiscardEdits()
948 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
951 int wxTextCtrl::GetNumberOfLines() const
953 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
956 long wxTextCtrl::XYToPosition(long x
, long y
) const
958 // This gets the char index for the _beginning_ of this line
959 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
961 return charIndex
+ x
;
964 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
966 HWND hWnd
= GetHwnd();
968 // This gets the line number containing the character
973 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
976 #endif // wxUSE_RICHEDIT
978 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
987 // This gets the char index for the _beginning_ of this line
988 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
989 if ( charIndex
== -1 )
994 // The X position must therefore be the different between pos and charIndex
996 *x
= pos
- charIndex
;
1003 void wxTextCtrl::ShowPosition(long pos
)
1005 HWND hWnd
= GetHwnd();
1007 // To scroll to a position, we pass the number of lines and characters
1008 // to scroll *by*. This means that we need to:
1009 // (1) Find the line position of the current line.
1010 // (2) Find the line position of pos.
1011 // (3) Scroll by (pos - current).
1012 // For now, ignore the horizontal scrolling.
1014 // Is this where scrolling is relative to - the line containing the caret?
1015 // Or is the first visible line??? Try first visible line.
1016 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1018 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1020 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1022 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1024 if (linesToScroll
!= 0)
1025 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1028 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1030 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1033 int wxTextCtrl::GetLineLength(long lineNo
) const
1035 long pos
= XYToPosition(0, lineNo
);
1037 return GetLengthOfLineContainingPos(pos
);
1040 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1042 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1044 // there must be at least enough place for the length WORD in the
1046 len
+= sizeof(WORD
);
1049 wxChar
*buf
= str
.GetWriteBuf(len
);
1051 *(WORD
*)buf
= (WORD
)len
;
1052 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1055 str
.UngetWriteBuf(len
);
1060 void wxTextCtrl::SetMaxLength(unsigned long len
)
1062 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1065 // ----------------------------------------------------------------------------
1067 // ----------------------------------------------------------------------------
1069 void wxTextCtrl::Undo()
1073 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1077 void wxTextCtrl::Redo()
1081 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1082 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1086 bool wxTextCtrl::CanUndo() const
1088 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1091 bool wxTextCtrl::CanRedo() const
1093 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1096 // ----------------------------------------------------------------------------
1097 // implemenation details
1098 // ----------------------------------------------------------------------------
1100 void wxTextCtrl::Command(wxCommandEvent
& event
)
1102 SetValue(event
.GetString());
1103 ProcessCommand (event
);
1106 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1108 // By default, load the first file into the text window.
1109 if (event
.GetNumberOfFiles() > 0)
1111 LoadFile(event
.GetFiles()[0]);
1115 // ----------------------------------------------------------------------------
1116 // kbd input processing
1117 // ----------------------------------------------------------------------------
1119 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1121 MSG
*msg
= (MSG
*)pMsg
;
1123 // check for our special keys here: if we don't do it and the parent frame
1124 // uses them as accelerators, they wouldn't work at all, so we disable
1125 // usual preprocessing for them
1126 if ( msg
->message
== WM_KEYDOWN
)
1128 WORD vkey
= (WORD
) msg
->wParam
;
1129 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1131 if ( vkey
== VK_BACK
)
1136 if ( wxIsCtrlDown() )
1150 else if ( wxIsShiftDown() )
1152 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1158 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1161 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1163 switch ( event
.KeyCode() )
1166 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1168 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1169 InitCommandEvent(event
);
1170 event
.SetString(GetValue());
1171 if ( GetEventHandler()->ProcessEvent(event
) )
1174 //else: multiline controls need Enter for themselves
1179 // always produce navigation event - even if we process TAB
1180 // ourselves the fact that we got here means that the user code
1181 // decided to skip processing of this TAB - probably to let it
1182 // do its default job.
1184 wxNavigationKeyEvent eventNav
;
1185 eventNav
.SetDirection(!event
.ShiftDown());
1186 eventNav
.SetWindowChange(event
.ControlDown());
1187 eventNav
.SetEventObject(this);
1189 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1195 // no, we didn't process it
1199 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1201 // we always want the characters and the arrows
1202 if ( nMsg
== WM_GETDLGCODE
)
1204 // we always want the chars and the arrows
1205 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1207 // we may have several different cases:
1208 // 1. normal case: both TAB and ENTER are used for dialog navigation
1209 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
1210 // control in the dialog
1211 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
1212 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
1214 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
1215 lDlgCode
|= DLGC_WANTMESSAGE
;
1216 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
1217 lDlgCode
|= DLGC_WANTTAB
;
1222 return wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1225 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1232 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1235 event
.SetEventObject( this );
1236 GetEventHandler()->ProcessEvent(event
);
1242 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1243 InitCommandEvent(event
);
1244 event
.SetString(GetValue());
1245 ProcessCommand(event
);
1250 // the text size limit has been hit - increase it
1251 if ( !AdjustSpaceLimit() )
1253 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1254 InitCommandEvent(event
);
1255 event
.SetString(GetValue());
1256 ProcessCommand(event
);
1260 // the other notification messages are not processed
1274 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1280 WXUINT
WXUNUSED(message
),
1281 WXWPARAM
WXUNUSED(wParam
),
1282 WXLPARAM
WXUNUSED(lParam
)
1289 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1290 return (WXHBRUSH
) hbrush
;
1292 #endif // wxUSE_CTL3D
1295 if (GetParent()->GetTransparentBackground())
1296 SetBkMode(hdc
, TRANSPARENT
);
1298 SetBkMode(hdc
, OPAQUE
);
1300 wxColour colBack
= GetBackgroundColour();
1302 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1303 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1305 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1306 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1308 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1310 return (WXHBRUSH
)brush
->GetResourceHandle();
1313 // In WIN16, need to override normal erasing because
1314 // Ctl3D doesn't use the wxWindows background colour.
1316 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1318 wxColour
col(m_backgroundColour
);
1322 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1326 ::GetClientRect(GetHwnd(), &rect
);
1328 COLORREF ref
= wxColourToRGB(col
);
1329 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1331 wxLogLastError(wxT("CreateSolidBrush"));
1333 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1335 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1337 ::FillRect(hdc
, &rect
, hBrush
);
1338 ::DeleteObject(hBrush
);
1339 ::SetMapMode(hdc
, mode
);
1344 bool wxTextCtrl::AdjustSpaceLimit()
1347 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1349 // HACK: we try to automatically extend the limit for the amount of text
1350 // to allow (interactively) entering more than 64Kb of text under
1351 // Win9x but we shouldn't reset the text limit which was previously
1352 // set explicitly with SetMaxLength()
1354 // we could solve this by storing the limit we set in wxTextCtrl but
1355 // to save space we prefer to simply test here the actual limit
1356 // value: we consider that SetMaxLength() can only be called for
1358 if ( limit
< 0x8000 )
1360 // we've got more text than limit set by SetMaxLength()
1364 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1367 limit
= len
+ 0x8000; // 32Kb
1372 // as a nice side effect, this also allows passing limit > 64Kb
1373 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1376 #endif // wxUSE_RICHEDIT
1378 if ( limit
> 0xffff )
1380 // this will set it to a platform-dependent maximum (much more
1381 // than 64Kb under NT)
1385 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1390 // we changed the limit
1394 bool wxTextCtrl::AcceptsFocus() const
1396 // we don't want focus if we can't be edited
1397 return IsEditable() && wxControl::AcceptsFocus();
1400 wxSize
wxTextCtrl::DoGetBestSize() const
1403 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1405 int wText
= DEFAULT_ITEM_WIDTH
;
1407 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1408 if ( m_windowStyle
& wxTE_MULTILINE
)
1410 hText
*= wxMax(GetNumberOfLines(), 5);
1412 //else: for single line control everything is ok
1414 return wxSize(wText
, hText
);
1417 // ----------------------------------------------------------------------------
1418 // standard handlers for standard edit menu events
1419 // ----------------------------------------------------------------------------
1421 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1426 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1431 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1436 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1441 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1446 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1448 event
.Enable( CanCut() );
1451 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1453 event
.Enable( CanCopy() );
1456 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1458 event
.Enable( CanPaste() );
1461 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1463 event
.Enable( CanUndo() );
1466 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1468 event
.Enable( CanRedo() );
1471 // the rest of the file only deals with the rich edit controls
1474 // ----------------------------------------------------------------------------
1475 // EN_LINK processing
1476 // ----------------------------------------------------------------------------
1478 bool wxTextCtrl::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
*result
)
1480 NMHDR
*hdr
= (NMHDR
* )lParam
;
1481 if ( hdr
->code
== EN_LINK
)
1483 ENLINK
*enlink
= (ENLINK
*)hdr
;
1485 switch ( enlink
->msg
)
1488 // ok, so it is hardcoded - do we really nee to customize it?
1489 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1493 case WM_LBUTTONDOWN
:
1495 case WM_LBUTTONDBLCLK
:
1496 case WM_RBUTTONDOWN
:
1498 case WM_RBUTTONDBLCLK
:
1499 // send a mouse event
1501 static const wxEventType eventsMouse
[] =
1512 // the event ids are consecutive
1514 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1516 InitMouseEvent(evtMouse
,
1517 GET_X_LPARAM(enlink
->lParam
),
1518 GET_Y_LPARAM(enlink
->lParam
),
1521 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1523 enlink
->chrg
.cpMax
);
1525 InitCommandEvent(event
);
1527 *result
= ProcessCommand(event
);
1539 // ----------------------------------------------------------------------------
1540 // colour setting for the rich edit controls
1541 // ----------------------------------------------------------------------------
1543 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1545 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1547 // colour didn't really change
1553 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1554 // EM_SETBKGNDCOLOR additionally
1555 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1561 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1563 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1565 // colour didn't really change
1571 // change the colour of everything
1574 cf
.cbSize
= sizeof(cf
);
1575 cf
.dwMask
= CFM_COLOR
;
1576 cf
.crTextColor
= wxColourToRGB(colour
);
1577 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1583 // ----------------------------------------------------------------------------
1584 // styling support for rich edit controls
1585 // ----------------------------------------------------------------------------
1587 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1591 // can't do it with normal text control
1595 // the richedit 1.0 doesn't handle setting background colour, so don't
1596 // even try to do anything if it's the only thing we want to change
1597 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() )
1599 // nothing to do: return TRUE if there was really nothing to do and
1600 // FALSE if we failed to set bg colour
1601 return !style
.HasBackgroundColour();
1604 // order the range if needed
1612 // we can only change the format of the selection, so select the range we
1613 // want and restore the old selection later
1614 long startOld
, endOld
;
1615 GetSelection(&startOld
, &endOld
);
1617 // but do we really have to change the selection?
1618 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1622 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1625 // initialize CHARFORMAT struct
1634 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1635 // CHARFORMAT in that case
1637 if ( m_verRichEdit
== 1 )
1639 // this is the only thing the control is going to grok
1640 cf
.cbSize
= sizeof(CHARFORMAT
);
1645 // CHARFORMAT or CHARFORMAT2
1646 cf
.cbSize
= sizeof(cf
);
1649 if ( style
.HasFont() )
1651 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1652 // but using it doesn't seem to hurt neither so leaving it for now
1654 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1655 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1657 // fill in data from LOGFONT but recalculate lfHeight because we need
1658 // the real height in twips and not the negative number which
1659 // wxFillLogFont() returns (this is correct in general and works with
1660 // the Windows font mapper, but not here)
1662 wxFillLogFont(&lf
, &style
.GetFont());
1663 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1664 cf
.bCharSet
= lf
.lfCharSet
;
1665 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1666 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1668 // also deal with underline/italic/bold attributes: note that we must
1669 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1670 // style to allow clearing it
1673 cf
.dwEffects
|= CFE_ITALIC
;
1676 if ( lf
.lfWeight
== FW_BOLD
)
1678 cf
.dwEffects
|= CFE_BOLD
;
1681 if ( lf
.lfUnderline
)
1683 cf
.dwEffects
|= CFE_UNDERLINE
;
1686 // strikeout fonts are not supported by wxWindows
1689 if ( style
.HasTextColour() )
1691 cf
.dwMask
|= CFM_COLOR
;
1692 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1696 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1698 cf
.dwMask
|= CFM_BACKCOLOR
;
1699 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1701 #endif // wxUSE_RICHEDIT2
1703 // do format the selection
1704 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1705 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1708 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1713 // restore the original selection
1714 DoSetSelection(startOld
, endOld
, FALSE
);
1720 // ----------------------------------------------------------------------------
1722 // ----------------------------------------------------------------------------
1724 bool wxRichEditModule::OnInit()
1726 // don't do anything - we will load it when needed
1730 void wxRichEditModule::OnExit()
1732 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
1734 if ( ms_hRichEdit
[i
] )
1736 ::FreeLibrary(ms_hRichEdit
[i
]);
1742 bool wxRichEditModule::Load(int version
)
1744 // we don't support loading richedit 3.0 as I don't know how to distinguish
1745 // it from 2.0 anyhow
1746 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
1747 _T("incorrect richedit control version requested") );
1749 // make it the index in the array
1752 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
1754 // we had already tried to load it and failed
1758 if ( ms_hRichEdit
[version
] )
1760 // we've already got this one
1764 wxString dllname
= version
? _T("riched20") : _T("riched32");
1765 dllname
+= _T(".dll");
1767 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
1769 if ( !ms_hRichEdit
[version
] )
1771 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1773 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
1781 #endif // wxUSE_RICHEDIT
1783 #endif // wxUSE_TEXTCTRL