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