1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/textctrlce.cpp
3 // Purpose: wxTextCtrl implementation for smart phones driven by WinCE
4 // Author: Wlodzimierz ABX Skiba
8 // Copyright: (c) Wlodzimierz Skiba
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_TEXTCTRL && defined(__SMARTPHONE__) && defined(__WXWINCE__)
29 #include "wx/textctrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
35 #include "wx/spinbutt.h"
36 #include "wx/textfile.h"
38 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
40 #define IsVertical(wxStyle) (true)
42 // ----------------------------------------------------------------------------
43 // event tables and other macros
44 // ----------------------------------------------------------------------------
46 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
47 EVT_CHAR(wxTextCtrl::OnChar
)
49 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
50 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
51 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
52 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
53 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
54 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
55 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
57 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
58 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
59 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
60 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
61 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
62 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
63 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
65 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
)
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // the margin between the up-down control and its buddy (can be arbitrary,
73 // choose what you like - or may be decide during run-time depending on the
75 static const int MARGIN_BETWEEN
= 0;
77 // ============================================================================
79 // ============================================================================
81 wxArrayTextSpins
wxTextCtrl::ms_allTextSpins
;
83 // ----------------------------------------------------------------------------
84 // wnd proc for the buddy text ctrl
85 // ----------------------------------------------------------------------------
87 LRESULT APIENTRY _EXPORT
wxBuddyTextCtrlWndProc(HWND hwnd
,
92 wxTextCtrl
*spin
= (wxTextCtrl
*)wxGetWindowUserData(hwnd
);
94 // forward some messages (the key and focus ones only so far) to
99 // if the focus comes from the spin control itself, don't set it
100 // back to it -- we don't want to go into an infinite loop
101 if ( (WXHWND
)wParam
== spin
->GetHWND() )
110 spin
->MSWWindowProc(message
, wParam
, lParam
);
112 // The control may have been deleted at this point, so check.
113 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
118 // we want to get WXK_RETURN in order to generate the event for it
119 return DLGC_WANTCHARS
;
122 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
123 hwnd
, message
, wParam
, lParam
);
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 void wxTextCtrl::Init()
132 m_suppressNextUpdate
= false;
133 m_isNativeCaretShown
= true;
136 wxTextCtrl::~wxTextCtrl()
140 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
141 const wxString
& value
,
145 const wxValidator
& validator
,
146 const wxString
& name
)
148 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
149 style
|= wxBORDER_SIMPLE
;
151 SetWindowStyle(style
);
154 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
156 wxSize
sizeText(size
), sizeBtn(size
);
157 sizeBtn
.x
= GetBestSpinnerSize(IsVertical(style
)).x
/ 2;
159 if ( sizeText
.x
== wxDefaultCoord
)
161 // DEFAULT_ITEM_WIDTH is the default width for the text control
162 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
165 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
166 if ( sizeText
.x
<= 0 )
168 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
172 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
174 // we need to turn '\n's into "\r\n"s for the multiline controls
176 if ( m_windowStyle
& wxTE_MULTILINE
)
178 valueWin
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
185 // we must create the list control before the spin button for the purpose
186 // of the dialog navigation: if there is a static text just before the spin
187 // control, activating it by Alt-letter should give focus to the text
188 // control, not the spin and the dialog navigation code will give focus to
189 // the next control (at Windows level), not the one after it
191 // create the text window
193 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
195 exStyle
, // sunken border
196 wxT("EDIT"), // window class
197 valueWin
, // no window title
198 msStyle
, // style (will be shown later)
199 pos
.x
, pos
.y
, // position
200 0, 0, // size (will be set later)
201 GetHwndOf(parent
), // parent
202 (HMENU
)-1, // control id
203 wxGetInstance(), // app instance
204 NULL
// unused client data
209 wxLogLastError(wxT("CreateWindow(buddy text window)"));
214 // initialize wxControl
215 if ( !CreateControl(parent
, id
, posBtn
, sizeBtn
, style
, validator
, name
) )
218 // now create the real HWND
219 WXDWORD spiner_style
= WS_VISIBLE
|
224 if ( !IsVertical(style
) )
225 spiner_style
|= UDS_HORZ
;
227 if ( style
& wxSP_WRAP
)
228 spiner_style
|= UDS_WRAP
;
230 if ( !MSWCreateControl(UPDOWN_CLASS
, spiner_style
, posBtn
, sizeBtn
, wxT(""), 0) )
233 // subclass the text ctrl to be able to intercept some events
234 wxSetWindowUserData(GetBuddyHwnd(), this);
235 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
236 wxBuddyTextCtrlWndProc
);
238 // set up fonts and colours (This is nomally done in MSWCreateControl)
241 SetFont(GetDefaultAttributes().font
);
243 // set the size of the text window - can do it only now, because we
244 // couldn't call DoGetBestSize() before as font wasn't set
245 if ( sizeText
.y
<= 0 )
248 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
250 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
253 SetInitialSize(size
);
255 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
257 // associate the list window with the spin button
258 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)GetBuddyHwnd(), 0);
260 // do it after finishing with m_hwndBuddy creation to avoid generating
261 // initial wxEVT_COMMAND_TEXT_UPDATED message
262 ms_allTextSpins
.Add(this);
267 // Make sure the window style (etc.) reflects the HWND style (roughly)
268 void wxTextCtrl::AdoptAttributesFromHWND()
270 wxWindow::AdoptAttributesFromHWND();
272 long style
= ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE
);
274 if (style
& ES_MULTILINE
)
275 m_windowStyle
|= wxTE_MULTILINE
;
276 if (style
& ES_PASSWORD
)
277 m_windowStyle
|= wxTE_PASSWORD
;
278 if (style
& ES_READONLY
)
279 m_windowStyle
|= wxTE_READONLY
;
280 if (style
& ES_WANTRETURN
)
281 m_windowStyle
|= wxTE_PROCESS_ENTER
;
282 if (style
& ES_CENTER
)
283 m_windowStyle
|= wxTE_CENTRE
;
284 if (style
& ES_RIGHT
)
285 m_windowStyle
|= wxTE_RIGHT
;
288 WXDWORD
wxTextCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
290 // we never have an external border
291 WXDWORD msStyle
= wxControl::MSWGetStyle
293 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
296 msStyle
|= WS_VISIBLE
;
298 // styles which we alaways add by default
299 if ( style
& wxTE_MULTILINE
)
301 wxASSERT_MSG( !(style
& wxTE_PROCESS_ENTER
),
302 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
304 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
;
305 if ( !(style
& wxTE_NO_VSCROLL
) )
307 // always adjust the vertical scrollbar automatically if we have it
308 msStyle
|= WS_VSCROLL
| ES_AUTOVSCROLL
;
311 style
|= wxTE_PROCESS_ENTER
;
315 // there is really no reason to not have this style for single line
317 msStyle
|= ES_AUTOHSCROLL
;
320 // note that wxTE_DONTWRAP is the same as wxHSCROLL so if we have a horz
321 // scrollbar, there is no wrapping -- which makes sense
322 if ( style
& wxTE_DONTWRAP
)
324 // automatically scroll the control horizontally as necessary
326 // NB: ES_AUTOHSCROLL is needed for richedit controls or they don't
327 // show horz scrollbar at all, even in spite of WS_HSCROLL, and as
328 // it doesn't seem to do any harm for plain edit controls, add it
330 msStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
333 if ( style
& wxTE_READONLY
)
334 msStyle
|= ES_READONLY
;
336 if ( style
& wxTE_PASSWORD
)
337 msStyle
|= ES_PASSWORD
;
339 if ( style
& wxTE_NOHIDESEL
)
340 msStyle
|= ES_NOHIDESEL
;
342 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
343 if ( style
& wxTE_CENTRE
)
344 msStyle
|= ES_CENTER
;
345 else if ( style
& wxTE_RIGHT
)
348 msStyle
|= ES_LEFT
; // ES_LEFT is 0 as well but for consistency...
353 // ----------------------------------------------------------------------------
354 // set/get the controls text
355 // ----------------------------------------------------------------------------
357 wxString
wxTextCtrl::GetValue() const
359 // range 0..-1 is special for GetRange() and means to retrieve all text
360 return GetRange(0, -1);
363 wxString
wxTextCtrl::GetRange(long from
, long to
) const
367 if ( from
>= to
&& to
!= -1 )
369 // nothing to retrieve
374 str
= wxGetWindowText(GetBuddyHwnd());
376 // need only a range?
379 str
= str
.Mid(from
, to
- from
);
382 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
383 // canonical one (same one as above) for consistency with the other kinds
384 // of controls and, more importantly, with the other ports
385 str
= wxTextFile::Translate(str
, wxTextFileType_Unix
);
390 void wxTextCtrl::DoSetValue(const wxString
& value
, int flags
)
392 // if the text is long enough, it's faster to just set it instead of first
393 // comparing it with the old one (chances are that it will be different
394 // anyhow, this comparison is there to avoid flicker for small single-line
395 // edit controls mostly)
396 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
398 DoWriteText(value
, flags
);
400 // for compatibility, don't move the cursor when doing SetValue()
401 SetInsertionPoint(0);
405 // still send an event for consistency
406 if ( flags
& SetValue_SendEvent
)
410 // we should reset the modified flag even if the value didn't really change
412 // mark the control as being not dirty - we changed its text, not the
417 void wxTextCtrl::WriteText(const wxString
& value
)
422 void wxTextCtrl::DoWriteText(const wxString
& value
, int flags
)
424 bool selectionOnly
= (flags
& SetValue_SelectionOnly
) != 0;
426 if ( m_windowStyle
& wxTE_MULTILINE
)
427 valueDos
= wxTextFile::Translate(value
, wxTextFileType_Dos
);
431 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
432 // call below which is confusing for the client code and so should be
435 if ( selectionOnly
&& HasSelection() )
437 m_suppressNextUpdate
= true;
440 ::SendMessage(GetBuddyHwnd(), selectionOnly
? EM_REPLACESEL
: WM_SETTEXT
,
441 0, (LPARAM
)valueDos
.c_str());
443 if ( !selectionOnly
&& !( flags
& SetValue_SendEvent
) )
445 // Windows already sends an update event for single-line
447 if ( m_windowStyle
& wxTE_MULTILINE
)
454 void wxTextCtrl::AppendText(const wxString
& text
)
456 SetInsertionPointEnd();
461 void wxTextCtrl::Clear()
463 ::SetWindowText(GetBuddyHwnd(), wxEmptyString
);
465 // Windows already sends an update event for single-line
467 if ( m_windowStyle
& wxTE_MULTILINE
)
471 // ----------------------------------------------------------------------------
472 // Clipboard operations
473 // ----------------------------------------------------------------------------
475 void wxTextCtrl::Copy()
479 ::SendMessage(GetBuddyHwnd(), WM_COPY
, 0, 0L);
483 void wxTextCtrl::Cut()
487 ::SendMessage(GetBuddyHwnd(), WM_CUT
, 0, 0L);
491 void wxTextCtrl::Paste()
495 ::SendMessage(GetBuddyHwnd(), WM_PASTE
, 0, 0L);
499 bool wxTextCtrl::HasSelection() const
502 GetSelection(&from
, &to
);
506 bool wxTextCtrl::CanCopy() const
508 // Can copy if there's a selection
509 return HasSelection();
512 bool wxTextCtrl::CanCut() const
514 return CanCopy() && IsEditable();
517 bool wxTextCtrl::CanPaste() const
522 // Standard edit control: check for straight text on clipboard
523 if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) )
526 bool isTextAvailable
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0;
529 return isTextAvailable
;
532 // ----------------------------------------------------------------------------
534 // ----------------------------------------------------------------------------
536 void wxTextCtrl::SetEditable(bool editable
)
538 ::SendMessage(GetBuddyHwnd(), EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
541 void wxTextCtrl::SetInsertionPoint(long pos
)
543 DoSetSelection(pos
, pos
);
546 void wxTextCtrl::SetInsertionPointEnd()
548 if ( GetInsertionPoint() != GetLastPosition() )
549 SetInsertionPoint(GetLastPosition());
552 long wxTextCtrl::GetInsertionPoint() const
554 DWORD Pos
= (DWORD
)::SendMessage(GetBuddyHwnd(), EM_GETSEL
, 0, 0L);
558 wxTextPos
wxTextCtrl::GetLastPosition() const
560 int numLines
= GetNumberOfLines();
561 long posStartLastLine
= XYToPosition(0, numLines
- 1);
563 long lenLastLine
= GetLengthOfLineContainingPos(posStartLastLine
);
565 return posStartLastLine
+ lenLastLine
;
568 void wxTextCtrl::GetSelection(long* from
, long* to
) const
570 DWORD dwStart
, dwEnd
;
571 ::SendMessage(GetBuddyHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
577 bool wxTextCtrl::IsEditable() const
579 if ( !GetBuddyHwnd() )
582 long style
= ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE
);
584 return (style
& ES_READONLY
) == 0;
587 // ----------------------------------------------------------------------------
589 // ----------------------------------------------------------------------------
591 void wxTextCtrl::SetSelection(long from
, long to
)
593 // if from and to are both -1, it means (in wxWidgets) that all text should
594 // be selected - translate into Windows convention
595 if ( (from
== -1) && (to
== -1) )
601 DoSetSelection(from
, to
);
604 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
)
606 ::SendMessage(GetBuddyHwnd(), EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
610 ::SendMessage(GetBuddyHwnd(), EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
614 // ----------------------------------------------------------------------------
615 // Working with files
616 // ----------------------------------------------------------------------------
618 bool wxTextCtrl::LoadFile(const wxString
& file
)
620 if ( wxTextCtrlBase::LoadFile(file
) )
622 // update the size limit if needed
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
637 // Set selection and remove it
638 DoSetSelection(from
, to
, false);
640 DoWriteText(value
, SetValue_SelectionOnly
);
643 void wxTextCtrl::Remove(long from
, long to
)
645 Replace(from
, to
, wxEmptyString
);
648 bool wxTextCtrl::IsModified() const
650 return ::SendMessage(GetBuddyHwnd(), EM_GETMODIFY
, 0, 0) != 0;
653 void wxTextCtrl::MarkDirty()
655 ::SendMessage(GetBuddyHwnd(), EM_SETMODIFY
, TRUE
, 0L);
658 void wxTextCtrl::DiscardEdits()
660 ::SendMessage(GetBuddyHwnd(), EM_SETMODIFY
, FALSE
, 0L);
663 int wxTextCtrl::GetNumberOfLines() const
665 return (int)::SendMessage(GetBuddyHwnd(), EM_GETLINECOUNT
, 0, 0L);
668 // ----------------------------------------------------------------------------
669 // Positions <-> coords
670 // ----------------------------------------------------------------------------
672 long wxTextCtrl::XYToPosition(long x
, long y
) const
674 // This gets the char index for the _beginning_ of this line
675 long charIndex
= ::SendMessage(GetBuddyHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
677 return charIndex
+ x
;
680 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
682 // This gets the line number containing the character
683 long lineNo
= ::SendMessage(GetBuddyHwnd(), EM_LINEFROMCHAR
, (WPARAM
)pos
, 0);
691 // This gets the char index for the _beginning_ of this line
692 long charIndex
= ::SendMessage(GetBuddyHwnd(), EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
693 if ( charIndex
== -1 )
698 // The X position must therefore be the different between pos and charIndex
700 *x
= pos
- charIndex
;
707 wxTextCtrlHitTestResult
708 wxTextCtrl::HitTest(const wxPoint
& pt
, long *posOut
) const
710 // first get the position from Windows
711 // for the plain ones, we are limited to 16 bit positions which are
712 // combined in a single 32 bit value
713 LPARAM lParam
= MAKELPARAM(pt
.x
, pt
.y
);
715 LRESULT pos
= ::SendMessage(GetBuddyHwnd(), EM_CHARFROMPOS
, 0, lParam
);
719 // this seems to indicate an error...
720 return wxTE_HT_UNKNOWN
;
723 // for plain EDIT controls the higher word contains something else
727 // next determine where it is relatively to our point: EM_CHARFROMPOS
728 // always returns the closest character but we need to be more precise, so
729 // double check that we really are where it pretends
732 LRESULT lRc
= ::SendMessage(GetBuddyHwnd(), EM_POSFROMCHAR
, pos
, 0);
736 // this is apparently returned when pos corresponds to the last
743 ptReal
.x
= LOWORD(lRc
);
744 ptReal
.y
= HIWORD(lRc
);
747 wxTextCtrlHitTestResult rc
;
749 if ( pt
.y
> ptReal
.y
+ GetCharHeight() )
751 else if ( pt
.x
> ptReal
.x
+ GetCharWidth() )
754 rc
= wxTE_HT_ON_TEXT
;
762 void wxTextCtrl::ShowPosition(long pos
)
764 int currentLineLineNo
= (int)::SendMessage(GetBuddyHwnd(), EM_GETFIRSTVISIBLELINE
, 0, 0L);
766 int specifiedLineLineNo
= (int)::SendMessage(GetBuddyHwnd(), EM_LINEFROMCHAR
, (WPARAM
)pos
, 0L);
768 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
770 if (linesToScroll
!= 0)
771 (void)::SendMessage(GetBuddyHwnd(), EM_LINESCROLL
, 0, (LPARAM
)linesToScroll
);
774 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const
776 return ::SendMessage(GetBuddyHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0L);
779 int wxTextCtrl::GetLineLength(long lineNo
) const
781 long pos
= XYToPosition(0, lineNo
);
783 return GetLengthOfLineContainingPos(pos
);
786 wxString
wxTextCtrl::GetLineText(long lineNo
) const
788 size_t len
= (size_t)GetLineLength(lineNo
) + 1;
790 // there must be at least enough place for the length WORD in the
796 wxStringBufferLength
tmp(str
, len
);
799 *(WORD
*)buf
= (WORD
)len
;
800 len
= (size_t)::SendMessage(GetBuddyHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
802 // remove the '\n' at the end, if any (this is how this function is
803 // supposed to work according to the docs)
804 if ( buf
[len
- 1] == wxT('\n') )
816 void wxTextCtrl::SetMaxLength(unsigned long len
)
818 ::SendMessage(GetBuddyHwnd(), EM_LIMITTEXT
, len
, 0);
821 // ----------------------------------------------------------------------------
823 // ----------------------------------------------------------------------------
825 void wxTextCtrl::Undo()
829 ::SendMessage(GetBuddyHwnd(), EM_UNDO
, 0, 0);
833 void wxTextCtrl::Redo()
837 ::SendMessage(GetBuddyHwnd(), EM_UNDO
, 0, 0);
841 bool wxTextCtrl::CanUndo() const
843 return ::SendMessage(GetBuddyHwnd(), EM_CANUNDO
, 0, 0) != 0;
846 bool wxTextCtrl::CanRedo() const
848 return ::SendMessage(GetBuddyHwnd(), EM_CANUNDO
, 0, 0) != 0;
851 // ----------------------------------------------------------------------------
853 // ----------------------------------------------------------------------------
855 // ----------------------------------------------------------------------------
856 // implemenation details
857 // ----------------------------------------------------------------------------
859 void wxTextCtrl::Command(wxCommandEvent
& event
)
861 SetValue(event
.GetString());
862 ProcessCommand (event
);
865 // ----------------------------------------------------------------------------
866 // kbd input processing
867 // ----------------------------------------------------------------------------
869 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
871 switch ( event
.GetKeyCode() )
874 if ( !HasFlag(wxTE_MULTILINE
) )
876 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
877 InitCommandEvent(event
);
878 event
.SetString(GetValue());
879 if ( GetEventHandler()->ProcessEvent(event
) )
882 //else: multiline controls need Enter for themselves
887 // ok, so this is getting absolutely ridiculous but I don't see
888 // any other way to fix this bug: when a multiline text control is
889 // inside a wxFrame, we need to generate the navigation event as
890 // otherwise nothing happens at all, but when the same control is
891 // created inside a dialog, IsDialogMessage() *does* switch focus
892 // all by itself and so if we do it here as well, it is advanced
893 // twice and goes to the next control... to prevent this from
894 // happening we're doing this ugly check, the logic being that if
895 // we don't have focus then it had been already changed to the next
898 // the right thing to do would, of course, be to understand what
899 // the hell is IsDialogMessage() doing but this is beyond my feeble
900 // forces at the moment unfortunately
901 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
903 if ( FindFocus() == this )
906 if (!event
.ShiftDown())
907 flags
|= wxNavigationKeyEvent::IsForward
;
908 if (event
.ControlDown())
909 flags
|= wxNavigationKeyEvent::WinChange
;
916 // Insert tab since calling the default Windows handler
917 // doesn't seem to do it
918 WriteText(wxT("\t"));
923 // no, we didn't process it
927 WXLRESULT
wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
929 WXLRESULT lRc
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
931 if ( nMsg
== WM_GETDLGCODE
)
933 // we always want the chars and the arrows: the arrows for navigation
934 // and the chars because we want Ctrl-C to work even in a read only
936 long lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
940 // we may have several different cases:
941 // 1. normal case: both TAB and ENTER are used for dlg navigation
942 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
943 // next control in the dialog
944 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
946 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
947 // to the next control
949 // the multiline edit control should always get <Return> for itself
950 if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) )
951 lDlgCode
|= DLGC_WANTMESSAGE
;
953 if ( HasFlag(wxTE_PROCESS_TAB
) )
954 lDlgCode
|= DLGC_WANTTAB
;
960 // NB: use "=", not "|=" as the base class version returns the
961 // same flags is this state as usual (i.e. including
962 // DLGC_WANTMESSAGE). This is strange (how does it work in the
963 // native Win32 apps?) but for now live with it.
971 // ----------------------------------------------------------------------------
972 // text control event processing
973 // ----------------------------------------------------------------------------
975 bool wxTextCtrl::SendUpdateEvent()
977 // is event reporting suspended?
978 if ( m_suppressNextUpdate
)
980 // do process the next one
981 m_suppressNextUpdate
= false;
986 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
987 InitCommandEvent(event
);
988 event
.SetString(GetValue());
990 return ProcessCommand(event
);
993 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1000 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1003 event
.SetEventObject(this);
1004 GetEventHandler()->ProcessEvent(event
);
1013 // the text size limit has been hit -- try to increase it
1014 if ( !AdjustSpaceLimit() )
1016 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
);
1017 InitCommandEvent(event
);
1018 event
.SetString(GetValue());
1019 ProcessCommand(event
);
1023 // the other edit notification messages are not processed
1032 bool wxTextCtrl::AdjustSpaceLimit()
1034 unsigned int limit
= ::SendMessage(GetBuddyHwnd(), EM_GETLIMITTEXT
, 0, 0);
1036 // HACK: we try to automatically extend the limit for the amount of text
1037 // to allow (interactively) entering more than 64Kb of text under
1038 // Win9x but we shouldn't reset the text limit which was previously
1039 // set explicitly with SetMaxLength()
1041 // we could solve this by storing the limit we set in wxTextCtrl but
1042 // to save space we prefer to simply test here the actual limit
1043 // value: we consider that SetMaxLength() can only be called for
1045 if ( limit
< 0x8000 )
1047 // we've got more text than limit set by SetMaxLength()
1051 unsigned int len
= ::GetWindowTextLength(GetBuddyHwnd());
1054 limit
= len
+ 0x8000; // 32Kb
1056 if ( limit
> 0xffff )
1058 // this will set it to a platform-dependent maximum (much more
1059 // than 64Kb under NT)
1063 ::SendMessage(GetBuddyHwnd(), EM_LIMITTEXT
, limit
, 0L);
1066 // we changed the limit
1070 bool wxTextCtrl::AcceptsFocus() const
1072 // we don't want focus if we can't be edited unless we're a multiline
1073 // control because then it might be still nice to get focus from keyboard
1074 // to be able to scroll it without mouse
1075 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1078 void wxTextCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
1080 int widthBtn
= GetBestSpinnerSize(IsVertical(GetWindowStyle())).x
/ 2;
1081 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
1082 if ( widthText
<= 0 )
1084 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
1087 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
1089 wxLogLastError(wxT("MoveWindow(buddy)"));
1092 x
+= widthText
+ MARGIN_BETWEEN
;
1093 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
1095 wxLogLastError(wxT("MoveWindow"));
1099 wxSize
wxTextCtrl::DoGetBestSize() const
1102 wxGetCharSize(GetBuddyHwnd(), &cx
, &cy
, GetFont());
1104 int wText
= DEFAULT_ITEM_WIDTH
;
1107 if ( m_windowStyle
& wxTE_MULTILINE
)
1109 hText
*= wxMax(GetNumberOfLines(), 5);
1111 //else: for single line control everything is ok
1113 // we have to add the adjustments for the control height only once, not
1114 // once per line, so do it after multiplication above
1115 hText
+= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
) - cy
;
1117 return wxSize(wText
, hText
);
1120 // ----------------------------------------------------------------------------
1121 // standard handlers for standard edit menu events
1122 // ----------------------------------------------------------------------------
1124 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1129 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1134 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1139 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1144 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1149 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1152 GetSelection(& from
, & to
);
1153 if (from
!= -1 && to
!= -1)
1157 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1159 SetSelection(-1, -1);
1162 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1164 event
.Enable( CanCut() );
1167 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1169 event
.Enable( CanCopy() );
1172 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1174 event
.Enable( CanPaste() );
1177 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1179 event
.Enable( CanUndo() );
1182 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1184 event
.Enable( CanRedo() );
1187 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1190 GetSelection(& from
, & to
);
1191 event
.Enable(from
!= -1 && to
!= -1 && from
!= to
&& IsEditable()) ;
1194 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1196 event
.Enable(GetLastPosition() > 0);
1199 void wxTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
1201 // be sure the caret remains invisible if the user had hidden it
1202 if ( !m_isNativeCaretShown
)
1204 ::HideCaret(GetBuddyHwnd());
1208 #endif // wxUSE_TEXTCTRL && __SMARTPHONE__ && __WXWINCE__