1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "textctrl.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
43 #include "wx/module.h"
46 #include "wx/clipbrd.h"
49 #include "wx/textfile.h"
53 #include "wx/msw/private.h"
57 #include <sys/types.h>
59 #if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__))
63 // old mingw32 doesn't define this
65 #define CFM_CHARSET 0x08000000
69 #define CFM_BACKCOLOR 0x04000000
72 // cygwin does not have these defined for richedit
74 #define ENM_LINK 0x04000000
77 #ifndef EM_AUTOURLDETECT
78 #define EM_AUTOURLDETECT (WM_USER + 91)
82 #define EN_LINK 0x070b
84 typedef struct _enlink
95 #define SF_UNICODE 0x0010
98 // Watcom C++ doesn't define this
100 #define SCF_ALL 0x0004
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
109 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
111 #endif // wxUSE_RICHEDIT
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
119 // this module initializes RichEdit DLL(s) if needed
120 class wxRichEditModule
: public wxModule
123 virtual bool OnInit();
124 virtual void OnExit();
126 // load the richedit DLL of at least of required version
127 static bool Load(int version
= 1);
130 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
131 static HINSTANCE ms_hRichEdit
[2];
133 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
136 HINSTANCE
wxRichEditModule::ms_hRichEdit
[2] = { NULL
, NULL
};
138 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
140 #endif // wxUSE_RICHEDIT
142 // ----------------------------------------------------------------------------
143 // event tables and other macros
144 // ----------------------------------------------------------------------------
146 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
148 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
149 EVT_CHAR(wxTextCtrl::OnChar
)
150 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
152 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
153 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
154 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
155 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
156 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
158 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
159 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
160 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
161 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
162 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
164 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
168 // ============================================================================
170 // ============================================================================
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void wxTextCtrl::Init()
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 msStyle
|= ES_AUTOVSCROLL
;
261 m_verRichEdit
= verRichEdit
;
262 if ( m_verRichEdit
== 1 )
264 windowClass
= wxT("RICHEDIT");
268 #ifndef RICHEDIT_CLASS
269 wxString RICHEDIT_CLASS
;
270 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
272 RICHEDIT_CLASS
+= _T('W');
274 RICHEDIT_CLASS
+= _T('A');
275 #endif // Unicode/ANSI
276 #endif // !RICHEDIT_CLASS
278 windowClass
= RICHEDIT_CLASS
;
282 #endif // wxUSE_RICHEDIT
284 // we need to turn '\n's into "\r\n"s for the multiline controls
286 if ( m_windowStyle
& wxTE_MULTILINE
)
288 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
295 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
298 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
303 // have to enable events manually
304 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
306 if ( m_windowStyle
& wxTE_AUTO_URL
)
310 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
313 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
315 #endif // wxUSE_RICHEDIT
320 // Make sure the window style (etc.) reflects the HWND style (roughly)
321 void wxTextCtrl::AdoptAttributesFromHWND()
323 wxWindow::AdoptAttributesFromHWND();
325 HWND hWnd
= GetHwnd();
326 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
328 // retrieve the style to see whether this is an edit or richedit ctrl
330 wxString classname
= wxGetWindowClass(GetHWND());
332 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
339 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
341 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
346 #endif // wxUSE_RICHEDIT
348 if (style
& ES_MULTILINE
)
349 m_windowStyle
|= wxTE_MULTILINE
;
350 if (style
& ES_PASSWORD
)
351 m_windowStyle
|= wxTE_PASSWORD
;
352 if (style
& ES_READONLY
)
353 m_windowStyle
|= wxTE_READONLY
;
354 if (style
& ES_WANTRETURN
)
355 m_windowStyle
|= wxTE_PROCESS_ENTER
;
358 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
360 long msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
365 if ( style
& wxTE_MULTILINE
)
367 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
368 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
370 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
371 if ( !(style
& wxTE_NO_VSCROLL
) )
372 msStyle
|= WS_VSCROLL
;
374 style
|= wxTE_PROCESS_ENTER
;
378 // there is really no reason to not have this style for single line
380 msStyle
|= ES_AUTOHSCROLL
;
383 if ( style
& wxHSCROLL
)
384 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
386 if ( style
& wxTE_READONLY
)
387 msStyle
|= ES_READONLY
;
389 if ( style
& wxTE_PASSWORD
)
390 msStyle
|= ES_PASSWORD
;
392 if ( style
& wxTE_AUTO_SCROLL
)
393 msStyle
|= ES_AUTOHSCROLL
;
395 if ( style
& wxTE_NOHIDESEL
)
396 msStyle
|= ES_NOHIDESEL
;
401 void wxTextCtrl::SetWindowStyleFlag(long style
)
404 // we have to deal with some styles separately because they can't be
405 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
406 // using richedit-specific EM_SETOPTIONS
408 ((style
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) )
410 bool set
= (style
& wxTE_NOHIDESEL
) != 0;
412 ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set
? ECOOP_OR
: ECOOP_AND
,
413 set
? ECO_NOHIDESEL
: ~ECO_NOHIDESEL
);
415 #endif // wxUSE_RICHEDIT
417 wxControl::SetWindowStyleFlag(style
);
420 // ----------------------------------------------------------------------------
421 // set/get the controls text
422 // ----------------------------------------------------------------------------
424 wxString
wxTextCtrl::GetValue() const
426 // range 0..-1 is special for GetRange() and means to retrieve all text
427 return GetRange(0, -1);
430 wxString
wxTextCtrl::GetRange(long from
, long to
) const
434 if ( from
>= to
&& to
!= -1 )
436 // nothing to retrieve
443 int len
= GetWindowTextLength(GetHwnd());
446 // alloc one extra WORD as needed by the control
447 wxChar
*p
= str
.GetWriteBuf(++len
);
450 textRange
.chrg
.cpMin
= from
;
451 textRange
.chrg
.cpMax
= to
== -1 ? len
: to
;
452 textRange
.lpstrText
= p
;
454 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
456 if ( m_verRichEdit
> 1 )
458 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
459 // neither Unix nor Windows style - convert it to something
463 if ( *p
== _T('\r') )
470 if ( m_verRichEdit
== 1 )
472 // convert to the canonical form - see comment below
473 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
476 //else: no text at all, leave the string empty
479 #endif // wxUSE_RICHEDIT
482 str
= wxGetWindowText(GetHWND());
484 // need only a range?
487 str
= str
.Mid(from
, to
- from
);
490 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
491 // canonical one (same one as above) for consistency with the other kinds
492 // of controls and, more importantly, with the other ports
493 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
499 void wxTextCtrl::SetValue(const wxString
& value
)
501 // if the text is long enough, it's faster to just set it instead of first
502 // comparing it with the old one (chances are that it will be different
503 // anyhow, this comparison is there to avoid flicker for small single-line
504 // edit controls mostly)
505 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
507 DoWriteText(value
, FALSE
/* not selection only */);
509 // mark the control as being not dirty - we changed its text, not the
513 // for compatibility, don't move the cursor when doing SetValue()
514 SetInsertionPoint(0);
518 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
520 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
524 wchar_t *wbuf
= (wchar_t *)buf
;
525 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
530 cb
-= sizeof(wchar_t);
531 (*pcb
) += sizeof(wchar_t);
534 *(const wchar_t **)dwCookie
= wpc
;
539 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from utils.cpp
541 #if wxUSE_UNICODE_MSLU
542 bool wxTextCtrl::StreamIn(const wxString
& value
,
543 wxFontEncoding
WXUNUSED(encoding
),
546 const wchar_t *wpc
= value
.c_str();
547 #else // !wxUSE_UNICODE_MSLU
548 bool wxTextCtrl::StreamIn(const wxString
& value
,
549 wxFontEncoding encoding
,
552 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
553 // text in the non default charset - otherwise it thinks it knows better
554 // than we do and always shows it in the default one
556 // first get the Windows code page for this encoding
557 long codepage
= wxEncodingToCodepage(encoding
);
558 if ( codepage
== -1 )
564 // next translate to Unicode using this code page
565 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
568 wxWCharBuffer
wchBuf(len
);
570 wchar_t *wchBuf
= (wchar_t *)malloc((len
+ 1)*sizeof(wchar_t));
573 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
574 (wchar_t *)(const wchar_t *)wchBuf
, len
) )
576 wxLogLastError(_T("MultiByteToWideChar"));
579 // finally, stream it in the control
580 const wchar_t *wpc
= wchBuf
;
581 #endif // wxUSE_UNICODE_MSLU
585 eds
.dwCookie
= (DWORD
)&wpc
;
586 // the cast below is needed for broken (very) old mingw32 headers
587 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
589 if ( !::SendMessage(GetHwnd(), EM_STREAMIN
,
592 (selectionOnly
? SFF_SELECTION
: 0),
593 (LPARAM
)&eds
) || eds
.dwError
)
595 wxLogLastError(_T("EM_STREAMIN"));
600 #endif // !wxUSE_WCHAR_T
605 #endif // wxUSE_RICHEDIT
607 void wxTextCtrl::WriteText(const wxString
& value
)
612 void wxTextCtrl::DoWriteText(const wxString
& value
, bool selectionOnly
)
615 if ( m_windowStyle
& wxTE_MULTILINE
)
616 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
621 // there are several complications with the rich edit controls here
625 // first, ensure that the new text will be in the default style
626 if ( !m_defaultStyle
.IsDefault() )
629 GetSelection(&start
, &end
);
630 SetStyle(start
, end
, m_defaultStyle
);
633 #if wxUSE_UNICODE_MSLU
634 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
635 // but EM_STREAMIN works
636 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
638 done
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
);
640 #endif // wxUSE_UNICODE_MSLU
643 // next check if the text we're inserting must be shown in a non
644 // default charset -- this only works for RichEdit > 1.0
645 if ( GetRichVersion() > 1 )
647 wxFont font
= m_defaultStyle
.GetFont();
653 wxFontEncoding encoding
= font
.GetEncoding();
654 if ( encoding
!= wxFONTENCODING_SYSTEM
)
656 done
= StreamIn(valueDos
, encoding
, selectionOnly
);
660 #endif // !wxUSE_UNICODE
664 #endif // wxUSE_RICHEDIT
666 if ( !selectionOnly
)
668 SetSelection(-1, -1);
671 ::SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
677 void wxTextCtrl::AppendText(const wxString
& text
)
679 SetInsertionPointEnd();
684 void wxTextCtrl::Clear()
686 ::SetWindowText(GetHwnd(), wxT(""));
689 // ----------------------------------------------------------------------------
690 // Clipboard operations
691 // ----------------------------------------------------------------------------
693 void wxTextCtrl::Copy()
697 ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L);
701 void wxTextCtrl::Cut()
705 ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L);
709 void wxTextCtrl::Paste()
713 ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L);
717 bool wxTextCtrl::CanCopy() const
719 // Can copy if there's a selection
721 GetSelection(&from
, &to
);
725 bool wxTextCtrl::CanCut() const
727 return CanCopy() && IsEditable();
730 bool wxTextCtrl::CanPaste() const
738 UINT cf
= 0; // 0 == any format
740 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
742 #endif // wxUSE_RICHEDIT
744 // Standard edit control: check for straight text on clipboard
745 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
748 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
751 return isTextAvailable
;
754 // ----------------------------------------------------------------------------
756 // ----------------------------------------------------------------------------
758 void wxTextCtrl::SetEditable(bool editable
)
760 HWND hWnd
= GetHwnd();
761 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
764 void wxTextCtrl::SetInsertionPoint(long pos
)
766 DoSetSelection(pos
, pos
);
769 void wxTextCtrl::SetInsertionPointEnd()
774 if ( m_verRichEdit
== 1 )
776 // we don't have to waste time calling GetLastPosition() in this case
779 else // !RichEdit 1.0
780 #endif // wxUSE_RICHEDIT
782 pos
= GetLastPosition();
785 SetInsertionPoint(pos
);
788 long wxTextCtrl::GetInsertionPoint() const
796 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
799 #endif // wxUSE_RICHEDIT
801 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
805 long wxTextCtrl::GetLastPosition() const
807 int numLines
= GetNumberOfLines();
808 long posStartLastLine
= XYToPosition(0, numLines
- 1);
810 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
812 return posStartLastLine
+ lenLastLine
;
815 // If the return values from and to are the same, there is no
817 void wxTextCtrl::GetSelection(long* from
, long* to
) const
823 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
825 *from
= charRange
.cpMin
;
826 *to
= charRange
.cpMax
;
829 #endif // !wxUSE_RICHEDIT
831 DWORD dwStart
, dwEnd
;
832 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
839 bool wxTextCtrl::IsEditable() const
841 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
843 return (style
& ES_READONLY
) == 0;
846 // ----------------------------------------------------------------------------
848 // ----------------------------------------------------------------------------
850 void wxTextCtrl::SetSelection(long from
, long to
)
852 // if from and to are both -1, it means (in wxWindows) that all text should
853 // be selected - translate into Windows convention
854 if ( (from
== -1) && (to
== -1) )
860 DoSetSelection(from
, to
);
863 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
865 HWND hWnd
= GetHwnd();
874 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
877 #endif // wxUSE_RICHEDIT
879 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
884 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
887 // WPARAM is 0: selection is scrolled into view
888 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
892 // ----------------------------------------------------------------------------
894 // ----------------------------------------------------------------------------
896 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
898 // Set selection and remove it
899 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
901 SendMessage(GetHwnd(), EM_REPLACESEL
,
907 (LPARAM
)value
.c_str());
910 void wxTextCtrl::Remove(long from
, long to
)
912 Replace(from
, to
, _T(""));
915 bool wxTextCtrl::LoadFile(const wxString
& file
)
917 if ( wxTextCtrlBase::LoadFile(file
) )
919 // update the size limit if needed
928 bool wxTextCtrl::IsModified() const
930 return SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0;
933 // Makes 'unmodified'
934 void wxTextCtrl::DiscardEdits()
936 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
939 int wxTextCtrl::GetNumberOfLines() const
941 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
944 long wxTextCtrl::XYToPosition(long x
, long y
) const
946 // This gets the char index for the _beginning_ of this line
947 long charIndex
= SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
949 return charIndex
+ x
;
952 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
954 HWND hWnd
= GetHwnd();
956 // This gets the line number containing the character
961 lineNo
= SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
964 #endif // wxUSE_RICHEDIT
966 lineNo
= SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
975 // This gets the char index for the _beginning_ of this line
976 long charIndex
= SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
977 if ( charIndex
== -1 )
982 // The X position must therefore be the different between pos and charIndex
984 *x
= pos
- charIndex
;
991 void wxTextCtrl::ShowPosition(long pos
)
993 HWND hWnd
= GetHwnd();
995 // To scroll to a position, we pass the number of lines and characters
996 // to scroll *by*. This means that we need to:
997 // (1) Find the line position of the current line.
998 // (2) Find the line position of pos.
999 // (3) Scroll by (pos - current).
1000 // For now, ignore the horizontal scrolling.
1002 // Is this where scrolling is relative to - the line containing the caret?
1003 // Or is the first visible line??? Try first visible line.
1004 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1006 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
1008 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
1010 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
1012 if (linesToScroll
!= 0)
1013 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
1016 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
1018 return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0);
1021 int wxTextCtrl::GetLineLength(long lineNo
) const
1023 long pos
= XYToPosition(0, lineNo
);
1025 return GetLengthOfLineContainingPos(pos
);
1028 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1030 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
1032 // there must be at least enough place for the length WORD in the
1034 len
+= sizeof(WORD
);
1037 wxChar
*buf
= str
.GetWriteBuf(len
);
1039 *(WORD
*)buf
= (WORD
)len
;
1040 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
1043 str
.UngetWriteBuf(len
);
1048 void wxTextCtrl::SetMaxLength(unsigned long len
)
1050 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
1053 // ----------------------------------------------------------------------------
1055 // ----------------------------------------------------------------------------
1057 void wxTextCtrl::Undo()
1061 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1065 void wxTextCtrl::Redo()
1069 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1070 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1074 bool wxTextCtrl::CanUndo() const
1076 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1079 bool wxTextCtrl::CanRedo() const
1081 return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0;
1084 // ----------------------------------------------------------------------------
1085 // implemenation details
1086 // ----------------------------------------------------------------------------
1088 void wxTextCtrl::Command(wxCommandEvent
& event
)
1090 SetValue(event
.GetString());
1091 ProcessCommand (event
);
1094 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1096 // By default, load the first file into the text window.
1097 if (event
.GetNumberOfFiles() > 0)
1099 LoadFile(event
.GetFiles()[0]);
1103 // ----------------------------------------------------------------------------
1104 // kbd input processing
1105 // ----------------------------------------------------------------------------
1107 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1109 MSG
*msg
= (MSG
*)pMsg
;
1111 // check for our special keys here: if we don't do it and the parent frame
1112 // uses them as accelerators, they wouldn't work at all, so we disable
1113 // usual preprocessing for them
1114 if ( msg
->message
== WM_KEYDOWN
)
1116 WORD vkey
= (WORD
) msg
->wParam
;
1117 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1119 if ( vkey
== VK_BACK
)
1124 if ( wxIsCtrlDown() )
1138 else if ( wxIsShiftDown() )
1140 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1146 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1149 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1151 switch ( event
.KeyCode() )
1154 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1156 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1157 InitCommandEvent(event
);
1158 event
.SetString(GetValue());
1159 if ( GetEventHandler()->ProcessEvent(event
) )
1162 //else: multiline controls need Enter for themselves
1167 // always produce navigation event - even if we process TAB
1168 // ourselves the fact that we got here means that the user code
1169 // decided to skip processing of this TAB - probably to let it
1170 // do its default job.
1172 wxNavigationKeyEvent eventNav
;
1173 eventNav
.SetDirection(!event
.ShiftDown());
1174 eventNav
.SetWindowChange(event
.ControlDown());
1175 eventNav
.SetEventObject(this);
1177 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1183 // no, we didn't process it
1187 long wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1189 // we always want the characters and the arrows
1190 if ( nMsg
== WM_GETDLGCODE
)
1192 // we always want the chars and the arrows
1193 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1195 // we may have several different cases:
1196 // 1. normal case: both TAB and ENTER are used for dialog navigation
1197 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
1198 // control in the dialog
1199 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
1200 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
1202 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
1203 lDlgCode
|= DLGC_WANTMESSAGE
;
1204 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
1205 lDlgCode
|= DLGC_WANTTAB
;
1210 return wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1213 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1220 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1223 event
.SetEventObject( this );
1224 GetEventHandler()->ProcessEvent(event
);
1230 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1231 InitCommandEvent(event
);
1232 event
.SetString(GetValue());
1233 ProcessCommand(event
);
1238 // the text size limit has been hit - increase it
1239 if ( !AdjustSpaceLimit() )
1241 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1242 InitCommandEvent(event
);
1243 event
.SetString(GetValue());
1244 ProcessCommand(event
);
1248 // the other notification messages are not processed
1262 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1268 WXUINT
WXUNUSED(message
),
1269 WXWPARAM
WXUNUSED(wParam
),
1270 WXLPARAM
WXUNUSED(lParam
)
1277 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1278 return (WXHBRUSH
) hbrush
;
1280 #endif // wxUSE_CTL3D
1283 if (GetParent()->GetTransparentBackground())
1284 SetBkMode(hdc
, TRANSPARENT
);
1286 SetBkMode(hdc
, OPAQUE
);
1288 wxColour colBack
= GetBackgroundColour();
1290 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1291 colBack
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1293 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1294 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1296 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1298 return (WXHBRUSH
)brush
->GetResourceHandle();
1301 // In WIN16, need to override normal erasing because
1302 // Ctl3D doesn't use the wxWindows background colour.
1304 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1306 wxColour
col(m_backgroundColour
);
1310 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
1314 ::GetClientRect(GetHwnd(), &rect
);
1316 COLORREF ref
= wxColourToRGB(col
);
1317 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1319 wxLogLastError(wxT("CreateSolidBrush"));
1321 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1323 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1325 ::FillRect(hdc
, &rect
, hBrush
);
1326 ::DeleteObject(hBrush
);
1327 ::SetMapMode(hdc
, mode
);
1332 bool wxTextCtrl::AdjustSpaceLimit()
1335 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1337 // HACK: we try to automatically extend the limit for the amount of text
1338 // to allow (interactively) entering more than 64Kb of text under
1339 // Win9x but we shouldn't reset the text limit which was previously
1340 // set explicitly with SetMaxLength()
1342 // we could solve this by storing the limit we set in wxTextCtrl but
1343 // to save space we prefer to simply test here the actual limit
1344 // value: we consider that SetMaxLength() can only be called for
1346 if ( limit
< 0x8000 )
1348 // we've got more text than limit set by SetMaxLength()
1352 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1355 limit
= len
+ 0x8000; // 32Kb
1360 // as a nice side effect, this also allows passing limit > 64Kb
1361 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1364 #endif // wxUSE_RICHEDIT
1366 if ( limit
> 0xffff )
1368 // this will set it to a platform-dependent maximum (much more
1369 // than 64Kb under NT)
1373 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1378 // we changed the limit
1382 bool wxTextCtrl::AcceptsFocus() const
1384 // we don't want focus if we can't be edited
1385 return IsEditable() && wxControl::AcceptsFocus();
1388 wxSize
wxTextCtrl::DoGetBestSize() const
1391 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1393 int wText
= DEFAULT_ITEM_WIDTH
;
1395 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1396 if ( m_windowStyle
& wxTE_MULTILINE
)
1398 hText
*= wxMax(GetNumberOfLines(), 5);
1400 //else: for single line control everything is ok
1402 return wxSize(wText
, hText
);
1405 // ----------------------------------------------------------------------------
1406 // standard handlers for standard edit menu events
1407 // ----------------------------------------------------------------------------
1409 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1414 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1419 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1424 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1429 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1434 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1436 event
.Enable( CanCut() );
1439 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1441 event
.Enable( CanCopy() );
1444 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1446 event
.Enable( CanPaste() );
1449 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1451 event
.Enable( CanUndo() );
1454 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1456 event
.Enable( CanRedo() );
1459 // the rest of the file only deals with the rich edit controls
1462 // ----------------------------------------------------------------------------
1463 // EN_LINK processing
1464 // ----------------------------------------------------------------------------
1466 bool wxTextCtrl::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
*result
)
1468 NMHDR
*hdr
= (NMHDR
* )lParam
;
1469 if ( hdr
->code
== EN_LINK
)
1471 ENLINK
*enlink
= (ENLINK
*)hdr
;
1473 switch ( enlink
->msg
)
1476 // ok, so it is hardcoded - do we really nee to customize it?
1477 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1482 case WM_LBUTTONDOWN
:
1484 case WM_LBUTTONDBLCLK
:
1485 case WM_RBUTTONDOWN
:
1487 case WM_RBUTTONDBLCLK
:
1488 // send a mouse event
1490 static const wxEventType eventsMouse
[] =
1501 // the event ids are consecutive
1503 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1505 InitMouseEvent(evtMouse
,
1506 GET_X_LPARAM(enlink
->lParam
),
1507 GET_Y_LPARAM(enlink
->lParam
),
1510 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1512 enlink
->chrg
.cpMax
);
1514 InitCommandEvent(event
);
1516 *result
= ProcessCommand(event
);
1528 // ----------------------------------------------------------------------------
1529 // colour setting for the rich edit controls
1530 // ----------------------------------------------------------------------------
1532 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1534 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1536 // colour didn't really change
1542 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1543 // EM_SETBKGNDCOLOR additionally
1544 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1550 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1552 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1554 // colour didn't really change
1560 // change the colour of everything
1563 cf
.cbSize
= sizeof(cf
);
1564 cf
.dwMask
= CFM_COLOR
;
1565 cf
.crTextColor
= wxColourToRGB(colour
);
1566 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1572 // ----------------------------------------------------------------------------
1573 // styling support for rich edit controls
1574 // ----------------------------------------------------------------------------
1576 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1580 // can't do it with normal text control
1584 // the richedit 1.0 doesn't handle setting background colour, so don't
1585 // even try to do anything if it's the only thing we want to change
1586 if ( m_verRichEdit
== 1 && !style
.HasFont() && !style
.HasTextColour() )
1588 // nothing to do: return TRUE if there was really nothing to do and
1589 // FALSE if we failed to set bg colour
1590 return !style
.HasBackgroundColour();
1593 // order the range if needed
1601 // we can only change the format of the selection, so select the range we
1602 // want and restore the old selection later
1603 long startOld
, endOld
;
1604 GetSelection(&startOld
, &endOld
);
1606 // but do we really have to change the selection?
1607 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1611 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1614 // initialize CHARFORMAT struct
1623 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1624 // CHARFORMAT in that case
1626 if ( m_verRichEdit
== 1 )
1628 // this is the only thing the control is going to grok
1629 cf
.cbSize
= sizeof(CHARFORMAT
);
1634 // CHARFORMAT or CHARFORMAT2
1635 cf
.cbSize
= sizeof(cf
);
1638 if ( style
.HasFont() )
1640 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1641 // but using it doesn't seem to hurt neither so leaving it for now
1643 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1644 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1646 // fill in data from LOGFONT but recalculate lfHeight because we need
1647 // the real height in twips and not the negative number which
1648 // wxFillLogFont() returns (this is correct in general and works with
1649 // the Windows font mapper, but not here)
1651 wxFillLogFont(&lf
, &style
.GetFont());
1652 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1653 cf
.bCharSet
= lf
.lfCharSet
;
1654 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1655 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1657 // also deal with underline/italic/bold attributes: note that we must
1658 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1659 // style to allow clearing it
1662 cf
.dwEffects
|= CFE_ITALIC
;
1665 if ( lf
.lfWeight
== FW_BOLD
)
1667 cf
.dwEffects
|= CFE_BOLD
;
1670 if ( lf
.lfUnderline
)
1672 cf
.dwEffects
|= CFE_UNDERLINE
;
1675 // strikeout fonts are not supported by wxWindows
1678 if ( style
.HasTextColour() )
1680 cf
.dwMask
|= CFM_COLOR
;
1681 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1685 if ( m_verRichEdit
!= 1 && style
.HasBackgroundColour() )
1687 cf
.dwMask
|= CFM_BACKCOLOR
;
1688 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1690 #endif // wxUSE_RICHEDIT2
1692 // do format the selection
1693 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1694 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1697 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1702 // restore the original selection
1703 DoSetSelection(startOld
, endOld
, FALSE
);
1709 // ----------------------------------------------------------------------------
1711 // ----------------------------------------------------------------------------
1713 bool wxRichEditModule::OnInit()
1715 // don't do anything - we will load it when needed
1719 void wxRichEditModule::OnExit()
1721 for ( size_t i
= 0; i
< WXSIZEOF(ms_hRichEdit
); i
++ )
1723 if ( ms_hRichEdit
[i
] )
1725 ::FreeLibrary(ms_hRichEdit
[i
]);
1731 bool wxRichEditModule::Load(int version
)
1733 // we don't support loading richedit 3.0 as I don't know how to distinguish
1734 // it from 2.0 anyhow
1735 wxCHECK_MSG( version
== 1 || version
== 2, FALSE
,
1736 _T("incorrect richedit control version requested") );
1738 // make it the index in the array
1741 if ( ms_hRichEdit
[version
] == (HINSTANCE
)-1 )
1743 // we had already tried to load it and failed
1747 if ( ms_hRichEdit
[version
] )
1749 // we've already got this one
1753 wxString dllname
= version
? _T("riched20") : _T("riched32");
1754 dllname
+= _T(".dll");
1756 ms_hRichEdit
[version
] = ::LoadLibrary(dllname
);
1758 if ( !ms_hRichEdit
[version
] )
1760 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1762 ms_hRichEdit
[version
] = (HINSTANCE
)-1;
1770 #endif // wxUSE_RICHEDIT
1772 #endif // wxUSE_TEXTCTRL