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