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 // we always want the characters and the arrows
238 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
240 // we may have several different cases:
241 // 1. normal case: both TAB and ENTER are used for dialog navigation
242 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
243 // control in the dialog
244 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
245 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
247 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
248 m_lDlgCode
|= DLGC_WANTMESSAGE
;
249 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
250 m_lDlgCode
|= DLGC_WANTTAB
;
252 // do create the control - either an EDIT or RICHEDIT
253 wxString windowClass
= wxT("EDIT");
256 if ( m_windowStyle
& wxTE_AUTO_URL
)
258 // automatic URL detection only works in RichEdit 2.0+
259 m_windowStyle
|= wxTE_RICH2
;
262 if ( m_windowStyle
& wxTE_RICH2
)
264 // using richedit 2.0 implies using wxTE_RICH
265 m_windowStyle
|= wxTE_RICH
;
268 // we need to load the richedit DLL before creating the rich edit control
269 if ( m_windowStyle
& wxTE_RICH
)
271 static bool s_errorGiven
= FALSE
;// MT-FIXME
273 // Which version do we need? Use 1.0 by default because it is much more
274 // like the the standard EDIT or 2.0 if explicitly requested, but use
275 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
277 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
278 // (and thus EDIT) much better than RichEdit 2.0 so we probably
279 // should use 3.0 if available as it is the best of both worlds -
280 // but as I can't test it right now I don't do it (VZ)
282 const int verRichEdit
= 2;
283 #else // !wxUSE_UNICODE
284 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
285 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
287 // only give the error msg once if the DLL can't be loaded
290 // try to load the RichEdit DLL (will do nothing if already done)
291 if ( !wxRichEditModule::Load(verRichEdit
) )
294 // try another version?
295 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
297 if ( !wxRichEditModule::Load(verRichEdit
) )
298 #endif // wxUSE_UNICODE
300 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
307 // have we managed to load any richedit version?
310 msStyle
|= ES_AUTOVSCROLL
;
312 m_verRichEdit
= verRichEdit
;
313 if ( m_verRichEdit
== 1 )
315 windowClass
= wxT("RICHEDIT");
319 #ifndef RICHEDIT_CLASS
320 wxString RICHEDIT_CLASS
;
321 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
323 RICHEDIT_CLASS
+= _T('W');
325 RICHEDIT_CLASS
+= _T('A');
326 #endif // Unicode/ANSI
327 #endif // !RICHEDIT_CLASS
329 windowClass
= RICHEDIT_CLASS
;
333 #endif // wxUSE_RICHEDIT
335 // we need to turn '\n's into "\r\n"s for the multiline controls
337 if ( m_windowStyle
& wxTE_MULTILINE
)
339 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
346 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
349 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
354 // have to enable events manually
355 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
357 if ( m_windowStyle
& wxTE_AUTO_URL
)
361 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
364 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
366 #endif // wxUSE_RICHEDIT
371 // Make sure the window style (etc.) reflects the HWND style (roughly)
372 void wxTextCtrl::AdoptAttributesFromHWND()
374 wxWindow::AdoptAttributesFromHWND();
376 HWND hWnd
= GetHwnd();
377 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
379 // retrieve the style to see whether this is an edit or richedit ctrl
381 wxString classname
= wxGetWindowClass(GetHWND());
383 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
390 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
392 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
397 #endif // wxUSE_RICHEDIT
399 if (style
& ES_MULTILINE
)
400 m_windowStyle
|= wxTE_MULTILINE
;
401 if (style
& ES_PASSWORD
)
402 m_windowStyle
|= wxTE_PASSWORD
;
403 if (style
& ES_READONLY
)
404 m_windowStyle
|= wxTE_READONLY
;
405 if (style
& ES_WANTRETURN
)
406 m_windowStyle
|= wxTE_PROCESS_ENTER
;
409 // ----------------------------------------------------------------------------
410 // set/get the controls text
411 // ----------------------------------------------------------------------------
413 wxString
wxTextCtrl::GetValue() const
415 // range 0..-1 is special for GetRange() and means to retrieve all text
416 return GetRange(0, -1);
419 wxString
wxTextCtrl::GetRange(long from
, long to
) const
423 if ( from
>= to
&& to
!= -1 )
425 // nothing to retrieve
432 int len
= GetWindowTextLength(GetHwnd());
435 // alloc one extra WORD as needed by the control
436 wxChar
*p
= str
.GetWriteBuf(++len
);
439 textRange
.chrg
.cpMin
= from
;
440 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
441 textRange
.lpstrText
= p
;
443 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
445 if ( m_verRichEdit
> 1 )
447 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
448 // neither Unix nor Windows style - convert it to something
452 if ( *p
== _T('\r') )
459 if ( m_verRichEdit
== 1 )
461 // convert to the canonical form - see comment below
462 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
465 //else: no text at all, leave the string empty
468 #endif // wxUSE_RICHEDIT
471 str
= wxGetWindowText(GetHWND());
473 // need only a range?
476 str
= str
.Mid(from
, to
- from
);
479 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
480 // canonical one (same one as above) for consistency with the other kinds
481 // of controls and, more importantly, with the other ports
482 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
488 void wxTextCtrl::SetValue(const wxString
& value
)
490 // if the text is long enough, it's faster to just set it instead of first
491 // comparing it with the old one (chances are that it will be different
492 // anyhow, this comparison is there to avoid flicker for small single-line
493 // edit controls mostly)
494 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
496 DoWriteText(value
, FALSE
/* not selection only */);
498 // mark the control as being not dirty - we changed its text, not the
502 // for compatibility, don't move the cursor when doing SetValue()
503 SetInsertionPoint(0);
507 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
509 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
513 wchar_t *wbuf
= (wchar_t *)buf
;
514 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
519 cb
-= sizeof(wchar_t);
520 (*pcb
) += sizeof(wchar_t);
523 *(const wchar_t **)dwCookie
= wpc
;
528 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from strconv.cpp
530 #if wxUSE_UNICODE_MSLU
531 bool wxTextCtrl::StreamIn(const wxString
& value
,
532 wxFontEncoding
WXUNUSED(encoding
),
535 const wchar_t *wpc
= value
.c_str();
536 #else // !wxUSE_UNICODE_MSLU
537 bool wxTextCtrl::StreamIn(const wxString
& value
,
538 wxFontEncoding encoding
,
541 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
542 // text in the non default charset - otherwise it thinks it knows better
543 // than we do and always shows it in the default one
545 // first get the Windows code page for this encoding
546 long codepage
= wxEncodingToCodepage(encoding
);
547 if ( codepage
== -1 )
553 // next translate to Unicode using this code page
554 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
557 wxWCharBuffer
wchBuf(len
);
559 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
562 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
563 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
565 wxLogLastError(_T("MultiByteToWideChar"));
568 // finally, stream it in the control
569 const wchar_t *wpc
= wchBuf
;
570 #endif // wxUSE_UNICODE_MSLU
574 eds
.dwCookie
= (DWORD
)&wpc
;
575 // the cast below is needed for broken (very) old mingw32 headers
576 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
578 if ( !::SendMessage(GetHwnd(), EM_STREAMIN
,
581 (selectionOnly
? SFF_SELECTION
: 0),
582 (LPARAM
)&eds
) || eds
.dwError
)
584 wxLogLastError(_T("EM_STREAMIN"));
589 #endif // !wxUSE_WCHAR_T
594 #endif // wxUSE_RICHEDIT
596 void wxTextCtrl::WriteText(const wxString
& value
)
601 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
604 if ( m_windowStyle
& wxTE_MULTILINE
)
605 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
610 // there are several complications with the rich edit controls here
614 // first, ensure that the new text will be in the default style
615 if ( !m_defaultStyle
.IsDefault() )
618 GetSelection(&start
, &end
);
619 SetStyle(start
, end
, m_defaultStyle
);
622 #if wxUSE_UNICODE_MSLU
623 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
624 // but EM_STREAMIN works
625 if ( wxGetOsVersion() == wxWIN95
&& GetRichVersion() > 1 )
627 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
629 #endif // wxUSE_UNICODE_MSLU
632 // next check if the text we're inserting must be shown in a non
633 // default charset -- this only works for RichEdit > 1.0
634 if ( GetRichVersion() > 1 )
636 wxFont font
= m_defaultStyle
.GetFont();
642 wxFontEncoding encoding
= font
.GetEncoding();
643 if ( encoding
!= wxFONTENCODING_SYSTEM
)
645 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
649 #endif // !wxUSE_UNICODE
653 #endif // wxUSE_RICHEDIT
655 if ( !selectionOnly
)
657 SetSelection(-1, -1);
660 ::SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
666 void wxTextCtrl::AppendText(const wxString
& text
)
668 SetInsertionPointEnd();
673 void wxTextCtrl::Clear()
675 ::SetWindowText(GetHwnd(), wxT(""));
678 // ----------------------------------------------------------------------------
679 // Clipboard operations
680 // ----------------------------------------------------------------------------
682 void wxTextCtrl::Copy()
686 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
690 void wxTextCtrl::Cut()
694 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
698 void wxTextCtrl::Paste()
702 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
706 bool wxTextCtrl::CanCopy() const
708 // Can copy if there's a selection
710 GetSelection(&from
, &to
);
714 bool wxTextCtrl::CanCut() const
716 return CanCopy() && IsEditable();
719 bool wxTextCtrl::CanPaste() const
727 UINT cf
= 0; // 0 == any format
729 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
731 #endif // wxUSE_RICHEDIT
733 // Standard edit control: check for straight text on clipboard
734 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
737 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
740 return isTextAvailable
;
743 // ----------------------------------------------------------------------------
745 // ----------------------------------------------------------------------------
747 void wxTextCtrl::SetEditable(bool editable
)
749 HWND hWnd
= GetHwnd();
750 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
753 void wxTextCtrl::SetInsertionPoint(long pos
)
755 DoSetSelection(pos
, pos
);
758 void wxTextCtrl::SetInsertionPointEnd()
763 if ( m_verRichEdit
== 1 )
765 // we don't have to waste time calling GetLastPosition() in this case
768 else // !RichEdit 1.0
769 #endif // wxUSE_RICHEDIT
771 pos
= GetLastPosition();
774 SetInsertionPoint(pos
);
777 long wxTextCtrl::GetInsertionPoint() const
785 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
788 #endif // wxUSE_RICHEDIT
790 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
794 long wxTextCtrl::GetLastPosition() const
796 int numLines
= GetNumberOfLines();
797 long posStartLastLine
= XYToPosition(0, numLines
- 1);
799 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
801 return posStartLastLine
+ lenLastLine
;
804 // If the return values from and to are the same, there is no
806 void wxTextCtrl::GetSelection(long* from
, long* to
) const
812 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
814 *from
= charRange
.cpMin
;
815 *to
= charRange
.cpMax
;
818 #endif // !wxUSE_RICHEDIT
820 DWORD dwStart
, dwEnd
;
821 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
828 bool wxTextCtrl::IsEditable() const
830 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
832 return (style
& ES_READONLY
) == 0;
835 // ----------------------------------------------------------------------------
837 // ----------------------------------------------------------------------------
839 void wxTextCtrl::SetSelection(long from
, long to
)
841 // if from and to are both -1, it means (in wxWindows) that all text should
842 // be selected - translate into Windows convention
843 if ( (from
== -1) && (to
== -1) )
849 DoSetSelection(from
, to
);
852 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
854 HWND hWnd
= GetHwnd();
863 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
866 #endif // wxUSE_RICHEDIT
868 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
873 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
876 // WPARAM is 0: selection is scrolled into view
877 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
881 // ----------------------------------------------------------------------------
883 // ----------------------------------------------------------------------------
885 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
887 // Set selection and remove it
888 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
890 SendMessage(GetHwnd(), EM_REPLACESEL
,
896 (LPARAM
)value
.c_str());
899 void wxTextCtrl::Remove(long from
, long to
)
901 Replace(from
, to
, _T(""));
904 bool wxTextCtrl::LoadFile(const wxString
& file
)
906 if ( wxTextCtrlBase::LoadFile(file
) )
908 // update the size limit if needed
917 bool wxTextCtrl::IsModified() const
919 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
922 // Makes 'unmodified'
923 void wxTextCtrl::DiscardEdits()
925 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
928 int wxTextCtrl::GetNumberOfLines() const
930 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
933 long wxTextCtrl::XYToPosition(long x
, long y
) const
935 // This gets the char index for the _beginning_ of this line
936 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
938 return charIndex
+ x
;
941 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
943 HWND hWnd
= GetHwnd();
945 // This gets the line number containing the character
950 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
953 #endif // wxUSE_RICHEDIT
955 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
964 // This gets the char index for the _beginning_ of this line
965 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
966 if ( charIndex
== -1 )
971 // The X position must therefore be the different between pos and charIndex
973 *x
= pos
- charIndex
;
980 void wxTextCtrl::ShowPosition(long pos
)
982 HWND hWnd
= GetHwnd();
984 // To scroll to a position, we pass the number of lines and characters
985 // to scroll *by*. This means that we need to:
986 // (1) Find the line position of the current line.
987 // (2) Find the line position of pos.
988 // (3) Scroll by (pos - current).
989 // For now, ignore the horizontal scrolling.
991 // Is this where scrolling is relative to - the line containing the caret?
992 // Or is the first visible line??? Try first visible line.
993 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
995 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
997 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
999 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1001 if (linesToScroll
!= 0)
1002 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1005 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1007 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1010 int wxTextCtrl::GetLineLength(long lineNo
) const
1012 long pos
= XYToPosition(0, lineNo
);
1014 return GetLengthOfLineContainingPos(pos
);
1017 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1019 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1021 // there must be at least enough place for the length WORD in the
1023 len
+= sizeof(WORD
);
1026 wxChar
*buf
= str
.GetWriteBuf(len
);
1028 *(WORD
*)buf
= (WORD
)len
;
1029 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1032 str
.UngetWriteBuf(len
);
1037 void wxTextCtrl::SetMaxLength(unsigned long len
)
1039 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1042 // ----------------------------------------------------------------------------
1044 // ----------------------------------------------------------------------------
1046 void wxTextCtrl::Undo()
1050 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1054 void wxTextCtrl::Redo()
1058 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1059 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1063 bool wxTextCtrl::CanUndo() const
1065 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1068 bool wxTextCtrl::CanRedo() const
1070 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1073 // ----------------------------------------------------------------------------
1074 // implemenation details
1075 // ----------------------------------------------------------------------------
1077 void wxTextCtrl::Command(wxCommandEvent
& event
)
1079 SetValue(event
.GetString());
1080 ProcessCommand (event
);
1083 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1085 // By default, load the first file into the text window.
1086 if (event
.GetNumberOfFiles() > 0)
1088 LoadFile(event
.GetFiles()[0]);
1092 // ----------------------------------------------------------------------------
1093 // kbd input processing
1094 // ----------------------------------------------------------------------------
1096 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1098 MSG
*msg
= (MSG
*)pMsg
;
1100 // check for our special keys here: if we don't do it and the parent frame
1101 // uses them as accelerators, they wouldn't work at all, so we disable
1102 // usual preprocessing for them
1103 if ( msg
->message
== WM_KEYDOWN
)
1105 WORD vkey
= msg
->wParam
;
1106 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1108 if ( vkey
== VK_BACK
)
1113 if ( wxIsCtrlDown() )
1127 else if ( wxIsShiftDown() )
1129 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1135 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1138 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1140 switch ( event
.KeyCode() )
1143 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1145 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1146 InitCommandEvent(event
);
1147 event
.SetString(GetValue());
1148 if ( GetEventHandler()->ProcessEvent(event
) )
1151 //else: multiline controls need Enter for themselves
1156 // always produce navigation event - even if we process TAB
1157 // ourselves the fact that we got here means that the user code
1158 // decided to skip processing of this TAB - probably to let it
1159 // do its default job.
1161 wxNavigationKeyEvent eventNav
;
1162 eventNav
.SetDirection(!event
.ShiftDown());
1163 eventNav
.SetWindowChange(event
.ControlDown());
1164 eventNav
.SetEventObject(this);
1166 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1172 // no, we didn't process it
1176 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1183 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1186 event
.SetEventObject( this );
1187 GetEventHandler()->ProcessEvent(event
);
1193 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1194 InitCommandEvent(event
);
1195 event
.SetString(GetValue());
1196 ProcessCommand(event
);
1201 // the text size limit has been hit - increase it
1202 if ( !AdjustSpaceLimit() )
1204 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1205 InitCommandEvent(event
);
1206 event
.SetString(GetValue());
1207 ProcessCommand(event
);
1211 // the other notification messages are not processed
1225 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1231 WXUINT
WXUNUSED(message
),
1232 WXWPARAM
WXUNUSED(wParam
),
1233 WXLPARAM
WXUNUSED(lParam
)
1240 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1241 return (WXHBRUSH
) hbrush
;
1243 #endif // wxUSE_CTL3D
1246 if (GetParent()->GetTransparentBackground())
1247 SetBkMode(hdc
, TRANSPARENT
);
1249 SetBkMode(hdc
, OPAQUE
);
1251 wxColour colBack
= GetBackgroundColour();
1253 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1254 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1256 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1257 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1259 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1261 return (WXHBRUSH
)brush
->GetResourceHandle();
1264 // In WIN16, need to override normal erasing because
1265 // Ctl3D doesn't use the wxWindows background colour.
1267 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1269 wxColour
col(m_backgroundColour
);
1273 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1277 ::GetClientRect(GetHwnd(), &rect
);
1279 COLORREF ref
= wxColourToRGB(col
);
1280 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1282 wxLogLastError(wxT("CreateSolidBrush"));
1284 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1286 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1288 ::FillRect(hdc
, &rect
, hBrush
);
1289 ::DeleteObject(hBrush
);
1290 ::SetMapMode(hdc
, mode
);
1295 bool wxTextCtrl::AdjustSpaceLimit()
1298 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1300 // HACK: we try to automatically extend the limit for the amount of text
1301 // to allow (interactively) entering more than 64Kb of text under
1302 // Win9x but we shouldn't reset the text limit which was previously
1303 // set explicitly with SetMaxLength()
1305 // we could solve this by storing the limit we set in wxTextCtrl but
1306 // to save space we prefer to simply test here the actual limit
1307 // value: we consider that SetMaxLength() can only be called for
1309 if ( limit
< 0x8000 )
1311 // we've got more text than limit set by SetMaxLength()
1315 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1318 limit
= len
+ 0x8000; // 32Kb
1323 // as a nice side effect, this also allows passing limit > 64Kb
1324 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1327 #endif // wxUSE_RICHEDIT
1329 if ( limit
> 0xffff )
1331 // this will set it to a platform-dependent maximum (much more
1332 // than 64Kb under NT)
1336 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1341 // we changed the limit
1345 bool wxTextCtrl::AcceptsFocus() const
1347 // we don't want focus if we can't be edited
1348 return IsEditable() && wxControl::AcceptsFocus();
1351 wxSize
wxTextCtrl::DoGetBestSize() const
1354 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1356 int wText
= DEFAULT_ITEM_WIDTH
;
1358 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1359 if ( m_windowStyle
& wxTE_MULTILINE
)
1361 hText
*= wxMax(GetNumberOfLines(), 5);
1363 //else: for single line control everything is ok
1365 return wxSize(wText
, hText
);
1368 // ----------------------------------------------------------------------------
1369 // standard handlers for standard edit menu events
1370 // ----------------------------------------------------------------------------
1372 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1377 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1382 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1387 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1392 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1397 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1399 event
.Enable( CanCut() );
1402 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1404 event
.Enable( CanCopy() );
1407 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1409 event
.Enable( CanPaste() );
1412 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1414 event
.Enable( CanUndo() );
1417 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1419 event
.Enable( CanRedo() );
1422 // the rest of the file only deals with the rich edit controls
1425 // ----------------------------------------------------------------------------
1426 // EN_LINK processing
1427 // ----------------------------------------------------------------------------
1429 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1431 NMHDR
*hdr
= (NMHDR
* )lParam
;
1432 if ( hdr
->code
== EN_LINK
)
1434 ENLINK
*enlink
= (ENLINK
*)hdr
;
1436 switch ( enlink
->msg
)
1439 // ok, so it is hardcoded - do we really nee to customize it?
1440 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1445 case WM_LBUTTONDOWN
:
1447 case WM_LBUTTONDBLCLK
:
1448 case WM_RBUTTONDOWN
:
1450 case WM_RBUTTONDBLCLK
:
1451 // send a mouse event
1453 static const wxEventType eventsMouse
[] =
1464 // the event ids are consecutive
1466 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1468 InitMouseEvent(evtMouse
,
1469 GET_X_LPARAM(enlink
->lParam
),
1470 GET_Y_LPARAM(enlink
->lParam
),
1473 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1475 enlink
->chrg
.cpMax
);
1477 InitCommandEvent(event
);
1479 *result
= ProcessCommand(event
);
1491 // ----------------------------------------------------------------------------
1492 // colour setting for the rich edit controls
1493 // ----------------------------------------------------------------------------
1495 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1497 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1499 // colour didn't really change
1505 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1506 // EM_SETBKGNDCOLOR additionally
1507 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1513 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1515 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1517 // colour didn't really change
1523 // change the colour of everything
1526 cf
.cbSize
= sizeof(cf
);
1527 cf
.dwMask
= CFM_COLOR
;
1528 cf
.crTextColor
= wxColourToRGB(colour
);
1529 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1535 // ----------------------------------------------------------------------------
1536 // styling support for rich edit controls
1537 // ----------------------------------------------------------------------------
1539 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1543 // can't do it with normal text control
1547 // the richedit 1.0 doesn't handle setting background colour, so don't
1548 // even try to do anything if it's the only thing we want to change
1549 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() )
1551 // nothing to do: return TRUE if there was really nothing to do and
1552 // FALSE if we failed to set bg colour
1553 return !style
.HasBackgroundColour();
1556 // order the range if needed
1564 // we can only change the format of the selection, so select the range we
1565 // want and restore the old selection later
1566 long startOld
, endOld
;
1567 GetSelection(&startOld
, &endOld
);
1569 // but do we really have to change the selection?
1570 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1574 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1577 // initialize CHARFORMAT struct
1586 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1587 // CHARFORMAT in that case
1589 if ( m_verRichEdit
== 1 )
1591 // this is the only thing the control is going to grok
1592 cf
.cbSize
= sizeof(CHARFORMAT
);
1597 // CHARFORMAT or CHARFORMAT2
1598 cf
.cbSize
= sizeof(cf
);
1601 if ( style
.HasFont() )
1603 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1604 // but using it doesn't seem to hurt neither so leaving it for now
1606 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1607 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1609 // fill in data from LOGFONT but recalculate lfHeight because we need
1610 // the real height in twips and not the negative number which
1611 // wxFillLogFont() returns (this is correct in general and works with
1612 // the Windows font mapper, but not here)
1614 wxFillLogFont(&lf
, &style
.GetFont());
1615 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1616 cf
.bCharSet
= lf
.lfCharSet
;
1617 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1618 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1620 // also deal with underline/italic/bold attributes: note that we must
1621 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1622 // style to allow clearing it
1625 cf
.dwEffects
|= CFE_ITALIC
;
1628 if ( lf
.lfWeight
== FW_BOLD
)
1630 cf
.dwEffects
|= CFE_BOLD
;
1633 if ( lf
.lfUnderline
)
1635 cf
.dwEffects
|= CFE_UNDERLINE
;
1638 // strikeout fonts are not supported by wxWindows
1641 if ( style
.HasTextColour() )
1643 cf
.dwMask
|= CFM_COLOR
;
1644 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1648 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1650 cf
.dwMask
|= CFM_BACKCOLOR
;
1651 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1653 #endif // wxUSE_RICHEDIT2
1655 // do format the selection
1656 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1657 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1660 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1665 // restore the original selection
1666 DoSetSelection(startOld
, endOld
, FALSE
);
1672 // ----------------------------------------------------------------------------
1674 // ----------------------------------------------------------------------------
1676 bool wxRichEditModule::OnInit()
1678 // don't do anything - we will load it when needed
1682 void wxRichEditModule::OnExit()
1684 for ( int i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
1686 if ( ms_hRichEdit
[i
] )
1688 ::FreeLibrary(ms_hRichEdit
[i
]);
1694 bool wxRichEditModule::Load(int version
)
1696 // we don't support loading richedit 3.0 as I don't know how to distinguish
1697 // it from 2.0 anyhow
1698 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
1699 _T("incorrect richedit control version requested") );
1701 // make it the index in the array
1704 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
1706 // we had already tried to load it and failed
1710 if ( ms_hRichEdit
[version
] )
1712 // we've already got this one
1716 wxString dllname
= version
? _T("riched20") : _T("riched32");
1717 dllname
+= _T(".dll");
1719 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
1721 if ( !ms_hRichEdit
[version
] )
1723 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1725 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
1733 #endif // wxUSE_RICHEDIT
1735 #endif // wxUSE_TEXTCTRL