]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textctrl.cpp
Bugfix in GSocket_Cleanup()
[wxWidgets.git] / src / msw / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose: wxTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "textctrl.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/textctrl.h"
33 #include "wx/settings.h"
34 #include "wx/brush.h"
35 #include "wx/utils.h"
36 #include "wx/log.h"
37 #endif
38
39 #if wxUSE_CLIPBOARD
40 #include "wx/app.h"
41 #include "wx/clipbrd.h"
42 #endif
43
44 #include "wx/textfile.h"
45
46 #include <windowsx.h>
47
48 #include "wx/msw/private.h"
49
50 #include <string.h>
51 #include <stdlib.h>
52 #include <sys/types.h>
53
54 #if wxUSE_IOSTREAMH
55 # include <fstream.h>
56 #else
57 # include <fstream>
58 #endif
59
60 #if wxUSE_RICHEDIT && (!defined(__GNUWIN32__) || defined(wxUSE_NORLANDER_HEADERS))
61 #include <richedit.h>
62 #endif
63
64 #if !USE_SHARED_LIBRARY
65
66 // ----------------------------------------------------------------------------
67 // event tables and other macros
68 // ----------------------------------------------------------------------------
69
70 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
71
72 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
73 EVT_CHAR(wxTextCtrl::OnChar)
74 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
75
76 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
77 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
78 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
79 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
80 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
81
82 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
83 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
84 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
85 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
86 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
87 END_EVENT_TABLE()
88
89 #endif // USE_SHARED_LIBRARY
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // creation
97 // ----------------------------------------------------------------------------
98
99 wxTextCtrl::wxTextCtrl()
100 {
101 #if wxUSE_RICHEDIT
102 m_isRich = FALSE;
103 #endif
104 }
105
106 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
107 const wxString& value,
108 const wxPoint& pos,
109 const wxSize& size,
110 long style,
111 const wxValidator& validator,
112 const wxString& name)
113 {
114 // base initialization
115 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
116 return FALSE;
117
118 SetValidator(validator);
119 if ( parent )
120 parent->AddChild(this);
121
122 // set colours
123 SetupColours();
124
125 // translate wxWin style flags to MSW ones, checking for consistency while
126 // doing it
127 long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
128 if ( m_windowStyle & wxTE_MULTILINE )
129 {
130 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
131 _T("wxTE_PROCESS_ENTER style is ignored for multiline "
132 "text controls (they always process it)") );
133
134 msStyle |= ES_MULTILINE | ES_WANTRETURN;
135 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
136 msStyle |= WS_VSCROLL;
137 m_windowStyle |= wxTE_PROCESS_ENTER;
138 }
139 else
140 msStyle |= ES_AUTOHSCROLL;
141
142 if (m_windowStyle & wxTE_READONLY)
143 msStyle |= ES_READONLY;
144
145 if (m_windowStyle & wxHSCROLL)
146 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
147 if (m_windowStyle & wxTE_PASSWORD) // hidden input
148 msStyle |= ES_PASSWORD;
149
150 // we always want the characters and the arrows
151 m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
152
153 // we may have several different cases:
154 // 1. normal case: both TAB and ENTER are used for dialog navigation
155 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
156 // control in the dialog
157 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
158 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
159 // the next control
160 if ( m_windowStyle & wxTE_PROCESS_ENTER )
161 m_lDlgCode |= DLGC_WANTMESSAGE;
162 if ( m_windowStyle & wxTE_PROCESS_TAB )
163 m_lDlgCode |= DLGC_WANTTAB;
164
165 // do create the control - either an EDIT or RICHEDIT
166 const wxChar *windowClass = _T("EDIT");
167
168 #if wxUSE_RICHEDIT
169 if ( m_windowStyle & wxTE_RICH )
170 {
171 msStyle |= ES_AUTOVSCROLL;
172 m_isRich = TRUE;
173 windowClass = _T("RICHEDIT");
174 }
175 else
176 m_isRich = FALSE;
177 #endif
178
179 bool want3D;
180 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
181
182 // Even with extended styles, need to combine with WS_BORDER for them to
183 // look right.
184 if ( want3D || wxStyleHasBorder(m_windowStyle) )
185 msStyle |= WS_BORDER;
186
187 // NB: don't use pos and size as CreateWindowEx arguments because they
188 // might be -1 in which case we should use the default values (and
189 // SetSize called below takes care of it)
190 m_hWnd = (WXHWND)::CreateWindowEx(exStyle,
191 windowClass,
192 NULL,
193 msStyle,
194 0, 0, 0, 0,
195 GetHwndOf(parent),
196 (HMENU)m_windowId,
197 wxGetInstance(),
198 NULL);
199
200 wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create text ctrl") );
201
202 #if wxUSE_CTL3D
203 if ( want3D )
204 {
205 Ctl3dSubclassCtl(GetHwnd());
206 m_useCtl3D = TRUE;
207 }
208 #endif
209
210 #if wxUSE_RICHEDIT
211 if (m_isRich)
212 {
213 // Have to enable events
214 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0,
215 ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE);
216 }
217 #endif
218
219 SubclassWin(GetHWND());
220
221 // set font, position, size and initial value
222 wxFont& fontParent = parent->GetFont();
223 if ( fontParent.Ok() )
224 {
225 SetFont(fontParent);
226 }
227 else
228 {
229 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT));
230 }
231
232 // Causes a crash for Symantec C++ and WIN32 for some reason
233 #if !(defined(__SC__) && defined(__WIN32__))
234 if ( !value.IsEmpty() )
235 {
236 SetValue(value);
237 }
238 #endif
239
240 SetSize(pos.x, pos.y, size.x, size.y);
241
242 return TRUE;
243 }
244
245 // Make sure the window style (etc.) reflects the HWND style (roughly)
246 void wxTextCtrl::AdoptAttributesFromHWND()
247 {
248 wxWindow::AdoptAttributesFromHWND();
249
250 HWND hWnd = GetHwnd();
251 long style = GetWindowLong(hWnd, GWL_STYLE);
252
253 // retrieve the style to see whether this is an edit or richedit ctrl
254 #if wxUSE_RICHEDIT
255 wxChar buf[256];
256
257 GetClassName(hWnd, buf, WXSIZEOF(buf));
258
259 if ( wxStricmp(buf, _T("EDIT")) == 0 )
260 m_isRich = FALSE;
261 else
262 m_isRich = TRUE;
263 #endif // wxUSE_RICHEDIT
264
265 if (style & ES_MULTILINE)
266 m_windowStyle |= wxTE_MULTILINE;
267 if (style & ES_PASSWORD)
268 m_windowStyle |= wxTE_PASSWORD;
269 if (style & ES_READONLY)
270 m_windowStyle |= wxTE_READONLY;
271 if (style & ES_WANTRETURN)
272 m_windowStyle |= wxTE_PROCESS_ENTER;
273 }
274
275 void wxTextCtrl::SetupColours()
276 {
277 // FIXME why is bg colour not inherited from parent?
278 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
279 SetForegroundColour(GetParent()->GetForegroundColour());
280 }
281
282 // ----------------------------------------------------------------------------
283 // set/get the controls text
284 // ----------------------------------------------------------------------------
285
286 wxString wxTextCtrl::GetValue() const
287 {
288 return wxGetWindowText(GetHWND());
289 }
290
291 void wxTextCtrl::SetValue(const wxString& value)
292 {
293 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
294
295 SetWindowText(GetHwnd(), valueDos);
296
297 AdjustSpaceLimit();
298 }
299
300 void wxTextCtrl::WriteText(const wxString& value)
301 {
302 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
303
304 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
305
306 AdjustSpaceLimit();
307 }
308
309 void wxTextCtrl::AppendText(const wxString& text)
310 {
311 SetInsertionPointEnd();
312 WriteText(text);
313 }
314
315 void wxTextCtrl::Clear()
316 {
317 SetWindowText(GetHwnd(), _T(""));
318 }
319
320 // ----------------------------------------------------------------------------
321 // Clipboard operations
322 // ----------------------------------------------------------------------------
323
324 void wxTextCtrl::Copy()
325 {
326 if (CanCopy())
327 {
328 HWND hWnd = GetHwnd();
329 SendMessage(hWnd, WM_COPY, 0, 0L);
330 }
331 }
332
333 void wxTextCtrl::Cut()
334 {
335 if (CanCut())
336 {
337 HWND hWnd = GetHwnd();
338 SendMessage(hWnd, WM_CUT, 0, 0L);
339 }
340 }
341
342 void wxTextCtrl::Paste()
343 {
344 if (CanPaste())
345 {
346 HWND hWnd = GetHwnd();
347 SendMessage(hWnd, WM_PASTE, 0, 0L);
348 }
349 }
350
351 bool wxTextCtrl::CanCopy() const
352 {
353 // Can copy if there's a selection
354 long from, to;
355 GetSelection(& from, & to);
356 return (from != to);
357 }
358
359 bool wxTextCtrl::CanCut() const
360 {
361 // Can cut if there's a selection
362 long from, to;
363 GetSelection(& from, & to);
364 return (from != to);
365 }
366
367 bool wxTextCtrl::CanPaste() const
368 {
369 #if wxUSE_RICHEDIT
370 if (m_isRich)
371 {
372 int dataFormat = 0; // 0 == any format
373 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
374 }
375 #endif
376 if (!IsEditable())
377 return FALSE;
378
379 // Standard edit control: check for straight text on clipboard
380 bool isTextAvailable = FALSE;
381 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
382 {
383 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
384 ::CloseClipboard();
385 }
386
387 return isTextAvailable;
388 }
389
390 // ----------------------------------------------------------------------------
391 // Accessors
392 // ----------------------------------------------------------------------------
393
394 void wxTextCtrl::SetEditable(bool editable)
395 {
396 HWND hWnd = GetHwnd();
397 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
398 }
399
400 void wxTextCtrl::SetInsertionPoint(long pos)
401 {
402 HWND hWnd = GetHwnd();
403 #ifdef __WIN32__
404 #if wxUSE_RICHEDIT
405 if ( m_isRich)
406 {
407 CHARRANGE range;
408 range.cpMin = pos;
409 range.cpMax = pos;
410 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
411 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
412 }
413 else
414 #endif // wxUSE_RICHEDIT
415 {
416 SendMessage(hWnd, EM_SETSEL, pos, pos);
417 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
418 }
419 #else // Win16
420 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
421 #endif // Win32/16
422
423 static const char *nothing = "";
424 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
425 }
426
427 void wxTextCtrl::SetInsertionPointEnd()
428 {
429 long pos = GetLastPosition();
430 SetInsertionPoint(pos);
431 }
432
433 long wxTextCtrl::GetInsertionPoint() const
434 {
435 #if wxUSE_RICHEDIT
436 if (m_isRich)
437 {
438 CHARRANGE range;
439 range.cpMin = 0;
440 range.cpMax = 0;
441 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
442 return range.cpMin;
443 }
444 #endif
445
446 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
447 return Pos & 0xFFFF;
448 }
449
450 long wxTextCtrl::GetLastPosition() const
451 {
452 HWND hWnd = GetHwnd();
453
454 // Will always return a number > 0 (according to docs)
455 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
456
457 // This gets the char index for the _beginning_ of the last line
458 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
459
460 // Get number of characters in the last line. We'll add this to the character
461 // index for the last line, 1st position.
462 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
463
464 return (long)(charIndex + lineLength);
465 }
466
467 // If the return values from and to are the same, there is no
468 // selection.
469 void wxTextCtrl::GetSelection(long* from, long* to) const
470 {
471 #if wxUSE_RICHEDIT
472 if (m_isRich)
473 {
474 CHARRANGE charRange;
475 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
476
477 *from = charRange.cpMin;
478 *to = charRange.cpMax;
479
480 return;
481 }
482 #endif
483 DWORD dwStart, dwEnd;
484 WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position
485 LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position
486
487 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
488
489 *from = dwStart;
490 *to = dwEnd;
491 }
492
493 bool wxTextCtrl::IsEditable() const
494 {
495 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
496
497 return ((style & ES_READONLY) == 0);
498 }
499
500 // ----------------------------------------------------------------------------
501 // Editing
502 // ----------------------------------------------------------------------------
503
504 void wxTextCtrl::Replace(long from, long to, const wxString& value)
505 {
506 #if wxUSE_CLIPBOARD
507 HWND hWnd = GetHwnd();
508 long fromChar = from;
509 long toChar = to;
510
511 // Set selection and remove it
512 #ifdef __WIN32__
513 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
514 #else
515 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
516 #endif
517 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
518
519 // Now replace with 'value', by pasting.
520 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
521
522 // Paste into edit control
523 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
524 #else
525 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
526 #endif
527 }
528
529 void wxTextCtrl::Remove(long from, long to)
530 {
531 HWND hWnd = GetHwnd();
532 long fromChar = from;
533 long toChar = to;
534
535 // Cut all selected text
536 #ifdef __WIN32__
537 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
538 #else
539 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
540 #endif
541 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
542 }
543
544 void wxTextCtrl::SetSelection(long from, long to)
545 {
546 HWND hWnd = GetHwnd();
547 long fromChar = from;
548 long toChar = to;
549
550 // if from and to are both -1, it means (in wxWindows) that all text should
551 // be selected. Translate into Windows convention
552 if ((from == -1) && (to == -1))
553 {
554 fromChar = 0;
555 toChar = -1;
556 }
557
558 #ifdef __WIN32__
559 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
560 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
561 #else
562 // WPARAM is 0: selection is scrolled into view
563 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
564 #endif
565 }
566
567 bool wxTextCtrl::LoadFile(const wxString& file)
568 {
569 if ( wxTextCtrlBase::LoadFile(file) )
570 {
571 // update the size limit if needed
572 AdjustSpaceLimit();
573
574 return TRUE;
575 }
576
577 return FALSE;
578 }
579
580 bool wxTextCtrl::IsModified() const
581 {
582 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
583 }
584
585 // Makes 'unmodified'
586 void wxTextCtrl::DiscardEdits()
587 {
588 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
589 }
590
591 int wxTextCtrl::GetNumberOfLines() const
592 {
593 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
594 }
595
596 long wxTextCtrl::XYToPosition(long x, long y) const
597 {
598 HWND hWnd = GetHwnd();
599
600 // This gets the char index for the _beginning_ of this line
601 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
602 return (long)(x + charIndex);
603 }
604
605 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
606 {
607 HWND hWnd = GetHwnd();
608
609 // This gets the line number containing the character
610 int lineNo;
611 #if wxUSE_RICHEDIT
612 if ( m_isRich )
613 {
614 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
615 }
616 else
617 #endif // wxUSE_RICHEDIT
618 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
619
620 if ( lineNo == -1 )
621 {
622 // no such line
623 return FALSE;
624 }
625
626 // This gets the char index for the _beginning_ of this line
627 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
628 if ( charIndex == -1 )
629 {
630 return FALSE;
631 }
632
633 // The X position must therefore be the different between pos and charIndex
634 if ( x )
635 *x = (long)(pos - charIndex);
636 if ( y )
637 *y = (long)lineNo;
638
639 return TRUE;
640 }
641
642 void wxTextCtrl::ShowPosition(long pos)
643 {
644 HWND hWnd = GetHwnd();
645
646 // To scroll to a position, we pass the number of lines and characters
647 // to scroll *by*. This means that we need to:
648 // (1) Find the line position of the current line.
649 // (2) Find the line position of pos.
650 // (3) Scroll by (pos - current).
651 // For now, ignore the horizontal scrolling.
652
653 // Is this where scrolling is relative to - the line containing the caret?
654 // Or is the first visible line??? Try first visible line.
655 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
656
657 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
658
659 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
660
661 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
662
663 if (linesToScroll != 0)
664 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
665 }
666
667 int wxTextCtrl::GetLineLength(long lineNo) const
668 {
669 long charIndex = XYToPosition(0, lineNo);
670 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
671 return len;
672 }
673
674 wxString wxTextCtrl::GetLineText(long lineNo) const
675 {
676 size_t len = (size_t)GetLineLength(lineNo) + 1;
677 char *buf = (char *)malloc(len);
678 *(WORD *)buf = len;
679 int noChars = (int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
680 buf[noChars] = 0;
681
682 wxString str(buf);
683
684 free(buf);
685
686 return str;
687 }
688
689 // ----------------------------------------------------------------------------
690 // Undo/redo
691 // ----------------------------------------------------------------------------
692
693 void wxTextCtrl::Undo()
694 {
695 if (CanUndo())
696 {
697 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
698 }
699 }
700
701 void wxTextCtrl::Redo()
702 {
703 if (CanRedo())
704 {
705 // Same as Undo, since Undo undoes the undo, i.e. a redo.
706 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
707 }
708 }
709
710 bool wxTextCtrl::CanUndo() const
711 {
712 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
713 }
714
715 bool wxTextCtrl::CanRedo() const
716 {
717 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
718 }
719
720 // ----------------------------------------------------------------------------
721 // implemenation details
722 // ----------------------------------------------------------------------------
723
724 void wxTextCtrl::Command(wxCommandEvent & event)
725 {
726 SetValue(event.GetString());
727 ProcessCommand (event);
728 }
729
730 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
731 {
732 // By default, load the first file into the text window.
733 if (event.GetNumberOfFiles() > 0)
734 {
735 LoadFile(event.GetFiles()[0]);
736 }
737 }
738
739 WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
740 WXUINT message, WXWPARAM wParam,
741 WXLPARAM lParam)
742 {
743 #if wxUSE_CTL3D
744 if ( m_useCtl3D )
745 {
746 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
747 return (WXHBRUSH) hbrush;
748 }
749 #endif
750
751 HDC hdc = (HDC)pDC;
752 SetBkMode(hdc, GetParent()->GetTransparentBackground() ? TRANSPARENT
753 : OPAQUE);
754
755 ::SetBkColor(hdc, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
756 ::SetTextColor(hdc, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
757
758 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
759
760 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
761 }
762
763 void wxTextCtrl::OnChar(wxKeyEvent& event)
764 {
765 switch ( event.KeyCode() )
766 {
767 case WXK_RETURN:
768 if ( !(m_windowStyle & wxTE_MULTILINE) )
769 {
770 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
771 event.SetEventObject( this );
772 if ( GetEventHandler()->ProcessEvent(event) )
773 return;
774 }
775 //else: multiline controls need Enter for themselves
776
777 break;
778
779 case WXK_TAB:
780 // always produce navigation event - even if we process TAB
781 // ourselves the fact that we got here means that the user code
782 // decided to skip processing of this TAB - probably to let it
783 // do its default job.
784 //
785 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
786 // handled by Windows
787 {
788 wxNavigationKeyEvent eventNav;
789 eventNav.SetDirection(!event.ShiftDown());
790 eventNav.SetWindowChange(FALSE);
791 eventNav.SetEventObject(this);
792
793 if ( GetEventHandler()->ProcessEvent(eventNav) )
794 return;
795 }
796 break;
797
798 default:
799 event.Skip();
800 return;
801 }
802
803 // don't just call event.Skip() because this will cause TABs and ENTERs
804 // be passed upwards and we don't always want this - instead process it
805 // right here
806
807 // FIXME
808 event.Skip();
809 }
810
811 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
812 {
813 switch (param)
814 {
815 case EN_SETFOCUS:
816 case EN_KILLFOCUS:
817 {
818 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
819 : wxEVT_SET_FOCUS,
820 m_windowId);
821 event.SetEventObject( this );
822 GetEventHandler()->ProcessEvent(event);
823 }
824 break;
825
826 case EN_CHANGE:
827 {
828 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
829 wxString val(GetValue());
830 if ( !val.IsNull() )
831 event.m_commandString = WXSTRINGCAST val;
832 event.SetEventObject( this );
833 ProcessCommand(event);
834 }
835 break;
836
837 case EN_ERRSPACE:
838 // the text size limit has been hit - increase it
839 AdjustSpaceLimit();
840 break;
841
842 // the other notification messages are not processed
843 case EN_UPDATE:
844 case EN_MAXTEXT:
845 case EN_HSCROLL:
846 case EN_VSCROLL:
847 default:
848 return FALSE;
849 }
850
851 // processed
852 return TRUE;
853 }
854
855 void wxTextCtrl::AdjustSpaceLimit()
856 {
857 #ifndef __WIN16__
858 unsigned int len = ::GetWindowTextLength(GetHwnd()),
859 limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
860 if ( len > limit )
861 {
862 limit = len + 0x8000; // 32Kb
863
864 #if wxUSE_RICHEDIT
865 if ( m_isRich || limit > 0xffff )
866 #else
867 if ( limit > 0xffff )
868 #endif
869 ::SendMessage(GetHwnd(), EM_LIMITTEXT, 0, limit);
870 else
871 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
872 }
873 #endif
874 }
875
876 bool wxTextCtrl::AcceptsFocus() const
877 {
878 // we don't want focus if we can't be edited
879 return IsEditable() && wxControl::AcceptsFocus();
880 }
881
882 wxSize wxTextCtrl::DoGetBestSize()
883 {
884 int cx, cy;
885 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
886
887 int wText = DEFAULT_ITEM_WIDTH;
888
889 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
890 if ( m_windowStyle & wxTE_MULTILINE )
891 {
892 hText *= wxMin(GetNumberOfLines(), 5);
893 }
894 //else: for single line control everything is ok
895
896 return wxSize(wText, hText);
897 }
898
899 // ----------------------------------------------------------------------------
900 // standard handlers for standard edit menu events
901 // ----------------------------------------------------------------------------
902
903 void wxTextCtrl::OnCut(wxCommandEvent& event)
904 {
905 Cut();
906 }
907
908 void wxTextCtrl::OnCopy(wxCommandEvent& event)
909 {
910 Copy();
911 }
912
913 void wxTextCtrl::OnPaste(wxCommandEvent& event)
914 {
915 Paste();
916 }
917
918 void wxTextCtrl::OnUndo(wxCommandEvent& event)
919 {
920 Undo();
921 }
922
923 void wxTextCtrl::OnRedo(wxCommandEvent& event)
924 {
925 Redo();
926 }
927
928 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
929 {
930 event.Enable( CanCut() );
931 }
932
933 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
934 {
935 event.Enable( CanCopy() );
936 }
937
938 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
939 {
940 event.Enable( CanPaste() );
941 }
942
943 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
944 {
945 event.Enable( CanUndo() );
946 }
947
948 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
949 {
950 event.Enable( CanRedo() );
951 }
952