1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "textctrl.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
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 // old mingw32 doesn't define this
70 #define CFM_CHARSET 0x08000000
74 #define CFM_BACKCOLOR 0x04000000
77 // cygwin does not have these defined for richedit
79 #define ENM_LINK 0x04000000
82 #ifndef EM_AUTOURLDETECT
83 #define EM_AUTOURLDETECT (WM_USER + 91)
87 #define EN_LINK 0x070b
89 typedef struct _enlink
100 #define SF_UNICODE 0x0010
103 // Watcom C++ doesn't define this
105 #define SCF_ALL 0x0004
108 #endif // wxUSE_RICHEDIT
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
116 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
118 #endif // wxUSE_RICHEDIT
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
126 // this module initializes RichEdit DLL(s) if needed
127 class wxRichEditModule
: public wxModule
130 virtual bool OnInit();
131 virtual void OnExit();
133 // load the richedit DLL of at least of required version
134 static bool Load(int version
= 1);
137 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
138 static HINSTANCE ms_hRichEdit
[2];
140 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
143 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
145 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
147 #endif // wxUSE_RICHEDIT
149 // ----------------------------------------------------------------------------
150 // event tables and other macros
151 // ----------------------------------------------------------------------------
153 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
155 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
156 EVT_CHAR(wxTextCtrl::OnChar
)
157 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
160 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
163 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
164 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
165 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
166 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
167 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
168 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
169 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
171 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
172 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
173 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
174 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
175 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
176 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
177 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
179 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
183 // ============================================================================
185 // ============================================================================
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 void wxTextCtrl::Init()
195 #endif // wxUSE_RICHEDIT
197 m_privateContextMenu
= NULL
;
198 m_suppressNextUpdate
= FALSE
;
201 wxTextCtrl::~wxTextCtrl()
203 if (m_privateContextMenu
)
205 delete m_privateContextMenu
;
206 m_privateContextMenu
= NULL
;
210 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
211 const wxString
& value
,
215 const wxValidator
& validator
,
216 const wxString
& name
)
218 // base initialization
219 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
223 parent
->AddChild(this);
225 // translate wxWin style flags to MSW ones
226 WXDWORD msStyle
= MSWGetCreateWindowFlags();
228 // do create the control - either an EDIT or RICHEDIT
229 wxString windowClass
= wxT("EDIT");
232 if ( m_windowStyle
& wxTE_AUTO_URL
)
234 // automatic URL detection only works in RichEdit 2.0+
235 m_windowStyle
|= wxTE_RICH2
;
238 if ( m_windowStyle
& wxTE_RICH2
)
240 // using richedit 2.0 implies using wxTE_RICH
241 m_windowStyle
|= wxTE_RICH
;
244 // we need to load the richedit DLL before creating the rich edit control
245 if ( m_windowStyle
& wxTE_RICH
)
247 static bool s_errorGiven
= FALSE
;// MT-FIXME
249 // Which version do we need? Use 1.0 by default because it is much more
250 // like the the standard EDIT or 2.0 if explicitly requested, but use
251 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
253 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
254 // (and thus EDIT) much better than RichEdit 2.0 so we probably
255 // should use 3.0 if available as it is the best of both worlds -
256 // but as I can't test it right now I don't do it (VZ)
258 const int verRichEdit
= 2;
259 #else // !wxUSE_UNICODE
260 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
261 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
263 // only give the error msg once if the DLL can't be loaded
266 // try to load the RichEdit DLL (will do nothing if already done)
267 if ( !wxRichEditModule::Load(verRichEdit
) )
270 // try another version?
271 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
273 if ( !wxRichEditModule::Load(verRichEdit
) )
274 #endif // wxUSE_UNICODE
276 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
283 // have we managed to load any richedit version?
286 m_verRichEdit
= verRichEdit
;
287 if ( m_verRichEdit
== 1 )
289 windowClass
= wxT("RICHEDIT");
293 #ifndef RICHEDIT_CLASS
294 wxString RICHEDIT_CLASS
;
295 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
297 RICHEDIT_CLASS
+= _T('W');
299 RICHEDIT_CLASS
+= _T('A');
300 #endif // Unicode/ANSI
301 #endif // !RICHEDIT_CLASS
303 windowClass
= RICHEDIT_CLASS
;
307 #endif // wxUSE_RICHEDIT
309 // we need to turn '\n's into "\r\n"s for the multiline controls
311 if ( m_windowStyle
& wxTE_MULTILINE
)
313 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
320 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
323 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
328 // enable the events we're interested in: we want to get EN_CHANGE as
329 // for the normal controls
330 LPARAM mask
= ENM_CHANGE
;
332 if ( GetRichVersion() == 1 )
334 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
335 // explained in its handler
336 mask
|= ENM_MOUSEEVENTS
;
338 else if ( m_windowStyle
& wxTE_AUTO_URL
)
342 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
345 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
347 #endif // wxUSE_RICHEDIT
352 // Make sure the window style (etc.) reflects the HWND style (roughly)
353 void wxTextCtrl::AdoptAttributesFromHWND()
355 wxWindow::AdoptAttributesFromHWND();
357 HWND hWnd
= GetHwnd();
358 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
360 // retrieve the style to see whether this is an edit or richedit ctrl
362 wxString classname
= wxGetWindowClass(GetHWND());
364 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
371 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
373 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
378 #endif // wxUSE_RICHEDIT
380 if (style
& ES_MULTILINE
)
381 m_windowStyle
|= wxTE_MULTILINE
;
382 if (style
& ES_PASSWORD
)
383 m_windowStyle
|= wxTE_PASSWORD
;
384 if (style
& ES_READONLY
)
385 m_windowStyle
|= wxTE_READONLY
;
386 if (style
& ES_WANTRETURN
)
387 m_windowStyle
|= wxTE_PROCESS_ENTER
;
388 if (style
& ES_CENTER
)
389 m_windowStyle
|= wxTE_CENTRE
;
390 if (style
& ES_RIGHT
)
391 m_windowStyle
|= wxTE_RIGHT
;
394 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
396 // default border for the text controls is the sunken one
397 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
399 style
|= wxBORDER_SUNKEN
;
402 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
404 // styles which we alaways add by default
405 if ( style
& wxTE_MULTILINE
)
407 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
408 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
410 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
411 if ( !(style
& wxTE_NO_VSCROLL
) )
413 // always adjust the vertical scrollbar automatically if we have it
414 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
416 // we have to use this style for the rich edit controls because
417 // without it the vertical scrollbar never appears at all in
418 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
419 if ( style
& wxTE_RICH2
)
421 msStyle
|= ES_DISABLENOSCROLL
;
425 style
|= wxTE_PROCESS_ENTER
;
429 // there is really no reason to not have this style for single line
431 msStyle
|= ES_AUTOHSCROLL
;
434 // styles which we add depending on the specified wxWindows styles
435 if ( style
& wxHSCROLL
)
437 // automatically scroll the control horizontally as necessary
438 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
441 if ( style
& wxTE_READONLY
)
442 msStyle
|= ES_READONLY
;
444 if ( style
& wxTE_PASSWORD
)
445 msStyle
|= ES_PASSWORD
;
447 if ( style
& wxTE_NOHIDESEL
)
448 msStyle
|= ES_NOHIDESEL
;
450 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
451 if ( style
& wxTE_CENTRE
)
452 msStyle
|= ES_CENTER
;
453 else if ( style
& wxTE_RIGHT
)
456 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
461 void wxTextCtrl::SetWindowStyleFlag(long style
)
463 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
464 style
|= wxBORDER_SUNKEN
;
467 // we have to deal with some styles separately because they can't be
468 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
469 // using richedit-specific EM_SETOPTIONS
471 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
473 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
475 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
476 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
478 #endif // wxUSE_RICHEDIT
480 wxControl::SetWindowStyleFlag(style
);
483 // ----------------------------------------------------------------------------
484 // set/get the controls text
485 // ----------------------------------------------------------------------------
487 wxString
wxTextCtrl::GetValue() const
489 // range 0..-1 is special for GetRange() and means to retrieve all text
490 return GetRange(0, -1);
493 wxString
wxTextCtrl::GetRange(long from
, long to
) const
497 if ( from
>= to
&& to
!= -1 )
499 // nothing to retrieve
506 int len
= GetWindowTextLength(GetHwnd());
509 // alloc one extra WORD as needed by the control
510 wxChar
*p
= str
.GetWriteBuf(++len
);
513 textRange
.chrg
.cpMin
= from
;
514 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
515 textRange
.lpstrText
= p
;
517 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
519 if ( m_verRichEdit
> 1 )
521 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
522 // neither Unix nor Windows style - convert it to something
526 if ( *p
== _T('\r') )
533 if ( m_verRichEdit
== 1 )
535 // convert to the canonical form - see comment below
536 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
539 //else: no text at all, leave the string empty
542 #endif // wxUSE_RICHEDIT
545 str
= wxGetWindowText(GetHWND());
547 // need only a range?
550 str
= str
.Mid(from
, to
- from
);
553 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
554 // canonical one (same one as above) for consistency with the other kinds
555 // of controls and, more importantly, with the other ports
556 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
562 void wxTextCtrl::SetValue(const wxString
& value
)
564 // if the text is long enough, it's faster to just set it instead of first
565 // comparing it with the old one (chances are that it will be different
566 // anyhow, this comparison is there to avoid flicker for small single-line
567 // edit controls mostly)
568 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
570 DoWriteText(value
, FALSE
/* not selection only */);
572 // mark the control as being not dirty - we changed its text, not the
576 // for compatibility, don't move the cursor when doing SetValue()
577 SetInsertionPoint(0);
581 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
583 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
587 wchar_t *wbuf
= (wchar_t *)buf
;
588 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
593 cb
-= sizeof(wchar_t);
594 (*pcb
) += sizeof(wchar_t);
597 *(const wchar_t **)dwCookie
= wpc
;
602 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from utils.cpp
604 #if wxUSE_UNICODE_MSLU
605 bool wxTextCtrl::StreamIn(const wxString
& value
,
606 wxFontEncoding
WXUNUSED(encoding
),
609 const wchar_t *wpc
= value
.c_str();
610 #else // !wxUSE_UNICODE_MSLU
611 bool wxTextCtrl::StreamIn(const wxString
& value
,
612 wxFontEncoding encoding
,
615 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
616 // text in the non default charset -- otherwise it thinks it knows better
617 // than we do and always shows it in the default one
619 // first get the Windows code page for this encoding
620 long codepage
= wxEncodingToCodepage(encoding
);
621 if ( codepage
== -1 )
627 // next translate to Unicode using this code page
628 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
631 wxWCharBuffer
wchBuf(len
);
633 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
636 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
637 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
639 wxLogLastError(_T("MultiByteToWideChar"));
642 // finally, stream it in the control
643 const wchar_t *wpc
= wchBuf
;
644 #endif // wxUSE_UNICODE_MSLU
648 eds
.dwCookie
= (DWORD
)&wpc
;
649 // the cast below is needed for broken (very) old mingw32 headers
650 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
652 // we're going to receive 2 EN_CHANGE notifications if we got any selection
653 // (same problem as in DoWriteText())
654 if ( selectionOnly
&& HasSelection() )
656 // so suppress one of them
657 m_suppressNextUpdate
= TRUE
;
660 ::SendMessage(GetHwnd(), EM_STREAMIN
,
663 (selectionOnly
? SFF_SELECTION
: 0),
668 wxLogLastError(_T("EM_STREAMIN"));
673 #endif // !wxUSE_WCHAR_T
678 #endif // wxUSE_RICHEDIT
680 void wxTextCtrl::WriteText(const wxString
& value
)
685 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
688 if ( m_windowStyle
& wxTE_MULTILINE
)
689 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
694 // there are several complications with the rich edit controls here
698 // first, ensure that the new text will be in the default style
699 if ( !m_defaultStyle
.IsDefault() )
702 GetSelection(&start
, &end
);
703 SetStyle(start
, end
, m_defaultStyle
);
706 #if wxUSE_UNICODE_MSLU
707 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
708 // but EM_STREAMIN works
709 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
711 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
713 #endif // wxUSE_UNICODE_MSLU
716 // next check if the text we're inserting must be shown in a non
717 // default charset -- this only works for RichEdit > 1.0
718 if ( GetRichVersion() > 1 )
720 wxFont font
= m_defaultStyle
.GetFont();
726 wxFontEncoding encoding
= font
.GetEncoding();
727 if ( encoding
!= wxFONTENCODING_SYSTEM
)
729 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
733 #endif // !wxUSE_UNICODE
737 #endif // wxUSE_RICHEDIT
739 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
740 // call below which is confusing for the client code and so should be
743 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
744 // and there is a non empty selection currently and (b) rich text
745 // controls in any case
749 #endif // wxUSE_RICHEDIT
750 (selectionOnly
&& HasSelection()) )
752 m_suppressNextUpdate
= TRUE
;
755 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
756 0, (LPARAM
)valueDos
.c_str());
758 // OTOH, non rich text controls don't generate any events at all when
759 // we use WM_SETTEXT -- have to emulate them here
763 #endif // wxUSE_RICHEDIT
766 // Windows already sends an update event for single-line
768 if ( m_windowStyle
& wxTE_MULTILINE
)
776 void wxTextCtrl::AppendText(const wxString
& text
)
778 SetInsertionPointEnd();
783 if ( IsMultiLine() && GetRichVersion() > 1 )
785 // setting the caret to the end and showing it simply doesn't work for
786 // RichEdit 2.0 -- force it to still do what we want
787 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
789 #endif // wxUSE_RICHEDIT
792 void wxTextCtrl::Clear()
794 ::SetWindowText(GetHwnd(), wxT(""));
798 #endif // wxUSE_RICHEDIT
800 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
801 // but the normal ones don't -- make Clear() behaviour consistent by
802 // always sending this event
804 // Windows already sends an update event for single-line
806 if ( m_windowStyle
& wxTE_MULTILINE
)
813 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
817 size_t lenOld
= GetValue().length();
819 wxUint32 code
= event
.GetRawKeyCode();
820 ::keybd_event(code
, 0, 0 /* key press */, 0);
821 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
823 // assume that any alphanumeric key changes the total number of characters
824 // in the control - this should work in 99% of cases
825 return GetValue().length() != lenOld
;
830 // ----------------------------------------------------------------------------
831 // Clipboard operations
832 // ----------------------------------------------------------------------------
834 void wxTextCtrl::Copy()
838 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
842 void wxTextCtrl::Cut()
846 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
850 void wxTextCtrl::Paste()
854 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
858 bool wxTextCtrl::HasSelection() const
861 GetSelection(&from
, &to
);
865 bool wxTextCtrl::CanCopy() const
867 // Can copy if there's a selection
868 return HasSelection();
871 bool wxTextCtrl::CanCut() const
873 return CanCopy() && IsEditable();
876 bool wxTextCtrl::CanPaste() const
884 UINT cf
= 0; // 0 == any format
886 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
888 #endif // wxUSE_RICHEDIT
890 // Standard edit control: check for straight text on clipboard
891 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
894 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
897 return isTextAvailable
;
900 // ----------------------------------------------------------------------------
902 // ----------------------------------------------------------------------------
904 void wxTextCtrl::SetEditable(bool editable
)
906 HWND hWnd
= GetHwnd();
907 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
910 void wxTextCtrl::SetInsertionPoint(long pos
)
912 DoSetSelection(pos
, pos
);
915 void wxTextCtrl::SetInsertionPointEnd()
920 if ( m_verRichEdit
== 1 )
922 // we don't have to waste time calling GetLastPosition() in this case
925 else // !RichEdit 1.0
926 #endif // wxUSE_RICHEDIT
928 pos
= GetLastPosition();
931 SetInsertionPoint(pos
);
934 long wxTextCtrl::GetInsertionPoint() const
942 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
945 #endif // wxUSE_RICHEDIT
947 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
951 long wxTextCtrl::GetLastPosition() const
953 int numLines
= GetNumberOfLines();
954 long posStartLastLine
= XYToPosition(0, numLines
- 1);
956 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
958 return posStartLastLine
+ lenLastLine
;
961 // If the return values from and to are the same, there is no
963 void wxTextCtrl::GetSelection(long* from
, long* to
) const
969 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
971 *from
= charRange
.cpMin
;
972 *to
= charRange
.cpMax
;
975 #endif // !wxUSE_RICHEDIT
977 DWORD dwStart
, dwEnd
;
978 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
985 bool wxTextCtrl::IsEditable() const
987 // strangely enough, we may be called before the control is created: our
988 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
993 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
995 return (style
& ES_READONLY
) == 0;
998 // ----------------------------------------------------------------------------
1000 // ----------------------------------------------------------------------------
1002 void wxTextCtrl::SetSelection(long from
, long to
)
1004 // if from and to are both -1, it means (in wxWindows) that all text should
1005 // be selected - translate into Windows convention
1006 if ( (from
== -1) && (to
== -1) )
1012 DoSetSelection(from
, to
);
1015 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
1017 HWND hWnd
= GetHwnd();
1026 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
1029 #endif // wxUSE_RICHEDIT
1031 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1037 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1038 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1039 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1040 // option is set (but it does work ok in richedit 1.0 mode...)
1042 // so to make it work we either need to give focus to it here which
1043 // will probably create many problems (dummy focus events; window
1044 // containing the text control being brought to foreground
1045 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1046 // create other problems too -- and in fact it does because if we turn
1047 // on/off this style while appending the text to the control, the
1048 // vertical scrollbar never appears in it even if we append tons of
1049 // text and to work around this the only solution I found was to use
1050 // ES_DISABLENOSCROLL
1052 // this is very ugly but I don't see any other way to make this work
1053 if ( GetRichVersion() > 1 )
1055 if ( !HasFlag(wxTE_NOHIDESEL
) )
1057 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1058 ECOOP_OR
, ECO_NOHIDESEL
);
1060 //else: everything is already ok
1062 #endif // wxUSE_RICHEDIT
1064 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1067 // restore ECO_NOHIDESEL if we changed it
1068 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1070 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1071 ECOOP_AND
, ~ECO_NOHIDESEL
);
1073 #endif // wxUSE_RICHEDIT
1076 // WPARAM is 0: selection is scrolled into view
1077 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1081 // ----------------------------------------------------------------------------
1083 // ----------------------------------------------------------------------------
1085 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1087 // Set selection and remove it
1088 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1090 SendMessage(GetHwnd(), EM_REPLACESEL
,
1096 (LPARAM
)value
.c_str());
1099 void wxTextCtrl::Remove(long from
, long to
)
1101 Replace(from
, to
, _T(""));
1104 bool wxTextCtrl::LoadFile(const wxString
& file
)
1106 if ( wxTextCtrlBase::LoadFile(file
) )
1108 // update the size limit if needed
1117 bool wxTextCtrl::IsModified() const
1119 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1122 // Makes 'unmodified'
1123 void wxTextCtrl::DiscardEdits()
1125 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1128 int wxTextCtrl::GetNumberOfLines() const
1130 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1133 long wxTextCtrl::XYToPosition(long x
, long y
) const
1135 // This gets the char index for the _beginning_ of this line
1136 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1138 return charIndex
+ x
;
1141 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1143 HWND hWnd
= GetHwnd();
1145 // This gets the line number containing the character
1150 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1153 #endif // wxUSE_RICHEDIT
1155 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1164 // This gets the char index for the _beginning_ of this line
1165 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1166 if ( charIndex
== -1 )
1171 // The X position must therefore be the different between pos and charIndex
1173 *x
= pos
- charIndex
;
1180 void wxTextCtrl::ShowPosition(long pos
)
1182 HWND hWnd
= GetHwnd();
1184 // To scroll to a position, we pass the number of lines and characters
1185 // to scroll *by*. This means that we need to:
1186 // (1) Find the line position of the current line.
1187 // (2) Find the line position of pos.
1188 // (3) Scroll by (pos - current).
1189 // For now, ignore the horizontal scrolling.
1191 // Is this where scrolling is relative to - the line containing the caret?
1192 // Or is the first visible line??? Try first visible line.
1193 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1195 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1197 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1199 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1201 if (linesToScroll
!= 0)
1202 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1205 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1207 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1210 int wxTextCtrl::GetLineLength(long lineNo
) const
1212 long pos
= XYToPosition(0, lineNo
);
1214 return GetLengthOfLineContainingPos(pos
);
1217 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1219 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1221 // there must be at least enough place for the length WORD in the
1223 len
+= sizeof(WORD
);
1226 wxChar
*buf
= str
.GetWriteBuf(len
);
1228 *(WORD
*)buf
= (WORD
)len
;
1229 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1232 str
.UngetWriteBuf(len
);
1237 void wxTextCtrl::SetMaxLength(unsigned long len
)
1239 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1242 // ----------------------------------------------------------------------------
1244 // ----------------------------------------------------------------------------
1246 void wxTextCtrl::Undo()
1250 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1254 void wxTextCtrl::Redo()
1258 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1259 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1263 bool wxTextCtrl::CanUndo() const
1265 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1268 bool wxTextCtrl::CanRedo() const
1270 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1273 // ----------------------------------------------------------------------------
1274 // implemenation details
1275 // ----------------------------------------------------------------------------
1277 void wxTextCtrl::Command(wxCommandEvent
& event
)
1279 SetValue(event
.GetString());
1280 ProcessCommand (event
);
1283 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1285 // By default, load the first file into the text window.
1286 if (event
.GetNumberOfFiles() > 0)
1288 LoadFile(event
.GetFiles()[0]);
1292 // ----------------------------------------------------------------------------
1293 // kbd input processing
1294 // ----------------------------------------------------------------------------
1296 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1298 MSG
*msg
= (MSG
*)pMsg
;
1300 // check for our special keys here: if we don't do it and the parent frame
1301 // uses them as accelerators, they wouldn't work at all, so we disable
1302 // usual preprocessing for them
1303 if ( msg
->message
== WM_KEYDOWN
)
1305 WORD vkey
= (WORD
) msg
->wParam
;
1306 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1308 if ( vkey
== VK_BACK
)
1313 // we want to process some Ctrl-foo and Shift-bar but no key
1314 // combinations without either Ctrl or Shift nor with both of them
1316 const int ctrl
= wxIsCtrlDown(),
1317 shift
= wxIsShiftDown();
1318 switch ( ctrl
+ shift
)
1321 wxFAIL_MSG( _T("how many modifiers have we got?") );
1329 // either Ctrl or Shift pressed
1344 else // Shift is pressed
1346 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1353 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1356 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1358 switch ( event
.GetKeyCode() )
1361 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1363 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1364 InitCommandEvent(event
);
1365 event
.SetString(GetValue());
1366 if ( GetEventHandler()->ProcessEvent(event
) )
1369 //else: multiline controls need Enter for themselves
1374 // always produce navigation event - even if we process TAB
1375 // ourselves the fact that we got here means that the user code
1376 // decided to skip processing of this TAB - probably to let it
1377 // do its default job.
1379 wxNavigationKeyEvent eventNav
;
1380 eventNav
.SetDirection(!event
.ShiftDown());
1381 eventNav
.SetWindowChange(event
.ControlDown());
1382 eventNav
.SetEventObject(this);
1384 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1390 // no, we didn't process it
1394 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1396 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1398 if ( nMsg
== WM_GETDLGCODE
)
1400 // we always want the chars and the arrows: the arrows for navigation
1401 // and the chars because we want Ctrl-C to work even in a read only
1403 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1407 // we may have several different cases:
1408 // 1. normal case: both TAB and ENTER are used for dlg navigation
1409 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1410 // next control in the dialog
1411 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1413 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1414 // to the next control
1416 // the multiline edit control should always get <Return> for itself
1417 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1418 lDlgCode
|= DLGC_WANTMESSAGE
;
1420 if ( HasFlag(wxTE_PROCESS_TAB
) )
1421 lDlgCode
|= DLGC_WANTTAB
;
1427 // NB: use "=", not "|=" as the base class version returns the
1428 // same flags is this state as usual (i.e. including
1429 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1430 // native Win32 apps?) but for now live with it.
1438 // ----------------------------------------------------------------------------
1439 // text control event processing
1440 // ----------------------------------------------------------------------------
1442 bool wxTextCtrl::SendUpdateEvent()
1444 // is event reporting suspended?
1445 if ( m_suppressNextUpdate
)
1447 // do process the next one
1448 m_suppressNextUpdate
= FALSE
;
1453 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1454 InitCommandEvent(event
);
1455 event
.SetString(GetValue());
1457 return ProcessCommand(event
);
1460 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1467 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1470 event
.SetEventObject(this);
1471 GetEventHandler()->ProcessEvent(event
);
1480 // the text size limit has been hit -- try to increase it
1481 if ( !AdjustSpaceLimit() )
1483 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1484 InitCommandEvent(event
);
1485 event
.SetString(GetValue());
1486 ProcessCommand(event
);
1490 // the other edit notification messages are not processed
1499 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1505 WXUINT
WXUNUSED(message
),
1506 WXWPARAM
WXUNUSED(wParam
),
1507 WXLPARAM
WXUNUSED(lParam
)
1514 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1515 return (WXHBRUSH
) hbrush
;
1517 #endif // wxUSE_CTL3D
1520 if (GetParent()->GetTransparentBackground())
1521 SetBkMode(hdc
, TRANSPARENT
);
1523 SetBkMode(hdc
, OPAQUE
);
1525 wxColour colBack
= GetBackgroundColour();
1527 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1528 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1530 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1531 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1533 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1535 return (WXHBRUSH
)brush
->GetResourceHandle();
1538 // In WIN16, need to override normal erasing because
1539 // Ctl3D doesn't use the wxWindows background colour.
1541 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1543 wxColour
col(m_backgroundColour
);
1547 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1551 ::GetClientRect(GetHwnd(), &rect
);
1553 COLORREF ref
= wxColourToRGB(col
);
1554 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1556 wxLogLastError(wxT("CreateSolidBrush"));
1558 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1560 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1562 ::FillRect(hdc
, &rect
, hBrush
);
1563 ::DeleteObject(hBrush
);
1564 ::SetMapMode(hdc
, mode
);
1569 bool wxTextCtrl::AdjustSpaceLimit()
1572 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1574 // HACK: we try to automatically extend the limit for the amount of text
1575 // to allow (interactively) entering more than 64Kb of text under
1576 // Win9x but we shouldn't reset the text limit which was previously
1577 // set explicitly with SetMaxLength()
1579 // we could solve this by storing the limit we set in wxTextCtrl but
1580 // to save space we prefer to simply test here the actual limit
1581 // value: we consider that SetMaxLength() can only be called for
1583 if ( limit
< 0x8000 )
1585 // we've got more text than limit set by SetMaxLength()
1589 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1592 limit
= len
+ 0x8000; // 32Kb
1597 // as a nice side effect, this also allows passing limit > 64Kb
1598 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1601 #endif // wxUSE_RICHEDIT
1603 if ( limit
> 0xffff )
1605 // this will set it to a platform-dependent maximum (much more
1606 // than 64Kb under NT)
1610 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1615 // we changed the limit
1619 bool wxTextCtrl::AcceptsFocus() const
1621 // we don't want focus if we can't be edited unless we're a multiline
1622 // control because then it might be still nice to get focus from keyboard
1623 // to be able to scroll it without mouse
1624 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1627 wxSize
wxTextCtrl::DoGetBestSize() const
1630 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1632 int wText
= DEFAULT_ITEM_WIDTH
;
1634 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1635 if ( m_windowStyle
& wxTE_MULTILINE
)
1637 hText
*= wxMax(GetNumberOfLines(), 5);
1639 //else: for single line control everything is ok
1641 return wxSize(wText
, hText
);
1644 // ----------------------------------------------------------------------------
1645 // standard handlers for standard edit menu events
1646 // ----------------------------------------------------------------------------
1648 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1653 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1658 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1663 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1668 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1673 void wxTextCtrl::OnDelete(wxCommandEvent
& event
)
1676 GetSelection(& from
, & to
);
1677 if (from
!= -1 && to
!= -1)
1681 void wxTextCtrl::OnSelectAll(wxCommandEvent
& event
)
1683 SetSelection(-1, -1);
1686 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1688 event
.Enable( CanCut() );
1691 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1693 event
.Enable( CanCopy() );
1696 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1698 event
.Enable( CanPaste() );
1701 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1703 event
.Enable( CanUndo() );
1706 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1708 event
.Enable( CanRedo() );
1711 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1714 GetSelection(& from
, & to
);
1715 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1718 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1720 event
.Enable(GetLastPosition() > 0);
1723 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1728 if (!m_privateContextMenu
)
1730 m_privateContextMenu
= new wxMenu
;
1731 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1732 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1733 m_privateContextMenu
->AppendSeparator();
1734 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1735 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1736 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1737 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1738 m_privateContextMenu
->AppendSeparator();
1739 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1741 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1749 // the rest of the file only deals with the rich edit controls
1752 // ----------------------------------------------------------------------------
1753 // EN_LINK processing
1754 // ----------------------------------------------------------------------------
1756 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1758 NMHDR
*hdr
= (NMHDR
* )lParam
;
1759 switch ( hdr
->code
)
1763 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1764 UINT msg
= msgf
->msg
;
1766 // this is a bit crazy but richedit 1.0 sends us all mouse
1767 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1768 // generate the wxWin events for this message manually
1770 // NB: in fact, this is still not totally correct as it does
1771 // send us WM_LBUTTONUP if the selection was cleared by the
1772 // last click -- so currently we get 2 events in this case,
1773 // but as I don't see any obvious way to check for this I
1774 // leave this code in place because it's still better than
1775 // not getting left up events at all
1776 if ( msg
== WM_LBUTTONUP
)
1778 WXUINT flags
= msgf
->wParam
;
1779 int x
= GET_X_LPARAM(msgf
->lParam
),
1780 y
= GET_Y_LPARAM(msgf
->lParam
);
1782 HandleMouseEvent(msg
, x
, y
, flags
);
1786 // return TRUE to process the event (and FALSE to ignore it)
1791 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1793 switch ( enlink
->msg
)
1796 // ok, so it is hardcoded - do we really nee to
1798 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1803 case WM_LBUTTONDOWN
:
1805 case WM_LBUTTONDBLCLK
:
1806 case WM_RBUTTONDOWN
:
1808 case WM_RBUTTONDBLCLK
:
1809 // send a mouse event
1811 static const wxEventType eventsMouse
[] =
1822 // the event ids are consecutive
1824 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1826 InitMouseEvent(evtMouse
,
1827 GET_X_LPARAM(enlink
->lParam
),
1828 GET_Y_LPARAM(enlink
->lParam
),
1831 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1833 enlink
->chrg
.cpMax
);
1835 InitCommandEvent(event
);
1837 *result
= ProcessCommand(event
);
1845 // not processed, leave it to the base class
1846 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1849 // ----------------------------------------------------------------------------
1850 // colour setting for the rich edit controls
1851 // ----------------------------------------------------------------------------
1853 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1855 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1857 // colour didn't really change
1863 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1864 // EM_SETBKGNDCOLOR additionally
1865 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1871 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1873 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1875 // colour didn't really change
1881 // change the colour of everything
1884 cf
.cbSize
= sizeof(cf
);
1885 cf
.dwMask
= CFM_COLOR
;
1886 cf
.crTextColor
= wxColourToRGB(colour
);
1887 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1893 // ----------------------------------------------------------------------------
1894 // styling support for rich edit controls
1895 // ----------------------------------------------------------------------------
1899 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1903 // can't do it with normal text control
1907 // the richedit 1.0 doesn't handle setting background colour, so don't
1908 // even try to do anything if it's the only thing we want to change
1909 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() )
1911 // nothing to do: return TRUE if there was really nothing to do and
1912 // FALSE if we failed to set bg colour
1913 return !style
.HasBackgroundColour();
1916 // order the range if needed
1924 // we can only change the format of the selection, so select the range we
1925 // want and restore the old selection later
1926 long startOld
, endOld
;
1927 GetSelection(&startOld
, &endOld
);
1929 // but do we really have to change the selection?
1930 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1934 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1937 // initialize CHARFORMAT struct
1946 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1947 // CHARFORMAT in that case
1949 if ( m_verRichEdit
== 1 )
1951 // this is the only thing the control is going to grok
1952 cf
.cbSize
= sizeof(CHARFORMAT
);
1957 // CHARFORMAT or CHARFORMAT2
1958 cf
.cbSize
= sizeof(cf
);
1961 if ( style
.HasFont() )
1963 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1964 // but using it doesn't seem to hurt neither so leaving it for now
1966 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1967 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1969 // fill in data from LOGFONT but recalculate lfHeight because we need
1970 // the real height in twips and not the negative number which
1971 // wxFillLogFont() returns (this is correct in general and works with
1972 // the Windows font mapper, but not here)
1974 wxFillLogFont(&lf
, &style
.GetFont());
1975 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1976 cf
.bCharSet
= lf
.lfCharSet
;
1977 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1978 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1980 // also deal with underline/italic/bold attributes: note that we must
1981 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1982 // style to allow clearing it
1985 cf
.dwEffects
|= CFE_ITALIC
;
1988 if ( lf
.lfWeight
== FW_BOLD
)
1990 cf
.dwEffects
|= CFE_BOLD
;
1993 if ( lf
.lfUnderline
)
1995 cf
.dwEffects
|= CFE_UNDERLINE
;
1998 // strikeout fonts are not supported by wxWindows
2001 if ( style
.HasTextColour() )
2003 cf
.dwMask
|= CFM_COLOR
;
2004 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
2008 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
2010 cf
.dwMask
|= CFM_BACKCOLOR
;
2011 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
2013 #endif // wxUSE_RICHEDIT2
2015 // do format the selection
2016 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
2017 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
2020 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
2025 // restore the original selection
2026 DoSetSelection(startOld
, endOld
, FALSE
);
2032 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2034 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2037 // we have to do this or the style wouldn't apply for the text typed by the
2039 long posLast
= GetLastPosition();
2040 SetStyle(posLast
, posLast
, m_defaultStyle
);
2047 // ----------------------------------------------------------------------------
2049 // ----------------------------------------------------------------------------
2051 bool wxRichEditModule::OnInit()
2053 // don't do anything - we will load it when needed
2057 void wxRichEditModule::OnExit()
2059 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2061 if ( ms_hRichEdit
[i
] )
2063 ::FreeLibrary(ms_hRichEdit
[i
]);
2069 bool wxRichEditModule::Load(int version
)
2071 // we don't support loading richedit 3.0 as I don't know how to distinguish
2072 // it from 2.0 anyhow
2073 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2074 _T("incorrect richedit control version requested") );
2076 // make it the index in the array
2079 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2081 // we had already tried to load it and failed
2085 if ( ms_hRichEdit
[version
] )
2087 // we've already got this one
2091 wxString dllname
= version
? _T("riched20") : _T("riched32");
2092 dllname
+= _T(".dll");
2094 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2096 if ( !ms_hRichEdit
[version
] )
2098 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2100 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2108 #endif // wxUSE_RICHEDIT
2110 #endif // wxUSE_TEXTCTRL