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