1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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"
44 #include "wx/module.h"
47 #include "wx/clipbrd.h"
50 #include "wx/textfile.h"
54 #include "wx/msw/private.h"
55 #include "wx/msw/winundef.h"
61 #include <sys/types.h>
66 // old mingw32 has richedit stuff directly in windows.h and doesn't have
68 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
72 #include "wx/msw/missing.h"
74 #endif // wxUSE_RICHEDIT
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
82 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
84 #endif // wxUSE_RICHEDIT
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
92 // this module initializes RichEdit DLL(s) if needed
93 class wxRichEditModule
: public wxModule
96 virtual bool OnInit();
97 virtual void OnExit();
99 // load the richedit DLL of at least of required version
100 static bool Load(int version
= 1);
103 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
104 static HINSTANCE ms_hRichEdit
[2];
106 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
109 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
111 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
113 #endif // wxUSE_RICHEDIT
115 // ----------------------------------------------------------------------------
116 // event tables and other macros
117 // ----------------------------------------------------------------------------
119 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
121 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
122 EVT_CHAR(wxTextCtrl::OnChar
)
123 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
126 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
129 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
130 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
131 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
132 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
133 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
134 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
135 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
137 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
138 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
139 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
140 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
141 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
142 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
143 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
145 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
148 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
)
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 void wxTextCtrl::Init()
163 #endif // wxUSE_RICHEDIT
165 m_privateContextMenu
= NULL
;
166 m_suppressNextUpdate
= FALSE
;
167 m_isNativeCaretShown
= true;
170 wxTextCtrl::~wxTextCtrl()
172 if (m_privateContextMenu
)
174 delete m_privateContextMenu
;
175 m_privateContextMenu
= NULL
;
179 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
180 const wxString
& value
,
184 const wxValidator
& validator
,
185 const wxString
& name
)
187 // base initialization
188 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
192 parent
->AddChild(this);
194 // translate wxWin style flags to MSW ones
195 WXDWORD msStyle
= MSWGetCreateWindowFlags();
197 // do create the control - either an EDIT or RICHEDIT
198 wxString windowClass
= wxT("EDIT");
201 if ( m_windowStyle
& wxTE_AUTO_URL
)
203 // automatic URL detection only works in RichEdit 2.0+
204 m_windowStyle
|= wxTE_RICH2
;
207 if ( m_windowStyle
& wxTE_RICH2
)
209 // using richedit 2.0 implies using wxTE_RICH
210 m_windowStyle
|= wxTE_RICH
;
213 // we need to load the richedit DLL before creating the rich edit control
214 if ( m_windowStyle
& wxTE_RICH
)
216 static bool s_errorGiven
= FALSE
;// MT-FIXME
218 // Which version do we need? Use 1.0 by default because it is much more
219 // like the the standard EDIT or 2.0 if explicitly requested, but use
220 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
222 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
223 // (and thus EDIT) much better than RichEdit 2.0 so we probably
224 // should use 3.0 if available as it is the best of both worlds -
225 // but as I can't test it right now I don't do it (VZ)
227 const int verRichEdit
= 2;
228 #else // !wxUSE_UNICODE
229 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
230 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
232 // only give the error msg once if the DLL can't be loaded
235 // try to load the RichEdit DLL (will do nothing if already done)
236 if ( !wxRichEditModule::Load(verRichEdit
) )
239 // try another version?
240 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
242 if ( !wxRichEditModule::Load(verRichEdit
) )
243 #endif // wxUSE_UNICODE
245 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
252 // have we managed to load any richedit version?
255 m_verRichEdit
= verRichEdit
;
256 if ( m_verRichEdit
== 1 )
258 windowClass
= wxT("RICHEDIT");
262 #ifndef RICHEDIT_CLASS
263 wxString RICHEDIT_CLASS
;
264 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
266 RICHEDIT_CLASS
+= _T('W');
268 RICHEDIT_CLASS
+= _T('A');
269 #endif // Unicode/ANSI
270 #endif // !RICHEDIT_CLASS
272 windowClass
= RICHEDIT_CLASS
;
276 #endif // wxUSE_RICHEDIT
278 // we need to turn '\n's into "\r\n"s for the multiline controls
280 if ( m_windowStyle
& wxTE_MULTILINE
)
282 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
289 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
292 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
297 // enable the events we're interested in: we want to get EN_CHANGE as
298 // for the normal controls
299 LPARAM mask
= ENM_CHANGE
;
301 if ( GetRichVersion() == 1 )
303 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
304 // explained in its handler
305 mask
|= ENM_MOUSEEVENTS
;
307 else if ( m_windowStyle
& wxTE_AUTO_URL
)
311 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
314 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
316 #endif // wxUSE_RICHEDIT
321 // Make sure the window style (etc.) reflects the HWND style (roughly)
322 void wxTextCtrl::AdoptAttributesFromHWND()
324 wxWindow::AdoptAttributesFromHWND();
326 HWND hWnd
= GetHwnd();
327 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
329 // retrieve the style to see whether this is an edit or richedit ctrl
331 wxString classname
= wxGetWindowClass(GetHWND());
333 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
340 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
342 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
347 #endif // wxUSE_RICHEDIT
349 if (style
& ES_MULTILINE
)
350 m_windowStyle
|= wxTE_MULTILINE
;
351 if (style
& ES_PASSWORD
)
352 m_windowStyle
|= wxTE_PASSWORD
;
353 if (style
& ES_READONLY
)
354 m_windowStyle
|= wxTE_READONLY
;
355 if (style
& ES_WANTRETURN
)
356 m_windowStyle
|= wxTE_PROCESS_ENTER
;
357 if (style
& ES_CENTER
)
358 m_windowStyle
|= wxTE_CENTRE
;
359 if (style
& ES_RIGHT
)
360 m_windowStyle
|= wxTE_RIGHT
;
363 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
365 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
367 // styles which we alaways add by default
368 if ( style
& wxTE_MULTILINE
)
370 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
371 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
373 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
374 if ( !(style
& wxTE_NO_VSCROLL
) )
376 // always adjust the vertical scrollbar automatically if we have it
377 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
380 // we have to use this style for the rich edit controls because
381 // without it the vertical scrollbar never appears at all in
382 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
383 if ( style
& wxTE_RICH2
)
385 msStyle
|= ES_DISABLENOSCROLL
;
387 #endif // wxUSE_RICHEDIT
390 style
|= wxTE_PROCESS_ENTER
;
394 // there is really no reason to not have this style for single line
396 msStyle
|= ES_AUTOHSCROLL
;
399 // styles which we add depending on the specified wxWindows styles
400 if ( style
& wxHSCROLL
)
402 // automatically scroll the control horizontally as necessary
403 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
406 if ( style
& wxTE_READONLY
)
407 msStyle
|= ES_READONLY
;
409 if ( style
& wxTE_PASSWORD
)
410 msStyle
|= ES_PASSWORD
;
412 if ( style
& wxTE_NOHIDESEL
)
413 msStyle
|= ES_NOHIDESEL
;
415 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
416 if ( style
& wxTE_CENTRE
)
417 msStyle
|= ES_CENTER
;
418 else if ( style
& wxTE_RIGHT
)
421 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
426 void wxTextCtrl::SetWindowStyleFlag(long style
)
429 // we have to deal with some styles separately because they can't be
430 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
431 // using richedit-specific EM_SETOPTIONS
433 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
435 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
437 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
438 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
440 #endif // wxUSE_RICHEDIT
442 wxControl::SetWindowStyleFlag(style
);
445 // ----------------------------------------------------------------------------
446 // set/get the controls text
447 // ----------------------------------------------------------------------------
449 wxString
wxTextCtrl::GetValue() const
451 // range 0..-1 is special for GetRange() and means to retrieve all text
452 return GetRange(0, -1);
455 wxString
wxTextCtrl::GetRange(long from
, long to
) const
459 if ( from
>= to
&& to
!= -1 )
461 // nothing to retrieve
468 int len
= GetWindowTextLength(GetHwnd());
472 // alloc one extra WORD as needed by the control
473 wxStringBuffer
tmp(str
, ++len
);
477 textRange
.chrg
.cpMin
= from
;
478 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
479 textRange
.lpstrText
= p
;
481 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
,
482 0, (LPARAM
)&textRange
);
484 if ( m_verRichEdit
> 1 )
486 // RichEdit 2.0 uses just CR ('\r') for the
487 // newlines which is neither Unix nor Windows
488 // style - convert it to something reasonable
491 if ( *p
== _T('\r') )
497 if ( m_verRichEdit
== 1 )
499 // convert to the canonical form - see comment below
500 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
503 //else: no text at all, leave the string empty
506 #endif // wxUSE_RICHEDIT
509 str
= wxGetWindowText(GetHWND());
511 // need only a range?
514 str
= str
.Mid(from
, to
- from
);
517 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
518 // canonical one (same one as above) for consistency with the other kinds
519 // of controls and, more importantly, with the other ports
520 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
526 void wxTextCtrl::SetValue(const wxString
& value
)
528 // if the text is long enough, it's faster to just set it instead of first
529 // comparing it with the old one (chances are that it will be different
530 // anyhow, this comparison is there to avoid flicker for small single-line
531 // edit controls mostly)
532 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
534 DoWriteText(value
, FALSE
/* not selection only */);
538 // still send an event for consistency
542 // we should reset the modified flag even if the value didn't really change
544 // mark the control as being not dirty - we changed its text, not the
548 // for compatibility, don't move the cursor when doing SetValue()
549 SetInsertionPoint(0);
552 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
554 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
558 wchar_t *wbuf
= (wchar_t *)buf
;
559 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
564 cb
-= sizeof(wchar_t);
565 (*pcb
) += sizeof(wchar_t);
568 *(const wchar_t **)dwCookie
= wpc
;
574 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
576 #if wxUSE_UNICODE_MSLU
577 bool wxTextCtrl::StreamIn(const wxString
& value
,
578 wxFontEncoding
WXUNUSED(encoding
),
581 const wchar_t *wpc
= value
.c_str();
582 #else // !wxUSE_UNICODE_MSLU
583 bool wxTextCtrl::StreamIn(const wxString
& value
,
584 wxFontEncoding encoding
,
587 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
588 // text in the non default charset -- otherwise it thinks it knows better
589 // than we do and always shows it in the default one
591 // first get the Windows code page for this encoding
592 long codepage
= wxEncodingToCodepage(encoding
);
593 if ( codepage
== -1 )
599 // next translate to Unicode using this code page
600 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
603 wxWCharBuffer
wchBuf(len
);
605 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
608 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
609 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
611 wxLogLastError(_T("MultiByteToWideChar"));
614 // finally, stream it in the control
615 const wchar_t *wpc
= wchBuf
;
616 #endif // wxUSE_UNICODE_MSLU
620 eds
.dwCookie
= (DWORD
)&wpc
;
621 // the cast below is needed for broken (very) old mingw32 headers
622 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
624 // we're going to receive 2 EN_CHANGE notifications if we got any selection
625 // (same problem as in DoWriteText())
626 if ( selectionOnly
&& HasSelection() )
628 // so suppress one of them
629 m_suppressNextUpdate
= TRUE
;
632 ::SendMessage(GetHwnd(), EM_STREAMIN
,
635 (selectionOnly
? SFF_SELECTION
: 0),
640 wxLogLastError(_T("EM_STREAMIN"));
645 #endif // !wxUSE_WCHAR_T
650 #endif // wxUSE_RICHEDIT
652 void wxTextCtrl::WriteText(const wxString
& value
)
657 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
660 if ( m_windowStyle
& wxTE_MULTILINE
)
661 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
666 // there are several complications with the rich edit controls here
670 // first, ensure that the new text will be in the default style
671 if ( !m_defaultStyle
.IsDefault() )
674 GetSelection(&start
, &end
);
675 SetStyle(start
, end
, m_defaultStyle
);
678 #if wxUSE_UNICODE_MSLU
679 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
680 // but EM_STREAMIN works
681 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
683 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
685 #endif // wxUSE_UNICODE_MSLU
688 // next check if the text we're inserting must be shown in a non
689 // default charset -- this only works for RichEdit > 1.0
690 if ( GetRichVersion() > 1 )
692 wxFont font
= m_defaultStyle
.GetFont();
698 wxFontEncoding encoding
= font
.GetEncoding();
699 if ( encoding
!= wxFONTENCODING_SYSTEM
)
701 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
705 #endif // !wxUSE_UNICODE
709 #endif // wxUSE_RICHEDIT
711 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
712 // call below which is confusing for the client code and so should be
715 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
716 // and there is a non empty selection currently and (b) rich text
717 // controls in any case
721 #endif // wxUSE_RICHEDIT
722 (selectionOnly
&& HasSelection()) )
724 m_suppressNextUpdate
= TRUE
;
727 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
728 0, (LPARAM
)valueDos
.c_str());
730 // OTOH, non rich text controls don't generate any events at all when
731 // we use WM_SETTEXT -- have to emulate them here
735 #endif // wxUSE_RICHEDIT
738 // Windows already sends an update event for single-line
740 if ( m_windowStyle
& wxTE_MULTILINE
)
748 void wxTextCtrl::AppendText(const wxString
& text
)
750 SetInsertionPointEnd();
755 if ( IsMultiLine() && GetRichVersion() > 1 )
757 // setting the caret to the end and showing it simply doesn't work for
758 // RichEdit 2.0 -- force it to still do what we want
759 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
761 #endif // wxUSE_RICHEDIT
764 void wxTextCtrl::Clear()
766 ::SetWindowText(GetHwnd(), wxEmptyString
);
770 #endif // wxUSE_RICHEDIT
772 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
773 // but the normal ones don't -- make Clear() behaviour consistent by
774 // always sending this event
776 // Windows already sends an update event for single-line
778 if ( m_windowStyle
& wxTE_MULTILINE
)
785 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
789 size_t lenOld
= GetValue().length();
791 wxUint32 code
= event
.GetRawKeyCode();
792 ::keybd_event(code
, 0, 0 /* key press */, 0);
793 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
795 // assume that any alphanumeric key changes the total number of characters
796 // in the control - this should work in 99% of cases
797 return GetValue().length() != lenOld
;
802 // ----------------------------------------------------------------------------
803 // Clipboard operations
804 // ----------------------------------------------------------------------------
806 void wxTextCtrl::Copy()
810 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
814 void wxTextCtrl::Cut()
818 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
822 void wxTextCtrl::Paste()
826 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
830 bool wxTextCtrl::HasSelection() const
833 GetSelection(&from
, &to
);
837 bool wxTextCtrl::CanCopy() const
839 // Can copy if there's a selection
840 return HasSelection();
843 bool wxTextCtrl::CanCut() const
845 return CanCopy() && IsEditable();
848 bool wxTextCtrl::CanPaste() const
856 UINT cf
= 0; // 0 == any format
858 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
860 #endif // wxUSE_RICHEDIT
862 // Standard edit control: check for straight text on clipboard
863 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
866 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
869 return isTextAvailable
;
872 // ----------------------------------------------------------------------------
874 // ----------------------------------------------------------------------------
876 void wxTextCtrl::SetEditable(bool editable
)
878 HWND hWnd
= GetHwnd();
879 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
882 void wxTextCtrl::SetInsertionPoint(long pos
)
884 DoSetSelection(pos
, pos
);
887 void wxTextCtrl::SetInsertionPointEnd()
889 // we must not do anything if the caret is already there because calling
890 // SetInsertionPoint() thaws the controls if Freeze() had been called even
891 // if it doesn't actually move the caret anywhere and so the simple fact of
892 // doing it results in horrible flicker when appending big amounts of text
893 // to the control in a few chunks (see DoAddText() test in the text sample)
894 if ( GetInsertionPoint() == GetLastPosition() )
900 if ( m_verRichEdit
== 1 )
902 // we don't have to waste time calling GetLastPosition() in this case
905 else // !RichEdit 1.0
906 #endif // wxUSE_RICHEDIT
908 pos
= GetLastPosition();
911 SetInsertionPoint(pos
);
914 long wxTextCtrl::GetInsertionPoint() const
922 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
925 #endif // wxUSE_RICHEDIT
927 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
931 long wxTextCtrl::GetLastPosition() const
933 int numLines
= GetNumberOfLines();
934 long posStartLastLine
= XYToPosition(0, numLines
- 1);
936 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
938 return posStartLastLine
+ lenLastLine
;
941 // If the return values from and to are the same, there is no
943 void wxTextCtrl::GetSelection(long* from
, long* to
) const
949 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
951 *from
= charRange
.cpMin
;
952 *to
= charRange
.cpMax
;
955 #endif // !wxUSE_RICHEDIT
957 DWORD dwStart
, dwEnd
;
958 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
965 bool wxTextCtrl::IsEditable() const
967 // strangely enough, we may be called before the control is created: our
968 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
973 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
975 return (style
& ES_READONLY
) == 0;
978 // ----------------------------------------------------------------------------
980 // ----------------------------------------------------------------------------
982 void wxTextCtrl::SetSelection(long from
, long to
)
984 // if from and to are both -1, it means (in wxWindows) that all text should
985 // be selected - translate into Windows convention
986 if ( (from
== -1) && (to
== -1) )
992 DoSetSelection(from
, to
);
995 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
997 HWND hWnd
= GetHwnd();
1006 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
1009 #endif // wxUSE_RICHEDIT
1011 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1017 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1018 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1019 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1020 // option is set (but it does work ok in richedit 1.0 mode...)
1022 // so to make it work we either need to give focus to it here which
1023 // will probably create many problems (dummy focus events; window
1024 // containing the text control being brought to foreground
1025 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1026 // create other problems too -- and in fact it does because if we turn
1027 // on/off this style while appending the text to the control, the
1028 // vertical scrollbar never appears in it even if we append tons of
1029 // text and to work around this the only solution I found was to use
1030 // ES_DISABLENOSCROLL
1032 // this is very ugly but I don't see any other way to make this work
1033 if ( GetRichVersion() > 1 )
1035 if ( !HasFlag(wxTE_NOHIDESEL
) )
1037 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1038 ECOOP_OR
, ECO_NOHIDESEL
);
1040 //else: everything is already ok
1042 #endif // wxUSE_RICHEDIT
1044 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1047 // restore ECO_NOHIDESEL if we changed it
1048 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1050 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1051 ECOOP_AND
, ~ECO_NOHIDESEL
);
1053 #endif // wxUSE_RICHEDIT
1056 // WPARAM is 0: selection is scrolled into view
1057 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1061 // ----------------------------------------------------------------------------
1063 // ----------------------------------------------------------------------------
1065 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1067 // Set selection and remove it
1068 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1070 SendMessage(GetHwnd(), EM_REPLACESEL
,
1076 (LPARAM
)value
.c_str());
1079 void wxTextCtrl::Remove(long from
, long to
)
1081 Replace(from
, to
, wxEmptyString
);
1084 bool wxTextCtrl::LoadFile(const wxString
& file
)
1086 if ( wxTextCtrlBase::LoadFile(file
) )
1088 // update the size limit if needed
1097 bool wxTextCtrl::IsModified() const
1099 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1102 // Makes 'unmodified'
1103 void wxTextCtrl::DiscardEdits()
1105 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1108 int wxTextCtrl::GetNumberOfLines() const
1110 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1113 long wxTextCtrl::XYToPosition(long x
, long y
) const
1115 // This gets the char index for the _beginning_ of this line
1116 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1118 return charIndex
+ x
;
1121 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1123 HWND hWnd
= GetHwnd();
1125 // This gets the line number containing the character
1130 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1133 #endif // wxUSE_RICHEDIT
1135 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1144 // This gets the char index for the _beginning_ of this line
1145 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1146 if ( charIndex
== -1 )
1151 // The X position must therefore be the different between pos and charIndex
1153 *x
= pos
- charIndex
;
1160 void wxTextCtrl::ShowPosition(long pos
)
1162 HWND hWnd
= GetHwnd();
1164 // To scroll to a position, we pass the number of lines and characters
1165 // to scroll *by*. This means that we need to:
1166 // (1) Find the line position of the current line.
1167 // (2) Find the line position of pos.
1168 // (3) Scroll by (pos - current).
1169 // For now, ignore the horizontal scrolling.
1171 // Is this where scrolling is relative to - the line containing the caret?
1172 // Or is the first visible line??? Try first visible line.
1173 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1175 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1177 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1179 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1181 if (linesToScroll
!= 0)
1182 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1185 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1187 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1190 int wxTextCtrl::GetLineLength(long lineNo
) const
1192 long pos
= XYToPosition(0, lineNo
);
1194 return GetLengthOfLineContainingPos(pos
);
1197 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1199 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1201 // there must be at least enough place for the length WORD in the
1203 len
+= sizeof(WORD
);
1207 wxStringBufferLength
tmp(str
, len
);
1210 *(WORD
*)buf
= (WORD
)len
;
1211 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
,
1212 lineNo
, (LPARAM
)buf
);
1220 void wxTextCtrl::SetMaxLength(unsigned long len
)
1222 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1225 // ----------------------------------------------------------------------------
1227 // ----------------------------------------------------------------------------
1229 void wxTextCtrl::Undo()
1233 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1237 void wxTextCtrl::Redo()
1241 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1242 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1246 bool wxTextCtrl::CanUndo() const
1248 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1251 bool wxTextCtrl::CanRedo() const
1253 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1256 // ----------------------------------------------------------------------------
1257 // caret handling (Windows only)
1258 // ----------------------------------------------------------------------------
1260 bool wxTextCtrl::ShowNativeCaret(bool show
)
1262 if ( show
!= m_isNativeCaretShown
)
1264 if ( !(show
? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) )
1266 // not an error, may simply indicate that it's not shown/hidden
1267 // yet (i.e. it had been hidden/showh 2 times before)
1271 m_isNativeCaretShown
= show
;
1277 // ----------------------------------------------------------------------------
1278 // implemenation details
1279 // ----------------------------------------------------------------------------
1281 void wxTextCtrl::Command(wxCommandEvent
& event
)
1283 SetValue(event
.GetString());
1284 ProcessCommand (event
);
1287 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1289 // By default, load the first file into the text window.
1290 if (event
.GetNumberOfFiles() > 0)
1292 LoadFile(event
.GetFiles()[0]);
1296 // ----------------------------------------------------------------------------
1297 // kbd input processing
1298 // ----------------------------------------------------------------------------
1300 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1302 MSG
*msg
= (MSG
*)pMsg
;
1304 // check for our special keys here: if we don't do it and the parent frame
1305 // uses them as accelerators, they wouldn't work at all, so we disable
1306 // usual preprocessing for them
1307 if ( msg
->message
== WM_KEYDOWN
)
1309 WORD vkey
= (WORD
) msg
->wParam
;
1310 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1312 if ( vkey
== VK_BACK
)
1317 // we want to process some Ctrl-foo and Shift-bar but no key
1318 // combinations without either Ctrl or Shift nor with both of them
1320 const int ctrl
= wxIsCtrlDown(),
1321 shift
= wxIsShiftDown();
1322 switch ( ctrl
+ shift
)
1325 wxFAIL_MSG( _T("how many modifiers have we got?") );
1333 // either Ctrl or Shift pressed
1348 else // Shift is pressed
1350 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1357 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1360 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1362 switch ( event
.GetKeyCode() )
1365 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1367 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1368 InitCommandEvent(event
);
1369 event
.SetString(GetValue());
1370 if ( GetEventHandler()->ProcessEvent(event
) )
1373 //else: multiline controls need Enter for themselves
1378 // always produce navigation event - even if we process TAB
1379 // ourselves the fact that we got here means that the user code
1380 // decided to skip processing of this TAB - probably to let it
1381 // do its default job.
1383 wxNavigationKeyEvent eventNav
;
1384 eventNav
.SetDirection(!event
.ShiftDown());
1385 eventNav
.SetWindowChange(event
.ControlDown());
1386 eventNav
.SetEventObject(this);
1388 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1394 // no, we didn't process it
1398 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1400 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1402 if ( nMsg
== WM_GETDLGCODE
)
1404 // we always want the chars and the arrows: the arrows for navigation
1405 // and the chars because we want Ctrl-C to work even in a read only
1407 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1411 // we may have several different cases:
1412 // 1. normal case: both TAB and ENTER are used for dlg navigation
1413 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1414 // next control in the dialog
1415 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1417 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1418 // to the next control
1420 // the multiline edit control should always get <Return> for itself
1421 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1422 lDlgCode
|= DLGC_WANTMESSAGE
;
1424 if ( HasFlag(wxTE_PROCESS_TAB
) )
1425 lDlgCode
|= DLGC_WANTTAB
;
1431 // NB: use "=", not "|=" as the base class version returns the
1432 // same flags is this state as usual (i.e. including
1433 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1434 // native Win32 apps?) but for now live with it.
1442 // ----------------------------------------------------------------------------
1443 // text control event processing
1444 // ----------------------------------------------------------------------------
1446 bool wxTextCtrl::SendUpdateEvent()
1448 // is event reporting suspended?
1449 if ( m_suppressNextUpdate
)
1451 // do process the next one
1452 m_suppressNextUpdate
= FALSE
;
1457 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1458 InitCommandEvent(event
);
1459 event
.SetString(GetValue());
1461 return ProcessCommand(event
);
1464 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1471 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1474 event
.SetEventObject(this);
1475 GetEventHandler()->ProcessEvent(event
);
1484 // the text size limit has been hit -- try to increase it
1485 if ( !AdjustSpaceLimit() )
1487 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1488 InitCommandEvent(event
);
1489 event
.SetString(GetValue());
1490 ProcessCommand(event
);
1494 // the other edit notification messages are not processed
1503 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1509 WXUINT
WXUNUSED(message
),
1510 WXWPARAM
WXUNUSED(wParam
),
1511 WXLPARAM
WXUNUSED(lParam
)
1518 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1519 return (WXHBRUSH
) hbrush
;
1521 #endif // wxUSE_CTL3D
1524 wxColour colBack
= GetBackgroundColour();
1526 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1527 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1529 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1530 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1532 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1534 return (WXHBRUSH
)brush
->GetResourceHandle();
1537 // In WIN16, need to override normal erasing because
1538 // Ctl3D doesn't use the wxWindows background colour.
1540 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1542 wxColour
col(m_backgroundColour
);
1546 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1550 ::GetClientRect(GetHwnd(), &rect
);
1552 COLORREF ref
= wxColourToRGB(col
);
1553 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1555 wxLogLastError(wxT("CreateSolidBrush"));
1557 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1559 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1561 ::FillRect(hdc
, &rect
, hBrush
);
1562 ::DeleteObject(hBrush
);
1563 ::SetMapMode(hdc
, mode
);
1568 bool wxTextCtrl::AdjustSpaceLimit()
1571 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1573 // HACK: we try to automatically extend the limit for the amount of text
1574 // to allow (interactively) entering more than 64Kb of text under
1575 // Win9x but we shouldn't reset the text limit which was previously
1576 // set explicitly with SetMaxLength()
1578 // we could solve this by storing the limit we set in wxTextCtrl but
1579 // to save space we prefer to simply test here the actual limit
1580 // value: we consider that SetMaxLength() can only be called for
1582 if ( limit
< 0x8000 )
1584 // we've got more text than limit set by SetMaxLength()
1588 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1591 limit
= len
+ 0x8000; // 32Kb
1596 // as a nice side effect, this also allows passing limit > 64Kb
1597 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1600 #endif // wxUSE_RICHEDIT
1602 if ( limit
> 0xffff )
1604 // this will set it to a platform-dependent maximum (much more
1605 // than 64Kb under NT)
1609 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1614 // we changed the limit
1618 bool wxTextCtrl::AcceptsFocus() const
1620 // we don't want focus if we can't be edited unless we're a multiline
1621 // control because then it might be still nice to get focus from keyboard
1622 // to be able to scroll it without mouse
1623 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1626 wxSize
wxTextCtrl::DoGetBestSize() const
1629 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1631 int wText
= DEFAULT_ITEM_WIDTH
;
1633 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1634 if ( m_windowStyle
& wxTE_MULTILINE
)
1636 hText
*= wxMax(GetNumberOfLines(), 5);
1638 //else: for single line control everything is ok
1640 return wxSize(wText
, hText
);
1643 // ----------------------------------------------------------------------------
1644 // standard handlers for standard edit menu events
1645 // ----------------------------------------------------------------------------
1647 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1652 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1657 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1662 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1667 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1672 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1675 GetSelection(& from
, & to
);
1676 if (from
!= -1 && to
!= -1)
1680 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1682 SetSelection(-1, -1);
1685 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1687 event
.Enable( CanCut() );
1690 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1692 event
.Enable( CanCopy() );
1695 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1697 event
.Enable( CanPaste() );
1700 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1702 event
.Enable( CanUndo() );
1705 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1707 event
.Enable( CanRedo() );
1710 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1713 GetSelection(& from
, & to
);
1714 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1717 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1719 event
.Enable(GetLastPosition() > 0);
1722 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1727 if (!m_privateContextMenu
)
1729 m_privateContextMenu
= new wxMenu
;
1730 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1731 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1732 m_privateContextMenu
->AppendSeparator();
1733 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1734 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1735 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1736 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1737 m_privateContextMenu
->AppendSeparator();
1738 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1740 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1748 void wxTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
1750 // be sure the caret remains invisible if the user had hidden it
1751 if ( !m_isNativeCaretShown
)
1753 ::HideCaret(GetHwnd());
1757 // the rest of the file only deals with the rich edit controls
1760 // ----------------------------------------------------------------------------
1761 // EN_LINK processing
1762 // ----------------------------------------------------------------------------
1764 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1766 NMHDR
*hdr
= (NMHDR
* )lParam
;
1767 switch ( hdr
->code
)
1771 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1772 UINT msg
= msgf
->msg
;
1774 // this is a bit crazy but richedit 1.0 sends us all mouse
1775 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1776 // generate the wxWin events for this message manually
1778 // NB: in fact, this is still not totally correct as it does
1779 // send us WM_LBUTTONUP if the selection was cleared by the
1780 // last click -- so currently we get 2 events in this case,
1781 // but as I don't see any obvious way to check for this I
1782 // leave this code in place because it's still better than
1783 // not getting left up events at all
1784 if ( msg
== WM_LBUTTONUP
)
1786 WXUINT flags
= msgf
->wParam
;
1787 int x
= GET_X_LPARAM(msgf
->lParam
),
1788 y
= GET_Y_LPARAM(msgf
->lParam
);
1790 HandleMouseEvent(msg
, x
, y
, flags
);
1794 // return TRUE to process the event (and FALSE to ignore it)
1799 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1801 switch ( enlink
->msg
)
1804 // ok, so it is hardcoded - do we really nee to
1806 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1811 case WM_LBUTTONDOWN
:
1813 case WM_LBUTTONDBLCLK
:
1814 case WM_RBUTTONDOWN
:
1816 case WM_RBUTTONDBLCLK
:
1817 // send a mouse event
1819 static const wxEventType eventsMouse
[] =
1830 // the event ids are consecutive
1832 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1834 InitMouseEvent(evtMouse
,
1835 GET_X_LPARAM(enlink
->lParam
),
1836 GET_Y_LPARAM(enlink
->lParam
),
1839 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1841 enlink
->chrg
.cpMax
);
1843 InitCommandEvent(event
);
1845 *result
= ProcessCommand(event
);
1853 // not processed, leave it to the base class
1854 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1857 // ----------------------------------------------------------------------------
1858 // colour setting for the rich edit controls
1859 // ----------------------------------------------------------------------------
1861 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1863 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1865 // colour didn't really change
1871 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1872 // EM_SETBKGNDCOLOR additionally
1873 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1879 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1881 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1883 // colour didn't really change
1889 // change the colour of everything
1892 cf
.cbSize
= sizeof(cf
);
1893 cf
.dwMask
= CFM_COLOR
;
1894 cf
.crTextColor
= wxColourToRGB(colour
);
1895 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1901 // ----------------------------------------------------------------------------
1902 // styling support for rich edit controls
1903 // ----------------------------------------------------------------------------
1907 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1911 // can't do it with normal text control
1915 // the richedit 1.0 doesn't handle setting background colour, so don't
1916 // even try to do anything if it's the only thing we want to change
1917 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1918 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1921 // nothing to do: return TRUE if there was really nothing to do and
1922 // FALSE if we failed to set bg colour
1923 return !style
.HasBackgroundColour();
1926 // order the range if needed
1934 // we can only change the format of the selection, so select the range we
1935 // want and restore the old selection later
1936 long startOld
, endOld
;
1937 GetSelection(&startOld
, &endOld
);
1939 // but do we really have to change the selection?
1940 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1944 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1947 // initialize CHARFORMAT struct
1956 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1957 // CHARFORMAT in that case
1959 if ( m_verRichEdit
== 1 )
1961 // this is the only thing the control is going to grok
1962 cf
.cbSize
= sizeof(CHARFORMAT
);
1967 // CHARFORMAT or CHARFORMAT2
1968 cf
.cbSize
= sizeof(cf
);
1971 if ( style
.HasFont() )
1973 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1974 // but using it doesn't seem to hurt neither so leaving it for now
1976 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1977 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1979 // fill in data from LOGFONT but recalculate lfHeight because we need
1980 // the real height in twips and not the negative number which
1981 // wxFillLogFont() returns (this is correct in general and works with
1982 // the Windows font mapper, but not here)
1984 wxFillLogFont(&lf
, &style
.GetFont());
1985 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1986 cf
.bCharSet
= lf
.lfCharSet
;
1987 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1988 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1990 // also deal with underline/italic/bold attributes: note that we must
1991 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1992 // style to allow clearing it
1995 cf
.dwEffects
|= CFE_ITALIC
;
1998 if ( lf
.lfWeight
== FW_BOLD
)
2000 cf
.dwEffects
|= CFE_BOLD
;
2003 if ( lf
.lfUnderline
)
2005 cf
.dwEffects
|= CFE_UNDERLINE
;
2008 // strikeout fonts are not supported by wxWindows
2011 if ( style
.HasTextColour() )
2013 cf
.dwMask
|= CFM_COLOR
;
2014 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
2018 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
2020 cf
.dwMask
|= CFM_BACKCOLOR
;
2021 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
2023 #endif // wxUSE_RICHEDIT2
2025 // do format the selection
2026 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
2027 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
2030 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
2033 // now do the paragraph formatting
2036 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2037 // PARAFORMAT in that case
2039 if ( m_verRichEdit
== 1 )
2041 // this is the only thing the control is going to grok
2042 pf
.cbSize
= sizeof(PARAFORMAT
);
2047 // PARAFORMAT or PARAFORMAT2
2048 pf
.cbSize
= sizeof(pf
);
2051 if (style
.HasAlignment())
2053 pf
.dwMask
|= PFM_ALIGNMENT
;
2054 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2055 pf
.wAlignment
= PFA_RIGHT
;
2056 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2057 pf
.wAlignment
= PFA_CENTER
;
2058 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2059 pf
.wAlignment
= PFA_JUSTIFY
;
2061 pf
.wAlignment
= PFA_LEFT
;
2064 if (style
.HasLeftIndent())
2066 pf
.dwMask
|= PFM_STARTINDENT
;
2068 // Convert from 1/10 mm to TWIPS
2069 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2071 // TODO: do we need to specify dxOffset?
2074 if (style
.HasRightIndent())
2076 pf
.dwMask
|= PFM_RIGHTINDENT
;
2078 // Convert from 1/10 mm to TWIPS
2079 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2082 if (style
.HasTabs())
2084 pf
.dwMask
|= PFM_TABSTOPS
;
2086 const wxArrayInt
& tabs
= style
.GetTabs();
2088 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2090 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2092 // Convert from 1/10 mm to TWIPS
2093 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2099 // do format the selection
2100 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2101 0, (LPARAM
) &pf
) != 0;
2104 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2110 // restore the original selection
2111 DoSetSelection(startOld
, endOld
, FALSE
);
2117 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2119 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2122 // we have to do this or the style wouldn't apply for the text typed by the
2124 long posLast
= GetLastPosition();
2125 SetStyle(posLast
, posLast
, m_defaultStyle
);
2130 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2134 // can't do it with normal text control
2138 // initialize CHARFORMAT struct
2147 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2148 // CHARFORMAT in that case
2150 if ( m_verRichEdit
== 1 )
2152 // this is the only thing the control is going to grok
2153 cf
.cbSize
= sizeof(CHARFORMAT
);
2158 // CHARFORMAT or CHARFORMAT2
2159 cf
.cbSize
= sizeof(cf
);
2161 // we can only change the format of the selection, so select the range we
2162 // want and restore the old selection later
2163 long startOld
, endOld
;
2164 GetSelection(&startOld
, &endOld
);
2166 // but do we really have to change the selection?
2167 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2171 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2174 // get the selection formatting
2175 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2176 SCF_SELECTION
, (LPARAM
)&cf
) ;
2179 lf
.lfHeight
= cf
.yHeight
;
2181 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2182 lf
.lfClipPrecision
= 0;
2183 lf
.lfEscapement
= 0;
2184 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2185 if (cf
.dwEffects
& CFE_ITALIC
)
2187 lf
.lfOrientation
= 0;
2188 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2190 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2191 lf
.lfStrikeOut
= TRUE
;
2192 if (cf
.dwEffects
& CFE_UNDERLINE
)
2193 lf
.lfUnderline
= TRUE
;
2194 if (cf
.dwEffects
& CFE_BOLD
)
2195 lf
.lfWeight
= FW_BOLD
;
2197 wxFont font
= wxCreateFontFromLogFont(& lf
);
2200 style
.SetFont(font
);
2202 style
.SetTextColour(wxColour(cf
.crTextColor
));
2205 if ( m_verRichEdit
!= 1 )
2207 // cf.dwMask |= CFM_BACKCOLOR;
2208 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2210 #endif // wxUSE_RICHEDIT2
2212 // now get the paragraph formatting
2215 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2216 // PARAFORMAT in that case
2218 if ( m_verRichEdit
== 1 )
2220 // this is the only thing the control is going to grok
2221 pf
.cbSize
= sizeof(PARAFORMAT
);
2226 // PARAFORMAT or PARAFORMAT2
2227 pf
.cbSize
= sizeof(pf
);
2230 // do format the selection
2231 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2233 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2234 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2236 if (pf
.wAlignment
== PFA_CENTER
)
2237 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2238 else if (pf
.wAlignment
== PFA_RIGHT
)
2239 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2240 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2241 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2243 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2245 wxArrayInt tabStops
;
2247 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2249 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2254 // restore the original selection
2255 DoSetSelection(startOld
, endOld
, FALSE
);
2263 // ----------------------------------------------------------------------------
2265 // ----------------------------------------------------------------------------
2267 bool wxRichEditModule::OnInit()
2269 // don't do anything - we will load it when needed
2273 void wxRichEditModule::OnExit()
2275 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2277 if ( ms_hRichEdit
[i
] )
2279 ::FreeLibrary(ms_hRichEdit
[i
]);
2280 ms_hRichEdit
[i
] = NULL
;
2286 bool wxRichEditModule::Load(int version
)
2288 // we don't support loading richedit 3.0 as I don't know how to distinguish
2289 // it from 2.0 anyhow
2290 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2291 _T("incorrect richedit control version requested") );
2293 // make it the index in the array
2296 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2298 // we had already tried to load it and failed
2302 if ( ms_hRichEdit
[version
] )
2304 // we've already got this one
2308 wxString dllname
= version
? _T("riched20") : _T("riched32");
2309 dllname
+= _T(".dll");
2311 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2313 if ( !ms_hRichEdit
[version
] )
2315 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2317 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2325 #endif // wxUSE_RICHEDIT
2327 #endif // wxUSE_TEXTCTRL