1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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"
60 #include <sys/types.h>
65 // old mingw32 has richedit stuff directly in windows.h and doesn't have
67 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
71 #include "wx/msw/missing.h"
73 #endif // wxUSE_RICHEDIT
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
81 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
83 #endif // wxUSE_RICHEDIT
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
91 // this module initializes RichEdit DLL(s) if needed
92 class wxRichEditModule
: public wxModule
95 virtual bool OnInit();
96 virtual void OnExit();
98 // load the richedit DLL of at least of required version
99 static bool Load(int version
= 1);
102 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
103 static HINSTANCE ms_hRichEdit
[2];
105 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
108 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
110 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
112 #endif // wxUSE_RICHEDIT
114 // ----------------------------------------------------------------------------
115 // event tables and other macros
116 // ----------------------------------------------------------------------------
118 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
120 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
121 EVT_CHAR(wxTextCtrl::OnChar
)
122 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
125 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
128 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
129 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
130 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
131 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
132 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
133 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
134 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
136 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
137 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
138 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
139 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
140 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
141 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
142 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
144 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
148 // ============================================================================
150 // ============================================================================
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 void wxTextCtrl::Init()
160 #endif // wxUSE_RICHEDIT
162 m_privateContextMenu
= NULL
;
163 m_suppressNextUpdate
= FALSE
;
166 wxTextCtrl::~wxTextCtrl()
168 if (m_privateContextMenu
)
170 delete m_privateContextMenu
;
171 m_privateContextMenu
= NULL
;
175 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
176 const wxString
& value
,
180 const wxValidator
& validator
,
181 const wxString
& name
)
183 // base initialization
184 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
188 parent
->AddChild(this);
190 // translate wxWin style flags to MSW ones
191 WXDWORD msStyle
= MSWGetCreateWindowFlags();
193 // do create the control - either an EDIT or RICHEDIT
194 wxString windowClass
= wxT("EDIT");
197 if ( m_windowStyle
& wxTE_AUTO_URL
)
199 // automatic URL detection only works in RichEdit 2.0+
200 m_windowStyle
|= wxTE_RICH2
;
203 if ( m_windowStyle
& wxTE_RICH2
)
205 // using richedit 2.0 implies using wxTE_RICH
206 m_windowStyle
|= wxTE_RICH
;
209 // we need to load the richedit DLL before creating the rich edit control
210 if ( m_windowStyle
& wxTE_RICH
)
212 static bool s_errorGiven
= FALSE
;// MT-FIXME
214 // Which version do we need? Use 1.0 by default because it is much more
215 // like the the standard EDIT or 2.0 if explicitly requested, but use
216 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
218 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
219 // (and thus EDIT) much better than RichEdit 2.0 so we probably
220 // should use 3.0 if available as it is the best of both worlds -
221 // but as I can't test it right now I don't do it (VZ)
223 const int verRichEdit
= 2;
224 #else // !wxUSE_UNICODE
225 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
226 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
228 // only give the error msg once if the DLL can't be loaded
231 // try to load the RichEdit DLL (will do nothing if already done)
232 if ( !wxRichEditModule::Load(verRichEdit
) )
235 // try another version?
236 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
238 if ( !wxRichEditModule::Load(verRichEdit
) )
239 #endif // wxUSE_UNICODE
241 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
248 // have we managed to load any richedit version?
251 m_verRichEdit
= verRichEdit
;
252 if ( m_verRichEdit
== 1 )
254 windowClass
= wxT("RICHEDIT");
258 #ifndef RICHEDIT_CLASS
259 wxString RICHEDIT_CLASS
;
260 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
262 RICHEDIT_CLASS
+= _T('W');
264 RICHEDIT_CLASS
+= _T('A');
265 #endif // Unicode/ANSI
266 #endif // !RICHEDIT_CLASS
268 windowClass
= RICHEDIT_CLASS
;
272 #endif // wxUSE_RICHEDIT
274 // we need to turn '\n's into "\r\n"s for the multiline controls
276 if ( m_windowStyle
& wxTE_MULTILINE
)
278 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
285 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
288 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
293 // enable the events we're interested in: we want to get EN_CHANGE as
294 // for the normal controls
295 LPARAM mask
= ENM_CHANGE
;
297 if ( GetRichVersion() == 1 )
299 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
300 // explained in its handler
301 mask
|= ENM_MOUSEEVENTS
;
303 else if ( m_windowStyle
& wxTE_AUTO_URL
)
307 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
310 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
312 #endif // wxUSE_RICHEDIT
317 // Make sure the window style (etc.) reflects the HWND style (roughly)
318 void wxTextCtrl::AdoptAttributesFromHWND()
320 wxWindow::AdoptAttributesFromHWND();
322 HWND hWnd
= GetHwnd();
323 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
325 // retrieve the style to see whether this is an edit or richedit ctrl
327 wxString classname
= wxGetWindowClass(GetHWND());
329 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
336 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
338 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
343 #endif // wxUSE_RICHEDIT
345 if (style
& ES_MULTILINE
)
346 m_windowStyle
|= wxTE_MULTILINE
;
347 if (style
& ES_PASSWORD
)
348 m_windowStyle
|= wxTE_PASSWORD
;
349 if (style
& ES_READONLY
)
350 m_windowStyle
|= wxTE_READONLY
;
351 if (style
& ES_WANTRETURN
)
352 m_windowStyle
|= wxTE_PROCESS_ENTER
;
353 if (style
& ES_CENTER
)
354 m_windowStyle
|= wxTE_CENTRE
;
355 if (style
& ES_RIGHT
)
356 m_windowStyle
|= wxTE_RIGHT
;
359 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
361 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
363 // styles which we alaways add by default
364 if ( style
& wxTE_MULTILINE
)
366 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
367 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
369 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
370 if ( !(style
& wxTE_NO_VSCROLL
) )
372 // always adjust the vertical scrollbar automatically if we have it
373 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
376 // we have to use this style for the rich edit controls because
377 // without it the vertical scrollbar never appears at all in
378 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
379 if ( style
& wxTE_RICH2
)
381 msStyle
|= ES_DISABLENOSCROLL
;
383 #endif // wxUSE_RICHEDIT
386 style
|= wxTE_PROCESS_ENTER
;
390 // there is really no reason to not have this style for single line
392 msStyle
|= ES_AUTOHSCROLL
;
395 // styles which we add depending on the specified wxWindows styles
396 if ( style
& wxHSCROLL
)
398 // automatically scroll the control horizontally as necessary
399 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
402 if ( style
& wxTE_READONLY
)
403 msStyle
|= ES_READONLY
;
405 if ( style
& wxTE_PASSWORD
)
406 msStyle
|= ES_PASSWORD
;
408 if ( style
& wxTE_NOHIDESEL
)
409 msStyle
|= ES_NOHIDESEL
;
411 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
412 if ( style
& wxTE_CENTRE
)
413 msStyle
|= ES_CENTER
;
414 else if ( style
& wxTE_RIGHT
)
417 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
422 void wxTextCtrl::SetWindowStyleFlag(long style
)
425 // we have to deal with some styles separately because they can't be
426 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
427 // using richedit-specific EM_SETOPTIONS
429 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
431 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
433 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
434 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
436 #endif // wxUSE_RICHEDIT
438 wxControl::SetWindowStyleFlag(style
);
441 // ----------------------------------------------------------------------------
442 // set/get the controls text
443 // ----------------------------------------------------------------------------
445 wxString
wxTextCtrl::GetValue() const
447 // range 0..-1 is special for GetRange() and means to retrieve all text
448 return GetRange(0, -1);
451 wxString
wxTextCtrl::GetRange(long from
, long to
) const
455 if ( from
>= to
&& to
!= -1 )
457 // nothing to retrieve
464 int len
= GetWindowTextLength(GetHwnd());
467 // alloc one extra WORD as needed by the control
468 wxChar
*p
= str
.GetWriteBuf(++len
);
471 textRange
.chrg
.cpMin
= from
;
472 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
473 textRange
.lpstrText
= p
;
475 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
477 if ( m_verRichEdit
> 1 )
479 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
480 // neither Unix nor Windows style - convert it to something
484 if ( *p
== _T('\r') )
491 if ( m_verRichEdit
== 1 )
493 // convert to the canonical form - see comment below
494 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
497 //else: no text at all, leave the string empty
500 #endif // wxUSE_RICHEDIT
503 str
= wxGetWindowText(GetHWND());
505 // need only a range?
508 str
= str
.Mid(from
, to
- from
);
511 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
512 // canonical one (same one as above) for consistency with the other kinds
513 // of controls and, more importantly, with the other ports
514 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
520 void wxTextCtrl::SetValue(const wxString
& value
)
522 // if the text is long enough, it's faster to just set it instead of first
523 // comparing it with the old one (chances are that it will be different
524 // anyhow, this comparison is there to avoid flicker for small single-line
525 // edit controls mostly)
526 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
528 DoWriteText(value
, FALSE
/* not selection only */);
531 // we should reset the modified flag even if the value didn't really change
533 // mark the control as being not dirty - we changed its text, not the
537 // for compatibility, don't move the cursor when doing SetValue()
538 SetInsertionPoint(0);
541 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
543 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
547 wchar_t *wbuf
= (wchar_t *)buf
;
548 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
553 cb
-= sizeof(wchar_t);
554 (*pcb
) += sizeof(wchar_t);
557 *(const wchar_t **)dwCookie
= wpc
;
563 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
565 #if wxUSE_UNICODE_MSLU
566 bool wxTextCtrl::StreamIn(const wxString
& value
,
567 wxFontEncoding
WXUNUSED(encoding
),
570 const wchar_t *wpc
= value
.c_str();
571 #else // !wxUSE_UNICODE_MSLU
572 bool wxTextCtrl::StreamIn(const wxString
& value
,
573 wxFontEncoding encoding
,
576 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
577 // text in the non default charset -- otherwise it thinks it knows better
578 // than we do and always shows it in the default one
580 // first get the Windows code page for this encoding
581 long codepage
= wxEncodingToCodepage(encoding
);
582 if ( codepage
== -1 )
588 // next translate to Unicode using this code page
589 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
592 wxWCharBuffer
wchBuf(len
);
594 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
597 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
598 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
600 wxLogLastError(_T("MultiByteToWideChar"));
603 // finally, stream it in the control
604 const wchar_t *wpc
= wchBuf
;
605 #endif // wxUSE_UNICODE_MSLU
609 eds
.dwCookie
= (DWORD
)&wpc
;
610 // the cast below is needed for broken (very) old mingw32 headers
611 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
613 // we're going to receive 2 EN_CHANGE notifications if we got any selection
614 // (same problem as in DoWriteText())
615 if ( selectionOnly
&& HasSelection() )
617 // so suppress one of them
618 m_suppressNextUpdate
= TRUE
;
621 ::SendMessage(GetHwnd(), EM_STREAMIN
,
624 (selectionOnly
? SFF_SELECTION
: 0),
629 wxLogLastError(_T("EM_STREAMIN"));
634 #endif // !wxUSE_WCHAR_T
639 #endif // wxUSE_RICHEDIT
641 void wxTextCtrl::WriteText(const wxString
& value
)
646 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
649 if ( m_windowStyle
& wxTE_MULTILINE
)
650 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
655 // there are several complications with the rich edit controls here
659 // first, ensure that the new text will be in the default style
660 if ( !m_defaultStyle
.IsDefault() )
663 GetSelection(&start
, &end
);
664 SetStyle(start
, end
, m_defaultStyle
);
667 #if wxUSE_UNICODE_MSLU
668 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
669 // but EM_STREAMIN works
670 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
672 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
674 #endif // wxUSE_UNICODE_MSLU
677 // next check if the text we're inserting must be shown in a non
678 // default charset -- this only works for RichEdit > 1.0
679 if ( GetRichVersion() > 1 )
681 wxFont font
= m_defaultStyle
.GetFont();
687 wxFontEncoding encoding
= font
.GetEncoding();
688 if ( encoding
!= wxFONTENCODING_SYSTEM
)
690 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
694 #endif // !wxUSE_UNICODE
698 #endif // wxUSE_RICHEDIT
700 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
701 // call below which is confusing for the client code and so should be
704 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
705 // and there is a non empty selection currently and (b) rich text
706 // controls in any case
710 #endif // wxUSE_RICHEDIT
711 (selectionOnly
&& HasSelection()) )
713 m_suppressNextUpdate
= TRUE
;
716 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
717 0, (LPARAM
)valueDos
.c_str());
719 // OTOH, non rich text controls don't generate any events at all when
720 // we use WM_SETTEXT -- have to emulate them here
724 #endif // wxUSE_RICHEDIT
727 // Windows already sends an update event for single-line
729 if ( m_windowStyle
& wxTE_MULTILINE
)
737 void wxTextCtrl::AppendText(const wxString
& text
)
739 SetInsertionPointEnd();
744 if ( IsMultiLine() && GetRichVersion() > 1 )
746 // setting the caret to the end and showing it simply doesn't work for
747 // RichEdit 2.0 -- force it to still do what we want
748 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
750 #endif // wxUSE_RICHEDIT
753 void wxTextCtrl::Clear()
755 ::SetWindowText(GetHwnd(), wxEmptyString
);
759 #endif // wxUSE_RICHEDIT
761 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
762 // but the normal ones don't -- make Clear() behaviour consistent by
763 // always sending this event
765 // Windows already sends an update event for single-line
767 if ( m_windowStyle
& wxTE_MULTILINE
)
774 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
778 size_t lenOld
= GetValue().length();
780 wxUint32 code
= event
.GetRawKeyCode();
781 ::keybd_event(code
, 0, 0 /* key press */, 0);
782 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
784 // assume that any alphanumeric key changes the total number of characters
785 // in the control - this should work in 99% of cases
786 return GetValue().length() != lenOld
;
791 // ----------------------------------------------------------------------------
792 // Clipboard operations
793 // ----------------------------------------------------------------------------
795 void wxTextCtrl::Copy()
799 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
803 void wxTextCtrl::Cut()
807 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
811 void wxTextCtrl::Paste()
815 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
819 bool wxTextCtrl::HasSelection() const
822 GetSelection(&from
, &to
);
826 bool wxTextCtrl::CanCopy() const
828 // Can copy if there's a selection
829 return HasSelection();
832 bool wxTextCtrl::CanCut() const
834 return CanCopy() && IsEditable();
837 bool wxTextCtrl::CanPaste() const
845 UINT cf
= 0; // 0 == any format
847 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
849 #endif // wxUSE_RICHEDIT
851 // Standard edit control: check for straight text on clipboard
852 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
855 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
858 return isTextAvailable
;
861 // ----------------------------------------------------------------------------
863 // ----------------------------------------------------------------------------
865 void wxTextCtrl::SetEditable(bool editable
)
867 HWND hWnd
= GetHwnd();
868 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
871 void wxTextCtrl::SetInsertionPoint(long pos
)
873 DoSetSelection(pos
, pos
);
876 void wxTextCtrl::SetInsertionPointEnd()
878 // we must not do anything if the caret is already there because calling
879 // SetInsertionPoint() thaws the controls if Freeze() had been called even
880 // if it doesn't actually move the caret anywhere and so the simple fact of
881 // doing it results in horrible flicker when appending big amounts of text
882 // to the control in a few chunks (see DoAddText() test in the text sample)
883 if ( GetInsertionPoint() == GetLastPosition() )
889 if ( m_verRichEdit
== 1 )
891 // we don't have to waste time calling GetLastPosition() in this case
894 else // !RichEdit 1.0
895 #endif // wxUSE_RICHEDIT
897 pos
= GetLastPosition();
900 SetInsertionPoint(pos
);
903 long wxTextCtrl::GetInsertionPoint() const
911 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
914 #endif // wxUSE_RICHEDIT
916 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
920 long wxTextCtrl::GetLastPosition() const
922 int numLines
= GetNumberOfLines();
923 long posStartLastLine
= XYToPosition(0, numLines
- 1);
925 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
927 return posStartLastLine
+ lenLastLine
;
930 // If the return values from and to are the same, there is no
932 void wxTextCtrl::GetSelection(long* from
, long* to
) const
938 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
940 *from
= charRange
.cpMin
;
941 *to
= charRange
.cpMax
;
944 #endif // !wxUSE_RICHEDIT
946 DWORD dwStart
, dwEnd
;
947 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
954 bool wxTextCtrl::IsEditable() const
956 // strangely enough, we may be called before the control is created: our
957 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
962 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
964 return (style
& ES_READONLY
) == 0;
967 // ----------------------------------------------------------------------------
969 // ----------------------------------------------------------------------------
971 void wxTextCtrl::SetSelection(long from
, long to
)
973 // if from and to are both -1, it means (in wxWindows) that all text should
974 // be selected - translate into Windows convention
975 if ( (from
== -1) && (to
== -1) )
981 DoSetSelection(from
, to
);
984 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
986 HWND hWnd
= GetHwnd();
995 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
998 #endif // wxUSE_RICHEDIT
1000 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1006 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1007 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1008 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1009 // option is set (but it does work ok in richedit 1.0 mode...)
1011 // so to make it work we either need to give focus to it here which
1012 // will probably create many problems (dummy focus events; window
1013 // containing the text control being brought to foreground
1014 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1015 // create other problems too -- and in fact it does because if we turn
1016 // on/off this style while appending the text to the control, the
1017 // vertical scrollbar never appears in it even if we append tons of
1018 // text and to work around this the only solution I found was to use
1019 // ES_DISABLENOSCROLL
1021 // this is very ugly but I don't see any other way to make this work
1022 if ( GetRichVersion() > 1 )
1024 if ( !HasFlag(wxTE_NOHIDESEL
) )
1026 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1027 ECOOP_OR
, ECO_NOHIDESEL
);
1029 //else: everything is already ok
1031 #endif // wxUSE_RICHEDIT
1033 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1036 // restore ECO_NOHIDESEL if we changed it
1037 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1039 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1040 ECOOP_AND
, ~ECO_NOHIDESEL
);
1042 #endif // wxUSE_RICHEDIT
1045 // WPARAM is 0: selection is scrolled into view
1046 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1050 // ----------------------------------------------------------------------------
1052 // ----------------------------------------------------------------------------
1054 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1056 // Set selection and remove it
1057 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1059 SendMessage(GetHwnd(), EM_REPLACESEL
,
1065 (LPARAM
)value
.c_str());
1068 void wxTextCtrl::Remove(long from
, long to
)
1070 Replace(from
, to
, wxEmptyString
);
1073 bool wxTextCtrl::LoadFile(const wxString
& file
)
1075 if ( wxTextCtrlBase::LoadFile(file
) )
1077 // update the size limit if needed
1086 bool wxTextCtrl::IsModified() const
1088 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1091 // Makes 'unmodified'
1092 void wxTextCtrl::DiscardEdits()
1094 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1097 int wxTextCtrl::GetNumberOfLines() const
1099 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1102 long wxTextCtrl::XYToPosition(long x
, long y
) const
1104 // This gets the char index for the _beginning_ of this line
1105 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1107 return charIndex
+ x
;
1110 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1112 HWND hWnd
= GetHwnd();
1114 // This gets the line number containing the character
1119 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1122 #endif // wxUSE_RICHEDIT
1124 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1133 // This gets the char index for the _beginning_ of this line
1134 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1135 if ( charIndex
== -1 )
1140 // The X position must therefore be the different between pos and charIndex
1142 *x
= pos
- charIndex
;
1149 void wxTextCtrl::ShowPosition(long pos
)
1151 HWND hWnd
= GetHwnd();
1153 // To scroll to a position, we pass the number of lines and characters
1154 // to scroll *by*. This means that we need to:
1155 // (1) Find the line position of the current line.
1156 // (2) Find the line position of pos.
1157 // (3) Scroll by (pos - current).
1158 // For now, ignore the horizontal scrolling.
1160 // Is this where scrolling is relative to - the line containing the caret?
1161 // Or is the first visible line??? Try first visible line.
1162 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1164 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1166 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1168 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1170 if (linesToScroll
!= 0)
1171 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1174 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1176 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1179 int wxTextCtrl::GetLineLength(long lineNo
) const
1181 long pos
= XYToPosition(0, lineNo
);
1183 return GetLengthOfLineContainingPos(pos
);
1186 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1188 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1190 // there must be at least enough place for the length WORD in the
1192 len
+= sizeof(WORD
);
1195 wxChar
*buf
= str
.GetWriteBuf(len
);
1197 *(WORD
*)buf
= (WORD
)len
;
1198 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1201 str
.UngetWriteBuf(len
);
1206 void wxTextCtrl::SetMaxLength(unsigned long len
)
1208 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1211 // ----------------------------------------------------------------------------
1213 // ----------------------------------------------------------------------------
1215 void wxTextCtrl::Undo()
1219 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1223 void wxTextCtrl::Redo()
1227 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1228 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1232 bool wxTextCtrl::CanUndo() const
1234 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1237 bool wxTextCtrl::CanRedo() const
1239 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1242 // ----------------------------------------------------------------------------
1243 // implemenation details
1244 // ----------------------------------------------------------------------------
1246 void wxTextCtrl::Command(wxCommandEvent
& event
)
1248 SetValue(event
.GetString());
1249 ProcessCommand (event
);
1252 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1254 // By default, load the first file into the text window.
1255 if (event
.GetNumberOfFiles() > 0)
1257 LoadFile(event
.GetFiles()[0]);
1261 // ----------------------------------------------------------------------------
1262 // kbd input processing
1263 // ----------------------------------------------------------------------------
1265 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1267 MSG
*msg
= (MSG
*)pMsg
;
1269 // check for our special keys here: if we don't do it and the parent frame
1270 // uses them as accelerators, they wouldn't work at all, so we disable
1271 // usual preprocessing for them
1272 if ( msg
->message
== WM_KEYDOWN
)
1274 WORD vkey
= (WORD
) msg
->wParam
;
1275 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1277 if ( vkey
== VK_BACK
)
1282 // we want to process some Ctrl-foo and Shift-bar but no key
1283 // combinations without either Ctrl or Shift nor with both of them
1285 const int ctrl
= wxIsCtrlDown(),
1286 shift
= wxIsShiftDown();
1287 switch ( ctrl
+ shift
)
1290 wxFAIL_MSG( _T("how many modifiers have we got?") );
1298 // either Ctrl or Shift pressed
1313 else // Shift is pressed
1315 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1322 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1325 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1327 switch ( event
.GetKeyCode() )
1330 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1332 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1333 InitCommandEvent(event
);
1334 event
.SetString(GetValue());
1335 if ( GetEventHandler()->ProcessEvent(event
) )
1338 //else: multiline controls need Enter for themselves
1343 // always produce navigation event - even if we process TAB
1344 // ourselves the fact that we got here means that the user code
1345 // decided to skip processing of this TAB - probably to let it
1346 // do its default job.
1348 wxNavigationKeyEvent eventNav
;
1349 eventNav
.SetDirection(!event
.ShiftDown());
1350 eventNav
.SetWindowChange(event
.ControlDown());
1351 eventNav
.SetEventObject(this);
1353 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1359 // no, we didn't process it
1363 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1365 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1367 if ( nMsg
== WM_GETDLGCODE
)
1369 // we always want the chars and the arrows: the arrows for navigation
1370 // and the chars because we want Ctrl-C to work even in a read only
1372 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1376 // we may have several different cases:
1377 // 1. normal case: both TAB and ENTER are used for dlg navigation
1378 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1379 // next control in the dialog
1380 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1382 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1383 // to the next control
1385 // the multiline edit control should always get <Return> for itself
1386 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1387 lDlgCode
|= DLGC_WANTMESSAGE
;
1389 if ( HasFlag(wxTE_PROCESS_TAB
) )
1390 lDlgCode
|= DLGC_WANTTAB
;
1396 // NB: use "=", not "|=" as the base class version returns the
1397 // same flags is this state as usual (i.e. including
1398 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1399 // native Win32 apps?) but for now live with it.
1407 // ----------------------------------------------------------------------------
1408 // text control event processing
1409 // ----------------------------------------------------------------------------
1411 bool wxTextCtrl::SendUpdateEvent()
1413 // is event reporting suspended?
1414 if ( m_suppressNextUpdate
)
1416 // do process the next one
1417 m_suppressNextUpdate
= FALSE
;
1422 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1423 InitCommandEvent(event
);
1424 event
.SetString(GetValue());
1426 return ProcessCommand(event
);
1429 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1436 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1439 event
.SetEventObject(this);
1440 GetEventHandler()->ProcessEvent(event
);
1449 // the text size limit has been hit -- try to increase it
1450 if ( !AdjustSpaceLimit() )
1452 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1453 InitCommandEvent(event
);
1454 event
.SetString(GetValue());
1455 ProcessCommand(event
);
1459 // the other edit notification messages are not processed
1468 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1474 WXUINT
WXUNUSED(message
),
1475 WXWPARAM
WXUNUSED(wParam
),
1476 WXLPARAM
WXUNUSED(lParam
)
1483 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1484 return (WXHBRUSH
) hbrush
;
1486 #endif // wxUSE_CTL3D
1489 wxColour colBack
= GetBackgroundColour();
1491 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1492 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1494 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1495 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1497 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1499 return (WXHBRUSH
)brush
->GetResourceHandle();
1502 // In WIN16, need to override normal erasing because
1503 // Ctl3D doesn't use the wxWindows background colour.
1505 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1507 wxColour
col(m_backgroundColour
);
1511 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1515 ::GetClientRect(GetHwnd(), &rect
);
1517 COLORREF ref
= wxColourToRGB(col
);
1518 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1520 wxLogLastError(wxT("CreateSolidBrush"));
1522 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1524 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1526 ::FillRect(hdc
, &rect
, hBrush
);
1527 ::DeleteObject(hBrush
);
1528 ::SetMapMode(hdc
, mode
);
1533 bool wxTextCtrl::AdjustSpaceLimit()
1536 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1538 // HACK: we try to automatically extend the limit for the amount of text
1539 // to allow (interactively) entering more than 64Kb of text under
1540 // Win9x but we shouldn't reset the text limit which was previously
1541 // set explicitly with SetMaxLength()
1543 // we could solve this by storing the limit we set in wxTextCtrl but
1544 // to save space we prefer to simply test here the actual limit
1545 // value: we consider that SetMaxLength() can only be called for
1547 if ( limit
< 0x8000 )
1549 // we've got more text than limit set by SetMaxLength()
1553 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1556 limit
= len
+ 0x8000; // 32Kb
1561 // as a nice side effect, this also allows passing limit > 64Kb
1562 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1565 #endif // wxUSE_RICHEDIT
1567 if ( limit
> 0xffff )
1569 // this will set it to a platform-dependent maximum (much more
1570 // than 64Kb under NT)
1574 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1579 // we changed the limit
1583 bool wxTextCtrl::AcceptsFocus() const
1585 // we don't want focus if we can't be edited unless we're a multiline
1586 // control because then it might be still nice to get focus from keyboard
1587 // to be able to scroll it without mouse
1588 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1591 wxSize
wxTextCtrl::DoGetBestSize() const
1594 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1596 int wText
= DEFAULT_ITEM_WIDTH
;
1598 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1599 if ( m_windowStyle
& wxTE_MULTILINE
)
1601 hText
*= wxMax(GetNumberOfLines(), 5);
1603 //else: for single line control everything is ok
1605 return wxSize(wText
, hText
);
1608 // ----------------------------------------------------------------------------
1609 // standard handlers for standard edit menu events
1610 // ----------------------------------------------------------------------------
1612 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1617 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1622 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1627 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1632 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1637 void wxTextCtrl::OnDelete(wxCommandEvent
& event
)
1640 GetSelection(& from
, & to
);
1641 if (from
!= -1 && to
!= -1)
1645 void wxTextCtrl::OnSelectAll(wxCommandEvent
& event
)
1647 SetSelection(-1, -1);
1650 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1652 event
.Enable( CanCut() );
1655 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1657 event
.Enable( CanCopy() );
1660 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1662 event
.Enable( CanPaste() );
1665 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1667 event
.Enable( CanUndo() );
1670 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1672 event
.Enable( CanRedo() );
1675 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1678 GetSelection(& from
, & to
);
1679 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1682 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1684 event
.Enable(GetLastPosition() > 0);
1687 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1692 if (!m_privateContextMenu
)
1694 m_privateContextMenu
= new wxMenu
;
1695 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1696 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1697 m_privateContextMenu
->AppendSeparator();
1698 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1699 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1700 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1701 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1702 m_privateContextMenu
->AppendSeparator();
1703 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1705 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1713 // the rest of the file only deals with the rich edit controls
1716 // ----------------------------------------------------------------------------
1717 // EN_LINK processing
1718 // ----------------------------------------------------------------------------
1720 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1722 NMHDR
*hdr
= (NMHDR
* )lParam
;
1723 switch ( hdr
->code
)
1727 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1728 UINT msg
= msgf
->msg
;
1730 // this is a bit crazy but richedit 1.0 sends us all mouse
1731 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1732 // generate the wxWin events for this message manually
1734 // NB: in fact, this is still not totally correct as it does
1735 // send us WM_LBUTTONUP if the selection was cleared by the
1736 // last click -- so currently we get 2 events in this case,
1737 // but as I don't see any obvious way to check for this I
1738 // leave this code in place because it's still better than
1739 // not getting left up events at all
1740 if ( msg
== WM_LBUTTONUP
)
1742 WXUINT flags
= msgf
->wParam
;
1743 int x
= GET_X_LPARAM(msgf
->lParam
),
1744 y
= GET_Y_LPARAM(msgf
->lParam
);
1746 HandleMouseEvent(msg
, x
, y
, flags
);
1750 // return TRUE to process the event (and FALSE to ignore it)
1755 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1757 switch ( enlink
->msg
)
1760 // ok, so it is hardcoded - do we really nee to
1762 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1767 case WM_LBUTTONDOWN
:
1769 case WM_LBUTTONDBLCLK
:
1770 case WM_RBUTTONDOWN
:
1772 case WM_RBUTTONDBLCLK
:
1773 // send a mouse event
1775 static const wxEventType eventsMouse
[] =
1786 // the event ids are consecutive
1788 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1790 InitMouseEvent(evtMouse
,
1791 GET_X_LPARAM(enlink
->lParam
),
1792 GET_Y_LPARAM(enlink
->lParam
),
1795 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1797 enlink
->chrg
.cpMax
);
1799 InitCommandEvent(event
);
1801 *result
= ProcessCommand(event
);
1809 // not processed, leave it to the base class
1810 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1813 // ----------------------------------------------------------------------------
1814 // colour setting for the rich edit controls
1815 // ----------------------------------------------------------------------------
1817 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1819 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1821 // colour didn't really change
1827 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1828 // EM_SETBKGNDCOLOR additionally
1829 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1835 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1837 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1839 // colour didn't really change
1845 // change the colour of everything
1848 cf
.cbSize
= sizeof(cf
);
1849 cf
.dwMask
= CFM_COLOR
;
1850 cf
.crTextColor
= wxColourToRGB(colour
);
1851 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1857 // ----------------------------------------------------------------------------
1858 // styling support for rich edit controls
1859 // ----------------------------------------------------------------------------
1863 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1867 // can't do it with normal text control
1871 // the richedit 1.0 doesn't handle setting background colour, so don't
1872 // even try to do anything if it's the only thing we want to change
1873 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1874 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1877 // nothing to do: return TRUE if there was really nothing to do and
1878 // FALSE if we failed to set bg colour
1879 return !style
.HasBackgroundColour();
1882 // order the range if needed
1890 // we can only change the format of the selection, so select the range we
1891 // want and restore the old selection later
1892 long startOld
, endOld
;
1893 GetSelection(&startOld
, &endOld
);
1895 // but do we really have to change the selection?
1896 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1900 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1903 // initialize CHARFORMAT struct
1912 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1913 // CHARFORMAT in that case
1915 if ( m_verRichEdit
== 1 )
1917 // this is the only thing the control is going to grok
1918 cf
.cbSize
= sizeof(CHARFORMAT
);
1923 // CHARFORMAT or CHARFORMAT2
1924 cf
.cbSize
= sizeof(cf
);
1927 if ( style
.HasFont() )
1929 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1930 // but using it doesn't seem to hurt neither so leaving it for now
1932 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1933 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1935 // fill in data from LOGFONT but recalculate lfHeight because we need
1936 // the real height in twips and not the negative number which
1937 // wxFillLogFont() returns (this is correct in general and works with
1938 // the Windows font mapper, but not here)
1940 wxFillLogFont(&lf
, &style
.GetFont());
1941 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1942 cf
.bCharSet
= lf
.lfCharSet
;
1943 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1944 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1946 // also deal with underline/italic/bold attributes: note that we must
1947 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1948 // style to allow clearing it
1951 cf
.dwEffects
|= CFE_ITALIC
;
1954 if ( lf
.lfWeight
== FW_BOLD
)
1956 cf
.dwEffects
|= CFE_BOLD
;
1959 if ( lf
.lfUnderline
)
1961 cf
.dwEffects
|= CFE_UNDERLINE
;
1964 // strikeout fonts are not supported by wxWindows
1967 if ( style
.HasTextColour() )
1969 cf
.dwMask
|= CFM_COLOR
;
1970 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1974 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1976 cf
.dwMask
|= CFM_BACKCOLOR
;
1977 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1979 #endif // wxUSE_RICHEDIT2
1981 // do format the selection
1982 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1983 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1986 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1989 // now do the paragraph formatting
1992 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
1993 // PARAFORMAT in that case
1995 if ( m_verRichEdit
== 1 )
1997 // this is the only thing the control is going to grok
1998 pf
.cbSize
= sizeof(PARAFORMAT
);
2003 // PARAFORMAT or PARAFORMAT2
2004 pf
.cbSize
= sizeof(pf
);
2007 if (style
.HasAlignment())
2009 pf
.dwMask
|= PFM_ALIGNMENT
;
2010 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2011 pf
.wAlignment
= PFA_RIGHT
;
2012 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2013 pf
.wAlignment
= PFA_CENTER
;
2014 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2015 pf
.wAlignment
= PFA_JUSTIFY
;
2017 pf
.wAlignment
= PFA_LEFT
;
2020 if (style
.HasLeftIndent())
2022 pf
.dwMask
|= PFM_STARTINDENT
;
2024 // Convert from 1/10 mm to TWIPS
2025 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2027 // TODO: do we need to specify dxOffset?
2030 if (style
.HasRightIndent())
2032 pf
.dwMask
|= PFM_RIGHTINDENT
;
2034 // Convert from 1/10 mm to TWIPS
2035 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2038 if (style
.HasTabs())
2040 pf
.dwMask
|= PFM_TABSTOPS
;
2042 const wxArrayInt
& tabs
= style
.GetTabs();
2044 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2046 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2048 // Convert from 1/10 mm to TWIPS
2049 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2055 // do format the selection
2056 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2057 0, (LPARAM
) &pf
) != 0;
2060 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2066 // restore the original selection
2067 DoSetSelection(startOld
, endOld
, FALSE
);
2073 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2075 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2078 // we have to do this or the style wouldn't apply for the text typed by the
2080 long posLast
= GetLastPosition();
2081 SetStyle(posLast
, posLast
, m_defaultStyle
);
2086 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2090 // can't do it with normal text control
2094 // initialize CHARFORMAT struct
2103 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2104 // CHARFORMAT in that case
2106 if ( m_verRichEdit
== 1 )
2108 // this is the only thing the control is going to grok
2109 cf
.cbSize
= sizeof(CHARFORMAT
);
2114 // CHARFORMAT or CHARFORMAT2
2115 cf
.cbSize
= sizeof(cf
);
2117 // we can only change the format of the selection, so select the range we
2118 // want and restore the old selection later
2119 long startOld
, endOld
;
2120 GetSelection(&startOld
, &endOld
);
2122 // but do we really have to change the selection?
2123 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2127 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2130 // get the selection formatting
2131 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2132 SCF_SELECTION
, (LPARAM
)&cf
) ;
2135 lf
.lfHeight
= cf
.yHeight
;
2137 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2138 lf
.lfClipPrecision
= 0;
2139 lf
.lfEscapement
= 0;
2140 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2141 if (cf
.dwEffects
& CFE_ITALIC
)
2143 lf
.lfOrientation
= 0;
2144 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2146 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2147 lf
.lfStrikeOut
= TRUE
;
2148 if (cf
.dwEffects
& CFE_UNDERLINE
)
2149 lf
.lfUnderline
= TRUE
;
2150 if (cf
.dwEffects
& CFE_BOLD
)
2151 lf
.lfWeight
= FW_BOLD
;
2153 wxFont font
= wxCreateFontFromLogFont(& lf
);
2156 style
.SetFont(font
);
2158 style
.SetTextColour(wxColour(cf
.crTextColor
));
2161 if ( m_verRichEdit
!= 1 )
2163 // cf.dwMask |= CFM_BACKCOLOR;
2164 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2166 #endif // wxUSE_RICHEDIT2
2168 // now get the paragraph formatting
2171 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2172 // PARAFORMAT in that case
2174 if ( m_verRichEdit
== 1 )
2176 // this is the only thing the control is going to grok
2177 pf
.cbSize
= sizeof(PARAFORMAT
);
2182 // PARAFORMAT or PARAFORMAT2
2183 pf
.cbSize
= sizeof(pf
);
2186 // do format the selection
2187 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2189 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2190 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2192 if (pf
.wAlignment
== PFA_CENTER
)
2193 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2194 else if (pf
.wAlignment
== PFA_RIGHT
)
2195 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2196 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2197 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2199 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2201 wxArrayInt tabStops
;
2203 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2205 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2210 // restore the original selection
2211 DoSetSelection(startOld
, endOld
, FALSE
);
2219 // ----------------------------------------------------------------------------
2221 // ----------------------------------------------------------------------------
2223 bool wxRichEditModule::OnInit()
2225 // don't do anything - we will load it when needed
2229 void wxRichEditModule::OnExit()
2231 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2233 if ( ms_hRichEdit
[i
] )
2235 ::FreeLibrary(ms_hRichEdit
[i
]);
2241 bool wxRichEditModule::Load(int version
)
2243 // we don't support loading richedit 3.0 as I don't know how to distinguish
2244 // it from 2.0 anyhow
2245 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2246 _T("incorrect richedit control version requested") );
2248 // make it the index in the array
2251 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2253 // we had already tried to load it and failed
2257 if ( ms_hRichEdit
[version
] )
2259 // we've already got this one
2263 wxString dllname
= version
? _T("riched20") : _T("riched32");
2264 dllname
+= _T(".dll");
2266 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2268 if ( !ms_hRichEdit
[version
] )
2270 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2272 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2280 #endif // wxUSE_RICHEDIT
2282 #endif // wxUSE_TEXTCTRL