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 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
104 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
);
106 #endif // wxUSE_RICHEDIT
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
114 // this module initializes RichEdit DLL if needed
115 class wxRichEditModule
: public wxModule
118 virtual bool OnInit();
119 virtual void OnExit();
121 // get the version currently loaded, -1 if none
122 static int GetLoadedVersion() { return ms_verRichEdit
; }
124 // load the richedit DLL of at least of required version
125 static bool Load(int version
= 1);
128 // the handle to richedit DLL and the version of the DLL loaded
129 static HINSTANCE ms_hRichEdit
;
131 // the DLL version loaded or -1 if none
132 static int ms_verRichEdit
;
134 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
137 HINSTANCE
wxRichEditModule::ms_hRichEdit
= (HINSTANCE
)NULL
;
138 int wxRichEditModule::ms_verRichEdit
= -1;
140 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
142 #endif // wxUSE_RICHEDIT
144 // ----------------------------------------------------------------------------
145 // event tables and other macros
146 // ----------------------------------------------------------------------------
148 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
150 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
151 EVT_CHAR(wxTextCtrl::OnChar
)
152 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
154 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
155 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
156 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
157 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
158 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
160 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
161 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
162 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
163 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
164 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
166 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
170 // ============================================================================
172 // ============================================================================
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 void wxTextCtrl::Init()
185 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
186 const wxString
& value
,
190 const wxValidator
& validator
,
191 const wxString
& name
)
193 // base initialization
194 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
198 parent
->AddChild(this);
200 // translate wxWin style flags to MSW ones, checking for consistency while
202 long msStyle
= ES_LEFT
| WS_TABSTOP
;
204 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
205 msStyle
|= WS_CLIPSIBLINGS
;
207 if ( m_windowStyle
& wxTE_MULTILINE
)
209 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
210 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
212 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
213 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
214 msStyle
|= WS_VSCROLL
;
215 m_windowStyle
|= wxTE_PROCESS_ENTER
;
219 // there is really no reason to not have this style for single line
221 msStyle
|= ES_AUTOHSCROLL
;
224 if ( m_windowStyle
& wxHSCROLL
)
225 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
227 if ( m_windowStyle
& wxTE_READONLY
)
228 msStyle
|= ES_READONLY
;
230 if ( m_windowStyle
& wxTE_PASSWORD
)
231 msStyle
|= ES_PASSWORD
;
233 if ( m_windowStyle
& wxTE_AUTO_SCROLL
)
234 msStyle
|= ES_AUTOHSCROLL
;
236 if ( m_windowStyle
& wxTE_NOHIDESEL
)
237 msStyle
|= ES_NOHIDESEL
;
239 // we always want the characters and the arrows
240 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
242 // we may have several different cases:
243 // 1. normal case: both TAB and ENTER are used for dialog navigation
244 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
245 // control in the dialog
246 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
247 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
249 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
250 m_lDlgCode
|= DLGC_WANTMESSAGE
;
251 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
252 m_lDlgCode
|= DLGC_WANTTAB
;
254 // do create the control - either an EDIT or RICHEDIT
255 wxString windowClass
= wxT("EDIT");
258 if ( m_windowStyle
& wxTE_RICH
)
260 static bool s_errorGiven
= FALSE
; // MT-FIXME
262 // only give the error msg once if the DLL can't be loaded
265 // first try to load the RichEdit DLL (will do nothing if already
267 if ( !wxRichEditModule::Load() )
269 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
277 msStyle
|= ES_AUTOVSCROLL
;
279 m_verRichEdit
= wxRichEditModule::GetLoadedVersion();
280 if ( m_verRichEdit
== 1 )
282 windowClass
= wxT("RICHEDIT");
286 #ifndef RICHEDIT_CLASS
287 wxString RICHEDIT_CLASS
;
288 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), m_verRichEdit
);
290 RICHEDIT_CLASS
+= _T('W');
292 RICHEDIT_CLASS
+= _T('A');
293 #endif // Unicode/ANSI
294 #endif // !RICHEDIT_CLASS
296 windowClass
= RICHEDIT_CLASS
;
300 #endif // wxUSE_RICHEDIT
302 // we need to turn '\n's into "\r\n"s for the multiline controls
304 if ( m_windowStyle
& wxTE_MULTILINE
)
306 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
313 if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) )
316 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
321 // have to enable events manually
322 LPARAM mask
= ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
;
324 if ( m_windowStyle
& wxTE_AUTO_URL
)
328 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0);
331 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
);
333 #endif // wxUSE_RICHEDIT
338 // Make sure the window style (etc.) reflects the HWND style (roughly)
339 void wxTextCtrl::AdoptAttributesFromHWND()
341 wxWindow::AdoptAttributesFromHWND();
343 HWND hWnd
= GetHwnd();
344 long style
= ::GetWindowLong(hWnd
, GWL_STYLE
);
346 // retrieve the style to see whether this is an edit or richedit ctrl
348 wxString classname
= wxGetWindowClass(GetHWND());
350 if ( classname
.IsSameAs(_T("EDIT"), FALSE
/* no case */) )
357 if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 )
359 wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str());
364 #endif // wxUSE_RICHEDIT
366 if (style
& ES_MULTILINE
)
367 m_windowStyle
|= wxTE_MULTILINE
;
368 if (style
& ES_PASSWORD
)
369 m_windowStyle
|= wxTE_PASSWORD
;
370 if (style
& ES_READONLY
)
371 m_windowStyle
|= wxTE_READONLY
;
372 if (style
& ES_WANTRETURN
)
373 m_windowStyle
|= wxTE_PROCESS_ENTER
;
376 // ----------------------------------------------------------------------------
377 // set/get the controls text
378 // ----------------------------------------------------------------------------
380 wxString
wxTextCtrl::GetValue() const
382 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
383 // retrieving more than 64Kb under Win9x
389 int len
= GetWindowTextLength(GetHwnd());
392 // alloc one extra WORD as needed by the control
393 wxChar
*p
= str
.GetWriteBuf(++len
);
396 textRange
.chrg
.cpMin
= 0;
397 textRange
.chrg
.cpMax
= -1;
398 textRange
.lpstrText
= p
;
400 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
402 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
403 // the newlines which is neither Unix nor Windows style (Win95 with
404 // riched20.dll shows this behaviour) - convert it to something
408 if ( *p
== _T('\r') )
414 //else: no text at all, leave the string empty
418 #endif // wxUSE_RICHEDIT
420 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
421 // same one as above for consitency
422 wxString str
= wxGetWindowText(GetHWND());
424 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
427 void wxTextCtrl::SetValue(const wxString
& value
)
429 // if the text is long enough, it's faster to just set it instead of first
430 // comparing it with the old one (chances are that it will be different
431 // anyhow, this comparison is there to avoid flicker for small single-line
432 // edit controls mostly)
433 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
435 // it is simpler to do this but it could be more efficient to reproduce
436 // WriteText() logic here
441 // mark the control as being not dirty - we changed its text, not the
445 // for compatibility, don't move the cursor when doing SetValue()
446 SetInsertionPoint(0);
450 #if wxUSE_RICHEDIT && !wxUSE_UNICODE
452 DWORD CALLBACK
wxRichEditStreamIn(DWORD dwCookie
, BYTE
*buf
, LONG cb
, LONG
*pcb
)
456 wchar_t *wbuf
= (wchar_t *)buf
;
457 const wchar_t *wpc
= *(const wchar_t **)dwCookie
;
462 cb
-= sizeof(wchar_t);
463 (*pcb
) += sizeof(wchar_t);
466 *(const wchar_t **)dwCookie
= wpc
;
471 extern long wxEncodingToCodepage(wxFontEncoding encoding
); // from strconv.cpp
473 bool wxTextCtrl::StreamIn(const wxString
& value
, wxFontEncoding encoding
)
475 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
476 // text in the non default charset - otherwise it thinks it knows better
477 // than we do and always shows it in the default one
479 // first get the Windows code page for this encoding
480 long codepage
= wxEncodingToCodepage(encoding
);
481 if ( codepage
== -1 )
487 // next translate to Unicode using this code page
488 int len
= ::MultiByteToWideChar(codepage
, 0, value
, -1, NULL
, 0);
489 wxWCharBuffer
wchBuf(len
);
490 if ( !::MultiByteToWideChar(codepage
, 0, value
, -1,
491 (wchar_t *)wchBuf
.data(), len
) )
493 wxLogLastError(_T("MultiByteToWideChar"));
496 // finally, stream it in the control
497 const wchar_t *wpc
= wchBuf
;
501 eds
.dwCookie
= (DWORD
)&wpc
;
502 // the cast below is needed for broken (very) old mingw32 headers
503 eds
.pfnCallback
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
;
505 if ( !::SendMessage(GetHwnd(), EM_STREAMIN
,
506 SF_TEXT
| SF_UNICODE
| SFF_SELECTION
,
507 (LPARAM
)&eds
) || eds
.dwError
)
509 wxLogLastError(_T("EM_STREAMIN"));
517 #endif // wxUSE_RICHEDIT
519 void wxTextCtrl::WriteText(const wxString
& value
)
521 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
524 // there are several complications with the rich edit controls here
528 // first, ensure that the new text will be in the default style
529 if ( !m_defaultStyle
.IsDefault() )
532 GetSelection(&start
, &end
);
533 SetStyle(start
, end
, m_defaultStyle
);
537 // next check if the text we're inserting must be shown in a non
538 // default charset -- this only works for RichEdit > 1.0
539 if ( GetRichVersion() > 1 )
541 wxFont font
= m_defaultStyle
.GetFont();
547 wxFontEncoding encoding
= font
.GetEncoding();
548 if ( encoding
!= wxFONTENCODING_SYSTEM
)
550 done
= StreamIn(valueDos
, encoding
);
554 #endif // !wxUSE_UNICODE
558 #endif // wxUSE_RICHEDIT
560 ::SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
566 void wxTextCtrl::AppendText(const wxString
& text
)
568 SetInsertionPointEnd();
573 void wxTextCtrl::Clear()
575 ::SetWindowText(GetHwnd(), wxT(""));
578 // ----------------------------------------------------------------------------
579 // Clipboard operations
580 // ----------------------------------------------------------------------------
582 void wxTextCtrl::Copy()
586 HWND hWnd
= GetHwnd();
587 SendMessage(hWnd
, WM_COPY
, 0, 0L);
591 void wxTextCtrl::Cut()
595 HWND hWnd
= GetHwnd();
596 SendMessage(hWnd
, WM_CUT
, 0, 0L);
600 void wxTextCtrl::Paste()
604 HWND hWnd
= GetHwnd();
605 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
609 bool wxTextCtrl::CanCopy() const
611 // Can copy if there's a selection
613 GetSelection(& from
, & to
);
614 return (from
!= to
) ;
617 bool wxTextCtrl::CanCut() const
619 // Can cut if there's a selection
621 GetSelection(& from
, & to
);
622 return (from
!= to
) && (IsEditable());
625 bool wxTextCtrl::CanPaste() const
633 UINT cf
= 0; // 0 == any format
635 return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0;
637 #endif // wxUSE_RICHEDIT
639 // Standard edit control: check for straight text on clipboard
640 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
643 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
646 return isTextAvailable
;
649 // ----------------------------------------------------------------------------
651 // ----------------------------------------------------------------------------
653 void wxTextCtrl::SetEditable(bool editable
)
655 HWND hWnd
= GetHwnd();
656 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
659 void wxTextCtrl::SetInsertionPoint(long pos
)
661 SetSelection(pos
, pos
);
664 void wxTextCtrl::SetInsertionPointEnd()
666 long pos
= GetLastPosition();
667 SetInsertionPoint(pos
);
670 long wxTextCtrl::GetInsertionPoint() const
678 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
681 #endif // wxUSE_RICHEDIT
683 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
687 long wxTextCtrl::GetLastPosition() const
689 HWND hWnd
= GetHwnd();
691 // Will always return a number > 0 (according to docs)
692 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
694 // This gets the char index for the _beginning_ of the last line
695 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
697 // Get number of characters in the last line. We'll add this to the character
698 // index for the last line, 1st position.
699 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
701 return (long)(charIndex
+ lineLength
);
704 // If the return values from and to are the same, there is no
706 void wxTextCtrl::GetSelection(long* from
, long* to
) const
712 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
);
714 *from
= charRange
.cpMin
;
715 *to
= charRange
.cpMax
;
718 #endif // !wxUSE_RICHEDIT
720 DWORD dwStart
, dwEnd
;
721 ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
728 wxString
wxTextCtrl::GetStringSelection() const
730 // the base class version works correctly for the rich text controls
731 // because there the lines are terminated with just '\r' which means that
732 // the string length is not changed in the result of the translations doen
733 // in GetValue() but for the normal ones when we replace "\r\n" with '\n'
734 // we break the indices
737 return wxTextCtrlBase::GetStringSelection();
738 #endif // wxUSE_RICHEDIT
741 GetSelection(&from
, &to
);
746 str
= wxGetWindowText(GetHWND()).Mid(from
, to
- from
);
748 // and now that we have the correct selection, convert it to the
750 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
756 bool wxTextCtrl::IsEditable() const
758 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
760 return ((style
& ES_READONLY
) == 0);
763 // ----------------------------------------------------------------------------
765 // ----------------------------------------------------------------------------
767 void wxTextCtrl::SetSelection(long from
, long to
)
769 DoSetSelection(from
, to
);
772 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
774 // if from and to are both -1, it means (in wxWindows) that all text should
775 // be selected - translate into Windows convention
776 if ( (from
== -1) && (to
== -1) )
782 HWND hWnd
= GetHwnd();
791 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
794 #endif // wxUSE_RICHEDIT
796 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
802 // EM_SCROLLCARET doesn't work for me with the richedit controls
803 // unless it has focus although I don't see any mention of it in the
804 // docs - this is the only workaround I found. Of course it's not
805 // perfect as it adds the dummy focus set/kill events but it's still
806 // better than nothing
810 hwndFocusOld
= ::GetFocus();
811 if ( hwndFocusOld
== GetHwnd() )
813 // don't change focus
819 ::SetFocus(GetHwnd());
826 #endif // wxUSE_RICHEDIT
828 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
833 ::SetFocus(hwndFocusOld
);
835 #endif // wxUSE_RICHEDIT
838 // WPARAM is 0: selection is scrolled into view
839 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(from
, to
));
843 // ----------------------------------------------------------------------------
845 // ----------------------------------------------------------------------------
847 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
849 // Set selection and remove it
850 DoSetSelection(from
, to
, FALSE
/* don't scroll caret into view */);
852 SendMessage(GetHwnd(), EM_REPLACESEL
,
858 (LPARAM
)value
.c_str());
861 void wxTextCtrl::Remove(long from
, long to
)
863 Replace(from
, to
, _T(""));
866 bool wxTextCtrl::LoadFile(const wxString
& file
)
868 if ( wxTextCtrlBase::LoadFile(file
) )
870 // update the size limit if needed
879 bool wxTextCtrl::IsModified() const
881 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
884 // Makes 'unmodified'
885 void wxTextCtrl::DiscardEdits()
887 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
890 int wxTextCtrl::GetNumberOfLines() const
892 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
895 long wxTextCtrl::XYToPosition(long x
, long y
) const
897 HWND hWnd
= GetHwnd();
899 // This gets the char index for the _beginning_ of this line
900 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
901 return (long)(x
+ charIndex
);
904 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
906 HWND hWnd
= GetHwnd();
908 // This gets the line number containing the character
913 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
916 #endif // wxUSE_RICHEDIT
917 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
925 // This gets the char index for the _beginning_ of this line
926 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
927 if ( charIndex
== -1 )
932 // The X position must therefore be the different between pos and charIndex
934 *x
= (long)(pos
- charIndex
);
941 void wxTextCtrl::ShowPosition(long pos
)
943 HWND hWnd
= GetHwnd();
945 // To scroll to a position, we pass the number of lines and characters
946 // to scroll *by*. This means that we need to:
947 // (1) Find the line position of the current line.
948 // (2) Find the line position of pos.
949 // (3) Scroll by (pos - current).
950 // For now, ignore the horizontal scrolling.
952 // Is this where scrolling is relative to - the line containing the caret?
953 // Or is the first visible line??? Try first visible line.
954 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
956 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
958 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
960 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
962 if (linesToScroll
!= 0)
963 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
966 int wxTextCtrl::GetLineLength(long lineNo
) const
968 long charIndex
= XYToPosition(0, lineNo
);
969 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
973 wxString
wxTextCtrl::GetLineText(long lineNo
) const
975 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
977 // there must be at least enough place for the length WORD in the
982 wxChar
*buf
= str
.GetWriteBuf(len
);
984 *(WORD
*)buf
= (WORD
)len
;
985 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
988 str
.UngetWriteBuf(len
);
993 void wxTextCtrl::SetMaxLength(unsigned long len
)
995 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0);
998 // ----------------------------------------------------------------------------
1000 // ----------------------------------------------------------------------------
1002 void wxTextCtrl::Undo()
1006 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1010 void wxTextCtrl::Redo()
1014 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1015 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
1019 bool wxTextCtrl::CanUndo() const
1021 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
1024 bool wxTextCtrl::CanRedo() const
1026 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
1029 // ----------------------------------------------------------------------------
1030 // implemenation details
1031 // ----------------------------------------------------------------------------
1033 void wxTextCtrl::Command(wxCommandEvent
& event
)
1035 SetValue(event
.GetString());
1036 ProcessCommand (event
);
1039 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1041 // By default, load the first file into the text window.
1042 if (event
.GetNumberOfFiles() > 0)
1044 LoadFile(event
.GetFiles()[0]);
1048 // ----------------------------------------------------------------------------
1049 // kbd input processing
1050 // ----------------------------------------------------------------------------
1052 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
1054 MSG
*msg
= (MSG
*)pMsg
;
1056 // check for our special keys here: if we don't do it and the parent frame
1057 // uses them as accelerators, they wouldn't work at all, so we disable
1058 // usual preprocessing for them
1059 if ( msg
->message
== WM_KEYDOWN
)
1061 WORD vkey
= msg
->wParam
;
1062 if ( (HIWORD(msg
->lParam
) & KF_ALTDOWN
) == KF_ALTDOWN
)
1064 if ( vkey
== VK_BACK
)
1069 if ( wxIsCtrlDown() )
1083 else if ( wxIsShiftDown() )
1085 if ( vkey
== VK_INSERT
|| vkey
== VK_DELETE
)
1091 return wxControl::MSWShouldPreProcessMessage(pMsg
);
1094 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1096 switch ( event
.KeyCode() )
1099 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1101 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1102 InitCommandEvent(event
);
1103 event
.SetString(GetValue());
1104 if ( GetEventHandler()->ProcessEvent(event
) )
1107 //else: multiline controls need Enter for themselves
1112 // always produce navigation event - even if we process TAB
1113 // ourselves the fact that we got here means that the user code
1114 // decided to skip processing of this TAB - probably to let it
1115 // do its default job.
1117 wxNavigationKeyEvent eventNav
;
1118 eventNav
.SetDirection(!event
.ShiftDown());
1119 eventNav
.SetWindowChange(event
.ControlDown());
1120 eventNav
.SetEventObject(this);
1122 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1128 // no, we didn't process it
1132 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1139 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1142 event
.SetEventObject( this );
1143 GetEventHandler()->ProcessEvent(event
);
1149 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1150 InitCommandEvent(event
);
1151 event
.SetString(GetValue());
1152 ProcessCommand(event
);
1157 // the text size limit has been hit - increase it
1158 if ( !AdjustSpaceLimit() )
1160 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1161 InitCommandEvent(event
);
1162 event
.SetString(GetValue());
1163 ProcessCommand(event
);
1167 // the other notification messages are not processed
1181 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
1187 WXUINT
WXUNUSED(message
),
1188 WXWPARAM
WXUNUSED(wParam
),
1189 WXLPARAM
WXUNUSED(lParam
)
1196 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1197 return (WXHBRUSH
) hbrush
;
1199 #endif // wxUSE_CTL3D
1202 if (GetParent()->GetTransparentBackground())
1203 SetBkMode(hdc
, TRANSPARENT
);
1205 SetBkMode(hdc
, OPAQUE
);
1207 wxColour colBack
= GetBackgroundColour();
1209 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1210 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
1212 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1213 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1215 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1217 return (WXHBRUSH
)brush
->GetResourceHandle();
1220 // In WIN16, need to override normal erasing because
1221 // Ctl3D doesn't use the wxWindows background colour.
1223 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1225 wxColour
col(m_backgroundColour
);
1229 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1233 ::GetClientRect(GetHwnd(), &rect
);
1235 COLORREF ref
= PALETTERGB(col
.Red(),
1238 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1240 wxLogLastError(wxT("CreateSolidBrush"));
1242 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1244 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1246 ::FillRect(hdc
, &rect
, hBrush
);
1247 ::DeleteObject(hBrush
);
1248 ::SetMapMode(hdc
, mode
);
1253 bool wxTextCtrl::AdjustSpaceLimit()
1256 unsigned int limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1258 // HACK: we try to automatically extend the limit for the amount of text
1259 // to allow (interactively) entering more than 64Kb of text under
1260 // Win9x but we shouldn't reset the text limit which was previously
1261 // set explicitly with SetMaxLength()
1263 // we could solve this by storing the limit we set in wxTextCtrl but
1264 // to save space we prefer to simply test here the actual limit
1265 // value: we consider that SetMaxLength() can only be called for
1267 if ( limit
< 0x8000 )
1269 // we've got more text than limit set by SetMaxLength()
1273 unsigned int len
= ::GetWindowTextLength(GetHwnd());
1276 limit
= len
+ 0x8000; // 32Kb
1281 // as a nice side effect, this also allows passing limit > 64Kb
1282 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1285 #endif // wxUSE_RICHEDIT
1287 if ( limit
> 0xffff )
1289 // this will set it to a platform-dependent maximum (much more
1290 // than 64Kb under NT)
1294 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1299 // we changed the limit
1303 bool wxTextCtrl::AcceptsFocus() const
1305 // we don't want focus if we can't be edited
1306 return IsEditable() && wxControl::AcceptsFocus();
1309 wxSize
wxTextCtrl::DoGetBestSize() const
1312 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1314 int wText
= DEFAULT_ITEM_WIDTH
;
1316 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1317 if ( m_windowStyle
& wxTE_MULTILINE
)
1319 hText
*= wxMax(GetNumberOfLines(), 5);
1321 //else: for single line control everything is ok
1323 return wxSize(wText
, hText
);
1326 // ----------------------------------------------------------------------------
1327 // standard handlers for standard edit menu events
1328 // ----------------------------------------------------------------------------
1330 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1335 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1340 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1345 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1350 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1355 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1357 event
.Enable( CanCut() );
1360 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1362 event
.Enable( CanCopy() );
1365 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1367 event
.Enable( CanPaste() );
1370 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1372 event
.Enable( CanUndo() );
1375 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1377 event
.Enable( CanRedo() );
1380 // the rest of the file only deals with the rich edit controls
1383 // ----------------------------------------------------------------------------
1384 // EN_LINK processing
1385 // ----------------------------------------------------------------------------
1387 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1389 NMHDR
*hdr
= (NMHDR
* )lParam
;
1390 if ( hdr
->code
== EN_LINK
)
1392 ENLINK
*enlink
= (ENLINK
*)hdr
;
1394 switch ( enlink
->msg
)
1397 // ok, so it is hardcoded - do we really nee to customize it?
1398 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND
)));
1403 case WM_LBUTTONDOWN
:
1405 case WM_LBUTTONDBLCLK
:
1406 case WM_RBUTTONDOWN
:
1408 case WM_RBUTTONDBLCLK
:
1409 // send a mouse event
1411 static const wxEventType eventsMouse
[] =
1422 // the event ids are consecutive
1424 evtMouse(eventsMouse
[enlink
->msg
- WM_MOUSEMOVE
]);
1426 InitMouseEvent(evtMouse
,
1427 GET_X_LPARAM(enlink
->lParam
),
1428 GET_Y_LPARAM(enlink
->lParam
),
1431 wxTextUrlEvent
event(m_windowId
, evtMouse
,
1433 enlink
->chrg
.cpMax
);
1435 InitCommandEvent(event
);
1437 *result
= ProcessCommand(event
);
1449 // ----------------------------------------------------------------------------
1450 // colour setting for the rich edit controls
1451 // ----------------------------------------------------------------------------
1453 // Watcom C++ doesn't define this
1455 #define SCF_ALL 0x0004
1458 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1460 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1462 // colour didn't really change
1468 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1469 // EM_SETBKGNDCOLOR additionally
1470 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1476 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1478 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1480 // colour didn't really change
1486 // change the colour of everything
1489 cf
.cbSize
= sizeof(cf
);
1490 cf
.dwMask
= CFM_COLOR
;
1491 cf
.crTextColor
= wxColourToRGB(colour
);
1492 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1498 // ----------------------------------------------------------------------------
1499 // styling support for rich edit controls
1500 // ----------------------------------------------------------------------------
1502 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1506 // can't do it with normal text control
1510 // the rich text control doesn't handle setting background colour, so don't
1511 // even try if it's the only thing we want to change
1512 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1513 !style
.HasFont() && !style
.HasTextColour() )
1515 // nothing to do: return TRUE if there was really nothing to do and
1516 // FALSE if we failed to set bg colour
1517 return !style
.HasBackgroundColour();
1520 // order the range if needed
1528 // we can only change the format of the selection, so select the range we
1529 // want and restore the old selection later
1530 long startOld
, endOld
;
1531 GetSelection(&startOld
, &endOld
);
1533 // but do we really have to change the selection?
1534 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1538 DoSetSelection(start
, end
, FALSE
/* don't scroll caret into view */);
1541 // initialize CHARFORMAT struct
1548 cf
.cbSize
= sizeof(cf
);
1550 if ( style
.HasFont() )
1552 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1553 // but using it doesn't seem to hurt neither so leaving it for now
1555 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
|
1556 CFM_ITALIC
| CFM_BOLD
| CFM_UNDERLINE
;
1558 // fill in data from LOGFONT but recalculate lfHeight because we need
1559 // the real height in twips and not the negative number which
1560 // wxFillLogFont() returns (this is correct in general and works with
1561 // the Windows font mapper, but not here)
1563 wxFillLogFont(&lf
, &style
.GetFont());
1564 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1565 cf
.bCharSet
= lf
.lfCharSet
;
1566 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1567 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1569 // also deal with underline/italic/bold attributes: note that we must
1570 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1571 // style to allow clearing it
1574 cf
.dwEffects
|= CFE_ITALIC
;
1577 if ( lf
.lfWeight
== FW_BOLD
)
1579 cf
.dwEffects
|= CFE_BOLD
;
1582 if ( lf
.lfUnderline
)
1584 cf
.dwEffects
|= CFE_UNDERLINE
;
1587 // strikeout fonts are not supported by wxWindows
1590 if ( style
.HasTextColour() )
1592 cf
.dwMask
|= CFM_COLOR
;
1593 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1597 if ( wxRichEditModule::GetLoadedVersion() > 1 && style
.HasBackgroundColour() )
1599 cf
.dwMask
|= CFM_BACKCOLOR
;
1600 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1602 #endif // wxUSE_RICHEDIT2
1604 // do format the selection
1605 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1606 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1609 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1614 // restore the original selection
1615 DoSetSelection(startOld
, endOld
, FALSE
);
1621 // ----------------------------------------------------------------------------
1623 // ----------------------------------------------------------------------------
1625 bool wxRichEditModule::OnInit()
1627 // don't do anything - we will load it when needed
1631 void wxRichEditModule::OnExit()
1635 FreeLibrary(ms_hRichEdit
);
1640 bool wxRichEditModule::Load(int version
)
1642 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1643 _T("incorrect richedit control version requested") );
1645 if ( version
<= ms_verRichEdit
)
1647 // we've already got this or better
1653 ::FreeLibrary(ms_hRichEdit
);
1656 // always try load riched20.dll first - like this we won't have to reload
1657 // it later if we're first asked for RE 1 and then for RE 2 or 3
1658 wxString dllname
= _T("riched20.dll");
1659 ms_hRichEdit
= ::LoadLibrary(dllname
);
1660 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1662 if ( !ms_hRichEdit
&& (version
== 1) )
1664 // fall back to RE 1
1665 dllname
= _T("riched32.dll");
1666 ms_hRichEdit
= ::LoadLibrary(dllname
);
1670 if ( !ms_hRichEdit
)
1672 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1674 ms_verRichEdit
= -1;
1682 #endif // wxUSE_RICHEDIT
1684 #endif // wxUSE_TEXTCTRL