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 WX_DEFINE_FLAGS( wxTextCtrlStyle
)
122 WX_BEGIN_FLAGS( wxTextCtrlStyle
)
123 // new style border flags, we put them first to
124 // use them for streaming out
125 WX_FLAGS_MEMBER(wxBORDER_SIMPLE
)
126 WX_FLAGS_MEMBER(wxBORDER_SUNKEN
)
127 WX_FLAGS_MEMBER(wxBORDER_DOUBLE
)
128 WX_FLAGS_MEMBER(wxBORDER_RAISED
)
129 WX_FLAGS_MEMBER(wxBORDER_STATIC
)
130 WX_FLAGS_MEMBER(wxBORDER_NONE
)
132 // old style border flags
133 WX_FLAGS_MEMBER(wxSIMPLE_BORDER
)
134 WX_FLAGS_MEMBER(wxSUNKEN_BORDER
)
135 WX_FLAGS_MEMBER(wxDOUBLE_BORDER
)
136 WX_FLAGS_MEMBER(wxRAISED_BORDER
)
137 WX_FLAGS_MEMBER(wxSTATIC_BORDER
)
138 WX_FLAGS_MEMBER(wxNO_BORDER
)
140 // standard window styles
141 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL
)
142 WX_FLAGS_MEMBER(wxCLIP_CHILDREN
)
143 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
144 WX_FLAGS_MEMBER(wxWANTS_CHARS
)
145 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
146 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB
)
147 WX_FLAGS_MEMBER(wxVSCROLL
)
148 WX_FLAGS_MEMBER(wxHSCROLL
)
150 WX_FLAGS_MEMBER(wxTE_PROCESS_ENTER
)
151 WX_FLAGS_MEMBER(wxTE_PROCESS_TAB
)
152 WX_FLAGS_MEMBER(wxTE_MULTILINE
)
153 WX_FLAGS_MEMBER(wxTE_PASSWORD
)
154 WX_FLAGS_MEMBER(wxTE_READONLY
)
155 WX_FLAGS_MEMBER(wxHSCROLL
)
156 WX_FLAGS_MEMBER(wxTE_RICH
)
157 WX_FLAGS_MEMBER(wxTE_RICH2
)
158 WX_FLAGS_MEMBER(wxTE_AUTO_URL
)
159 WX_FLAGS_MEMBER(wxTE_NOHIDESEL
)
160 WX_FLAGS_MEMBER(wxTE_LEFT
)
161 WX_FLAGS_MEMBER(wxTE_CENTRE
)
162 WX_FLAGS_MEMBER(wxTE_RIGHT
)
163 WX_FLAGS_MEMBER(wxTE_DONTWRAP
)
164 WX_FLAGS_MEMBER(wxTE_LINEWRAP
)
165 WX_FLAGS_MEMBER(wxTE_WORDWRAP
)
167 WX_END_FLAGS( wxTextCtrlStyle
)
169 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl
, wxControl
,"wx/textctrl.h")
171 WX_BEGIN_PROPERTIES_TABLE(wxTextCtrl
)
172 WX_PROPERTY( Font
, wxFont
, SetFont
, GetFont
,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
173 WX_PROPERTY( Value
, wxString
, SetValue
, GetValue
, wxEmptyString
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
174 WX_PROPERTY_FLAGS( WindowStyle
, wxTextCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
175 WX_END_PROPERTIES_TABLE()
177 WX_BEGIN_HANDLERS_TABLE(wxTextCtrl
)
178 WX_END_HANDLERS_TABLE()
180 WX_CONSTRUCTOR_6( wxTextCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Value
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
182 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
186 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
187 EVT_CHAR(wxTextCtrl::OnChar
)
188 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
191 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
194 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
195 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
196 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
197 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
198 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
199 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
200 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
202 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
203 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
204 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
205 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
206 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
207 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
208 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
210 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
213 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
)
216 // ============================================================================
218 // ============================================================================
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 void wxTextCtrl::Init()
228 #endif // wxUSE_RICHEDIT
230 m_privateContextMenu
= NULL
;
231 m_suppressNextUpdate
= FALSE
;
232 m_isNativeCaretShown
= true;
235 wxTextCtrl::~wxTextCtrl()
237 if (m_privateContextMenu
)
239 delete m_privateContextMenu
;
240 m_privateContextMenu
= NULL
;
244 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
245 const wxString
& value
,
249 const wxValidator
& validator
,
250 const wxString
& name
)
252 // base initialization
253 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
257 parent
->AddChild(this);
259 // translate wxWin style flags to MSW ones
260 WXDWORD msStyle
= MSWGetCreateWindowFlags();
262 // do create the control - either an EDIT or RICHEDIT
263 wxString windowClass
= wxT("EDIT");
266 if ( m_windowStyle
& wxTE_AUTO_URL
)
268 // automatic URL detection only works in RichEdit 2.0+
269 m_windowStyle
|= wxTE_RICH2
;
272 if ( m_windowStyle
& wxTE_RICH2
)
274 // using richedit 2.0 implies using wxTE_RICH
275 m_windowStyle
|= wxTE_RICH
;
278 // we need to load the richedit DLL before creating the rich edit control
279 if ( m_windowStyle
& wxTE_RICH
)
281 static bool s_errorGiven
= FALSE
;// MT-FIXME
283 // Which version do we need? Use 1.0 by default because it is much more
284 // like the the standard EDIT or 2.0 if explicitly requested, but use
285 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
287 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
288 // (and thus EDIT) much better than RichEdit 2.0 so we probably
289 // should use 3.0 if available as it is the best of both worlds -
290 // but as I can't test it right now I don't do it (VZ)
292 const int verRichEdit
= 2;
293 #else // !wxUSE_UNICODE
294 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
295 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
297 // only give the error msg once if the DLL can't be loaded
300 // try to load the RichEdit DLL (will do nothing if already done)
301 if ( !wxRichEditModule::Load(verRichEdit
) )
304 // try another version?
305 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
307 if ( !wxRichEditModule::Load(verRichEdit
) )
308 #endif // wxUSE_UNICODE
310 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
317 // have we managed to load any richedit version?
320 m_verRichEdit
= verRichEdit
;
321 if ( m_verRichEdit
== 1 )
323 windowClass
= wxT("RICHEDIT");
327 #ifndef RICHEDIT_CLASS
328 wxString RICHEDIT_CLASS
;
329 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
331 RICHEDIT_CLASS
+= _T('W');
333 RICHEDIT_CLASS
+= _T('A');
334 #endif // Unicode/ANSI
335 #endif // !RICHEDIT_CLASS
337 windowClass
= RICHEDIT_CLASS
;
341 #endif // wxUSE_RICHEDIT
343 // we need to turn '\n's into "\r\n"s for the multiline controls
345 if ( m_windowStyle
& wxTE_MULTILINE
)
347 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
354 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
357 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
362 // enable the events we're interested in: we want to get EN_CHANGE as
363 // for the normal controls
364 LPARAM mask
= ENM_CHANGE
;
366 if ( GetRichVersion() == 1 )
368 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
369 // explained in its handler
370 mask
|= ENM_MOUSEEVENTS
;
372 else if ( m_windowStyle
& wxTE_AUTO_URL
)
376 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
379 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
381 #endif // wxUSE_RICHEDIT
386 // Make sure the window style (etc.) reflects the HWND style (roughly)
387 void wxTextCtrl::AdoptAttributesFromHWND()
389 wxWindow::AdoptAttributesFromHWND();
391 HWND hWnd
= GetHwnd();
392 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
394 // retrieve the style to see whether this is an edit or richedit ctrl
396 wxString classname
= wxGetWindowClass(GetHWND());
398 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
405 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
407 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
412 #endif // wxUSE_RICHEDIT
414 if (style
& ES_MULTILINE
)
415 m_windowStyle
|= wxTE_MULTILINE
;
416 if (style
& ES_PASSWORD
)
417 m_windowStyle
|= wxTE_PASSWORD
;
418 if (style
& ES_READONLY
)
419 m_windowStyle
|= wxTE_READONLY
;
420 if (style
& ES_WANTRETURN
)
421 m_windowStyle
|= wxTE_PROCESS_ENTER
;
422 if (style
& ES_CENTER
)
423 m_windowStyle
|= wxTE_CENTRE
;
424 if (style
& ES_RIGHT
)
425 m_windowStyle
|= wxTE_RIGHT
;
428 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
430 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
432 // styles which we alaways add by default
433 if ( style
& wxTE_MULTILINE
)
435 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
436 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
438 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
439 if ( !(style
& wxTE_NO_VSCROLL
) )
441 // always adjust the vertical scrollbar automatically if we have it
442 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
445 // we have to use this style for the rich edit controls because
446 // without it the vertical scrollbar never appears at all in
447 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
448 if ( style
& wxTE_RICH2
)
450 msStyle
|= ES_DISABLENOSCROLL
;
452 #endif // wxUSE_RICHEDIT
455 style
|= wxTE_PROCESS_ENTER
;
459 // there is really no reason to not have this style for single line
461 msStyle
|= ES_AUTOHSCROLL
;
464 // styles which we add depending on the specified wxWindows styles
465 if ( style
& wxHSCROLL
)
467 // automatically scroll the control horizontally as necessary
468 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
471 if ( style
& wxTE_READONLY
)
472 msStyle
|= ES_READONLY
;
474 if ( style
& wxTE_PASSWORD
)
475 msStyle
|= ES_PASSWORD
;
477 if ( style
& wxTE_NOHIDESEL
)
478 msStyle
|= ES_NOHIDESEL
;
480 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
481 if ( style
& wxTE_CENTRE
)
482 msStyle
|= ES_CENTER
;
483 else if ( style
& wxTE_RIGHT
)
486 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
491 void wxTextCtrl::SetWindowStyleFlag(long style
)
494 // we have to deal with some styles separately because they can't be
495 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
496 // using richedit-specific EM_SETOPTIONS
498 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
500 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
502 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
503 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
505 #endif // wxUSE_RICHEDIT
507 wxControl::SetWindowStyleFlag(style
);
510 // ----------------------------------------------------------------------------
511 // set/get the controls text
512 // ----------------------------------------------------------------------------
514 wxString
wxTextCtrl::GetValue() const
516 // range 0..-1 is special for GetRange() and means to retrieve all text
517 return GetRange(0, -1);
520 wxString
wxTextCtrl::GetRange(long from
, long to
) const
524 if ( from
>= to
&& to
!= -1 )
526 // nothing to retrieve
533 int len
= GetWindowTextLength(GetHwnd());
537 // alloc one extra WORD as needed by the control
538 wxStringBuffer
tmp(str
, ++len
);
542 textRange
.chrg
.cpMin
= from
;
543 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
544 textRange
.lpstrText
= p
;
546 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
,
547 0, (LPARAM
)&textRange
);
549 if ( m_verRichEdit
> 1 )
551 // RichEdit 2.0 uses just CR ('\r') for the
552 // newlines which is neither Unix nor Windows
553 // style - convert it to something reasonable
556 if ( *p
== _T('\r') )
562 if ( m_verRichEdit
== 1 )
564 // convert to the canonical form - see comment below
565 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
568 //else: no text at all, leave the string empty
571 #endif // wxUSE_RICHEDIT
574 str
= wxGetWindowText(GetHWND());
576 // need only a range?
579 str
= str
.Mid(from
, to
- from
);
582 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
583 // canonical one (same one as above) for consistency with the other kinds
584 // of controls and, more importantly, with the other ports
585 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
591 void wxTextCtrl::SetValue(const wxString
& value
)
593 // if the text is long enough, it's faster to just set it instead of first
594 // comparing it with the old one (chances are that it will be different
595 // anyhow, this comparison is there to avoid flicker for small single-line
596 // edit controls mostly)
597 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
599 DoWriteText(value
, FALSE
/* not selection only */);
603 // still send an event for consistency
607 // we should reset the modified flag even if the value didn't really change
609 // mark the control as being not dirty - we changed its text, not the
613 // for compatibility, don't move the cursor when doing SetValue()
614 SetInsertionPoint(0);
617 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
619 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
623 wchar_t *wbuf
= (wchar_t *)buf
;
624 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
629 cb
-= sizeof(wchar_t);
630 (*pcb
) += sizeof(wchar_t);
633 *(const wchar_t **)dwCookie
= wpc
;
639 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
641 #if wxUSE_UNICODE_MSLU
642 bool wxTextCtrl::StreamIn(const wxString
& value
,
643 wxFontEncoding
WXUNUSED(encoding
),
646 const wchar_t *wpc
= value
.c_str();
647 #else // !wxUSE_UNICODE_MSLU
648 bool wxTextCtrl::StreamIn(const wxString
& value
,
649 wxFontEncoding encoding
,
652 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
653 // text in the non default charset -- otherwise it thinks it knows better
654 // than we do and always shows it in the default one
656 // first get the Windows code page for this encoding
657 long codepage
= wxEncodingToCodepage(encoding
);
658 if ( codepage
== -1 )
664 // next translate to Unicode using this code page
665 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
668 wxWCharBuffer
wchBuf(len
);
670 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
673 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
674 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
676 wxLogLastError(_T("MultiByteToWideChar"));
679 // finally, stream it in the control
680 const wchar_t *wpc
= wchBuf
;
681 #endif // wxUSE_UNICODE_MSLU
685 eds
.dwCookie
= (DWORD
)&wpc
;
686 // the cast below is needed for broken (very) old mingw32 headers
687 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
689 // we're going to receive 2 EN_CHANGE notifications if we got any selection
690 // (same problem as in DoWriteText())
691 if ( selectionOnly
&& HasSelection() )
693 // so suppress one of them
694 m_suppressNextUpdate
= TRUE
;
697 ::SendMessage(GetHwnd(), EM_STREAMIN
,
700 (selectionOnly
? SFF_SELECTION
: 0),
705 wxLogLastError(_T("EM_STREAMIN"));
710 #endif // !wxUSE_WCHAR_T
715 #endif // wxUSE_RICHEDIT
717 void wxTextCtrl::WriteText(const wxString
& value
)
722 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
725 if ( m_windowStyle
& wxTE_MULTILINE
)
726 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
731 // there are several complications with the rich edit controls here
735 // first, ensure that the new text will be in the default style
736 if ( !m_defaultStyle
.IsDefault() )
739 GetSelection(&start
, &end
);
740 SetStyle(start
, end
, m_defaultStyle
);
743 #if wxUSE_UNICODE_MSLU
744 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
745 // but EM_STREAMIN works
746 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
748 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
750 #endif // wxUSE_UNICODE_MSLU
753 // next check if the text we're inserting must be shown in a non
754 // default charset -- this only works for RichEdit > 1.0
755 if ( GetRichVersion() > 1 )
757 wxFont font
= m_defaultStyle
.GetFont();
763 wxFontEncoding encoding
= font
.GetEncoding();
764 if ( encoding
!= wxFONTENCODING_SYSTEM
)
766 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
770 #endif // !wxUSE_UNICODE
774 #endif // wxUSE_RICHEDIT
776 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
777 // call below which is confusing for the client code and so should be
780 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
781 // and there is a non empty selection currently and (b) rich text
782 // controls in any case
786 #endif // wxUSE_RICHEDIT
787 (selectionOnly
&& HasSelection()) )
789 m_suppressNextUpdate
= TRUE
;
792 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
793 0, (LPARAM
)valueDos
.c_str());
795 // OTOH, non rich text controls don't generate any events at all when
796 // we use WM_SETTEXT -- have to emulate them here
800 #endif // wxUSE_RICHEDIT
803 // Windows already sends an update event for single-line
805 if ( m_windowStyle
& wxTE_MULTILINE
)
813 void wxTextCtrl::AppendText(const wxString
& text
)
815 SetInsertionPointEnd();
820 if ( IsMultiLine() && GetRichVersion() > 1 )
822 // setting the caret to the end and showing it simply doesn't work for
823 // RichEdit 2.0 -- force it to still do what we want
824 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
826 #endif // wxUSE_RICHEDIT
829 void wxTextCtrl::Clear()
831 ::SetWindowText(GetHwnd(), wxEmptyString
);
835 #endif // wxUSE_RICHEDIT
837 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
838 // but the normal ones don't -- make Clear() behaviour consistent by
839 // always sending this event
841 // Windows already sends an update event for single-line
843 if ( m_windowStyle
& wxTE_MULTILINE
)
850 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
854 size_t lenOld
= GetValue().length();
856 wxUint32 code
= event
.GetRawKeyCode();
857 ::keybd_event(code
, 0, 0 /* key press */, 0);
858 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
860 // assume that any alphanumeric key changes the total number of characters
861 // in the control - this should work in 99% of cases
862 return GetValue().length() != lenOld
;
867 // ----------------------------------------------------------------------------
868 // Clipboard operations
869 // ----------------------------------------------------------------------------
871 void wxTextCtrl::Copy()
875 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
879 void wxTextCtrl::Cut()
883 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
887 void wxTextCtrl::Paste()
891 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
895 bool wxTextCtrl::HasSelection() const
898 GetSelection(&from
, &to
);
902 bool wxTextCtrl::CanCopy() const
904 // Can copy if there's a selection
905 return HasSelection();
908 bool wxTextCtrl::CanCut() const
910 return CanCopy() && IsEditable();
913 bool wxTextCtrl::CanPaste() const
921 UINT cf
= 0; // 0 == any format
923 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
925 #endif // wxUSE_RICHEDIT
927 // Standard edit control: check for straight text on clipboard
928 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
931 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
934 return isTextAvailable
;
937 // ----------------------------------------------------------------------------
939 // ----------------------------------------------------------------------------
941 void wxTextCtrl::SetEditable(bool editable
)
943 HWND hWnd
= GetHwnd();
944 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
947 void wxTextCtrl::SetInsertionPoint(long pos
)
949 DoSetSelection(pos
, pos
);
952 void wxTextCtrl::SetInsertionPointEnd()
954 // we must not do anything if the caret is already there because calling
955 // SetInsertionPoint() thaws the controls if Freeze() had been called even
956 // if it doesn't actually move the caret anywhere and so the simple fact of
957 // doing it results in horrible flicker when appending big amounts of text
958 // to the control in a few chunks (see DoAddText() test in the text sample)
959 if ( GetInsertionPoint() == GetLastPosition() )
965 if ( m_verRichEdit
== 1 )
967 // we don't have to waste time calling GetLastPosition() in this case
970 else // !RichEdit 1.0
971 #endif // wxUSE_RICHEDIT
973 pos
= GetLastPosition();
976 SetInsertionPoint(pos
);
979 long wxTextCtrl::GetInsertionPoint() const
987 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
990 #endif // wxUSE_RICHEDIT
992 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
996 long wxTextCtrl::GetLastPosition() const
998 int numLines
= GetNumberOfLines();
999 long posStartLastLine
= XYToPosition(0, numLines
- 1);
1001 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
1003 return posStartLastLine
+ lenLastLine
;
1006 // If the return values from and to are the same, there is no
1008 void wxTextCtrl::GetSelection(long* from
, long* to
) const
1013 CHARRANGE charRange
;
1014 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
1016 *from
= charRange
.cpMin
;
1017 *to
= charRange
.cpMax
;
1020 #endif // !wxUSE_RICHEDIT
1022 DWORD dwStart
, dwEnd
;
1023 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
1030 bool wxTextCtrl::IsEditable() const
1032 // strangely enough, we may be called before the control is created: our
1033 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
1038 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
1040 return (style
& ES_READONLY
) == 0;
1043 // ----------------------------------------------------------------------------
1045 // ----------------------------------------------------------------------------
1047 void wxTextCtrl::SetSelection(long from
, long to
)
1049 // if from and to are both -1, it means (in wxWindows) that all text should
1050 // be selected - translate into Windows convention
1051 if ( (from
== -1) && (to
== -1) )
1057 DoSetSelection(from
, to
);
1060 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
1062 HWND hWnd
= GetHwnd();
1071 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
1074 #endif // wxUSE_RICHEDIT
1076 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1082 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1083 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1084 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1085 // option is set (but it does work ok in richedit 1.0 mode...)
1087 // so to make it work we either need to give focus to it here which
1088 // will probably create many problems (dummy focus events; window
1089 // containing the text control being brought to foreground
1090 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1091 // create other problems too -- and in fact it does because if we turn
1092 // on/off this style while appending the text to the control, the
1093 // vertical scrollbar never appears in it even if we append tons of
1094 // text and to work around this the only solution I found was to use
1095 // ES_DISABLENOSCROLL
1097 // this is very ugly but I don't see any other way to make this work
1098 if ( GetRichVersion() > 1 )
1100 if ( !HasFlag(wxTE_NOHIDESEL
) )
1102 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1103 ECOOP_OR
, ECO_NOHIDESEL
);
1105 //else: everything is already ok
1107 #endif // wxUSE_RICHEDIT
1109 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1112 // restore ECO_NOHIDESEL if we changed it
1113 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1115 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1116 ECOOP_AND
, ~ECO_NOHIDESEL
);
1118 #endif // wxUSE_RICHEDIT
1121 // WPARAM is 0: selection is scrolled into view
1122 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1126 // ----------------------------------------------------------------------------
1128 // ----------------------------------------------------------------------------
1130 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1132 // Set selection and remove it
1133 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1135 SendMessage(GetHwnd(), EM_REPLACESEL
,
1141 (LPARAM
)value
.c_str());
1144 void wxTextCtrl::Remove(long from
, long to
)
1146 Replace(from
, to
, wxEmptyString
);
1149 bool wxTextCtrl::LoadFile(const wxString
& file
)
1151 if ( wxTextCtrlBase::LoadFile(file
) )
1153 // update the size limit if needed
1162 bool wxTextCtrl::IsModified() const
1164 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1167 // Makes 'unmodified'
1168 void wxTextCtrl::DiscardEdits()
1170 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1173 int wxTextCtrl::GetNumberOfLines() const
1175 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1178 long wxTextCtrl::XYToPosition(long x
, long y
) const
1180 // This gets the char index for the _beginning_ of this line
1181 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1183 return charIndex
+ x
;
1186 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1188 HWND hWnd
= GetHwnd();
1190 // This gets the line number containing the character
1195 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1198 #endif // wxUSE_RICHEDIT
1200 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1209 // This gets the char index for the _beginning_ of this line
1210 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1211 if ( charIndex
== -1 )
1216 // The X position must therefore be the different between pos and charIndex
1218 *x
= pos
- charIndex
;
1225 void wxTextCtrl::ShowPosition(long pos
)
1227 HWND hWnd
= GetHwnd();
1229 // To scroll to a position, we pass the number of lines and characters
1230 // to scroll *by*. This means that we need to:
1231 // (1) Find the line position of the current line.
1232 // (2) Find the line position of pos.
1233 // (3) Scroll by (pos - current).
1234 // For now, ignore the horizontal scrolling.
1236 // Is this where scrolling is relative to - the line containing the caret?
1237 // Or is the first visible line??? Try first visible line.
1238 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1240 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1242 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1244 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1246 if (linesToScroll
!= 0)
1247 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1250 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1252 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1255 int wxTextCtrl::GetLineLength(long lineNo
) const
1257 long pos
= XYToPosition(0, lineNo
);
1259 return GetLengthOfLineContainingPos(pos
);
1262 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1264 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1266 // there must be at least enough place for the length WORD in the
1268 len
+= sizeof(WORD
);
1272 wxStringBufferLength
tmp(str
, len
);
1275 *(WORD
*)buf
= (WORD
)len
;
1276 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
,
1277 lineNo
, (LPARAM
)buf
);
1285 void wxTextCtrl::SetMaxLength(unsigned long len
)
1287 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1290 // ----------------------------------------------------------------------------
1292 // ----------------------------------------------------------------------------
1294 void wxTextCtrl::Undo()
1298 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1302 void wxTextCtrl::Redo()
1306 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1307 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1311 bool wxTextCtrl::CanUndo() const
1313 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1316 bool wxTextCtrl::CanRedo() const
1318 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1321 // ----------------------------------------------------------------------------
1322 // caret handling (Windows only)
1323 // ----------------------------------------------------------------------------
1325 bool wxTextCtrl::ShowNativeCaret(bool show
)
1327 if ( show
!= m_isNativeCaretShown
)
1329 if ( !(show
? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) )
1331 // not an error, may simply indicate that it's not shown/hidden
1332 // yet (i.e. it had been hidden/showh 2 times before)
1336 m_isNativeCaretShown
= show
;
1342 // ----------------------------------------------------------------------------
1343 // implemenation details
1344 // ----------------------------------------------------------------------------
1346 void wxTextCtrl::Command(wxCommandEvent
& event
)
1348 SetValue(event
.GetString());
1349 ProcessCommand (event
);
1352 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1354 // By default, load the first file into the text window.
1355 if (event
.GetNumberOfFiles() > 0)
1357 LoadFile(event
.GetFiles()[0]);
1361 // ----------------------------------------------------------------------------
1362 // kbd input processing
1363 // ----------------------------------------------------------------------------
1365 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1367 MSG
*msg
= (MSG
*)pMsg
;
1369 // check for our special keys here: if we don't do it and the parent frame
1370 // uses them as accelerators, they wouldn't work at all, so we disable
1371 // usual preprocessing for them
1372 if ( msg
->message
== WM_KEYDOWN
)
1374 WORD vkey
= (WORD
) msg
->wParam
;
1375 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1377 if ( vkey
== VK_BACK
)
1382 // we want to process some Ctrl-foo and Shift-bar but no key
1383 // combinations without either Ctrl or Shift nor with both of them
1385 const int ctrl
= wxIsCtrlDown(),
1386 shift
= wxIsShiftDown();
1387 switch ( ctrl
+ shift
)
1390 wxFAIL_MSG( _T("how many modifiers have we got?") );
1398 // either Ctrl or Shift pressed
1413 else // Shift is pressed
1415 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1422 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1425 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1427 switch ( event
.GetKeyCode() )
1430 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1432 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1433 InitCommandEvent(event
);
1434 event
.SetString(GetValue());
1435 if ( GetEventHandler()->ProcessEvent(event
) )
1438 //else: multiline controls need Enter for themselves
1443 // always produce navigation event - even if we process TAB
1444 // ourselves the fact that we got here means that the user code
1445 // decided to skip processing of this TAB - probably to let it
1446 // do its default job.
1448 wxNavigationKeyEvent eventNav
;
1449 eventNav
.SetDirection(!event
.ShiftDown());
1450 eventNav
.SetWindowChange(event
.ControlDown());
1451 eventNav
.SetEventObject(this);
1453 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1459 // no, we didn't process it
1463 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1465 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1467 if ( nMsg
== WM_GETDLGCODE
)
1469 // we always want the chars and the arrows: the arrows for navigation
1470 // and the chars because we want Ctrl-C to work even in a read only
1472 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1476 // we may have several different cases:
1477 // 1. normal case: both TAB and ENTER are used for dlg navigation
1478 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1479 // next control in the dialog
1480 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1482 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1483 // to the next control
1485 // the multiline edit control should always get <Return> for itself
1486 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1487 lDlgCode
|= DLGC_WANTMESSAGE
;
1489 if ( HasFlag(wxTE_PROCESS_TAB
) )
1490 lDlgCode
|= DLGC_WANTTAB
;
1496 // NB: use "=", not "|=" as the base class version returns the
1497 // same flags is this state as usual (i.e. including
1498 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1499 // native Win32 apps?) but for now live with it.
1507 // ----------------------------------------------------------------------------
1508 // text control event processing
1509 // ----------------------------------------------------------------------------
1511 bool wxTextCtrl::SendUpdateEvent()
1513 // is event reporting suspended?
1514 if ( m_suppressNextUpdate
)
1516 // do process the next one
1517 m_suppressNextUpdate
= FALSE
;
1522 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1523 InitCommandEvent(event
);
1524 event
.SetString(GetValue());
1526 return ProcessCommand(event
);
1529 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1536 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1539 event
.SetEventObject(this);
1540 GetEventHandler()->ProcessEvent(event
);
1549 // the text size limit has been hit -- try to increase it
1550 if ( !AdjustSpaceLimit() )
1552 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1553 InitCommandEvent(event
);
1554 event
.SetString(GetValue());
1555 ProcessCommand(event
);
1559 // the other edit notification messages are not processed
1568 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1574 WXUINT
WXUNUSED(message
),
1575 WXWPARAM
WXUNUSED(wParam
),
1576 WXLPARAM
WXUNUSED(lParam
)
1583 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1584 return (WXHBRUSH
) hbrush
;
1586 #endif // wxUSE_CTL3D
1589 wxColour colBack
= GetBackgroundColour();
1591 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1592 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1594 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1595 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1597 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1599 return (WXHBRUSH
)brush
->GetResourceHandle();
1602 // In WIN16, need to override normal erasing because
1603 // Ctl3D doesn't use the wxWindows background colour.
1605 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1607 wxColour
col(m_backgroundColour
);
1611 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1615 ::GetClientRect(GetHwnd(), &rect
);
1617 COLORREF ref
= wxColourToRGB(col
);
1618 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1620 wxLogLastError(wxT("CreateSolidBrush"));
1622 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1624 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1626 ::FillRect(hdc
, &rect
, hBrush
);
1627 ::DeleteObject(hBrush
);
1628 ::SetMapMode(hdc
, mode
);
1633 bool wxTextCtrl::AdjustSpaceLimit()
1636 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1638 // HACK: we try to automatically extend the limit for the amount of text
1639 // to allow (interactively) entering more than 64Kb of text under
1640 // Win9x but we shouldn't reset the text limit which was previously
1641 // set explicitly with SetMaxLength()
1643 // we could solve this by storing the limit we set in wxTextCtrl but
1644 // to save space we prefer to simply test here the actual limit
1645 // value: we consider that SetMaxLength() can only be called for
1647 if ( limit
< 0x8000 )
1649 // we've got more text than limit set by SetMaxLength()
1653 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1656 limit
= len
+ 0x8000; // 32Kb
1661 // as a nice side effect, this also allows passing limit > 64Kb
1662 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1665 #endif // wxUSE_RICHEDIT
1667 if ( limit
> 0xffff )
1669 // this will set it to a platform-dependent maximum (much more
1670 // than 64Kb under NT)
1674 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1679 // we changed the limit
1683 bool wxTextCtrl::AcceptsFocus() const
1685 // we don't want focus if we can't be edited unless we're a multiline
1686 // control because then it might be still nice to get focus from keyboard
1687 // to be able to scroll it without mouse
1688 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1691 wxSize
wxTextCtrl::DoGetBestSize() const
1694 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1696 int wText
= DEFAULT_ITEM_WIDTH
;
1698 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1699 if ( m_windowStyle
& wxTE_MULTILINE
)
1701 hText
*= wxMax(GetNumberOfLines(), 5);
1703 //else: for single line control everything is ok
1705 return wxSize(wText
, hText
);
1708 // ----------------------------------------------------------------------------
1709 // standard handlers for standard edit menu events
1710 // ----------------------------------------------------------------------------
1712 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1717 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1722 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1727 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1732 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1737 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1740 GetSelection(& from
, & to
);
1741 if (from
!= -1 && to
!= -1)
1745 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1747 SetSelection(-1, -1);
1750 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1752 event
.Enable( CanCut() );
1755 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1757 event
.Enable( CanCopy() );
1760 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1762 event
.Enable( CanPaste() );
1765 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1767 event
.Enable( CanUndo() );
1770 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1772 event
.Enable( CanRedo() );
1775 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1778 GetSelection(& from
, & to
);
1779 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1782 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1784 event
.Enable(GetLastPosition() > 0);
1787 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1792 if (!m_privateContextMenu
)
1794 m_privateContextMenu
= new wxMenu
;
1795 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1796 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1797 m_privateContextMenu
->AppendSeparator();
1798 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1799 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1800 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1801 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1802 m_privateContextMenu
->AppendSeparator();
1803 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1805 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1813 void wxTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
1815 // be sure the caret remains invisible if the user had hidden it
1816 if ( !m_isNativeCaretShown
)
1818 ::HideCaret(GetHwnd());
1822 // the rest of the file only deals with the rich edit controls
1825 // ----------------------------------------------------------------------------
1826 // EN_LINK processing
1827 // ----------------------------------------------------------------------------
1829 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1831 NMHDR
*hdr
= (NMHDR
* )lParam
;
1832 switch ( hdr
->code
)
1836 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1837 UINT msg
= msgf
->msg
;
1839 // this is a bit crazy but richedit 1.0 sends us all mouse
1840 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1841 // generate the wxWin events for this message manually
1843 // NB: in fact, this is still not totally correct as it does
1844 // send us WM_LBUTTONUP if the selection was cleared by the
1845 // last click -- so currently we get 2 events in this case,
1846 // but as I don't see any obvious way to check for this I
1847 // leave this code in place because it's still better than
1848 // not getting left up events at all
1849 if ( msg
== WM_LBUTTONUP
)
1851 WXUINT flags
= msgf
->wParam
;
1852 int x
= GET_X_LPARAM(msgf
->lParam
),
1853 y
= GET_Y_LPARAM(msgf
->lParam
);
1855 HandleMouseEvent(msg
, x
, y
, flags
);
1859 // return TRUE to process the event (and FALSE to ignore it)
1864 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1866 switch ( enlink
->msg
)
1869 // ok, so it is hardcoded - do we really nee to
1871 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1876 case WM_LBUTTONDOWN
:
1878 case WM_LBUTTONDBLCLK
:
1879 case WM_RBUTTONDOWN
:
1881 case WM_RBUTTONDBLCLK
:
1882 // send a mouse event
1884 static const wxEventType eventsMouse
[] =
1895 // the event ids are consecutive
1897 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1899 InitMouseEvent(evtMouse
,
1900 GET_X_LPARAM(enlink
->lParam
),
1901 GET_Y_LPARAM(enlink
->lParam
),
1904 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1906 enlink
->chrg
.cpMax
);
1908 InitCommandEvent(event
);
1910 *result
= ProcessCommand(event
);
1918 // not processed, leave it to the base class
1919 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1922 // ----------------------------------------------------------------------------
1923 // colour setting for the rich edit controls
1924 // ----------------------------------------------------------------------------
1926 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1928 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1930 // colour didn't really change
1936 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1937 // EM_SETBKGNDCOLOR additionally
1938 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1944 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1946 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1948 // colour didn't really change
1954 // change the colour of everything
1957 cf
.cbSize
= sizeof(cf
);
1958 cf
.dwMask
= CFM_COLOR
;
1959 cf
.crTextColor
= wxColourToRGB(colour
);
1960 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1966 // ----------------------------------------------------------------------------
1967 // styling support for rich edit controls
1968 // ----------------------------------------------------------------------------
1972 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1976 // can't do it with normal text control
1980 // the richedit 1.0 doesn't handle setting background colour, so don't
1981 // even try to do anything if it's the only thing we want to change
1982 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1983 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1986 // nothing to do: return TRUE if there was really nothing to do and
1987 // FALSE if we failed to set bg colour
1988 return !style
.HasBackgroundColour();
1991 // order the range if needed
1999 // we can only change the format of the selection, so select the range we
2000 // want and restore the old selection later
2001 long startOld
, endOld
;
2002 GetSelection(&startOld
, &endOld
);
2004 // but do we really have to change the selection?
2005 bool changeSel
= start
!= startOld
|| end
!= endOld
;
2009 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
2012 // initialize CHARFORMAT struct
2021 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2022 // CHARFORMAT in that case
2024 if ( m_verRichEdit
== 1 )
2026 // this is the only thing the control is going to grok
2027 cf
.cbSize
= sizeof(CHARFORMAT
);
2032 // CHARFORMAT or CHARFORMAT2
2033 cf
.cbSize
= sizeof(cf
);
2036 if ( style
.HasFont() )
2038 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
2039 // but using it doesn't seem to hurt neither so leaving it for now
2041 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
2042 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
2044 // fill in data from LOGFONT but recalculate lfHeight because we need
2045 // the real height in twips and not the negative number which
2046 // wxFillLogFont() returns (this is correct in general and works with
2047 // the Windows font mapper, but not here)
2049 wxFillLogFont(&lf
, &style
.GetFont());
2050 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
2051 cf
.bCharSet
= lf
.lfCharSet
;
2052 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
2053 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
2055 // also deal with underline/italic/bold attributes: note that we must
2056 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
2057 // style to allow clearing it
2060 cf
.dwEffects
|= CFE_ITALIC
;
2063 if ( lf
.lfWeight
== FW_BOLD
)
2065 cf
.dwEffects
|= CFE_BOLD
;
2068 if ( lf
.lfUnderline
)
2070 cf
.dwEffects
|= CFE_UNDERLINE
;
2073 // strikeout fonts are not supported by wxWindows
2076 if ( style
.HasTextColour() )
2078 cf
.dwMask
|= CFM_COLOR
;
2079 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
2083 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
2085 cf
.dwMask
|= CFM_BACKCOLOR
;
2086 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
2088 #endif // wxUSE_RICHEDIT2
2090 // do format the selection
2091 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
2092 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
2095 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
2098 // now do the paragraph formatting
2101 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2102 // PARAFORMAT in that case
2104 if ( m_verRichEdit
== 1 )
2106 // this is the only thing the control is going to grok
2107 pf
.cbSize
= sizeof(PARAFORMAT
);
2112 // PARAFORMAT or PARAFORMAT2
2113 pf
.cbSize
= sizeof(pf
);
2116 if (style
.HasAlignment())
2118 pf
.dwMask
|= PFM_ALIGNMENT
;
2119 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2120 pf
.wAlignment
= PFA_RIGHT
;
2121 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2122 pf
.wAlignment
= PFA_CENTER
;
2123 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2124 pf
.wAlignment
= PFA_JUSTIFY
;
2126 pf
.wAlignment
= PFA_LEFT
;
2129 if (style
.HasLeftIndent())
2131 pf
.dwMask
|= PFM_STARTINDENT
;
2133 // Convert from 1/10 mm to TWIPS
2134 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2136 // TODO: do we need to specify dxOffset?
2139 if (style
.HasRightIndent())
2141 pf
.dwMask
|= PFM_RIGHTINDENT
;
2143 // Convert from 1/10 mm to TWIPS
2144 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2147 if (style
.HasTabs())
2149 pf
.dwMask
|= PFM_TABSTOPS
;
2151 const wxArrayInt
& tabs
= style
.GetTabs();
2153 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2155 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2157 // Convert from 1/10 mm to TWIPS
2158 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2164 // do format the selection
2165 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2166 0, (LPARAM
) &pf
) != 0;
2169 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2175 // restore the original selection
2176 DoSetSelection(startOld
, endOld
, FALSE
);
2182 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2184 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2187 // we have to do this or the style wouldn't apply for the text typed by the
2189 long posLast
= GetLastPosition();
2190 SetStyle(posLast
, posLast
, m_defaultStyle
);
2195 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2199 // can't do it with normal text control
2203 // initialize CHARFORMAT struct
2212 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2213 // CHARFORMAT in that case
2215 if ( m_verRichEdit
== 1 )
2217 // this is the only thing the control is going to grok
2218 cf
.cbSize
= sizeof(CHARFORMAT
);
2223 // CHARFORMAT or CHARFORMAT2
2224 cf
.cbSize
= sizeof(cf
);
2226 // we can only change the format of the selection, so select the range we
2227 // want and restore the old selection later
2228 long startOld
, endOld
;
2229 GetSelection(&startOld
, &endOld
);
2231 // but do we really have to change the selection?
2232 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2236 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2239 // get the selection formatting
2240 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2241 SCF_SELECTION
, (LPARAM
)&cf
) ;
2244 lf
.lfHeight
= cf
.yHeight
;
2246 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2247 lf
.lfClipPrecision
= 0;
2248 lf
.lfEscapement
= 0;
2249 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2250 if (cf
.dwEffects
& CFE_ITALIC
)
2252 lf
.lfOrientation
= 0;
2253 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2255 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2256 lf
.lfStrikeOut
= TRUE
;
2257 if (cf
.dwEffects
& CFE_UNDERLINE
)
2258 lf
.lfUnderline
= TRUE
;
2259 if (cf
.dwEffects
& CFE_BOLD
)
2260 lf
.lfWeight
= FW_BOLD
;
2262 wxFont font
= wxCreateFontFromLogFont(& lf
);
2265 style
.SetFont(font
);
2267 style
.SetTextColour(wxColour(cf
.crTextColor
));
2270 if ( m_verRichEdit
!= 1 )
2272 // cf.dwMask |= CFM_BACKCOLOR;
2273 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2275 #endif // wxUSE_RICHEDIT2
2277 // now get the paragraph formatting
2280 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2281 // PARAFORMAT in that case
2283 if ( m_verRichEdit
== 1 )
2285 // this is the only thing the control is going to grok
2286 pf
.cbSize
= sizeof(PARAFORMAT
);
2291 // PARAFORMAT or PARAFORMAT2
2292 pf
.cbSize
= sizeof(pf
);
2295 // do format the selection
2296 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2298 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2299 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2301 if (pf
.wAlignment
== PFA_CENTER
)
2302 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2303 else if (pf
.wAlignment
== PFA_RIGHT
)
2304 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2305 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2306 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2308 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2310 wxArrayInt tabStops
;
2312 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2314 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2319 // restore the original selection
2320 DoSetSelection(startOld
, endOld
, FALSE
);
2328 // ----------------------------------------------------------------------------
2330 // ----------------------------------------------------------------------------
2332 bool wxRichEditModule::OnInit()
2334 // don't do anything - we will load it when needed
2338 void wxRichEditModule::OnExit()
2340 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2342 if ( ms_hRichEdit
[i
] )
2344 ::FreeLibrary(ms_hRichEdit
[i
]);
2345 ms_hRichEdit
[i
] = NULL
;
2351 bool wxRichEditModule::Load(int version
)
2353 // we don't support loading richedit 3.0 as I don't know how to distinguish
2354 // it from 2.0 anyhow
2355 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2356 _T("incorrect richedit control version requested") );
2358 // make it the index in the array
2361 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2363 // we had already tried to load it and failed
2367 if ( ms_hRichEdit
[version
] )
2369 // we've already got this one
2373 wxString dllname
= version
? _T("riched20") : _T("riched32");
2374 dllname
+= _T(".dll");
2376 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2378 if ( !ms_hRichEdit
[version
] )
2380 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2382 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2390 #endif // wxUSE_RICHEDIT
2392 #endif // wxUSE_TEXTCTRL