1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/textctrlce.cpp
3 // Purpose: wxTextCtrl implementation for smart phones driven by WinCE
4 // Author: Wlodzimierz ABX Skiba
7 // Copyright: (c) Wlodzimierz Skiba
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_TEXTCTRL && defined(__SMARTPHONE__) && defined(__WXWINCE__)
28 #include "wx/textctrl.h"
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
34 #include "wx/spinbutt.h"
35 #include "wx/textfile.h"
37 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
39 #define IsVertical(wxStyle) (true)
41 // ----------------------------------------------------------------------------
42 // event tables and other macros
43 // ----------------------------------------------------------------------------
45 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
46 EVT_CHAR(wxTextCtrl::OnChar
)
48 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
49 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
50 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
51 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
52 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
53 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
54 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
56 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
57 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
58 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
59 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
60 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
61 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
62 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
64 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
)
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // the margin between the up-down control and its buddy (can be arbitrary,
72 // choose what you like - or may be decide during run-time depending on the
74 static const int MARGIN_BETWEEN
= 0;
76 // ============================================================================
78 // ============================================================================
80 wxArrayTextSpins
wxTextCtrl::ms_allTextSpins
;
82 // ----------------------------------------------------------------------------
83 // wnd proc for the buddy text ctrl
84 // ----------------------------------------------------------------------------
86 LRESULT APIENTRY _EXPORT
wxBuddyTextCtrlWndProc(HWND hwnd
,
91 wxTextCtrl
*spin
= (wxTextCtrl
*)wxGetWindowUserData(hwnd
);
93 // forward some messages (the key and focus ones only so far) to
98 // if the focus comes from the spin control itself, don't set it
99 // back to it -- we don't want to go into an infinite loop
100 if ( (WXHWND
)wParam
== spin
->GetHWND() )
109 spin
->MSWWindowProc(message
, wParam
, lParam
);
111 // The control may have been deleted at this point, so check.
112 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
117 // we want to get WXK_RETURN in order to generate the event for it
118 return DLGC_WANTCHARS
;
121 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
122 hwnd
, message
, wParam
, lParam
);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 void wxTextCtrl::Init()
131 m_suppressNextUpdate
= false;
132 m_isNativeCaretShown
= true;
135 wxTextCtrl::~wxTextCtrl()
139 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
140 const wxString
& value
,
144 const wxValidator
& validator
,
145 const wxString
& name
)
147 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
148 style
|= wxBORDER_SIMPLE
;
150 SetWindowStyle(style
);
153 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
155 wxSize
sizeText(size
), sizeBtn(size
);
156 sizeBtn
.x
= GetBestSpinnerSize(IsVertical(style
)).x
/ 2;
158 if ( sizeText
.x
== wxDefaultCoord
)
160 // DEFAULT_ITEM_WIDTH is the default width for the text control
161 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
164 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
165 if ( sizeText
.x
<= 0 )
167 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
171 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
173 // we need to turn '\n's into "\r\n"s for the multiline controls
175 if ( m_windowStyle
& wxTE_MULTILINE
)
177 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
184 // we must create the list control before the spin button for the purpose
185 // of the dialog navigation: if there is a static text just before the spin
186 // control, activating it by Alt-letter should give focus to the text
187 // control, not the spin and the dialog navigation code will give focus to
188 // the next control (at Windows level), not the one after it
190 // create the text window
192 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
194 exStyle
, // sunken border
195 wxT("EDIT"), // window class
196 valueWin
, // no window title
197 msStyle
, // style (will be shown later)
198 pos
.x
, pos
.y
, // position
199 0, 0, // size (will be set later)
200 GetHwndOf(parent
), // parent
201 (HMENU
)-1, // control id
202 wxGetInstance(), // app instance
203 NULL
// unused client data
208 wxLogLastError(wxT("CreateWindow(buddy text window)"));
213 // initialize wxControl
214 if ( !CreateControl(parent
, id
, posBtn
, sizeBtn
, style
, validator
, name
) )
217 // now create the real HWND
218 WXDWORD spiner_style
= WS_VISIBLE
|
223 if ( !IsVertical(style
) )
224 spiner_style
|= UDS_HORZ
;
226 if ( style
& wxSP_WRAP
)
227 spiner_style
|= UDS_WRAP
;
229 if ( !MSWCreateControl(UPDOWN_CLASS
, spiner_style
, posBtn
, sizeBtn
, wxT(""), 0) )
232 // subclass the text ctrl to be able to intercept some events
233 wxSetWindowUserData(GetBuddyHwnd(), this);
234 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
235 wxBuddyTextCtrlWndProc
);
237 // set up fonts and colours (This is nomally done in MSWCreateControl)
240 SetFont(GetDefaultAttributes().font
);
242 // set the size of the text window - can do it only now, because we
243 // couldn't call DoGetBestSize() before as font wasn't set
244 if ( sizeText
.y
<= 0 )
247 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
249 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
252 SetInitialSize(size
);
254 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
256 // associate the list window with the spin button
257 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)GetBuddyHwnd(), 0);
259 // do it after finishing with m_hwndBuddy creation to avoid generating
260 // initial wxEVT_TEXT message
261 ms_allTextSpins
.Add(this);
266 // Make sure the window style (etc.) reflects the HWND style (roughly)
267 void wxTextCtrl::AdoptAttributesFromHWND()
269 wxWindow::AdoptAttributesFromHWND();
271 long style
= ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE
);
273 if (style
& ES_MULTILINE
)
274 m_windowStyle
|= wxTE_MULTILINE
;
275 if (style
& ES_PASSWORD
)
276 m_windowStyle
|= wxTE_PASSWORD
;
277 if (style
& ES_READONLY
)
278 m_windowStyle
|= wxTE_READONLY
;
279 if (style
& ES_WANTRETURN
)
280 m_windowStyle
|= wxTE_PROCESS_ENTER
;
281 if (style
& ES_CENTER
)
282 m_windowStyle
|= wxTE_CENTRE
;
283 if (style
& ES_RIGHT
)
284 m_windowStyle
|= wxTE_RIGHT
;
287 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
289 // we never have an external border
290 WXDWORD msStyle
= wxControl::MSWGetStyle
292 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
295 msStyle
|= WS_VISIBLE
;
297 // styles which we alaways add by default
298 if ( style
& wxTE_MULTILINE
)
300 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
301 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
303 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
304 if ( !(style
& wxTE_NO_VSCROLL
) )
306 // always adjust the vertical scrollbar automatically if we have it
307 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
310 style
|= wxTE_PROCESS_ENTER
;
314 // there is really no reason to not have this style for single line
316 msStyle
|= ES_AUTOHSCROLL
;
319 // note that wxTE_DONTWRAP is the same as wxHSCROLL so if we have a horz
320 // scrollbar, there is no wrapping -- which makes sense
321 if ( style
& wxTE_DONTWRAP
)
323 // automatically scroll the control horizontally as necessary
325 // NB: ES_AUTOHSCROLL is needed for richedit controls or they don't
326 // show horz scrollbar at all, even in spite of WS_HSCROLL, and as
327 // it doesn't seem to do any harm for plain edit controls, add it
329 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
332 if ( style
& wxTE_READONLY
)
333 msStyle
|= ES_READONLY
;
335 if ( style
& wxTE_PASSWORD
)
336 msStyle
|= ES_PASSWORD
;
338 if ( style
& wxTE_NOHIDESEL
)
339 msStyle
|= ES_NOHIDESEL
;
341 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
342 if ( style
& wxTE_CENTRE
)
343 msStyle
|= ES_CENTER
;
344 else if ( style
& wxTE_RIGHT
)
347 msStyle
|= ES_LEFT
; // ES_LEFT is 0 as well but for consistency...
352 // ----------------------------------------------------------------------------
353 // set/get the controls text
354 // ----------------------------------------------------------------------------
356 wxString
wxTextCtrl::GetValue() const
358 // range 0..-1 is special for GetRange() and means to retrieve all text
359 return GetRange(0, -1);
362 wxString
wxTextCtrl::GetRange(long from
, long to
) const
366 if ( from
>= to
&& to
!= -1 )
368 // nothing to retrieve
373 str
= wxGetWindowText(GetBuddyHwnd());
375 // need only a range?
378 str
= str
.Mid(from
, to
- from
);
381 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
382 // canonical one (same one as above) for consistency with the other kinds
383 // of controls and, more importantly, with the other ports
384 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
389 void wxTextCtrl::DoSetValue(const wxString
& value
, int flags
)
391 // if the text is long enough, it's faster to just set it instead of first
392 // comparing it with the old one (chances are that it will be different
393 // anyhow, this comparison is there to avoid flicker for small single-line
394 // edit controls mostly)
395 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
397 DoWriteText(value
, flags
);
399 // for compatibility, don't move the cursor when doing SetValue()
400 SetInsertionPoint(0);
404 // still send an event for consistency
405 if ( flags
& SetValue_SendEvent
)
409 // we should reset the modified flag even if the value didn't really change
411 // mark the control as being not dirty - we changed its text, not the
416 void wxTextCtrl::WriteText(const wxString
& value
)
421 void wxTextCtrl::DoWriteText(const wxString
& value
, int flags
)
423 bool selectionOnly
= (flags
& SetValue_SelectionOnly
) != 0;
425 if ( m_windowStyle
& wxTE_MULTILINE
)
426 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
430 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
431 // call below which is confusing for the client code and so should be
434 if ( selectionOnly
&& HasSelection() )
436 m_suppressNextUpdate
= true;
439 ::SendMessage(GetBuddyHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
440 0, (LPARAM
)valueDos
.c_str());
442 if ( !selectionOnly
&& !( flags
& SetValue_SendEvent
) )
444 // Windows already sends an update event for single-line
446 if ( m_windowStyle
& wxTE_MULTILINE
)
453 void wxTextCtrl::AppendText(const wxString
& text
)
455 SetInsertionPointEnd();
460 void wxTextCtrl::Clear()
462 ::SetWindowText(GetBuddyHwnd(), wxEmptyString
);
464 // Windows already sends an update event for single-line
466 if ( m_windowStyle
& wxTE_MULTILINE
)
470 // ----------------------------------------------------------------------------
471 // Clipboard operations
472 // ----------------------------------------------------------------------------
474 void wxTextCtrl::Copy()
478 ::SendMessage(GetBuddyHwnd(), WM_COPY
, 0, 0L);
482 void wxTextCtrl::Cut()
486 ::SendMessage(GetBuddyHwnd(), WM_CUT
, 0, 0L);
490 void wxTextCtrl::Paste()
494 ::SendMessage(GetBuddyHwnd(), WM_PASTE
, 0, 0L);
498 bool wxTextCtrl::HasSelection() const
501 GetSelection(&from
, &to
);
505 bool wxTextCtrl::CanCopy() const
507 // Can copy if there's a selection
508 return HasSelection();
511 bool wxTextCtrl::CanCut() const
513 return CanCopy() && IsEditable();
516 bool wxTextCtrl::CanPaste() const
521 // Standard edit control: check for straight text on clipboard
522 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
525 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
528 return isTextAvailable
;
531 // ----------------------------------------------------------------------------
533 // ----------------------------------------------------------------------------
535 void wxTextCtrl::SetEditable(bool editable
)
537 ::SendMessage(GetBuddyHwnd(), EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
540 void wxTextCtrl::SetInsertionPoint(long pos
)
542 DoSetSelection(pos
, pos
);
545 void wxTextCtrl::SetInsertionPointEnd()
547 if ( GetInsertionPoint() != GetLastPosition() )
548 SetInsertionPoint(GetLastPosition());
551 long wxTextCtrl::GetInsertionPoint() const
553 DWORD Pos
= (DWORD
)::SendMessage(GetBuddyHwnd(), EM_GETSEL
, 0, 0L);
557 wxTextPos
wxTextCtrl::GetLastPosition() const
559 int numLines
= GetNumberOfLines();
560 long posStartLastLine
= XYToPosition(0, numLines
- 1);
562 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
564 return posStartLastLine
+ lenLastLine
;
567 void wxTextCtrl::GetSelection(long* from
, long* to
) const
569 DWORD dwStart
, dwEnd
;
570 ::SendMessage(GetBuddyHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
576 bool wxTextCtrl::IsEditable() const
578 if ( !GetBuddyHwnd() )
581 long style
= ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE
);
583 return (style
& ES_READONLY
) == 0;
586 // ----------------------------------------------------------------------------
588 // ----------------------------------------------------------------------------
590 void wxTextCtrl::SetSelection(long from
, long to
)
592 // if from and to are both -1, it means (in wxWidgets) that all text should
593 // be selected - translate into Windows convention
594 if ( (from
== -1) && (to
== -1) )
600 DoSetSelection(from
, to
);
603 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
605 ::SendMessage(GetBuddyHwnd(), EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
609 ::SendMessage(GetBuddyHwnd(), EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
613 // ----------------------------------------------------------------------------
614 // Working with files
615 // ----------------------------------------------------------------------------
617 bool wxTextCtrl::LoadFile(const wxString
& file
)
619 if ( wxTextCtrlBase::LoadFile(file
) )
621 // update the size limit if needed
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
634 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
636 // Set selection and remove it
637 DoSetSelection(from
, to
, false);
639 DoWriteText(value
, SetValue_SelectionOnly
);
642 void wxTextCtrl::Remove(long from
, long to
)
644 Replace(from
, to
, wxEmptyString
);
647 bool wxTextCtrl::IsModified() const
649 return ::SendMessage(GetBuddyHwnd(), EM_GETMODIFY
, 0, 0) != 0;
652 void wxTextCtrl::MarkDirty()
654 ::SendMessage(GetBuddyHwnd(), EM_SETMODIFY
, TRUE
, 0L);
657 void wxTextCtrl::DiscardEdits()
659 ::SendMessage(GetBuddyHwnd(), EM_SETMODIFY
, FALSE
, 0L);
662 int wxTextCtrl::GetNumberOfLines() const
664 return (int)::SendMessage(GetBuddyHwnd(), EM_GETLINECOUNT
, 0, 0L);
667 // ----------------------------------------------------------------------------
668 // Positions <-> coords
669 // ----------------------------------------------------------------------------
671 long wxTextCtrl::XYToPosition(long x
, long y
) const
673 // This gets the char index for the _beginning_ of this line
674 long charIndex
= ::SendMessage(GetBuddyHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
676 return charIndex
+ x
;
679 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
681 // This gets the line number containing the character
682 long lineNo
= ::SendMessage(GetBuddyHwnd(), EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
690 // This gets the char index for the _beginning_ of this line
691 long charIndex
= ::SendMessage(GetBuddyHwnd(), EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
692 if ( charIndex
== -1 )
697 // The X position must therefore be the different between pos and charIndex
699 *x
= pos
- charIndex
;
706 wxTextCtrlHitTestResult
707 wxTextCtrl::HitTest(const wxPoint
& pt
, long *posOut
) const
709 // first get the position from Windows
710 // for the plain ones, we are limited to 16 bit positions which are
711 // combined in a single 32 bit value
712 LPARAM lParam
= MAKELPARAM(pt
.x
, pt
.y
);
714 LRESULT pos
= ::SendMessage(GetBuddyHwnd(), EM_CHARFROMPOS
, 0, lParam
);
718 // this seems to indicate an error...
719 return wxTE_HT_UNKNOWN
;
722 // for plain EDIT controls the higher word contains something else
726 // next determine where it is relatively to our point: EM_CHARFROMPOS
727 // always returns the closest character but we need to be more precise, so
728 // double check that we really are where it pretends
731 LRESULT lRc
= ::SendMessage(GetBuddyHwnd(), EM_POSFROMCHAR
, pos
, 0);
735 // this is apparently returned when pos corresponds to the last
742 ptReal
.x
= LOWORD(lRc
);
743 ptReal
.y
= HIWORD(lRc
);
746 wxTextCtrlHitTestResult rc
;
748 if ( pt
.y
> ptReal
.y
+ GetCharHeight() )
750 else if ( pt
.x
> ptReal
.x
+ GetCharWidth() )
753 rc
= wxTE_HT_ON_TEXT
;
761 void wxTextCtrl::ShowPosition(long pos
)
763 int currentLineLineNo
= (int)::SendMessage(GetBuddyHwnd(), EM_GETFIRSTVISIBLELINE
, 0, 0L);
765 int specifiedLineLineNo
= (int)::SendMessage(GetBuddyHwnd(), EM_LINEFROMCHAR
, (WPARAM
)pos
, 0L);
767 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
769 if (linesToScroll
!= 0)
770 (void)::SendMessage(GetBuddyHwnd(), EM_LINESCROLL
, 0, (LPARAM
)linesToScroll
);
773 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
775 return ::SendMessage(GetBuddyHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0L);
778 int wxTextCtrl::GetLineLength(long lineNo
) const
780 long pos
= XYToPosition(0, lineNo
);
782 return GetLengthOfLineContainingPos(pos
);
785 wxString
wxTextCtrl::GetLineText(long lineNo
) const
787 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
789 // there must be at least enough place for the length WORD in the
795 wxStringBufferLength
tmp(str
, len
);
798 *(WORD
*)buf
= (WORD
)len
;
799 len
= (size_t)::SendMessage(GetBuddyHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
801 // remove the '\n' at the end, if any (this is how this function is
802 // supposed to work according to the docs)
803 if ( buf
[len
- 1] == wxT('\n') )
815 void wxTextCtrl::SetMaxLength(unsigned long len
)
817 ::SendMessage(GetBuddyHwnd(), EM_LIMITTEXT
, len
, 0);
820 // ----------------------------------------------------------------------------
822 // ----------------------------------------------------------------------------
824 void wxTextCtrl::Undo()
828 ::SendMessage(GetBuddyHwnd(), EM_UNDO
, 0, 0);
832 void wxTextCtrl::Redo()
836 ::SendMessage(GetBuddyHwnd(), EM_UNDO
, 0, 0);
840 bool wxTextCtrl::CanUndo() const
842 return ::SendMessage(GetBuddyHwnd(), EM_CANUNDO
, 0, 0) != 0;
845 bool wxTextCtrl::CanRedo() const
847 return ::SendMessage(GetBuddyHwnd(), EM_CANUNDO
, 0, 0) != 0;
850 // ----------------------------------------------------------------------------
852 // ----------------------------------------------------------------------------
854 // ----------------------------------------------------------------------------
855 // implemenation details
856 // ----------------------------------------------------------------------------
858 void wxTextCtrl::Command(wxCommandEvent
& event
)
860 SetValue(event
.GetString());
861 ProcessCommand (event
);
864 // ----------------------------------------------------------------------------
865 // kbd input processing
866 // ----------------------------------------------------------------------------
868 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
870 switch ( event
.GetKeyCode() )
873 if ( !HasFlag(wxTE_MULTILINE
) )
875 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
876 InitCommandEvent(event
);
877 event
.SetString(GetValue());
878 if ( GetEventHandler()->ProcessEvent(event
) )
881 //else: multiline controls need Enter for themselves
886 // ok, so this is getting absolutely ridiculous but I don't see
887 // any other way to fix this bug: when a multiline text control is
888 // inside a wxFrame, we need to generate the navigation event as
889 // otherwise nothing happens at all, but when the same control is
890 // created inside a dialog, IsDialogMessage() *does* switch focus
891 // all by itself and so if we do it here as well, it is advanced
892 // twice and goes to the next control... to prevent this from
893 // happening we're doing this ugly check, the logic being that if
894 // we don't have focus then it had been already changed to the next
897 // the right thing to do would, of course, be to understand what
898 // the hell is IsDialogMessage() doing but this is beyond my feeble
899 // forces at the moment unfortunately
900 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
902 if ( FindFocus() == this )
905 if (!event
.ShiftDown())
906 flags
|= wxNavigationKeyEvent::IsForward
;
907 if (event
.ControlDown())
908 flags
|= wxNavigationKeyEvent::WinChange
;
915 // Insert tab since calling the default Windows handler
916 // doesn't seem to do it
917 WriteText(wxT("\t"));
922 // no, we didn't process it
926 WXLRESULT
wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
928 WXLRESULT lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
930 if ( nMsg
== WM_GETDLGCODE
)
932 // we always want the chars and the arrows: the arrows for navigation
933 // and the chars because we want Ctrl-C to work even in a read only
935 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
939 // we may have several different cases:
940 // 1. normal case: both TAB and ENTER are used for dlg navigation
941 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
942 // next control in the dialog
943 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
945 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
946 // to the next control
948 // the multiline edit control should always get <Return> for itself
949 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
950 lDlgCode
|= DLGC_WANTMESSAGE
;
952 if ( HasFlag(wxTE_PROCESS_TAB
) )
953 lDlgCode
|= DLGC_WANTTAB
;
959 // NB: use "=", not "|=" as the base class version returns the
960 // same flags is this state as usual (i.e. including
961 // DLGC_WANTMESSAGE). This is strange (how does it work in the
962 // native Win32 apps?) but for now live with it.
970 // ----------------------------------------------------------------------------
971 // text control event processing
972 // ----------------------------------------------------------------------------
974 bool wxTextCtrl::SendUpdateEvent()
976 // is event reporting suspended?
977 if ( m_suppressNextUpdate
)
979 // do process the next one
980 m_suppressNextUpdate
= false;
985 wxCommandEvent
event(wxEVT_TEXT
, GetId());
986 InitCommandEvent(event
);
987 event
.SetString(GetValue());
989 return ProcessCommand(event
);
992 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
999 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1002 event
.SetEventObject(this);
1003 GetEventHandler()->ProcessEvent(event
);
1012 // the text size limit has been hit -- try to increase it
1013 if ( !AdjustSpaceLimit() )
1015 wxCommandEvent
event(wxEVT_TEXT_MAXLEN
, m_windowId
);
1016 InitCommandEvent(event
);
1017 event
.SetString(GetValue());
1018 ProcessCommand(event
);
1022 // the other edit notification messages are not processed
1031 bool wxTextCtrl::AdjustSpaceLimit()
1033 unsigned int limit
= ::SendMessage(GetBuddyHwnd(), EM_GETLIMITTEXT
, 0, 0);
1035 // HACK: we try to automatically extend the limit for the amount of text
1036 // to allow (interactively) entering more than 64Kb of text under
1037 // Win9x but we shouldn't reset the text limit which was previously
1038 // set explicitly with SetMaxLength()
1040 // we could solve this by storing the limit we set in wxTextCtrl but
1041 // to save space we prefer to simply test here the actual limit
1042 // value: we consider that SetMaxLength() can only be called for
1044 if ( limit
< 0x8000 )
1046 // we've got more text than limit set by SetMaxLength()
1050 unsigned int len
= ::GetWindowTextLength(GetBuddyHwnd());
1053 limit
= len
+ 0x8000; // 32Kb
1055 if ( limit
> 0xffff )
1057 // this will set it to a platform-dependent maximum (much more
1058 // than 64Kb under NT)
1062 ::SendMessage(GetBuddyHwnd(), EM_LIMITTEXT
, limit
, 0L);
1065 // we changed the limit
1069 bool wxTextCtrl::AcceptsFocus() const
1071 // we don't want focus if we can't be edited unless we're a multiline
1072 // control because then it might be still nice to get focus from keyboard
1073 // to be able to scroll it without mouse
1074 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1077 void wxTextCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
1079 int widthBtn
= GetBestSpinnerSize(IsVertical(GetWindowStyle())).x
/ 2;
1080 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
1081 if ( widthText
<= 0 )
1083 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
1086 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
1088 wxLogLastError(wxT("MoveWindow(buddy)"));
1091 x
+= widthText
+ MARGIN_BETWEEN
;
1092 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
1094 wxLogLastError(wxT("MoveWindow"));
1098 wxSize
wxTextCtrl::DoGetBestSize() const
1101 wxGetCharSize(GetBuddyHwnd(), &cx
, &cy
, GetFont());
1103 int wText
= DEFAULT_ITEM_WIDTH
;
1106 if ( m_windowStyle
& wxTE_MULTILINE
)
1108 hText
*= wxMax(GetNumberOfLines(), 5);
1110 //else: for single line control everything is ok
1112 // we have to add the adjustments for the control height only once, not
1113 // once per line, so do it after multiplication above
1114 hText
+= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
) - cy
;
1116 return wxSize(wText
, hText
);
1119 // ----------------------------------------------------------------------------
1120 // standard handlers for standard edit menu events
1121 // ----------------------------------------------------------------------------
1123 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1128 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1133 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1138 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1143 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1148 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1151 GetSelection(& from
, & to
);
1152 if (from
!= -1 && to
!= -1)
1156 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1158 SetSelection(-1, -1);
1161 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1163 event
.Enable( CanCut() );
1166 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1168 event
.Enable( CanCopy() );
1171 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1173 event
.Enable( CanPaste() );
1176 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1178 event
.Enable( CanUndo() );
1181 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1183 event
.Enable( CanRedo() );
1186 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1189 GetSelection(& from
, & to
);
1190 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1193 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1195 event
.Enable(GetLastPosition() > 0);
1198 void wxTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
1200 // be sure the caret remains invisible if the user had hidden it
1201 if ( !m_isNativeCaretShown
)
1203 ::HideCaret(GetBuddyHwnd());
1207 #endif // wxUSE_TEXTCTRL && __SMARTPHONE__ && __WXWINCE__