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