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>
60 #if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__))
64 // old mingw32 doesn't define this
66 #define CFM_CHARSET 0x08000000
70 #define CFM_BACKCOLOR 0x04000000
73 // cygwin does not have these defined for richedit
75 #define ENM_LINK 0x04000000
78 #ifndef EM_AUTOURLDETECT
79 #define EM_AUTOURLDETECT (WM_USER + 91)
83 #define EN_LINK 0x070b
85 typedef struct _enlink
96 #define SF_UNICODE 0x0010
99 // Watcom C++ doesn't define this
101 #define SCF_ALL 0x0004
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
110 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
112 #endif // wxUSE_RICHEDIT
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
120 // this module initializes RichEdit DLL(s) if needed
121 class wxRichEditModule
: public wxModule
124 virtual bool OnInit();
125 virtual void OnExit();
127 // load the richedit DLL of at least of required version
128 static bool Load(int version
= 1);
131 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
132 static HINSTANCE ms_hRichEdit
[2];
134 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
137 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
139 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
141 #endif // wxUSE_RICHEDIT
143 // ----------------------------------------------------------------------------
144 // event tables and other macros
145 // ----------------------------------------------------------------------------
147 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
149 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
150 EVT_CHAR(wxTextCtrl::OnChar
)
151 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
154 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
157 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
158 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
159 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
160 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
161 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
162 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
163 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
165 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
166 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
167 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
168 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
169 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
170 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
171 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
173 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
177 // ============================================================================
179 // ============================================================================
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 void wxTextCtrl::Init()
189 #endif // wxUSE_RICHEDIT
191 m_privateContextMenu
= NULL
;
192 m_suppressNextUpdate
= FALSE
;
195 wxTextCtrl::~wxTextCtrl()
197 if (m_privateContextMenu
)
199 delete m_privateContextMenu
;
200 m_privateContextMenu
= NULL
;
204 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
205 const wxString
& value
,
209 const wxValidator
& validator
,
210 const wxString
& name
)
212 // base initialization
213 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
217 parent
->AddChild(this);
219 // translate wxWin style flags to MSW ones
220 WXDWORD msStyle
= MSWGetCreateWindowFlags();
222 // do create the control - either an EDIT or RICHEDIT
223 wxString windowClass
= wxT("EDIT");
226 if ( m_windowStyle
& wxTE_AUTO_URL
)
228 // automatic URL detection only works in RichEdit 2.0+
229 m_windowStyle
|= wxTE_RICH2
;
232 if ( m_windowStyle
& wxTE_RICH2
)
234 // using richedit 2.0 implies using wxTE_RICH
235 m_windowStyle
|= wxTE_RICH
;
238 // we need to load the richedit DLL before creating the rich edit control
239 if ( m_windowStyle
& wxTE_RICH
)
241 static bool s_errorGiven
= FALSE
;// MT-FIXME
243 // Which version do we need? Use 1.0 by default because it is much more
244 // like the the standard EDIT or 2.0 if explicitly requested, but use
245 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
247 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
248 // (and thus EDIT) much better than RichEdit 2.0 so we probably
249 // should use 3.0 if available as it is the best of both worlds -
250 // but as I can't test it right now I don't do it (VZ)
252 const int verRichEdit
= 2;
253 #else // !wxUSE_UNICODE
254 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
255 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
257 // only give the error msg once if the DLL can't be loaded
260 // try to load the RichEdit DLL (will do nothing if already done)
261 if ( !wxRichEditModule::Load(verRichEdit
) )
264 // try another version?
265 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
267 if ( !wxRichEditModule::Load(verRichEdit
) )
268 #endif // wxUSE_UNICODE
270 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
277 // have we managed to load any richedit version?
280 msStyle
|= ES_AUTOVSCROLL
;
282 m_verRichEdit
= verRichEdit
;
283 if ( m_verRichEdit
== 1 )
285 windowClass
= wxT("RICHEDIT");
289 #ifndef RICHEDIT_CLASS
290 wxString RICHEDIT_CLASS
;
291 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
293 RICHEDIT_CLASS
+= _T('W');
295 RICHEDIT_CLASS
+= _T('A');
296 #endif // Unicode/ANSI
297 #endif // !RICHEDIT_CLASS
299 windowClass
= RICHEDIT_CLASS
;
303 #endif // wxUSE_RICHEDIT
305 // we need to turn '\n's into "\r\n"s for the multiline controls
307 if ( m_windowStyle
& wxTE_MULTILINE
)
309 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
316 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
319 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
324 // enable the events we're interested in: we want to get EN_CHANGE as
325 // for the normal controls
326 LPARAM mask
= ENM_CHANGE
;
328 if ( GetRichVersion() == 1 )
330 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
331 // explained in its handler
332 mask
|= ENM_MOUSEEVENTS
;
334 else if ( m_windowStyle
& wxTE_AUTO_URL
)
338 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
341 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
343 #endif // wxUSE_RICHEDIT
348 // Make sure the window style (etc.) reflects the HWND style (roughly)
349 void wxTextCtrl::AdoptAttributesFromHWND()
351 wxWindow::AdoptAttributesFromHWND();
353 HWND hWnd
= GetHwnd();
354 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
356 // retrieve the style to see whether this is an edit or richedit ctrl
358 wxString classname
= wxGetWindowClass(GetHWND());
360 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
367 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
369 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
374 #endif // wxUSE_RICHEDIT
376 if (style
& ES_MULTILINE
)
377 m_windowStyle
|= wxTE_MULTILINE
;
378 if (style
& ES_PASSWORD
)
379 m_windowStyle
|= wxTE_PASSWORD
;
380 if (style
& ES_READONLY
)
381 m_windowStyle
|= wxTE_READONLY
;
382 if (style
& ES_WANTRETURN
)
383 m_windowStyle
|= wxTE_PROCESS_ENTER
;
384 if (style
& ES_CENTER
)
385 m_windowStyle
|= wxTE_CENTRE
;
386 if (style
& ES_RIGHT
)
387 m_windowStyle
|= wxTE_RIGHT
;
390 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
392 // default border for the text controls is the sunken one
393 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
395 style
|= wxBORDER_SUNKEN
;
398 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
403 if ( style
& wxTE_MULTILINE
)
405 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
406 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
408 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
409 if ( !(style
& wxTE_NO_VSCROLL
) )
410 msStyle
|= WS_VSCROLL
;
412 style
|= wxTE_PROCESS_ENTER
;
416 // there is really no reason to not have this style for single line
418 msStyle
|= ES_AUTOHSCROLL
;
421 if ( style
& wxHSCROLL
)
422 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
424 if ( style
& wxTE_READONLY
)
425 msStyle
|= ES_READONLY
;
427 if ( style
& wxTE_PASSWORD
)
428 msStyle
|= ES_PASSWORD
;
430 if ( style
& wxTE_AUTO_SCROLL
)
431 msStyle
|= ES_AUTOHSCROLL
;
433 if ( style
& wxTE_NOHIDESEL
)
434 msStyle
|= ES_NOHIDESEL
;
436 if ( style
& wxTE_CENTRE
)
437 msStyle
|= ES_CENTER
;
439 if ( style
& wxTE_RIGHT
)
445 void wxTextCtrl::SetWindowStyleFlag(long style
)
447 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
448 style
|= wxBORDER_SUNKEN
;
451 // we have to deal with some styles separately because they can't be
452 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
453 // using richedit-specific EM_SETOPTIONS
455 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
457 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
459 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
460 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
462 #endif // wxUSE_RICHEDIT
464 wxControl::SetWindowStyleFlag(style
);
467 // ----------------------------------------------------------------------------
468 // set/get the controls text
469 // ----------------------------------------------------------------------------
471 wxString
wxTextCtrl::GetValue() const
473 // range 0..-1 is special for GetRange() and means to retrieve all text
474 return GetRange(0, -1);
477 wxString
wxTextCtrl::GetRange(long from
, long to
) const
481 if ( from
>= to
&& to
!= -1 )
483 // nothing to retrieve
490 int len
= GetWindowTextLength(GetHwnd());
493 // alloc one extra WORD as needed by the control
494 wxChar
*p
= str
.GetWriteBuf(++len
);
497 textRange
.chrg
.cpMin
= from
;
498 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
499 textRange
.lpstrText
= p
;
501 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
503 if ( m_verRichEdit
> 1 )
505 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
506 // neither Unix nor Windows style - convert it to something
510 if ( *p
== _T('\r') )
517 if ( m_verRichEdit
== 1 )
519 // convert to the canonical form - see comment below
520 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
523 //else: no text at all, leave the string empty
526 #endif // wxUSE_RICHEDIT
529 str
= wxGetWindowText(GetHWND());
531 // need only a range?
534 str
= str
.Mid(from
, to
- from
);
537 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
538 // canonical one (same one as above) for consistency with the other kinds
539 // of controls and, more importantly, with the other ports
540 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
546 void wxTextCtrl::SetValue(const wxString
& value
)
548 // if the text is long enough, it's faster to just set it instead of first
549 // comparing it with the old one (chances are that it will be different
550 // anyhow, this comparison is there to avoid flicker for small single-line
551 // edit controls mostly)
552 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
554 DoWriteText(value
, FALSE
/* not selection only */);
556 // mark the control as being not dirty - we changed its text, not the
560 // for compatibility, don't move the cursor when doing SetValue()
561 SetInsertionPoint(0);
565 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
567 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
571 wchar_t *wbuf
= (wchar_t *)buf
;
572 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
577 cb
-= sizeof(wchar_t);
578 (*pcb
) += sizeof(wchar_t);
581 *(const wchar_t **)dwCookie
= wpc
;
586 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from utils.cpp
589 bool wxTextCtrl::StreamIn(const wxString
& value
,
590 wxFontEncoding
WXUNUSED(encoding
),
597 #if wxUSE_UNICODE_MSLU
598 bool wxTextCtrl::StreamIn(const wxString
& value
,
599 wxFontEncoding
WXUNUSED(encoding
),
602 const wchar_t *wpc
= value
.c_str();
603 #else // !wxUSE_UNICODE_MSLU
604 bool wxTextCtrl::StreamIn(const wxString
& value
,
605 wxFontEncoding encoding
,
608 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
609 // text in the non default charset -- otherwise it thinks it knows better
610 // than we do and always shows it in the default one
612 // first get the Windows code page for this encoding
613 long codepage
= wxEncodingToCodepage(encoding
);
614 if ( codepage
== -1 )
620 // next translate to Unicode using this code page
621 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
624 wxWCharBuffer
wchBuf(len
);
626 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
629 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
630 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
632 wxLogLastError(_T("MultiByteToWideChar"));
635 // finally, stream it in the control
636 const wchar_t *wpc
= wchBuf
;
637 #endif // wxUSE_UNICODE_MSLU
641 eds
.dwCookie
= (DWORD
)&wpc
;
642 // the cast below is needed for broken (very) old mingw32 headers
643 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
645 // we're going to receive 2 EN_CHANGE notifications if we got any selection
646 // (same problem as in DoWriteText())
647 if ( selectionOnly
&& HasSelection() )
649 // so suppress one of them
650 m_suppressNextUpdate
= TRUE
;
653 if ( !::SendMessage(GetHwnd(), EM_STREAMIN
,
656 (selectionOnly
? SFF_SELECTION
: 0),
657 (LPARAM
)&eds
) || eds
.dwError
)
659 wxLogLastError(_T("EM_STREAMIN"));
664 #endif // !wxUSE_WCHAR_T
669 #endif // __WXWINE__/!__WXWINE__
671 #endif // wxUSE_RICHEDIT
673 void wxTextCtrl::WriteText(const wxString
& value
)
678 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
681 if ( m_windowStyle
& wxTE_MULTILINE
)
682 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
687 // there are several complications with the rich edit controls here
691 // first, ensure that the new text will be in the default style
692 if ( !m_defaultStyle
.IsDefault() )
695 GetSelection(&start
, &end
);
696 SetStyle(start
, end
, m_defaultStyle
);
699 #if wxUSE_UNICODE_MSLU
700 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
701 // but EM_STREAMIN works
702 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
704 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
706 #endif // wxUSE_UNICODE_MSLU
708 #if !wxUSE_UNICODE && !defined(__WXWINE__)
709 // next check if the text we're inserting must be shown in a non
710 // default charset -- this only works for RichEdit > 1.0
711 if ( GetRichVersion() > 1 )
713 wxFont font
= m_defaultStyle
.GetFont();
719 wxFontEncoding encoding
= font
.GetEncoding();
720 if ( encoding
!= wxFONTENCODING_SYSTEM
)
722 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
726 #endif // !wxUSE_UNICODE
730 #endif // wxUSE_RICHEDIT
732 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
733 // call below which is confusing for the client code and so should be
736 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
737 // and there is a non empty selection currently and (b) rich text
738 // controls in any case
742 #endif // wxUSE_RICHEDIT
743 (selectionOnly
&& HasSelection()) )
745 m_suppressNextUpdate
= TRUE
;
748 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
749 0, (LPARAM
)valueDos
.c_str());
751 // OTOH, non rich text controls don't generate any events at all when
752 // we use WM_SETTEXT -- have to emulate them here
756 #endif // wxUSE_RICHEDIT
759 // Windows already sends an update event for single-line
761 if ( m_windowStyle
& wxTE_MULTILINE
)
769 void wxTextCtrl::AppendText(const wxString
& text
)
771 SetInsertionPointEnd();
776 if ( IsMultiLine() && GetRichVersion() > 1 )
778 // setting the caret to the end and showing it simply doesn't work for
779 // RichEdit 2.0 -- force it to still do what we want
780 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
782 #endif // wxUSE_RICHEDIT
785 void wxTextCtrl::Clear()
787 ::SetWindowText(GetHwnd(), wxT(""));
791 #endif // wxUSE_RICHEDIT
793 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
794 // but the normal ones don't -- make Clear() behaviour consistent by
795 // always sending this event
797 // Windows already sends an update event for single-line
799 if ( m_windowStyle
& wxTE_MULTILINE
)
806 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
810 size_t lenOld
= GetValue().length();
812 wxUint32 code
= event
.GetRawKeyCode();
813 ::keybd_event(code
, 0, 0 /* key press */, 0);
814 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
816 // assume that any alphanumeric key changes the total number of characters
817 // in the control - this should work in 99% of cases
818 return GetValue().length() != lenOld
;
823 // ----------------------------------------------------------------------------
824 // Clipboard operations
825 // ----------------------------------------------------------------------------
827 void wxTextCtrl::Copy()
831 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
835 void wxTextCtrl::Cut()
839 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
843 void wxTextCtrl::Paste()
847 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
851 bool wxTextCtrl::HasSelection() const
854 GetSelection(&from
, &to
);
858 bool wxTextCtrl::CanCopy() const
860 // Can copy if there's a selection
861 return HasSelection();
864 bool wxTextCtrl::CanCut() const
866 return CanCopy() && IsEditable();
869 bool wxTextCtrl::CanPaste() const
877 UINT cf
= 0; // 0 == any format
879 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
881 #endif // wxUSE_RICHEDIT
883 // Standard edit control: check for straight text on clipboard
884 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
887 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
890 return isTextAvailable
;
893 // ----------------------------------------------------------------------------
895 // ----------------------------------------------------------------------------
897 void wxTextCtrl::SetEditable(bool editable
)
899 HWND hWnd
= GetHwnd();
900 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
903 void wxTextCtrl::SetInsertionPoint(long pos
)
905 DoSetSelection(pos
, pos
);
908 void wxTextCtrl::SetInsertionPointEnd()
913 if ( m_verRichEdit
== 1 )
915 // we don't have to waste time calling GetLastPosition() in this case
918 else // !RichEdit 1.0
919 #endif // wxUSE_RICHEDIT
921 pos
= GetLastPosition();
924 SetInsertionPoint(pos
);
927 long wxTextCtrl::GetInsertionPoint() const
935 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
938 #endif // wxUSE_RICHEDIT
940 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
944 long wxTextCtrl::GetLastPosition() const
946 int numLines
= GetNumberOfLines();
947 long posStartLastLine
= XYToPosition(0, numLines
- 1);
949 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
951 return posStartLastLine
+ lenLastLine
;
954 // If the return values from and to are the same, there is no
956 void wxTextCtrl::GetSelection(long* from
, long* to
) const
962 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
964 *from
= charRange
.cpMin
;
965 *to
= charRange
.cpMax
;
968 #endif // !wxUSE_RICHEDIT
970 DWORD dwStart
, dwEnd
;
971 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
978 bool wxTextCtrl::IsEditable() const
980 // strangely enough, we may be called before the control is created: our
981 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
986 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
988 return (style
& ES_READONLY
) == 0;
991 // ----------------------------------------------------------------------------
993 // ----------------------------------------------------------------------------
995 void wxTextCtrl::SetSelection(long from
, long to
)
997 // if from and to are both -1, it means (in wxWindows) that all text should
998 // be selected - translate into Windows convention
999 if ( (from
== -1) && (to
== -1) )
1005 DoSetSelection(from
, to
);
1008 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
1010 HWND hWnd
= GetHwnd();
1016 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1017 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1018 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1019 // option is set (but it does work ok in richedit 1.0 mode...)
1021 // so to make it work we either need to give focus to it here which
1022 // will probably create many problems (dummy focus events; window
1023 // containing the text control being brought to foreground
1024 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1025 // create other problems too -- or it might not, so let's try to do it
1026 if ( GetRichVersion() > 1 )
1028 if ( !HasFlag(wxTE_NOHIDESEL
) )
1030 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1031 ECOOP_OR
, ECO_NOHIDESEL
);
1033 //else: everything is already ok
1039 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
1042 #endif // wxUSE_RICHEDIT
1044 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1049 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1053 // restore ECO_NOHIDESEL if we changed it
1054 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1056 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1057 ECOOP_AND
, ~ECO_NOHIDESEL
);
1059 #endif // wxUSE_RICHEDIT
1062 // WPARAM is 0: selection is scrolled into view
1063 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1067 // ----------------------------------------------------------------------------
1069 // ----------------------------------------------------------------------------
1071 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1073 // Set selection and remove it
1074 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1076 SendMessage(GetHwnd(), EM_REPLACESEL
,
1082 (LPARAM
)value
.c_str());
1085 void wxTextCtrl::Remove(long from
, long to
)
1087 Replace(from
, to
, _T(""));
1090 bool wxTextCtrl::LoadFile(const wxString
& file
)
1092 if ( wxTextCtrlBase::LoadFile(file
) )
1094 // update the size limit if needed
1103 bool wxTextCtrl::IsModified() const
1105 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1108 // Makes 'unmodified'
1109 void wxTextCtrl::DiscardEdits()
1111 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1114 int wxTextCtrl::GetNumberOfLines() const
1116 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1119 long wxTextCtrl::XYToPosition(long x
, long y
) const
1121 // This gets the char index for the _beginning_ of this line
1122 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1124 return charIndex
+ x
;
1127 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1129 HWND hWnd
= GetHwnd();
1131 // This gets the line number containing the character
1136 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1139 #endif // wxUSE_RICHEDIT
1141 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1150 // This gets the char index for the _beginning_ of this line
1151 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1152 if ( charIndex
== -1 )
1157 // The X position must therefore be the different between pos and charIndex
1159 *x
= pos
- charIndex
;
1166 void wxTextCtrl::ShowPosition(long pos
)
1168 HWND hWnd
= GetHwnd();
1170 // To scroll to a position, we pass the number of lines and characters
1171 // to scroll *by*. This means that we need to:
1172 // (1) Find the line position of the current line.
1173 // (2) Find the line position of pos.
1174 // (3) Scroll by (pos - current).
1175 // For now, ignore the horizontal scrolling.
1177 // Is this where scrolling is relative to - the line containing the caret?
1178 // Or is the first visible line??? Try first visible line.
1179 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1181 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1183 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1185 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1187 if (linesToScroll
!= 0)
1188 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1191 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1193 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1196 int wxTextCtrl::GetLineLength(long lineNo
) const
1198 long pos
= XYToPosition(0, lineNo
);
1200 return GetLengthOfLineContainingPos(pos
);
1203 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1205 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1207 // there must be at least enough place for the length WORD in the
1209 len
+= sizeof(WORD
);
1212 wxChar
*buf
= str
.GetWriteBuf(len
);
1214 *(WORD
*)buf
= (WORD
)len
;
1215 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1218 str
.UngetWriteBuf(len
);
1223 void wxTextCtrl::SetMaxLength(unsigned long len
)
1225 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1228 // ----------------------------------------------------------------------------
1230 // ----------------------------------------------------------------------------
1232 void wxTextCtrl::Undo()
1236 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1240 void wxTextCtrl::Redo()
1244 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1245 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1249 bool wxTextCtrl::CanUndo() const
1251 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1254 bool wxTextCtrl::CanRedo() const
1256 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1259 // ----------------------------------------------------------------------------
1260 // implemenation details
1261 // ----------------------------------------------------------------------------
1263 void wxTextCtrl::Command(wxCommandEvent
& event
)
1265 SetValue(event
.GetString());
1266 ProcessCommand (event
);
1269 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1271 // By default, load the first file into the text window.
1272 if (event
.GetNumberOfFiles() > 0)
1274 LoadFile(event
.GetFiles()[0]);
1278 // ----------------------------------------------------------------------------
1279 // kbd input processing
1280 // ----------------------------------------------------------------------------
1282 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1284 MSG
*msg
= (MSG
*)pMsg
;
1286 // check for our special keys here: if we don't do it and the parent frame
1287 // uses them as accelerators, they wouldn't work at all, so we disable
1288 // usual preprocessing for them
1289 if ( msg
->message
== WM_KEYDOWN
)
1291 WORD vkey
= (WORD
) msg
->wParam
;
1292 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1294 if ( vkey
== VK_BACK
)
1299 // we want to process some Ctrl-foo and Shift-bar but no key
1300 // combinations without either Ctrl or Shift nor with both of them
1302 const int ctrl
= wxIsCtrlDown(),
1303 shift
= wxIsShiftDown();
1304 switch ( ctrl
+ shift
)
1307 wxFAIL_MSG( _T("how many modifiers have we got?") );
1315 // either Ctrl or Shift pressed
1330 else // Shift is pressed
1332 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1339 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1342 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1344 switch ( event
.KeyCode() )
1347 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1349 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1350 InitCommandEvent(event
);
1351 event
.SetString(GetValue());
1352 if ( GetEventHandler()->ProcessEvent(event
) )
1355 //else: multiline controls need Enter for themselves
1360 // always produce navigation event - even if we process TAB
1361 // ourselves the fact that we got here means that the user code
1362 // decided to skip processing of this TAB - probably to let it
1363 // do its default job.
1365 wxNavigationKeyEvent eventNav
;
1366 eventNav
.SetDirection(!event
.ShiftDown());
1367 eventNav
.SetWindowChange(event
.ControlDown());
1368 eventNav
.SetEventObject(this);
1370 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1376 // no, we didn't process it
1380 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1382 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1384 if ( nMsg
== WM_GETDLGCODE
)
1386 // we always want the chars and the arrows: the arrows for navigation
1387 // and the chars because we want Ctrl-C to work even in a read only
1389 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1393 // we may have several different cases:
1394 // 1. normal case: both TAB and ENTER are used for dlg navigation
1395 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1396 // next control in the dialog
1397 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1399 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1400 // to the next control
1402 // the multiline edit control should always get <Return> for itself
1403 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1404 lDlgCode
|= DLGC_WANTMESSAGE
;
1406 if ( HasFlag(wxTE_PROCESS_TAB
) )
1407 lDlgCode
|= DLGC_WANTTAB
;
1413 // NB: use "=", not "|=" as the base class version returns the
1414 // same flags is this state as usual (i.e. including
1415 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1416 // native Win32 apps?) but for now live with it.
1424 // ----------------------------------------------------------------------------
1425 // text control event processing
1426 // ----------------------------------------------------------------------------
1428 bool wxTextCtrl::SendUpdateEvent()
1430 // is event reporting suspended?
1431 if ( m_suppressNextUpdate
)
1433 // do process the next one
1434 m_suppressNextUpdate
= FALSE
;
1439 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1440 InitCommandEvent(event
);
1441 event
.SetString(GetValue());
1443 return ProcessCommand(event
);
1446 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1453 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1456 event
.SetEventObject(this);
1457 GetEventHandler()->ProcessEvent(event
);
1466 // the text size limit has been hit -- try to increase it
1467 if ( !AdjustSpaceLimit() )
1469 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1470 InitCommandEvent(event
);
1471 event
.SetString(GetValue());
1472 ProcessCommand(event
);
1476 // the other edit notification messages are not processed
1485 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1491 WXUINT
WXUNUSED(message
),
1492 WXWPARAM
WXUNUSED(wParam
),
1493 WXLPARAM
WXUNUSED(lParam
)
1500 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1501 return (WXHBRUSH
) hbrush
;
1503 #endif // wxUSE_CTL3D
1506 if (GetParent()->GetTransparentBackground())
1507 SetBkMode(hdc
, TRANSPARENT
);
1509 SetBkMode(hdc
, OPAQUE
);
1511 wxColour colBack
= GetBackgroundColour();
1513 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1514 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1516 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1517 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1519 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1521 return (WXHBRUSH
)brush
->GetResourceHandle();
1524 // In WIN16, need to override normal erasing because
1525 // Ctl3D doesn't use the wxWindows background colour.
1527 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1529 wxColour
col(m_backgroundColour
);
1533 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1537 ::GetClientRect(GetHwnd(), &rect
);
1539 COLORREF ref
= wxColourToRGB(col
);
1540 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1542 wxLogLastError(wxT("CreateSolidBrush"));
1544 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1546 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1548 ::FillRect(hdc
, &rect
, hBrush
);
1549 ::DeleteObject(hBrush
);
1550 ::SetMapMode(hdc
, mode
);
1555 bool wxTextCtrl::AdjustSpaceLimit()
1558 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1560 // HACK: we try to automatically extend the limit for the amount of text
1561 // to allow (interactively) entering more than 64Kb of text under
1562 // Win9x but we shouldn't reset the text limit which was previously
1563 // set explicitly with SetMaxLength()
1565 // we could solve this by storing the limit we set in wxTextCtrl but
1566 // to save space we prefer to simply test here the actual limit
1567 // value: we consider that SetMaxLength() can only be called for
1569 if ( limit
< 0x8000 )
1571 // we've got more text than limit set by SetMaxLength()
1575 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1578 limit
= len
+ 0x8000; // 32Kb
1583 // as a nice side effect, this also allows passing limit > 64Kb
1584 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1587 #endif // wxUSE_RICHEDIT
1589 if ( limit
> 0xffff )
1591 // this will set it to a platform-dependent maximum (much more
1592 // than 64Kb under NT)
1596 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1601 // we changed the limit
1605 bool wxTextCtrl::AcceptsFocus() const
1607 // we don't want focus if we can't be edited unless we're a multiline
1608 // control because then it might be still nice to get focus from keyboard
1609 // to be able to scroll it without mouse
1610 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1613 wxSize
wxTextCtrl::DoGetBestSize() const
1616 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1618 int wText
= DEFAULT_ITEM_WIDTH
;
1620 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1621 if ( m_windowStyle
& wxTE_MULTILINE
)
1623 hText
*= wxMax(GetNumberOfLines(), 5);
1625 //else: for single line control everything is ok
1627 return wxSize(wText
, hText
);
1630 // ----------------------------------------------------------------------------
1631 // standard handlers for standard edit menu events
1632 // ----------------------------------------------------------------------------
1634 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1639 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1644 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1649 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1654 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1659 void wxTextCtrl::OnDelete(wxCommandEvent
& event
)
1662 GetSelection(& from
, & to
);
1663 if (from
!= -1 && to
!= -1)
1667 void wxTextCtrl::OnSelectAll(wxCommandEvent
& event
)
1669 SetSelection(-1, -1);
1672 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1674 event
.Enable( CanCut() );
1677 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1679 event
.Enable( CanCopy() );
1682 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1684 event
.Enable( CanPaste() );
1687 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1689 event
.Enable( CanUndo() );
1692 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1694 event
.Enable( CanRedo() );
1697 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1700 GetSelection(& from
, & to
);
1701 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1704 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1706 event
.Enable(GetLastPosition() > 0);
1709 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1714 if (!m_privateContextMenu
)
1716 m_privateContextMenu
= new wxMenu
;
1717 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1718 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1719 m_privateContextMenu
->AppendSeparator();
1720 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1721 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1722 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1723 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1724 m_privateContextMenu
->AppendSeparator();
1725 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1727 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1735 // the rest of the file only deals with the rich edit controls
1738 // ----------------------------------------------------------------------------
1739 // EN_LINK processing
1740 // ----------------------------------------------------------------------------
1742 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1745 NMHDR
*hdr
= (NMHDR
* )lParam
;
1746 switch ( hdr
->code
)
1750 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1751 UINT msg
= msgf
->msg
;
1753 // this is a bit crazy but richedit 1.0 sends us all mouse
1754 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1755 // generate the wxWin events for this message manually
1757 // NB: in fact, this is still not totally correct as it does
1758 // send us WM_LBUTTONUP if the selection was cleared by the
1759 // last click -- so currently we get 2 events in this case,
1760 // but as I don't see any obvious way to check for this I
1761 // leave this code in place because it's still better than
1762 // not getting left up events at all
1763 if ( msg
== WM_LBUTTONUP
)
1765 WXUINT flags
= msgf
->wParam
;
1766 int x
= GET_X_LPARAM(msgf
->lParam
),
1767 y
= GET_Y_LPARAM(msgf
->lParam
);
1769 HandleMouseEvent(msg
, x
, y
, flags
);
1773 // return TRUE to process the event (and FALSE to ignore it)
1778 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1780 switch ( enlink
->msg
)
1783 // ok, so it is hardcoded - do we really nee to
1785 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1790 case WM_LBUTTONDOWN
:
1792 case WM_LBUTTONDBLCLK
:
1793 case WM_RBUTTONDOWN
:
1795 case WM_RBUTTONDBLCLK
:
1796 // send a mouse event
1798 static const wxEventType eventsMouse
[] =
1809 // the event ids are consecutive
1811 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1813 InitMouseEvent(evtMouse
,
1814 GET_X_LPARAM(enlink
->lParam
),
1815 GET_Y_LPARAM(enlink
->lParam
),
1818 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1820 enlink
->chrg
.cpMax
);
1822 InitCommandEvent(event
);
1824 *result
= ProcessCommand(event
);
1833 // not processed, leave it to the base class
1834 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1837 // ----------------------------------------------------------------------------
1838 // colour setting for the rich edit controls
1839 // ----------------------------------------------------------------------------
1841 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1843 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1845 // colour didn't really change
1851 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1852 // EM_SETBKGNDCOLOR additionally
1853 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1859 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1861 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1863 // colour didn't really change
1869 // change the colour of everything
1872 cf
.cbSize
= sizeof(cf
);
1873 cf
.dwMask
= CFM_COLOR
;
1874 cf
.crTextColor
= wxColourToRGB(colour
);
1875 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1881 // ----------------------------------------------------------------------------
1882 // styling support for rich edit controls
1883 // ----------------------------------------------------------------------------
1887 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1894 // can't do it with normal text control
1898 // the richedit 1.0 doesn't handle setting background colour, so don't
1899 // even try to do anything if it's the only thing we want to change
1900 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() )
1902 // nothing to do: return TRUE if there was really nothing to do and
1903 // FALSE if we failed to set bg colour
1904 return !style
.HasBackgroundColour();
1907 // order the range if needed
1915 // we can only change the format of the selection, so select the range we
1916 // want and restore the old selection later
1917 long startOld
, endOld
;
1918 GetSelection(&startOld
, &endOld
);
1920 // but do we really have to change the selection?
1921 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1925 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1928 // initialize CHARFORMAT struct
1937 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1938 // CHARFORMAT in that case
1940 if ( m_verRichEdit
== 1 )
1942 // this is the only thing the control is going to grok
1943 cf
.cbSize
= sizeof(CHARFORMAT
);
1948 // CHARFORMAT or CHARFORMAT2
1949 cf
.cbSize
= sizeof(cf
);
1952 if ( style
.HasFont() )
1954 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1955 // but using it doesn't seem to hurt neither so leaving it for now
1957 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1958 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1960 // fill in data from LOGFONT but recalculate lfHeight because we need
1961 // the real height in twips and not the negative number which
1962 // wxFillLogFont() returns (this is correct in general and works with
1963 // the Windows font mapper, but not here)
1965 wxFillLogFont(&lf
, &style
.GetFont());
1966 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1967 cf
.bCharSet
= lf
.lfCharSet
;
1968 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1969 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1971 // also deal with underline/italic/bold attributes: note that we must
1972 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1973 // style to allow clearing it
1976 cf
.dwEffects
|= CFE_ITALIC
;
1979 if ( lf
.lfWeight
== FW_BOLD
)
1981 cf
.dwEffects
|= CFE_BOLD
;
1984 if ( lf
.lfUnderline
)
1986 cf
.dwEffects
|= CFE_UNDERLINE
;
1989 // strikeout fonts are not supported by wxWindows
1992 if ( style
.HasTextColour() )
1994 cf
.dwMask
|= CFM_COLOR
;
1995 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1999 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
2001 cf
.dwMask
|= CFM_BACKCOLOR
;
2002 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
2004 #endif // wxUSE_RICHEDIT2
2006 // do format the selection
2007 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
2008 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
2011 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
2016 // restore the original selection
2017 DoSetSelection(startOld
, endOld
, FALSE
);
2024 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2026 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2029 // we have to do this or the style wouldn't apply for the text typed by the
2031 long posLast
= GetLastPosition();
2032 SetStyle(posLast
, posLast
, m_defaultStyle
);
2039 // ----------------------------------------------------------------------------
2041 // ----------------------------------------------------------------------------
2043 bool wxRichEditModule::OnInit()
2045 // don't do anything - we will load it when needed
2049 void wxRichEditModule::OnExit()
2051 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2053 if ( ms_hRichEdit
[i
] )
2055 ::FreeLibrary(ms_hRichEdit
[i
]);
2061 bool wxRichEditModule::Load(int version
)
2063 // we don't support loading richedit 3.0 as I don't know how to distinguish
2064 // it from 2.0 anyhow
2065 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2066 _T("incorrect richedit control version requested") );
2068 // make it the index in the array
2071 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2073 // we had already tried to load it and failed
2077 if ( ms_hRichEdit
[version
] )
2079 // we've already got this one
2083 wxString dllname
= version
? _T("riched20") : _T("riched32");
2084 dllname
+= _T(".dll");
2086 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2088 if ( !ms_hRichEdit
[version
] )
2090 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2092 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2100 #endif // wxUSE_RICHEDIT
2102 #endif // wxUSE_TEXTCTRL