1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "textctrl.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
44 #include "wx/module.h"
47 #include "wx/clipbrd.h"
50 #include "wx/textfile.h"
54 #include "wx/msw/private.h"
55 #include "wx/msw/winundef.h"
61 #include <sys/types.h>
66 // old mingw32 has richedit stuff directly in windows.h and doesn't have
68 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
72 #include "wx/msw/missing.h"
74 #endif // wxUSE_RICHEDIT
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
82 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
84 #endif // wxUSE_RICHEDIT
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
92 // this module initializes RichEdit DLL(s) if needed
93 class wxRichEditModule
: public wxModule
96 virtual bool OnInit();
97 virtual void OnExit();
99 // load the richedit DLL of at least of required version
100 static bool Load(int version
= 1);
103 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
104 static HINSTANCE ms_hRichEdit
[2];
106 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
109 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
111 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
113 #endif // wxUSE_RICHEDIT
115 // ----------------------------------------------------------------------------
116 // event tables and other macros
117 // ----------------------------------------------------------------------------
119 #if wxUSE_EXTENDED_RTTI
120 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl
, wxControl
,"wx/textctrl.h")
122 WX_BEGIN_PROPERTIES_TABLE(wxTextCtrl
)
123 WX_PROPERTY_SET_AND_GET_BY_REF_RET_BOOL( Font
, wxFont
, SetFont
, GetFont
, )
124 WX_PROPERTY( Value
, wxString
, SetValue
, GetValue
, wxEmptyString
)
125 WX_END_PROPERTIES_TABLE()
127 WX_BEGIN_HANDLERS_TABLE(wxTextCtrl
)
128 WX_END_HANDLERS_TABLE()
130 WX_CONSTRUCTOR_6( wxTextCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
132 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
136 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
137 EVT_CHAR(wxTextCtrl::OnChar
)
138 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
141 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
144 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
145 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
146 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
147 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
148 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
149 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
150 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
152 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
153 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
154 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
155 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
156 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
157 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
158 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
160 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
163 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
)
166 // ============================================================================
168 // ============================================================================
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 void wxTextCtrl::Init()
178 #endif // wxUSE_RICHEDIT
180 m_privateContextMenu
= NULL
;
181 m_suppressNextUpdate
= FALSE
;
182 m_isNativeCaretShown
= true;
185 wxTextCtrl::~wxTextCtrl()
187 if (m_privateContextMenu
)
189 delete m_privateContextMenu
;
190 m_privateContextMenu
= NULL
;
194 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
195 const wxString
& value
,
199 const wxValidator
& validator
,
200 const wxString
& name
)
202 // base initialization
203 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
207 parent
->AddChild(this);
209 // translate wxWin style flags to MSW ones
210 WXDWORD msStyle
= MSWGetCreateWindowFlags();
212 // do create the control - either an EDIT or RICHEDIT
213 wxString windowClass
= wxT("EDIT");
216 if ( m_windowStyle
& wxTE_AUTO_URL
)
218 // automatic URL detection only works in RichEdit 2.0+
219 m_windowStyle
|= wxTE_RICH2
;
222 if ( m_windowStyle
& wxTE_RICH2
)
224 // using richedit 2.0 implies using wxTE_RICH
225 m_windowStyle
|= wxTE_RICH
;
228 // we need to load the richedit DLL before creating the rich edit control
229 if ( m_windowStyle
& wxTE_RICH
)
231 static bool s_errorGiven
= FALSE
;// MT-FIXME
233 // Which version do we need? Use 1.0 by default because it is much more
234 // like the the standard EDIT or 2.0 if explicitly requested, but use
235 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
237 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
238 // (and thus EDIT) much better than RichEdit 2.0 so we probably
239 // should use 3.0 if available as it is the best of both worlds -
240 // but as I can't test it right now I don't do it (VZ)
242 const int verRichEdit
= 2;
243 #else // !wxUSE_UNICODE
244 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
245 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
247 // only give the error msg once if the DLL can't be loaded
250 // try to load the RichEdit DLL (will do nothing if already done)
251 if ( !wxRichEditModule::Load(verRichEdit
) )
254 // try another version?
255 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
257 if ( !wxRichEditModule::Load(verRichEdit
) )
258 #endif // wxUSE_UNICODE
260 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
267 // have we managed to load any richedit version?
270 m_verRichEdit
= verRichEdit
;
271 if ( m_verRichEdit
== 1 )
273 windowClass
= wxT("RICHEDIT");
277 #ifndef RICHEDIT_CLASS
278 wxString RICHEDIT_CLASS
;
279 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
281 RICHEDIT_CLASS
+= _T('W');
283 RICHEDIT_CLASS
+= _T('A');
284 #endif // Unicode/ANSI
285 #endif // !RICHEDIT_CLASS
287 windowClass
= RICHEDIT_CLASS
;
291 #endif // wxUSE_RICHEDIT
293 // we need to turn '\n's into "\r\n"s for the multiline controls
295 if ( m_windowStyle
& wxTE_MULTILINE
)
297 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
304 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
307 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
312 // enable the events we're interested in: we want to get EN_CHANGE as
313 // for the normal controls
314 LPARAM mask
= ENM_CHANGE
;
316 if ( GetRichVersion() == 1 )
318 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
319 // explained in its handler
320 mask
|= ENM_MOUSEEVENTS
;
322 else if ( m_windowStyle
& wxTE_AUTO_URL
)
326 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
329 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
331 #endif // wxUSE_RICHEDIT
336 // Make sure the window style (etc.) reflects the HWND style (roughly)
337 void wxTextCtrl::AdoptAttributesFromHWND()
339 wxWindow::AdoptAttributesFromHWND();
341 HWND hWnd
= GetHwnd();
342 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
344 // retrieve the style to see whether this is an edit or richedit ctrl
346 wxString classname
= wxGetWindowClass(GetHWND());
348 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
355 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
357 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
362 #endif // wxUSE_RICHEDIT
364 if (style
& ES_MULTILINE
)
365 m_windowStyle
|= wxTE_MULTILINE
;
366 if (style
& ES_PASSWORD
)
367 m_windowStyle
|= wxTE_PASSWORD
;
368 if (style
& ES_READONLY
)
369 m_windowStyle
|= wxTE_READONLY
;
370 if (style
& ES_WANTRETURN
)
371 m_windowStyle
|= wxTE_PROCESS_ENTER
;
372 if (style
& ES_CENTER
)
373 m_windowStyle
|= wxTE_CENTRE
;
374 if (style
& ES_RIGHT
)
375 m_windowStyle
|= wxTE_RIGHT
;
378 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
380 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
382 // styles which we alaways add by default
383 if ( style
& wxTE_MULTILINE
)
385 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
386 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
388 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
389 if ( !(style
& wxTE_NO_VSCROLL
) )
391 // always adjust the vertical scrollbar automatically if we have it
392 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
395 // we have to use this style for the rich edit controls because
396 // without it the vertical scrollbar never appears at all in
397 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
398 if ( style
& wxTE_RICH2
)
400 msStyle
|= ES_DISABLENOSCROLL
;
402 #endif // wxUSE_RICHEDIT
405 style
|= wxTE_PROCESS_ENTER
;
409 // there is really no reason to not have this style for single line
411 msStyle
|= ES_AUTOHSCROLL
;
414 // styles which we add depending on the specified wxWindows styles
415 if ( style
& wxHSCROLL
)
417 // automatically scroll the control horizontally as necessary
418 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
421 if ( style
& wxTE_READONLY
)
422 msStyle
|= ES_READONLY
;
424 if ( style
& wxTE_PASSWORD
)
425 msStyle
|= ES_PASSWORD
;
427 if ( style
& wxTE_NOHIDESEL
)
428 msStyle
|= ES_NOHIDESEL
;
430 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
431 if ( style
& wxTE_CENTRE
)
432 msStyle
|= ES_CENTER
;
433 else if ( style
& wxTE_RIGHT
)
436 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
441 void wxTextCtrl::SetWindowStyleFlag(long style
)
444 // we have to deal with some styles separately because they can't be
445 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
446 // using richedit-specific EM_SETOPTIONS
448 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
450 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
452 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
453 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
455 #endif // wxUSE_RICHEDIT
457 wxControl::SetWindowStyleFlag(style
);
460 // ----------------------------------------------------------------------------
461 // set/get the controls text
462 // ----------------------------------------------------------------------------
464 wxString
wxTextCtrl::GetValue() const
466 // range 0..-1 is special for GetRange() and means to retrieve all text
467 return GetRange(0, -1);
470 wxString
wxTextCtrl::GetRange(long from
, long to
) const
474 if ( from
>= to
&& to
!= -1 )
476 // nothing to retrieve
483 int len
= GetWindowTextLength(GetHwnd());
487 // alloc one extra WORD as needed by the control
488 wxStringBuffer
tmp(str
, ++len
);
492 textRange
.chrg
.cpMin
= from
;
493 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
494 textRange
.lpstrText
= p
;
496 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
,
497 0, (LPARAM
)&textRange
);
499 if ( m_verRichEdit
> 1 )
501 // RichEdit 2.0 uses just CR ('\r') for the
502 // newlines which is neither Unix nor Windows
503 // style - convert it to something reasonable
506 if ( *p
== _T('\r') )
512 if ( m_verRichEdit
== 1 )
514 // convert to the canonical form - see comment below
515 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
518 //else: no text at all, leave the string empty
521 #endif // wxUSE_RICHEDIT
524 str
= wxGetWindowText(GetHWND());
526 // need only a range?
529 str
= str
.Mid(from
, to
- from
);
532 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
533 // canonical one (same one as above) for consistency with the other kinds
534 // of controls and, more importantly, with the other ports
535 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
541 void wxTextCtrl::SetValue(const wxString
& value
)
543 // if the text is long enough, it's faster to just set it instead of first
544 // comparing it with the old one (chances are that it will be different
545 // anyhow, this comparison is there to avoid flicker for small single-line
546 // edit controls mostly)
547 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
549 DoWriteText(value
, FALSE
/* not selection only */);
553 // still send an event for consistency
557 // we should reset the modified flag even if the value didn't really change
559 // mark the control as being not dirty - we changed its text, not the
563 // for compatibility, don't move the cursor when doing SetValue()
564 SetInsertionPoint(0);
567 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
569 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
573 wchar_t *wbuf
= (wchar_t *)buf
;
574 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
579 cb
-= sizeof(wchar_t);
580 (*pcb
) += sizeof(wchar_t);
583 *(const wchar_t **)dwCookie
= wpc
;
589 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
591 #if wxUSE_UNICODE_MSLU
592 bool wxTextCtrl::StreamIn(const wxString
& value
,
593 wxFontEncoding
WXUNUSED(encoding
),
596 const wchar_t *wpc
= value
.c_str();
597 #else // !wxUSE_UNICODE_MSLU
598 bool wxTextCtrl::StreamIn(const wxString
& value
,
599 wxFontEncoding encoding
,
602 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
603 // text in the non default charset -- otherwise it thinks it knows better
604 // than we do and always shows it in the default one
606 // first get the Windows code page for this encoding
607 long codepage
= wxEncodingToCodepage(encoding
);
608 if ( codepage
== -1 )
614 // next translate to Unicode using this code page
615 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
618 wxWCharBuffer
wchBuf(len
);
620 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
623 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
624 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
626 wxLogLastError(_T("MultiByteToWideChar"));
629 // finally, stream it in the control
630 const wchar_t *wpc
= wchBuf
;
631 #endif // wxUSE_UNICODE_MSLU
635 eds
.dwCookie
= (DWORD
)&wpc
;
636 // the cast below is needed for broken (very) old mingw32 headers
637 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
639 // we're going to receive 2 EN_CHANGE notifications if we got any selection
640 // (same problem as in DoWriteText())
641 if ( selectionOnly
&& HasSelection() )
643 // so suppress one of them
644 m_suppressNextUpdate
= TRUE
;
647 ::SendMessage(GetHwnd(), EM_STREAMIN
,
650 (selectionOnly
? SFF_SELECTION
: 0),
655 wxLogLastError(_T("EM_STREAMIN"));
660 #endif // !wxUSE_WCHAR_T
665 #endif // wxUSE_RICHEDIT
667 void wxTextCtrl::WriteText(const wxString
& value
)
672 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
675 if ( m_windowStyle
& wxTE_MULTILINE
)
676 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
681 // there are several complications with the rich edit controls here
685 // first, ensure that the new text will be in the default style
686 if ( !m_defaultStyle
.IsDefault() )
689 GetSelection(&start
, &end
);
690 SetStyle(start
, end
, m_defaultStyle
);
693 #if wxUSE_UNICODE_MSLU
694 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
695 // but EM_STREAMIN works
696 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
698 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
700 #endif // wxUSE_UNICODE_MSLU
703 // next check if the text we're inserting must be shown in a non
704 // default charset -- this only works for RichEdit > 1.0
705 if ( GetRichVersion() > 1 )
707 wxFont font
= m_defaultStyle
.GetFont();
713 wxFontEncoding encoding
= font
.GetEncoding();
714 if ( encoding
!= wxFONTENCODING_SYSTEM
)
716 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
720 #endif // !wxUSE_UNICODE
724 #endif // wxUSE_RICHEDIT
726 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
727 // call below which is confusing for the client code and so should be
730 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
731 // and there is a non empty selection currently and (b) rich text
732 // controls in any case
736 #endif // wxUSE_RICHEDIT
737 (selectionOnly
&& HasSelection()) )
739 m_suppressNextUpdate
= TRUE
;
742 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
743 0, (LPARAM
)valueDos
.c_str());
745 // OTOH, non rich text controls don't generate any events at all when
746 // we use WM_SETTEXT -- have to emulate them here
750 #endif // wxUSE_RICHEDIT
753 // Windows already sends an update event for single-line
755 if ( m_windowStyle
& wxTE_MULTILINE
)
763 void wxTextCtrl::AppendText(const wxString
& text
)
765 SetInsertionPointEnd();
770 if ( IsMultiLine() && GetRichVersion() > 1 )
772 // setting the caret to the end and showing it simply doesn't work for
773 // RichEdit 2.0 -- force it to still do what we want
774 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
776 #endif // wxUSE_RICHEDIT
779 void wxTextCtrl::Clear()
781 ::SetWindowText(GetHwnd(), wxEmptyString
);
785 #endif // wxUSE_RICHEDIT
787 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
788 // but the normal ones don't -- make Clear() behaviour consistent by
789 // always sending this event
791 // Windows already sends an update event for single-line
793 if ( m_windowStyle
& wxTE_MULTILINE
)
800 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
804 size_t lenOld
= GetValue().length();
806 wxUint32 code
= event
.GetRawKeyCode();
807 ::keybd_event(code
, 0, 0 /* key press */, 0);
808 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
810 // assume that any alphanumeric key changes the total number of characters
811 // in the control - this should work in 99% of cases
812 return GetValue().length() != lenOld
;
817 // ----------------------------------------------------------------------------
818 // Clipboard operations
819 // ----------------------------------------------------------------------------
821 void wxTextCtrl::Copy()
825 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
829 void wxTextCtrl::Cut()
833 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
837 void wxTextCtrl::Paste()
841 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
845 bool wxTextCtrl::HasSelection() const
848 GetSelection(&from
, &to
);
852 bool wxTextCtrl::CanCopy() const
854 // Can copy if there's a selection
855 return HasSelection();
858 bool wxTextCtrl::CanCut() const
860 return CanCopy() && IsEditable();
863 bool wxTextCtrl::CanPaste() const
871 UINT cf
= 0; // 0 == any format
873 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
875 #endif // wxUSE_RICHEDIT
877 // Standard edit control: check for straight text on clipboard
878 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
881 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
884 return isTextAvailable
;
887 // ----------------------------------------------------------------------------
889 // ----------------------------------------------------------------------------
891 void wxTextCtrl::SetEditable(bool editable
)
893 HWND hWnd
= GetHwnd();
894 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
897 void wxTextCtrl::SetInsertionPoint(long pos
)
899 DoSetSelection(pos
, pos
);
902 void wxTextCtrl::SetInsertionPointEnd()
904 // we must not do anything if the caret is already there because calling
905 // SetInsertionPoint() thaws the controls if Freeze() had been called even
906 // if it doesn't actually move the caret anywhere and so the simple fact of
907 // doing it results in horrible flicker when appending big amounts of text
908 // to the control in a few chunks (see DoAddText() test in the text sample)
909 if ( GetInsertionPoint() == GetLastPosition() )
915 if ( m_verRichEdit
== 1 )
917 // we don't have to waste time calling GetLastPosition() in this case
920 else // !RichEdit 1.0
921 #endif // wxUSE_RICHEDIT
923 pos
= GetLastPosition();
926 SetInsertionPoint(pos
);
929 long wxTextCtrl::GetInsertionPoint() const
937 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
940 #endif // wxUSE_RICHEDIT
942 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
946 long wxTextCtrl::GetLastPosition() const
948 int numLines
= GetNumberOfLines();
949 long posStartLastLine
= XYToPosition(0, numLines
- 1);
951 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
953 return posStartLastLine
+ lenLastLine
;
956 // If the return values from and to are the same, there is no
958 void wxTextCtrl::GetSelection(long* from
, long* to
) const
964 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
966 *from
= charRange
.cpMin
;
967 *to
= charRange
.cpMax
;
970 #endif // !wxUSE_RICHEDIT
972 DWORD dwStart
, dwEnd
;
973 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
980 bool wxTextCtrl::IsEditable() const
982 // strangely enough, we may be called before the control is created: our
983 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
988 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
990 return (style
& ES_READONLY
) == 0;
993 // ----------------------------------------------------------------------------
995 // ----------------------------------------------------------------------------
997 void wxTextCtrl::SetSelection(long from
, long to
)
999 // if from and to are both -1, it means (in wxWindows) that all text should
1000 // be selected - translate into Windows convention
1001 if ( (from
== -1) && (to
== -1) )
1007 DoSetSelection(from
, to
);
1010 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
1012 HWND hWnd
= GetHwnd();
1021 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
1024 #endif // wxUSE_RICHEDIT
1026 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1032 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1033 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1034 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1035 // option is set (but it does work ok in richedit 1.0 mode...)
1037 // so to make it work we either need to give focus to it here which
1038 // will probably create many problems (dummy focus events; window
1039 // containing the text control being brought to foreground
1040 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1041 // create other problems too -- and in fact it does because if we turn
1042 // on/off this style while appending the text to the control, the
1043 // vertical scrollbar never appears in it even if we append tons of
1044 // text and to work around this the only solution I found was to use
1045 // ES_DISABLENOSCROLL
1047 // this is very ugly but I don't see any other way to make this work
1048 if ( GetRichVersion() > 1 )
1050 if ( !HasFlag(wxTE_NOHIDESEL
) )
1052 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1053 ECOOP_OR
, ECO_NOHIDESEL
);
1055 //else: everything is already ok
1057 #endif // wxUSE_RICHEDIT
1059 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1062 // restore ECO_NOHIDESEL if we changed it
1063 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1065 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1066 ECOOP_AND
, ~ECO_NOHIDESEL
);
1068 #endif // wxUSE_RICHEDIT
1071 // WPARAM is 0: selection is scrolled into view
1072 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1076 // ----------------------------------------------------------------------------
1078 // ----------------------------------------------------------------------------
1080 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1082 // Set selection and remove it
1083 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1085 SendMessage(GetHwnd(), EM_REPLACESEL
,
1091 (LPARAM
)value
.c_str());
1094 void wxTextCtrl::Remove(long from
, long to
)
1096 Replace(from
, to
, wxEmptyString
);
1099 bool wxTextCtrl::LoadFile(const wxString
& file
)
1101 if ( wxTextCtrlBase::LoadFile(file
) )
1103 // update the size limit if needed
1112 bool wxTextCtrl::IsModified() const
1114 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1117 // Makes 'unmodified'
1118 void wxTextCtrl::DiscardEdits()
1120 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1123 int wxTextCtrl::GetNumberOfLines() const
1125 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1128 long wxTextCtrl::XYToPosition(long x
, long y
) const
1130 // This gets the char index for the _beginning_ of this line
1131 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1133 return charIndex
+ x
;
1136 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1138 HWND hWnd
= GetHwnd();
1140 // This gets the line number containing the character
1145 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1148 #endif // wxUSE_RICHEDIT
1150 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1159 // This gets the char index for the _beginning_ of this line
1160 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1161 if ( charIndex
== -1 )
1166 // The X position must therefore be the different between pos and charIndex
1168 *x
= pos
- charIndex
;
1175 void wxTextCtrl::ShowPosition(long pos
)
1177 HWND hWnd
= GetHwnd();
1179 // To scroll to a position, we pass the number of lines and characters
1180 // to scroll *by*. This means that we need to:
1181 // (1) Find the line position of the current line.
1182 // (2) Find the line position of pos.
1183 // (3) Scroll by (pos - current).
1184 // For now, ignore the horizontal scrolling.
1186 // Is this where scrolling is relative to - the line containing the caret?
1187 // Or is the first visible line??? Try first visible line.
1188 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1190 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1192 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1194 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1196 if (linesToScroll
!= 0)
1197 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1200 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1202 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1205 int wxTextCtrl::GetLineLength(long lineNo
) const
1207 long pos
= XYToPosition(0, lineNo
);
1209 return GetLengthOfLineContainingPos(pos
);
1212 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1214 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1216 // there must be at least enough place for the length WORD in the
1218 len
+= sizeof(WORD
);
1222 wxStringBufferLength
tmp(str
, len
);
1225 *(WORD
*)buf
= (WORD
)len
;
1226 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
,
1227 lineNo
, (LPARAM
)buf
);
1235 void wxTextCtrl::SetMaxLength(unsigned long len
)
1237 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1240 // ----------------------------------------------------------------------------
1242 // ----------------------------------------------------------------------------
1244 void wxTextCtrl::Undo()
1248 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1252 void wxTextCtrl::Redo()
1256 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1257 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1261 bool wxTextCtrl::CanUndo() const
1263 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1266 bool wxTextCtrl::CanRedo() const
1268 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1271 // ----------------------------------------------------------------------------
1272 // caret handling (Windows only)
1273 // ----------------------------------------------------------------------------
1275 bool wxTextCtrl::ShowNativeCaret(bool show
)
1277 if ( show
!= m_isNativeCaretShown
)
1279 if ( !(show
? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) )
1281 // not an error, may simply indicate that it's not shown/hidden
1282 // yet (i.e. it had been hidden/showh 2 times before)
1286 m_isNativeCaretShown
= show
;
1292 // ----------------------------------------------------------------------------
1293 // implemenation details
1294 // ----------------------------------------------------------------------------
1296 void wxTextCtrl::Command(wxCommandEvent
& event
)
1298 SetValue(event
.GetString());
1299 ProcessCommand (event
);
1302 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1304 // By default, load the first file into the text window.
1305 if (event
.GetNumberOfFiles() > 0)
1307 LoadFile(event
.GetFiles()[0]);
1311 // ----------------------------------------------------------------------------
1312 // kbd input processing
1313 // ----------------------------------------------------------------------------
1315 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1317 MSG
*msg
= (MSG
*)pMsg
;
1319 // check for our special keys here: if we don't do it and the parent frame
1320 // uses them as accelerators, they wouldn't work at all, so we disable
1321 // usual preprocessing for them
1322 if ( msg
->message
== WM_KEYDOWN
)
1324 WORD vkey
= (WORD
) msg
->wParam
;
1325 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1327 if ( vkey
== VK_BACK
)
1332 // we want to process some Ctrl-foo and Shift-bar but no key
1333 // combinations without either Ctrl or Shift nor with both of them
1335 const int ctrl
= wxIsCtrlDown(),
1336 shift
= wxIsShiftDown();
1337 switch ( ctrl
+ shift
)
1340 wxFAIL_MSG( _T("how many modifiers have we got?") );
1348 // either Ctrl or Shift pressed
1363 else // Shift is pressed
1365 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1372 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1375 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1377 switch ( event
.GetKeyCode() )
1380 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1382 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1383 InitCommandEvent(event
);
1384 event
.SetString(GetValue());
1385 if ( GetEventHandler()->ProcessEvent(event
) )
1388 //else: multiline controls need Enter for themselves
1393 // always produce navigation event - even if we process TAB
1394 // ourselves the fact that we got here means that the user code
1395 // decided to skip processing of this TAB - probably to let it
1396 // do its default job.
1398 wxNavigationKeyEvent eventNav
;
1399 eventNav
.SetDirection(!event
.ShiftDown());
1400 eventNav
.SetWindowChange(event
.ControlDown());
1401 eventNav
.SetEventObject(this);
1403 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1409 // no, we didn't process it
1413 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1415 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1417 if ( nMsg
== WM_GETDLGCODE
)
1419 // we always want the chars and the arrows: the arrows for navigation
1420 // and the chars because we want Ctrl-C to work even in a read only
1422 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1426 // we may have several different cases:
1427 // 1. normal case: both TAB and ENTER are used for dlg navigation
1428 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1429 // next control in the dialog
1430 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1432 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1433 // to the next control
1435 // the multiline edit control should always get <Return> for itself
1436 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1437 lDlgCode
|= DLGC_WANTMESSAGE
;
1439 if ( HasFlag(wxTE_PROCESS_TAB
) )
1440 lDlgCode
|= DLGC_WANTTAB
;
1446 // NB: use "=", not "|=" as the base class version returns the
1447 // same flags is this state as usual (i.e. including
1448 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1449 // native Win32 apps?) but for now live with it.
1457 // ----------------------------------------------------------------------------
1458 // text control event processing
1459 // ----------------------------------------------------------------------------
1461 bool wxTextCtrl::SendUpdateEvent()
1463 // is event reporting suspended?
1464 if ( m_suppressNextUpdate
)
1466 // do process the next one
1467 m_suppressNextUpdate
= FALSE
;
1472 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1473 InitCommandEvent(event
);
1474 event
.SetString(GetValue());
1476 return ProcessCommand(event
);
1479 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1486 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1489 event
.SetEventObject(this);
1490 GetEventHandler()->ProcessEvent(event
);
1499 // the text size limit has been hit -- try to increase it
1500 if ( !AdjustSpaceLimit() )
1502 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1503 InitCommandEvent(event
);
1504 event
.SetString(GetValue());
1505 ProcessCommand(event
);
1509 // the other edit notification messages are not processed
1518 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1524 WXUINT
WXUNUSED(message
),
1525 WXWPARAM
WXUNUSED(wParam
),
1526 WXLPARAM
WXUNUSED(lParam
)
1533 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1534 return (WXHBRUSH
) hbrush
;
1536 #endif // wxUSE_CTL3D
1539 wxColour colBack
= GetBackgroundColour();
1541 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1542 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1544 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1545 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1547 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1549 return (WXHBRUSH
)brush
->GetResourceHandle();
1552 // In WIN16, need to override normal erasing because
1553 // Ctl3D doesn't use the wxWindows background colour.
1555 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1557 wxColour
col(m_backgroundColour
);
1561 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1565 ::GetClientRect(GetHwnd(), &rect
);
1567 COLORREF ref
= wxColourToRGB(col
);
1568 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1570 wxLogLastError(wxT("CreateSolidBrush"));
1572 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1574 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1576 ::FillRect(hdc
, &rect
, hBrush
);
1577 ::DeleteObject(hBrush
);
1578 ::SetMapMode(hdc
, mode
);
1583 bool wxTextCtrl::AdjustSpaceLimit()
1586 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1588 // HACK: we try to automatically extend the limit for the amount of text
1589 // to allow (interactively) entering more than 64Kb of text under
1590 // Win9x but we shouldn't reset the text limit which was previously
1591 // set explicitly with SetMaxLength()
1593 // we could solve this by storing the limit we set in wxTextCtrl but
1594 // to save space we prefer to simply test here the actual limit
1595 // value: we consider that SetMaxLength() can only be called for
1597 if ( limit
< 0x8000 )
1599 // we've got more text than limit set by SetMaxLength()
1603 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1606 limit
= len
+ 0x8000; // 32Kb
1611 // as a nice side effect, this also allows passing limit > 64Kb
1612 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1615 #endif // wxUSE_RICHEDIT
1617 if ( limit
> 0xffff )
1619 // this will set it to a platform-dependent maximum (much more
1620 // than 64Kb under NT)
1624 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1629 // we changed the limit
1633 bool wxTextCtrl::AcceptsFocus() const
1635 // we don't want focus if we can't be edited unless we're a multiline
1636 // control because then it might be still nice to get focus from keyboard
1637 // to be able to scroll it without mouse
1638 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1641 wxSize
wxTextCtrl::DoGetBestSize() const
1644 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1646 int wText
= DEFAULT_ITEM_WIDTH
;
1648 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1649 if ( m_windowStyle
& wxTE_MULTILINE
)
1651 hText
*= wxMax(GetNumberOfLines(), 5);
1653 //else: for single line control everything is ok
1655 return wxSize(wText
, hText
);
1658 // ----------------------------------------------------------------------------
1659 // standard handlers for standard edit menu events
1660 // ----------------------------------------------------------------------------
1662 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1667 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1672 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1677 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1682 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1687 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1690 GetSelection(& from
, & to
);
1691 if (from
!= -1 && to
!= -1)
1695 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1697 SetSelection(-1, -1);
1700 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1702 event
.Enable( CanCut() );
1705 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1707 event
.Enable( CanCopy() );
1710 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1712 event
.Enable( CanPaste() );
1715 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1717 event
.Enable( CanUndo() );
1720 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1722 event
.Enable( CanRedo() );
1725 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1728 GetSelection(& from
, & to
);
1729 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1732 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1734 event
.Enable(GetLastPosition() > 0);
1737 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1742 if (!m_privateContextMenu
)
1744 m_privateContextMenu
= new wxMenu
;
1745 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1746 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1747 m_privateContextMenu
->AppendSeparator();
1748 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1749 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1750 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1751 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1752 m_privateContextMenu
->AppendSeparator();
1753 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1755 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1763 void wxTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
1765 // be sure the caret remains invisible if the user had hidden it
1766 if ( !m_isNativeCaretShown
)
1768 ::HideCaret(GetHwnd());
1772 // the rest of the file only deals with the rich edit controls
1775 // ----------------------------------------------------------------------------
1776 // EN_LINK processing
1777 // ----------------------------------------------------------------------------
1779 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1781 NMHDR
*hdr
= (NMHDR
* )lParam
;
1782 switch ( hdr
->code
)
1786 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1787 UINT msg
= msgf
->msg
;
1789 // this is a bit crazy but richedit 1.0 sends us all mouse
1790 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1791 // generate the wxWin events for this message manually
1793 // NB: in fact, this is still not totally correct as it does
1794 // send us WM_LBUTTONUP if the selection was cleared by the
1795 // last click -- so currently we get 2 events in this case,
1796 // but as I don't see any obvious way to check for this I
1797 // leave this code in place because it's still better than
1798 // not getting left up events at all
1799 if ( msg
== WM_LBUTTONUP
)
1801 WXUINT flags
= msgf
->wParam
;
1802 int x
= GET_X_LPARAM(msgf
->lParam
),
1803 y
= GET_Y_LPARAM(msgf
->lParam
);
1805 HandleMouseEvent(msg
, x
, y
, flags
);
1809 // return TRUE to process the event (and FALSE to ignore it)
1814 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1816 switch ( enlink
->msg
)
1819 // ok, so it is hardcoded - do we really nee to
1821 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1826 case WM_LBUTTONDOWN
:
1828 case WM_LBUTTONDBLCLK
:
1829 case WM_RBUTTONDOWN
:
1831 case WM_RBUTTONDBLCLK
:
1832 // send a mouse event
1834 static const wxEventType eventsMouse
[] =
1845 // the event ids are consecutive
1847 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1849 InitMouseEvent(evtMouse
,
1850 GET_X_LPARAM(enlink
->lParam
),
1851 GET_Y_LPARAM(enlink
->lParam
),
1854 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1856 enlink
->chrg
.cpMax
);
1858 InitCommandEvent(event
);
1860 *result
= ProcessCommand(event
);
1868 // not processed, leave it to the base class
1869 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1872 // ----------------------------------------------------------------------------
1873 // colour setting for the rich edit controls
1874 // ----------------------------------------------------------------------------
1876 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1878 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1880 // colour didn't really change
1886 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1887 // EM_SETBKGNDCOLOR additionally
1888 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1894 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1896 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1898 // colour didn't really change
1904 // change the colour of everything
1907 cf
.cbSize
= sizeof(cf
);
1908 cf
.dwMask
= CFM_COLOR
;
1909 cf
.crTextColor
= wxColourToRGB(colour
);
1910 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1916 // ----------------------------------------------------------------------------
1917 // styling support for rich edit controls
1918 // ----------------------------------------------------------------------------
1922 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1926 // can't do it with normal text control
1930 // the richedit 1.0 doesn't handle setting background colour, so don't
1931 // even try to do anything if it's the only thing we want to change
1932 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1933 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1936 // nothing to do: return TRUE if there was really nothing to do and
1937 // FALSE if we failed to set bg colour
1938 return !style
.HasBackgroundColour();
1941 // order the range if needed
1949 // we can only change the format of the selection, so select the range we
1950 // want and restore the old selection later
1951 long startOld
, endOld
;
1952 GetSelection(&startOld
, &endOld
);
1954 // but do we really have to change the selection?
1955 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1959 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1962 // initialize CHARFORMAT struct
1971 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1972 // CHARFORMAT in that case
1974 if ( m_verRichEdit
== 1 )
1976 // this is the only thing the control is going to grok
1977 cf
.cbSize
= sizeof(CHARFORMAT
);
1982 // CHARFORMAT or CHARFORMAT2
1983 cf
.cbSize
= sizeof(cf
);
1986 if ( style
.HasFont() )
1988 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1989 // but using it doesn't seem to hurt neither so leaving it for now
1991 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1992 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1994 // fill in data from LOGFONT but recalculate lfHeight because we need
1995 // the real height in twips and not the negative number which
1996 // wxFillLogFont() returns (this is correct in general and works with
1997 // the Windows font mapper, but not here)
1999 wxFillLogFont(&lf
, &style
.GetFont());
2000 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
2001 cf
.bCharSet
= lf
.lfCharSet
;
2002 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
2003 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
2005 // also deal with underline/italic/bold attributes: note that we must
2006 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
2007 // style to allow clearing it
2010 cf
.dwEffects
|= CFE_ITALIC
;
2013 if ( lf
.lfWeight
== FW_BOLD
)
2015 cf
.dwEffects
|= CFE_BOLD
;
2018 if ( lf
.lfUnderline
)
2020 cf
.dwEffects
|= CFE_UNDERLINE
;
2023 // strikeout fonts are not supported by wxWindows
2026 if ( style
.HasTextColour() )
2028 cf
.dwMask
|= CFM_COLOR
;
2029 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
2033 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
2035 cf
.dwMask
|= CFM_BACKCOLOR
;
2036 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
2038 #endif // wxUSE_RICHEDIT2
2040 // do format the selection
2041 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
2042 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
2045 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
2048 // now do the paragraph formatting
2051 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2052 // PARAFORMAT in that case
2054 if ( m_verRichEdit
== 1 )
2056 // this is the only thing the control is going to grok
2057 pf
.cbSize
= sizeof(PARAFORMAT
);
2062 // PARAFORMAT or PARAFORMAT2
2063 pf
.cbSize
= sizeof(pf
);
2066 if (style
.HasAlignment())
2068 pf
.dwMask
|= PFM_ALIGNMENT
;
2069 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2070 pf
.wAlignment
= PFA_RIGHT
;
2071 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2072 pf
.wAlignment
= PFA_CENTER
;
2073 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2074 pf
.wAlignment
= PFA_JUSTIFY
;
2076 pf
.wAlignment
= PFA_LEFT
;
2079 if (style
.HasLeftIndent())
2081 pf
.dwMask
|= PFM_STARTINDENT
;
2083 // Convert from 1/10 mm to TWIPS
2084 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2086 // TODO: do we need to specify dxOffset?
2089 if (style
.HasRightIndent())
2091 pf
.dwMask
|= PFM_RIGHTINDENT
;
2093 // Convert from 1/10 mm to TWIPS
2094 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2097 if (style
.HasTabs())
2099 pf
.dwMask
|= PFM_TABSTOPS
;
2101 const wxArrayInt
& tabs
= style
.GetTabs();
2103 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2105 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2107 // Convert from 1/10 mm to TWIPS
2108 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2114 // do format the selection
2115 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2116 0, (LPARAM
) &pf
) != 0;
2119 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2125 // restore the original selection
2126 DoSetSelection(startOld
, endOld
, FALSE
);
2132 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2134 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2137 // we have to do this or the style wouldn't apply for the text typed by the
2139 long posLast
= GetLastPosition();
2140 SetStyle(posLast
, posLast
, m_defaultStyle
);
2145 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2149 // can't do it with normal text control
2153 // initialize CHARFORMAT struct
2162 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2163 // CHARFORMAT in that case
2165 if ( m_verRichEdit
== 1 )
2167 // this is the only thing the control is going to grok
2168 cf
.cbSize
= sizeof(CHARFORMAT
);
2173 // CHARFORMAT or CHARFORMAT2
2174 cf
.cbSize
= sizeof(cf
);
2176 // we can only change the format of the selection, so select the range we
2177 // want and restore the old selection later
2178 long startOld
, endOld
;
2179 GetSelection(&startOld
, &endOld
);
2181 // but do we really have to change the selection?
2182 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2186 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2189 // get the selection formatting
2190 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2191 SCF_SELECTION
, (LPARAM
)&cf
) ;
2194 lf
.lfHeight
= cf
.yHeight
;
2196 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2197 lf
.lfClipPrecision
= 0;
2198 lf
.lfEscapement
= 0;
2199 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2200 if (cf
.dwEffects
& CFE_ITALIC
)
2202 lf
.lfOrientation
= 0;
2203 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2205 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2206 lf
.lfStrikeOut
= TRUE
;
2207 if (cf
.dwEffects
& CFE_UNDERLINE
)
2208 lf
.lfUnderline
= TRUE
;
2209 if (cf
.dwEffects
& CFE_BOLD
)
2210 lf
.lfWeight
= FW_BOLD
;
2212 wxFont font
= wxCreateFontFromLogFont(& lf
);
2215 style
.SetFont(font
);
2217 style
.SetTextColour(wxColour(cf
.crTextColor
));
2220 if ( m_verRichEdit
!= 1 )
2222 // cf.dwMask |= CFM_BACKCOLOR;
2223 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2225 #endif // wxUSE_RICHEDIT2
2227 // now get the paragraph formatting
2230 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2231 // PARAFORMAT in that case
2233 if ( m_verRichEdit
== 1 )
2235 // this is the only thing the control is going to grok
2236 pf
.cbSize
= sizeof(PARAFORMAT
);
2241 // PARAFORMAT or PARAFORMAT2
2242 pf
.cbSize
= sizeof(pf
);
2245 // do format the selection
2246 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2248 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2249 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2251 if (pf
.wAlignment
== PFA_CENTER
)
2252 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2253 else if (pf
.wAlignment
== PFA_RIGHT
)
2254 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2255 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2256 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2258 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2260 wxArrayInt tabStops
;
2262 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2264 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2269 // restore the original selection
2270 DoSetSelection(startOld
, endOld
, FALSE
);
2278 // ----------------------------------------------------------------------------
2280 // ----------------------------------------------------------------------------
2282 bool wxRichEditModule::OnInit()
2284 // don't do anything - we will load it when needed
2288 void wxRichEditModule::OnExit()
2290 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2292 if ( ms_hRichEdit
[i
] )
2294 ::FreeLibrary(ms_hRichEdit
[i
]);
2295 ms_hRichEdit
[i
] = NULL
;
2301 bool wxRichEditModule::Load(int version
)
2303 // we don't support loading richedit 3.0 as I don't know how to distinguish
2304 // it from 2.0 anyhow
2305 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2306 _T("incorrect richedit control version requested") );
2308 // make it the index in the array
2311 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2313 // we had already tried to load it and failed
2317 if ( ms_hRichEdit
[version
] )
2319 // we've already got this one
2323 wxString dllname
= version
? _T("riched20") : _T("riched32");
2324 dllname
+= _T(".dll");
2326 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2328 if ( !ms_hRichEdit
[version
] )
2330 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2332 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2340 #endif // wxUSE_RICHEDIT
2342 #endif // wxUSE_TEXTCTRL