]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textctrl.cpp
Add a missing structure definition, move all rich text missing
[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
9 // Licence: wxWindows licence
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 #include "wx/menu.h"
42 #endif
43
44 #include "wx/module.h"
45
46 #if wxUSE_CLIPBOARD
47 #include "wx/clipbrd.h"
48 #endif
49
50 #include "wx/textfile.h"
51
52 #include <windowsx.h>
53
54 #include "wx/msw/private.h"
55
56 #include <string.h>
57 #include <stdlib.h>
58 #include <sys/types.h>
59
60 #if wxUSE_RICHEDIT
61
62 // old mingw32 has richedit stuff directly in windows.h and doesn't have
63 // richedit.h at all
64 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
65 #include <richedit.h>
66 #endif
67
68 #include "wx/msw/missing.h"
69
70 #endif // wxUSE_RICHEDIT
71
72 // ----------------------------------------------------------------------------
73 // private functions
74 // ----------------------------------------------------------------------------
75
76 #if wxUSE_RICHEDIT
77
78 DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb);
79
80 #endif // wxUSE_RICHEDIT
81
82 // ----------------------------------------------------------------------------
83 // private classes
84 // ----------------------------------------------------------------------------
85
86 #if wxUSE_RICHEDIT
87
88 // this module initializes RichEdit DLL(s) if needed
89 class wxRichEditModule : public wxModule
90 {
91 public:
92 virtual bool OnInit();
93 virtual void OnExit();
94
95 // load the richedit DLL of at least of required version
96 static bool Load(int version = 1);
97
98 private:
99 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
100 static HINSTANCE ms_hRichEdit[2];
101
102 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
103 };
104
105 HINSTANCE wxRichEditModule::ms_hRichEdit[2] = { NULL, NULL };
106
107 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
108
109 #endif // wxUSE_RICHEDIT
110
111 // ----------------------------------------------------------------------------
112 // event tables and other macros
113 // ----------------------------------------------------------------------------
114
115 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
116
117 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
118 EVT_CHAR(wxTextCtrl::OnChar)
119 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
120
121 #if wxUSE_RICHEDIT
122 EVT_RIGHT_UP(wxTextCtrl::OnRightClick)
123 #endif
124
125 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
126 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
127 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
128 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
129 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
130 EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete)
131 EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll)
132
133 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
134 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
135 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
136 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
137 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
138 EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete)
139 EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll)
140 #ifdef __WIN16__
141 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
142 #endif
143 END_EVENT_TABLE()
144
145 // ============================================================================
146 // implementation
147 // ============================================================================
148
149 // ----------------------------------------------------------------------------
150 // creation
151 // ----------------------------------------------------------------------------
152
153 void wxTextCtrl::Init()
154 {
155 #if wxUSE_RICHEDIT
156 m_verRichEdit = 0;
157 #endif // wxUSE_RICHEDIT
158
159 m_privateContextMenu = NULL;
160 m_suppressNextUpdate = FALSE;
161 }
162
163 wxTextCtrl::~wxTextCtrl()
164 {
165 if (m_privateContextMenu)
166 {
167 delete m_privateContextMenu;
168 m_privateContextMenu = NULL;
169 }
170 }
171
172 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
173 const wxString& value,
174 const wxPoint& pos,
175 const wxSize& size,
176 long style,
177 const wxValidator& validator,
178 const wxString& name)
179 {
180 // base initialization
181 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
182 return FALSE;
183
184 if ( parent )
185 parent->AddChild(this);
186
187 // translate wxWin style flags to MSW ones
188 WXDWORD msStyle = MSWGetCreateWindowFlags();
189
190 // do create the control - either an EDIT or RICHEDIT
191 wxString windowClass = wxT("EDIT");
192
193 #if wxUSE_RICHEDIT
194 if ( m_windowStyle & wxTE_AUTO_URL )
195 {
196 // automatic URL detection only works in RichEdit 2.0+
197 m_windowStyle |= wxTE_RICH2;
198 }
199
200 if ( m_windowStyle & wxTE_RICH2 )
201 {
202 // using richedit 2.0 implies using wxTE_RICH
203 m_windowStyle |= wxTE_RICH;
204 }
205
206 // we need to load the richedit DLL before creating the rich edit control
207 if ( m_windowStyle & wxTE_RICH )
208 {
209 static bool s_errorGiven = FALSE;// MT-FIXME
210
211 // Which version do we need? Use 1.0 by default because it is much more
212 // like the the standard EDIT or 2.0 if explicitly requested, but use
213 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
214 //
215 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
216 // (and thus EDIT) much better than RichEdit 2.0 so we probably
217 // should use 3.0 if available as it is the best of both worlds -
218 // but as I can't test it right now I don't do it (VZ)
219 #if wxUSE_UNICODE
220 const int verRichEdit = 2;
221 #else // !wxUSE_UNICODE
222 int verRichEdit = m_windowStyle & wxTE_RICH2 ? 2 : 1;
223 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
224
225 // only give the error msg once if the DLL can't be loaded
226 if ( !s_errorGiven )
227 {
228 // try to load the RichEdit DLL (will do nothing if already done)
229 if ( !wxRichEditModule::Load(verRichEdit) )
230 {
231 #if !wxUSE_UNICODE
232 // try another version?
233 verRichEdit = 3 - verRichEdit; // 1 <-> 2
234
235 if ( !wxRichEditModule::Load(verRichEdit) )
236 #endif // wxUSE_UNICODE
237 {
238 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
239
240 s_errorGiven = TRUE;
241 }
242 }
243 }
244
245 // have we managed to load any richedit version?
246 if ( !s_errorGiven )
247 {
248 m_verRichEdit = verRichEdit;
249 if ( m_verRichEdit == 1 )
250 {
251 windowClass = wxT("RICHEDIT");
252 }
253 else
254 {
255 #ifndef RICHEDIT_CLASS
256 wxString RICHEDIT_CLASS;
257 RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), m_verRichEdit);
258 #if wxUSE_UNICODE
259 RICHEDIT_CLASS += _T('W');
260 #else // ANSI
261 RICHEDIT_CLASS += _T('A');
262 #endif // Unicode/ANSI
263 #endif // !RICHEDIT_CLASS
264
265 windowClass = RICHEDIT_CLASS;
266 }
267 }
268 }
269 #endif // wxUSE_RICHEDIT
270
271 // we need to turn '\n's into "\r\n"s for the multiline controls
272 wxString valueWin;
273 if ( m_windowStyle & wxTE_MULTILINE )
274 {
275 valueWin = wxTextFile::Translate(value, wxTextFileType_Dos);
276 }
277 else // single line
278 {
279 valueWin = value;
280 }
281
282 if ( !MSWCreateControl(windowClass, msStyle, pos, size, valueWin) )
283 return FALSE;
284
285 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
286
287 #if wxUSE_RICHEDIT
288 if ( IsRich() )
289 {
290 // enable the events we're interested in: we want to get EN_CHANGE as
291 // for the normal controls
292 LPARAM mask = ENM_CHANGE;
293
294 if ( GetRichVersion() == 1 )
295 {
296 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
297 // explained in its handler
298 mask |= ENM_MOUSEEVENTS;
299 }
300 else if ( m_windowStyle & wxTE_AUTO_URL )
301 {
302 mask |= ENM_LINK;
303
304 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0);
305 }
306
307 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask);
308 }
309 #endif // wxUSE_RICHEDIT
310
311 return TRUE;
312 }
313
314 // Make sure the window style (etc.) reflects the HWND style (roughly)
315 void wxTextCtrl::AdoptAttributesFromHWND()
316 {
317 wxWindow::AdoptAttributesFromHWND();
318
319 HWND hWnd = GetHwnd();
320 long style = ::GetWindowLong(hWnd, GWL_STYLE);
321
322 // retrieve the style to see whether this is an edit or richedit ctrl
323 #if wxUSE_RICHEDIT
324 wxString classname = wxGetWindowClass(GetHWND());
325
326 if ( classname.IsSameAs(_T("EDIT"), FALSE /* no case */) )
327 {
328 m_verRichEdit = 0;
329 }
330 else // rich edit?
331 {
332 wxChar c;
333 if ( wxSscanf(classname, _T("RichEdit%d0%c"), &m_verRichEdit, &c) != 2 )
334 {
335 wxLogDebug(_T("Unknown edit control '%s'."), classname.c_str());
336
337 m_verRichEdit = 0;
338 }
339 }
340 #endif // wxUSE_RICHEDIT
341
342 if (style & ES_MULTILINE)
343 m_windowStyle |= wxTE_MULTILINE;
344 if (style & ES_PASSWORD)
345 m_windowStyle |= wxTE_PASSWORD;
346 if (style & ES_READONLY)
347 m_windowStyle |= wxTE_READONLY;
348 if (style & ES_WANTRETURN)
349 m_windowStyle |= wxTE_PROCESS_ENTER;
350 if (style & ES_CENTER)
351 m_windowStyle |= wxTE_CENTRE;
352 if (style & ES_RIGHT)
353 m_windowStyle |= wxTE_RIGHT;
354 }
355
356 WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
357 {
358 // default border for the text controls is the sunken one
359 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
360 {
361 style |= wxBORDER_SUNKEN;
362 }
363
364 long msStyle = wxControl::MSWGetStyle(style, exstyle);
365
366 // styles which we alaways add by default
367 if ( style & wxTE_MULTILINE )
368 {
369 wxASSERT_MSG( !(style & wxTE_PROCESS_ENTER),
370 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
371
372 msStyle |= ES_MULTILINE | ES_WANTRETURN;
373 if ( !(style & wxTE_NO_VSCROLL) )
374 {
375 // always adjust the vertical scrollbar automatically if we have it
376 msStyle |= WS_VSCROLL | ES_AUTOVSCROLL;
377
378 #if wxUSE_RICHEDIT
379 // we have to use this style for the rich edit controls because
380 // without it the vertical scrollbar never appears at all in
381 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
382 if ( style & wxTE_RICH2 )
383 {
384 msStyle |= ES_DISABLENOSCROLL;
385 }
386 #endif // wxUSE_RICHEDIT
387 }
388
389 style |= wxTE_PROCESS_ENTER;
390 }
391 else // !multiline
392 {
393 // there is really no reason to not have this style for single line
394 // text controls
395 msStyle |= ES_AUTOHSCROLL;
396 }
397
398 // styles which we add depending on the specified wxWindows styles
399 if ( style & wxHSCROLL )
400 {
401 // automatically scroll the control horizontally as necessary
402 msStyle |= WS_HSCROLL;// | ES_AUTOHSCROLL;
403 }
404
405 if ( style & wxTE_READONLY )
406 msStyle |= ES_READONLY;
407
408 if ( style & wxTE_PASSWORD )
409 msStyle |= ES_PASSWORD;
410
411 if ( style & wxTE_NOHIDESEL )
412 msStyle |= ES_NOHIDESEL;
413
414 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
415 if ( style & wxTE_CENTRE )
416 msStyle |= ES_CENTER;
417 else if ( style & wxTE_RIGHT )
418 msStyle |= ES_RIGHT;
419 else
420 msStyle |= ES_LEFT; // ES_LEFT if 0 as well but for consistency...
421
422 return msStyle;
423 }
424
425 void wxTextCtrl::SetWindowStyleFlag(long style)
426 {
427 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
428 style |= wxBORDER_SUNKEN;
429
430 #if wxUSE_RICHEDIT
431 // we have to deal with some styles separately because they can't be
432 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
433 // using richedit-specific EM_SETOPTIONS
434 if ( IsRich() &&
435 ((style & wxTE_NOHIDESEL) != (GetWindowStyle() & wxTE_NOHIDESEL)) )
436 {
437 bool set = (style & wxTE_NOHIDESEL) != 0;
438
439 ::SendMessage(GetHwnd(), EM_SETOPTIONS, set ? ECOOP_OR : ECOOP_AND,
440 set ? ECO_NOHIDESEL : ~ECO_NOHIDESEL);
441 }
442 #endif // wxUSE_RICHEDIT
443
444 wxControl::SetWindowStyleFlag(style);
445 }
446
447 // ----------------------------------------------------------------------------
448 // set/get the controls text
449 // ----------------------------------------------------------------------------
450
451 wxString wxTextCtrl::GetValue() const
452 {
453 // range 0..-1 is special for GetRange() and means to retrieve all text
454 return GetRange(0, -1);
455 }
456
457 wxString wxTextCtrl::GetRange(long from, long to) const
458 {
459 wxString str;
460
461 if ( from >= to && to != -1 )
462 {
463 // nothing to retrieve
464 return str;
465 }
466
467 #if wxUSE_RICHEDIT
468 if ( IsRich() )
469 {
470 int len = GetWindowTextLength(GetHwnd());
471 if ( len > from )
472 {
473 // alloc one extra WORD as needed by the control
474 wxChar *p = str.GetWriteBuf(++len);
475
476 TEXTRANGE textRange;
477 textRange.chrg.cpMin = from;
478 textRange.chrg.cpMax = to == -1 ? len : to;
479 textRange.lpstrText = p;
480
481 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
482
483 if ( m_verRichEdit > 1 )
484 {
485 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
486 // neither Unix nor Windows style - convert it to something
487 // reasonable
488 for ( ; *p; p++ )
489 {
490 if ( *p == _T('\r') )
491 *p = _T('\n');
492 }
493 }
494
495 str.UngetWriteBuf();
496
497 if ( m_verRichEdit == 1 )
498 {
499 // convert to the canonical form - see comment below
500 str = wxTextFile::Translate(str, wxTextFileType_Unix);
501 }
502 }
503 //else: no text at all, leave the string empty
504 }
505 else
506 #endif // wxUSE_RICHEDIT
507 {
508 // retrieve all text
509 str = wxGetWindowText(GetHWND());
510
511 // need only a range?
512 if ( from < to )
513 {
514 str = str.Mid(from, to - from);
515 }
516
517 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
518 // canonical one (same one as above) for consistency with the other kinds
519 // of controls and, more importantly, with the other ports
520 str = wxTextFile::Translate(str, wxTextFileType_Unix);
521 }
522
523 return str;
524 }
525
526 void wxTextCtrl::SetValue(const wxString& value)
527 {
528 // if the text is long enough, it's faster to just set it instead of first
529 // comparing it with the old one (chances are that it will be different
530 // anyhow, this comparison is there to avoid flicker for small single-line
531 // edit controls mostly)
532 if ( (value.length() > 0x400) || (value != GetValue()) )
533 {
534 DoWriteText(value, FALSE /* not selection only */);
535 }
536
537 // we should reset the modified flag even if the value didn't really change
538
539 // mark the control as being not dirty - we changed its text, not the
540 // user
541 DiscardEdits();
542
543 // for compatibility, don't move the cursor when doing SetValue()
544 SetInsertionPoint(0);
545 }
546
547 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
548
549 DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb)
550 {
551 *pcb = 0;
552
553 wchar_t *wbuf = (wchar_t *)buf;
554 const wchar_t *wpc = *(const wchar_t **)dwCookie;
555 while ( cb && *wpc )
556 {
557 *wbuf++ = *wpc++;
558
559 cb -= sizeof(wchar_t);
560 (*pcb) += sizeof(wchar_t);
561 }
562
563 *(const wchar_t **)dwCookie = wpc;
564
565 return 0;
566 }
567
568 extern long wxEncodingToCodepage(wxFontEncoding encoding); // from utils.cpp
569
570 #if wxUSE_UNICODE_MSLU
571 bool wxTextCtrl::StreamIn(const wxString& value,
572 wxFontEncoding WXUNUSED(encoding),
573 bool selectionOnly)
574 {
575 const wchar_t *wpc = value.c_str();
576 #else // !wxUSE_UNICODE_MSLU
577 bool wxTextCtrl::StreamIn(const wxString& value,
578 wxFontEncoding encoding,
579 bool selectionOnly)
580 {
581 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
582 // text in the non default charset -- otherwise it thinks it knows better
583 // than we do and always shows it in the default one
584
585 // first get the Windows code page for this encoding
586 long codepage = wxEncodingToCodepage(encoding);
587 if ( codepage == -1 )
588 {
589 // unknown encoding
590 return FALSE;
591 }
592
593 // next translate to Unicode using this code page
594 int len = ::MultiByteToWideChar(codepage, 0, value, -1, NULL, 0);
595
596 #if wxUSE_WCHAR_T
597 wxWCharBuffer wchBuf(len);
598 #else
599 wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t));
600 #endif
601
602 if ( !::MultiByteToWideChar(codepage, 0, value, -1,
603 (wchar_t *)(const wchar_t *)wchBuf, len) )
604 {
605 wxLogLastError(_T("MultiByteToWideChar"));
606 }
607
608 // finally, stream it in the control
609 const wchar_t *wpc = wchBuf;
610 #endif // wxUSE_UNICODE_MSLU
611
612 EDITSTREAM eds;
613 wxZeroMemory(eds);
614 eds.dwCookie = (DWORD)&wpc;
615 // the cast below is needed for broken (very) old mingw32 headers
616 eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn;
617
618 // we're going to receive 2 EN_CHANGE notifications if we got any selection
619 // (same problem as in DoWriteText())
620 if ( selectionOnly && HasSelection() )
621 {
622 // so suppress one of them
623 m_suppressNextUpdate = TRUE;
624 }
625
626 ::SendMessage(GetHwnd(), EM_STREAMIN,
627 SF_TEXT |
628 SF_UNICODE |
629 (selectionOnly ? SFF_SELECTION : 0),
630 (LPARAM)&eds);
631
632 if ( eds.dwError )
633 {
634 wxLogLastError(_T("EM_STREAMIN"));
635 }
636
637 #if !wxUSE_WCHAR_T
638 free(wchBuf);
639 #endif // !wxUSE_WCHAR_T
640
641 return TRUE;
642 }
643
644 #endif // wxUSE_RICHEDIT
645
646 void wxTextCtrl::WriteText(const wxString& value)
647 {
648 DoWriteText(value);
649 }
650
651 void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly)
652 {
653 wxString valueDos;
654 if ( m_windowStyle & wxTE_MULTILINE )
655 valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
656 else
657 valueDos = value;
658
659 #if wxUSE_RICHEDIT
660 // there are several complications with the rich edit controls here
661 bool done = FALSE;
662 if ( IsRich() )
663 {
664 // first, ensure that the new text will be in the default style
665 if ( !m_defaultStyle.IsDefault() )
666 {
667 long start, end;
668 GetSelection(&start, &end);
669 SetStyle(start, end, m_defaultStyle);
670 }
671
672 #if wxUSE_UNICODE_MSLU
673 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
674 // but EM_STREAMIN works
675 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
676 {
677 done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly);
678 }
679 #endif // wxUSE_UNICODE_MSLU
680
681 #if !wxUSE_UNICODE
682 // next check if the text we're inserting must be shown in a non
683 // default charset -- this only works for RichEdit > 1.0
684 if ( GetRichVersion() > 1 )
685 {
686 wxFont font = m_defaultStyle.GetFont();
687 if ( !font.Ok() )
688 font = GetFont();
689
690 if ( font.Ok() )
691 {
692 wxFontEncoding encoding = font.GetEncoding();
693 if ( encoding != wxFONTENCODING_SYSTEM )
694 {
695 done = StreamIn(valueDos, encoding, selectionOnly);
696 }
697 }
698 }
699 #endif // !wxUSE_UNICODE
700 }
701
702 if ( !done )
703 #endif // wxUSE_RICHEDIT
704 {
705 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
706 // call below which is confusing for the client code and so should be
707 // avoided
708 //
709 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
710 // and there is a non empty selection currently and (b) rich text
711 // controls in any case
712 if (
713 #if wxUSE_RICHEDIT
714 IsRich() ||
715 #endif // wxUSE_RICHEDIT
716 (selectionOnly && HasSelection()) )
717 {
718 m_suppressNextUpdate = TRUE;
719 }
720
721 ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT,
722 0, (LPARAM)valueDos.c_str());
723
724 // OTOH, non rich text controls don't generate any events at all when
725 // we use WM_SETTEXT -- have to emulate them here
726 if ( !selectionOnly
727 #if wxUSE_RICHEDIT
728 && !IsRich()
729 #endif // wxUSE_RICHEDIT
730 )
731 {
732 // Windows already sends an update event for single-line
733 // controls.
734 if ( m_windowStyle & wxTE_MULTILINE )
735 SendUpdateEvent();
736 }
737 }
738
739 AdjustSpaceLimit();
740 }
741
742 void wxTextCtrl::AppendText(const wxString& text)
743 {
744 SetInsertionPointEnd();
745
746 WriteText(text);
747
748 #if wxUSE_RICHEDIT
749 if ( IsMultiLine() && GetRichVersion() > 1 )
750 {
751 // setting the caret to the end and showing it simply doesn't work for
752 // RichEdit 2.0 -- force it to still do what we want
753 ::SendMessage(GetHwnd(), EM_LINESCROLL, 0, GetNumberOfLines());
754 }
755 #endif // wxUSE_RICHEDIT
756 }
757
758 void wxTextCtrl::Clear()
759 {
760 ::SetWindowText(GetHwnd(), wxT(""));
761
762 #if wxUSE_RICHEDIT
763 if ( !IsRich() )
764 #endif // wxUSE_RICHEDIT
765 {
766 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
767 // but the normal ones don't -- make Clear() behaviour consistent by
768 // always sending this event
769
770 // Windows already sends an update event for single-line
771 // controls.
772 if ( m_windowStyle & wxTE_MULTILINE )
773 SendUpdateEvent();
774 }
775 }
776
777 #ifdef __WIN32__
778
779 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event)
780 {
781 SetFocus();
782
783 size_t lenOld = GetValue().length();
784
785 wxUint32 code = event.GetRawKeyCode();
786 ::keybd_event(code, 0, 0 /* key press */, 0);
787 ::keybd_event(code, 0, KEYEVENTF_KEYUP, 0);
788
789 // assume that any alphanumeric key changes the total number of characters
790 // in the control - this should work in 99% of cases
791 return GetValue().length() != lenOld;
792 }
793
794 #endif // __WIN32__
795
796 // ----------------------------------------------------------------------------
797 // Clipboard operations
798 // ----------------------------------------------------------------------------
799
800 void wxTextCtrl::Copy()
801 {
802 if (CanCopy())
803 {
804 ::SendMessage(GetHwnd(), WM_COPY, 0, 0L);
805 }
806 }
807
808 void wxTextCtrl::Cut()
809 {
810 if (CanCut())
811 {
812 ::SendMessage(GetHwnd(), WM_CUT, 0, 0L);
813 }
814 }
815
816 void wxTextCtrl::Paste()
817 {
818 if (CanPaste())
819 {
820 ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
821 }
822 }
823
824 bool wxTextCtrl::HasSelection() const
825 {
826 long from, to;
827 GetSelection(&from, &to);
828 return from != to;
829 }
830
831 bool wxTextCtrl::CanCopy() const
832 {
833 // Can copy if there's a selection
834 return HasSelection();
835 }
836
837 bool wxTextCtrl::CanCut() const
838 {
839 return CanCopy() && IsEditable();
840 }
841
842 bool wxTextCtrl::CanPaste() const
843 {
844 if ( !IsEditable() )
845 return FALSE;
846
847 #if wxUSE_RICHEDIT
848 if ( IsRich() )
849 {
850 UINT cf = 0; // 0 == any format
851
852 return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0;
853 }
854 #endif // wxUSE_RICHEDIT
855
856 // Standard edit control: check for straight text on clipboard
857 if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
858 return FALSE;
859
860 bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0;
861 ::CloseClipboard();
862
863 return isTextAvailable;
864 }
865
866 // ----------------------------------------------------------------------------
867 // Accessors
868 // ----------------------------------------------------------------------------
869
870 void wxTextCtrl::SetEditable(bool editable)
871 {
872 HWND hWnd = GetHwnd();
873 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
874 }
875
876 void wxTextCtrl::SetInsertionPoint(long pos)
877 {
878 DoSetSelection(pos, pos);
879 }
880
881 void wxTextCtrl::SetInsertionPointEnd()
882 {
883 // we must not do anything if the caret is already there because calling
884 // SetInsertionPoint() thaws the controls if Freeze() had been called even
885 // if it doesn't actually move the caret anywhere and so the simple fact of
886 // doing it results in horrible flicker when appending big amounts of text
887 // to the control in a few chunks (see DoAddText() test in the text sample)
888 if ( GetInsertionPoint() == GetLastPosition() )
889 return;
890
891 long pos;
892
893 #if wxUSE_RICHEDIT
894 if ( m_verRichEdit == 1 )
895 {
896 // we don't have to waste time calling GetLastPosition() in this case
897 pos = -1;
898 }
899 else // !RichEdit 1.0
900 #endif // wxUSE_RICHEDIT
901 {
902 pos = GetLastPosition();
903 }
904
905 SetInsertionPoint(pos);
906 }
907
908 long wxTextCtrl::GetInsertionPoint() const
909 {
910 #if wxUSE_RICHEDIT
911 if ( IsRich() )
912 {
913 CHARRANGE range;
914 range.cpMin = 0;
915 range.cpMax = 0;
916 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
917 return range.cpMin;
918 }
919 #endif // wxUSE_RICHEDIT
920
921 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
922 return Pos & 0xFFFF;
923 }
924
925 long wxTextCtrl::GetLastPosition() const
926 {
927 int numLines = GetNumberOfLines();
928 long posStartLastLine = XYToPosition(0, numLines - 1);
929
930 long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine);
931
932 return posStartLastLine + lenLastLine;
933 }
934
935 // If the return values from and to are the same, there is no
936 // selection.
937 void wxTextCtrl::GetSelection(long* from, long* to) const
938 {
939 #if wxUSE_RICHEDIT
940 if ( IsRich() )
941 {
942 CHARRANGE charRange;
943 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange);
944
945 *from = charRange.cpMin;
946 *to = charRange.cpMax;
947 }
948 else
949 #endif // !wxUSE_RICHEDIT
950 {
951 DWORD dwStart, dwEnd;
952 ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
953
954 *from = dwStart;
955 *to = dwEnd;
956 }
957 }
958
959 bool wxTextCtrl::IsEditable() const
960 {
961 // strangely enough, we may be called before the control is created: our
962 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
963 // us
964 if ( !m_hWnd )
965 return TRUE;
966
967 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
968
969 return (style & ES_READONLY) == 0;
970 }
971
972 // ----------------------------------------------------------------------------
973 // selection
974 // ----------------------------------------------------------------------------
975
976 void wxTextCtrl::SetSelection(long from, long to)
977 {
978 // if from and to are both -1, it means (in wxWindows) that all text should
979 // be selected - translate into Windows convention
980 if ( (from == -1) && (to == -1) )
981 {
982 from = 0;
983 to = -1;
984 }
985
986 DoSetSelection(from, to);
987 }
988
989 void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret)
990 {
991 HWND hWnd = GetHwnd();
992
993 #ifdef __WIN32__
994 #if wxUSE_RICHEDIT
995 if ( IsRich() )
996 {
997 CHARRANGE range;
998 range.cpMin = from;
999 range.cpMax = to;
1000 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
1001 }
1002 else
1003 #endif // wxUSE_RICHEDIT
1004 {
1005 SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to);
1006 }
1007
1008 if ( scrollCaret )
1009 {
1010 #if wxUSE_RICHEDIT
1011 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1012 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1013 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1014 // option is set (but it does work ok in richedit 1.0 mode...)
1015 //
1016 // so to make it work we either need to give focus to it here which
1017 // will probably create many problems (dummy focus events; window
1018 // containing the text control being brought to foreground
1019 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1020 // create other problems too -- and in fact it does because if we turn
1021 // on/off this style while appending the text to the control, the
1022 // vertical scrollbar never appears in it even if we append tons of
1023 // text and to work around this the only solution I found was to use
1024 // ES_DISABLENOSCROLL
1025 //
1026 // this is very ugly but I don't see any other way to make this work
1027 if ( GetRichVersion() > 1 )
1028 {
1029 if ( !HasFlag(wxTE_NOHIDESEL) )
1030 {
1031 ::SendMessage(GetHwnd(), EM_SETOPTIONS,
1032 ECOOP_OR, ECO_NOHIDESEL);
1033 }
1034 //else: everything is already ok
1035 }
1036 #endif // wxUSE_RICHEDIT
1037
1038 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
1039
1040 #if wxUSE_RICHEDIT
1041 // restore ECO_NOHIDESEL if we changed it
1042 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL) )
1043 {
1044 ::SendMessage(GetHwnd(), EM_SETOPTIONS,
1045 ECOOP_AND, ~ECO_NOHIDESEL);
1046 }
1047 #endif // wxUSE_RICHEDIT
1048 }
1049 #else // Win16
1050 // WPARAM is 0: selection is scrolled into view
1051 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(from, to));
1052 #endif // Win32/16
1053 }
1054
1055 // ----------------------------------------------------------------------------
1056 // Editing
1057 // ----------------------------------------------------------------------------
1058
1059 void wxTextCtrl::Replace(long from, long to, const wxString& value)
1060 {
1061 // Set selection and remove it
1062 DoSetSelection(from, to, FALSE /* don't scroll caret into view */);
1063
1064 SendMessage(GetHwnd(), EM_REPLACESEL,
1065 #ifdef __WIN32__
1066 TRUE,
1067 #else
1068 FALSE,
1069 #endif
1070 (LPARAM)value.c_str());
1071 }
1072
1073 void wxTextCtrl::Remove(long from, long to)
1074 {
1075 Replace(from, to, _T(""));
1076 }
1077
1078 bool wxTextCtrl::LoadFile(const wxString& file)
1079 {
1080 if ( wxTextCtrlBase::LoadFile(file) )
1081 {
1082 // update the size limit if needed
1083 AdjustSpaceLimit();
1084
1085 return TRUE;
1086 }
1087
1088 return FALSE;
1089 }
1090
1091 bool wxTextCtrl::IsModified() const
1092 {
1093 return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0;
1094 }
1095
1096 // Makes 'unmodified'
1097 void wxTextCtrl::DiscardEdits()
1098 {
1099 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
1100 }
1101
1102 int wxTextCtrl::GetNumberOfLines() const
1103 {
1104 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
1105 }
1106
1107 long wxTextCtrl::XYToPosition(long x, long y) const
1108 {
1109 // This gets the char index for the _beginning_ of this line
1110 long charIndex = SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
1111
1112 return charIndex + x;
1113 }
1114
1115 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
1116 {
1117 HWND hWnd = GetHwnd();
1118
1119 // This gets the line number containing the character
1120 long lineNo;
1121 #if wxUSE_RICHEDIT
1122 if ( IsRich() )
1123 {
1124 lineNo = SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
1125 }
1126 else
1127 #endif // wxUSE_RICHEDIT
1128 {
1129 lineNo = SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
1130 }
1131
1132 if ( lineNo == -1 )
1133 {
1134 // no such line
1135 return FALSE;
1136 }
1137
1138 // This gets the char index for the _beginning_ of this line
1139 long charIndex = SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
1140 if ( charIndex == -1 )
1141 {
1142 return FALSE;
1143 }
1144
1145 // The X position must therefore be the different between pos and charIndex
1146 if ( x )
1147 *x = pos - charIndex;
1148 if ( y )
1149 *y = lineNo;
1150
1151 return TRUE;
1152 }
1153
1154 void wxTextCtrl::ShowPosition(long pos)
1155 {
1156 HWND hWnd = GetHwnd();
1157
1158 // To scroll to a position, we pass the number of lines and characters
1159 // to scroll *by*. This means that we need to:
1160 // (1) Find the line position of the current line.
1161 // (2) Find the line position of pos.
1162 // (3) Scroll by (pos - current).
1163 // For now, ignore the horizontal scrolling.
1164
1165 // Is this where scrolling is relative to - the line containing the caret?
1166 // Or is the first visible line??? Try first visible line.
1167 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1168
1169 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
1170
1171 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
1172
1173 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
1174
1175 if (linesToScroll != 0)
1176 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
1177 }
1178
1179 long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const
1180 {
1181 return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0);
1182 }
1183
1184 int wxTextCtrl::GetLineLength(long lineNo) const
1185 {
1186 long pos = XYToPosition(0, lineNo);
1187
1188 return GetLengthOfLineContainingPos(pos);
1189 }
1190
1191 wxString wxTextCtrl::GetLineText(long lineNo) const
1192 {
1193 size_t len = (size_t)GetLineLength(lineNo) + 1;
1194
1195 // there must be at least enough place for the length WORD in the
1196 // buffer
1197 len += sizeof(WORD);
1198
1199 wxString str;
1200 wxChar *buf = str.GetWriteBuf(len);
1201
1202 *(WORD *)buf = (WORD)len;
1203 len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
1204 buf[len] = 0;
1205
1206 str.UngetWriteBuf(len);
1207
1208 return str;
1209 }
1210
1211 void wxTextCtrl::SetMaxLength(unsigned long len)
1212 {
1213 ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0);
1214 }
1215
1216 // ----------------------------------------------------------------------------
1217 // Undo/redo
1218 // ----------------------------------------------------------------------------
1219
1220 void wxTextCtrl::Undo()
1221 {
1222 if (CanUndo())
1223 {
1224 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
1225 }
1226 }
1227
1228 void wxTextCtrl::Redo()
1229 {
1230 if (CanRedo())
1231 {
1232 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1233 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
1234 }
1235 }
1236
1237 bool wxTextCtrl::CanUndo() const
1238 {
1239 return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
1240 }
1241
1242 bool wxTextCtrl::CanRedo() const
1243 {
1244 return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
1245 }
1246
1247 // ----------------------------------------------------------------------------
1248 // implemenation details
1249 // ----------------------------------------------------------------------------
1250
1251 void wxTextCtrl::Command(wxCommandEvent & event)
1252 {
1253 SetValue(event.GetString());
1254 ProcessCommand (event);
1255 }
1256
1257 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
1258 {
1259 // By default, load the first file into the text window.
1260 if (event.GetNumberOfFiles() > 0)
1261 {
1262 LoadFile(event.GetFiles()[0]);
1263 }
1264 }
1265
1266 // ----------------------------------------------------------------------------
1267 // kbd input processing
1268 // ----------------------------------------------------------------------------
1269
1270 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* pMsg)
1271 {
1272 MSG *msg = (MSG *)pMsg;
1273
1274 // check for our special keys here: if we don't do it and the parent frame
1275 // uses them as accelerators, they wouldn't work at all, so we disable
1276 // usual preprocessing for them
1277 if ( msg->message == WM_KEYDOWN )
1278 {
1279 WORD vkey = (WORD) msg->wParam;
1280 if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN )
1281 {
1282 if ( vkey == VK_BACK )
1283 return FALSE;
1284 }
1285 else // no Alt
1286 {
1287 // we want to process some Ctrl-foo and Shift-bar but no key
1288 // combinations without either Ctrl or Shift nor with both of them
1289 // pressed
1290 const int ctrl = wxIsCtrlDown(),
1291 shift = wxIsShiftDown();
1292 switch ( ctrl + shift )
1293 {
1294 default:
1295 wxFAIL_MSG( _T("how many modifiers have we got?") );
1296 // fall through
1297
1298 case 0:
1299 case 2:
1300 break;
1301
1302 case 1:
1303 // either Ctrl or Shift pressed
1304 if ( ctrl )
1305 {
1306 switch ( vkey )
1307 {
1308 case 'C':
1309 case 'V':
1310 case 'X':
1311 case VK_INSERT:
1312 case VK_DELETE:
1313 case VK_HOME:
1314 case VK_END:
1315 return FALSE;
1316 }
1317 }
1318 else // Shift is pressed
1319 {
1320 if ( vkey == VK_INSERT || vkey == VK_DELETE )
1321 return FALSE;
1322 }
1323 }
1324 }
1325 }
1326
1327 return wxControl::MSWShouldPreProcessMessage(pMsg);
1328 }
1329
1330 void wxTextCtrl::OnChar(wxKeyEvent& event)
1331 {
1332 switch ( event.GetKeyCode() )
1333 {
1334 case WXK_RETURN:
1335 if ( !(m_windowStyle & wxTE_MULTILINE) )
1336 {
1337 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1338 InitCommandEvent(event);
1339 event.SetString(GetValue());
1340 if ( GetEventHandler()->ProcessEvent(event) )
1341 return;
1342 }
1343 //else: multiline controls need Enter for themselves
1344
1345 break;
1346
1347 case WXK_TAB:
1348 // always produce navigation event - even if we process TAB
1349 // ourselves the fact that we got here means that the user code
1350 // decided to skip processing of this TAB - probably to let it
1351 // do its default job.
1352 {
1353 wxNavigationKeyEvent eventNav;
1354 eventNav.SetDirection(!event.ShiftDown());
1355 eventNav.SetWindowChange(event.ControlDown());
1356 eventNav.SetEventObject(this);
1357
1358 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
1359 return;
1360 }
1361 break;
1362 }
1363
1364 // no, we didn't process it
1365 event.Skip();
1366 }
1367
1368 long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1369 {
1370 long lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
1371
1372 if ( nMsg == WM_GETDLGCODE )
1373 {
1374 // we always want the chars and the arrows: the arrows for navigation
1375 // and the chars because we want Ctrl-C to work even in a read only
1376 // control
1377 long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
1378
1379 if ( IsEditable() )
1380 {
1381 // we may have several different cases:
1382 // 1. normal case: both TAB and ENTER are used for dlg navigation
1383 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1384 // next control in the dialog
1385 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1386 // navigation
1387 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1388 // to the next control
1389
1390 // the multiline edit control should always get <Return> for itself
1391 if ( HasFlag(wxTE_PROCESS_ENTER) || HasFlag(wxTE_MULTILINE) )
1392 lDlgCode |= DLGC_WANTMESSAGE;
1393
1394 if ( HasFlag(wxTE_PROCESS_TAB) )
1395 lDlgCode |= DLGC_WANTTAB;
1396
1397 lRc |= lDlgCode;
1398 }
1399 else // !editable
1400 {
1401 // NB: use "=", not "|=" as the base class version returns the
1402 // same flags is this state as usual (i.e. including
1403 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1404 // native Win32 apps?) but for now live with it.
1405 lRc = lDlgCode;
1406 }
1407 }
1408
1409 return lRc;
1410 }
1411
1412 // ----------------------------------------------------------------------------
1413 // text control event processing
1414 // ----------------------------------------------------------------------------
1415
1416 bool wxTextCtrl::SendUpdateEvent()
1417 {
1418 // is event reporting suspended?
1419 if ( m_suppressNextUpdate )
1420 {
1421 // do process the next one
1422 m_suppressNextUpdate = FALSE;
1423
1424 return FALSE;
1425 }
1426
1427 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
1428 InitCommandEvent(event);
1429 event.SetString(GetValue());
1430
1431 return ProcessCommand(event);
1432 }
1433
1434 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
1435 {
1436 switch ( param )
1437 {
1438 case EN_SETFOCUS:
1439 case EN_KILLFOCUS:
1440 {
1441 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1442 : wxEVT_SET_FOCUS,
1443 m_windowId);
1444 event.SetEventObject(this);
1445 GetEventHandler()->ProcessEvent(event);
1446 }
1447 break;
1448
1449 case EN_CHANGE:
1450 SendUpdateEvent();
1451 break;
1452
1453 case EN_MAXTEXT:
1454 // the text size limit has been hit -- try to increase it
1455 if ( !AdjustSpaceLimit() )
1456 {
1457 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId);
1458 InitCommandEvent(event);
1459 event.SetString(GetValue());
1460 ProcessCommand(event);
1461 }
1462 break;
1463
1464 // the other edit notification messages are not processed
1465 default:
1466 return FALSE;
1467 }
1468
1469 // processed
1470 return TRUE;
1471 }
1472
1473 WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
1474 #if wxUSE_CTL3D
1475 WXUINT message,
1476 WXWPARAM wParam,
1477 WXLPARAM lParam
1478 #else
1479 WXUINT WXUNUSED(message),
1480 WXWPARAM WXUNUSED(wParam),
1481 WXLPARAM WXUNUSED(lParam)
1482 #endif
1483 )
1484 {
1485 #if wxUSE_CTL3D
1486 if ( m_useCtl3D )
1487 {
1488 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
1489 return (WXHBRUSH) hbrush;
1490 }
1491 #endif // wxUSE_CTL3D
1492
1493 HDC hdc = (HDC)pDC;
1494 if (GetParent()->GetTransparentBackground())
1495 SetBkMode(hdc, TRANSPARENT);
1496 else
1497 SetBkMode(hdc, OPAQUE);
1498
1499 wxColour colBack = GetBackgroundColour();
1500
1501 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
1502 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1503
1504 ::SetBkColor(hdc, wxColourToRGB(colBack));
1505 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
1506
1507 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
1508
1509 return (WXHBRUSH)brush->GetResourceHandle();
1510 }
1511
1512 // In WIN16, need to override normal erasing because
1513 // Ctl3D doesn't use the wxWindows background colour.
1514 #ifdef __WIN16__
1515 void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
1516 {
1517 wxColour col(m_backgroundColour);
1518
1519 #if wxUSE_CTL3D
1520 if (m_useCtl3D)
1521 col = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1522 #endif
1523
1524 RECT rect;
1525 ::GetClientRect(GetHwnd(), &rect);
1526
1527 COLORREF ref = wxColourToRGB(col);
1528 HBRUSH hBrush = ::CreateSolidBrush(ref);
1529 if ( !hBrush )
1530 wxLogLastError(wxT("CreateSolidBrush"));
1531
1532 HDC hdc = (HDC)event.GetDC()->GetHDC();
1533
1534 int mode = ::SetMapMode(hdc, MM_TEXT);
1535
1536 ::FillRect(hdc, &rect, hBrush);
1537 ::DeleteObject(hBrush);
1538 ::SetMapMode(hdc, mode);
1539
1540 }
1541 #endif // Win16
1542
1543 bool wxTextCtrl::AdjustSpaceLimit()
1544 {
1545 #ifndef __WIN16__
1546 unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
1547
1548 // HACK: we try to automatically extend the limit for the amount of text
1549 // to allow (interactively) entering more than 64Kb of text under
1550 // Win9x but we shouldn't reset the text limit which was previously
1551 // set explicitly with SetMaxLength()
1552 //
1553 // we could solve this by storing the limit we set in wxTextCtrl but
1554 // to save space we prefer to simply test here the actual limit
1555 // value: we consider that SetMaxLength() can only be called for
1556 // values < 32Kb
1557 if ( limit < 0x8000 )
1558 {
1559 // we've got more text than limit set by SetMaxLength()
1560 return FALSE;
1561 }
1562
1563 unsigned int len = ::GetWindowTextLength(GetHwnd());
1564 if ( len >= limit )
1565 {
1566 limit = len + 0x8000; // 32Kb
1567
1568 #if wxUSE_RICHEDIT
1569 if ( IsRich() )
1570 {
1571 // as a nice side effect, this also allows passing limit > 64Kb
1572 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
1573 }
1574 else
1575 #endif // wxUSE_RICHEDIT
1576 {
1577 if ( limit > 0xffff )
1578 {
1579 // this will set it to a platform-dependent maximum (much more
1580 // than 64Kb under NT)
1581 limit = 0;
1582 }
1583
1584 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
1585 }
1586 }
1587 #endif // !Win16
1588
1589 // we changed the limit
1590 return TRUE;
1591 }
1592
1593 bool wxTextCtrl::AcceptsFocus() const
1594 {
1595 // we don't want focus if we can't be edited unless we're a multiline
1596 // control because then it might be still nice to get focus from keyboard
1597 // to be able to scroll it without mouse
1598 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1599 }
1600
1601 wxSize wxTextCtrl::DoGetBestSize() const
1602 {
1603 int cx, cy;
1604 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1605
1606 int wText = DEFAULT_ITEM_WIDTH;
1607
1608 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1609 if ( m_windowStyle & wxTE_MULTILINE )
1610 {
1611 hText *= wxMax(GetNumberOfLines(), 5);
1612 }
1613 //else: for single line control everything is ok
1614
1615 return wxSize(wText, hText);
1616 }
1617
1618 // ----------------------------------------------------------------------------
1619 // standard handlers for standard edit menu events
1620 // ----------------------------------------------------------------------------
1621
1622 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1623 {
1624 Cut();
1625 }
1626
1627 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1628 {
1629 Copy();
1630 }
1631
1632 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1633 {
1634 Paste();
1635 }
1636
1637 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1638 {
1639 Undo();
1640 }
1641
1642 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1643 {
1644 Redo();
1645 }
1646
1647 void wxTextCtrl::OnDelete(wxCommandEvent& event)
1648 {
1649 long from, to;
1650 GetSelection(& from, & to);
1651 if (from != -1 && to != -1)
1652 Remove(from, to);
1653 }
1654
1655 void wxTextCtrl::OnSelectAll(wxCommandEvent& event)
1656 {
1657 SetSelection(-1, -1);
1658 }
1659
1660 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1661 {
1662 event.Enable( CanCut() );
1663 }
1664
1665 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1666 {
1667 event.Enable( CanCopy() );
1668 }
1669
1670 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1671 {
1672 event.Enable( CanPaste() );
1673 }
1674
1675 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1676 {
1677 event.Enable( CanUndo() );
1678 }
1679
1680 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1681 {
1682 event.Enable( CanRedo() );
1683 }
1684
1685 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
1686 {
1687 long from, to;
1688 GetSelection(& from, & to);
1689 event.Enable(from != -1 && to != -1 && from != to && IsEditable()) ;
1690 }
1691
1692 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
1693 {
1694 event.Enable(GetLastPosition() > 0);
1695 }
1696
1697 void wxTextCtrl::OnRightClick(wxMouseEvent& event)
1698 {
1699 #if wxUSE_RICHEDIT
1700 if (IsRich())
1701 {
1702 if (!m_privateContextMenu)
1703 {
1704 m_privateContextMenu = new wxMenu;
1705 m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
1706 m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
1707 m_privateContextMenu->AppendSeparator();
1708 m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
1709 m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
1710 m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
1711 m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
1712 m_privateContextMenu->AppendSeparator();
1713 m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
1714 }
1715 PopupMenu(m_privateContextMenu, event.GetPosition());
1716 return;
1717 }
1718 else
1719 #endif
1720 event.Skip();
1721 }
1722
1723 // the rest of the file only deals with the rich edit controls
1724 #if wxUSE_RICHEDIT
1725
1726 // ----------------------------------------------------------------------------
1727 // EN_LINK processing
1728 // ----------------------------------------------------------------------------
1729
1730 bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1731 {
1732 NMHDR *hdr = (NMHDR* )lParam;
1733 switch ( hdr->code )
1734 {
1735 case EN_MSGFILTER:
1736 {
1737 const MSGFILTER *msgf = (MSGFILTER *)lParam;
1738 UINT msg = msgf->msg;
1739
1740 // this is a bit crazy but richedit 1.0 sends us all mouse
1741 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1742 // generate the wxWin events for this message manually
1743 //
1744 // NB: in fact, this is still not totally correct as it does
1745 // send us WM_LBUTTONUP if the selection was cleared by the
1746 // last click -- so currently we get 2 events in this case,
1747 // but as I don't see any obvious way to check for this I
1748 // leave this code in place because it's still better than
1749 // not getting left up events at all
1750 if ( msg == WM_LBUTTONUP )
1751 {
1752 WXUINT flags = msgf->wParam;
1753 int x = GET_X_LPARAM(msgf->lParam),
1754 y = GET_Y_LPARAM(msgf->lParam);
1755
1756 HandleMouseEvent(msg, x, y, flags);
1757 }
1758 }
1759
1760 // return TRUE to process the event (and FALSE to ignore it)
1761 return TRUE;
1762
1763 case EN_LINK:
1764 {
1765 const ENLINK *enlink = (ENLINK *)hdr;
1766
1767 switch ( enlink->msg )
1768 {
1769 case WM_SETCURSOR:
1770 // ok, so it is hardcoded - do we really nee to
1771 // customize it?
1772 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND)));
1773 *result = TRUE;
1774 break;
1775
1776 case WM_MOUSEMOVE:
1777 case WM_LBUTTONDOWN:
1778 case WM_LBUTTONUP:
1779 case WM_LBUTTONDBLCLK:
1780 case WM_RBUTTONDOWN:
1781 case WM_RBUTTONUP:
1782 case WM_RBUTTONDBLCLK:
1783 // send a mouse event
1784 {
1785 static const wxEventType eventsMouse[] =
1786 {
1787 wxEVT_MOTION,
1788 wxEVT_LEFT_DOWN,
1789 wxEVT_LEFT_UP,
1790 wxEVT_LEFT_DCLICK,
1791 wxEVT_RIGHT_DOWN,
1792 wxEVT_RIGHT_UP,
1793 wxEVT_RIGHT_DCLICK,
1794 };
1795
1796 // the event ids are consecutive
1797 wxMouseEvent
1798 evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]);
1799
1800 InitMouseEvent(evtMouse,
1801 GET_X_LPARAM(enlink->lParam),
1802 GET_Y_LPARAM(enlink->lParam),
1803 enlink->wParam);
1804
1805 wxTextUrlEvent event(m_windowId, evtMouse,
1806 enlink->chrg.cpMin,
1807 enlink->chrg.cpMax);
1808
1809 InitCommandEvent(event);
1810
1811 *result = ProcessCommand(event);
1812 }
1813 break;
1814 }
1815 }
1816 return TRUE;
1817 }
1818
1819 // not processed, leave it to the base class
1820 return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result);
1821 }
1822
1823 // ----------------------------------------------------------------------------
1824 // colour setting for the rich edit controls
1825 // ----------------------------------------------------------------------------
1826
1827 bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
1828 {
1829 if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
1830 {
1831 // colour didn't really change
1832 return FALSE;
1833 }
1834
1835 if ( IsRich() )
1836 {
1837 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1838 // EM_SETBKGNDCOLOR additionally
1839 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
1840 }
1841
1842 return TRUE;
1843 }
1844
1845 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1846 {
1847 if ( !wxTextCtrlBase::SetForegroundColour(colour) )
1848 {
1849 // colour didn't really change
1850 return FALSE;
1851 }
1852
1853 if ( IsRich() )
1854 {
1855 // change the colour of everything
1856 CHARFORMAT cf;
1857 wxZeroMemory(cf);
1858 cf.cbSize = sizeof(cf);
1859 cf.dwMask = CFM_COLOR;
1860 cf.crTextColor = wxColourToRGB(colour);
1861 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
1862 }
1863
1864 return TRUE;
1865 }
1866
1867 // ----------------------------------------------------------------------------
1868 // styling support for rich edit controls
1869 // ----------------------------------------------------------------------------
1870
1871 #if wxUSE_RICHEDIT
1872
1873 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
1874 {
1875 if ( !IsRich() )
1876 {
1877 // can't do it with normal text control
1878 return FALSE;
1879 }
1880
1881 // the richedit 1.0 doesn't handle setting background colour, so don't
1882 // even try to do anything if it's the only thing we want to change
1883 if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() &&
1884 !style.HasLeftIndent() && !style.HasRightIndent() && !style.HasAlignment() &&
1885 !style.HasTabs() )
1886 {
1887 // nothing to do: return TRUE if there was really nothing to do and
1888 // FALSE if we failed to set bg colour
1889 return !style.HasBackgroundColour();
1890 }
1891
1892 // order the range if needed
1893 if ( start > end )
1894 {
1895 long tmp = start;
1896 start = end;
1897 end = tmp;
1898 }
1899
1900 // we can only change the format of the selection, so select the range we
1901 // want and restore the old selection later
1902 long startOld, endOld;
1903 GetSelection(&startOld, &endOld);
1904
1905 // but do we really have to change the selection?
1906 bool changeSel = start != startOld || end != endOld;
1907
1908 if ( changeSel )
1909 {
1910 DoSetSelection(start, end, FALSE /* don't scroll caret into view */);
1911 }
1912
1913 // initialize CHARFORMAT struct
1914 #if wxUSE_RICHEDIT2
1915 CHARFORMAT2 cf;
1916 #else
1917 CHARFORMAT cf;
1918 #endif
1919
1920 wxZeroMemory(cf);
1921
1922 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1923 // CHARFORMAT in that case
1924 #if wxUSE_RICHEDIT2
1925 if ( m_verRichEdit == 1 )
1926 {
1927 // this is the only thing the control is going to grok
1928 cf.cbSize = sizeof(CHARFORMAT);
1929 }
1930 else
1931 #endif
1932 {
1933 // CHARFORMAT or CHARFORMAT2
1934 cf.cbSize = sizeof(cf);
1935 }
1936
1937 if ( style.HasFont() )
1938 {
1939 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1940 // but using it doesn't seem to hurt neither so leaving it for now
1941
1942 cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET |
1943 CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE;
1944
1945 // fill in data from LOGFONT but recalculate lfHeight because we need
1946 // the real height in twips and not the negative number which
1947 // wxFillLogFont() returns (this is correct in general and works with
1948 // the Windows font mapper, but not here)
1949 LOGFONT lf;
1950 wxFillLogFont(&lf, &style.GetFont());
1951 cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips
1952 cf.bCharSet = lf.lfCharSet;
1953 cf.bPitchAndFamily = lf.lfPitchAndFamily;
1954 wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
1955
1956 // also deal with underline/italic/bold attributes: note that we must
1957 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1958 // style to allow clearing it
1959 if ( lf.lfItalic )
1960 {
1961 cf.dwEffects |= CFE_ITALIC;
1962 }
1963
1964 if ( lf.lfWeight == FW_BOLD )
1965 {
1966 cf.dwEffects |= CFE_BOLD;
1967 }
1968
1969 if ( lf.lfUnderline )
1970 {
1971 cf.dwEffects |= CFE_UNDERLINE;
1972 }
1973
1974 // strikeout fonts are not supported by wxWindows
1975 }
1976
1977 if ( style.HasTextColour() )
1978 {
1979 cf.dwMask |= CFM_COLOR;
1980 cf.crTextColor = wxColourToRGB(style.GetTextColour());
1981 }
1982
1983 #if wxUSE_RICHEDIT2
1984 if ( m_verRichEdit != 1 && style.HasBackgroundColour() )
1985 {
1986 cf.dwMask |= CFM_BACKCOLOR;
1987 cf.crBackColor = wxColourToRGB(style.GetBackgroundColour());
1988 }
1989 #endif // wxUSE_RICHEDIT2
1990
1991 // do format the selection
1992 bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
1993 SCF_SELECTION, (LPARAM)&cf) != 0;
1994 if ( !ok )
1995 {
1996 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1997 }
1998
1999 // now do the paragraph formatting
2000 PARAFORMAT2 pf;
2001 wxZeroMemory(pf);
2002 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2003 // PARAFORMAT in that case
2004 #if wxUSE_RICHEDIT2
2005 if ( m_verRichEdit == 1 )
2006 {
2007 // this is the only thing the control is going to grok
2008 pf.cbSize = sizeof(PARAFORMAT);
2009 }
2010 else
2011 #endif
2012 {
2013 // PARAFORMAT or PARAFORMAT2
2014 pf.cbSize = sizeof(pf);
2015 }
2016
2017 if (style.HasAlignment())
2018 {
2019 pf.dwMask |= PFM_ALIGNMENT;
2020 if (style.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
2021 pf.wAlignment = PFA_RIGHT;
2022 else if (style.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
2023 pf.wAlignment = PFA_CENTER;
2024 else if (style.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED)
2025 pf.wAlignment = PFA_JUSTIFY;
2026 else
2027 pf.wAlignment = PFA_LEFT;
2028 }
2029
2030 if (style.HasLeftIndent())
2031 {
2032 pf.dwMask |= PFM_STARTINDENT;
2033
2034 // Convert from 1/10 mm to TWIPS
2035 pf.dxStartIndent = (int) (((double) style.GetLeftIndent()) * mm2twips / 10.0) ;
2036
2037 // TODO: do we need to specify dxOffset?
2038 }
2039
2040 if (style.HasRightIndent())
2041 {
2042 pf.dwMask |= PFM_RIGHTINDENT;
2043
2044 // Convert from 1/10 mm to TWIPS
2045 pf.dxRightIndent = (int) (((double) style.GetRightIndent()) * mm2twips / 10.0) ;
2046 }
2047
2048 if (style.HasTabs())
2049 {
2050 pf.dwMask |= PFM_TABSTOPS;
2051
2052 const wxArrayInt& tabs = style.GetTabs();
2053
2054 pf.cTabCount = wxMin(tabs.GetCount(), MAX_TAB_STOPS);
2055 size_t i;
2056 for (i = 0; i < (size_t) pf.cTabCount; i++)
2057 {
2058 // Convert from 1/10 mm to TWIPS
2059 pf.rgxTabs[i] = (int) (((double) tabs[i]) * mm2twips / 10.0) ;
2060 }
2061 }
2062
2063 if (pf.dwMask != 0)
2064 {
2065 // do format the selection
2066 bool ok = ::SendMessage(GetHwnd(), EM_SETPARAFORMAT,
2067 0, (LPARAM) &pf) != 0;
2068 if ( !ok )
2069 {
2070 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2071 }
2072 }
2073
2074 if ( changeSel )
2075 {
2076 // restore the original selection
2077 DoSetSelection(startOld, endOld, FALSE);
2078 }
2079
2080 return ok;
2081 }
2082
2083 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2084 {
2085 if ( !wxTextCtrlBase::SetDefaultStyle(style) )
2086 return FALSE;
2087
2088 // we have to do this or the style wouldn't apply for the text typed by the
2089 // user
2090 long posLast = GetLastPosition();
2091 SetStyle(posLast, posLast, m_defaultStyle);
2092
2093 return TRUE;
2094 }
2095
2096 bool wxTextCtrl::GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style))
2097 {
2098 // TODO
2099 return FALSE;
2100 }
2101
2102 #endif
2103
2104 // ----------------------------------------------------------------------------
2105 // wxRichEditModule
2106 // ----------------------------------------------------------------------------
2107
2108 bool wxRichEditModule::OnInit()
2109 {
2110 // don't do anything - we will load it when needed
2111 return TRUE;
2112 }
2113
2114 void wxRichEditModule::OnExit()
2115 {
2116 for ( size_t i = 0; i < WXSIZEOF(ms_hRichEdit); i++ )
2117 {
2118 if ( ms_hRichEdit[i] )
2119 {
2120 ::FreeLibrary(ms_hRichEdit[i]);
2121 }
2122 }
2123 }
2124
2125 /* static */
2126 bool wxRichEditModule::Load(int version)
2127 {
2128 // we don't support loading richedit 3.0 as I don't know how to distinguish
2129 // it from 2.0 anyhow
2130 wxCHECK_MSG( version == 1 || version == 2, FALSE,
2131 _T("incorrect richedit control version requested") );
2132
2133 // make it the index in the array
2134 version--;
2135
2136 if ( ms_hRichEdit[version] == (HINSTANCE)-1 )
2137 {
2138 // we had already tried to load it and failed
2139 return FALSE;
2140 }
2141
2142 if ( ms_hRichEdit[version] )
2143 {
2144 // we've already got this one
2145 return TRUE;
2146 }
2147
2148 wxString dllname = version ? _T("riched20") : _T("riched32");
2149 dllname += _T(".dll");
2150
2151 ms_hRichEdit[version] = ::LoadLibrary(dllname);
2152
2153 if ( !ms_hRichEdit[version] )
2154 {
2155 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
2156
2157 ms_hRichEdit[version] = (HINSTANCE)-1;
2158
2159 return FALSE;
2160 }
2161
2162 return TRUE;
2163 }
2164
2165 #endif // wxUSE_RICHEDIT
2166
2167 #endif // wxUSE_TEXTCTRL