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"
58 #include <sys/types.h>
62 // old mingw32 has richedit stuff directly in windows.h and doesn't have
64 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
68 #include "wx/msw/missing.h"
70 #endif // wxUSE_RICHEDIT
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
78 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
80 #endif // wxUSE_RICHEDIT
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
88 // this module initializes RichEdit DLL(s) if needed
89 class wxRichEditModule
: public wxModule
92 virtual bool OnInit();
93 virtual void OnExit();
95 // load the richedit DLL of at least of required version
96 static bool Load(int version
= 1);
99 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
100 static HINSTANCE ms_hRichEdit
[2];
102 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
105 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
107 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
109 #endif // wxUSE_RICHEDIT
111 // ----------------------------------------------------------------------------
112 // event tables and other macros
113 // ----------------------------------------------------------------------------
115 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
117 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
118 EVT_CHAR(wxTextCtrl::OnChar
)
119 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
122 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
125 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
126 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
127 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
128 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
129 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
130 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
131 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
133 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
134 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
135 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
136 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
137 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
138 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
139 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
141 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
145 // ============================================================================
147 // ============================================================================
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxTextCtrl::Init()
157 #endif // wxUSE_RICHEDIT
159 m_privateContextMenu
= NULL
;
160 m_suppressNextUpdate
= FALSE
;
163 wxTextCtrl::~wxTextCtrl()
165 if (m_privateContextMenu
)
167 delete m_privateContextMenu
;
168 m_privateContextMenu
= NULL
;
172 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
173 const wxString
& value
,
177 const wxValidator
& validator
,
178 const wxString
& name
)
180 // base initialization
181 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
185 parent
->AddChild(this);
187 // translate wxWin style flags to MSW ones
188 WXDWORD msStyle
= MSWGetCreateWindowFlags();
190 // do create the control - either an EDIT or RICHEDIT
191 wxString windowClass
= wxT("EDIT");
194 if ( m_windowStyle
& wxTE_AUTO_URL
)
196 // automatic URL detection only works in RichEdit 2.0+
197 m_windowStyle
|= wxTE_RICH2
;
200 if ( m_windowStyle
& wxTE_RICH2
)
202 // using richedit 2.0 implies using wxTE_RICH
203 m_windowStyle
|= wxTE_RICH
;
206 // we need to load the richedit DLL before creating the rich edit control
207 if ( m_windowStyle
& wxTE_RICH
)
209 static bool s_errorGiven
= FALSE
;// MT-FIXME
211 // Which version do we need? Use 1.0 by default because it is much more
212 // like the the standard EDIT or 2.0 if explicitly requested, but use
213 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
215 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
216 // (and thus EDIT) much better than RichEdit 2.0 so we probably
217 // should use 3.0 if available as it is the best of both worlds -
218 // but as I can't test it right now I don't do it (VZ)
220 const int verRichEdit
= 2;
221 #else // !wxUSE_UNICODE
222 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
223 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
225 // only give the error msg once if the DLL can't be loaded
228 // try to load the RichEdit DLL (will do nothing if already done)
229 if ( !wxRichEditModule::Load(verRichEdit
) )
232 // try another version?
233 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
235 if ( !wxRichEditModule::Load(verRichEdit
) )
236 #endif // wxUSE_UNICODE
238 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
245 // have we managed to load any richedit version?
248 m_verRichEdit
= verRichEdit
;
249 if ( m_verRichEdit
== 1 )
251 windowClass
= wxT("RICHEDIT");
255 #ifndef RICHEDIT_CLASS
256 wxString RICHEDIT_CLASS
;
257 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
259 RICHEDIT_CLASS
+= _T('W');
261 RICHEDIT_CLASS
+= _T('A');
262 #endif // Unicode/ANSI
263 #endif // !RICHEDIT_CLASS
265 windowClass
= RICHEDIT_CLASS
;
269 #endif // wxUSE_RICHEDIT
271 // we need to turn '\n's into "\r\n"s for the multiline controls
273 if ( m_windowStyle
& wxTE_MULTILINE
)
275 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
282 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
285 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
290 // enable the events we're interested in: we want to get EN_CHANGE as
291 // for the normal controls
292 LPARAM mask
= ENM_CHANGE
;
294 if ( GetRichVersion() == 1 )
296 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
297 // explained in its handler
298 mask
|= ENM_MOUSEEVENTS
;
300 else if ( m_windowStyle
& wxTE_AUTO_URL
)
304 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
307 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
309 #endif // wxUSE_RICHEDIT
314 // Make sure the window style (etc.) reflects the HWND style (roughly)
315 void wxTextCtrl::AdoptAttributesFromHWND()
317 wxWindow::AdoptAttributesFromHWND();
319 HWND hWnd
= GetHwnd();
320 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
322 // retrieve the style to see whether this is an edit or richedit ctrl
324 wxString classname
= wxGetWindowClass(GetHWND());
326 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
333 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
335 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
340 #endif // wxUSE_RICHEDIT
342 if (style
& ES_MULTILINE
)
343 m_windowStyle
|= wxTE_MULTILINE
;
344 if (style
& ES_PASSWORD
)
345 m_windowStyle
|= wxTE_PASSWORD
;
346 if (style
& ES_READONLY
)
347 m_windowStyle
|= wxTE_READONLY
;
348 if (style
& ES_WANTRETURN
)
349 m_windowStyle
|= wxTE_PROCESS_ENTER
;
350 if (style
& ES_CENTER
)
351 m_windowStyle
|= wxTE_CENTRE
;
352 if (style
& ES_RIGHT
)
353 m_windowStyle
|= wxTE_RIGHT
;
356 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
358 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
360 // styles which we alaways add by default
361 if ( style
& wxTE_MULTILINE
)
363 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
364 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
366 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
367 if ( !(style
& wxTE_NO_VSCROLL
) )
369 // always adjust the vertical scrollbar automatically if we have it
370 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
373 // we have to use this style for the rich edit controls because
374 // without it the vertical scrollbar never appears at all in
375 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
376 if ( style
& wxTE_RICH2
)
378 msStyle
|= ES_DISABLENOSCROLL
;
380 #endif // wxUSE_RICHEDIT
383 style
|= wxTE_PROCESS_ENTER
;
387 // there is really no reason to not have this style for single line
389 msStyle
|= ES_AUTOHSCROLL
;
392 // styles which we add depending on the specified wxWindows styles
393 if ( style
& wxHSCROLL
)
395 // automatically scroll the control horizontally as necessary
396 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
399 if ( style
& wxTE_READONLY
)
400 msStyle
|= ES_READONLY
;
402 if ( style
& wxTE_PASSWORD
)
403 msStyle
|= ES_PASSWORD
;
405 if ( style
& wxTE_NOHIDESEL
)
406 msStyle
|= ES_NOHIDESEL
;
408 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
409 if ( style
& wxTE_CENTRE
)
410 msStyle
|= ES_CENTER
;
411 else if ( style
& wxTE_RIGHT
)
414 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
419 void wxTextCtrl::SetWindowStyleFlag(long style
)
422 // we have to deal with some styles separately because they can't be
423 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
424 // using richedit-specific EM_SETOPTIONS
426 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
428 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
430 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
431 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
433 #endif // wxUSE_RICHEDIT
435 wxControl::SetWindowStyleFlag(style
);
438 // ----------------------------------------------------------------------------
439 // set/get the controls text
440 // ----------------------------------------------------------------------------
442 wxString
wxTextCtrl::GetValue() const
444 // range 0..-1 is special for GetRange() and means to retrieve all text
445 return GetRange(0, -1);
448 wxString
wxTextCtrl::GetRange(long from
, long to
) const
452 if ( from
>= to
&& to
!= -1 )
454 // nothing to retrieve
461 int len
= GetWindowTextLength(GetHwnd());
464 // alloc one extra WORD as needed by the control
465 wxChar
*p
= str
.GetWriteBuf(++len
);
468 textRange
.chrg
.cpMin
= from
;
469 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
470 textRange
.lpstrText
= p
;
472 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
474 if ( m_verRichEdit
> 1 )
476 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
477 // neither Unix nor Windows style - convert it to something
481 if ( *p
== _T('\r') )
488 if ( m_verRichEdit
== 1 )
490 // convert to the canonical form - see comment below
491 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
494 //else: no text at all, leave the string empty
497 #endif // wxUSE_RICHEDIT
500 str
= wxGetWindowText(GetHWND());
502 // need only a range?
505 str
= str
.Mid(from
, to
- from
);
508 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
509 // canonical one (same one as above) for consistency with the other kinds
510 // of controls and, more importantly, with the other ports
511 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
517 void wxTextCtrl::SetValue(const wxString
& value
)
519 // if the text is long enough, it's faster to just set it instead of first
520 // comparing it with the old one (chances are that it will be different
521 // anyhow, this comparison is there to avoid flicker for small single-line
522 // edit controls mostly)
523 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
525 DoWriteText(value
, FALSE
/* not selection only */);
528 // we should reset the modified flag even if the value didn't really change
530 // mark the control as being not dirty - we changed its text, not the
534 // for compatibility, don't move the cursor when doing SetValue()
535 SetInsertionPoint(0);
538 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
540 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
544 wchar_t *wbuf
= (wchar_t *)buf
;
545 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
550 cb
-= sizeof(wchar_t);
551 (*pcb
) += sizeof(wchar_t);
554 *(const wchar_t **)dwCookie
= wpc
;
560 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
562 #if wxUSE_UNICODE_MSLU
563 bool wxTextCtrl::StreamIn(const wxString
& value
,
564 wxFontEncoding
WXUNUSED(encoding
),
567 const wchar_t *wpc
= value
.c_str();
568 #else // !wxUSE_UNICODE_MSLU
569 bool wxTextCtrl::StreamIn(const wxString
& value
,
570 wxFontEncoding encoding
,
573 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
574 // text in the non default charset -- otherwise it thinks it knows better
575 // than we do and always shows it in the default one
577 // first get the Windows code page for this encoding
578 long codepage
= wxEncodingToCodepage(encoding
);
579 if ( codepage
== -1 )
585 // next translate to Unicode using this code page
586 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
589 wxWCharBuffer
wchBuf(len
);
591 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
594 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
595 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
597 wxLogLastError(_T("MultiByteToWideChar"));
600 // finally, stream it in the control
601 const wchar_t *wpc
= wchBuf
;
602 #endif // wxUSE_UNICODE_MSLU
606 eds
.dwCookie
= (DWORD
)&wpc
;
607 // the cast below is needed for broken (very) old mingw32 headers
608 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
610 // we're going to receive 2 EN_CHANGE notifications if we got any selection
611 // (same problem as in DoWriteText())
612 if ( selectionOnly
&& HasSelection() )
614 // so suppress one of them
615 m_suppressNextUpdate
= TRUE
;
618 ::SendMessage(GetHwnd(), EM_STREAMIN
,
621 (selectionOnly
? SFF_SELECTION
: 0),
626 wxLogLastError(_T("EM_STREAMIN"));
631 #endif // !wxUSE_WCHAR_T
636 #endif // wxUSE_RICHEDIT
638 void wxTextCtrl::WriteText(const wxString
& value
)
643 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
646 if ( m_windowStyle
& wxTE_MULTILINE
)
647 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
652 // there are several complications with the rich edit controls here
656 // first, ensure that the new text will be in the default style
657 if ( !m_defaultStyle
.IsDefault() )
660 GetSelection(&start
, &end
);
661 SetStyle(start
, end
, m_defaultStyle
);
664 #if wxUSE_UNICODE_MSLU
665 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
666 // but EM_STREAMIN works
667 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
669 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
671 #endif // wxUSE_UNICODE_MSLU
674 // next check if the text we're inserting must be shown in a non
675 // default charset -- this only works for RichEdit > 1.0
676 if ( GetRichVersion() > 1 )
678 wxFont font
= m_defaultStyle
.GetFont();
684 wxFontEncoding encoding
= font
.GetEncoding();
685 if ( encoding
!= wxFONTENCODING_SYSTEM
)
687 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
691 #endif // !wxUSE_UNICODE
695 #endif // wxUSE_RICHEDIT
697 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
698 // call below which is confusing for the client code and so should be
701 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
702 // and there is a non empty selection currently and (b) rich text
703 // controls in any case
707 #endif // wxUSE_RICHEDIT
708 (selectionOnly
&& HasSelection()) )
710 m_suppressNextUpdate
= TRUE
;
713 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
714 0, (LPARAM
)valueDos
.c_str());
716 // OTOH, non rich text controls don't generate any events at all when
717 // we use WM_SETTEXT -- have to emulate them here
721 #endif // wxUSE_RICHEDIT
724 // Windows already sends an update event for single-line
726 if ( m_windowStyle
& wxTE_MULTILINE
)
734 void wxTextCtrl::AppendText(const wxString
& text
)
736 SetInsertionPointEnd();
741 if ( IsMultiLine() && GetRichVersion() > 1 )
743 // setting the caret to the end and showing it simply doesn't work for
744 // RichEdit 2.0 -- force it to still do what we want
745 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
747 #endif // wxUSE_RICHEDIT
750 void wxTextCtrl::Clear()
752 ::SetWindowText(GetHwnd(), wxEmptyString
);
756 #endif // wxUSE_RICHEDIT
758 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
759 // but the normal ones don't -- make Clear() behaviour consistent by
760 // always sending this event
762 // Windows already sends an update event for single-line
764 if ( m_windowStyle
& wxTE_MULTILINE
)
771 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
775 size_t lenOld
= GetValue().length();
777 wxUint32 code
= event
.GetRawKeyCode();
778 ::keybd_event(code
, 0, 0 /* key press */, 0);
779 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
781 // assume that any alphanumeric key changes the total number of characters
782 // in the control - this should work in 99% of cases
783 return GetValue().length() != lenOld
;
788 // ----------------------------------------------------------------------------
789 // Clipboard operations
790 // ----------------------------------------------------------------------------
792 void wxTextCtrl::Copy()
796 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
800 void wxTextCtrl::Cut()
804 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
808 void wxTextCtrl::Paste()
812 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
816 bool wxTextCtrl::HasSelection() const
819 GetSelection(&from
, &to
);
823 bool wxTextCtrl::CanCopy() const
825 // Can copy if there's a selection
826 return HasSelection();
829 bool wxTextCtrl::CanCut() const
831 return CanCopy() && IsEditable();
834 bool wxTextCtrl::CanPaste() const
842 UINT cf
= 0; // 0 == any format
844 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
846 #endif // wxUSE_RICHEDIT
848 // Standard edit control: check for straight text on clipboard
849 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
852 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
855 return isTextAvailable
;
858 // ----------------------------------------------------------------------------
860 // ----------------------------------------------------------------------------
862 void wxTextCtrl::SetEditable(bool editable
)
864 HWND hWnd
= GetHwnd();
865 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
868 void wxTextCtrl::SetInsertionPoint(long pos
)
870 DoSetSelection(pos
, pos
);
873 void wxTextCtrl::SetInsertionPointEnd()
875 // we must not do anything if the caret is already there because calling
876 // SetInsertionPoint() thaws the controls if Freeze() had been called even
877 // if it doesn't actually move the caret anywhere and so the simple fact of
878 // doing it results in horrible flicker when appending big amounts of text
879 // to the control in a few chunks (see DoAddText() test in the text sample)
880 if ( GetInsertionPoint() == GetLastPosition() )
886 if ( m_verRichEdit
== 1 )
888 // we don't have to waste time calling GetLastPosition() in this case
891 else // !RichEdit 1.0
892 #endif // wxUSE_RICHEDIT
894 pos
= GetLastPosition();
897 SetInsertionPoint(pos
);
900 long wxTextCtrl::GetInsertionPoint() const
908 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
911 #endif // wxUSE_RICHEDIT
913 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
917 long wxTextCtrl::GetLastPosition() const
919 int numLines
= GetNumberOfLines();
920 long posStartLastLine
= XYToPosition(0, numLines
- 1);
922 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
924 return posStartLastLine
+ lenLastLine
;
927 // If the return values from and to are the same, there is no
929 void wxTextCtrl::GetSelection(long* from
, long* to
) const
935 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
937 *from
= charRange
.cpMin
;
938 *to
= charRange
.cpMax
;
941 #endif // !wxUSE_RICHEDIT
943 DWORD dwStart
, dwEnd
;
944 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
951 bool wxTextCtrl::IsEditable() const
953 // strangely enough, we may be called before the control is created: our
954 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
959 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
961 return (style
& ES_READONLY
) == 0;
964 // ----------------------------------------------------------------------------
966 // ----------------------------------------------------------------------------
968 void wxTextCtrl::SetSelection(long from
, long to
)
970 // if from and to are both -1, it means (in wxWindows) that all text should
971 // be selected - translate into Windows convention
972 if ( (from
== -1) && (to
== -1) )
978 DoSetSelection(from
, to
);
981 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
983 HWND hWnd
= GetHwnd();
992 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
995 #endif // wxUSE_RICHEDIT
997 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1003 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1004 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1005 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1006 // option is set (but it does work ok in richedit 1.0 mode...)
1008 // so to make it work we either need to give focus to it here which
1009 // will probably create many problems (dummy focus events; window
1010 // containing the text control being brought to foreground
1011 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1012 // create other problems too -- and in fact it does because if we turn
1013 // on/off this style while appending the text to the control, the
1014 // vertical scrollbar never appears in it even if we append tons of
1015 // text and to work around this the only solution I found was to use
1016 // ES_DISABLENOSCROLL
1018 // this is very ugly but I don't see any other way to make this work
1019 if ( GetRichVersion() > 1 )
1021 if ( !HasFlag(wxTE_NOHIDESEL
) )
1023 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1024 ECOOP_OR
, ECO_NOHIDESEL
);
1026 //else: everything is already ok
1028 #endif // wxUSE_RICHEDIT
1030 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1033 // restore ECO_NOHIDESEL if we changed it
1034 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1036 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1037 ECOOP_AND
, ~ECO_NOHIDESEL
);
1039 #endif // wxUSE_RICHEDIT
1042 // WPARAM is 0: selection is scrolled into view
1043 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1047 // ----------------------------------------------------------------------------
1049 // ----------------------------------------------------------------------------
1051 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1053 // Set selection and remove it
1054 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1056 SendMessage(GetHwnd(), EM_REPLACESEL
,
1062 (LPARAM
)value
.c_str());
1065 void wxTextCtrl::Remove(long from
, long to
)
1067 Replace(from
, to
, wxEmptyString
);
1070 bool wxTextCtrl::LoadFile(const wxString
& file
)
1072 if ( wxTextCtrlBase::LoadFile(file
) )
1074 // update the size limit if needed
1083 bool wxTextCtrl::IsModified() const
1085 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1088 // Makes 'unmodified'
1089 void wxTextCtrl::DiscardEdits()
1091 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1094 int wxTextCtrl::GetNumberOfLines() const
1096 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1099 long wxTextCtrl::XYToPosition(long x
, long y
) const
1101 // This gets the char index for the _beginning_ of this line
1102 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1104 return charIndex
+ x
;
1107 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1109 HWND hWnd
= GetHwnd();
1111 // This gets the line number containing the character
1116 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1119 #endif // wxUSE_RICHEDIT
1121 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1130 // This gets the char index for the _beginning_ of this line
1131 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1132 if ( charIndex
== -1 )
1137 // The X position must therefore be the different between pos and charIndex
1139 *x
= pos
- charIndex
;
1146 void wxTextCtrl::ShowPosition(long pos
)
1148 HWND hWnd
= GetHwnd();
1150 // To scroll to a position, we pass the number of lines and characters
1151 // to scroll *by*. This means that we need to:
1152 // (1) Find the line position of the current line.
1153 // (2) Find the line position of pos.
1154 // (3) Scroll by (pos - current).
1155 // For now, ignore the horizontal scrolling.
1157 // Is this where scrolling is relative to - the line containing the caret?
1158 // Or is the first visible line??? Try first visible line.
1159 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1161 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1163 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1165 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1167 if (linesToScroll
!= 0)
1168 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1171 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1173 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1176 int wxTextCtrl::GetLineLength(long lineNo
) const
1178 long pos
= XYToPosition(0, lineNo
);
1180 return GetLengthOfLineContainingPos(pos
);
1183 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1185 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1187 // there must be at least enough place for the length WORD in the
1189 len
+= sizeof(WORD
);
1192 wxChar
*buf
= str
.GetWriteBuf(len
);
1194 *(WORD
*)buf
= (WORD
)len
;
1195 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1198 str
.UngetWriteBuf(len
);
1203 void wxTextCtrl::SetMaxLength(unsigned long len
)
1205 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1208 // ----------------------------------------------------------------------------
1210 // ----------------------------------------------------------------------------
1212 void wxTextCtrl::Undo()
1216 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1220 void wxTextCtrl::Redo()
1224 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1225 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1229 bool wxTextCtrl::CanUndo() const
1231 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1234 bool wxTextCtrl::CanRedo() const
1236 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1239 // ----------------------------------------------------------------------------
1240 // implemenation details
1241 // ----------------------------------------------------------------------------
1243 void wxTextCtrl::Command(wxCommandEvent
& event
)
1245 SetValue(event
.GetString());
1246 ProcessCommand (event
);
1249 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1251 // By default, load the first file into the text window.
1252 if (event
.GetNumberOfFiles() > 0)
1254 LoadFile(event
.GetFiles()[0]);
1258 // ----------------------------------------------------------------------------
1259 // kbd input processing
1260 // ----------------------------------------------------------------------------
1262 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1264 MSG
*msg
= (MSG
*)pMsg
;
1266 // check for our special keys here: if we don't do it and the parent frame
1267 // uses them as accelerators, they wouldn't work at all, so we disable
1268 // usual preprocessing for them
1269 if ( msg
->message
== WM_KEYDOWN
)
1271 WORD vkey
= (WORD
) msg
->wParam
;
1272 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1274 if ( vkey
== VK_BACK
)
1279 // we want to process some Ctrl-foo and Shift-bar but no key
1280 // combinations without either Ctrl or Shift nor with both of them
1282 const int ctrl
= wxIsCtrlDown(),
1283 shift
= wxIsShiftDown();
1284 switch ( ctrl
+ shift
)
1287 wxFAIL_MSG( _T("how many modifiers have we got?") );
1295 // either Ctrl or Shift pressed
1310 else // Shift is pressed
1312 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1319 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1322 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1324 switch ( event
.GetKeyCode() )
1327 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1329 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1330 InitCommandEvent(event
);
1331 event
.SetString(GetValue());
1332 if ( GetEventHandler()->ProcessEvent(event
) )
1335 //else: multiline controls need Enter for themselves
1340 // always produce navigation event - even if we process TAB
1341 // ourselves the fact that we got here means that the user code
1342 // decided to skip processing of this TAB - probably to let it
1343 // do its default job.
1345 wxNavigationKeyEvent eventNav
;
1346 eventNav
.SetDirection(!event
.ShiftDown());
1347 eventNav
.SetWindowChange(event
.ControlDown());
1348 eventNav
.SetEventObject(this);
1350 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1356 // no, we didn't process it
1360 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1362 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1364 if ( nMsg
== WM_GETDLGCODE
)
1366 // we always want the chars and the arrows: the arrows for navigation
1367 // and the chars because we want Ctrl-C to work even in a read only
1369 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1373 // we may have several different cases:
1374 // 1. normal case: both TAB and ENTER are used for dlg navigation
1375 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1376 // next control in the dialog
1377 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1379 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1380 // to the next control
1382 // the multiline edit control should always get <Return> for itself
1383 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1384 lDlgCode
|= DLGC_WANTMESSAGE
;
1386 if ( HasFlag(wxTE_PROCESS_TAB
) )
1387 lDlgCode
|= DLGC_WANTTAB
;
1393 // NB: use "=", not "|=" as the base class version returns the
1394 // same flags is this state as usual (i.e. including
1395 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1396 // native Win32 apps?) but for now live with it.
1404 // ----------------------------------------------------------------------------
1405 // text control event processing
1406 // ----------------------------------------------------------------------------
1408 bool wxTextCtrl::SendUpdateEvent()
1410 // is event reporting suspended?
1411 if ( m_suppressNextUpdate
)
1413 // do process the next one
1414 m_suppressNextUpdate
= FALSE
;
1419 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1420 InitCommandEvent(event
);
1421 event
.SetString(GetValue());
1423 return ProcessCommand(event
);
1426 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1433 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1436 event
.SetEventObject(this);
1437 GetEventHandler()->ProcessEvent(event
);
1446 // the text size limit has been hit -- try to increase it
1447 if ( !AdjustSpaceLimit() )
1449 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1450 InitCommandEvent(event
);
1451 event
.SetString(GetValue());
1452 ProcessCommand(event
);
1456 // the other edit notification messages are not processed
1465 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1471 WXUINT
WXUNUSED(message
),
1472 WXWPARAM
WXUNUSED(wParam
),
1473 WXLPARAM
WXUNUSED(lParam
)
1480 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1481 return (WXHBRUSH
) hbrush
;
1483 #endif // wxUSE_CTL3D
1486 wxColour colBack
= GetBackgroundColour();
1488 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1489 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1491 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1492 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1494 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1496 return (WXHBRUSH
)brush
->GetResourceHandle();
1499 // In WIN16, need to override normal erasing because
1500 // Ctl3D doesn't use the wxWindows background colour.
1502 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1504 wxColour
col(m_backgroundColour
);
1508 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1512 ::GetClientRect(GetHwnd(), &rect
);
1514 COLORREF ref
= wxColourToRGB(col
);
1515 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1517 wxLogLastError(wxT("CreateSolidBrush"));
1519 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1521 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1523 ::FillRect(hdc
, &rect
, hBrush
);
1524 ::DeleteObject(hBrush
);
1525 ::SetMapMode(hdc
, mode
);
1530 bool wxTextCtrl::AdjustSpaceLimit()
1533 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1535 // HACK: we try to automatically extend the limit for the amount of text
1536 // to allow (interactively) entering more than 64Kb of text under
1537 // Win9x but we shouldn't reset the text limit which was previously
1538 // set explicitly with SetMaxLength()
1540 // we could solve this by storing the limit we set in wxTextCtrl but
1541 // to save space we prefer to simply test here the actual limit
1542 // value: we consider that SetMaxLength() can only be called for
1544 if ( limit
< 0x8000 )
1546 // we've got more text than limit set by SetMaxLength()
1550 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1553 limit
= len
+ 0x8000; // 32Kb
1558 // as a nice side effect, this also allows passing limit > 64Kb
1559 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1562 #endif // wxUSE_RICHEDIT
1564 if ( limit
> 0xffff )
1566 // this will set it to a platform-dependent maximum (much more
1567 // than 64Kb under NT)
1571 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1576 // we changed the limit
1580 bool wxTextCtrl::AcceptsFocus() const
1582 // we don't want focus if we can't be edited unless we're a multiline
1583 // control because then it might be still nice to get focus from keyboard
1584 // to be able to scroll it without mouse
1585 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1588 wxSize
wxTextCtrl::DoGetBestSize() const
1591 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1593 int wText
= DEFAULT_ITEM_WIDTH
;
1595 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1596 if ( m_windowStyle
& wxTE_MULTILINE
)
1598 hText
*= wxMax(GetNumberOfLines(), 5);
1600 //else: for single line control everything is ok
1602 return wxSize(wText
, hText
);
1605 // ----------------------------------------------------------------------------
1606 // standard handlers for standard edit menu events
1607 // ----------------------------------------------------------------------------
1609 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1614 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1619 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1624 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1629 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1634 void wxTextCtrl::OnDelete(wxCommandEvent
& event
)
1637 GetSelection(& from
, & to
);
1638 if (from
!= -1 && to
!= -1)
1642 void wxTextCtrl::OnSelectAll(wxCommandEvent
& event
)
1644 SetSelection(-1, -1);
1647 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1649 event
.Enable( CanCut() );
1652 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1654 event
.Enable( CanCopy() );
1657 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1659 event
.Enable( CanPaste() );
1662 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1664 event
.Enable( CanUndo() );
1667 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1669 event
.Enable( CanRedo() );
1672 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1675 GetSelection(& from
, & to
);
1676 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1679 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1681 event
.Enable(GetLastPosition() > 0);
1684 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1689 if (!m_privateContextMenu
)
1691 m_privateContextMenu
= new wxMenu
;
1692 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1693 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1694 m_privateContextMenu
->AppendSeparator();
1695 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1696 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1697 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1698 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1699 m_privateContextMenu
->AppendSeparator();
1700 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1702 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1710 // the rest of the file only deals with the rich edit controls
1713 // ----------------------------------------------------------------------------
1714 // EN_LINK processing
1715 // ----------------------------------------------------------------------------
1717 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1719 NMHDR
*hdr
= (NMHDR
* )lParam
;
1720 switch ( hdr
->code
)
1724 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1725 UINT msg
= msgf
->msg
;
1727 // this is a bit crazy but richedit 1.0 sends us all mouse
1728 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1729 // generate the wxWin events for this message manually
1731 // NB: in fact, this is still not totally correct as it does
1732 // send us WM_LBUTTONUP if the selection was cleared by the
1733 // last click -- so currently we get 2 events in this case,
1734 // but as I don't see any obvious way to check for this I
1735 // leave this code in place because it's still better than
1736 // not getting left up events at all
1737 if ( msg
== WM_LBUTTONUP
)
1739 WXUINT flags
= msgf
->wParam
;
1740 int x
= GET_X_LPARAM(msgf
->lParam
),
1741 y
= GET_Y_LPARAM(msgf
->lParam
);
1743 HandleMouseEvent(msg
, x
, y
, flags
);
1747 // return TRUE to process the event (and FALSE to ignore it)
1752 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1754 switch ( enlink
->msg
)
1757 // ok, so it is hardcoded - do we really nee to
1759 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1764 case WM_LBUTTONDOWN
:
1766 case WM_LBUTTONDBLCLK
:
1767 case WM_RBUTTONDOWN
:
1769 case WM_RBUTTONDBLCLK
:
1770 // send a mouse event
1772 static const wxEventType eventsMouse
[] =
1783 // the event ids are consecutive
1785 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1787 InitMouseEvent(evtMouse
,
1788 GET_X_LPARAM(enlink
->lParam
),
1789 GET_Y_LPARAM(enlink
->lParam
),
1792 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1794 enlink
->chrg
.cpMax
);
1796 InitCommandEvent(event
);
1798 *result
= ProcessCommand(event
);
1806 // not processed, leave it to the base class
1807 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1810 // ----------------------------------------------------------------------------
1811 // colour setting for the rich edit controls
1812 // ----------------------------------------------------------------------------
1814 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1816 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1818 // colour didn't really change
1824 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1825 // EM_SETBKGNDCOLOR additionally
1826 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1832 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1834 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1836 // colour didn't really change
1842 // change the colour of everything
1845 cf
.cbSize
= sizeof(cf
);
1846 cf
.dwMask
= CFM_COLOR
;
1847 cf
.crTextColor
= wxColourToRGB(colour
);
1848 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1854 // ----------------------------------------------------------------------------
1855 // styling support for rich edit controls
1856 // ----------------------------------------------------------------------------
1860 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1864 // can't do it with normal text control
1868 // the richedit 1.0 doesn't handle setting background colour, so don't
1869 // even try to do anything if it's the only thing we want to change
1870 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1871 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1874 // nothing to do: return TRUE if there was really nothing to do and
1875 // FALSE if we failed to set bg colour
1876 return !style
.HasBackgroundColour();
1879 // order the range if needed
1887 // we can only change the format of the selection, so select the range we
1888 // want and restore the old selection later
1889 long startOld
, endOld
;
1890 GetSelection(&startOld
, &endOld
);
1892 // but do we really have to change the selection?
1893 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1897 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1900 // initialize CHARFORMAT struct
1909 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1910 // CHARFORMAT in that case
1912 if ( m_verRichEdit
== 1 )
1914 // this is the only thing the control is going to grok
1915 cf
.cbSize
= sizeof(CHARFORMAT
);
1920 // CHARFORMAT or CHARFORMAT2
1921 cf
.cbSize
= sizeof(cf
);
1924 if ( style
.HasFont() )
1926 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1927 // but using it doesn't seem to hurt neither so leaving it for now
1929 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1930 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1932 // fill in data from LOGFONT but recalculate lfHeight because we need
1933 // the real height in twips and not the negative number which
1934 // wxFillLogFont() returns (this is correct in general and works with
1935 // the Windows font mapper, but not here)
1937 wxFillLogFont(&lf
, &style
.GetFont());
1938 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1939 cf
.bCharSet
= lf
.lfCharSet
;
1940 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1941 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1943 // also deal with underline/italic/bold attributes: note that we must
1944 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1945 // style to allow clearing it
1948 cf
.dwEffects
|= CFE_ITALIC
;
1951 if ( lf
.lfWeight
== FW_BOLD
)
1953 cf
.dwEffects
|= CFE_BOLD
;
1956 if ( lf
.lfUnderline
)
1958 cf
.dwEffects
|= CFE_UNDERLINE
;
1961 // strikeout fonts are not supported by wxWindows
1964 if ( style
.HasTextColour() )
1966 cf
.dwMask
|= CFM_COLOR
;
1967 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1971 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1973 cf
.dwMask
|= CFM_BACKCOLOR
;
1974 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1976 #endif // wxUSE_RICHEDIT2
1978 // do format the selection
1979 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1980 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1983 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1986 // now do the paragraph formatting
1989 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
1990 // PARAFORMAT in that case
1992 if ( m_verRichEdit
== 1 )
1994 // this is the only thing the control is going to grok
1995 pf
.cbSize
= sizeof(PARAFORMAT
);
2000 // PARAFORMAT or PARAFORMAT2
2001 pf
.cbSize
= sizeof(pf
);
2004 if (style
.HasAlignment())
2006 pf
.dwMask
|= PFM_ALIGNMENT
;
2007 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2008 pf
.wAlignment
= PFA_RIGHT
;
2009 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2010 pf
.wAlignment
= PFA_CENTER
;
2011 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2012 pf
.wAlignment
= PFA_JUSTIFY
;
2014 pf
.wAlignment
= PFA_LEFT
;
2017 if (style
.HasLeftIndent())
2019 pf
.dwMask
|= PFM_STARTINDENT
;
2021 // Convert from 1/10 mm to TWIPS
2022 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2024 // TODO: do we need to specify dxOffset?
2027 if (style
.HasRightIndent())
2029 pf
.dwMask
|= PFM_RIGHTINDENT
;
2031 // Convert from 1/10 mm to TWIPS
2032 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2035 if (style
.HasTabs())
2037 pf
.dwMask
|= PFM_TABSTOPS
;
2039 const wxArrayInt
& tabs
= style
.GetTabs();
2041 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2043 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2045 // Convert from 1/10 mm to TWIPS
2046 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2052 // do format the selection
2053 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2054 0, (LPARAM
) &pf
) != 0;
2057 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2063 // restore the original selection
2064 DoSetSelection(startOld
, endOld
, FALSE
);
2070 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2072 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2075 // we have to do this or the style wouldn't apply for the text typed by the
2077 long posLast
= GetLastPosition();
2078 SetStyle(posLast
, posLast
, m_defaultStyle
);
2083 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2087 // can't do it with normal text control
2091 // initialize CHARFORMAT struct
2100 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2101 // CHARFORMAT in that case
2103 if ( m_verRichEdit
== 1 )
2105 // this is the only thing the control is going to grok
2106 cf
.cbSize
= sizeof(CHARFORMAT
);
2111 // CHARFORMAT or CHARFORMAT2
2112 cf
.cbSize
= sizeof(cf
);
2114 // we can only change the format of the selection, so select the range we
2115 // want and restore the old selection later
2116 long startOld
, endOld
;
2117 GetSelection(&startOld
, &endOld
);
2119 // but do we really have to change the selection?
2120 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2124 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2127 // get the selection formatting
2128 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2129 SCF_SELECTION
, (LPARAM
)&cf
) ;
2132 lf
.lfHeight
= cf
.yHeight
;
2134 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2135 lf
.lfClipPrecision
= 0;
2136 lf
.lfEscapement
= 0;
2137 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2138 if (cf
.dwEffects
& CFE_ITALIC
)
2140 lf
.lfOrientation
= 0;
2141 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2143 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2144 lf
.lfStrikeOut
= TRUE
;
2145 if (cf
.dwEffects
& CFE_UNDERLINE
)
2146 lf
.lfUnderline
= TRUE
;
2147 if (cf
.dwEffects
& CFE_BOLD
)
2148 lf
.lfWeight
= FW_BOLD
;
2150 wxFont font
= wxCreateFontFromLogFont(& lf
);
2153 style
.SetFont(font
);
2155 style
.SetTextColour(wxColour(cf
.crTextColor
));
2158 if ( m_verRichEdit
!= 1 )
2160 // cf.dwMask |= CFM_BACKCOLOR;
2161 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2163 #endif // wxUSE_RICHEDIT2
2165 // now get the paragraph formatting
2168 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2169 // PARAFORMAT in that case
2171 if ( m_verRichEdit
== 1 )
2173 // this is the only thing the control is going to grok
2174 pf
.cbSize
= sizeof(PARAFORMAT
);
2179 // PARAFORMAT or PARAFORMAT2
2180 pf
.cbSize
= sizeof(pf
);
2183 // do format the selection
2184 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2186 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2187 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2189 if (pf
.wAlignment
== PFA_CENTER
)
2190 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2191 else if (pf
.wAlignment
== PFA_RIGHT
)
2192 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2193 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2194 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2196 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2198 wxArrayInt tabStops
;
2200 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2202 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2207 // restore the original selection
2208 DoSetSelection(startOld
, endOld
, FALSE
);
2216 // ----------------------------------------------------------------------------
2218 // ----------------------------------------------------------------------------
2220 bool wxRichEditModule::OnInit()
2222 // don't do anything - we will load it when needed
2226 void wxRichEditModule::OnExit()
2228 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2230 if ( ms_hRichEdit
[i
] )
2232 ::FreeLibrary(ms_hRichEdit
[i
]);
2238 bool wxRichEditModule::Load(int version
)
2240 // we don't support loading richedit 3.0 as I don't know how to distinguish
2241 // it from 2.0 anyhow
2242 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2243 _T("incorrect richedit control version requested") );
2245 // make it the index in the array
2248 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2250 // we had already tried to load it and failed
2254 if ( ms_hRichEdit
[version
] )
2256 // we've already got this one
2260 wxString dllname
= version
? _T("riched20") : _T("riched32");
2261 dllname
+= _T(".dll");
2263 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2265 if ( !ms_hRichEdit
[version
] )
2267 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2269 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2277 #endif // wxUSE_RICHEDIT
2279 #endif // wxUSE_TEXTCTRL