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
)
448 // we have to deal with some styles separately because they can't be
449 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
450 // using richedit-specific EM_SETOPTIONS
452 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
454 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
456 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
457 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
459 #endif // wxUSE_RICHEDIT
461 wxControl::SetWindowStyleFlag(style
);
464 // ----------------------------------------------------------------------------
465 // set/get the controls text
466 // ----------------------------------------------------------------------------
468 wxString
wxTextCtrl::GetValue() const
470 // range 0..-1 is special for GetRange() and means to retrieve all text
471 return GetRange(0, -1);
474 wxString
wxTextCtrl::GetRange(long from
, long to
) const
478 if ( from
>= to
&& to
!= -1 )
480 // nothing to retrieve
487 int len
= GetWindowTextLength(GetHwnd());
490 // alloc one extra WORD as needed by the control
491 wxChar
*p
= str
.GetWriteBuf(++len
);
494 textRange
.chrg
.cpMin
= from
;
495 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
496 textRange
.lpstrText
= p
;
498 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
500 if ( m_verRichEdit
> 1 )
502 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
503 // neither Unix nor Windows style - convert it to something
507 if ( *p
== _T('\r') )
514 if ( m_verRichEdit
== 1 )
516 // convert to the canonical form - see comment below
517 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
520 //else: no text at all, leave the string empty
523 #endif // wxUSE_RICHEDIT
526 str
= wxGetWindowText(GetHWND());
528 // need only a range?
531 str
= str
.Mid(from
, to
- from
);
534 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
535 // canonical one (same one as above) for consistency with the other kinds
536 // of controls and, more importantly, with the other ports
537 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
543 void wxTextCtrl::SetValue(const wxString
& value
)
545 // if the text is long enough, it's faster to just set it instead of first
546 // comparing it with the old one (chances are that it will be different
547 // anyhow, this comparison is there to avoid flicker for small single-line
548 // edit controls mostly)
549 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
551 DoWriteText(value
, FALSE
/* not selection only */);
553 // mark the control as being not dirty - we changed its text, not the
557 // for compatibility, don't move the cursor when doing SetValue()
558 SetInsertionPoint(0);
562 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
564 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
568 wchar_t *wbuf
= (wchar_t *)buf
;
569 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
574 cb
-= sizeof(wchar_t);
575 (*pcb
) += sizeof(wchar_t);
578 *(const wchar_t **)dwCookie
= wpc
;
583 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from utils.cpp
586 bool wxTextCtrl::StreamIn(const wxString
& value
,
587 wxFontEncoding
WXUNUSED(encoding
),
594 #if wxUSE_UNICODE_MSLU
595 bool wxTextCtrl::StreamIn(const wxString
& value
,
596 wxFontEncoding
WXUNUSED(encoding
),
599 const wchar_t *wpc
= value
.c_str();
600 #else // !wxUSE_UNICODE_MSLU
601 bool wxTextCtrl::StreamIn(const wxString
& value
,
602 wxFontEncoding encoding
,
605 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
606 // text in the non default charset -- otherwise it thinks it knows better
607 // than we do and always shows it in the default one
609 // first get the Windows code page for this encoding
610 long codepage
= wxEncodingToCodepage(encoding
);
611 if ( codepage
== -1 )
617 // next translate to Unicode using this code page
618 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
621 wxWCharBuffer
wchBuf(len
);
623 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
626 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
627 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
629 wxLogLastError(_T("MultiByteToWideChar"));
632 // finally, stream it in the control
633 const wchar_t *wpc
= wchBuf
;
634 #endif // wxUSE_UNICODE_MSLU
638 eds
.dwCookie
= (DWORD
)&wpc
;
639 // the cast below is needed for broken (very) old mingw32 headers
640 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
642 // we're going to receive 2 EN_CHANGE notifications if we got any selection
643 // (same problem as in DoWriteText())
644 if ( selectionOnly
&& HasSelection() )
646 // so suppress one of them
647 m_suppressNextUpdate
= TRUE
;
650 if ( !::SendMessage(GetHwnd(), EM_STREAMIN
,
653 (selectionOnly
? SFF_SELECTION
: 0),
654 (LPARAM
)&eds
) || eds
.dwError
)
656 wxLogLastError(_T("EM_STREAMIN"));
661 #endif // !wxUSE_WCHAR_T
666 #endif // __WXWINE__/!__WXWINE__
668 #endif // wxUSE_RICHEDIT
670 void wxTextCtrl::WriteText(const wxString
& value
)
675 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
678 if ( m_windowStyle
& wxTE_MULTILINE
)
679 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
684 // there are several complications with the rich edit controls here
688 // first, ensure that the new text will be in the default style
689 if ( !m_defaultStyle
.IsDefault() )
692 GetSelection(&start
, &end
);
693 SetStyle(start
, end
, m_defaultStyle
);
696 #if wxUSE_UNICODE_MSLU
697 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
698 // but EM_STREAMIN works
699 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
701 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
703 #endif // wxUSE_UNICODE_MSLU
705 #if !wxUSE_UNICODE && !defined(__WXWINE__)
706 // next check if the text we're inserting must be shown in a non
707 // default charset -- this only works for RichEdit > 1.0
708 if ( GetRichVersion() > 1 )
710 wxFont font
= m_defaultStyle
.GetFont();
716 wxFontEncoding encoding
= font
.GetEncoding();
717 if ( encoding
!= wxFONTENCODING_SYSTEM
)
719 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
723 #endif // !wxUSE_UNICODE
727 #endif // wxUSE_RICHEDIT
729 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
730 // call below which is confusing for the client code and so should be
733 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
734 // and there is a non empty selection currently and (b) rich text
735 // controls in any case
739 #endif // wxUSE_RICHEDIT
740 (selectionOnly
&& HasSelection()) )
742 m_suppressNextUpdate
= TRUE
;
745 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
746 0, (LPARAM
)valueDos
.c_str());
748 // OTOH, non rich text controls don't generate any events at all when
749 // we use WM_SETTEXT -- have to emulate them here
753 #endif // wxUSE_RICHEDIT
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(), wxT(""));
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
796 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
800 size_t lenOld
= GetValue().length();
802 wxUint32 code
= event
.GetRawKeyCode();
803 ::keybd_event(code
, 0, 0 /* key press */, 0);
804 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
806 // assume that any alphanumeric key changes the total number of characters
807 // in the control - this should work in 99% of cases
808 return GetValue().length() != lenOld
;
813 // ----------------------------------------------------------------------------
814 // Clipboard operations
815 // ----------------------------------------------------------------------------
817 void wxTextCtrl::Copy()
821 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
825 void wxTextCtrl::Cut()
829 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
833 void wxTextCtrl::Paste()
837 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
841 bool wxTextCtrl::HasSelection() const
844 GetSelection(&from
, &to
);
848 bool wxTextCtrl::CanCopy() const
850 // Can copy if there's a selection
851 return HasSelection();
854 bool wxTextCtrl::CanCut() const
856 return CanCopy() && IsEditable();
859 bool wxTextCtrl::CanPaste() const
867 UINT cf
= 0; // 0 == any format
869 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
871 #endif // wxUSE_RICHEDIT
873 // Standard edit control: check for straight text on clipboard
874 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
877 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
880 return isTextAvailable
;
883 // ----------------------------------------------------------------------------
885 // ----------------------------------------------------------------------------
887 void wxTextCtrl::SetEditable(bool editable
)
889 HWND hWnd
= GetHwnd();
890 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
893 void wxTextCtrl::SetInsertionPoint(long pos
)
895 DoSetSelection(pos
, pos
);
898 void wxTextCtrl::SetInsertionPointEnd()
903 if ( m_verRichEdit
== 1 )
905 // we don't have to waste time calling GetLastPosition() in this case
908 else // !RichEdit 1.0
909 #endif // wxUSE_RICHEDIT
911 pos
= GetLastPosition();
914 SetInsertionPoint(pos
);
917 long wxTextCtrl::GetInsertionPoint() const
925 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
928 #endif // wxUSE_RICHEDIT
930 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
934 long wxTextCtrl::GetLastPosition() const
936 int numLines
= GetNumberOfLines();
937 long posStartLastLine
= XYToPosition(0, numLines
- 1);
939 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
941 return posStartLastLine
+ lenLastLine
;
944 // If the return values from and to are the same, there is no
946 void wxTextCtrl::GetSelection(long* from
, long* to
) const
952 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
954 *from
= charRange
.cpMin
;
955 *to
= charRange
.cpMax
;
958 #endif // !wxUSE_RICHEDIT
960 DWORD dwStart
, dwEnd
;
961 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
968 bool wxTextCtrl::IsEditable() const
970 // strangely enough, we may be called before the control is created: our
971 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
976 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
978 return (style
& ES_READONLY
) == 0;
981 // ----------------------------------------------------------------------------
983 // ----------------------------------------------------------------------------
985 void wxTextCtrl::SetSelection(long from
, long to
)
987 // if from and to are both -1, it means (in wxWindows) that all text should
988 // be selected - translate into Windows convention
989 if ( (from
== -1) && (to
== -1) )
995 DoSetSelection(from
, to
);
998 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
1000 HWND hWnd
= GetHwnd();
1009 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
1012 #endif // wxUSE_RICHEDIT
1014 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1019 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1022 // WPARAM is 0: selection is scrolled into view
1023 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1027 // ----------------------------------------------------------------------------
1029 // ----------------------------------------------------------------------------
1031 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1033 // Set selection and remove it
1034 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1036 SendMessage(GetHwnd(), EM_REPLACESEL
,
1042 (LPARAM
)value
.c_str());
1045 void wxTextCtrl::Remove(long from
, long to
)
1047 Replace(from
, to
, _T(""));
1050 bool wxTextCtrl::LoadFile(const wxString
& file
)
1052 if ( wxTextCtrlBase::LoadFile(file
) )
1054 // update the size limit if needed
1063 bool wxTextCtrl::IsModified() const
1065 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1068 // Makes 'unmodified'
1069 void wxTextCtrl::DiscardEdits()
1071 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1074 int wxTextCtrl::GetNumberOfLines() const
1076 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1079 long wxTextCtrl::XYToPosition(long x
, long y
) const
1081 // This gets the char index for the _beginning_ of this line
1082 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1084 return charIndex
+ x
;
1087 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1089 HWND hWnd
= GetHwnd();
1091 // This gets the line number containing the character
1096 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1099 #endif // wxUSE_RICHEDIT
1101 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1110 // This gets the char index for the _beginning_ of this line
1111 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1112 if ( charIndex
== -1 )
1117 // The X position must therefore be the different between pos and charIndex
1119 *x
= pos
- charIndex
;
1126 void wxTextCtrl::ShowPosition(long pos
)
1128 HWND hWnd
= GetHwnd();
1130 // To scroll to a position, we pass the number of lines and characters
1131 // to scroll *by*. This means that we need to:
1132 // (1) Find the line position of the current line.
1133 // (2) Find the line position of pos.
1134 // (3) Scroll by (pos - current).
1135 // For now, ignore the horizontal scrolling.
1137 // Is this where scrolling is relative to - the line containing the caret?
1138 // Or is the first visible line??? Try first visible line.
1139 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1141 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1143 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1145 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1147 if (linesToScroll
!= 0)
1148 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1151 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1153 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1156 int wxTextCtrl::GetLineLength(long lineNo
) const
1158 long pos
= XYToPosition(0, lineNo
);
1160 return GetLengthOfLineContainingPos(pos
);
1163 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1165 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1167 // there must be at least enough place for the length WORD in the
1169 len
+= sizeof(WORD
);
1172 wxChar
*buf
= str
.GetWriteBuf(len
);
1174 *(WORD
*)buf
= (WORD
)len
;
1175 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1178 str
.UngetWriteBuf(len
);
1183 void wxTextCtrl::SetMaxLength(unsigned long len
)
1185 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1188 // ----------------------------------------------------------------------------
1190 // ----------------------------------------------------------------------------
1192 void wxTextCtrl::Undo()
1196 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1200 void wxTextCtrl::Redo()
1204 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1205 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1209 bool wxTextCtrl::CanUndo() const
1211 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1214 bool wxTextCtrl::CanRedo() const
1216 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1219 // ----------------------------------------------------------------------------
1220 // implemenation details
1221 // ----------------------------------------------------------------------------
1223 void wxTextCtrl::Command(wxCommandEvent
& event
)
1225 SetValue(event
.GetString());
1226 ProcessCommand (event
);
1229 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1231 // By default, load the first file into the text window.
1232 if (event
.GetNumberOfFiles() > 0)
1234 LoadFile(event
.GetFiles()[0]);
1238 // ----------------------------------------------------------------------------
1239 // kbd input processing
1240 // ----------------------------------------------------------------------------
1242 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1244 MSG
*msg
= (MSG
*)pMsg
;
1246 // check for our special keys here: if we don't do it and the parent frame
1247 // uses them as accelerators, they wouldn't work at all, so we disable
1248 // usual preprocessing for them
1249 if ( msg
->message
== WM_KEYDOWN
)
1251 WORD vkey
= (WORD
) msg
->wParam
;
1252 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1254 if ( vkey
== VK_BACK
)
1259 // we want to process some Ctrl-foo and Shift-bar but no key
1260 // combinations without either Ctrl or Shift nor with both of them
1262 const int ctrl
= wxIsCtrlDown(),
1263 shift
= wxIsShiftDown();
1264 switch ( ctrl
+ shift
)
1267 wxFAIL_MSG( _T("how many modifiers have we got?") );
1275 // either Ctrl or Shift pressed
1290 else // Shift is pressed
1292 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1299 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1302 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1304 switch ( event
.KeyCode() )
1307 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1309 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1310 InitCommandEvent(event
);
1311 event
.SetString(GetValue());
1312 if ( GetEventHandler()->ProcessEvent(event
) )
1315 //else: multiline controls need Enter for themselves
1320 // always produce navigation event - even if we process TAB
1321 // ourselves the fact that we got here means that the user code
1322 // decided to skip processing of this TAB - probably to let it
1323 // do its default job.
1325 wxNavigationKeyEvent eventNav
;
1326 eventNav
.SetDirection(!event
.ShiftDown());
1327 eventNav
.SetWindowChange(event
.ControlDown());
1328 eventNav
.SetEventObject(this);
1330 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1336 // no, we didn't process it
1340 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1342 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1344 if ( nMsg
== WM_GETDLGCODE
)
1346 // we always want the chars and the arrows: the arrows for navigation
1347 // and the chars because we want Ctrl-C to work even in a read only
1349 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1353 // we may have several different cases:
1354 // 1. normal case: both TAB and ENTER are used for dlg navigation
1355 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1356 // next control in the dialog
1357 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1359 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1360 // to the next control
1362 // the multiline edit control should always get <Return> for itself
1363 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1364 lDlgCode
|= DLGC_WANTMESSAGE
;
1366 if ( HasFlag(wxTE_PROCESS_TAB
) )
1367 lDlgCode
|= DLGC_WANTTAB
;
1373 // NB: use "=", not "|=" as the base class version returns the
1374 // same flags is this state as usual (i.e. including
1375 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1376 // native Win32 apps?) but for now live with it.
1384 // ----------------------------------------------------------------------------
1385 // text control event processing
1386 // ----------------------------------------------------------------------------
1388 bool wxTextCtrl::SendUpdateEvent()
1390 // is event reporting suspended?
1391 if ( m_suppressNextUpdate
)
1393 // do process the next one
1394 m_suppressNextUpdate
= FALSE
;
1399 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1400 InitCommandEvent(event
);
1401 event
.SetString(GetValue());
1403 return ProcessCommand(event
);
1406 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1413 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1416 event
.SetEventObject(this);
1417 GetEventHandler()->ProcessEvent(event
);
1426 // the text size limit has been hit -- try to increase it
1427 if ( !AdjustSpaceLimit() )
1429 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1430 InitCommandEvent(event
);
1431 event
.SetString(GetValue());
1432 ProcessCommand(event
);
1436 // the other edit notification messages are not processed
1445 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1451 WXUINT
WXUNUSED(message
),
1452 WXWPARAM
WXUNUSED(wParam
),
1453 WXLPARAM
WXUNUSED(lParam
)
1460 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1461 return (WXHBRUSH
) hbrush
;
1463 #endif // wxUSE_CTL3D
1466 if (GetParent()->GetTransparentBackground())
1467 SetBkMode(hdc
, TRANSPARENT
);
1469 SetBkMode(hdc
, OPAQUE
);
1471 wxColour colBack
= GetBackgroundColour();
1473 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1474 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1476 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1477 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1479 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1481 return (WXHBRUSH
)brush
->GetResourceHandle();
1484 // In WIN16, need to override normal erasing because
1485 // Ctl3D doesn't use the wxWindows background colour.
1487 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1489 wxColour
col(m_backgroundColour
);
1493 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1497 ::GetClientRect(GetHwnd(), &rect
);
1499 COLORREF ref
= wxColourToRGB(col
);
1500 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1502 wxLogLastError(wxT("CreateSolidBrush"));
1504 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1506 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1508 ::FillRect(hdc
, &rect
, hBrush
);
1509 ::DeleteObject(hBrush
);
1510 ::SetMapMode(hdc
, mode
);
1515 bool wxTextCtrl::AdjustSpaceLimit()
1518 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1520 // HACK: we try to automatically extend the limit for the amount of text
1521 // to allow (interactively) entering more than 64Kb of text under
1522 // Win9x but we shouldn't reset the text limit which was previously
1523 // set explicitly with SetMaxLength()
1525 // we could solve this by storing the limit we set in wxTextCtrl but
1526 // to save space we prefer to simply test here the actual limit
1527 // value: we consider that SetMaxLength() can only be called for
1529 if ( limit
< 0x8000 )
1531 // we've got more text than limit set by SetMaxLength()
1535 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1538 limit
= len
+ 0x8000; // 32Kb
1543 // as a nice side effect, this also allows passing limit > 64Kb
1544 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1547 #endif // wxUSE_RICHEDIT
1549 if ( limit
> 0xffff )
1551 // this will set it to a platform-dependent maximum (much more
1552 // than 64Kb under NT)
1556 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1561 // we changed the limit
1565 bool wxTextCtrl::AcceptsFocus() const
1567 // we don't want focus if we can't be edited unless we're a multiline
1568 // control because then it might be still nice to get focus from keyboard
1569 // to be able to scroll it without mouse
1570 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1573 wxSize
wxTextCtrl::DoGetBestSize() const
1576 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1578 int wText
= DEFAULT_ITEM_WIDTH
;
1580 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1581 if ( m_windowStyle
& wxTE_MULTILINE
)
1583 hText
*= wxMax(GetNumberOfLines(), 5);
1585 //else: for single line control everything is ok
1587 return wxSize(wText
, hText
);
1590 // ----------------------------------------------------------------------------
1591 // standard handlers for standard edit menu events
1592 // ----------------------------------------------------------------------------
1594 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1599 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1604 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1609 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1614 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1619 void wxTextCtrl::OnDelete(wxCommandEvent
& event
)
1622 GetSelection(& from
, & to
);
1623 if (from
!= -1 && to
!= -1)
1627 void wxTextCtrl::OnSelectAll(wxCommandEvent
& event
)
1629 SetSelection(-1, -1);
1632 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1634 event
.Enable( CanCut() );
1637 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1639 event
.Enable( CanCopy() );
1642 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1644 event
.Enable( CanPaste() );
1647 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1649 event
.Enable( CanUndo() );
1652 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1654 event
.Enable( CanRedo() );
1657 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1660 GetSelection(& from
, & to
);
1661 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1664 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1666 event
.Enable(GetLastPosition() > 0);
1669 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1674 if (!m_privateContextMenu
)
1676 m_privateContextMenu
= new wxMenu
;
1677 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1678 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1679 m_privateContextMenu
->AppendSeparator();
1680 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1681 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1682 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1683 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1684 m_privateContextMenu
->AppendSeparator();
1685 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1687 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1695 // the rest of the file only deals with the rich edit controls
1698 // ----------------------------------------------------------------------------
1699 // EN_LINK processing
1700 // ----------------------------------------------------------------------------
1702 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1705 NMHDR
*hdr
= (NMHDR
* )lParam
;
1706 switch ( hdr
->code
)
1710 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1711 UINT msg
= msgf
->msg
;
1713 // this is a bit crazy but richedit 1.0 sends us all mouse
1714 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1715 // generate the wxWin events for this message manually
1717 // NB: in fact, this is still not totally correct as it does
1718 // send us WM_LBUTTONUP if the selection was cleared by the
1719 // last click -- so currently we get 2 events in this case,
1720 // but as I don't see any obvious way to check for this I
1721 // leave this code in place because it's still better than
1722 // not getting left up events at all
1723 if ( msg
== WM_LBUTTONUP
)
1725 WXUINT flags
= msgf
->wParam
;
1726 int x
= GET_X_LPARAM(msgf
->lParam
),
1727 y
= GET_Y_LPARAM(msgf
->lParam
);
1729 HandleMouseEvent(msg
, x
, y
, flags
);
1733 // return TRUE to process the event (and FALSE to ignore it)
1738 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1740 switch ( enlink
->msg
)
1743 // ok, so it is hardcoded - do we really nee to
1745 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1750 case WM_LBUTTONDOWN
:
1752 case WM_LBUTTONDBLCLK
:
1753 case WM_RBUTTONDOWN
:
1755 case WM_RBUTTONDBLCLK
:
1756 // send a mouse event
1758 static const wxEventType eventsMouse
[] =
1769 // the event ids are consecutive
1771 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1773 InitMouseEvent(evtMouse
,
1774 GET_X_LPARAM(enlink
->lParam
),
1775 GET_Y_LPARAM(enlink
->lParam
),
1778 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1780 enlink
->chrg
.cpMax
);
1782 InitCommandEvent(event
);
1784 *result
= ProcessCommand(event
);
1793 // not processed, leave it to the base class
1794 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1797 // ----------------------------------------------------------------------------
1798 // colour setting for the rich edit controls
1799 // ----------------------------------------------------------------------------
1801 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1803 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1805 // colour didn't really change
1811 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1812 // EM_SETBKGNDCOLOR additionally
1813 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1819 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1821 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1823 // colour didn't really change
1829 // change the colour of everything
1832 cf
.cbSize
= sizeof(cf
);
1833 cf
.dwMask
= CFM_COLOR
;
1834 cf
.crTextColor
= wxColourToRGB(colour
);
1835 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1841 // ----------------------------------------------------------------------------
1842 // styling support for rich edit controls
1843 // ----------------------------------------------------------------------------
1847 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1854 // can't do it with normal text control
1858 // the richedit 1.0 doesn't handle setting background colour, so don't
1859 // even try to do anything if it's the only thing we want to change
1860 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() )
1862 // nothing to do: return TRUE if there was really nothing to do and
1863 // FALSE if we failed to set bg colour
1864 return !style
.HasBackgroundColour();
1867 // order the range if needed
1875 // we can only change the format of the selection, so select the range we
1876 // want and restore the old selection later
1877 long startOld
, endOld
;
1878 GetSelection(&startOld
, &endOld
);
1880 // but do we really have to change the selection?
1881 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1885 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1888 // initialize CHARFORMAT struct
1897 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1898 // CHARFORMAT in that case
1900 if ( m_verRichEdit
== 1 )
1902 // this is the only thing the control is going to grok
1903 cf
.cbSize
= sizeof(CHARFORMAT
);
1908 // CHARFORMAT or CHARFORMAT2
1909 cf
.cbSize
= sizeof(cf
);
1912 if ( style
.HasFont() )
1914 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1915 // but using it doesn't seem to hurt neither so leaving it for now
1917 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1918 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1920 // fill in data from LOGFONT but recalculate lfHeight because we need
1921 // the real height in twips and not the negative number which
1922 // wxFillLogFont() returns (this is correct in general and works with
1923 // the Windows font mapper, but not here)
1925 wxFillLogFont(&lf
, &style
.GetFont());
1926 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1927 cf
.bCharSet
= lf
.lfCharSet
;
1928 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1929 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1931 // also deal with underline/italic/bold attributes: note that we must
1932 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1933 // style to allow clearing it
1936 cf
.dwEffects
|= CFE_ITALIC
;
1939 if ( lf
.lfWeight
== FW_BOLD
)
1941 cf
.dwEffects
|= CFE_BOLD
;
1944 if ( lf
.lfUnderline
)
1946 cf
.dwEffects
|= CFE_UNDERLINE
;
1949 // strikeout fonts are not supported by wxWindows
1952 if ( style
.HasTextColour() )
1954 cf
.dwMask
|= CFM_COLOR
;
1955 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1959 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1961 cf
.dwMask
|= CFM_BACKCOLOR
;
1962 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1964 #endif // wxUSE_RICHEDIT2
1966 // do format the selection
1967 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1968 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1971 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1976 // restore the original selection
1977 DoSetSelection(startOld
, endOld
, FALSE
);
1984 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
1986 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
1989 // we have to do this or the style wouldn't apply for the text typed by the
1991 long posLast
= GetLastPosition();
1992 SetStyle(posLast
, posLast
, m_defaultStyle
);
1999 // ----------------------------------------------------------------------------
2001 // ----------------------------------------------------------------------------
2003 bool wxRichEditModule::OnInit()
2005 // don't do anything - we will load it when needed
2009 void wxRichEditModule::OnExit()
2011 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2013 if ( ms_hRichEdit
[i
] )
2015 ::FreeLibrary(ms_hRichEdit
[i
]);
2021 bool wxRichEditModule::Load(int version
)
2023 // we don't support loading richedit 3.0 as I don't know how to distinguish
2024 // it from 2.0 anyhow
2025 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2026 _T("incorrect richedit control version requested") );
2028 // make it the index in the array
2031 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2033 // we had already tried to load it and failed
2037 if ( ms_hRichEdit
[version
] )
2039 // we've already got this one
2043 wxString dllname
= version
? _T("riched20") : _T("riched32");
2044 dllname
+= _T(".dll");
2046 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2048 if ( !ms_hRichEdit
[version
] )
2050 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2052 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2060 #endif // wxUSE_RICHEDIT
2062 #endif // wxUSE_TEXTCTRL