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