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(), wxT(""));
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
, _T(""));
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 if (GetParent()->GetTransparentBackground())
1487 SetBkMode(hdc
, TRANSPARENT
);
1489 SetBkMode(hdc
, OPAQUE
);
1491 wxColour colBack
= GetBackgroundColour();
1493 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1494 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1496 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1497 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1499 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1501 return (WXHBRUSH
)brush
->GetResourceHandle();
1504 // In WIN16, need to override normal erasing because
1505 // Ctl3D doesn't use the wxWindows background colour.
1507 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1509 wxColour
col(m_backgroundColour
);
1513 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1517 ::GetClientRect(GetHwnd(), &rect
);
1519 COLORREF ref
= wxColourToRGB(col
);
1520 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1522 wxLogLastError(wxT("CreateSolidBrush"));
1524 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1526 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1528 ::FillRect(hdc
, &rect
, hBrush
);
1529 ::DeleteObject(hBrush
);
1530 ::SetMapMode(hdc
, mode
);
1535 bool wxTextCtrl::AdjustSpaceLimit()
1538 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1540 // HACK: we try to automatically extend the limit for the amount of text
1541 // to allow (interactively) entering more than 64Kb of text under
1542 // Win9x but we shouldn't reset the text limit which was previously
1543 // set explicitly with SetMaxLength()
1545 // we could solve this by storing the limit we set in wxTextCtrl but
1546 // to save space we prefer to simply test here the actual limit
1547 // value: we consider that SetMaxLength() can only be called for
1549 if ( limit
< 0x8000 )
1551 // we've got more text than limit set by SetMaxLength()
1555 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1558 limit
= len
+ 0x8000; // 32Kb
1563 // as a nice side effect, this also allows passing limit > 64Kb
1564 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1567 #endif // wxUSE_RICHEDIT
1569 if ( limit
> 0xffff )
1571 // this will set it to a platform-dependent maximum (much more
1572 // than 64Kb under NT)
1576 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1581 // we changed the limit
1585 bool wxTextCtrl::AcceptsFocus() const
1587 // we don't want focus if we can't be edited unless we're a multiline
1588 // control because then it might be still nice to get focus from keyboard
1589 // to be able to scroll it without mouse
1590 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1593 wxSize
wxTextCtrl::DoGetBestSize() const
1596 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1598 int wText
= DEFAULT_ITEM_WIDTH
;
1600 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1601 if ( m_windowStyle
& wxTE_MULTILINE
)
1603 hText
*= wxMax(GetNumberOfLines(), 5);
1605 //else: for single line control everything is ok
1607 return wxSize(wText
, hText
);
1610 // ----------------------------------------------------------------------------
1611 // standard handlers for standard edit menu events
1612 // ----------------------------------------------------------------------------
1614 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1619 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1624 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1629 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1634 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1639 void wxTextCtrl::OnDelete(wxCommandEvent
& event
)
1642 GetSelection(& from
, & to
);
1643 if (from
!= -1 && to
!= -1)
1647 void wxTextCtrl::OnSelectAll(wxCommandEvent
& event
)
1649 SetSelection(-1, -1);
1652 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1654 event
.Enable( CanCut() );
1657 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1659 event
.Enable( CanCopy() );
1662 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1664 event
.Enable( CanPaste() );
1667 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1669 event
.Enable( CanUndo() );
1672 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1674 event
.Enable( CanRedo() );
1677 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1680 GetSelection(& from
, & to
);
1681 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1684 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1686 event
.Enable(GetLastPosition() > 0);
1689 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1694 if (!m_privateContextMenu
)
1696 m_privateContextMenu
= new wxMenu
;
1697 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1698 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1699 m_privateContextMenu
->AppendSeparator();
1700 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1701 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1702 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1703 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1704 m_privateContextMenu
->AppendSeparator();
1705 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1707 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1715 // the rest of the file only deals with the rich edit controls
1718 // ----------------------------------------------------------------------------
1719 // EN_LINK processing
1720 // ----------------------------------------------------------------------------
1722 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1724 NMHDR
*hdr
= (NMHDR
* )lParam
;
1725 switch ( hdr
->code
)
1729 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1730 UINT msg
= msgf
->msg
;
1732 // this is a bit crazy but richedit 1.0 sends us all mouse
1733 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1734 // generate the wxWin events for this message manually
1736 // NB: in fact, this is still not totally correct as it does
1737 // send us WM_LBUTTONUP if the selection was cleared by the
1738 // last click -- so currently we get 2 events in this case,
1739 // but as I don't see any obvious way to check for this I
1740 // leave this code in place because it's still better than
1741 // not getting left up events at all
1742 if ( msg
== WM_LBUTTONUP
)
1744 WXUINT flags
= msgf
->wParam
;
1745 int x
= GET_X_LPARAM(msgf
->lParam
),
1746 y
= GET_Y_LPARAM(msgf
->lParam
);
1748 HandleMouseEvent(msg
, x
, y
, flags
);
1752 // return TRUE to process the event (and FALSE to ignore it)
1757 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1759 switch ( enlink
->msg
)
1762 // ok, so it is hardcoded - do we really nee to
1764 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1769 case WM_LBUTTONDOWN
:
1771 case WM_LBUTTONDBLCLK
:
1772 case WM_RBUTTONDOWN
:
1774 case WM_RBUTTONDBLCLK
:
1775 // send a mouse event
1777 static const wxEventType eventsMouse
[] =
1788 // the event ids are consecutive
1790 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1792 InitMouseEvent(evtMouse
,
1793 GET_X_LPARAM(enlink
->lParam
),
1794 GET_Y_LPARAM(enlink
->lParam
),
1797 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1799 enlink
->chrg
.cpMax
);
1801 InitCommandEvent(event
);
1803 *result
= ProcessCommand(event
);
1811 // not processed, leave it to the base class
1812 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1815 // ----------------------------------------------------------------------------
1816 // colour setting for the rich edit controls
1817 // ----------------------------------------------------------------------------
1819 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1821 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1823 // colour didn't really change
1829 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1830 // EM_SETBKGNDCOLOR additionally
1831 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1837 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1839 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1841 // colour didn't really change
1847 // change the colour of everything
1850 cf
.cbSize
= sizeof(cf
);
1851 cf
.dwMask
= CFM_COLOR
;
1852 cf
.crTextColor
= wxColourToRGB(colour
);
1853 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1859 // ----------------------------------------------------------------------------
1860 // styling support for rich edit controls
1861 // ----------------------------------------------------------------------------
1865 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1869 // can't do it with normal text control
1873 // the richedit 1.0 doesn't handle setting background colour, so don't
1874 // even try to do anything if it's the only thing we want to change
1875 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1876 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1879 // nothing to do: return TRUE if there was really nothing to do and
1880 // FALSE if we failed to set bg colour
1881 return !style
.HasBackgroundColour();
1884 // order the range if needed
1892 // we can only change the format of the selection, so select the range we
1893 // want and restore the old selection later
1894 long startOld
, endOld
;
1895 GetSelection(&startOld
, &endOld
);
1897 // but do we really have to change the selection?
1898 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1902 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1905 // initialize CHARFORMAT struct
1914 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1915 // CHARFORMAT in that case
1917 if ( m_verRichEdit
== 1 )
1919 // this is the only thing the control is going to grok
1920 cf
.cbSize
= sizeof(CHARFORMAT
);
1925 // CHARFORMAT or CHARFORMAT2
1926 cf
.cbSize
= sizeof(cf
);
1929 if ( style
.HasFont() )
1931 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1932 // but using it doesn't seem to hurt neither so leaving it for now
1934 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1935 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1937 // fill in data from LOGFONT but recalculate lfHeight because we need
1938 // the real height in twips and not the negative number which
1939 // wxFillLogFont() returns (this is correct in general and works with
1940 // the Windows font mapper, but not here)
1942 wxFillLogFont(&lf
, &style
.GetFont());
1943 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1944 cf
.bCharSet
= lf
.lfCharSet
;
1945 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1946 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1948 // also deal with underline/italic/bold attributes: note that we must
1949 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1950 // style to allow clearing it
1953 cf
.dwEffects
|= CFE_ITALIC
;
1956 if ( lf
.lfWeight
== FW_BOLD
)
1958 cf
.dwEffects
|= CFE_BOLD
;
1961 if ( lf
.lfUnderline
)
1963 cf
.dwEffects
|= CFE_UNDERLINE
;
1966 // strikeout fonts are not supported by wxWindows
1969 if ( style
.HasTextColour() )
1971 cf
.dwMask
|= CFM_COLOR
;
1972 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1976 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1978 cf
.dwMask
|= CFM_BACKCOLOR
;
1979 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1981 #endif // wxUSE_RICHEDIT2
1983 // do format the selection
1984 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1985 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1988 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1991 // now do the paragraph formatting
1994 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
1995 // PARAFORMAT in that case
1997 if ( m_verRichEdit
== 1 )
1999 // this is the only thing the control is going to grok
2000 pf
.cbSize
= sizeof(PARAFORMAT
);
2005 // PARAFORMAT or PARAFORMAT2
2006 pf
.cbSize
= sizeof(pf
);
2009 if (style
.HasAlignment())
2011 pf
.dwMask
|= PFM_ALIGNMENT
;
2012 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2013 pf
.wAlignment
= PFA_RIGHT
;
2014 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2015 pf
.wAlignment
= PFA_CENTER
;
2016 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2017 pf
.wAlignment
= PFA_JUSTIFY
;
2019 pf
.wAlignment
= PFA_LEFT
;
2022 if (style
.HasLeftIndent())
2024 pf
.dwMask
|= PFM_STARTINDENT
;
2026 // Convert from 1/10 mm to TWIPS
2027 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2029 // TODO: do we need to specify dxOffset?
2032 if (style
.HasRightIndent())
2034 pf
.dwMask
|= PFM_RIGHTINDENT
;
2036 // Convert from 1/10 mm to TWIPS
2037 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2040 if (style
.HasTabs())
2042 pf
.dwMask
|= PFM_TABSTOPS
;
2044 const wxArrayInt
& tabs
= style
.GetTabs();
2046 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2048 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2050 // Convert from 1/10 mm to TWIPS
2051 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2057 // do format the selection
2058 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2059 0, (LPARAM
) &pf
) != 0;
2062 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2068 // restore the original selection
2069 DoSetSelection(startOld
, endOld
, FALSE
);
2075 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2077 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2080 // we have to do this or the style wouldn't apply for the text typed by the
2082 long posLast
= GetLastPosition();
2083 SetStyle(posLast
, posLast
, m_defaultStyle
);
2088 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2092 // can't do it with normal text control
2096 // initialize CHARFORMAT struct
2105 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2106 // CHARFORMAT in that case
2108 if ( m_verRichEdit
== 1 )
2110 // this is the only thing the control is going to grok
2111 cf
.cbSize
= sizeof(CHARFORMAT
);
2116 // CHARFORMAT or CHARFORMAT2
2117 cf
.cbSize
= sizeof(cf
);
2119 // we can only change the format of the selection, so select the range we
2120 // want and restore the old selection later
2121 long startOld
, endOld
;
2122 GetSelection(&startOld
, &endOld
);
2124 // but do we really have to change the selection?
2125 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2129 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2132 // get the selection formatting
2133 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2134 SCF_SELECTION
, (LPARAM
)&cf
) ;
2137 lf
.lfHeight
= cf
.yHeight
;
2139 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2140 lf
.lfClipPrecision
= 0;
2141 lf
.lfEscapement
= 0;
2142 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2143 if (cf
.dwEffects
& CFE_ITALIC
)
2145 lf
.lfOrientation
= 0;
2146 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2148 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2149 lf
.lfStrikeOut
= TRUE
;
2150 if (cf
.dwEffects
& CFE_UNDERLINE
)
2151 lf
.lfUnderline
= TRUE
;
2152 if (cf
.dwEffects
& CFE_BOLD
)
2153 lf
.lfWeight
= FW_BOLD
;
2155 wxFont font
= wxCreateFontFromLogFont(& lf
);
2158 style
.SetFont(font
);
2160 style
.SetTextColour(wxColour(cf
.crTextColor
));
2163 if ( m_verRichEdit
!= 1 )
2165 // cf.dwMask |= CFM_BACKCOLOR;
2166 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2168 #endif // wxUSE_RICHEDIT2
2170 // now get the paragraph formatting
2173 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2174 // PARAFORMAT in that case
2176 if ( m_verRichEdit
== 1 )
2178 // this is the only thing the control is going to grok
2179 pf
.cbSize
= sizeof(PARAFORMAT
);
2184 // PARAFORMAT or PARAFORMAT2
2185 pf
.cbSize
= sizeof(pf
);
2188 // do format the selection
2189 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2191 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2192 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2194 if (pf
.wAlignment
== PFA_CENTER
)
2195 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2196 else if (pf
.wAlignment
== PFA_RIGHT
)
2197 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2198 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2199 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2201 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2203 wxArrayInt tabStops
;
2205 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2207 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2212 // restore the original selection
2213 DoSetSelection(startOld
, endOld
, FALSE
);
2221 // ----------------------------------------------------------------------------
2223 // ----------------------------------------------------------------------------
2225 bool wxRichEditModule::OnInit()
2227 // don't do anything - we will load it when needed
2231 void wxRichEditModule::OnExit()
2233 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2235 if ( ms_hRichEdit
[i
] )
2237 ::FreeLibrary(ms_hRichEdit
[i
]);
2243 bool wxRichEditModule::Load(int version
)
2245 // we don't support loading richedit 3.0 as I don't know how to distinguish
2246 // it from 2.0 anyhow
2247 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2248 _T("incorrect richedit control version requested") );
2250 // make it the index in the array
2253 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2255 // we had already tried to load it and failed
2259 if ( ms_hRichEdit
[version
] )
2261 // we've already got this one
2265 wxString dllname
= version
? _T("riched20") : _T("riched32");
2266 dllname
+= _T(".dll");
2268 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2270 if ( !ms_hRichEdit
[version
] )
2272 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2274 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2282 #endif // wxUSE_RICHEDIT
2284 #endif // wxUSE_TEXTCTRL