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"
58 #include <sys/types.h>
62 // old mingw32 has richedit stuff directly in windows.h and doesn't have
64 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
68 #include "wx/msw/missing.h"
70 #endif // wxUSE_RICHEDIT
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
78 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
80 #endif // wxUSE_RICHEDIT
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
88 // this module initializes RichEdit DLL(s) if needed
89 class wxRichEditModule
: public wxModule
92 virtual bool OnInit();
93 virtual void OnExit();
95 // load the richedit DLL of at least of required version
96 static bool Load(int version
= 1);
99 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
100 static HINSTANCE ms_hRichEdit
[2];
102 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
105 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
107 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
109 #endif // wxUSE_RICHEDIT
111 // ----------------------------------------------------------------------------
112 // event tables and other macros
113 // ----------------------------------------------------------------------------
115 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
117 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
118 EVT_CHAR(wxTextCtrl::OnChar
)
119 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
122 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
125 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
126 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
127 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
128 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
129 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
130 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
131 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
133 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
134 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
135 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
136 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
137 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
138 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
139 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
141 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
145 // ============================================================================
147 // ============================================================================
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxTextCtrl::Init()
157 #endif // wxUSE_RICHEDIT
159 m_privateContextMenu
= NULL
;
160 m_suppressNextUpdate
= FALSE
;
163 wxTextCtrl::~wxTextCtrl()
165 if (m_privateContextMenu
)
167 delete m_privateContextMenu
;
168 m_privateContextMenu
= NULL
;
172 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
173 const wxString
& value
,
177 const wxValidator
& validator
,
178 const wxString
& name
)
180 // base initialization
181 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
185 parent
->AddChild(this);
187 // translate wxWin style flags to MSW ones
188 WXDWORD msStyle
= MSWGetCreateWindowFlags();
190 // do create the control - either an EDIT or RICHEDIT
191 wxString windowClass
= wxT("EDIT");
194 if ( m_windowStyle
& wxTE_AUTO_URL
)
196 // automatic URL detection only works in RichEdit 2.0+
197 m_windowStyle
|= wxTE_RICH2
;
200 if ( m_windowStyle
& wxTE_RICH2
)
202 // using richedit 2.0 implies using wxTE_RICH
203 m_windowStyle
|= wxTE_RICH
;
206 // we need to load the richedit DLL before creating the rich edit control
207 if ( m_windowStyle
& wxTE_RICH
)
209 static bool s_errorGiven
= FALSE
;// MT-FIXME
211 // Which version do we need? Use 1.0 by default because it is much more
212 // like the the standard EDIT or 2.0 if explicitly requested, but use
213 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
215 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
216 // (and thus EDIT) much better than RichEdit 2.0 so we probably
217 // should use 3.0 if available as it is the best of both worlds -
218 // but as I can't test it right now I don't do it (VZ)
220 const int verRichEdit
= 2;
221 #else // !wxUSE_UNICODE
222 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
223 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
225 // only give the error msg once if the DLL can't be loaded
228 // try to load the RichEdit DLL (will do nothing if already done)
229 if ( !wxRichEditModule::Load(verRichEdit
) )
232 // try another version?
233 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
235 if ( !wxRichEditModule::Load(verRichEdit
) )
236 #endif // wxUSE_UNICODE
238 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
245 // have we managed to load any richedit version?
248 m_verRichEdit
= verRichEdit
;
249 if ( m_verRichEdit
== 1 )
251 windowClass
= wxT("RICHEDIT");
255 #ifndef RICHEDIT_CLASS
256 wxString RICHEDIT_CLASS
;
257 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
259 RICHEDIT_CLASS
+= _T('W');
261 RICHEDIT_CLASS
+= _T('A');
262 #endif // Unicode/ANSI
263 #endif // !RICHEDIT_CLASS
265 windowClass
= RICHEDIT_CLASS
;
269 #endif // wxUSE_RICHEDIT
271 // we need to turn '\n's into "\r\n"s for the multiline controls
273 if ( m_windowStyle
& wxTE_MULTILINE
)
275 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
282 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
285 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
290 // enable the events we're interested in: we want to get EN_CHANGE as
291 // for the normal controls
292 LPARAM mask
= ENM_CHANGE
;
294 if ( GetRichVersion() == 1 )
296 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
297 // explained in its handler
298 mask
|= ENM_MOUSEEVENTS
;
300 else if ( m_windowStyle
& wxTE_AUTO_URL
)
304 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
307 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
309 #endif // wxUSE_RICHEDIT
314 // Make sure the window style (etc.) reflects the HWND style (roughly)
315 void wxTextCtrl::AdoptAttributesFromHWND()
317 wxWindow::AdoptAttributesFromHWND();
319 HWND hWnd
= GetHwnd();
320 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
322 // retrieve the style to see whether this is an edit or richedit ctrl
324 wxString classname
= wxGetWindowClass(GetHWND());
326 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
333 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
335 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
340 #endif // wxUSE_RICHEDIT
342 if (style
& ES_MULTILINE
)
343 m_windowStyle
|= wxTE_MULTILINE
;
344 if (style
& ES_PASSWORD
)
345 m_windowStyle
|= wxTE_PASSWORD
;
346 if (style
& ES_READONLY
)
347 m_windowStyle
|= wxTE_READONLY
;
348 if (style
& ES_WANTRETURN
)
349 m_windowStyle
|= wxTE_PROCESS_ENTER
;
350 if (style
& ES_CENTER
)
351 m_windowStyle
|= wxTE_CENTRE
;
352 if (style
& ES_RIGHT
)
353 m_windowStyle
|= wxTE_RIGHT
;
356 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
358 // default border for the text controls is the sunken one
359 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
361 style
|= wxBORDER_SUNKEN
;
364 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
366 // styles which we alaways add by default
367 if ( style
& wxTE_MULTILINE
)
369 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
370 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
372 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
373 if ( !(style
& wxTE_NO_VSCROLL
) )
375 // always adjust the vertical scrollbar automatically if we have it
376 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
379 // we have to use this style for the rich edit controls because
380 // without it the vertical scrollbar never appears at all in
381 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
382 if ( style
& wxTE_RICH2
)
384 msStyle
|= ES_DISABLENOSCROLL
;
386 #endif // wxUSE_RICHEDIT
389 style
|= wxTE_PROCESS_ENTER
;
393 // there is really no reason to not have this style for single line
395 msStyle
|= ES_AUTOHSCROLL
;
398 // styles which we add depending on the specified wxWindows styles
399 if ( style
& wxHSCROLL
)
401 // automatically scroll the control horizontally as necessary
402 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
405 if ( style
& wxTE_READONLY
)
406 msStyle
|= ES_READONLY
;
408 if ( style
& wxTE_PASSWORD
)
409 msStyle
|= ES_PASSWORD
;
411 if ( style
& wxTE_NOHIDESEL
)
412 msStyle
|= ES_NOHIDESEL
;
414 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
415 if ( style
& wxTE_CENTRE
)
416 msStyle
|= ES_CENTER
;
417 else if ( style
& wxTE_RIGHT
)
420 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
425 void wxTextCtrl::SetWindowStyleFlag(long style
)
427 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
428 style
|= wxBORDER_SUNKEN
;
431 // we have to deal with some styles separately because they can't be
432 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
433 // using richedit-specific EM_SETOPTIONS
435 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
437 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
439 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
440 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
442 #endif // wxUSE_RICHEDIT
444 wxControl::SetWindowStyleFlag(style
);
447 // ----------------------------------------------------------------------------
448 // set/get the controls text
449 // ----------------------------------------------------------------------------
451 wxString
wxTextCtrl::GetValue() const
453 // range 0..-1 is special for GetRange() and means to retrieve all text
454 return GetRange(0, -1);
457 wxString
wxTextCtrl::GetRange(long from
, long to
) const
461 if ( from
>= to
&& to
!= -1 )
463 // nothing to retrieve
470 int len
= GetWindowTextLength(GetHwnd());
473 // alloc one extra WORD as needed by the control
474 wxChar
*p
= str
.GetWriteBuf(++len
);
477 textRange
.chrg
.cpMin
= from
;
478 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
479 textRange
.lpstrText
= p
;
481 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
483 if ( m_verRichEdit
> 1 )
485 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
486 // neither Unix nor Windows style - convert it to something
490 if ( *p
== _T('\r') )
497 if ( m_verRichEdit
== 1 )
499 // convert to the canonical form - see comment below
500 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
503 //else: no text at all, leave the string empty
506 #endif // wxUSE_RICHEDIT
509 str
= wxGetWindowText(GetHWND());
511 // need only a range?
514 str
= str
.Mid(from
, to
- from
);
517 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
518 // canonical one (same one as above) for consistency with the other kinds
519 // of controls and, more importantly, with the other ports
520 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
526 void wxTextCtrl::SetValue(const wxString
& value
)
528 // if the text is long enough, it's faster to just set it instead of first
529 // comparing it with the old one (chances are that it will be different
530 // anyhow, this comparison is there to avoid flicker for small single-line
531 // edit controls mostly)
532 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
534 DoWriteText(value
, FALSE
/* not selection only */);
537 // we should reset the modified flag even if the value didn't really change
539 // mark the control as being not dirty - we changed its text, not the
543 // for compatibility, don't move the cursor when doing SetValue()
544 SetInsertionPoint(0);
547 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
549 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
553 wchar_t *wbuf
= (wchar_t *)buf
;
554 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
559 cb
-= sizeof(wchar_t);
560 (*pcb
) += sizeof(wchar_t);
563 *(const wchar_t **)dwCookie
= wpc
;
569 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
571 #if wxUSE_UNICODE_MSLU
572 bool wxTextCtrl::StreamIn(const wxString
& value
,
573 wxFontEncoding
WXUNUSED(encoding
),
576 const wchar_t *wpc
= value
.c_str();
577 #else // !wxUSE_UNICODE_MSLU
578 bool wxTextCtrl::StreamIn(const wxString
& value
,
579 wxFontEncoding encoding
,
582 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
583 // text in the non default charset -- otherwise it thinks it knows better
584 // than we do and always shows it in the default one
586 // first get the Windows code page for this encoding
587 long codepage
= wxEncodingToCodepage(encoding
);
588 if ( codepage
== -1 )
594 // next translate to Unicode using this code page
595 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
598 wxWCharBuffer
wchBuf(len
);
600 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
603 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
604 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
606 wxLogLastError(_T("MultiByteToWideChar"));
609 // finally, stream it in the control
610 const wchar_t *wpc
= wchBuf
;
611 #endif // wxUSE_UNICODE_MSLU
615 eds
.dwCookie
= (DWORD
)&wpc
;
616 // the cast below is needed for broken (very) old mingw32 headers
617 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
619 // we're going to receive 2 EN_CHANGE notifications if we got any selection
620 // (same problem as in DoWriteText())
621 if ( selectionOnly
&& HasSelection() )
623 // so suppress one of them
624 m_suppressNextUpdate
= TRUE
;
627 ::SendMessage(GetHwnd(), EM_STREAMIN
,
630 (selectionOnly
? SFF_SELECTION
: 0),
635 wxLogLastError(_T("EM_STREAMIN"));
640 #endif // !wxUSE_WCHAR_T
645 #endif // wxUSE_RICHEDIT
647 void wxTextCtrl::WriteText(const wxString
& value
)
652 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
655 if ( m_windowStyle
& wxTE_MULTILINE
)
656 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
661 // there are several complications with the rich edit controls here
665 // first, ensure that the new text will be in the default style
666 if ( !m_defaultStyle
.IsDefault() )
669 GetSelection(&start
, &end
);
670 SetStyle(start
, end
, m_defaultStyle
);
673 #if wxUSE_UNICODE_MSLU
674 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
675 // but EM_STREAMIN works
676 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
678 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
680 #endif // wxUSE_UNICODE_MSLU
683 // next check if the text we're inserting must be shown in a non
684 // default charset -- this only works for RichEdit > 1.0
685 if ( GetRichVersion() > 1 )
687 wxFont font
= m_defaultStyle
.GetFont();
693 wxFontEncoding encoding
= font
.GetEncoding();
694 if ( encoding
!= wxFONTENCODING_SYSTEM
)
696 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
700 #endif // !wxUSE_UNICODE
704 #endif // wxUSE_RICHEDIT
706 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
707 // call below which is confusing for the client code and so should be
710 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
711 // and there is a non empty selection currently and (b) rich text
712 // controls in any case
716 #endif // wxUSE_RICHEDIT
717 (selectionOnly
&& HasSelection()) )
719 m_suppressNextUpdate
= TRUE
;
722 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
723 0, (LPARAM
)valueDos
.c_str());
725 // OTOH, non rich text controls don't generate any events at all when
726 // we use WM_SETTEXT -- have to emulate them here
730 #endif // wxUSE_RICHEDIT
733 // Windows already sends an update event for single-line
735 if ( m_windowStyle
& wxTE_MULTILINE
)
743 void wxTextCtrl::AppendText(const wxString
& text
)
745 SetInsertionPointEnd();
750 if ( IsMultiLine() && GetRichVersion() > 1 )
752 // setting the caret to the end and showing it simply doesn't work for
753 // RichEdit 2.0 -- force it to still do what we want
754 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
756 #endif // wxUSE_RICHEDIT
759 void wxTextCtrl::Clear()
761 ::SetWindowText(GetHwnd(), wxT(""));
765 #endif // wxUSE_RICHEDIT
767 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
768 // but the normal ones don't -- make Clear() behaviour consistent by
769 // always sending this event
771 // Windows already sends an update event for single-line
773 if ( m_windowStyle
& wxTE_MULTILINE
)
780 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
784 size_t lenOld
= GetValue().length();
786 wxUint32 code
= event
.GetRawKeyCode();
787 ::keybd_event(code
, 0, 0 /* key press */, 0);
788 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
790 // assume that any alphanumeric key changes the total number of characters
791 // in the control - this should work in 99% of cases
792 return GetValue().length() != lenOld
;
797 // ----------------------------------------------------------------------------
798 // Clipboard operations
799 // ----------------------------------------------------------------------------
801 void wxTextCtrl::Copy()
805 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
809 void wxTextCtrl::Cut()
813 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
817 void wxTextCtrl::Paste()
821 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
825 bool wxTextCtrl::HasSelection() const
828 GetSelection(&from
, &to
);
832 bool wxTextCtrl::CanCopy() const
834 // Can copy if there's a selection
835 return HasSelection();
838 bool wxTextCtrl::CanCut() const
840 return CanCopy() && IsEditable();
843 bool wxTextCtrl::CanPaste() const
851 UINT cf
= 0; // 0 == any format
853 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
855 #endif // wxUSE_RICHEDIT
857 // Standard edit control: check for straight text on clipboard
858 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
861 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
864 return isTextAvailable
;
867 // ----------------------------------------------------------------------------
869 // ----------------------------------------------------------------------------
871 void wxTextCtrl::SetEditable(bool editable
)
873 HWND hWnd
= GetHwnd();
874 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
877 void wxTextCtrl::SetInsertionPoint(long pos
)
879 DoSetSelection(pos
, pos
);
882 void wxTextCtrl::SetInsertionPointEnd()
884 // we must not do anything if the caret is already there because calling
885 // SetInsertionPoint() thaws the controls if Freeze() had been called even
886 // if it doesn't actually move the caret anywhere and so the simple fact of
887 // doing it results in horrible flicker when appending big amounts of text
888 // to the control in a few chunks (see DoAddText() test in the text sample)
889 if ( GetInsertionPoint() == GetLastPosition() )
895 if ( m_verRichEdit
== 1 )
897 // we don't have to waste time calling GetLastPosition() in this case
900 else // !RichEdit 1.0
901 #endif // wxUSE_RICHEDIT
903 pos
= GetLastPosition();
906 SetInsertionPoint(pos
);
909 long wxTextCtrl::GetInsertionPoint() const
917 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
920 #endif // wxUSE_RICHEDIT
922 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
926 long wxTextCtrl::GetLastPosition() const
928 int numLines
= GetNumberOfLines();
929 long posStartLastLine
= XYToPosition(0, numLines
- 1);
931 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
933 return posStartLastLine
+ lenLastLine
;
936 // If the return values from and to are the same, there is no
938 void wxTextCtrl::GetSelection(long* from
, long* to
) const
944 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
946 *from
= charRange
.cpMin
;
947 *to
= charRange
.cpMax
;
950 #endif // !wxUSE_RICHEDIT
952 DWORD dwStart
, dwEnd
;
953 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
960 bool wxTextCtrl::IsEditable() const
962 // strangely enough, we may be called before the control is created: our
963 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
968 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
970 return (style
& ES_READONLY
) == 0;
973 // ----------------------------------------------------------------------------
975 // ----------------------------------------------------------------------------
977 void wxTextCtrl::SetSelection(long from
, long to
)
979 // if from and to are both -1, it means (in wxWindows) that all text should
980 // be selected - translate into Windows convention
981 if ( (from
== -1) && (to
== -1) )
987 DoSetSelection(from
, to
);
990 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
992 HWND hWnd
= GetHwnd();
1001 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
1004 #endif // wxUSE_RICHEDIT
1006 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1012 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1013 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1014 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1015 // option is set (but it does work ok in richedit 1.0 mode...)
1017 // so to make it work we either need to give focus to it here which
1018 // will probably create many problems (dummy focus events; window
1019 // containing the text control being brought to foreground
1020 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1021 // create other problems too -- and in fact it does because if we turn
1022 // on/off this style while appending the text to the control, the
1023 // vertical scrollbar never appears in it even if we append tons of
1024 // text and to work around this the only solution I found was to use
1025 // ES_DISABLENOSCROLL
1027 // this is very ugly but I don't see any other way to make this work
1028 if ( GetRichVersion() > 1 )
1030 if ( !HasFlag(wxTE_NOHIDESEL
) )
1032 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1033 ECOOP_OR
, ECO_NOHIDESEL
);
1035 //else: everything is already ok
1037 #endif // wxUSE_RICHEDIT
1039 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1042 // restore ECO_NOHIDESEL if we changed it
1043 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1045 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1046 ECOOP_AND
, ~ECO_NOHIDESEL
);
1048 #endif // wxUSE_RICHEDIT
1051 // WPARAM is 0: selection is scrolled into view
1052 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1056 // ----------------------------------------------------------------------------
1058 // ----------------------------------------------------------------------------
1060 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1062 // Set selection and remove it
1063 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1065 SendMessage(GetHwnd(), EM_REPLACESEL
,
1071 (LPARAM
)value
.c_str());
1074 void wxTextCtrl::Remove(long from
, long to
)
1076 Replace(from
, to
, _T(""));
1079 bool wxTextCtrl::LoadFile(const wxString
& file
)
1081 if ( wxTextCtrlBase::LoadFile(file
) )
1083 // update the size limit if needed
1092 bool wxTextCtrl::IsModified() const
1094 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1097 // Makes 'unmodified'
1098 void wxTextCtrl::DiscardEdits()
1100 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1103 int wxTextCtrl::GetNumberOfLines() const
1105 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1108 long wxTextCtrl::XYToPosition(long x
, long y
) const
1110 // This gets the char index for the _beginning_ of this line
1111 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1113 return charIndex
+ x
;
1116 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1118 HWND hWnd
= GetHwnd();
1120 // This gets the line number containing the character
1125 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1128 #endif // wxUSE_RICHEDIT
1130 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1139 // This gets the char index for the _beginning_ of this line
1140 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1141 if ( charIndex
== -1 )
1146 // The X position must therefore be the different between pos and charIndex
1148 *x
= pos
- charIndex
;
1155 void wxTextCtrl::ShowPosition(long pos
)
1157 HWND hWnd
= GetHwnd();
1159 // To scroll to a position, we pass the number of lines and characters
1160 // to scroll *by*. This means that we need to:
1161 // (1) Find the line position of the current line.
1162 // (2) Find the line position of pos.
1163 // (3) Scroll by (pos - current).
1164 // For now, ignore the horizontal scrolling.
1166 // Is this where scrolling is relative to - the line containing the caret?
1167 // Or is the first visible line??? Try first visible line.
1168 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1170 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1172 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1174 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1176 if (linesToScroll
!= 0)
1177 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1180 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1182 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1185 int wxTextCtrl::GetLineLength(long lineNo
) const
1187 long pos
= XYToPosition(0, lineNo
);
1189 return GetLengthOfLineContainingPos(pos
);
1192 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1194 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1196 // there must be at least enough place for the length WORD in the
1198 len
+= sizeof(WORD
);
1201 wxChar
*buf
= str
.GetWriteBuf(len
);
1203 *(WORD
*)buf
= (WORD
)len
;
1204 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1207 str
.UngetWriteBuf(len
);
1212 void wxTextCtrl::SetMaxLength(unsigned long len
)
1214 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1217 // ----------------------------------------------------------------------------
1219 // ----------------------------------------------------------------------------
1221 void wxTextCtrl::Undo()
1225 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1229 void wxTextCtrl::Redo()
1233 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1234 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1238 bool wxTextCtrl::CanUndo() const
1240 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1243 bool wxTextCtrl::CanRedo() const
1245 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1248 // ----------------------------------------------------------------------------
1249 // implemenation details
1250 // ----------------------------------------------------------------------------
1252 void wxTextCtrl::Command(wxCommandEvent
& event
)
1254 SetValue(event
.GetString());
1255 ProcessCommand (event
);
1258 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1260 // By default, load the first file into the text window.
1261 if (event
.GetNumberOfFiles() > 0)
1263 LoadFile(event
.GetFiles()[0]);
1267 // ----------------------------------------------------------------------------
1268 // kbd input processing
1269 // ----------------------------------------------------------------------------
1271 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1273 MSG
*msg
= (MSG
*)pMsg
;
1275 // check for our special keys here: if we don't do it and the parent frame
1276 // uses them as accelerators, they wouldn't work at all, so we disable
1277 // usual preprocessing for them
1278 if ( msg
->message
== WM_KEYDOWN
)
1280 WORD vkey
= (WORD
) msg
->wParam
;
1281 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1283 if ( vkey
== VK_BACK
)
1288 // we want to process some Ctrl-foo and Shift-bar but no key
1289 // combinations without either Ctrl or Shift nor with both of them
1291 const int ctrl
= wxIsCtrlDown(),
1292 shift
= wxIsShiftDown();
1293 switch ( ctrl
+ shift
)
1296 wxFAIL_MSG( _T("how many modifiers have we got?") );
1304 // either Ctrl or Shift pressed
1319 else // Shift is pressed
1321 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1328 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1331 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1333 switch ( event
.GetKeyCode() )
1336 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1338 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1339 InitCommandEvent(event
);
1340 event
.SetString(GetValue());
1341 if ( GetEventHandler()->ProcessEvent(event
) )
1344 //else: multiline controls need Enter for themselves
1349 // always produce navigation event - even if we process TAB
1350 // ourselves the fact that we got here means that the user code
1351 // decided to skip processing of this TAB - probably to let it
1352 // do its default job.
1354 wxNavigationKeyEvent eventNav
;
1355 eventNav
.SetDirection(!event
.ShiftDown());
1356 eventNav
.SetWindowChange(event
.ControlDown());
1357 eventNav
.SetEventObject(this);
1359 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1365 // no, we didn't process it
1369 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1371 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1373 if ( nMsg
== WM_GETDLGCODE
)
1375 // we always want the chars and the arrows: the arrows for navigation
1376 // and the chars because we want Ctrl-C to work even in a read only
1378 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1382 // we may have several different cases:
1383 // 1. normal case: both TAB and ENTER are used for dlg navigation
1384 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1385 // next control in the dialog
1386 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1388 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1389 // to the next control
1391 // the multiline edit control should always get <Return> for itself
1392 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1393 lDlgCode
|= DLGC_WANTMESSAGE
;
1395 if ( HasFlag(wxTE_PROCESS_TAB
) )
1396 lDlgCode
|= DLGC_WANTTAB
;
1402 // NB: use "=", not "|=" as the base class version returns the
1403 // same flags is this state as usual (i.e. including
1404 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1405 // native Win32 apps?) but for now live with it.
1413 // ----------------------------------------------------------------------------
1414 // text control event processing
1415 // ----------------------------------------------------------------------------
1417 bool wxTextCtrl::SendUpdateEvent()
1419 // is event reporting suspended?
1420 if ( m_suppressNextUpdate
)
1422 // do process the next one
1423 m_suppressNextUpdate
= FALSE
;
1428 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1429 InitCommandEvent(event
);
1430 event
.SetString(GetValue());
1432 return ProcessCommand(event
);
1435 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1442 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1445 event
.SetEventObject(this);
1446 GetEventHandler()->ProcessEvent(event
);
1455 // the text size limit has been hit -- try to increase it
1456 if ( !AdjustSpaceLimit() )
1458 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1459 InitCommandEvent(event
);
1460 event
.SetString(GetValue());
1461 ProcessCommand(event
);
1465 // the other edit notification messages are not processed
1474 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1480 WXUINT
WXUNUSED(message
),
1481 WXWPARAM
WXUNUSED(wParam
),
1482 WXLPARAM
WXUNUSED(lParam
)
1489 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1490 return (WXHBRUSH
) hbrush
;
1492 #endif // wxUSE_CTL3D
1495 if (GetParent()->GetTransparentBackground())
1496 SetBkMode(hdc
, TRANSPARENT
);
1498 SetBkMode(hdc
, OPAQUE
);
1500 wxColour colBack
= GetBackgroundColour();
1502 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1503 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1505 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1506 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1508 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1510 return (WXHBRUSH
)brush
->GetResourceHandle();
1513 // In WIN16, need to override normal erasing because
1514 // Ctl3D doesn't use the wxWindows background colour.
1516 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1518 wxColour
col(m_backgroundColour
);
1522 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1526 ::GetClientRect(GetHwnd(), &rect
);
1528 COLORREF ref
= wxColourToRGB(col
);
1529 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1531 wxLogLastError(wxT("CreateSolidBrush"));
1533 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1535 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1537 ::FillRect(hdc
, &rect
, hBrush
);
1538 ::DeleteObject(hBrush
);
1539 ::SetMapMode(hdc
, mode
);
1544 bool wxTextCtrl::AdjustSpaceLimit()
1547 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1549 // HACK: we try to automatically extend the limit for the amount of text
1550 // to allow (interactively) entering more than 64Kb of text under
1551 // Win9x but we shouldn't reset the text limit which was previously
1552 // set explicitly with SetMaxLength()
1554 // we could solve this by storing the limit we set in wxTextCtrl but
1555 // to save space we prefer to simply test here the actual limit
1556 // value: we consider that SetMaxLength() can only be called for
1558 if ( limit
< 0x8000 )
1560 // we've got more text than limit set by SetMaxLength()
1564 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1567 limit
= len
+ 0x8000; // 32Kb
1572 // as a nice side effect, this also allows passing limit > 64Kb
1573 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1576 #endif // wxUSE_RICHEDIT
1578 if ( limit
> 0xffff )
1580 // this will set it to a platform-dependent maximum (much more
1581 // than 64Kb under NT)
1585 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1590 // we changed the limit
1594 bool wxTextCtrl::AcceptsFocus() const
1596 // we don't want focus if we can't be edited unless we're a multiline
1597 // control because then it might be still nice to get focus from keyboard
1598 // to be able to scroll it without mouse
1599 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1602 wxSize
wxTextCtrl::DoGetBestSize() const
1605 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1607 int wText
= DEFAULT_ITEM_WIDTH
;
1609 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1610 if ( m_windowStyle
& wxTE_MULTILINE
)
1612 hText
*= wxMax(GetNumberOfLines(), 5);
1614 //else: for single line control everything is ok
1616 return wxSize(wText
, hText
);
1619 // ----------------------------------------------------------------------------
1620 // standard handlers for standard edit menu events
1621 // ----------------------------------------------------------------------------
1623 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1628 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1633 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1638 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1643 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1648 void wxTextCtrl::OnDelete(wxCommandEvent
& event
)
1651 GetSelection(& from
, & to
);
1652 if (from
!= -1 && to
!= -1)
1656 void wxTextCtrl::OnSelectAll(wxCommandEvent
& event
)
1658 SetSelection(-1, -1);
1661 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1663 event
.Enable( CanCut() );
1666 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1668 event
.Enable( CanCopy() );
1671 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1673 event
.Enable( CanPaste() );
1676 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1678 event
.Enable( CanUndo() );
1681 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1683 event
.Enable( CanRedo() );
1686 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1689 GetSelection(& from
, & to
);
1690 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1693 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1695 event
.Enable(GetLastPosition() > 0);
1698 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1703 if (!m_privateContextMenu
)
1705 m_privateContextMenu
= new wxMenu
;
1706 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1707 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1708 m_privateContextMenu
->AppendSeparator();
1709 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1710 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1711 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1712 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1713 m_privateContextMenu
->AppendSeparator();
1714 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1716 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1724 // the rest of the file only deals with the rich edit controls
1727 // ----------------------------------------------------------------------------
1728 // EN_LINK processing
1729 // ----------------------------------------------------------------------------
1731 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1733 NMHDR
*hdr
= (NMHDR
* )lParam
;
1734 switch ( hdr
->code
)
1738 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1739 UINT msg
= msgf
->msg
;
1741 // this is a bit crazy but richedit 1.0 sends us all mouse
1742 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1743 // generate the wxWin events for this message manually
1745 // NB: in fact, this is still not totally correct as it does
1746 // send us WM_LBUTTONUP if the selection was cleared by the
1747 // last click -- so currently we get 2 events in this case,
1748 // but as I don't see any obvious way to check for this I
1749 // leave this code in place because it's still better than
1750 // not getting left up events at all
1751 if ( msg
== WM_LBUTTONUP
)
1753 WXUINT flags
= msgf
->wParam
;
1754 int x
= GET_X_LPARAM(msgf
->lParam
),
1755 y
= GET_Y_LPARAM(msgf
->lParam
);
1757 HandleMouseEvent(msg
, x
, y
, flags
);
1761 // return TRUE to process the event (and FALSE to ignore it)
1766 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1768 switch ( enlink
->msg
)
1771 // ok, so it is hardcoded - do we really nee to
1773 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1778 case WM_LBUTTONDOWN
:
1780 case WM_LBUTTONDBLCLK
:
1781 case WM_RBUTTONDOWN
:
1783 case WM_RBUTTONDBLCLK
:
1784 // send a mouse event
1786 static const wxEventType eventsMouse
[] =
1797 // the event ids are consecutive
1799 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1801 InitMouseEvent(evtMouse
,
1802 GET_X_LPARAM(enlink
->lParam
),
1803 GET_Y_LPARAM(enlink
->lParam
),
1806 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1808 enlink
->chrg
.cpMax
);
1810 InitCommandEvent(event
);
1812 *result
= ProcessCommand(event
);
1820 // not processed, leave it to the base class
1821 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1824 // ----------------------------------------------------------------------------
1825 // colour setting for the rich edit controls
1826 // ----------------------------------------------------------------------------
1828 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1830 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1832 // colour didn't really change
1838 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1839 // EM_SETBKGNDCOLOR additionally
1840 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1846 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1848 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1850 // colour didn't really change
1856 // change the colour of everything
1859 cf
.cbSize
= sizeof(cf
);
1860 cf
.dwMask
= CFM_COLOR
;
1861 cf
.crTextColor
= wxColourToRGB(colour
);
1862 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1868 // ----------------------------------------------------------------------------
1869 // styling support for rich edit controls
1870 // ----------------------------------------------------------------------------
1874 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1878 // can't do it with normal text control
1882 // the richedit 1.0 doesn't handle setting background colour, so don't
1883 // even try to do anything if it's the only thing we want to change
1884 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1885 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1888 // nothing to do: return TRUE if there was really nothing to do and
1889 // FALSE if we failed to set bg colour
1890 return !style
.HasBackgroundColour();
1893 // order the range if needed
1901 // we can only change the format of the selection, so select the range we
1902 // want and restore the old selection later
1903 long startOld
, endOld
;
1904 GetSelection(&startOld
, &endOld
);
1906 // but do we really have to change the selection?
1907 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1911 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1914 // initialize CHARFORMAT struct
1923 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1924 // CHARFORMAT in that case
1926 if ( m_verRichEdit
== 1 )
1928 // this is the only thing the control is going to grok
1929 cf
.cbSize
= sizeof(CHARFORMAT
);
1934 // CHARFORMAT or CHARFORMAT2
1935 cf
.cbSize
= sizeof(cf
);
1938 if ( style
.HasFont() )
1940 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1941 // but using it doesn't seem to hurt neither so leaving it for now
1943 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1944 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1946 // fill in data from LOGFONT but recalculate lfHeight because we need
1947 // the real height in twips and not the negative number which
1948 // wxFillLogFont() returns (this is correct in general and works with
1949 // the Windows font mapper, but not here)
1951 wxFillLogFont(&lf
, &style
.GetFont());
1952 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1953 cf
.bCharSet
= lf
.lfCharSet
;
1954 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1955 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1957 // also deal with underline/italic/bold attributes: note that we must
1958 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1959 // style to allow clearing it
1962 cf
.dwEffects
|= CFE_ITALIC
;
1965 if ( lf
.lfWeight
== FW_BOLD
)
1967 cf
.dwEffects
|= CFE_BOLD
;
1970 if ( lf
.lfUnderline
)
1972 cf
.dwEffects
|= CFE_UNDERLINE
;
1975 // strikeout fonts are not supported by wxWindows
1978 if ( style
.HasTextColour() )
1980 cf
.dwMask
|= CFM_COLOR
;
1981 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1985 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1987 cf
.dwMask
|= CFM_BACKCOLOR
;
1988 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1990 #endif // wxUSE_RICHEDIT2
1992 // do format the selection
1993 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1994 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1997 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
2000 // now do the paragraph formatting
2003 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2004 // PARAFORMAT in that case
2006 if ( m_verRichEdit
== 1 )
2008 // this is the only thing the control is going to grok
2009 pf
.cbSize
= sizeof(PARAFORMAT
);
2014 // PARAFORMAT or PARAFORMAT2
2015 pf
.cbSize
= sizeof(pf
);
2018 if (style
.HasAlignment())
2020 pf
.dwMask
|= PFM_ALIGNMENT
;
2021 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2022 pf
.wAlignment
= PFA_RIGHT
;
2023 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2024 pf
.wAlignment
= PFA_CENTER
;
2025 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2026 pf
.wAlignment
= PFA_JUSTIFY
;
2028 pf
.wAlignment
= PFA_LEFT
;
2031 if (style
.HasLeftIndent())
2033 pf
.dwMask
|= PFM_STARTINDENT
;
2035 // Convert from 1/10 mm to TWIPS
2036 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2038 // TODO: do we need to specify dxOffset?
2041 if (style
.HasRightIndent())
2043 pf
.dwMask
|= PFM_RIGHTINDENT
;
2045 // Convert from 1/10 mm to TWIPS
2046 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2049 if (style
.HasTabs())
2051 pf
.dwMask
|= PFM_TABSTOPS
;
2053 const wxArrayInt
& tabs
= style
.GetTabs();
2055 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2057 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2059 // Convert from 1/10 mm to TWIPS
2060 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2066 // do format the selection
2067 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2068 0, (LPARAM
) &pf
) != 0;
2071 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2077 // restore the original selection
2078 DoSetSelection(startOld
, endOld
, FALSE
);
2084 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2086 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2089 // we have to do this or the style wouldn't apply for the text typed by the
2091 long posLast
= GetLastPosition();
2092 SetStyle(posLast
, posLast
, m_defaultStyle
);
2097 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2101 // can't do it with normal text control
2105 // initialize CHARFORMAT struct
2114 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2115 // CHARFORMAT in that case
2117 if ( m_verRichEdit
== 1 )
2119 // this is the only thing the control is going to grok
2120 cf
.cbSize
= sizeof(CHARFORMAT
);
2125 // CHARFORMAT or CHARFORMAT2
2126 cf
.cbSize
= sizeof(cf
);
2128 // we can only change the format of the selection, so select the range we
2129 // want and restore the old selection later
2130 long startOld
, endOld
;
2131 GetSelection(&startOld
, &endOld
);
2133 // but do we really have to change the selection?
2134 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2138 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2141 // get the selection formatting
2142 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2143 SCF_SELECTION
, (LPARAM
)&cf
) ;
2146 lf
.lfHeight
= cf
.yHeight
;
2148 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2149 lf
.lfClipPrecision
= 0;
2150 lf
.lfEscapement
= 0;
2151 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2152 if (cf
.dwEffects
& CFE_ITALIC
)
2154 lf
.lfOrientation
= 0;
2155 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2157 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2158 lf
.lfStrikeOut
= TRUE
;
2159 if (cf
.dwEffects
& CFE_UNDERLINE
)
2160 lf
.lfUnderline
= TRUE
;
2161 if (cf
.dwEffects
& CFE_BOLD
)
2162 lf
.lfWeight
= FW_BOLD
;
2164 wxFont font
= wxCreateFontFromLogFont(& lf
);
2167 style
.SetFont(font
);
2169 style
.SetTextColour(wxColour(cf
.crTextColor
));
2172 if ( m_verRichEdit
!= 1 )
2174 // cf.dwMask |= CFM_BACKCOLOR;
2175 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2177 #endif // wxUSE_RICHEDIT2
2179 // now get the paragraph formatting
2182 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2183 // PARAFORMAT in that case
2185 if ( m_verRichEdit
== 1 )
2187 // this is the only thing the control is going to grok
2188 pf
.cbSize
= sizeof(PARAFORMAT
);
2193 // PARAFORMAT or PARAFORMAT2
2194 pf
.cbSize
= sizeof(pf
);
2197 // do format the selection
2198 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2200 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2201 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2203 if (pf
.wAlignment
== PFA_CENTER
)
2204 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2205 else if (pf
.wAlignment
== PFA_RIGHT
)
2206 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2207 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2208 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2210 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2212 wxArrayInt tabStops
;
2214 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2216 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2221 // restore the original selection
2222 DoSetSelection(startOld
, endOld
, FALSE
);
2230 // ----------------------------------------------------------------------------
2232 // ----------------------------------------------------------------------------
2234 bool wxRichEditModule::OnInit()
2236 // don't do anything - we will load it when needed
2240 void wxRichEditModule::OnExit()
2242 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2244 if ( ms_hRichEdit
[i
] )
2246 ::FreeLibrary(ms_hRichEdit
[i
]);
2252 bool wxRichEditModule::Load(int version
)
2254 // we don't support loading richedit 3.0 as I don't know how to distinguish
2255 // it from 2.0 anyhow
2256 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2257 _T("incorrect richedit control version requested") );
2259 // make it the index in the array
2262 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2264 // we had already tried to load it and failed
2268 if ( ms_hRichEdit
[version
] )
2270 // we've already got this one
2274 wxString dllname
= version
? _T("riched20") : _T("riched32");
2275 dllname
+= _T(".dll");
2277 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2279 if ( !ms_hRichEdit
[version
] )
2281 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2283 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2291 #endif // wxUSE_RICHEDIT
2293 #endif // wxUSE_TEXTCTRL