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"
53 #include "wx/msw/winundef.h"
55 #include "wx/msw/private.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
)
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 void wxTextCtrl::Init()
161 #endif // wxUSE_RICHEDIT
163 m_privateContextMenu
= NULL
;
164 m_suppressNextUpdate
= FALSE
;
167 wxTextCtrl::~wxTextCtrl()
169 if (m_privateContextMenu
)
171 delete m_privateContextMenu
;
172 m_privateContextMenu
= NULL
;
176 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
177 const wxString
& value
,
181 const wxValidator
& validator
,
182 const wxString
& name
)
184 // base initialization
185 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
189 parent
->AddChild(this);
191 // translate wxWin style flags to MSW ones
192 WXDWORD msStyle
= MSWGetCreateWindowFlags();
194 // do create the control - either an EDIT or RICHEDIT
195 wxString windowClass
= wxT("EDIT");
198 if ( m_windowStyle
& wxTE_AUTO_URL
)
200 // automatic URL detection only works in RichEdit 2.0+
201 m_windowStyle
|= wxTE_RICH2
;
204 if ( m_windowStyle
& wxTE_RICH2
)
206 // using richedit 2.0 implies using wxTE_RICH
207 m_windowStyle
|= wxTE_RICH
;
210 // we need to load the richedit DLL before creating the rich edit control
211 if ( m_windowStyle
& wxTE_RICH
)
213 static bool s_errorGiven
= FALSE
;// MT-FIXME
215 // Which version do we need? Use 1.0 by default because it is much more
216 // like the the standard EDIT or 2.0 if explicitly requested, but use
217 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
219 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
220 // (and thus EDIT) much better than RichEdit 2.0 so we probably
221 // should use 3.0 if available as it is the best of both worlds -
222 // but as I can't test it right now I don't do it (VZ)
224 const int verRichEdit
= 2;
225 #else // !wxUSE_UNICODE
226 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
227 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
229 // only give the error msg once if the DLL can't be loaded
232 // try to load the RichEdit DLL (will do nothing if already done)
233 if ( !wxRichEditModule::Load(verRichEdit
) )
236 // try another version?
237 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
239 if ( !wxRichEditModule::Load(verRichEdit
) )
240 #endif // wxUSE_UNICODE
242 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
249 // have we managed to load any richedit version?
252 m_verRichEdit
= verRichEdit
;
253 if ( m_verRichEdit
== 1 )
255 windowClass
= wxT("RICHEDIT");
259 #ifndef RICHEDIT_CLASS
260 wxString RICHEDIT_CLASS
;
261 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
263 RICHEDIT_CLASS
+= _T('W');
265 RICHEDIT_CLASS
+= _T('A');
266 #endif // Unicode/ANSI
267 #endif // !RICHEDIT_CLASS
269 windowClass
= RICHEDIT_CLASS
;
273 #endif // wxUSE_RICHEDIT
275 // we need to turn '\n's into "\r\n"s for the multiline controls
277 if ( m_windowStyle
& wxTE_MULTILINE
)
279 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
286 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
289 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
294 // enable the events we're interested in: we want to get EN_CHANGE as
295 // for the normal controls
296 LPARAM mask
= ENM_CHANGE
;
298 if ( GetRichVersion() == 1 )
300 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
301 // explained in its handler
302 mask
|= ENM_MOUSEEVENTS
;
304 else if ( m_windowStyle
& wxTE_AUTO_URL
)
308 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
311 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
313 #endif // wxUSE_RICHEDIT
318 // Make sure the window style (etc.) reflects the HWND style (roughly)
319 void wxTextCtrl::AdoptAttributesFromHWND()
321 wxWindow::AdoptAttributesFromHWND();
323 HWND hWnd
= GetHwnd();
324 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
326 // retrieve the style to see whether this is an edit or richedit ctrl
328 wxString classname
= wxGetWindowClass(GetHWND());
330 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
337 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
339 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
344 #endif // wxUSE_RICHEDIT
346 if (style
& ES_MULTILINE
)
347 m_windowStyle
|= wxTE_MULTILINE
;
348 if (style
& ES_PASSWORD
)
349 m_windowStyle
|= wxTE_PASSWORD
;
350 if (style
& ES_READONLY
)
351 m_windowStyle
|= wxTE_READONLY
;
352 if (style
& ES_WANTRETURN
)
353 m_windowStyle
|= wxTE_PROCESS_ENTER
;
354 if (style
& ES_CENTER
)
355 m_windowStyle
|= wxTE_CENTRE
;
356 if (style
& ES_RIGHT
)
357 m_windowStyle
|= wxTE_RIGHT
;
360 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
362 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
364 // styles which we alaways add by default
365 if ( style
& wxTE_MULTILINE
)
367 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
368 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
370 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
371 if ( !(style
& wxTE_NO_VSCROLL
) )
373 // always adjust the vertical scrollbar automatically if we have it
374 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
377 // we have to use this style for the rich edit controls because
378 // without it the vertical scrollbar never appears at all in
379 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
380 if ( style
& wxTE_RICH2
)
382 msStyle
|= ES_DISABLENOSCROLL
;
384 #endif // wxUSE_RICHEDIT
387 style
|= wxTE_PROCESS_ENTER
;
391 // there is really no reason to not have this style for single line
393 msStyle
|= ES_AUTOHSCROLL
;
396 // styles which we add depending on the specified wxWindows styles
397 if ( style
& wxHSCROLL
)
399 // automatically scroll the control horizontally as necessary
400 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
403 if ( style
& wxTE_READONLY
)
404 msStyle
|= ES_READONLY
;
406 if ( style
& wxTE_PASSWORD
)
407 msStyle
|= ES_PASSWORD
;
409 if ( style
& wxTE_NOHIDESEL
)
410 msStyle
|= ES_NOHIDESEL
;
412 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
413 if ( style
& wxTE_CENTRE
)
414 msStyle
|= ES_CENTER
;
415 else if ( style
& wxTE_RIGHT
)
418 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
423 void wxTextCtrl::SetWindowStyleFlag(long style
)
426 // we have to deal with some styles separately because they can't be
427 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
428 // using richedit-specific EM_SETOPTIONS
430 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
432 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
434 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
435 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
437 #endif // wxUSE_RICHEDIT
439 wxControl::SetWindowStyleFlag(style
);
442 // ----------------------------------------------------------------------------
443 // set/get the controls text
444 // ----------------------------------------------------------------------------
446 wxString
wxTextCtrl::GetValue() const
448 // range 0..-1 is special for GetRange() and means to retrieve all text
449 return GetRange(0, -1);
452 wxString
wxTextCtrl::GetRange(long from
, long to
) const
456 if ( from
>= to
&& to
!= -1 )
458 // nothing to retrieve
465 int len
= GetWindowTextLength(GetHwnd());
468 // alloc one extra WORD as needed by the control
469 wxChar
*p
= str
.GetWriteBuf(++len
);
472 textRange
.chrg
.cpMin
= from
;
473 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
474 textRange
.lpstrText
= p
;
476 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
478 if ( m_verRichEdit
> 1 )
480 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
481 // neither Unix nor Windows style - convert it to something
485 if ( *p
== _T('\r') )
492 if ( m_verRichEdit
== 1 )
494 // convert to the canonical form - see comment below
495 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
498 //else: no text at all, leave the string empty
501 #endif // wxUSE_RICHEDIT
504 str
= wxGetWindowText(GetHWND());
506 // need only a range?
509 str
= str
.Mid(from
, to
- from
);
512 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
513 // canonical one (same one as above) for consistency with the other kinds
514 // of controls and, more importantly, with the other ports
515 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
521 void wxTextCtrl::SetValue(const wxString
& value
)
523 // if the text is long enough, it's faster to just set it instead of first
524 // comparing it with the old one (chances are that it will be different
525 // anyhow, this comparison is there to avoid flicker for small single-line
526 // edit controls mostly)
527 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
529 DoWriteText(value
, FALSE
/* not selection only */);
532 // we should reset the modified flag even if the value didn't really change
534 // mark the control as being not dirty - we changed its text, not the
538 // for compatibility, don't move the cursor when doing SetValue()
539 SetInsertionPoint(0);
542 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
544 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
548 wchar_t *wbuf
= (wchar_t *)buf
;
549 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
554 cb
-= sizeof(wchar_t);
555 (*pcb
) += sizeof(wchar_t);
558 *(const wchar_t **)dwCookie
= wpc
;
564 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
566 #if wxUSE_UNICODE_MSLU
567 bool wxTextCtrl::StreamIn(const wxString
& value
,
568 wxFontEncoding
WXUNUSED(encoding
),
571 const wchar_t *wpc
= value
.c_str();
572 #else // !wxUSE_UNICODE_MSLU
573 bool wxTextCtrl::StreamIn(const wxString
& value
,
574 wxFontEncoding encoding
,
577 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
578 // text in the non default charset -- otherwise it thinks it knows better
579 // than we do and always shows it in the default one
581 // first get the Windows code page for this encoding
582 long codepage
= wxEncodingToCodepage(encoding
);
583 if ( codepage
== -1 )
589 // next translate to Unicode using this code page
590 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
593 wxWCharBuffer
wchBuf(len
);
595 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
598 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
599 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
601 wxLogLastError(_T("MultiByteToWideChar"));
604 // finally, stream it in the control
605 const wchar_t *wpc
= wchBuf
;
606 #endif // wxUSE_UNICODE_MSLU
610 eds
.dwCookie
= (DWORD
)&wpc
;
611 // the cast below is needed for broken (very) old mingw32 headers
612 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
614 // we're going to receive 2 EN_CHANGE notifications if we got any selection
615 // (same problem as in DoWriteText())
616 if ( selectionOnly
&& HasSelection() )
618 // so suppress one of them
619 m_suppressNextUpdate
= TRUE
;
622 ::SendMessage(GetHwnd(), EM_STREAMIN
,
625 (selectionOnly
? SFF_SELECTION
: 0),
630 wxLogLastError(_T("EM_STREAMIN"));
635 #endif // !wxUSE_WCHAR_T
640 #endif // wxUSE_RICHEDIT
642 void wxTextCtrl::WriteText(const wxString
& value
)
647 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
650 if ( m_windowStyle
& wxTE_MULTILINE
)
651 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
656 // there are several complications with the rich edit controls here
660 // first, ensure that the new text will be in the default style
661 if ( !m_defaultStyle
.IsDefault() )
664 GetSelection(&start
, &end
);
665 SetStyle(start
, end
, m_defaultStyle
);
668 #if wxUSE_UNICODE_MSLU
669 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
670 // but EM_STREAMIN works
671 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
673 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
675 #endif // wxUSE_UNICODE_MSLU
678 // next check if the text we're inserting must be shown in a non
679 // default charset -- this only works for RichEdit > 1.0
680 if ( GetRichVersion() > 1 )
682 wxFont font
= m_defaultStyle
.GetFont();
688 wxFontEncoding encoding
= font
.GetEncoding();
689 if ( encoding
!= wxFONTENCODING_SYSTEM
)
691 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
695 #endif // !wxUSE_UNICODE
699 #endif // wxUSE_RICHEDIT
701 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
702 // call below which is confusing for the client code and so should be
705 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
706 // and there is a non empty selection currently and (b) rich text
707 // controls in any case
711 #endif // wxUSE_RICHEDIT
712 (selectionOnly
&& HasSelection()) )
714 m_suppressNextUpdate
= TRUE
;
717 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
718 0, (LPARAM
)valueDos
.c_str());
720 // OTOH, non rich text controls don't generate any events at all when
721 // we use WM_SETTEXT -- have to emulate them here
725 #endif // wxUSE_RICHEDIT
728 // Windows already sends an update event for single-line
730 if ( m_windowStyle
& wxTE_MULTILINE
)
738 void wxTextCtrl::AppendText(const wxString
& text
)
740 SetInsertionPointEnd();
745 if ( IsMultiLine() && GetRichVersion() > 1 )
747 // setting the caret to the end and showing it simply doesn't work for
748 // RichEdit 2.0 -- force it to still do what we want
749 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
751 #endif // wxUSE_RICHEDIT
754 void wxTextCtrl::Clear()
756 ::SetWindowText(GetHwnd(), wxEmptyString
);
760 #endif // wxUSE_RICHEDIT
762 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
763 // but the normal ones don't -- make Clear() behaviour consistent by
764 // always sending this event
766 // Windows already sends an update event for single-line
768 if ( m_windowStyle
& wxTE_MULTILINE
)
775 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
779 size_t lenOld
= GetValue().length();
781 wxUint32 code
= event
.GetRawKeyCode();
782 ::keybd_event(code
, 0, 0 /* key press */, 0);
783 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
785 // assume that any alphanumeric key changes the total number of characters
786 // in the control - this should work in 99% of cases
787 return GetValue().length() != lenOld
;
792 // ----------------------------------------------------------------------------
793 // Clipboard operations
794 // ----------------------------------------------------------------------------
796 void wxTextCtrl::Copy()
800 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
804 void wxTextCtrl::Cut()
808 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
812 void wxTextCtrl::Paste()
816 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
820 bool wxTextCtrl::HasSelection() const
823 GetSelection(&from
, &to
);
827 bool wxTextCtrl::CanCopy() const
829 // Can copy if there's a selection
830 return HasSelection();
833 bool wxTextCtrl::CanCut() const
835 return CanCopy() && IsEditable();
838 bool wxTextCtrl::CanPaste() const
846 UINT cf
= 0; // 0 == any format
848 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
850 #endif // wxUSE_RICHEDIT
852 // Standard edit control: check for straight text on clipboard
853 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
856 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
859 return isTextAvailable
;
862 // ----------------------------------------------------------------------------
864 // ----------------------------------------------------------------------------
866 void wxTextCtrl::SetEditable(bool editable
)
868 HWND hWnd
= GetHwnd();
869 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
872 void wxTextCtrl::SetInsertionPoint(long pos
)
874 DoSetSelection(pos
, pos
);
877 void wxTextCtrl::SetInsertionPointEnd()
879 // we must not do anything if the caret is already there because calling
880 // SetInsertionPoint() thaws the controls if Freeze() had been called even
881 // if it doesn't actually move the caret anywhere and so the simple fact of
882 // doing it results in horrible flicker when appending big amounts of text
883 // to the control in a few chunks (see DoAddText() test in the text sample)
884 if ( GetInsertionPoint() == GetLastPosition() )
890 if ( m_verRichEdit
== 1 )
892 // we don't have to waste time calling GetLastPosition() in this case
895 else // !RichEdit 1.0
896 #endif // wxUSE_RICHEDIT
898 pos
= GetLastPosition();
901 SetInsertionPoint(pos
);
904 long wxTextCtrl::GetInsertionPoint() const
912 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
915 #endif // wxUSE_RICHEDIT
917 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
921 long wxTextCtrl::GetLastPosition() const
923 int numLines
= GetNumberOfLines();
924 long posStartLastLine
= XYToPosition(0, numLines
- 1);
926 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
928 return posStartLastLine
+ lenLastLine
;
931 // If the return values from and to are the same, there is no
933 void wxTextCtrl::GetSelection(long* from
, long* to
) const
939 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
941 *from
= charRange
.cpMin
;
942 *to
= charRange
.cpMax
;
945 #endif // !wxUSE_RICHEDIT
947 DWORD dwStart
, dwEnd
;
948 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
955 bool wxTextCtrl::IsEditable() const
957 // strangely enough, we may be called before the control is created: our
958 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
963 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
965 return (style
& ES_READONLY
) == 0;
968 // ----------------------------------------------------------------------------
970 // ----------------------------------------------------------------------------
972 void wxTextCtrl::SetSelection(long from
, long to
)
974 // if from and to are both -1, it means (in wxWindows) that all text should
975 // be selected - translate into Windows convention
976 if ( (from
== -1) && (to
== -1) )
982 DoSetSelection(from
, to
);
985 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
987 HWND hWnd
= GetHwnd();
996 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
999 #endif // wxUSE_RICHEDIT
1001 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1007 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1008 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1009 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1010 // option is set (but it does work ok in richedit 1.0 mode...)
1012 // so to make it work we either need to give focus to it here which
1013 // will probably create many problems (dummy focus events; window
1014 // containing the text control being brought to foreground
1015 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1016 // create other problems too -- and in fact it does because if we turn
1017 // on/off this style while appending the text to the control, the
1018 // vertical scrollbar never appears in it even if we append tons of
1019 // text and to work around this the only solution I found was to use
1020 // ES_DISABLENOSCROLL
1022 // this is very ugly but I don't see any other way to make this work
1023 if ( GetRichVersion() > 1 )
1025 if ( !HasFlag(wxTE_NOHIDESEL
) )
1027 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1028 ECOOP_OR
, ECO_NOHIDESEL
);
1030 //else: everything is already ok
1032 #endif // wxUSE_RICHEDIT
1034 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1037 // restore ECO_NOHIDESEL if we changed it
1038 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1040 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1041 ECOOP_AND
, ~ECO_NOHIDESEL
);
1043 #endif // wxUSE_RICHEDIT
1046 // WPARAM is 0: selection is scrolled into view
1047 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1051 // ----------------------------------------------------------------------------
1053 // ----------------------------------------------------------------------------
1055 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1057 // Set selection and remove it
1058 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1060 SendMessage(GetHwnd(), EM_REPLACESEL
,
1066 (LPARAM
)value
.c_str());
1069 void wxTextCtrl::Remove(long from
, long to
)
1071 Replace(from
, to
, wxEmptyString
);
1074 bool wxTextCtrl::LoadFile(const wxString
& file
)
1076 if ( wxTextCtrlBase::LoadFile(file
) )
1078 // update the size limit if needed
1087 bool wxTextCtrl::IsModified() const
1089 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1092 // Makes 'unmodified'
1093 void wxTextCtrl::DiscardEdits()
1095 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1098 int wxTextCtrl::GetNumberOfLines() const
1100 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1103 long wxTextCtrl::XYToPosition(long x
, long y
) const
1105 // This gets the char index for the _beginning_ of this line
1106 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1108 return charIndex
+ x
;
1111 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1113 HWND hWnd
= GetHwnd();
1115 // This gets the line number containing the character
1120 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1123 #endif // wxUSE_RICHEDIT
1125 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1134 // This gets the char index for the _beginning_ of this line
1135 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1136 if ( charIndex
== -1 )
1141 // The X position must therefore be the different between pos and charIndex
1143 *x
= pos
- charIndex
;
1150 void wxTextCtrl::ShowPosition(long pos
)
1152 HWND hWnd
= GetHwnd();
1154 // To scroll to a position, we pass the number of lines and characters
1155 // to scroll *by*. This means that we need to:
1156 // (1) Find the line position of the current line.
1157 // (2) Find the line position of pos.
1158 // (3) Scroll by (pos - current).
1159 // For now, ignore the horizontal scrolling.
1161 // Is this where scrolling is relative to - the line containing the caret?
1162 // Or is the first visible line??? Try first visible line.
1163 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1165 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1167 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1169 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1171 if (linesToScroll
!= 0)
1172 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1175 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1177 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1180 int wxTextCtrl::GetLineLength(long lineNo
) const
1182 long pos
= XYToPosition(0, lineNo
);
1184 return GetLengthOfLineContainingPos(pos
);
1187 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1189 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1191 // there must be at least enough place for the length WORD in the
1193 len
+= sizeof(WORD
);
1196 wxChar
*buf
= str
.GetWriteBuf(len
);
1198 *(WORD
*)buf
= (WORD
)len
;
1199 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1202 str
.UngetWriteBuf(len
);
1207 void wxTextCtrl::SetMaxLength(unsigned long len
)
1209 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1212 // ----------------------------------------------------------------------------
1214 // ----------------------------------------------------------------------------
1216 void wxTextCtrl::Undo()
1220 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1224 void wxTextCtrl::Redo()
1228 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1229 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1233 bool wxTextCtrl::CanUndo() const
1235 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1238 bool wxTextCtrl::CanRedo() const
1240 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1243 // ----------------------------------------------------------------------------
1244 // implemenation details
1245 // ----------------------------------------------------------------------------
1247 void wxTextCtrl::Command(wxCommandEvent
& event
)
1249 SetValue(event
.GetString());
1250 ProcessCommand (event
);
1253 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1255 // By default, load the first file into the text window.
1256 if (event
.GetNumberOfFiles() > 0)
1258 LoadFile(event
.GetFiles()[0]);
1262 // ----------------------------------------------------------------------------
1263 // kbd input processing
1264 // ----------------------------------------------------------------------------
1266 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1268 MSG
*msg
= (MSG
*)pMsg
;
1270 // check for our special keys here: if we don't do it and the parent frame
1271 // uses them as accelerators, they wouldn't work at all, so we disable
1272 // usual preprocessing for them
1273 if ( msg
->message
== WM_KEYDOWN
)
1275 WORD vkey
= (WORD
) msg
->wParam
;
1276 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1278 if ( vkey
== VK_BACK
)
1283 // we want to process some Ctrl-foo and Shift-bar but no key
1284 // combinations without either Ctrl or Shift nor with both of them
1286 const int ctrl
= wxIsCtrlDown(),
1287 shift
= wxIsShiftDown();
1288 switch ( ctrl
+ shift
)
1291 wxFAIL_MSG( _T("how many modifiers have we got?") );
1299 // either Ctrl or Shift pressed
1314 else // Shift is pressed
1316 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1323 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1326 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1328 switch ( event
.GetKeyCode() )
1331 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1333 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1334 InitCommandEvent(event
);
1335 event
.SetString(GetValue());
1336 if ( GetEventHandler()->ProcessEvent(event
) )
1339 //else: multiline controls need Enter for themselves
1344 // always produce navigation event - even if we process TAB
1345 // ourselves the fact that we got here means that the user code
1346 // decided to skip processing of this TAB - probably to let it
1347 // do its default job.
1349 wxNavigationKeyEvent eventNav
;
1350 eventNav
.SetDirection(!event
.ShiftDown());
1351 eventNav
.SetWindowChange(event
.ControlDown());
1352 eventNav
.SetEventObject(this);
1354 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1360 // no, we didn't process it
1364 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1366 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1368 if ( nMsg
== WM_GETDLGCODE
)
1370 // we always want the chars and the arrows: the arrows for navigation
1371 // and the chars because we want Ctrl-C to work even in a read only
1373 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1377 // we may have several different cases:
1378 // 1. normal case: both TAB and ENTER are used for dlg navigation
1379 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1380 // next control in the dialog
1381 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1383 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1384 // to the next control
1386 // the multiline edit control should always get <Return> for itself
1387 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1388 lDlgCode
|= DLGC_WANTMESSAGE
;
1390 if ( HasFlag(wxTE_PROCESS_TAB
) )
1391 lDlgCode
|= DLGC_WANTTAB
;
1397 // NB: use "=", not "|=" as the base class version returns the
1398 // same flags is this state as usual (i.e. including
1399 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1400 // native Win32 apps?) but for now live with it.
1408 // ----------------------------------------------------------------------------
1409 // text control event processing
1410 // ----------------------------------------------------------------------------
1412 bool wxTextCtrl::SendUpdateEvent()
1414 // is event reporting suspended?
1415 if ( m_suppressNextUpdate
)
1417 // do process the next one
1418 m_suppressNextUpdate
= FALSE
;
1423 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1424 InitCommandEvent(event
);
1425 event
.SetString(GetValue());
1427 return ProcessCommand(event
);
1430 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1437 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1440 event
.SetEventObject(this);
1441 GetEventHandler()->ProcessEvent(event
);
1450 // the text size limit has been hit -- try to increase it
1451 if ( !AdjustSpaceLimit() )
1453 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1454 InitCommandEvent(event
);
1455 event
.SetString(GetValue());
1456 ProcessCommand(event
);
1460 // the other edit notification messages are not processed
1469 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1475 WXUINT
WXUNUSED(message
),
1476 WXWPARAM
WXUNUSED(wParam
),
1477 WXLPARAM
WXUNUSED(lParam
)
1484 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1485 return (WXHBRUSH
) hbrush
;
1487 #endif // wxUSE_CTL3D
1490 wxColour colBack
= GetBackgroundColour();
1492 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1493 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1495 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1496 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1498 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1500 return (WXHBRUSH
)brush
->GetResourceHandle();
1503 // In WIN16, need to override normal erasing because
1504 // Ctl3D doesn't use the wxWindows background colour.
1506 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1508 wxColour
col(m_backgroundColour
);
1512 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1516 ::GetClientRect(GetHwnd(), &rect
);
1518 COLORREF ref
= wxColourToRGB(col
);
1519 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1521 wxLogLastError(wxT("CreateSolidBrush"));
1523 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1525 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1527 ::FillRect(hdc
, &rect
, hBrush
);
1528 ::DeleteObject(hBrush
);
1529 ::SetMapMode(hdc
, mode
);
1534 bool wxTextCtrl::AdjustSpaceLimit()
1537 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1539 // HACK: we try to automatically extend the limit for the amount of text
1540 // to allow (interactively) entering more than 64Kb of text under
1541 // Win9x but we shouldn't reset the text limit which was previously
1542 // set explicitly with SetMaxLength()
1544 // we could solve this by storing the limit we set in wxTextCtrl but
1545 // to save space we prefer to simply test here the actual limit
1546 // value: we consider that SetMaxLength() can only be called for
1548 if ( limit
< 0x8000 )
1550 // we've got more text than limit set by SetMaxLength()
1554 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1557 limit
= len
+ 0x8000; // 32Kb
1562 // as a nice side effect, this also allows passing limit > 64Kb
1563 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1566 #endif // wxUSE_RICHEDIT
1568 if ( limit
> 0xffff )
1570 // this will set it to a platform-dependent maximum (much more
1571 // than 64Kb under NT)
1575 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1580 // we changed the limit
1584 bool wxTextCtrl::AcceptsFocus() const
1586 // we don't want focus if we can't be edited unless we're a multiline
1587 // control because then it might be still nice to get focus from keyboard
1588 // to be able to scroll it without mouse
1589 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1592 wxSize
wxTextCtrl::DoGetBestSize() const
1595 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1597 int wText
= DEFAULT_ITEM_WIDTH
;
1599 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1600 if ( m_windowStyle
& wxTE_MULTILINE
)
1602 hText
*= wxMax(GetNumberOfLines(), 5);
1604 //else: for single line control everything is ok
1606 return wxSize(wText
, hText
);
1609 // ----------------------------------------------------------------------------
1610 // standard handlers for standard edit menu events
1611 // ----------------------------------------------------------------------------
1613 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1618 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1623 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1628 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1633 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1638 void wxTextCtrl::OnDelete(wxCommandEvent
& event
)
1641 GetSelection(& from
, & to
);
1642 if (from
!= -1 && to
!= -1)
1646 void wxTextCtrl::OnSelectAll(wxCommandEvent
& event
)
1648 SetSelection(-1, -1);
1651 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1653 event
.Enable( CanCut() );
1656 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1658 event
.Enable( CanCopy() );
1661 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1663 event
.Enable( CanPaste() );
1666 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1668 event
.Enable( CanUndo() );
1671 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1673 event
.Enable( CanRedo() );
1676 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1679 GetSelection(& from
, & to
);
1680 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1683 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1685 event
.Enable(GetLastPosition() > 0);
1688 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1693 if (!m_privateContextMenu
)
1695 m_privateContextMenu
= new wxMenu
;
1696 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1697 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1698 m_privateContextMenu
->AppendSeparator();
1699 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1700 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1701 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1702 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1703 m_privateContextMenu
->AppendSeparator();
1704 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1706 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1714 // the rest of the file only deals with the rich edit controls
1717 // ----------------------------------------------------------------------------
1718 // EN_LINK processing
1719 // ----------------------------------------------------------------------------
1721 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1723 NMHDR
*hdr
= (NMHDR
* )lParam
;
1724 switch ( hdr
->code
)
1728 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1729 UINT msg
= msgf
->msg
;
1731 // this is a bit crazy but richedit 1.0 sends us all mouse
1732 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1733 // generate the wxWin events for this message manually
1735 // NB: in fact, this is still not totally correct as it does
1736 // send us WM_LBUTTONUP if the selection was cleared by the
1737 // last click -- so currently we get 2 events in this case,
1738 // but as I don't see any obvious way to check for this I
1739 // leave this code in place because it's still better than
1740 // not getting left up events at all
1741 if ( msg
== WM_LBUTTONUP
)
1743 WXUINT flags
= msgf
->wParam
;
1744 int x
= GET_X_LPARAM(msgf
->lParam
),
1745 y
= GET_Y_LPARAM(msgf
->lParam
);
1747 HandleMouseEvent(msg
, x
, y
, flags
);
1751 // return TRUE to process the event (and FALSE to ignore it)
1756 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1758 switch ( enlink
->msg
)
1761 // ok, so it is hardcoded - do we really nee to
1763 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1768 case WM_LBUTTONDOWN
:
1770 case WM_LBUTTONDBLCLK
:
1771 case WM_RBUTTONDOWN
:
1773 case WM_RBUTTONDBLCLK
:
1774 // send a mouse event
1776 static const wxEventType eventsMouse
[] =
1787 // the event ids are consecutive
1789 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1791 InitMouseEvent(evtMouse
,
1792 GET_X_LPARAM(enlink
->lParam
),
1793 GET_Y_LPARAM(enlink
->lParam
),
1796 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1798 enlink
->chrg
.cpMax
);
1800 InitCommandEvent(event
);
1802 *result
= ProcessCommand(event
);
1810 // not processed, leave it to the base class
1811 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1814 // ----------------------------------------------------------------------------
1815 // colour setting for the rich edit controls
1816 // ----------------------------------------------------------------------------
1818 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1820 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1822 // colour didn't really change
1828 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1829 // EM_SETBKGNDCOLOR additionally
1830 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1836 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1838 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1840 // colour didn't really change
1846 // change the colour of everything
1849 cf
.cbSize
= sizeof(cf
);
1850 cf
.dwMask
= CFM_COLOR
;
1851 cf
.crTextColor
= wxColourToRGB(colour
);
1852 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1858 // ----------------------------------------------------------------------------
1859 // styling support for rich edit controls
1860 // ----------------------------------------------------------------------------
1864 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1868 // can't do it with normal text control
1872 // the richedit 1.0 doesn't handle setting background colour, so don't
1873 // even try to do anything if it's the only thing we want to change
1874 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1875 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1878 // nothing to do: return TRUE if there was really nothing to do and
1879 // FALSE if we failed to set bg colour
1880 return !style
.HasBackgroundColour();
1883 // order the range if needed
1891 // we can only change the format of the selection, so select the range we
1892 // want and restore the old selection later
1893 long startOld
, endOld
;
1894 GetSelection(&startOld
, &endOld
);
1896 // but do we really have to change the selection?
1897 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1901 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1904 // initialize CHARFORMAT struct
1913 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1914 // CHARFORMAT in that case
1916 if ( m_verRichEdit
== 1 )
1918 // this is the only thing the control is going to grok
1919 cf
.cbSize
= sizeof(CHARFORMAT
);
1924 // CHARFORMAT or CHARFORMAT2
1925 cf
.cbSize
= sizeof(cf
);
1928 if ( style
.HasFont() )
1930 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1931 // but using it doesn't seem to hurt neither so leaving it for now
1933 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1934 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1936 // fill in data from LOGFONT but recalculate lfHeight because we need
1937 // the real height in twips and not the negative number which
1938 // wxFillLogFont() returns (this is correct in general and works with
1939 // the Windows font mapper, but not here)
1941 wxFillLogFont(&lf
, &style
.GetFont());
1942 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1943 cf
.bCharSet
= lf
.lfCharSet
;
1944 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1945 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1947 // also deal with underline/italic/bold attributes: note that we must
1948 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1949 // style to allow clearing it
1952 cf
.dwEffects
|= CFE_ITALIC
;
1955 if ( lf
.lfWeight
== FW_BOLD
)
1957 cf
.dwEffects
|= CFE_BOLD
;
1960 if ( lf
.lfUnderline
)
1962 cf
.dwEffects
|= CFE_UNDERLINE
;
1965 // strikeout fonts are not supported by wxWindows
1968 if ( style
.HasTextColour() )
1970 cf
.dwMask
|= CFM_COLOR
;
1971 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1975 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1977 cf
.dwMask
|= CFM_BACKCOLOR
;
1978 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1980 #endif // wxUSE_RICHEDIT2
1982 // do format the selection
1983 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1984 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1987 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1990 // now do the paragraph formatting
1993 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
1994 // PARAFORMAT in that case
1996 if ( m_verRichEdit
== 1 )
1998 // this is the only thing the control is going to grok
1999 pf
.cbSize
= sizeof(PARAFORMAT
);
2004 // PARAFORMAT or PARAFORMAT2
2005 pf
.cbSize
= sizeof(pf
);
2008 if (style
.HasAlignment())
2010 pf
.dwMask
|= PFM_ALIGNMENT
;
2011 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2012 pf
.wAlignment
= PFA_RIGHT
;
2013 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2014 pf
.wAlignment
= PFA_CENTER
;
2015 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2016 pf
.wAlignment
= PFA_JUSTIFY
;
2018 pf
.wAlignment
= PFA_LEFT
;
2021 if (style
.HasLeftIndent())
2023 pf
.dwMask
|= PFM_STARTINDENT
;
2025 // Convert from 1/10 mm to TWIPS
2026 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2028 // TODO: do we need to specify dxOffset?
2031 if (style
.HasRightIndent())
2033 pf
.dwMask
|= PFM_RIGHTINDENT
;
2035 // Convert from 1/10 mm to TWIPS
2036 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2039 if (style
.HasTabs())
2041 pf
.dwMask
|= PFM_TABSTOPS
;
2043 const wxArrayInt
& tabs
= style
.GetTabs();
2045 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2047 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2049 // Convert from 1/10 mm to TWIPS
2050 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2056 // do format the selection
2057 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2058 0, (LPARAM
) &pf
) != 0;
2061 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2067 // restore the original selection
2068 DoSetSelection(startOld
, endOld
, FALSE
);
2074 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2076 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2079 // we have to do this or the style wouldn't apply for the text typed by the
2081 long posLast
= GetLastPosition();
2082 SetStyle(posLast
, posLast
, m_defaultStyle
);
2087 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2091 // can't do it with normal text control
2095 // initialize CHARFORMAT struct
2104 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2105 // CHARFORMAT in that case
2107 if ( m_verRichEdit
== 1 )
2109 // this is the only thing the control is going to grok
2110 cf
.cbSize
= sizeof(CHARFORMAT
);
2115 // CHARFORMAT or CHARFORMAT2
2116 cf
.cbSize
= sizeof(cf
);
2118 // we can only change the format of the selection, so select the range we
2119 // want and restore the old selection later
2120 long startOld
, endOld
;
2121 GetSelection(&startOld
, &endOld
);
2123 // but do we really have to change the selection?
2124 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2128 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2131 // get the selection formatting
2132 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2133 SCF_SELECTION
, (LPARAM
)&cf
) ;
2136 lf
.lfHeight
= cf
.yHeight
;
2138 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2139 lf
.lfClipPrecision
= 0;
2140 lf
.lfEscapement
= 0;
2141 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2142 if (cf
.dwEffects
& CFE_ITALIC
)
2144 lf
.lfOrientation
= 0;
2145 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2147 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2148 lf
.lfStrikeOut
= TRUE
;
2149 if (cf
.dwEffects
& CFE_UNDERLINE
)
2150 lf
.lfUnderline
= TRUE
;
2151 if (cf
.dwEffects
& CFE_BOLD
)
2152 lf
.lfWeight
= FW_BOLD
;
2154 wxFont font
= wxCreateFontFromLogFont(& lf
);
2157 style
.SetFont(font
);
2159 style
.SetTextColour(wxColour(cf
.crTextColor
));
2162 if ( m_verRichEdit
!= 1 )
2164 // cf.dwMask |= CFM_BACKCOLOR;
2165 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2167 #endif // wxUSE_RICHEDIT2
2169 // now get the paragraph formatting
2172 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2173 // PARAFORMAT in that case
2175 if ( m_verRichEdit
== 1 )
2177 // this is the only thing the control is going to grok
2178 pf
.cbSize
= sizeof(PARAFORMAT
);
2183 // PARAFORMAT or PARAFORMAT2
2184 pf
.cbSize
= sizeof(pf
);
2187 // do format the selection
2188 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2190 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2191 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2193 if (pf
.wAlignment
== PFA_CENTER
)
2194 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2195 else if (pf
.wAlignment
== PFA_RIGHT
)
2196 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2197 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2198 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2200 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2202 wxArrayInt tabStops
;
2204 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2206 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2211 // restore the original selection
2212 DoSetSelection(startOld
, endOld
, FALSE
);
2220 // ----------------------------------------------------------------------------
2222 // ----------------------------------------------------------------------------
2224 bool wxRichEditModule::OnInit()
2226 // don't do anything - we will load it when needed
2230 void wxRichEditModule::OnExit()
2232 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2234 if ( ms_hRichEdit
[i
] )
2236 ::FreeLibrary(ms_hRichEdit
[i
]);
2242 bool wxRichEditModule::Load(int version
)
2244 // we don't support loading richedit 3.0 as I don't know how to distinguish
2245 // it from 2.0 anyhow
2246 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2247 _T("incorrect richedit control version requested") );
2249 // make it the index in the array
2252 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2254 // we had already tried to load it and failed
2258 if ( ms_hRichEdit
[version
] )
2260 // we've already got this one
2264 wxString dllname
= version
? _T("riched20") : _T("riched32");
2265 dllname
+= _T(".dll");
2267 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2269 if ( !ms_hRichEdit
[version
] )
2271 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2273 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2281 #endif // wxUSE_RICHEDIT
2283 #endif // wxUSE_TEXTCTRL