Provide shorter synonyms for wxEVT_XXX constants.
[wxWidgets.git] / src / msw / wince / textctrlce.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/textctrlce.cpp
3 // Purpose: wxTextCtrl implementation for smart phones driven by WinCE
4 // Author: Wlodzimierz ABX Skiba
5 // Modified by:
6 // Created: 30.08.2004
7 // RCS-ID: $Id$
8 // Copyright: (c) Wlodzimierz Skiba
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TEXTCTRL && defined(__SMARTPHONE__) && defined(__WXWINCE__)
28
29 #include "wx/textctrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #endif
34
35 #include "wx/spinbutt.h"
36 #include "wx/textfile.h"
37
38 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
39
40 #define IsVertical(wxStyle) (true)
41
42 // ----------------------------------------------------------------------------
43 // event tables and other macros
44 // ----------------------------------------------------------------------------
45
46 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
47 EVT_CHAR(wxTextCtrl::OnChar)
48
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)
56
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)
64
65 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus)
66 END_EVENT_TABLE()
67
68 // ----------------------------------------------------------------------------
69 // constants
70 // ----------------------------------------------------------------------------
71
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
74 // font size?)
75 static const int MARGIN_BETWEEN = 0;
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 wxArrayTextSpins wxTextCtrl::ms_allTextSpins;
82
83 // ----------------------------------------------------------------------------
84 // wnd proc for the buddy text ctrl
85 // ----------------------------------------------------------------------------
86
87 LRESULT APIENTRY _EXPORT wxBuddyTextCtrlWndProc(HWND hwnd,
88 UINT message,
89 WPARAM wParam,
90 LPARAM lParam)
91 {
92 wxTextCtrl *spin = (wxTextCtrl *)wxGetWindowUserData(hwnd);
93
94 // forward some messages (the key and focus ones only so far) to
95 // the spin ctrl
96 switch ( message )
97 {
98 case WM_SETFOCUS:
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() )
102 break;
103 //else: fall through
104
105 case WM_KILLFOCUS:
106 case WM_CHAR:
107 case WM_DEADCHAR:
108 case WM_KEYUP:
109 case WM_KEYDOWN:
110 spin->MSWWindowProc(message, wParam, lParam);
111
112 // The control may have been deleted at this point, so check.
113 if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin )
114 return 0;
115 break;
116
117 case WM_GETDLGCODE:
118 // we want to get WXK_RETURN in order to generate the event for it
119 return DLGC_WANTCHARS;
120 }
121
122 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
123 hwnd, message, wParam, lParam);
124 }
125
126 // ----------------------------------------------------------------------------
127 // creation
128 // ----------------------------------------------------------------------------
129
130 void wxTextCtrl::Init()
131 {
132 m_suppressNextUpdate = false;
133 m_isNativeCaretShown = true;
134 }
135
136 wxTextCtrl::~wxTextCtrl()
137 {
138 }
139
140 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
141 const wxString& value,
142 const wxPoint& pos,
143 const wxSize& size,
144 long style,
145 const wxValidator& validator,
146 const wxString& name)
147 {
148 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
149 style |= wxBORDER_SIMPLE;
150
151 SetWindowStyle(style);
152
153 WXDWORD exStyle = 0;
154 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
155
156 wxSize sizeText(size), sizeBtn(size);
157 sizeBtn.x = GetBestSpinnerSize(IsVertical(style)).x / 2;
158
159 if ( sizeText.x == wxDefaultCoord )
160 {
161 // DEFAULT_ITEM_WIDTH is the default width for the text control
162 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
163 }
164
165 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
166 if ( sizeText.x <= 0 )
167 {
168 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
169 }
170
171 wxPoint posBtn(pos);
172 posBtn.x += sizeText.x + MARGIN_BETWEEN;
173
174 // we need to turn '\n's into "\r\n"s for the multiline controls
175 wxString valueWin;
176 if ( m_windowStyle & wxTE_MULTILINE )
177 {
178 valueWin = wxTextFile::Translate(value, wxTextFileType_Dos);
179 }
180 else // single line
181 {
182 valueWin = value;
183 }
184
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
190
191 // create the text window
192
193 m_hwndBuddy = (WXHWND)::CreateWindowEx
194 (
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
205 );
206
207 if ( !m_hwndBuddy )
208 {
209 wxLogLastError(wxT("CreateWindow(buddy text window)"));
210
211 return false;
212 }
213
214 // initialize wxControl
215 if ( !CreateControl(parent, id, posBtn, sizeBtn, style, validator, name) )
216 return false;
217
218 // now create the real HWND
219 WXDWORD spiner_style = WS_VISIBLE |
220 UDS_ALIGNRIGHT |
221 UDS_EXPANDABLE |
222 UDS_NOSCROLL;
223
224 if ( !IsVertical(style) )
225 spiner_style |= UDS_HORZ;
226
227 if ( style & wxSP_WRAP )
228 spiner_style |= UDS_WRAP;
229
230 if ( !MSWCreateControl(UPDOWN_CLASS, spiner_style, posBtn, sizeBtn, wxT(""), 0) )
231 return false;
232
233 // subclass the text ctrl to be able to intercept some events
234 wxSetWindowUserData(GetBuddyHwnd(), this);
235 m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
236 wxBuddyTextCtrlWndProc);
237
238 // set up fonts and colours (This is nomally done in MSWCreateControl)
239 InheritAttributes();
240 if (!m_hasFont)
241 SetFont(GetDefaultAttributes().font);
242
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 )
246 {
247 int cx, cy;
248 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
249
250 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
251 }
252
253 SetInitialSize(size);
254
255 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
256
257 // associate the list window with the spin button
258 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0);
259
260 // do it after finishing with m_hwndBuddy creation to avoid generating
261 // initial wxEVT_TEXT message
262 ms_allTextSpins.Add(this);
263
264 return true;
265 }
266
267 // Make sure the window style (etc.) reflects the HWND style (roughly)
268 void wxTextCtrl::AdoptAttributesFromHWND()
269 {
270 wxWindow::AdoptAttributesFromHWND();
271
272 long style = ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE);
273
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;
286 }
287
288 WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
289 {
290 // we never have an external border
291 WXDWORD msStyle = wxControl::MSWGetStyle
292 (
293 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
294 );
295
296 msStyle |= WS_VISIBLE;
297
298 // styles which we alaways add by default
299 if ( style & wxTE_MULTILINE )
300 {
301 wxASSERT_MSG( !(style & wxTE_PROCESS_ENTER),
302 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
303
304 msStyle |= ES_MULTILINE | ES_WANTRETURN;
305 if ( !(style & wxTE_NO_VSCROLL) )
306 {
307 // always adjust the vertical scrollbar automatically if we have it
308 msStyle |= WS_VSCROLL | ES_AUTOVSCROLL;
309 }
310
311 style |= wxTE_PROCESS_ENTER;
312 }
313 else // !multiline
314 {
315 // there is really no reason to not have this style for single line
316 // text controls
317 msStyle |= ES_AUTOHSCROLL;
318 }
319
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 )
323 {
324 // automatically scroll the control horizontally as necessary
325 //
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
329 // always
330 msStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
331 }
332
333 if ( style & wxTE_READONLY )
334 msStyle |= ES_READONLY;
335
336 if ( style & wxTE_PASSWORD )
337 msStyle |= ES_PASSWORD;
338
339 if ( style & wxTE_NOHIDESEL )
340 msStyle |= ES_NOHIDESEL;
341
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 )
346 msStyle |= ES_RIGHT;
347 else
348 msStyle |= ES_LEFT; // ES_LEFT is 0 as well but for consistency...
349
350 return msStyle;
351 }
352
353 // ----------------------------------------------------------------------------
354 // set/get the controls text
355 // ----------------------------------------------------------------------------
356
357 wxString wxTextCtrl::GetValue() const
358 {
359 // range 0..-1 is special for GetRange() and means to retrieve all text
360 return GetRange(0, -1);
361 }
362
363 wxString wxTextCtrl::GetRange(long from, long to) const
364 {
365 wxString str;
366
367 if ( from >= to && to != -1 )
368 {
369 // nothing to retrieve
370 return str;
371 }
372
373 // retrieve all text
374 str = wxGetWindowText(GetBuddyHwnd());
375
376 // need only a range?
377 if ( from < to )
378 {
379 str = str.Mid(from, to - from);
380 }
381
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);
386
387 return str;
388 }
389
390 void wxTextCtrl::DoSetValue(const wxString& value, int flags)
391 {
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()) )
397 {
398 DoWriteText(value, flags);
399
400 // for compatibility, don't move the cursor when doing SetValue()
401 SetInsertionPoint(0);
402 }
403 else // same text
404 {
405 // still send an event for consistency
406 if ( flags & SetValue_SendEvent )
407 SendUpdateEvent();
408 }
409
410 // we should reset the modified flag even if the value didn't really change
411
412 // mark the control as being not dirty - we changed its text, not the
413 // user
414 DiscardEdits();
415 }
416
417 void wxTextCtrl::WriteText(const wxString& value)
418 {
419 DoWriteText(value);
420 }
421
422 void wxTextCtrl::DoWriteText(const wxString& value, int flags)
423 {
424 bool selectionOnly = (flags & SetValue_SelectionOnly) != 0;
425 wxString valueDos;
426 if ( m_windowStyle & wxTE_MULTILINE )
427 valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
428 else
429 valueDos = value;
430
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
433 // avoided
434 //
435 if ( selectionOnly && HasSelection() )
436 {
437 m_suppressNextUpdate = true;
438 }
439
440 ::SendMessage(GetBuddyHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT,
441 0, (LPARAM)valueDos.c_str());
442
443 if ( !selectionOnly && !( flags & SetValue_SendEvent ) )
444 {
445 // Windows already sends an update event for single-line
446 // controls.
447 if ( m_windowStyle & wxTE_MULTILINE )
448 SendUpdateEvent();
449 }
450
451 AdjustSpaceLimit();
452 }
453
454 void wxTextCtrl::AppendText(const wxString& text)
455 {
456 SetInsertionPointEnd();
457
458 WriteText(text);
459 }
460
461 void wxTextCtrl::Clear()
462 {
463 ::SetWindowText(GetBuddyHwnd(), wxEmptyString);
464
465 // Windows already sends an update event for single-line
466 // controls.
467 if ( m_windowStyle & wxTE_MULTILINE )
468 SendUpdateEvent();
469 }
470
471 // ----------------------------------------------------------------------------
472 // Clipboard operations
473 // ----------------------------------------------------------------------------
474
475 void wxTextCtrl::Copy()
476 {
477 if (CanCopy())
478 {
479 ::SendMessage(GetBuddyHwnd(), WM_COPY, 0, 0L);
480 }
481 }
482
483 void wxTextCtrl::Cut()
484 {
485 if (CanCut())
486 {
487 ::SendMessage(GetBuddyHwnd(), WM_CUT, 0, 0L);
488 }
489 }
490
491 void wxTextCtrl::Paste()
492 {
493 if (CanPaste())
494 {
495 ::SendMessage(GetBuddyHwnd(), WM_PASTE, 0, 0L);
496 }
497 }
498
499 bool wxTextCtrl::HasSelection() const
500 {
501 long from, to;
502 GetSelection(&from, &to);
503 return from != to;
504 }
505
506 bool wxTextCtrl::CanCopy() const
507 {
508 // Can copy if there's a selection
509 return HasSelection();
510 }
511
512 bool wxTextCtrl::CanCut() const
513 {
514 return CanCopy() && IsEditable();
515 }
516
517 bool wxTextCtrl::CanPaste() const
518 {
519 if ( !IsEditable() )
520 return false;
521
522 // Standard edit control: check for straight text on clipboard
523 if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
524 return false;
525
526 bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0;
527 ::CloseClipboard();
528
529 return isTextAvailable;
530 }
531
532 // ----------------------------------------------------------------------------
533 // Accessors
534 // ----------------------------------------------------------------------------
535
536 void wxTextCtrl::SetEditable(bool editable)
537 {
538 ::SendMessage(GetBuddyHwnd(), EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
539 }
540
541 void wxTextCtrl::SetInsertionPoint(long pos)
542 {
543 DoSetSelection(pos, pos);
544 }
545
546 void wxTextCtrl::SetInsertionPointEnd()
547 {
548 if ( GetInsertionPoint() != GetLastPosition() )
549 SetInsertionPoint(GetLastPosition());
550 }
551
552 long wxTextCtrl::GetInsertionPoint() const
553 {
554 DWORD Pos = (DWORD)::SendMessage(GetBuddyHwnd(), EM_GETSEL, 0, 0L);
555 return Pos & 0xFFFF;
556 }
557
558 wxTextPos wxTextCtrl::GetLastPosition() const
559 {
560 int numLines = GetNumberOfLines();
561 long posStartLastLine = XYToPosition(0, numLines - 1);
562
563 long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine);
564
565 return posStartLastLine + lenLastLine;
566 }
567
568 void wxTextCtrl::GetSelection(long* from, long* to) const
569 {
570 DWORD dwStart, dwEnd;
571 ::SendMessage(GetBuddyHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
572
573 *from = dwStart;
574 *to = dwEnd;
575 }
576
577 bool wxTextCtrl::IsEditable() const
578 {
579 if ( !GetBuddyHwnd() )
580 return true;
581
582 long style = ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE);
583
584 return (style & ES_READONLY) == 0;
585 }
586
587 // ----------------------------------------------------------------------------
588 // selection
589 // ----------------------------------------------------------------------------
590
591 void wxTextCtrl::SetSelection(long from, long to)
592 {
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) )
596 {
597 from = 0;
598 to = -1;
599 }
600
601 DoSetSelection(from, to);
602 }
603
604 void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret)
605 {
606 ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to);
607
608 if ( scrollCaret )
609 {
610 ::SendMessage(GetBuddyHwnd(), EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
611 }
612 }
613
614 // ----------------------------------------------------------------------------
615 // Working with files
616 // ----------------------------------------------------------------------------
617
618 bool wxTextCtrl::LoadFile(const wxString& file)
619 {
620 if ( wxTextCtrlBase::LoadFile(file) )
621 {
622 // update the size limit if needed
623 AdjustSpaceLimit();
624
625 return true;
626 }
627
628 return false;
629 }
630
631 // ----------------------------------------------------------------------------
632 // Editing
633 // ----------------------------------------------------------------------------
634
635 void wxTextCtrl::Replace(long from, long to, const wxString& value)
636 {
637 // Set selection and remove it
638 DoSetSelection(from, to, false);
639
640 DoWriteText(value, SetValue_SelectionOnly);
641 }
642
643 void wxTextCtrl::Remove(long from, long to)
644 {
645 Replace(from, to, wxEmptyString);
646 }
647
648 bool wxTextCtrl::IsModified() const
649 {
650 return ::SendMessage(GetBuddyHwnd(), EM_GETMODIFY, 0, 0) != 0;
651 }
652
653 void wxTextCtrl::MarkDirty()
654 {
655 ::SendMessage(GetBuddyHwnd(), EM_SETMODIFY, TRUE, 0L);
656 }
657
658 void wxTextCtrl::DiscardEdits()
659 {
660 ::SendMessage(GetBuddyHwnd(), EM_SETMODIFY, FALSE, 0L);
661 }
662
663 int wxTextCtrl::GetNumberOfLines() const
664 {
665 return (int)::SendMessage(GetBuddyHwnd(), EM_GETLINECOUNT, 0, 0L);
666 }
667
668 // ----------------------------------------------------------------------------
669 // Positions <-> coords
670 // ----------------------------------------------------------------------------
671
672 long wxTextCtrl::XYToPosition(long x, long y) const
673 {
674 // This gets the char index for the _beginning_ of this line
675 long charIndex = ::SendMessage(GetBuddyHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
676
677 return charIndex + x;
678 }
679
680 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
681 {
682 // This gets the line number containing the character
683 long lineNo = ::SendMessage(GetBuddyHwnd(), EM_LINEFROMCHAR, (WPARAM)pos, 0);
684
685 if ( lineNo == -1 )
686 {
687 // no such line
688 return false;
689 }
690
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 )
694 {
695 return false;
696 }
697
698 // The X position must therefore be the different between pos and charIndex
699 if ( x )
700 *x = pos - charIndex;
701 if ( y )
702 *y = lineNo;
703
704 return true;
705 }
706
707 wxTextCtrlHitTestResult
708 wxTextCtrl::HitTest(const wxPoint& pt, long *posOut) const
709 {
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);
714
715 LRESULT pos = ::SendMessage(GetBuddyHwnd(), EM_CHARFROMPOS, 0, lParam);
716
717 if ( pos == -1 )
718 {
719 // this seems to indicate an error...
720 return wxTE_HT_UNKNOWN;
721 }
722
723 // for plain EDIT controls the higher word contains something else
724 pos = LOWORD(pos);
725
726
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
730 POINTL ptReal;
731
732 LRESULT lRc = ::SendMessage(GetBuddyHwnd(), EM_POSFROMCHAR, pos, 0);
733
734 if ( lRc == -1 )
735 {
736 // this is apparently returned when pos corresponds to the last
737 // position
738 ptReal.x =
739 ptReal.y = 0;
740 }
741 else
742 {
743 ptReal.x = LOWORD(lRc);
744 ptReal.y = HIWORD(lRc);
745 }
746
747 wxTextCtrlHitTestResult rc;
748
749 if ( pt.y > ptReal.y + GetCharHeight() )
750 rc = wxTE_HT_BELOW;
751 else if ( pt.x > ptReal.x + GetCharWidth() )
752 rc = wxTE_HT_BEYOND;
753 else
754 rc = wxTE_HT_ON_TEXT;
755
756 if ( posOut )
757 *posOut = pos;
758
759 return rc;
760 }
761
762 void wxTextCtrl::ShowPosition(long pos)
763 {
764 int currentLineLineNo = (int)::SendMessage(GetBuddyHwnd(), EM_GETFIRSTVISIBLELINE, 0, 0L);
765
766 int specifiedLineLineNo = (int)::SendMessage(GetBuddyHwnd(), EM_LINEFROMCHAR, (WPARAM)pos, 0L);
767
768 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
769
770 if (linesToScroll != 0)
771 (void)::SendMessage(GetBuddyHwnd(), EM_LINESCROLL, 0, (LPARAM)linesToScroll);
772 }
773
774 long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const
775 {
776 return ::SendMessage(GetBuddyHwnd(), EM_LINELENGTH, (WPARAM)pos, 0L);
777 }
778
779 int wxTextCtrl::GetLineLength(long lineNo) const
780 {
781 long pos = XYToPosition(0, lineNo);
782
783 return GetLengthOfLineContainingPos(pos);
784 }
785
786 wxString wxTextCtrl::GetLineText(long lineNo) const
787 {
788 size_t len = (size_t)GetLineLength(lineNo) + 1;
789
790 // there must be at least enough place for the length WORD in the
791 // buffer
792 len += sizeof(WORD);
793
794 wxString str;
795 {
796 wxStringBufferLength tmp(str, len);
797 wxChar *buf = tmp;
798
799 *(WORD *)buf = (WORD)len;
800 len = (size_t)::SendMessage(GetBuddyHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
801
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') )
805 {
806 len--;
807 }
808
809 buf[len] = 0;
810 tmp.SetLength(len);
811 }
812
813 return str;
814 }
815
816 void wxTextCtrl::SetMaxLength(unsigned long len)
817 {
818 ::SendMessage(GetBuddyHwnd(), EM_LIMITTEXT, len, 0);
819 }
820
821 // ----------------------------------------------------------------------------
822 // Undo/redo
823 // ----------------------------------------------------------------------------
824
825 void wxTextCtrl::Undo()
826 {
827 if (CanUndo())
828 {
829 ::SendMessage(GetBuddyHwnd(), EM_UNDO, 0, 0);
830 }
831 }
832
833 void wxTextCtrl::Redo()
834 {
835 if (CanRedo())
836 {
837 ::SendMessage(GetBuddyHwnd(), EM_UNDO, 0, 0);
838 }
839 }
840
841 bool wxTextCtrl::CanUndo() const
842 {
843 return ::SendMessage(GetBuddyHwnd(), EM_CANUNDO, 0, 0) != 0;
844 }
845
846 bool wxTextCtrl::CanRedo() const
847 {
848 return ::SendMessage(GetBuddyHwnd(), EM_CANUNDO, 0, 0) != 0;
849 }
850
851 // ----------------------------------------------------------------------------
852 // caret handling
853 // ----------------------------------------------------------------------------
854
855 // ----------------------------------------------------------------------------
856 // implemenation details
857 // ----------------------------------------------------------------------------
858
859 void wxTextCtrl::Command(wxCommandEvent & event)
860 {
861 SetValue(event.GetString());
862 ProcessCommand (event);
863 }
864
865 // ----------------------------------------------------------------------------
866 // kbd input processing
867 // ----------------------------------------------------------------------------
868
869 void wxTextCtrl::OnChar(wxKeyEvent& event)
870 {
871 switch ( event.GetKeyCode() )
872 {
873 case WXK_RETURN:
874 if ( !HasFlag(wxTE_MULTILINE) )
875 {
876 wxCommandEvent event(wxEVT_TEXT_ENTER, m_windowId);
877 InitCommandEvent(event);
878 event.SetString(GetValue());
879 if ( GetEventHandler()->ProcessEvent(event) )
880 return;
881 }
882 //else: multiline controls need Enter for themselves
883
884 break;
885
886 case WXK_TAB:
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
896 // control
897 //
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))
902 {
903 if ( FindFocus() == this )
904 {
905 int flags = 0;
906 if (!event.ShiftDown())
907 flags |= wxNavigationKeyEvent::IsForward ;
908 if (event.ControlDown())
909 flags |= wxNavigationKeyEvent::WinChange ;
910 if (Navigate(flags))
911 return;
912 }
913 }
914 else
915 {
916 // Insert tab since calling the default Windows handler
917 // doesn't seem to do it
918 WriteText(wxT("\t"));
919 }
920 break;
921 }
922
923 // no, we didn't process it
924 event.Skip();
925 }
926
927 WXLRESULT wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
928 {
929 WXLRESULT lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
930
931 if ( nMsg == WM_GETDLGCODE )
932 {
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
935 // control
936 long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
937
938 if ( IsEditable() )
939 {
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
945 // navigation
946 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
947 // to the next control
948
949 // the multiline edit control should always get <Return> for itself
950 if ( HasFlag(wxTE_PROCESS_ENTER) || HasFlag(wxTE_MULTILINE) )
951 lDlgCode |= DLGC_WANTMESSAGE;
952
953 if ( HasFlag(wxTE_PROCESS_TAB) )
954 lDlgCode |= DLGC_WANTTAB;
955
956 lRc |= lDlgCode;
957 }
958 else // !editable
959 {
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.
964 lRc = lDlgCode;
965 }
966 }
967
968 return lRc;
969 }
970
971 // ----------------------------------------------------------------------------
972 // text control event processing
973 // ----------------------------------------------------------------------------
974
975 bool wxTextCtrl::SendUpdateEvent()
976 {
977 // is event reporting suspended?
978 if ( m_suppressNextUpdate )
979 {
980 // do process the next one
981 m_suppressNextUpdate = false;
982
983 return false;
984 }
985
986 wxCommandEvent event(wxEVT_TEXT, GetId());
987 InitCommandEvent(event);
988 event.SetString(GetValue());
989
990 return ProcessCommand(event);
991 }
992
993 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
994 {
995 switch ( param )
996 {
997 case EN_SETFOCUS:
998 case EN_KILLFOCUS:
999 {
1000 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1001 : wxEVT_SET_FOCUS,
1002 m_windowId);
1003 event.SetEventObject(this);
1004 GetEventHandler()->ProcessEvent(event);
1005 }
1006 break;
1007
1008 case EN_CHANGE:
1009 SendUpdateEvent();
1010 break;
1011
1012 case EN_MAXTEXT:
1013 // the text size limit has been hit -- try to increase it
1014 if ( !AdjustSpaceLimit() )
1015 {
1016 wxCommandEvent event(wxEVT_TEXT_MAXLEN, m_windowId);
1017 InitCommandEvent(event);
1018 event.SetString(GetValue());
1019 ProcessCommand(event);
1020 }
1021 break;
1022
1023 // the other edit notification messages are not processed
1024 default:
1025 return false;
1026 }
1027
1028 // processed
1029 return true;
1030 }
1031
1032 bool wxTextCtrl::AdjustSpaceLimit()
1033 {
1034 unsigned int limit = ::SendMessage(GetBuddyHwnd(), EM_GETLIMITTEXT, 0, 0);
1035
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()
1040 //
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
1044 // values < 32Kb
1045 if ( limit < 0x8000 )
1046 {
1047 // we've got more text than limit set by SetMaxLength()
1048 return false;
1049 }
1050
1051 unsigned int len = ::GetWindowTextLength(GetBuddyHwnd());
1052 if ( len >= limit )
1053 {
1054 limit = len + 0x8000; // 32Kb
1055
1056 if ( limit > 0xffff )
1057 {
1058 // this will set it to a platform-dependent maximum (much more
1059 // than 64Kb under NT)
1060 limit = 0;
1061 }
1062
1063 ::SendMessage(GetBuddyHwnd(), EM_LIMITTEXT, limit, 0L);
1064 }
1065
1066 // we changed the limit
1067 return true;
1068 }
1069
1070 bool wxTextCtrl::AcceptsFocus() const
1071 {
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();
1076 }
1077
1078 void wxTextCtrl::DoMoveWindow(int x, int y, int width, int height)
1079 {
1080 int widthBtn = GetBestSpinnerSize(IsVertical(GetWindowStyle())).x / 2;
1081 int widthText = width - widthBtn - MARGIN_BETWEEN;
1082 if ( widthText <= 0 )
1083 {
1084 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
1085 }
1086
1087 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
1088 {
1089 wxLogLastError(wxT("MoveWindow(buddy)"));
1090 }
1091
1092 x += widthText + MARGIN_BETWEEN;
1093 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
1094 {
1095 wxLogLastError(wxT("MoveWindow"));
1096 }
1097 }
1098
1099 wxSize wxTextCtrl::DoGetBestSize() const
1100 {
1101 int cx, cy;
1102 wxGetCharSize(GetBuddyHwnd(), &cx, &cy, GetFont());
1103
1104 int wText = DEFAULT_ITEM_WIDTH;
1105
1106 int hText = cy;
1107 if ( m_windowStyle & wxTE_MULTILINE )
1108 {
1109 hText *= wxMax(GetNumberOfLines(), 5);
1110 }
1111 //else: for single line control everything is ok
1112
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;
1116
1117 return wxSize(wText, hText);
1118 }
1119
1120 // ----------------------------------------------------------------------------
1121 // standard handlers for standard edit menu events
1122 // ----------------------------------------------------------------------------
1123
1124 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1125 {
1126 Cut();
1127 }
1128
1129 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1130 {
1131 Copy();
1132 }
1133
1134 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1135 {
1136 Paste();
1137 }
1138
1139 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1140 {
1141 Undo();
1142 }
1143
1144 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1145 {
1146 Redo();
1147 }
1148
1149 void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
1150 {
1151 long from, to;
1152 GetSelection(& from, & to);
1153 if (from != -1 && to != -1)
1154 Remove(from, to);
1155 }
1156
1157 void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1158 {
1159 SetSelection(-1, -1);
1160 }
1161
1162 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1163 {
1164 event.Enable( CanCut() );
1165 }
1166
1167 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1168 {
1169 event.Enable( CanCopy() );
1170 }
1171
1172 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1173 {
1174 event.Enable( CanPaste() );
1175 }
1176
1177 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1178 {
1179 event.Enable( CanUndo() );
1180 }
1181
1182 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1183 {
1184 event.Enable( CanRedo() );
1185 }
1186
1187 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
1188 {
1189 long from, to;
1190 GetSelection(& from, & to);
1191 event.Enable(from != -1 && to != -1 && from != to && IsEditable()) ;
1192 }
1193
1194 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
1195 {
1196 event.Enable(GetLastPosition() > 0);
1197 }
1198
1199 void wxTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
1200 {
1201 // be sure the caret remains invisible if the user had hidden it
1202 if ( !m_isNativeCaretShown )
1203 {
1204 ::HideCaret(GetBuddyHwnd());
1205 }
1206 }
1207
1208 #endif // wxUSE_TEXTCTRL && __SMARTPHONE__ && __WXWINCE__