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