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"
32 #include "wx/textctrl.h"
33 #include "wx/settings.h"
41 #include "wx/module.h"
44 #include "wx/clipbrd.h"
47 #include "wx/textfile.h"
51 #include "wx/msw/private.h"
55 #include <sys/types.h>
57 #if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__))
61 // old mingw32 doesn't define this
63 #define CFM_CHARSET 0x08000000
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
72 // this module initializes RichEdit DLL if needed
73 class wxRichEditModule
: public wxModule
76 virtual bool OnInit();
77 virtual void OnExit();
79 // get the version currently loaded, -1 if none
80 static int GetLoadedVersion() { return ms_verRichEdit
; }
82 // load the richedit DLL of at least of required version
83 static bool Load(int version
= 1);
86 // the handle to richedit DLL and the version of the DLL loaded
87 static HINSTANCE ms_hRichEdit
;
89 // the DLL version loaded or -1 if none
90 static int ms_verRichEdit
;
92 DECLARE_DYNAMIC_CLASS(wxRichEditModule
)
95 HINSTANCE
wxRichEditModule::ms_hRichEdit
= (HINSTANCE
)NULL
;
96 int wxRichEditModule::ms_verRichEdit
= -1;
98 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
)
100 #endif // wxUSE_RICHEDIT
102 // ----------------------------------------------------------------------------
103 // event tables and other macros
104 // ----------------------------------------------------------------------------
106 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
108 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
109 EVT_CHAR(wxTextCtrl::OnChar
)
110 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
112 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
113 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
114 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
115 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
116 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
118 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
119 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
120 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
121 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
122 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
124 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
128 // ============================================================================
130 // ============================================================================
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxTextCtrl::wxTextCtrl()
143 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
144 const wxString
& value
,
148 const wxValidator
& validator
,
149 const wxString
& name
)
151 // base initialization
152 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
156 parent
->AddChild(this);
158 // translate wxWin style flags to MSW ones, checking for consistency while
160 long msStyle
= ES_LEFT
| WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
162 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
163 msStyle
|= WS_CLIPSIBLINGS
;
165 if ( m_windowStyle
& wxTE_MULTILINE
)
167 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
168 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
170 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
171 if ((m_windowStyle
& wxTE_NO_VSCROLL
) == 0)
172 msStyle
|= WS_VSCROLL
;
173 m_windowStyle
|= wxTE_PROCESS_ENTER
;
176 msStyle
|= ES_AUTOHSCROLL
;
178 if (m_windowStyle
& wxHSCROLL
)
179 msStyle
|= (WS_HSCROLL
| ES_AUTOHSCROLL
);
181 if (m_windowStyle
& wxTE_READONLY
)
182 msStyle
|= ES_READONLY
;
184 if (m_windowStyle
& wxTE_PASSWORD
) // hidden input
185 msStyle
|= ES_PASSWORD
;
187 if (m_windowStyle
& wxTE_AUTO_SCROLL
)
188 msStyle
|= ES_AUTOHSCROLL
;
191 // we always want the characters and the arrows
192 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
194 // we may have several different cases:
195 // 1. normal case: both TAB and ENTER are used for dialog navigation
196 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
197 // control in the dialog
198 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
199 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
201 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
202 m_lDlgCode
|= DLGC_WANTMESSAGE
;
203 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
204 m_lDlgCode
|= DLGC_WANTTAB
;
206 // do create the control - either an EDIT or RICHEDIT
207 wxString windowClass
= wxT("EDIT");
210 if ( m_windowStyle
& wxTE_RICH
)
212 static bool s_errorGiven
= FALSE
; // MT-FIXME
214 // only give the error msg once if the DLL can't be loaded
217 // first try to load the RichEdit DLL (will do nothing if already
219 if ( !wxRichEditModule::Load() )
221 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
233 msStyle
|= ES_AUTOVSCROLL
;
236 int ver
= wxRichEditModule::GetLoadedVersion();
239 windowClass
= wxT("RICHEDIT");
243 #ifndef RICHEDIT_CLASS
244 wxString RICHEDIT_CLASS
;
245 RICHEDIT_CLASS
.Printf(_T("RichEdit%d0"), ver
);
247 RICHEDIT_CLASS
+= _T('W');
249 RICHEDIT_CLASS
+= _T('A');
250 #endif // Unicode/ANSI
251 #endif // !RICHEDIT_CLASS
253 windowClass
= RICHEDIT_CLASS
;
259 #endif // wxUSE_RICHEDIT
262 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
264 // Even with extended styles, need to combine with WS_BORDER for them to
266 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
267 msStyle
|= WS_BORDER
;
269 // NB: don't use pos and size as CreateWindowEx arguments because they
270 // might be -1 in which case we should use the default values (and
271 // SetSize called below takes care of it)
272 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
,
282 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create text ctrl") );
287 Ctl3dSubclassCtl(GetHwnd());
295 // Have to enable events
296 ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0,
297 ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
);
301 SubclassWin(GetHWND());
303 // set font, position, size and initial value
304 wxFont
& fontParent
= parent
->GetFont();
305 if ( fontParent
.Ok() )
311 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
314 // Causes a crash for Symantec C++ and WIN32 for some reason
315 #if !(defined(__SC__) && defined(__WIN32__))
316 if ( !value
.IsEmpty() )
325 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
330 // Make sure the window style (etc.) reflects the HWND style (roughly)
331 void wxTextCtrl::AdoptAttributesFromHWND()
333 wxWindow::AdoptAttributesFromHWND();
335 HWND hWnd
= GetHwnd();
336 long style
= GetWindowLong(hWnd
, GWL_STYLE
);
338 // retrieve the style to see whether this is an edit or richedit ctrl
342 GetClassName(hWnd
, buf
, WXSIZEOF(buf
));
344 if ( wxStricmp(buf
, wxT("EDIT")) == 0 )
348 #endif // wxUSE_RICHEDIT
350 if (style
& ES_MULTILINE
)
351 m_windowStyle
|= wxTE_MULTILINE
;
352 if (style
& ES_PASSWORD
)
353 m_windowStyle
|= wxTE_PASSWORD
;
354 if (style
& ES_READONLY
)
355 m_windowStyle
|= wxTE_READONLY
;
356 if (style
& ES_WANTRETURN
)
357 m_windowStyle
|= wxTE_PROCESS_ENTER
;
360 void wxTextCtrl::SetupColours()
362 wxColour bkgndColour
;
363 // if (IsEditable() || (m_windowStyle & wxTE_MULTILINE))
364 bkgndColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
366 // bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
368 SetBackgroundColour(bkgndColour
);
369 SetForegroundColour(GetParent()->GetForegroundColour());
372 // ----------------------------------------------------------------------------
373 // set/get the controls text
374 // ----------------------------------------------------------------------------
376 wxString
wxTextCtrl::GetValue() const
378 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
379 // retrieving more than 64Kb under Win9x
385 int len
= GetWindowTextLength(GetHwnd());
388 // alloc one extra WORD as needed by the control
389 wxChar
*p
= str
.GetWriteBuf(++len
);
392 textRange
.chrg
.cpMin
= 0;
393 textRange
.chrg
.cpMax
= -1;
394 textRange
.lpstrText
= p
;
396 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 0, (LPARAM
)&textRange
);
398 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
399 // the newlines which is neither Unix nor Windows style (Win95 with
400 // riched20.dll shows this behaviour) - convert it to something
404 if ( *p
== _T('\r') )
410 //else: no text at all, leave the string empty
414 #endif // wxUSE_RICHEDIT
416 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
417 // same one as above for consitency
418 wxString str
= wxGetWindowText(GetHWND());
420 return wxTextFile::Translate(str
, wxTextFileType_Unix
);
423 void wxTextCtrl::SetValue(const wxString
& value
)
425 // if the text is long enough, it's faster to just set it instead of first
426 // comparing it with the old one (chances are that it will be different
427 // anyhow, this comparison is there to avoid flicker for small single-line
428 // edit controls mostly)
429 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
431 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
433 SetWindowText(GetHwnd(), valueDos
.c_str());
435 // for compatibility with the GTK and because it is more logical, we
436 // move the cursor to the end of the text after SetValue()
438 // GRG, Jun/2000: Changed this back after a lot of discussion
439 // in the lists. wxWindows 2.2 will have a set of flags to
440 // customize this behaviour.
441 //SetInsertionPointEnd();
447 void wxTextCtrl::WriteText(const wxString
& value
)
449 wxString valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
452 // ensure that the new text will be in the default style
454 (m_defaultStyle
.HasFont() || m_defaultStyle
.HasTextColour()) )
457 GetSelection(&start
, &end
);
458 SetStyle(start
, end
, m_defaultStyle
);
460 #endif // wxUSE_RICHEDIT
462 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)valueDos
.c_str());
467 void wxTextCtrl::AppendText(const wxString
& text
)
469 SetInsertionPointEnd();
473 void wxTextCtrl::Clear()
475 SetWindowText(GetHwnd(), wxT(""));
478 // ----------------------------------------------------------------------------
479 // Clipboard operations
480 // ----------------------------------------------------------------------------
482 void wxTextCtrl::Copy()
486 HWND hWnd
= GetHwnd();
487 SendMessage(hWnd
, WM_COPY
, 0, 0L);
491 void wxTextCtrl::Cut()
495 HWND hWnd
= GetHwnd();
496 SendMessage(hWnd
, WM_CUT
, 0, 0L);
500 void wxTextCtrl::Paste()
504 HWND hWnd
= GetHwnd();
505 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
509 bool wxTextCtrl::CanCopy() const
511 // Can copy if there's a selection
513 GetSelection(& from
, & to
);
514 return (from
!= to
) ;
517 bool wxTextCtrl::CanCut() const
519 // Can cut if there's a selection
521 GetSelection(& from
, & to
);
522 return (from
!= to
) && (IsEditable());
525 bool wxTextCtrl::CanPaste() const
530 int dataFormat
= 0; // 0 == any format
531 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
537 // Standard edit control: check for straight text on clipboard
538 bool isTextAvailable
= FALSE
;
539 if ( ::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
541 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
545 return isTextAvailable
;
548 // ----------------------------------------------------------------------------
550 // ----------------------------------------------------------------------------
552 void wxTextCtrl::SetEditable(bool editable
)
554 HWND hWnd
= GetHwnd();
555 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
558 void wxTextCtrl::SetInsertionPoint(long pos
)
560 HWND hWnd
= GetHwnd();
568 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
569 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
572 #endif // wxUSE_RICHEDIT
574 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
575 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
578 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
581 static const wxChar
*nothing
= _T("");
582 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
585 void wxTextCtrl::SetInsertionPointEnd()
587 long pos
= GetLastPosition();
588 SetInsertionPoint(pos
);
591 long wxTextCtrl::GetInsertionPoint() const
599 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
604 DWORD Pos
= (DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
608 long wxTextCtrl::GetLastPosition() const
610 HWND hWnd
= GetHwnd();
612 // Will always return a number > 0 (according to docs)
613 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
615 // This gets the char index for the _beginning_ of the last line
616 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
618 // Get number of characters in the last line. We'll add this to the character
619 // index for the last line, 1st position.
620 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
622 return (long)(charIndex
+ lineLength
);
625 // If the return values from and to are the same, there is no
627 void wxTextCtrl::GetSelection(long* from
, long* to
) const
633 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
635 *from
= charRange
.cpMin
;
636 *to
= charRange
.cpMax
;
641 DWORD dwStart
, dwEnd
;
642 WPARAM wParam
= (WPARAM
) &dwStart
; // receives starting position
643 LPARAM lParam
= (LPARAM
) &dwEnd
; // receives ending position
645 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
652 bool wxTextCtrl::IsEditable() const
654 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
656 return ((style
& ES_READONLY
) == 0);
659 // ----------------------------------------------------------------------------
661 // ----------------------------------------------------------------------------
663 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
666 HWND hWnd
= GetHwnd();
667 long fromChar
= from
;
670 // Set selection and remove it
672 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
674 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
676 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
678 // Now replace with 'value', by pasting.
679 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const wxChar
*)value
, 0, 0);
681 // Paste into edit control
682 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
684 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
688 void wxTextCtrl::Remove(long from
, long to
)
690 HWND hWnd
= GetHwnd();
691 long fromChar
= from
;
694 // Cut all selected text
696 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
698 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
700 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
703 void wxTextCtrl::SetSelection(long from
, long to
)
705 HWND hWnd
= GetHwnd();
706 long fromChar
= from
;
709 // if from and to are both -1, it means (in wxWindows) that all text should
710 // be selected. Translate into Windows convention
711 if ((from
== -1) && (to
== -1))
718 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
719 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
721 // WPARAM is 0: selection is scrolled into view
722 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
726 bool wxTextCtrl::LoadFile(const wxString
& file
)
728 if ( wxTextCtrlBase::LoadFile(file
) )
730 // update the size limit if needed
739 bool wxTextCtrl::IsModified() const
741 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
744 // Makes 'unmodified'
745 void wxTextCtrl::DiscardEdits()
747 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
750 int wxTextCtrl::GetNumberOfLines() const
752 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
755 long wxTextCtrl::XYToPosition(long x
, long y
) const
757 HWND hWnd
= GetHwnd();
759 // This gets the char index for the _beginning_ of this line
760 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
761 return (long)(x
+ charIndex
);
764 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
766 HWND hWnd
= GetHwnd();
768 // This gets the line number containing the character
773 lineNo
= (int)SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
);
776 #endif // wxUSE_RICHEDIT
777 lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
785 // This gets the char index for the _beginning_ of this line
786 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
787 if ( charIndex
== -1 )
792 // The X position must therefore be the different between pos and charIndex
794 *x
= (long)(pos
- charIndex
);
801 void wxTextCtrl::ShowPosition(long pos
)
803 HWND hWnd
= GetHwnd();
805 // To scroll to a position, we pass the number of lines and characters
806 // to scroll *by*. This means that we need to:
807 // (1) Find the line position of the current line.
808 // (2) Find the line position of pos.
809 // (3) Scroll by (pos - current).
810 // For now, ignore the horizontal scrolling.
812 // Is this where scrolling is relative to - the line containing the caret?
813 // Or is the first visible line??? Try first visible line.
814 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
816 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
818 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
820 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
822 if (linesToScroll
!= 0)
823 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
826 int wxTextCtrl::GetLineLength(long lineNo
) const
828 long charIndex
= XYToPosition(0, lineNo
);
829 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
833 wxString
wxTextCtrl::GetLineText(long lineNo
) const
835 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
837 // there must be at least enough place for the length WORD in the
842 wxChar
*buf
= str
.GetWriteBuf(len
);
844 *(WORD
*)buf
= (WORD
)len
;
845 len
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
848 str
.UngetWriteBuf(len
);
853 // ----------------------------------------------------------------------------
855 // ----------------------------------------------------------------------------
857 void wxTextCtrl::Undo()
861 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
865 void wxTextCtrl::Redo()
869 // Same as Undo, since Undo undoes the undo, i.e. a redo.
870 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
874 bool wxTextCtrl::CanUndo() const
876 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
879 bool wxTextCtrl::CanRedo() const
881 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
884 // ----------------------------------------------------------------------------
885 // implemenation details
886 // ----------------------------------------------------------------------------
888 void wxTextCtrl::Command(wxCommandEvent
& event
)
890 SetValue(event
.GetString());
891 ProcessCommand (event
);
894 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
896 // By default, load the first file into the text window.
897 if (event
.GetNumberOfFiles() > 0)
899 LoadFile(event
.GetFiles()[0]);
903 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
905 switch ( event
.KeyCode() )
908 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
910 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
911 InitCommandEvent(event
);
912 event
.SetString(GetValue());
913 if ( GetEventHandler()->ProcessEvent(event
) )
916 //else: multiline controls need Enter for themselves
921 // always produce navigation event - even if we process TAB
922 // ourselves the fact that we got here means that the user code
923 // decided to skip processing of this TAB - probably to let it
924 // do its default job.
926 wxNavigationKeyEvent eventNav
;
927 eventNav
.SetDirection(!event
.ShiftDown());
928 eventNav
.SetWindowChange(event
.ControlDown());
929 eventNav
.SetEventObject(this);
931 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
937 // no, we didn't process it
941 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
948 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
951 event
.SetEventObject( this );
952 GetEventHandler()->ProcessEvent(event
);
958 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
959 InitCommandEvent(event
);
960 event
.SetString(GetValue());
961 ProcessCommand(event
);
966 // the text size limit has been hit - increase it
970 // the other notification messages are not processed
984 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
990 WXUINT
WXUNUSED(message
),
991 WXWPARAM
WXUNUSED(wParam
),
992 WXLPARAM
WXUNUSED(lParam
)
999 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1000 return (WXHBRUSH
) hbrush
;
1002 #endif // wxUSE_CTL3D
1005 if (GetParent()->GetTransparentBackground())
1006 SetBkMode(hdc
, TRANSPARENT
);
1008 SetBkMode(hdc
, OPAQUE
);
1010 wxColour colBack
= GetBackgroundColour();
1012 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE
) == 0)
1013 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
1015 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
1016 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
1018 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
1020 return (WXHBRUSH
)brush
->GetResourceHandle();
1023 // In WIN16, need to override normal erasing because
1024 // Ctl3D doesn't use the wxWindows background colour.
1026 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1028 wxColour
col(m_backgroundColour
);
1032 col
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
);
1036 ::GetClientRect(GetHwnd(), &rect
);
1038 COLORREF ref
= PALETTERGB(col
.Red(),
1041 HBRUSH hBrush
= ::CreateSolidBrush(ref
);
1043 wxLogLastError(wxT("CreateSolidBrush"));
1045 HDC hdc
= (HDC
)event
.GetDC()->GetHDC();
1047 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
1049 ::FillRect(hdc
, &rect
, hBrush
);
1050 ::DeleteObject(hBrush
);
1051 ::SetMapMode(hdc
, mode
);
1056 void wxTextCtrl::AdjustSpaceLimit()
1059 unsigned int len
= ::GetWindowTextLength(GetHwnd()),
1060 limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1063 limit
= len
+ 0x8000; // 32Kb
1068 // as a nice side effect, this also allows passing limit > 64Kb
1069 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, limit
);
1072 #endif // wxUSE_RICHEDIT
1074 if ( limit
> 0xffff )
1076 // this will set it to a platform-dependent maximum (much more
1077 // than 64Kb under NT)
1081 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1087 bool wxTextCtrl::AcceptsFocus() const
1089 // we don't want focus if we can't be edited
1090 return IsEditable() && wxControl::AcceptsFocus();
1093 wxSize
wxTextCtrl::DoGetBestSize() const
1096 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
1098 int wText
= DEFAULT_ITEM_WIDTH
;
1100 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
1101 if ( m_windowStyle
& wxTE_MULTILINE
)
1103 hText
*= wxMax(GetNumberOfLines(), 5);
1105 //else: for single line control everything is ok
1107 return wxSize(wText
, hText
);
1110 // ----------------------------------------------------------------------------
1111 // standard handlers for standard edit menu events
1112 // ----------------------------------------------------------------------------
1114 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1119 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1124 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1129 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1134 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1139 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1141 event
.Enable( CanCut() );
1144 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1146 event
.Enable( CanCopy() );
1149 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1151 event
.Enable( CanPaste() );
1154 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1156 event
.Enable( CanUndo() );
1159 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1161 event
.Enable( CanRedo() );
1164 // the rest of the file only deals with the rich edit controls
1167 // ----------------------------------------------------------------------------
1168 // colour setting for the rich edit controls
1169 // ----------------------------------------------------------------------------
1171 // Watcom C++ doesn't define this
1173 #define SCF_ALL 0x0004
1176 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1178 if ( !wxTextCtrlBase::SetBackgroundColour(colour
) )
1180 // colour didn't really change
1186 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1187 // EM_SETBKGNDCOLOR additionally
1188 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR
, 0, wxColourToRGB(colour
));
1194 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1196 if ( !wxTextCtrlBase::SetForegroundColour(colour
) )
1198 // colour didn't really change
1204 // change the colour of everything
1207 cf
.cbSize
= sizeof(cf
);
1208 cf
.dwMask
= CFM_COLOR
;
1209 cf
.crTextColor
= wxColourToRGB(colour
);
1210 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
);
1216 // ----------------------------------------------------------------------------
1217 // styling support for rich edit controls
1218 // ----------------------------------------------------------------------------
1220 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1224 // can't do it with normal text control
1228 // the rich text control doesn't handle setting background colour, so don't
1229 // even try if it's the only thing we want to change
1230 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1231 !style
.HasFont() && !style
.HasTextColour() )
1233 // nothing to do: return TRUE if there was really nothing to do and
1234 // FALSE if we failed to set bg colour
1235 return !style
.HasBackgroundColour();
1238 // order the range if needed
1246 // we can only change the format of the selection, so select the range we
1247 // want and restore the old selection later
1248 long startOld
, endOld
;
1249 GetSelection(&startOld
, &endOld
);
1251 // but do we really have to change the selection?
1252 bool changeSel
= start
!= startOld
|| end
!= endOld
;
1255 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
) start
, (LPARAM
) end
);
1257 // initialize CHARFORMAT struct
1264 cf
.cbSize
= sizeof(cf
);
1266 if ( style
.HasFont() )
1268 cf
.dwMask
|= CFM_FACE
| CFM_SIZE
| CFM_CHARSET
;
1270 // fill in data from LOGFONT but recalculate lfHeight because we need
1271 // the real height in twips and not the negative number which
1272 // wxFillLogFont() returns (this is correct in general and works with
1273 // the Windows font mapper, but not here)
1275 wxFillLogFont(&lf
, &style
.GetFont());
1276 cf
.yHeight
= 20*style
.GetFont().GetPointSize(); // 1 pt = 20 twips
1277 cf
.bCharSet
= lf
.lfCharSet
;
1278 cf
.bPitchAndFamily
= lf
.lfPitchAndFamily
;
1279 wxStrncpy( cf
.szFaceName
, lf
.lfFaceName
, WXSIZEOF(cf
.szFaceName
) );
1281 // also deal with underline/italic/bold attributes
1284 cf
.dwMask
|= CFM_ITALIC
;
1285 cf
.dwEffects
|= CFE_ITALIC
;
1288 if ( lf
.lfWeight
== FW_BOLD
)
1290 cf
.dwMask
|= CFM_BOLD
;
1291 cf
.dwEffects
|= CFE_BOLD
;
1294 if ( lf
.lfUnderline
)
1296 cf
.dwMask
|= CFM_UNDERLINE
;
1297 cf
.dwEffects
|= CFE_UNDERLINE
;
1300 // strikeout fonts are not supported by wxWindows
1303 if ( style
.HasTextColour() )
1305 cf
.dwMask
|= CFM_COLOR
;
1306 cf
.crTextColor
= wxColourToRGB(style
.GetTextColour());
1310 #ifndef CFM_BACKCOLOR
1311 #define CFM_BACKCOLOR 0x04000000
1314 if ( wxRichEditModule::GetLoadedVersion() > 1 && style
.HasBackgroundColour() )
1316 cf
.dwMask
|= CFM_BACKCOLOR
;
1317 cf
.crBackColor
= wxColourToRGB(style
.GetBackgroundColour());
1320 // do format the selection
1321 bool ok
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
,
1322 SCF_SELECTION
, (LPARAM
)&cf
) != 0;
1325 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1330 // restore the original selection
1331 SendMessage(GetHwnd(), EM_SETSEL
, (WPARAM
)startOld
, (LPARAM
)endOld
);
1337 // ----------------------------------------------------------------------------
1339 // ----------------------------------------------------------------------------
1341 bool wxRichEditModule::OnInit()
1343 // don't do anything - we will load it when needed
1347 void wxRichEditModule::OnExit()
1351 FreeLibrary(ms_hRichEdit
);
1356 bool wxRichEditModule::Load(int version
)
1358 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1359 _T("incorrect richedit control version requested") );
1361 if ( version
<= ms_verRichEdit
)
1363 // we've already got this or better
1369 ::FreeLibrary(ms_hRichEdit
);
1372 // always try load riched20.dll first - like this we won't have to reload
1373 // it later if we're first asked for RE 1 and then for RE 2 or 3
1374 wxString dllname
= _T("riched20.dll");
1375 ms_hRichEdit
= ::LoadLibrary(dllname
);
1376 ms_verRichEdit
= 2; // no way to tell if it's 2 or 3, assume 2
1378 if ( !ms_hRichEdit
&& (version
== 1) )
1380 // fall back to RE 1
1381 dllname
= _T("riched32.dll");
1382 ms_hRichEdit
= ::LoadLibrary(dllname
);
1386 if ( !ms_hRichEdit
)
1388 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1390 ms_verRichEdit
= -1;
1398 #endif // wxUSE_RICHEDIT