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