1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "textctrl.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
44 #include "wx/module.h"
47 #include "wx/clipbrd.h"
50 #include "wx/textfile.h"
54 #include "wx/msw/private.h"
55 #include "wx/msw/winundef.h"
61 #include <sys/types.h>
66 // old mingw32 has richedit stuff directly in windows.h and doesn't have
68 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
72 #include "wx/msw/missing.h"
74 #endif // wxUSE_RICHEDIT
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
82 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
84 #endif // wxUSE_RICHEDIT
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
92 // this module initializes RichEdit DLL(s) if needed
93 class wxRichEditModule
: public wxModule
96 virtual bool OnInit();
97 virtual void OnExit();
99 // load the richedit DLL of at least of required version
100 static bool Load(int version
= 1);
103 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
104 static HINSTANCE ms_hRichEdit
[2];
106 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
109 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
111 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
113 #endif // wxUSE_RICHEDIT
115 // ----------------------------------------------------------------------------
116 // event tables and other macros
117 // ----------------------------------------------------------------------------
119 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
125 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
126 EVT_CHAR(wxTextCtrl::OnChar
)
127 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
130 EVT_RIGHT_UP(wxTextCtrl::OnRightClick
)
133 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
134 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
135 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
136 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
137 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
138 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
139 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
141 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
142 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
143 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
144 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
145 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
146 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
147 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
149 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
152 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
)
155 // ============================================================================
157 // ============================================================================
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxTextCtrl::Init()
167 #endif // wxUSE_RICHEDIT
169 m_privateContextMenu
= NULL
;
170 m_suppressNextUpdate
= FALSE
;
171 m_isNativeCaretShown
= true;
174 wxTextCtrl::~wxTextCtrl()
176 if (m_privateContextMenu
)
178 delete m_privateContextMenu
;
179 m_privateContextMenu
= NULL
;
183 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
184 const wxString
& value
,
188 const wxValidator
& validator
,
189 const wxString
& name
)
191 // base initialization
192 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
196 parent
->AddChild(this);
198 // translate wxWin style flags to MSW ones
199 WXDWORD msStyle
= MSWGetCreateWindowFlags();
201 // do create the control - either an EDIT or RICHEDIT
202 wxString windowClass
= wxT("EDIT");
205 if ( m_windowStyle
& wxTE_AUTO_URL
)
207 // automatic URL detection only works in RichEdit 2.0+
208 m_windowStyle
|= wxTE_RICH2
;
211 if ( m_windowStyle
& wxTE_RICH2
)
213 // using richedit 2.0 implies using wxTE_RICH
214 m_windowStyle
|= wxTE_RICH
;
217 // we need to load the richedit DLL before creating the rich edit control
218 if ( m_windowStyle
& wxTE_RICH
)
220 static bool s_errorGiven
= FALSE
;// MT-FIXME
222 // Which version do we need? Use 1.0 by default because it is much more
223 // like the the standard EDIT or 2.0 if explicitly requested, but use
224 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
226 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
227 // (and thus EDIT) much better than RichEdit 2.0 so we probably
228 // should use 3.0 if available as it is the best of both worlds -
229 // but as I can't test it right now I don't do it (VZ)
231 const int verRichEdit
= 2;
232 #else // !wxUSE_UNICODE
233 int verRichEdit
= m_windowStyle
& wxTE_RICH2
? 2 : 1;
234 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
236 // only give the error msg once if the DLL can't be loaded
239 // try to load the RichEdit DLL (will do nothing if already done)
240 if ( !wxRichEditModule::Load(verRichEdit
) )
243 // try another version?
244 verRichEdit
= 3 - verRichEdit
; // 1 <-> 2
246 if ( !wxRichEditModule::Load(verRichEdit
) )
247 #endif // wxUSE_UNICODE
249 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
256 // have we managed to load any richedit version?
259 m_verRichEdit
= verRichEdit
;
260 if ( m_verRichEdit
== 1 )
262 windowClass
= wxT("RICHEDIT");
266 #ifndef RICHEDIT_CLASS
267 wxString RICHEDIT_CLASS
;
268 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
270 RICHEDIT_CLASS
+= _T('W');
272 RICHEDIT_CLASS
+= _T('A');
273 #endif // Unicode/ANSI
274 #endif // !RICHEDIT_CLASS
276 windowClass
= RICHEDIT_CLASS
;
280 #endif // wxUSE_RICHEDIT
282 // we need to turn '\n's into "\r\n"s for the multiline controls
284 if ( m_windowStyle
& wxTE_MULTILINE
)
286 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
293 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
296 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
301 // enable the events we're interested in: we want to get EN_CHANGE as
302 // for the normal controls
303 LPARAM mask
= ENM_CHANGE
;
305 if ( GetRichVersion() == 1 )
307 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
308 // explained in its handler
309 mask
|= ENM_MOUSEEVENTS
;
311 else if ( m_windowStyle
& wxTE_AUTO_URL
)
315 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
318 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
320 #endif // wxUSE_RICHEDIT
325 // Make sure the window style (etc.) reflects the HWND style (roughly)
326 void wxTextCtrl::AdoptAttributesFromHWND()
328 wxWindow::AdoptAttributesFromHWND();
330 HWND hWnd
= GetHwnd();
331 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
333 // retrieve the style to see whether this is an edit or richedit ctrl
335 wxString classname
= wxGetWindowClass(GetHWND());
337 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
344 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
346 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
351 #endif // wxUSE_RICHEDIT
353 if (style
& ES_MULTILINE
)
354 m_windowStyle
|= wxTE_MULTILINE
;
355 if (style
& ES_PASSWORD
)
356 m_windowStyle
|= wxTE_PASSWORD
;
357 if (style
& ES_READONLY
)
358 m_windowStyle
|= wxTE_READONLY
;
359 if (style
& ES_WANTRETURN
)
360 m_windowStyle
|= wxTE_PROCESS_ENTER
;
361 if (style
& ES_CENTER
)
362 m_windowStyle
|= wxTE_CENTRE
;
363 if (style
& ES_RIGHT
)
364 m_windowStyle
|= wxTE_RIGHT
;
367 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
369 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
371 // styles which we alaways add by default
372 if ( style
& wxTE_MULTILINE
)
374 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
375 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
377 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
378 if ( !(style
& wxTE_NO_VSCROLL
) )
380 // always adjust the vertical scrollbar automatically if we have it
381 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
384 // we have to use this style for the rich edit controls because
385 // without it the vertical scrollbar never appears at all in
386 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
387 if ( style
& wxTE_RICH2
)
389 msStyle
|= ES_DISABLENOSCROLL
;
391 #endif // wxUSE_RICHEDIT
394 style
|= wxTE_PROCESS_ENTER
;
398 // there is really no reason to not have this style for single line
400 msStyle
|= ES_AUTOHSCROLL
;
403 // styles which we add depending on the specified wxWindows styles
404 if ( style
& wxHSCROLL
)
406 // automatically scroll the control horizontally as necessary
407 msStyle
|= WS_HSCROLL
;// | ES_AUTOHSCROLL;
410 if ( style
& wxTE_READONLY
)
411 msStyle
|= ES_READONLY
;
413 if ( style
& wxTE_PASSWORD
)
414 msStyle
|= ES_PASSWORD
;
416 if ( style
& wxTE_NOHIDESEL
)
417 msStyle
|= ES_NOHIDESEL
;
419 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
420 if ( style
& wxTE_CENTRE
)
421 msStyle
|= ES_CENTER
;
422 else if ( style
& wxTE_RIGHT
)
425 msStyle
|= ES_LEFT
; // ES_LEFT if 0 as well but for consistency...
430 void wxTextCtrl::SetWindowStyleFlag(long style
)
433 // we have to deal with some styles separately because they can't be
434 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
435 // using richedit-specific EM_SETOPTIONS
437 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
439 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
441 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
442 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
444 #endif // wxUSE_RICHEDIT
446 wxControl::SetWindowStyleFlag(style
);
449 // ----------------------------------------------------------------------------
450 // set/get the controls text
451 // ----------------------------------------------------------------------------
453 wxString
wxTextCtrl::GetValue() const
455 // range 0..-1 is special for GetRange() and means to retrieve all text
456 return GetRange(0, -1);
459 wxString
wxTextCtrl::GetRange(long from
, long to
) const
463 if ( from
>= to
&& to
!= -1 )
465 // nothing to retrieve
472 int len
= GetWindowTextLength(GetHwnd());
476 // alloc one extra WORD as needed by the control
477 wxStringBuffer
tmp(str
, ++len
);
481 textRange
.chrg
.cpMin
= from
;
482 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
483 textRange
.lpstrText
= p
;
485 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
,
486 0, (LPARAM
)&textRange
);
488 if ( m_verRichEdit
> 1 )
490 // RichEdit 2.0 uses just CR ('\r') for the
491 // newlines which is neither Unix nor Windows
492 // style - convert it to something reasonable
495 if ( *p
== _T('\r') )
501 if ( m_verRichEdit
== 1 )
503 // convert to the canonical form - see comment below
504 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
507 //else: no text at all, leave the string empty
510 #endif // wxUSE_RICHEDIT
513 str
= wxGetWindowText(GetHWND());
515 // need only a range?
518 str
= str
.Mid(from
, to
- from
);
521 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
522 // canonical one (same one as above) for consistency with the other kinds
523 // of controls and, more importantly, with the other ports
524 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
530 void wxTextCtrl::SetValue(const wxString
& value
)
532 // if the text is long enough, it's faster to just set it instead of first
533 // comparing it with the old one (chances are that it will be different
534 // anyhow, this comparison is there to avoid flicker for small single-line
535 // edit controls mostly)
536 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
538 DoWriteText(value
, FALSE
/* not selection only */);
542 // still send an event for consistency
546 // we should reset the modified flag even if the value didn't really change
548 // mark the control as being not dirty - we changed its text, not the
552 // for compatibility, don't move the cursor when doing SetValue()
553 SetInsertionPoint(0);
556 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
558 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
562 wchar_t *wbuf
= (wchar_t *)buf
;
563 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
568 cb
-= sizeof(wchar_t);
569 (*pcb
) += sizeof(wchar_t);
572 *(const wchar_t **)dwCookie
= wpc
;
578 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
580 #if wxUSE_UNICODE_MSLU
581 bool wxTextCtrl::StreamIn(const wxString
& value
,
582 wxFontEncoding
WXUNUSED(encoding
),
585 const wchar_t *wpc
= value
.c_str();
586 #else // !wxUSE_UNICODE_MSLU
587 bool wxTextCtrl::StreamIn(const wxString
& value
,
588 wxFontEncoding encoding
,
591 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
592 // text in the non default charset -- otherwise it thinks it knows better
593 // than we do and always shows it in the default one
595 // first get the Windows code page for this encoding
596 long codepage
= wxEncodingToCodepage(encoding
);
597 if ( codepage
== -1 )
603 // next translate to Unicode using this code page
604 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
607 wxWCharBuffer
wchBuf(len
);
609 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
612 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
613 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
615 wxLogLastError(_T("MultiByteToWideChar"));
618 // finally, stream it in the control
619 const wchar_t *wpc
= wchBuf
;
620 #endif // wxUSE_UNICODE_MSLU
624 eds
.dwCookie
= (DWORD
)&wpc
;
625 // the cast below is needed for broken (very) old mingw32 headers
626 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
628 // we're going to receive 2 EN_CHANGE notifications if we got any selection
629 // (same problem as in DoWriteText())
630 if ( selectionOnly
&& HasSelection() )
632 // so suppress one of them
633 m_suppressNextUpdate
= TRUE
;
636 ::SendMessage(GetHwnd(), EM_STREAMIN
,
639 (selectionOnly
? SFF_SELECTION
: 0),
644 wxLogLastError(_T("EM_STREAMIN"));
649 #endif // !wxUSE_WCHAR_T
654 #endif // wxUSE_RICHEDIT
656 void wxTextCtrl::WriteText(const wxString
& value
)
661 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
664 if ( m_windowStyle
& wxTE_MULTILINE
)
665 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
670 // there are several complications with the rich edit controls here
674 // first, ensure that the new text will be in the default style
675 if ( !m_defaultStyle
.IsDefault() )
678 GetSelection(&start
, &end
);
679 SetStyle(start
, end
, m_defaultStyle
);
682 #if wxUSE_UNICODE_MSLU
683 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
684 // but EM_STREAMIN works
685 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
687 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
689 #endif // wxUSE_UNICODE_MSLU
692 // next check if the text we're inserting must be shown in a non
693 // default charset -- this only works for RichEdit > 1.0
694 if ( GetRichVersion() > 1 )
696 wxFont font
= m_defaultStyle
.GetFont();
702 wxFontEncoding encoding
= font
.GetEncoding();
703 if ( encoding
!= wxFONTENCODING_SYSTEM
)
705 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
709 #endif // !wxUSE_UNICODE
713 #endif // wxUSE_RICHEDIT
715 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
716 // call below which is confusing for the client code and so should be
719 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
720 // and there is a non empty selection currently and (b) rich text
721 // controls in any case
725 #endif // wxUSE_RICHEDIT
726 (selectionOnly
&& HasSelection()) )
728 m_suppressNextUpdate
= TRUE
;
731 ::SendMessage(GetHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
732 0, (LPARAM
)valueDos
.c_str());
734 // OTOH, non rich text controls don't generate any events at all when
735 // we use WM_SETTEXT -- have to emulate them here
739 #endif // wxUSE_RICHEDIT
742 // Windows already sends an update event for single-line
744 if ( m_windowStyle
& wxTE_MULTILINE
)
752 void wxTextCtrl::AppendText(const wxString
& text
)
754 SetInsertionPointEnd();
759 if ( IsMultiLine() && GetRichVersion() > 1 )
761 // setting the caret to the end and showing it simply doesn't work for
762 // RichEdit 2.0 -- force it to still do what we want
763 ::SendMessage(GetHwnd(), EM_LINESCROLL
, 0, GetNumberOfLines());
765 #endif // wxUSE_RICHEDIT
768 void wxTextCtrl::Clear()
770 ::SetWindowText(GetHwnd(), wxEmptyString
);
774 #endif // wxUSE_RICHEDIT
776 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
777 // but the normal ones don't -- make Clear() behaviour consistent by
778 // always sending this event
780 // Windows already sends an update event for single-line
782 if ( m_windowStyle
& wxTE_MULTILINE
)
789 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
)
793 size_t lenOld
= GetValue().length();
795 wxUint32 code
= event
.GetRawKeyCode();
796 ::keybd_event(code
, 0, 0 /* key press */, 0);
797 ::keybd_event(code
, 0, KEYEVENTF_KEYUP
, 0);
799 // assume that any alphanumeric key changes the total number of characters
800 // in the control - this should work in 99% of cases
801 return GetValue().length() != lenOld
;
806 // ----------------------------------------------------------------------------
807 // Clipboard operations
808 // ----------------------------------------------------------------------------
810 void wxTextCtrl::Copy()
814 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
818 void wxTextCtrl::Cut()
822 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
826 void wxTextCtrl::Paste()
830 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
834 bool wxTextCtrl::HasSelection() const
837 GetSelection(&from
, &to
);
841 bool wxTextCtrl::CanCopy() const
843 // Can copy if there's a selection
844 return HasSelection();
847 bool wxTextCtrl::CanCut() const
849 return CanCopy() && IsEditable();
852 bool wxTextCtrl::CanPaste() const
860 UINT cf
= 0; // 0 == any format
862 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
864 #endif // wxUSE_RICHEDIT
866 // Standard edit control: check for straight text on clipboard
867 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
870 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
873 return isTextAvailable
;
876 // ----------------------------------------------------------------------------
878 // ----------------------------------------------------------------------------
880 void wxTextCtrl::SetEditable(bool editable
)
882 HWND hWnd
= GetHwnd();
883 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
886 void wxTextCtrl::SetInsertionPoint(long pos
)
888 DoSetSelection(pos
, pos
);
891 void wxTextCtrl::SetInsertionPointEnd()
893 // we must not do anything if the caret is already there because calling
894 // SetInsertionPoint() thaws the controls if Freeze() had been called even
895 // if it doesn't actually move the caret anywhere and so the simple fact of
896 // doing it results in horrible flicker when appending big amounts of text
897 // to the control in a few chunks (see DoAddText() test in the text sample)
898 if ( GetInsertionPoint() == GetLastPosition() )
904 if ( m_verRichEdit
== 1 )
906 // we don't have to waste time calling GetLastPosition() in this case
909 else // !RichEdit 1.0
910 #endif // wxUSE_RICHEDIT
912 pos
= GetLastPosition();
915 SetInsertionPoint(pos
);
918 long wxTextCtrl::GetInsertionPoint() const
926 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
929 #endif // wxUSE_RICHEDIT
931 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
935 long wxTextCtrl::GetLastPosition() const
937 int numLines
= GetNumberOfLines();
938 long posStartLastLine
= XYToPosition(0, numLines
- 1);
940 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
942 return posStartLastLine
+ lenLastLine
;
945 // If the return values from and to are the same, there is no
947 void wxTextCtrl::GetSelection(long* from
, long* to
) const
953 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
955 *from
= charRange
.cpMin
;
956 *to
= charRange
.cpMax
;
959 #endif // !wxUSE_RICHEDIT
961 DWORD dwStart
, dwEnd
;
962 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
969 bool wxTextCtrl::IsEditable() const
971 // strangely enough, we may be called before the control is created: our
972 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
977 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
979 return (style
& ES_READONLY
) == 0;
982 // ----------------------------------------------------------------------------
984 // ----------------------------------------------------------------------------
986 void wxTextCtrl::SetSelection(long from
, long to
)
988 // if from and to are both -1, it means (in wxWindows) that all text should
989 // be selected - translate into Windows convention
990 if ( (from
== -1) && (to
== -1) )
996 DoSetSelection(from
, to
);
999 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
1001 HWND hWnd
= GetHwnd();
1010 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
1013 #endif // wxUSE_RICHEDIT
1015 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
1021 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1022 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1023 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1024 // option is set (but it does work ok in richedit 1.0 mode...)
1026 // so to make it work we either need to give focus to it here which
1027 // will probably create many problems (dummy focus events; window
1028 // containing the text control being brought to foreground
1029 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1030 // create other problems too -- and in fact it does because if we turn
1031 // on/off this style while appending the text to the control, the
1032 // vertical scrollbar never appears in it even if we append tons of
1033 // text and to work around this the only solution I found was to use
1034 // ES_DISABLENOSCROLL
1036 // this is very ugly but I don't see any other way to make this work
1037 if ( GetRichVersion() > 1 )
1039 if ( !HasFlag(wxTE_NOHIDESEL
) )
1041 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1042 ECOOP_OR
, ECO_NOHIDESEL
);
1044 //else: everything is already ok
1046 #endif // wxUSE_RICHEDIT
1048 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
1051 // restore ECO_NOHIDESEL if we changed it
1052 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) )
1054 ::SendMessage(GetHwnd(), EM_SETOPTIONS
,
1055 ECOOP_AND
, ~ECO_NOHIDESEL
);
1057 #endif // wxUSE_RICHEDIT
1060 // WPARAM is 0: selection is scrolled into view
1061 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
1065 // ----------------------------------------------------------------------------
1067 // ----------------------------------------------------------------------------
1069 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
1071 // Set selection and remove it
1072 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
1074 SendMessage(GetHwnd(), EM_REPLACESEL
,
1080 (LPARAM
)value
.c_str());
1083 void wxTextCtrl::Remove(long from
, long to
)
1085 Replace(from
, to
, wxEmptyString
);
1088 bool wxTextCtrl::LoadFile(const wxString
& file
)
1090 if ( wxTextCtrlBase::LoadFile(file
) )
1092 // update the size limit if needed
1101 bool wxTextCtrl::IsModified() const
1103 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
1106 // Makes 'unmodified'
1107 void wxTextCtrl::DiscardEdits()
1109 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
1112 int wxTextCtrl::GetNumberOfLines() const
1114 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
1117 long wxTextCtrl::XYToPosition(long x
, long y
) const
1119 // This gets the char index for the _beginning_ of this line
1120 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
1122 return charIndex
+ x
;
1125 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1127 HWND hWnd
= GetHwnd();
1129 // This gets the line number containing the character
1134 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
1137 #endif // wxUSE_RICHEDIT
1139 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
1148 // This gets the char index for the _beginning_ of this line
1149 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
1150 if ( charIndex
== -1 )
1155 // The X position must therefore be the different between pos and charIndex
1157 *x
= pos
- charIndex
;
1164 void wxTextCtrl::ShowPosition(long pos
)
1166 HWND hWnd
= GetHwnd();
1168 // To scroll to a position, we pass the number of lines and characters
1169 // to scroll *by*. This means that we need to:
1170 // (1) Find the line position of the current line.
1171 // (2) Find the line position of pos.
1172 // (3) Scroll by (pos - current).
1173 // For now, ignore the horizontal scrolling.
1175 // Is this where scrolling is relative to - the line containing the caret?
1176 // Or is the first visible line??? Try first visible line.
1177 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1179 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1181 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1183 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1185 if (linesToScroll
!= 0)
1186 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1189 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1191 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1194 int wxTextCtrl::GetLineLength(long lineNo
) const
1196 long pos
= XYToPosition(0, lineNo
);
1198 return GetLengthOfLineContainingPos(pos
);
1201 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1203 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1205 // there must be at least enough place for the length WORD in the
1207 len
+= sizeof(WORD
);
1211 wxStringBufferLength
tmp(str
, len
);
1214 *(WORD
*)buf
= (WORD
)len
;
1215 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
,
1216 lineNo
, (LPARAM
)buf
);
1224 void wxTextCtrl::SetMaxLength(unsigned long len
)
1226 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1229 // ----------------------------------------------------------------------------
1231 // ----------------------------------------------------------------------------
1233 void wxTextCtrl::Undo()
1237 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1241 void wxTextCtrl::Redo()
1245 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1246 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1250 bool wxTextCtrl::CanUndo() const
1252 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1255 bool wxTextCtrl::CanRedo() const
1257 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1260 // ----------------------------------------------------------------------------
1261 // caret handling (Windows only)
1262 // ----------------------------------------------------------------------------
1264 bool wxTextCtrl::ShowNativeCaret(bool show
)
1266 if ( show
!= m_isNativeCaretShown
)
1268 if ( !(show
? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) )
1270 // not an error, may simply indicate that it's not shown/hidden
1271 // yet (i.e. it had been hidden/showh 2 times before)
1275 m_isNativeCaretShown
= show
;
1281 // ----------------------------------------------------------------------------
1282 // implemenation details
1283 // ----------------------------------------------------------------------------
1285 void wxTextCtrl::Command(wxCommandEvent
& event
)
1287 SetValue(event
.GetString());
1288 ProcessCommand (event
);
1291 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1293 // By default, load the first file into the text window.
1294 if (event
.GetNumberOfFiles() > 0)
1296 LoadFile(event
.GetFiles()[0]);
1300 // ----------------------------------------------------------------------------
1301 // kbd input processing
1302 // ----------------------------------------------------------------------------
1304 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1306 MSG
*msg
= (MSG
*)pMsg
;
1308 // check for our special keys here: if we don't do it and the parent frame
1309 // uses them as accelerators, they wouldn't work at all, so we disable
1310 // usual preprocessing for them
1311 if ( msg
->message
== WM_KEYDOWN
)
1313 WORD vkey
= (WORD
) msg
->wParam
;
1314 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1316 if ( vkey
== VK_BACK
)
1321 // we want to process some Ctrl-foo and Shift-bar but no key
1322 // combinations without either Ctrl or Shift nor with both of them
1324 const int ctrl
= wxIsCtrlDown(),
1325 shift
= wxIsShiftDown();
1326 switch ( ctrl
+ shift
)
1329 wxFAIL_MSG( _T("how many modifiers have we got?") );
1337 // either Ctrl or Shift pressed
1352 else // Shift is pressed
1354 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1361 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1364 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1366 switch ( event
.GetKeyCode() )
1369 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1371 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1372 InitCommandEvent(event
);
1373 event
.SetString(GetValue());
1374 if ( GetEventHandler()->ProcessEvent(event
) )
1377 //else: multiline controls need Enter for themselves
1382 // always produce navigation event - even if we process TAB
1383 // ourselves the fact that we got here means that the user code
1384 // decided to skip processing of this TAB - probably to let it
1385 // do its default job.
1387 wxNavigationKeyEvent eventNav
;
1388 eventNav
.SetDirection(!event
.ShiftDown());
1389 eventNav
.SetWindowChange(event
.ControlDown());
1390 eventNav
.SetEventObject(this);
1392 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1398 // no, we didn't process it
1402 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1404 long lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1406 if ( nMsg
== WM_GETDLGCODE
)
1408 // we always want the chars and the arrows: the arrows for navigation
1409 // and the chars because we want Ctrl-C to work even in a read only
1411 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1415 // we may have several different cases:
1416 // 1. normal case: both TAB and ENTER are used for dlg navigation
1417 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1418 // next control in the dialog
1419 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1421 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1422 // to the next control
1424 // the multiline edit control should always get <Return> for itself
1425 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
1426 lDlgCode
|= DLGC_WANTMESSAGE
;
1428 if ( HasFlag(wxTE_PROCESS_TAB
) )
1429 lDlgCode
|= DLGC_WANTTAB
;
1435 // NB: use "=", not "|=" as the base class version returns the
1436 // same flags is this state as usual (i.e. including
1437 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1438 // native Win32 apps?) but for now live with it.
1446 // ----------------------------------------------------------------------------
1447 // text control event processing
1448 // ----------------------------------------------------------------------------
1450 bool wxTextCtrl::SendUpdateEvent()
1452 // is event reporting suspended?
1453 if ( m_suppressNextUpdate
)
1455 // do process the next one
1456 m_suppressNextUpdate
= FALSE
;
1461 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
1462 InitCommandEvent(event
);
1463 event
.SetString(GetValue());
1465 return ProcessCommand(event
);
1468 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1475 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1478 event
.SetEventObject(this);
1479 GetEventHandler()->ProcessEvent(event
);
1488 // the text size limit has been hit -- try to increase it
1489 if ( !AdjustSpaceLimit() )
1491 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1492 InitCommandEvent(event
);
1493 event
.SetString(GetValue());
1494 ProcessCommand(event
);
1498 // the other edit notification messages are not processed
1507 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1513 WXUINT
WXUNUSED(message
),
1514 WXWPARAM
WXUNUSED(wParam
),
1515 WXLPARAM
WXUNUSED(lParam
)
1522 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1523 return (WXHBRUSH
) hbrush
;
1525 #endif // wxUSE_CTL3D
1528 wxColour colBack
= GetBackgroundColour();
1530 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1531 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1533 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1534 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1536 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1538 return (WXHBRUSH
)brush
->GetResourceHandle();
1541 // In WIN16, need to override normal erasing because
1542 // Ctl3D doesn't use the wxWindows background colour.
1544 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1546 wxColour
col(m_backgroundColour
);
1550 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1554 ::GetClientRect(GetHwnd(), &rect
);
1556 COLORREF ref
= wxColourToRGB(col
);
1557 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1559 wxLogLastError(wxT("CreateSolidBrush"));
1561 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1563 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1565 ::FillRect(hdc
, &rect
, hBrush
);
1566 ::DeleteObject(hBrush
);
1567 ::SetMapMode(hdc
, mode
);
1572 bool wxTextCtrl::AdjustSpaceLimit()
1575 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1577 // HACK: we try to automatically extend the limit for the amount of text
1578 // to allow (interactively) entering more than 64Kb of text under
1579 // Win9x but we shouldn't reset the text limit which was previously
1580 // set explicitly with SetMaxLength()
1582 // we could solve this by storing the limit we set in wxTextCtrl but
1583 // to save space we prefer to simply test here the actual limit
1584 // value: we consider that SetMaxLength() can only be called for
1586 if ( limit
< 0x8000 )
1588 // we've got more text than limit set by SetMaxLength()
1592 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1595 limit
= len
+ 0x8000; // 32Kb
1600 // as a nice side effect, this also allows passing limit > 64Kb
1601 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1604 #endif // wxUSE_RICHEDIT
1606 if ( limit
> 0xffff )
1608 // this will set it to a platform-dependent maximum (much more
1609 // than 64Kb under NT)
1613 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1618 // we changed the limit
1622 bool wxTextCtrl::AcceptsFocus() const
1624 // we don't want focus if we can't be edited unless we're a multiline
1625 // control because then it might be still nice to get focus from keyboard
1626 // to be able to scroll it without mouse
1627 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1630 wxSize
wxTextCtrl::DoGetBestSize() const
1633 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1635 int wText
= DEFAULT_ITEM_WIDTH
;
1637 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1638 if ( m_windowStyle
& wxTE_MULTILINE
)
1640 hText
*= wxMax(GetNumberOfLines(), 5);
1642 //else: for single line control everything is ok
1644 return wxSize(wText
, hText
);
1647 // ----------------------------------------------------------------------------
1648 // standard handlers for standard edit menu events
1649 // ----------------------------------------------------------------------------
1651 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1656 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1661 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1666 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1671 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1676 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1679 GetSelection(& from
, & to
);
1680 if (from
!= -1 && to
!= -1)
1684 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1686 SetSelection(-1, -1);
1689 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1691 event
.Enable( CanCut() );
1694 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1696 event
.Enable( CanCopy() );
1699 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1701 event
.Enable( CanPaste() );
1704 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1706 event
.Enable( CanUndo() );
1709 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1711 event
.Enable( CanRedo() );
1714 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1717 GetSelection(& from
, & to
);
1718 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1721 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1723 event
.Enable(GetLastPosition() > 0);
1726 void wxTextCtrl::OnRightClick(wxMouseEvent
& event
)
1731 if (!m_privateContextMenu
)
1733 m_privateContextMenu
= new wxMenu
;
1734 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1735 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1736 m_privateContextMenu
->AppendSeparator();
1737 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1738 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1739 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1740 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1741 m_privateContextMenu
->AppendSeparator();
1742 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1744 PopupMenu(m_privateContextMenu
, event
.GetPosition());
1752 void wxTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
1754 // be sure the caret remains invisible if the user had hidden it
1755 if ( !m_isNativeCaretShown
)
1757 ::HideCaret(GetHwnd());
1761 // the rest of the file only deals with the rich edit controls
1764 // ----------------------------------------------------------------------------
1765 // EN_LINK processing
1766 // ----------------------------------------------------------------------------
1768 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1770 NMHDR
*hdr
= (NMHDR
* )lParam
;
1771 switch ( hdr
->code
)
1775 const MSGFILTER
*msgf
= (MSGFILTER
*)lParam
;
1776 UINT msg
= msgf
->msg
;
1778 // this is a bit crazy but richedit 1.0 sends us all mouse
1779 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1780 // generate the wxWin events for this message manually
1782 // NB: in fact, this is still not totally correct as it does
1783 // send us WM_LBUTTONUP if the selection was cleared by the
1784 // last click -- so currently we get 2 events in this case,
1785 // but as I don't see any obvious way to check for this I
1786 // leave this code in place because it's still better than
1787 // not getting left up events at all
1788 if ( msg
== WM_LBUTTONUP
)
1790 WXUINT flags
= msgf
->wParam
;
1791 int x
= GET_X_LPARAM(msgf
->lParam
),
1792 y
= GET_Y_LPARAM(msgf
->lParam
);
1794 HandleMouseEvent(msg
, x
, y
, flags
);
1798 // return TRUE to process the event (and FALSE to ignore it)
1803 const ENLINK
*enlink
= (ENLINK
*)hdr
;
1805 switch ( enlink
->msg
)
1808 // ok, so it is hardcoded - do we really nee to
1810 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1815 case WM_LBUTTONDOWN
:
1817 case WM_LBUTTONDBLCLK
:
1818 case WM_RBUTTONDOWN
:
1820 case WM_RBUTTONDBLCLK
:
1821 // send a mouse event
1823 static const wxEventType eventsMouse
[] =
1834 // the event ids are consecutive
1836 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1838 InitMouseEvent(evtMouse
,
1839 GET_X_LPARAM(enlink
->lParam
),
1840 GET_Y_LPARAM(enlink
->lParam
),
1843 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1845 enlink
->chrg
.cpMax
);
1847 InitCommandEvent(event
);
1849 *result
= ProcessCommand(event
);
1857 // not processed, leave it to the base class
1858 return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
1861 // ----------------------------------------------------------------------------
1862 // colour setting for the rich edit controls
1863 // ----------------------------------------------------------------------------
1865 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1867 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1869 // colour didn't really change
1875 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1876 // EM_SETBKGNDCOLOR additionally
1877 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1883 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1885 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1887 // colour didn't really change
1893 // change the colour of everything
1896 cf
.cbSize
= sizeof(cf
);
1897 cf
.dwMask
= CFM_COLOR
;
1898 cf
.crTextColor
= wxColourToRGB(colour
);
1899 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1905 // ----------------------------------------------------------------------------
1906 // styling support for rich edit controls
1907 // ----------------------------------------------------------------------------
1911 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1915 // can't do it with normal text control
1919 // the richedit 1.0 doesn't handle setting background colour, so don't
1920 // even try to do anything if it's the only thing we want to change
1921 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() &&
1922 !style
.HasLeftIndent() && !style
.HasRightIndent() && !style
.HasAlignment() &&
1925 // nothing to do: return TRUE if there was really nothing to do and
1926 // FALSE if we failed to set bg colour
1927 return !style
.HasBackgroundColour();
1930 // order the range if needed
1938 // we can only change the format of the selection, so select the range we
1939 // want and restore the old selection later
1940 long startOld
, endOld
;
1941 GetSelection(&startOld
, &endOld
);
1943 // but do we really have to change the selection?
1944 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1948 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1951 // initialize CHARFORMAT struct
1960 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1961 // CHARFORMAT in that case
1963 if ( m_verRichEdit
== 1 )
1965 // this is the only thing the control is going to grok
1966 cf
.cbSize
= sizeof(CHARFORMAT
);
1971 // CHARFORMAT or CHARFORMAT2
1972 cf
.cbSize
= sizeof(cf
);
1975 if ( style
.HasFont() )
1977 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1978 // but using it doesn't seem to hurt neither so leaving it for now
1980 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1981 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1983 // fill in data from LOGFONT but recalculate lfHeight because we need
1984 // the real height in twips and not the negative number which
1985 // wxFillLogFont() returns (this is correct in general and works with
1986 // the Windows font mapper, but not here)
1988 wxFillLogFont(&lf
, &style
.GetFont());
1989 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1990 cf
.bCharSet
= lf
.lfCharSet
;
1991 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1992 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1994 // also deal with underline/italic/bold attributes: note that we must
1995 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1996 // style to allow clearing it
1999 cf
.dwEffects
|= CFE_ITALIC
;
2002 if ( lf
.lfWeight
== FW_BOLD
)
2004 cf
.dwEffects
|= CFE_BOLD
;
2007 if ( lf
.lfUnderline
)
2009 cf
.dwEffects
|= CFE_UNDERLINE
;
2012 // strikeout fonts are not supported by wxWindows
2015 if ( style
.HasTextColour() )
2017 cf
.dwMask
|= CFM_COLOR
;
2018 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
2022 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
2024 cf
.dwMask
|= CFM_BACKCOLOR
;
2025 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
2027 #endif // wxUSE_RICHEDIT2
2029 // do format the selection
2030 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
2031 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
2034 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
2037 // now do the paragraph formatting
2040 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2041 // PARAFORMAT in that case
2043 if ( m_verRichEdit
== 1 )
2045 // this is the only thing the control is going to grok
2046 pf
.cbSize
= sizeof(PARAFORMAT
);
2051 // PARAFORMAT or PARAFORMAT2
2052 pf
.cbSize
= sizeof(pf
);
2055 if (style
.HasAlignment())
2057 pf
.dwMask
|= PFM_ALIGNMENT
;
2058 if (style
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2059 pf
.wAlignment
= PFA_RIGHT
;
2060 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2061 pf
.wAlignment
= PFA_CENTER
;
2062 else if (style
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
2063 pf
.wAlignment
= PFA_JUSTIFY
;
2065 pf
.wAlignment
= PFA_LEFT
;
2068 if (style
.HasLeftIndent())
2070 pf
.dwMask
|= PFM_STARTINDENT
;
2072 // Convert from 1/10 mm to TWIPS
2073 pf
.dxStartIndent
= (int) (((double) style
.GetLeftIndent()) * mm2twips
/ 10.0) ;
2075 // TODO: do we need to specify dxOffset?
2078 if (style
.HasRightIndent())
2080 pf
.dwMask
|= PFM_RIGHTINDENT
;
2082 // Convert from 1/10 mm to TWIPS
2083 pf
.dxRightIndent
= (int) (((double) style
.GetRightIndent()) * mm2twips
/ 10.0) ;
2086 if (style
.HasTabs())
2088 pf
.dwMask
|= PFM_TABSTOPS
;
2090 const wxArrayInt
& tabs
= style
.GetTabs();
2092 pf
.cTabCount
= wxMin(tabs
.GetCount(), MAX_TAB_STOPS
);
2094 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2096 // Convert from 1/10 mm to TWIPS
2097 pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips
/ 10.0) ;
2103 // do format the selection
2104 bool ok
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
,
2105 0, (LPARAM
) &pf
) != 0;
2108 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2114 // restore the original selection
2115 DoSetSelection(startOld
, endOld
, FALSE
);
2121 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2123 if ( !wxTextCtrlBase::SetDefaultStyle(style
) )
2126 // we have to do this or the style wouldn't apply for the text typed by the
2128 long posLast
= GetLastPosition();
2129 SetStyle(posLast
, posLast
, m_defaultStyle
);
2134 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2138 // can't do it with normal text control
2142 // initialize CHARFORMAT struct
2151 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2152 // CHARFORMAT in that case
2154 if ( m_verRichEdit
== 1 )
2156 // this is the only thing the control is going to grok
2157 cf
.cbSize
= sizeof(CHARFORMAT
);
2162 // CHARFORMAT or CHARFORMAT2
2163 cf
.cbSize
= sizeof(cf
);
2165 // we can only change the format of the selection, so select the range we
2166 // want and restore the old selection later
2167 long startOld
, endOld
;
2168 GetSelection(&startOld
, &endOld
);
2170 // but do we really have to change the selection?
2171 bool changeSel
= position
!= startOld
|| position
!= endOld
;
2175 DoSetSelection(position
, position
, FALSE
/* don't scroll caret into view */);
2178 // get the selection formatting
2179 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
,
2180 SCF_SELECTION
, (LPARAM
)&cf
) ;
2183 lf
.lfHeight
= cf
.yHeight
;
2185 lf
.lfCharSet
= ANSI_CHARSET
; // FIXME: how to get correct charset?
2186 lf
.lfClipPrecision
= 0;
2187 lf
.lfEscapement
= 0;
2188 wxStrcpy(lf
.lfFaceName
, cf
.szFaceName
);
2189 if (cf
.dwEffects
& CFE_ITALIC
)
2191 lf
.lfOrientation
= 0;
2192 lf
.lfPitchAndFamily
= cf
.bPitchAndFamily
;
2194 if (cf
.dwEffects
& CFE_STRIKEOUT
)
2195 lf
.lfStrikeOut
= TRUE
;
2196 if (cf
.dwEffects
& CFE_UNDERLINE
)
2197 lf
.lfUnderline
= TRUE
;
2198 if (cf
.dwEffects
& CFE_BOLD
)
2199 lf
.lfWeight
= FW_BOLD
;
2201 wxFont font
= wxCreateFontFromLogFont(& lf
);
2204 style
.SetFont(font
);
2206 style
.SetTextColour(wxColour(cf
.crTextColor
));
2209 if ( m_verRichEdit
!= 1 )
2211 // cf.dwMask |= CFM_BACKCOLOR;
2212 style
.SetBackgroundColour(wxColour(cf
.crBackColor
));
2214 #endif // wxUSE_RICHEDIT2
2216 // now get the paragraph formatting
2219 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2220 // PARAFORMAT in that case
2222 if ( m_verRichEdit
== 1 )
2224 // this is the only thing the control is going to grok
2225 pf
.cbSize
= sizeof(PARAFORMAT
);
2230 // PARAFORMAT or PARAFORMAT2
2231 pf
.cbSize
= sizeof(pf
);
2234 // do format the selection
2235 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ;
2237 style
.SetLeftIndent( (int) ((double) pf
.dxStartIndent
* twips2mm
* 10.0) );
2238 style
.SetRightIndent( (int) ((double) pf
.dxRightIndent
* twips2mm
* 10.0) );
2240 if (pf
.wAlignment
== PFA_CENTER
)
2241 style
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
2242 else if (pf
.wAlignment
== PFA_RIGHT
)
2243 style
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
2244 else if (pf
.wAlignment
== PFA_JUSTIFY
)
2245 style
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
2247 style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
2249 wxArrayInt tabStops
;
2251 for (i
= 0; i
< (size_t) pf
.cTabCount
; i
++)
2253 tabStops
[i
] = (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm
* 10.0) ;
2258 // restore the original selection
2259 DoSetSelection(startOld
, endOld
, FALSE
);
2267 // ----------------------------------------------------------------------------
2269 // ----------------------------------------------------------------------------
2271 bool wxRichEditModule::OnInit()
2273 // don't do anything - we will load it when needed
2277 void wxRichEditModule::OnExit()
2279 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
2281 if ( ms_hRichEdit
[i
] )
2283 ::FreeLibrary(ms_hRichEdit
[i
]);
2284 ms_hRichEdit
[i
] = NULL
;
2290 bool wxRichEditModule::Load(int version
)
2292 // we don't support loading richedit 3.0 as I don't know how to distinguish
2293 // it from 2.0 anyhow
2294 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
2295 _T("incorrect richedit control version requested") );
2297 // make it the index in the array
2300 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
2302 // we had already tried to load it and failed
2306 if ( ms_hRichEdit
[version
] )
2308 // we've already got this one
2312 wxString dllname
= version
? _T("riched20") : _T("riched32");
2313 dllname
+= _T(".dll");
2315 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
2317 if ( !ms_hRichEdit
[version
] )
2319 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
2321 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
2329 #endif // wxUSE_RICHEDIT
2331 #endif // wxUSE_TEXTCTRL