]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textctrl.cpp
162933d4c1471f6e085f39a3c398e60dd76ecd98
[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 && (!defined(__GNUWIN32__) || defined(wxUSE_NORLANDER_HEADERS))
62 #include <richedit.h>
63 #endif
64
65 // ----------------------------------------------------------------------------
66 // private classes
67 // ----------------------------------------------------------------------------
68
69 #if wxUSE_RICHEDIT
70
71 // this module initializes RichEdit DLL if needed
72 class wxRichEditModule : public wxModule
73 {
74 public:
75 virtual bool OnInit();
76 virtual void OnExit();
77
78 // get the version currently loaded, -1 if none
79 static int GetLoadedVersion() { return ms_verRichEdit; }
80
81 // load the richedit DLL of at least of required version
82 static bool Load(int version = 1);
83
84 private:
85 // the handle to richedit DLL and the version of the DLL loaded
86 static HINSTANCE ms_hRichEdit;
87
88 // the DLL version loaded or -1 if none
89 static int ms_verRichEdit;
90
91 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
92 };
93
94 HINSTANCE wxRichEditModule::ms_hRichEdit = (HINSTANCE)NULL;
95 int wxRichEditModule::ms_verRichEdit = -1;
96
97 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
98
99 #endif // wxUSE_RICHEDIT
100
101 // ----------------------------------------------------------------------------
102 // event tables and other macros
103 // ----------------------------------------------------------------------------
104
105 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
106
107 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
108 EVT_CHAR(wxTextCtrl::OnChar)
109 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
110
111 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
112 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
113 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
114 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
115 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
116
117 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
118 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
119 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
120 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
121 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
122 END_EVENT_TABLE()
123
124
125 // ============================================================================
126 // implementation
127 // ============================================================================
128
129 // ----------------------------------------------------------------------------
130 // creation
131 // ----------------------------------------------------------------------------
132
133 wxTextCtrl::wxTextCtrl()
134 {
135 #if wxUSE_RICHEDIT
136 m_isRich = FALSE;
137 #endif
138 }
139
140 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
141 const wxString& value,
142 const wxPoint& pos,
143 const wxSize& size,
144 long style,
145 const wxValidator& validator,
146 const wxString& name)
147 {
148 // base initialization
149 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
150 return FALSE;
151
152 if ( parent )
153 parent->AddChild(this);
154
155 // translate wxWin style flags to MSW ones, checking for consistency while
156 // doing it
157 long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
158 if ( m_windowStyle & wxTE_MULTILINE )
159 {
160 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
161 wxT("wxTE_PROCESS_ENTER style is ignored for multiline "
162 "text controls (they always process it)") );
163
164 msStyle |= ES_MULTILINE | ES_WANTRETURN;
165 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
166 msStyle |= WS_VSCROLL;
167 m_windowStyle |= wxTE_PROCESS_ENTER;
168 }
169 else
170 msStyle |= ES_AUTOHSCROLL;
171
172 if (m_windowStyle & wxHSCROLL)
173 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
174
175 if (m_windowStyle & wxTE_READONLY)
176 msStyle |= ES_READONLY;
177
178 if (m_windowStyle & wxHSCROLL)
179 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
180 if (m_windowStyle & wxTE_PASSWORD) // hidden input
181 msStyle |= ES_PASSWORD;
182
183 // we always want the characters and the arrows
184 m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
185
186 // we may have several different cases:
187 // 1. normal case: both TAB and ENTER are used for dialog navigation
188 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
189 // control in the dialog
190 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
191 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
192 // the next control
193 if ( m_windowStyle & wxTE_PROCESS_ENTER )
194 m_lDlgCode |= DLGC_WANTMESSAGE;
195 if ( m_windowStyle & wxTE_PROCESS_TAB )
196 m_lDlgCode |= DLGC_WANTTAB;
197
198 // do create the control - either an EDIT or RICHEDIT
199 wxString windowClass = wxT("EDIT");
200
201 #if wxUSE_RICHEDIT
202 if ( m_windowStyle & wxTE_RICH )
203 {
204 static bool s_errorGiven = FALSE; // MT-FIXME
205
206 // only give the error msg once if the DLL can't be loaded
207 if ( !s_errorGiven )
208 {
209 // first try to load the RichEdit DLL (will do nothing if already
210 // done)
211 if ( !wxRichEditModule::Load() )
212 {
213 wxLogError(_("Impossible to create a rich edit control, "
214 "using simple text control instead. Please "
215 "reinstall riched32.dll"));
216
217 s_errorGiven = TRUE;
218 }
219 }
220
221 if ( s_errorGiven )
222 {
223 m_isRich = FALSE;
224 }
225 else
226 {
227 msStyle |= ES_AUTOVSCROLL;
228 m_isRich = TRUE;
229
230 int ver = wxRichEditModule::GetLoadedVersion();
231 if ( ver == 1 )
232 {
233 windowClass = wxT("RICHEDIT");
234 }
235 else
236 {
237 #ifndef RICHEDIT_CLASS
238 wxString RICHEDIT_CLASS;
239 RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), ver);
240 #ifdef wxUSE_UNICODE
241 RICHEDIT_CLASS += _T('W');
242 #else // ANSI
243 RICHEDIT_CLASS += _T('A');
244 #endif // Unicode/ANSI
245 #endif // !RICHEDIT_CLASS
246
247 windowClass = RICHEDIT_CLASS;
248 }
249 }
250 }
251 else
252 m_isRich = FALSE;
253 #endif // wxUSE_RICHEDIT
254
255 bool want3D;
256 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
257
258 // Even with extended styles, need to combine with WS_BORDER for them to
259 // look right.
260 if ( want3D || wxStyleHasBorder(m_windowStyle) )
261 msStyle |= WS_BORDER;
262
263 // NB: don't use pos and size as CreateWindowEx arguments because they
264 // might be -1 in which case we should use the default values (and
265 // SetSize called below takes care of it)
266 m_hWnd = (WXHWND)::CreateWindowEx(exStyle,
267 windowClass,
268 NULL,
269 msStyle,
270 0, 0, 0, 0,
271 GetHwndOf(parent),
272 (HMENU)m_windowId,
273 wxGetInstance(),
274 NULL);
275
276 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create text ctrl") );
277
278 #if wxUSE_CTL3D
279 if ( want3D )
280 {
281 Ctl3dSubclassCtl(GetHwnd());
282 m_useCtl3D = TRUE;
283 }
284 #endif
285
286 #if wxUSE_RICHEDIT
287 if (m_isRich)
288 {
289 // Have to enable events
290 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0,
291 ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE);
292 }
293 #endif
294
295 SubclassWin(GetHWND());
296
297 // set font, position, size and initial value
298 wxFont& fontParent = parent->GetFont();
299 if ( fontParent.Ok() )
300 {
301 SetFont(fontParent);
302 }
303 else
304 {
305 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT));
306 }
307
308 // Causes a crash for Symantec C++ and WIN32 for some reason
309 #if !(defined(__SC__) && defined(__WIN32__))
310 if ( !value.IsEmpty() )
311 {
312 SetValue(value);
313 }
314 #endif
315
316 // set colours
317 SetupColours();
318
319 SetSize(pos.x, pos.y, size.x, size.y);
320
321 return TRUE;
322 }
323
324 // Make sure the window style (etc.) reflects the HWND style (roughly)
325 void wxTextCtrl::AdoptAttributesFromHWND()
326 {
327 wxWindow::AdoptAttributesFromHWND();
328
329 HWND hWnd = GetHwnd();
330 long style = GetWindowLong(hWnd, GWL_STYLE);
331
332 // retrieve the style to see whether this is an edit or richedit ctrl
333 #if wxUSE_RICHEDIT
334 wxChar buf[256];
335
336 GetClassName(hWnd, buf, WXSIZEOF(buf));
337
338 if ( wxStricmp(buf, wxT("EDIT")) == 0 )
339 m_isRich = FALSE;
340 else
341 m_isRich = TRUE;
342 #endif // wxUSE_RICHEDIT
343
344 if (style & ES_MULTILINE)
345 m_windowStyle |= wxTE_MULTILINE;
346 if (style & ES_PASSWORD)
347 m_windowStyle |= wxTE_PASSWORD;
348 if (style & ES_READONLY)
349 m_windowStyle |= wxTE_READONLY;
350 if (style & ES_WANTRETURN)
351 m_windowStyle |= wxTE_PROCESS_ENTER;
352 }
353
354 void wxTextCtrl::SetupColours()
355 {
356 // FIXME why is bg colour not inherited from parent?
357
358 wxColour bkgndColour;
359 if (IsEditable())
360 bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW);
361 else
362 bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
363
364 SetBackgroundColour(bkgndColour);
365 SetForegroundColour(GetParent()->GetForegroundColour());
366 }
367
368 // ----------------------------------------------------------------------------
369 // set/get the controls text
370 // ----------------------------------------------------------------------------
371
372 wxString wxTextCtrl::GetValue() const
373 {
374 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
375 // retrieving more than 64Kb under Win9x
376 #if wxUSE_RICHEDIT
377 if ( m_isRich )
378 {
379 wxString str;
380
381 int len = GetWindowTextLength(GetHwnd()) + 1;
382 wxChar *p = str.GetWriteBuf(len);
383
384 TEXTRANGE textRange;
385 textRange.chrg.cpMin = 0;
386 textRange.chrg.cpMax = -1;
387 textRange.lpstrText = p;
388
389 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
390
391 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for the
392 // newlines which is neither Unix nor Windows style (Win95 with
393 // riched20.dll shows this behaviour) - convert it to something
394 // reasonable
395 for ( ; *p; p++ )
396 {
397 if ( *p == _T('\r') )
398 *p = _T('\n');
399 }
400
401 str.UngetWriteBuf();
402
403 return str;
404 }
405 #endif // wxUSE_RICHEDIT
406
407 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
408 // same one as above for consitency
409 wxString str = wxGetWindowText(GetHWND());
410
411 return wxTextFile::Translate(str, wxTextFileType_Unix);
412 }
413
414 void wxTextCtrl::SetValue(const wxString& value)
415 {
416 // if the text is long enough, it's faster to just set it instead of first
417 // comparing it with the old one (chances are that it will be different
418 // anyhow, this comparison is there to avoid flicker for small single-line
419 // edit controls mostly)
420 if ( (value.length() > 0x400) || (value != GetValue()) )
421 {
422 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
423
424 SetWindowText(GetHwnd(), valueDos);
425
426 AdjustSpaceLimit();
427 }
428 }
429
430 void wxTextCtrl::WriteText(const wxString& value)
431 {
432 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
433
434 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
435
436 AdjustSpaceLimit();
437 }
438
439 void wxTextCtrl::AppendText(const wxString& text)
440 {
441 SetInsertionPointEnd();
442 WriteText(text);
443 }
444
445 void wxTextCtrl::Clear()
446 {
447 SetWindowText(GetHwnd(), wxT(""));
448 }
449
450 // ----------------------------------------------------------------------------
451 // Clipboard operations
452 // ----------------------------------------------------------------------------
453
454 void wxTextCtrl::Copy()
455 {
456 if (CanCopy())
457 {
458 HWND hWnd = GetHwnd();
459 SendMessage(hWnd, WM_COPY, 0, 0L);
460 }
461 }
462
463 void wxTextCtrl::Cut()
464 {
465 if (CanCut())
466 {
467 HWND hWnd = GetHwnd();
468 SendMessage(hWnd, WM_CUT, 0, 0L);
469 }
470 }
471
472 void wxTextCtrl::Paste()
473 {
474 if (CanPaste())
475 {
476 HWND hWnd = GetHwnd();
477 SendMessage(hWnd, WM_PASTE, 0, 0L);
478 }
479 }
480
481 bool wxTextCtrl::CanCopy() const
482 {
483 // Can copy if there's a selection
484 long from, to;
485 GetSelection(& from, & to);
486 return (from != to);
487 }
488
489 bool wxTextCtrl::CanCut() const
490 {
491 // Can cut if there's a selection
492 long from, to;
493 GetSelection(& from, & to);
494 return (from != to);
495 }
496
497 bool wxTextCtrl::CanPaste() const
498 {
499 #if wxUSE_RICHEDIT
500 if (m_isRich)
501 {
502 int dataFormat = 0; // 0 == any format
503 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
504 }
505 #endif
506 if (!IsEditable())
507 return FALSE;
508
509 // Standard edit control: check for straight text on clipboard
510 bool isTextAvailable = FALSE;
511 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
512 {
513 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
514 ::CloseClipboard();
515 }
516
517 return isTextAvailable;
518 }
519
520 // ----------------------------------------------------------------------------
521 // Accessors
522 // ----------------------------------------------------------------------------
523
524 void wxTextCtrl::SetEditable(bool editable)
525 {
526 bool isEditable = IsEditable();
527
528 HWND hWnd = GetHwnd();
529 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
530
531 if (editable != isEditable)
532 {
533 SetupColours();
534 Refresh();
535 }
536 }
537
538 void wxTextCtrl::SetInsertionPoint(long pos)
539 {
540 HWND hWnd = GetHwnd();
541 #ifdef __WIN32__
542 #if wxUSE_RICHEDIT
543 if ( m_isRich)
544 {
545 CHARRANGE range;
546 range.cpMin = pos;
547 range.cpMax = pos;
548 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
549 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
550 }
551 else
552 #endif // wxUSE_RICHEDIT
553 {
554 SendMessage(hWnd, EM_SETSEL, pos, pos);
555 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
556 }
557 #else // Win16
558 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
559 #endif // Win32/16
560
561 static const char *nothing = "";
562 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
563 }
564
565 void wxTextCtrl::SetInsertionPointEnd()
566 {
567 long pos = GetLastPosition();
568 SetInsertionPoint(pos);
569 }
570
571 long wxTextCtrl::GetInsertionPoint() const
572 {
573 #if wxUSE_RICHEDIT
574 if (m_isRich)
575 {
576 CHARRANGE range;
577 range.cpMin = 0;
578 range.cpMax = 0;
579 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
580 return range.cpMin;
581 }
582 #endif
583
584 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
585 return Pos & 0xFFFF;
586 }
587
588 long wxTextCtrl::GetLastPosition() const
589 {
590 HWND hWnd = GetHwnd();
591
592 // Will always return a number > 0 (according to docs)
593 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
594
595 // This gets the char index for the _beginning_ of the last line
596 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
597
598 // Get number of characters in the last line. We'll add this to the character
599 // index for the last line, 1st position.
600 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
601
602 return (long)(charIndex + lineLength);
603 }
604
605 // If the return values from and to are the same, there is no
606 // selection.
607 void wxTextCtrl::GetSelection(long* from, long* to) const
608 {
609 #if wxUSE_RICHEDIT
610 if (m_isRich)
611 {
612 CHARRANGE charRange;
613 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
614
615 *from = charRange.cpMin;
616 *to = charRange.cpMax;
617
618 return;
619 }
620 #endif
621 DWORD dwStart, dwEnd;
622 WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position
623 LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position
624
625 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
626
627 *from = dwStart;
628 *to = dwEnd;
629 }
630
631 bool wxTextCtrl::IsEditable() const
632 {
633 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
634
635 return ((style & ES_READONLY) == 0);
636 }
637
638 // ----------------------------------------------------------------------------
639 // Editing
640 // ----------------------------------------------------------------------------
641
642 void wxTextCtrl::Replace(long from, long to, const wxString& value)
643 {
644 #if wxUSE_CLIPBOARD
645 HWND hWnd = GetHwnd();
646 long fromChar = from;
647 long toChar = to;
648
649 // Set selection and remove it
650 #ifdef __WIN32__
651 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
652 #else
653 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
654 #endif
655 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
656
657 // Now replace with 'value', by pasting.
658 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
659
660 // Paste into edit control
661 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
662 #else
663 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
664 #endif
665 }
666
667 void wxTextCtrl::Remove(long from, long to)
668 {
669 HWND hWnd = GetHwnd();
670 long fromChar = from;
671 long toChar = to;
672
673 // Cut all selected text
674 #ifdef __WIN32__
675 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
676 #else
677 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
678 #endif
679 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
680 }
681
682 void wxTextCtrl::SetSelection(long from, long to)
683 {
684 HWND hWnd = GetHwnd();
685 long fromChar = from;
686 long toChar = to;
687
688 // if from and to are both -1, it means (in wxWindows) that all text should
689 // be selected. Translate into Windows convention
690 if ((from == -1) && (to == -1))
691 {
692 fromChar = 0;
693 toChar = -1;
694 }
695
696 #ifdef __WIN32__
697 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
698 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
699 #else
700 // WPARAM is 0: selection is scrolled into view
701 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
702 #endif
703 }
704
705 bool wxTextCtrl::LoadFile(const wxString& file)
706 {
707 if ( wxTextCtrlBase::LoadFile(file) )
708 {
709 // update the size limit if needed
710 AdjustSpaceLimit();
711
712 return TRUE;
713 }
714
715 return FALSE;
716 }
717
718 bool wxTextCtrl::IsModified() const
719 {
720 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
721 }
722
723 // Makes 'unmodified'
724 void wxTextCtrl::DiscardEdits()
725 {
726 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
727 }
728
729 int wxTextCtrl::GetNumberOfLines() const
730 {
731 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
732 }
733
734 long wxTextCtrl::XYToPosition(long x, long y) const
735 {
736 HWND hWnd = GetHwnd();
737
738 // This gets the char index for the _beginning_ of this line
739 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
740 return (long)(x + charIndex);
741 }
742
743 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
744 {
745 HWND hWnd = GetHwnd();
746
747 // This gets the line number containing the character
748 int lineNo;
749 #if wxUSE_RICHEDIT
750 if ( m_isRich )
751 {
752 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
753 }
754 else
755 #endif // wxUSE_RICHEDIT
756 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
757
758 if ( lineNo == -1 )
759 {
760 // no such line
761 return FALSE;
762 }
763
764 // This gets the char index for the _beginning_ of this line
765 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
766 if ( charIndex == -1 )
767 {
768 return FALSE;
769 }
770
771 // The X position must therefore be the different between pos and charIndex
772 if ( x )
773 *x = (long)(pos - charIndex);
774 if ( y )
775 *y = (long)lineNo;
776
777 return TRUE;
778 }
779
780 void wxTextCtrl::ShowPosition(long pos)
781 {
782 HWND hWnd = GetHwnd();
783
784 // To scroll to a position, we pass the number of lines and characters
785 // to scroll *by*. This means that we need to:
786 // (1) Find the line position of the current line.
787 // (2) Find the line position of pos.
788 // (3) Scroll by (pos - current).
789 // For now, ignore the horizontal scrolling.
790
791 // Is this where scrolling is relative to - the line containing the caret?
792 // Or is the first visible line??? Try first visible line.
793 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
794
795 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
796
797 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
798
799 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
800
801 if (linesToScroll != 0)
802 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
803 }
804
805 int wxTextCtrl::GetLineLength(long lineNo) const
806 {
807 long charIndex = XYToPosition(0, lineNo);
808 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
809 return len;
810 }
811
812 wxString wxTextCtrl::GetLineText(long lineNo) const
813 {
814 size_t len = (size_t)GetLineLength(lineNo) + 1;
815 char *buf = (char *)malloc(len);
816 *(WORD *)buf = len;
817 int noChars = (int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
818 buf[noChars] = 0;
819
820 wxString str(buf);
821
822 free(buf);
823
824 return str;
825 }
826
827 // ----------------------------------------------------------------------------
828 // Undo/redo
829 // ----------------------------------------------------------------------------
830
831 void wxTextCtrl::Undo()
832 {
833 if (CanUndo())
834 {
835 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
836 }
837 }
838
839 void wxTextCtrl::Redo()
840 {
841 if (CanRedo())
842 {
843 // Same as Undo, since Undo undoes the undo, i.e. a redo.
844 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
845 }
846 }
847
848 bool wxTextCtrl::CanUndo() const
849 {
850 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
851 }
852
853 bool wxTextCtrl::CanRedo() const
854 {
855 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
856 }
857
858 // ----------------------------------------------------------------------------
859 // implemenation details
860 // ----------------------------------------------------------------------------
861
862 void wxTextCtrl::Command(wxCommandEvent & event)
863 {
864 SetValue(event.GetString());
865 ProcessCommand (event);
866 }
867
868 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
869 {
870 // By default, load the first file into the text window.
871 if (event.GetNumberOfFiles() > 0)
872 {
873 LoadFile(event.GetFiles()[0]);
874 }
875 }
876
877 void wxTextCtrl::OnChar(wxKeyEvent& event)
878 {
879 switch ( event.KeyCode() )
880 {
881 case WXK_RETURN:
882 if ( !(m_windowStyle & wxTE_MULTILINE) )
883 {
884 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
885 event.SetEventObject( this );
886 if ( GetEventHandler()->ProcessEvent(event) )
887 return;
888 }
889 //else: multiline controls need Enter for themselves
890
891 break;
892
893 case WXK_TAB:
894 // always produce navigation event - even if we process TAB
895 // ourselves the fact that we got here means that the user code
896 // decided to skip processing of this TAB - probably to let it
897 // do its default job.
898 //
899 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
900 // handled by Windows
901 {
902 wxNavigationKeyEvent eventNav;
903 eventNav.SetDirection(!event.ShiftDown());
904 eventNav.SetWindowChange(FALSE);
905 eventNav.SetEventObject(this);
906
907 if ( GetEventHandler()->ProcessEvent(eventNav) )
908 return;
909 }
910 break;
911
912 default:
913 event.Skip();
914 return;
915 }
916
917 // don't just call event.Skip() because this will cause TABs and ENTERs
918 // be passed upwards and we don't always want this - instead process it
919 // right here
920
921 // FIXME
922 event.Skip();
923 }
924
925 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
926 {
927 switch (param)
928 {
929 case EN_SETFOCUS:
930 case EN_KILLFOCUS:
931 {
932 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
933 : wxEVT_SET_FOCUS,
934 m_windowId);
935 event.SetEventObject( this );
936 GetEventHandler()->ProcessEvent(event);
937 }
938 break;
939
940 case EN_CHANGE:
941 {
942 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
943 wxString val(GetValue());
944 if ( !val.IsNull() )
945 event.m_commandString = WXSTRINGCAST val;
946 event.SetEventObject( this );
947 ProcessCommand(event);
948 }
949 break;
950
951 case EN_MAXTEXT:
952 // the text size limit has been hit - increase it
953 AdjustSpaceLimit();
954 break;
955
956 // the other notification messages are not processed
957 case EN_UPDATE:
958 case EN_ERRSPACE:
959 case EN_HSCROLL:
960 case EN_VSCROLL:
961 default:
962 return FALSE;
963 }
964
965 // processed
966 return TRUE;
967 }
968
969 void wxTextCtrl::AdjustSpaceLimit()
970 {
971 #ifndef __WIN16__
972 unsigned int len = ::GetWindowTextLength(GetHwnd()),
973 limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
974 if ( len >= limit )
975 {
976 limit = len + 0x8000; // 32Kb
977
978 #if wxUSE_RICHEDIT
979 if ( m_isRich )
980 {
981 // as a nice side effect, this also allows passing limit > 64Kb
982 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
983 }
984 else
985 #endif // wxUSE_RICHEDIT
986 {
987 if ( limit > 0xffff )
988 {
989 // this will set it to a platform-dependent maximum (much more
990 // than 64Kb under NT)
991 limit = 0;
992 }
993
994 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
995 }
996 }
997 #endif // !Win16
998 }
999
1000 bool wxTextCtrl::AcceptsFocus() const
1001 {
1002 // we don't want focus if we can't be edited
1003 return IsEditable() && wxControl::AcceptsFocus();
1004 }
1005
1006 wxSize wxTextCtrl::DoGetBestSize() const
1007 {
1008 int cx, cy;
1009 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1010
1011 int wText = DEFAULT_ITEM_WIDTH;
1012
1013 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1014 if ( m_windowStyle & wxTE_MULTILINE )
1015 {
1016 hText *= wxMin(GetNumberOfLines(), 5);
1017 }
1018 //else: for single line control everything is ok
1019
1020 return wxSize(wText, hText);
1021 }
1022
1023 // ----------------------------------------------------------------------------
1024 // standard handlers for standard edit menu events
1025 // ----------------------------------------------------------------------------
1026
1027 void wxTextCtrl::OnCut(wxCommandEvent& event)
1028 {
1029 Cut();
1030 }
1031
1032 void wxTextCtrl::OnCopy(wxCommandEvent& event)
1033 {
1034 Copy();
1035 }
1036
1037 void wxTextCtrl::OnPaste(wxCommandEvent& event)
1038 {
1039 Paste();
1040 }
1041
1042 void wxTextCtrl::OnUndo(wxCommandEvent& event)
1043 {
1044 Undo();
1045 }
1046
1047 void wxTextCtrl::OnRedo(wxCommandEvent& event)
1048 {
1049 Redo();
1050 }
1051
1052 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1053 {
1054 event.Enable( CanCut() );
1055 }
1056
1057 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1058 {
1059 event.Enable( CanCopy() );
1060 }
1061
1062 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1063 {
1064 event.Enable( CanPaste() );
1065 }
1066
1067 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1068 {
1069 event.Enable( CanUndo() );
1070 }
1071
1072 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1073 {
1074 event.Enable( CanRedo() );
1075 }
1076
1077 // ----------------------------------------------------------------------------
1078 // wxRichEditModule
1079 // ----------------------------------------------------------------------------
1080
1081 #if wxUSE_RICHEDIT
1082
1083 bool wxRichEditModule::OnInit()
1084 {
1085 // don't do anything - we will load it when needed
1086 return TRUE;
1087 }
1088
1089 void wxRichEditModule::OnExit()
1090 {
1091 if ( ms_hRichEdit )
1092 {
1093 FreeLibrary(ms_hRichEdit);
1094 }
1095 }
1096
1097 /* static */
1098 bool wxRichEditModule::Load(int version)
1099 {
1100 wxCHECK_MSG( version >= 1 && version <= 3, FALSE,
1101 _T("incorrect richedit control version requested") );
1102
1103 if ( version <= ms_verRichEdit )
1104 {
1105 // we've already got this or better
1106 return TRUE;
1107 }
1108
1109 if ( ms_hRichEdit )
1110 {
1111 ::FreeLibrary(ms_hRichEdit);
1112 }
1113
1114 // always try load riched20.dll first - like this we won't have to reload
1115 // it later if we're first asked for RE 1 and then for RE 2 or 3
1116 wxString dllname = _T("riched20.dll");
1117 ms_hRichEdit = ::LoadLibrary(dllname);
1118 ms_verRichEdit = 2; // no way to tell if it's 2 or 3, assume 2
1119
1120 if ( !ms_hRichEdit && (version == 1) )
1121 {
1122 // fall back to RE 1
1123 dllname = _T("riched32.dll");
1124 ms_hRichEdit = ::LoadLibrary(dllname);
1125 ms_verRichEdit = 1;
1126 }
1127
1128 if ( !ms_hRichEdit )
1129 {
1130 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
1131
1132 ms_verRichEdit = -1;
1133
1134 return FALSE;
1135 }
1136
1137 return TRUE;
1138 }
1139
1140 #endif // wxUSE_RICHEDIT
1141