we don't need to subclass text control twice to implement support for WM_COPY/CUT...
[wxWidgets.git] / src / msw / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TEXTCTRL && !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
28
29 #ifndef WX_PRECOMP
30 #include "wx/textctrl.h"
31 #include "wx/settings.h"
32 #include "wx/brush.h"
33 #include "wx/utils.h"
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #include "wx/app.h"
37 #include "wx/menu.h"
38 #include "wx/math.h"
39 #include "wx/module.h"
40 #endif
41
42 #include "wx/sysopt.h"
43
44 #if wxUSE_CLIPBOARD
45 #include "wx/clipbrd.h"
46 #endif
47
48 #include "wx/textfile.h"
49
50 #include <windowsx.h>
51
52 #include "wx/msw/private.h"
53 #include "wx/msw/winundef.h"
54 #include "wx/msw/mslu.h"
55
56 #include <string.h>
57 #include <stdlib.h>
58
59 #ifndef __WXWINCE__
60 #include <sys/types.h>
61 #endif
62
63 #if wxUSE_RICHEDIT
64
65 #if wxUSE_INKEDIT
66 #include "wx/dynlib.h"
67 #endif
68
69 // old mingw32 has richedit stuff directly in windows.h and doesn't have
70 // richedit.h at all
71 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
72 #include <richedit.h>
73 #endif
74
75 #endif // wxUSE_RICHEDIT
76
77 #include "wx/msw/missing.h"
78
79 // ----------------------------------------------------------------------------
80 // private classes
81 // ----------------------------------------------------------------------------
82
83 #if wxUSE_RICHEDIT
84
85 // this module initializes RichEdit DLL(s) if needed
86 class wxRichEditModule : public wxModule
87 {
88 public:
89 enum Version
90 {
91 Version_1, // riched32.dll
92 Version_2or3, // both use riched20.dll
93 Version_41, // msftedit.dll (XP SP1 and Windows 2003)
94 Version_Max
95 };
96
97 virtual bool OnInit();
98 virtual void OnExit();
99
100 // load the richedit DLL for the specified version of rich edit
101 static bool Load(Version version);
102
103 #if wxUSE_INKEDIT
104 // load the InkEdit library
105 static bool LoadInkEdit();
106 #endif
107
108 private:
109 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
110 static HINSTANCE ms_hRichEdit[Version_Max];
111
112 #if wxUSE_INKEDIT
113 static wxDynamicLibrary ms_inkEditLib;
114 static bool ms_inkEditLibLoadAttemped;
115 #endif
116
117 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
118 };
119
120 HINSTANCE wxRichEditModule::ms_hRichEdit[Version_Max] = { NULL, NULL, NULL };
121
122 #if wxUSE_INKEDIT
123 wxDynamicLibrary wxRichEditModule::ms_inkEditLib;
124 bool wxRichEditModule::ms_inkEditLibLoadAttemped = false;
125 #endif
126
127 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
128
129 #endif // wxUSE_RICHEDIT
130
131 // a small class used to set m_updatesCount to 0 (to filter duplicate events if
132 // necessary) and to reset it back to -1 afterwards
133 class UpdatesCountFilter
134 {
135 public:
136 UpdatesCountFilter(int& count)
137 : m_count(count)
138 {
139 wxASSERT_MSG( m_count == -1 || m_count == -2,
140 _T("wrong initial m_updatesCount value") );
141
142 if (m_count != -2)
143 m_count = 0;
144 //else: we don't want to count how many update events we get as we're going
145 // to ignore all of them
146 }
147
148 ~UpdatesCountFilter()
149 {
150 m_count = -1;
151 }
152
153 // return true if an event has been received
154 bool GotUpdate() const
155 {
156 return m_count == 1;
157 }
158
159 private:
160 int& m_count;
161
162 DECLARE_NO_COPY_CLASS(UpdatesCountFilter)
163 };
164
165 // ----------------------------------------------------------------------------
166 // event tables and other macros
167 // ----------------------------------------------------------------------------
168
169 #if wxUSE_EXTENDED_RTTI
170 WX_DEFINE_FLAGS( wxTextCtrlStyle )
171
172 wxBEGIN_FLAGS( wxTextCtrlStyle )
173 // new style border flags, we put them first to
174 // use them for streaming out
175 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
176 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
177 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
178 wxFLAGS_MEMBER(wxBORDER_RAISED)
179 wxFLAGS_MEMBER(wxBORDER_STATIC)
180 wxFLAGS_MEMBER(wxBORDER_NONE)
181
182 // old style border flags
183 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
184 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
185 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
186 wxFLAGS_MEMBER(wxRAISED_BORDER)
187 wxFLAGS_MEMBER(wxSTATIC_BORDER)
188 wxFLAGS_MEMBER(wxBORDER)
189
190 // standard window styles
191 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
192 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
193 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
194 wxFLAGS_MEMBER(wxWANTS_CHARS)
195 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
196 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
197 wxFLAGS_MEMBER(wxVSCROLL)
198 wxFLAGS_MEMBER(wxHSCROLL)
199
200 wxFLAGS_MEMBER(wxTE_PROCESS_ENTER)
201 wxFLAGS_MEMBER(wxTE_PROCESS_TAB)
202 wxFLAGS_MEMBER(wxTE_MULTILINE)
203 wxFLAGS_MEMBER(wxTE_PASSWORD)
204 wxFLAGS_MEMBER(wxTE_READONLY)
205 wxFLAGS_MEMBER(wxHSCROLL)
206 wxFLAGS_MEMBER(wxTE_RICH)
207 wxFLAGS_MEMBER(wxTE_RICH2)
208 wxFLAGS_MEMBER(wxTE_AUTO_URL)
209 wxFLAGS_MEMBER(wxTE_NOHIDESEL)
210 wxFLAGS_MEMBER(wxTE_LEFT)
211 wxFLAGS_MEMBER(wxTE_CENTRE)
212 wxFLAGS_MEMBER(wxTE_RIGHT)
213 wxFLAGS_MEMBER(wxTE_DONTWRAP)
214 wxFLAGS_MEMBER(wxTE_CHARWRAP)
215 wxFLAGS_MEMBER(wxTE_WORDWRAP)
216
217 wxEND_FLAGS( wxTextCtrlStyle )
218
219 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl, wxControl,"wx/textctrl.h")
220
221 wxBEGIN_PROPERTIES_TABLE(wxTextCtrl)
222 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
223 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
224
225 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
226 wxPROPERTY( Value , wxString , SetValue, GetValue, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
227 wxPROPERTY_FLAGS( WindowStyle , wxTextCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
228 wxEND_PROPERTIES_TABLE()
229
230 wxBEGIN_HANDLERS_TABLE(wxTextCtrl)
231 wxEND_HANDLERS_TABLE()
232
233 wxCONSTRUCTOR_6( wxTextCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size , long , WindowStyle)
234 #else
235 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
236 #endif
237
238
239 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
240 EVT_CHAR(wxTextCtrl::OnChar)
241 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
242
243 #if wxUSE_RICHEDIT
244 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu)
245 #endif
246
247 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
248 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
249 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
250 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
251 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
252 EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete)
253 EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll)
254
255 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
256 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
257 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
258 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
259 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
260 EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete)
261 EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll)
262
263 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus)
264 END_EVENT_TABLE()
265
266 // ============================================================================
267 // implementation
268 // ============================================================================
269
270 // ----------------------------------------------------------------------------
271 // creation
272 // ----------------------------------------------------------------------------
273
274 void wxTextCtrl::Init()
275 {
276 #if wxUSE_RICHEDIT
277 m_verRichEdit = 0;
278 #endif // wxUSE_RICHEDIT
279
280 #if wxUSE_INKEDIT && wxUSE_RICHEDIT
281 m_isInkEdit = 0;
282 #endif
283
284 m_privateContextMenu = NULL;
285 m_updatesCount = -1;
286 m_isNativeCaretShown = true;
287 }
288
289 wxTextCtrl::~wxTextCtrl()
290 {
291 delete m_privateContextMenu;
292 }
293
294 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
295 const wxString& value,
296 const wxPoint& pos,
297 const wxSize& size,
298 long style,
299 const wxValidator& validator,
300 const wxString& name)
301 {
302 #ifdef __WXWINCE__
303 if ((style & wxBORDER_MASK) == 0)
304 style |= wxBORDER_SIMPLE;
305 #endif
306
307 // base initialization
308 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
309 return false;
310
311 // translate wxWin style flags to MSW ones
312 WXDWORD msStyle = MSWGetCreateWindowFlags();
313
314 // do create the control - either an EDIT or RICHEDIT
315 wxString windowClass = wxT("EDIT");
316
317 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
318 // A control that capitalizes the first letter
319 if (style & wxTE_CAPITALIZE)
320 windowClass = wxT("CAPEDIT");
321 #endif
322
323 #if wxUSE_RICHEDIT
324 if ( m_windowStyle & wxTE_AUTO_URL )
325 {
326 // automatic URL detection only works in RichEdit 2.0+
327 m_windowStyle |= wxTE_RICH2;
328 }
329
330 if ( m_windowStyle & wxTE_RICH2 )
331 {
332 // using richedit 2.0 implies using wxTE_RICH
333 m_windowStyle |= wxTE_RICH;
334 }
335
336 // we need to load the richedit DLL before creating the rich edit control
337 if ( m_windowStyle & wxTE_RICH )
338 {
339 // versions 2.0, 3.0 and 4.1 of rich edit are mostly compatible with
340 // each other but not with version 1.0, so we have separate flags for
341 // the version 1.0 and the others (and so m_verRichEdit may be 0 (plain
342 // EDIT control), 1 for version 1.0 or 2 for any higher version)
343 //
344 // notice that 1.0 has no Unicode support at all so in Unicode build we
345 // must use another version
346
347 #if wxUSE_UNICODE
348 m_verRichEdit = 2;
349 #else // !wxUSE_UNICODE
350 m_verRichEdit = m_windowStyle & wxTE_RICH2 ? 2 : 1;
351 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
352
353 #if wxUSE_INKEDIT
354 // First test if we can load an ink edit control. Normally, all edit
355 // controls will be made ink edit controls if a tablet environment is
356 // found (or if msw.inkedit != 0 and the InkEd.dll is present).
357 // However, an application can veto ink edit controls by either specifying
358 // msw.inkedit = 0 or by removing wxTE_RICH[2] from the style.
359 //
360 if ((wxSystemSettings::HasFeature(wxSYS_TABLET_PRESENT) || wxSystemOptions::GetOptionInt(wxT("msw.inkedit")) != 0) &&
361 !(wxSystemOptions::HasOption(wxT("msw.inkedit")) && wxSystemOptions::GetOptionInt(wxT("msw.inkedit")) == 0))
362 {
363 if (wxRichEditModule::LoadInkEdit())
364 {
365 windowClass = INKEDIT_CLASS;
366
367 #if wxUSE_INKEDIT && wxUSE_RICHEDIT
368 m_isInkEdit = 1;
369 #endif
370
371 // Fake rich text version for other calls
372 m_verRichEdit = 2;
373 }
374 }
375 #endif
376
377 if (!IsInkEdit())
378 {
379 if ( m_verRichEdit == 2 )
380 {
381 if ( wxRichEditModule::Load(wxRichEditModule::Version_41) )
382 {
383 // yes, class name for version 4.1 really is 5.0
384 windowClass = _T("RICHEDIT50W");
385 }
386 else if ( wxRichEditModule::Load(wxRichEditModule::Version_2or3) )
387 {
388 windowClass = _T("RichEdit20")
389 #if wxUSE_UNICODE
390 _T("W");
391 #else // ANSI
392 _T("A");
393 #endif // Unicode/ANSI
394 }
395 else // failed to load msftedit.dll and riched20.dll
396 {
397 m_verRichEdit = 1;
398 }
399 }
400
401 if ( m_verRichEdit == 1 )
402 {
403 if ( wxRichEditModule::Load(wxRichEditModule::Version_1) )
404 {
405 windowClass = _T("RICHEDIT");
406 }
407 else // failed to load any richedit control DLL
408 {
409 // only give the error msg once if the DLL can't be loaded
410 static bool s_errorGiven = false; // MT ok as only used by GUI
411
412 if ( !s_errorGiven )
413 {
414 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
415
416 s_errorGiven = true;
417 }
418
419 m_verRichEdit = 0;
420 }
421 }
422 } // !useInkEdit
423 }
424 #endif // wxUSE_RICHEDIT
425
426 // we need to turn '\n's into "\r\n"s for the multiline controls
427 wxString valueWin;
428 if ( m_windowStyle & wxTE_MULTILINE )
429 {
430 valueWin = wxTextFile::Translate(value, wxTextFileType_Dos);
431 }
432 else // single line
433 {
434 valueWin = value;
435 }
436
437 if ( !MSWCreateControl(windowClass, msStyle, pos, size, valueWin) )
438 return false;
439
440 #if wxUSE_RICHEDIT
441 if (IsRich())
442 {
443 #if wxUSE_INKEDIT
444 if (IsInkEdit())
445 {
446 // Pass IEM_InsertText (0) as wParam, in order to have the ink always
447 // converted to text.
448 ::SendMessage(GetHwnd(), EM_SETINKINSERTMODE, 0, 0);
449
450 // Make sure the mouse can be used for input
451 ::SendMessage(GetHwnd(), EM_SETUSEMOUSEFORINPUT, 1, 0);
452 }
453 #endif
454
455 // enable the events we're interested in: we want to get EN_CHANGE as
456 // for the normal controls
457 LPARAM mask = ENM_CHANGE;
458
459 if (GetRichVersion() == 1 && !IsInkEdit())
460 {
461 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
462 // explained in its handler
463 mask |= ENM_MOUSEEVENTS;
464
465 // we also need to force the appearance of the vertical scrollbar
466 // initially as otherwise the control doesn't refresh correctly
467 // after resize: but once the vertical scrollbar had been shown
468 // (even if it's subsequently hidden) it does
469 //
470 // this is clearly a bug and for now it has been only noticed under
471 // Windows XP, so if we're sure it works correctly under other
472 // systems we could do this only for XP
473 SetSize(-1, 1); // 1 is small enough to force vert scrollbar
474 SetSize(size);
475 }
476 else if ( m_windowStyle & wxTE_AUTO_URL )
477 {
478 mask |= ENM_LINK;
479
480 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0);
481 }
482
483 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask);
484 }
485 #endif // wxUSE_RICHEDIT
486
487 return true;
488 }
489
490 // Make sure the window style (etc.) reflects the HWND style (roughly)
491 void wxTextCtrl::AdoptAttributesFromHWND()
492 {
493 wxWindow::AdoptAttributesFromHWND();
494
495 HWND hWnd = GetHwnd();
496 long style = ::GetWindowLong(hWnd, GWL_STYLE);
497
498 // retrieve the style to see whether this is an edit or richedit ctrl
499 #if wxUSE_RICHEDIT
500 wxString classname = wxGetWindowClass(GetHWND());
501
502 if ( classname.IsSameAs(_T("EDIT"), false /* no case */) )
503 {
504 m_verRichEdit = 0;
505 }
506 else // rich edit?
507 {
508 wxChar c;
509 if ( wxSscanf(classname, _T("RichEdit%d0%c"), &m_verRichEdit, &c) != 2 )
510 {
511 wxLogDebug(_T("Unknown edit control '%s'."), classname.c_str());
512
513 m_verRichEdit = 0;
514 }
515 }
516 #endif // wxUSE_RICHEDIT
517
518 if (style & ES_MULTILINE)
519 m_windowStyle |= wxTE_MULTILINE;
520 if (style & ES_PASSWORD)
521 m_windowStyle |= wxTE_PASSWORD;
522 if (style & ES_READONLY)
523 m_windowStyle |= wxTE_READONLY;
524 if (style & ES_WANTRETURN)
525 m_windowStyle |= wxTE_PROCESS_ENTER;
526 if (style & ES_CENTER)
527 m_windowStyle |= wxTE_CENTRE;
528 if (style & ES_RIGHT)
529 m_windowStyle |= wxTE_RIGHT;
530 }
531
532 WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
533 {
534 long msStyle = wxControl::MSWGetStyle(style, exstyle);
535
536 // styles which we alaways add by default
537 if ( style & wxTE_MULTILINE )
538 {
539 msStyle |= ES_MULTILINE | ES_WANTRETURN;
540 if ( !(style & wxTE_NO_VSCROLL) )
541 {
542 // always adjust the vertical scrollbar automatically if we have it
543 msStyle |= WS_VSCROLL | ES_AUTOVSCROLL;
544
545 #if wxUSE_RICHEDIT
546 // we have to use this style for the rich edit controls because
547 // without it the vertical scrollbar never appears at all in
548 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
549 if ( style & wxTE_RICH2 )
550 {
551 msStyle |= ES_DISABLENOSCROLL;
552 }
553 #endif // wxUSE_RICHEDIT
554 }
555
556 style |= wxTE_PROCESS_ENTER;
557 }
558 else // !multiline
559 {
560 // there is really no reason to not have this style for single line
561 // text controls
562 msStyle |= ES_AUTOHSCROLL;
563 }
564
565 // note that wxTE_DONTWRAP is the same as wxHSCROLL so if we have a horz
566 // scrollbar, there is no wrapping -- which makes sense
567 if ( style & wxTE_DONTWRAP )
568 {
569 // automatically scroll the control horizontally as necessary
570 //
571 // NB: ES_AUTOHSCROLL is needed for richedit controls or they don't
572 // show horz scrollbar at all, even in spite of WS_HSCROLL, and as
573 // it doesn't seem to do any harm for plain edit controls, add it
574 // always
575 msStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
576 }
577
578 if ( style & wxTE_READONLY )
579 msStyle |= ES_READONLY;
580
581 if ( style & wxTE_PASSWORD )
582 msStyle |= ES_PASSWORD;
583
584 if ( style & wxTE_NOHIDESEL )
585 msStyle |= ES_NOHIDESEL;
586
587 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
588 if ( style & wxTE_CENTRE )
589 msStyle |= ES_CENTER;
590 else if ( style & wxTE_RIGHT )
591 msStyle |= ES_RIGHT;
592 else
593 msStyle |= ES_LEFT; // ES_LEFT is 0 as well but for consistency...
594
595 return msStyle;
596 }
597
598 void wxTextCtrl::SetWindowStyleFlag(long style)
599 {
600 #if wxUSE_RICHEDIT
601 // we have to deal with some styles separately because they can't be
602 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
603 // using richedit-specific EM_SETOPTIONS
604 if ( IsRich() &&
605 ((style & wxTE_NOHIDESEL) != (GetWindowStyle() & wxTE_NOHIDESEL)) )
606 {
607 bool set = (style & wxTE_NOHIDESEL) != 0;
608
609 ::SendMessage(GetHwnd(), EM_SETOPTIONS, set ? ECOOP_OR : ECOOP_AND,
610 set ? ECO_NOHIDESEL : ~ECO_NOHIDESEL);
611 }
612 #endif // wxUSE_RICHEDIT
613
614 wxControl::SetWindowStyleFlag(style);
615 }
616
617 // ----------------------------------------------------------------------------
618 // set/get the controls text
619 // ----------------------------------------------------------------------------
620
621 bool wxTextCtrl::IsEmpty() const
622 {
623 // this is an optimization for multiline controls containing a lot of text
624 if ( IsMultiLine() && GetNumberOfLines() != 1 )
625 return false;
626
627 return wxTextCtrlBase::IsEmpty();
628 }
629
630 wxString wxTextCtrl::GetValue() const
631 {
632 // range 0..-1 is special for GetRange() and means to retrieve all text
633 return GetRange(0, -1);
634 }
635
636 wxString wxTextCtrl::GetRange(long from, long to) const
637 {
638 wxString str;
639
640 if ( from >= to && to != -1 )
641 {
642 // nothing to retrieve
643 return str;
644 }
645
646 #if wxUSE_RICHEDIT
647 if ( IsRich() )
648 {
649 int len = GetWindowTextLength(GetHwnd());
650 if ( len > from )
651 {
652 if ( to == -1 )
653 to = len;
654
655 #if !wxUSE_UNICODE
656 // we must use EM_STREAMOUT if we don't want to lose all characters
657 // not representable in the current character set (EM_GETTEXTRANGE
658 // simply replaces them with question marks...)
659 if ( GetRichVersion() > 1 )
660 {
661 // we must have some encoding, otherwise any 8bit chars in the
662 // control are simply *lost* (replaced by '?')
663 wxFontEncoding encoding = wxFONTENCODING_SYSTEM;
664
665 wxFont font = m_defaultStyle.GetFont();
666 if ( !font.Ok() )
667 font = GetFont();
668
669 if ( font.Ok() )
670 {
671 encoding = font.GetEncoding();
672 }
673
674 if ( encoding == wxFONTENCODING_SYSTEM )
675 {
676 encoding = wxLocale::GetSystemEncoding();
677 }
678
679 if ( encoding == wxFONTENCODING_SYSTEM )
680 {
681 encoding = wxFONTENCODING_ISO8859_1;
682 }
683
684 str = StreamOut(encoding);
685
686 if ( !str.empty() )
687 {
688 // we have to manually extract the required part, luckily
689 // this is easy in this case as EOL characters in str are
690 // just LFs because we remove CRs in wxRichEditStreamOut
691 str = str.Mid(from, to - from);
692 }
693 }
694
695 // StreamOut() wasn't used or failed, try to do it in normal way
696 if ( str.empty() )
697 #endif // !wxUSE_UNICODE
698 {
699 // alloc one extra WORD as needed by the control
700 wxStringBuffer tmp(str, ++len);
701 wxChar *p = tmp;
702
703 TEXTRANGE textRange;
704 textRange.chrg.cpMin = from;
705 textRange.chrg.cpMax = to;
706 textRange.lpstrText = p;
707
708 (void)::SendMessage(GetHwnd(), EM_GETTEXTRANGE,
709 0, (LPARAM)&textRange);
710
711 if ( m_verRichEdit > 1 )
712 {
713 // RichEdit 2.0 uses just CR ('\r') for the
714 // newlines which is neither Unix nor Windows
715 // style - convert it to something reasonable
716 for ( ; *p; p++ )
717 {
718 if ( *p == _T('\r') )
719 *p = _T('\n');
720 }
721 }
722 }
723
724 if ( m_verRichEdit == 1 )
725 {
726 // convert to the canonical form - see comment below
727 str = wxTextFile::Translate(str, wxTextFileType_Unix);
728 }
729 }
730 //else: no text at all, leave the string empty
731 }
732 else
733 #endif // wxUSE_RICHEDIT
734 {
735 // retrieve all text
736 str = wxGetWindowText(GetHWND());
737
738 // need only a range?
739 if ( from < to )
740 {
741 str = str.Mid(from, to - from);
742 }
743
744 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
745 // canonical one (same one as above) for consistency with the other kinds
746 // of controls and, more importantly, with the other ports
747 str = wxTextFile::Translate(str, wxTextFileType_Unix);
748 }
749
750 return str;
751 }
752
753 void wxTextCtrl::DoSetValue(const wxString& value, int flags)
754 {
755 // if the text is long enough, it's faster to just set it instead of first
756 // comparing it with the old one (chances are that it will be different
757 // anyhow, this comparison is there to avoid flicker for small single-line
758 // edit controls mostly)
759 if ( (value.length() > 0x400) || (value != GetValue()) )
760 {
761 DoWriteText(value, flags /* doesn't include SelectionOnly here */);
762
763 // mark the control as being not dirty - we changed its text, not the
764 // user
765 DiscardEdits();
766
767 // for compatibility, don't move the cursor when doing SetValue()
768 SetInsertionPoint(0);
769 }
770 else // same text
771 {
772 // still reset the modified flag even if the value didn't really change
773 // because now it comes from the program and not the user (and do it
774 // before generating the event so that the event handler could get the
775 // expected value from IsModified())
776 DiscardEdits();
777
778 // still send an event for consistency
779 if (flags & SetValue_SendEvent)
780 SendUpdateEvent();
781 }
782 }
783
784 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
785
786 // TODO: using memcpy() would improve performance a lot for big amounts of text
787
788 DWORD CALLBACK
789 wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb)
790 {
791 *pcb = 0;
792
793 const wchar_t ** const ppws = (const wchar_t **)dwCookie;
794
795 wchar_t *wbuf = (wchar_t *)buf;
796 const wchar_t *wpc = *ppws;
797 while ( cb && *wpc )
798 {
799 *wbuf++ = *wpc++;
800
801 cb -= sizeof(wchar_t);
802 (*pcb) += sizeof(wchar_t);
803 }
804
805 *ppws = wpc;
806
807 return 0;
808 }
809
810 // helper struct used to pass parameters from wxTextCtrl to wxRichEditStreamOut
811 struct wxStreamOutData
812 {
813 wchar_t *wpc;
814 size_t len;
815 };
816
817 DWORD CALLBACK
818 wxRichEditStreamOut(DWORD_PTR dwCookie, BYTE *buf, LONG cb, LONG *pcb)
819 {
820 *pcb = 0;
821
822 wxStreamOutData *data = (wxStreamOutData *)dwCookie;
823
824 const wchar_t *wbuf = (const wchar_t *)buf;
825 wchar_t *wpc = data->wpc;
826 while ( cb )
827 {
828 wchar_t wch = *wbuf++;
829
830 // turn "\r\n" into "\n" on the fly
831 if ( wch != L'\r' )
832 *wpc++ = wch;
833 else
834 data->len--;
835
836 cb -= sizeof(wchar_t);
837 (*pcb) += sizeof(wchar_t);
838 }
839
840 data->wpc = wpc;
841
842 return 0;
843 }
844
845
846 #if wxUSE_UNICODE_MSLU
847 #define UNUSED_IF_MSLU(param)
848 #else
849 #define UNUSED_IF_MSLU(param) param
850 #endif
851
852 bool
853 wxTextCtrl::StreamIn(const wxString& value,
854 wxFontEncoding UNUSED_IF_MSLU(encoding),
855 bool selectionOnly)
856 {
857 #if wxUSE_UNICODE_MSLU
858 const wchar_t *wpc = value.c_str();
859 #else // !wxUSE_UNICODE_MSLU
860 wxCSConv conv(encoding);
861
862 const size_t len = conv.MB2WC(NULL, value, value.length());
863
864 #if wxUSE_WCHAR_T
865 wxWCharBuffer wchBuf(len);
866 wchar_t *wpc = wchBuf.data();
867 #else
868 wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t));
869 wchar_t *wpc = wchBuf;
870 #endif
871
872 conv.MB2WC(wpc, value, value.length());
873 #endif // wxUSE_UNICODE_MSLU
874
875 // finally, stream it in the control
876 EDITSTREAM eds;
877 wxZeroMemory(eds);
878 eds.dwCookie = (DWORD)&wpc;
879 // the cast below is needed for broken (very) old mingw32 headers
880 eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn;
881
882 // same problem as in DoWriteText(): we can get multiple events here
883 UpdatesCountFilter ucf(m_updatesCount);
884
885 ::SendMessage(GetHwnd(), EM_STREAMIN,
886 SF_TEXT |
887 SF_UNICODE |
888 (selectionOnly ? SFF_SELECTION : 0),
889 (LPARAM)&eds);
890
891 // It's okay for EN_UPDATE to not be sent if the selection is empty and
892 // the text is empty, otherwise warn the programmer about it.
893 wxASSERT_MSG( ucf.GotUpdate() || ( !HasSelection() && value.empty() ),
894 _T("EM_STREAMIN didn't send EN_UPDATE?") );
895
896 if ( eds.dwError )
897 {
898 wxLogLastError(_T("EM_STREAMIN"));
899 }
900
901 #if !wxUSE_WCHAR_T
902 free(wchBuf);
903 #endif // !wxUSE_WCHAR_T
904
905 return true;
906 }
907
908 #if !wxUSE_UNICODE_MSLU
909
910 wxString
911 wxTextCtrl::StreamOut(wxFontEncoding encoding, bool selectionOnly) const
912 {
913 wxString out;
914
915 const int len = GetWindowTextLength(GetHwnd());
916
917 #if wxUSE_WCHAR_T
918 wxWCharBuffer wchBuf(len);
919 wchar_t *wpc = wchBuf.data();
920 #else
921 wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t));
922 wchar_t *wpc = wchBuf;
923 #endif
924
925 wxStreamOutData data;
926 data.wpc = wpc;
927 data.len = len;
928
929 EDITSTREAM eds;
930 wxZeroMemory(eds);
931 eds.dwCookie = (DWORD)&data;
932 eds.pfnCallback = wxRichEditStreamOut;
933
934 ::SendMessage
935 (
936 GetHwnd(),
937 EM_STREAMOUT,
938 SF_TEXT | SF_UNICODE | (selectionOnly ? SFF_SELECTION : 0),
939 (LPARAM)&eds
940 );
941
942 if ( eds.dwError )
943 {
944 wxLogLastError(_T("EM_STREAMOUT"));
945 }
946 else // streamed out ok
947 {
948 // NUL-terminate the string because its length could have been
949 // decreased by wxRichEditStreamOut
950 *(wchBuf.data() + data.len) = L'\0';
951
952 // now convert to the given encoding (this is a possibly lossful
953 // conversion but what else can we do)
954 wxCSConv conv(encoding);
955 size_t lenNeeded = conv.WC2MB(NULL, wchBuf, 0);
956 if ( lenNeeded++ )
957 {
958 conv.WC2MB(wxStringBuffer(out, lenNeeded), wchBuf, lenNeeded);
959 }
960 }
961
962 #if !wxUSE_WCHAR_T
963 free(wchBuf);
964 #endif // !wxUSE_WCHAR_T
965
966 return out;
967 }
968
969 #endif // !wxUSE_UNICODE_MSLU
970
971 #endif // wxUSE_RICHEDIT
972
973 void wxTextCtrl::WriteText(const wxString& value)
974 {
975 DoWriteText(value);
976 }
977
978 void wxTextCtrl::DoWriteText(const wxString& value, int flags)
979 {
980 bool selectionOnly = (flags & SetValue_SelectionOnly) != 0;
981 wxString valueDos;
982 if ( m_windowStyle & wxTE_MULTILINE )
983 valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
984 else
985 valueDos = value;
986
987 #if wxUSE_RICHEDIT
988 // there are several complications with the rich edit controls here
989 bool done = false;
990 if ( IsRich() )
991 {
992 // first, ensure that the new text will be in the default style
993 if ( !m_defaultStyle.IsDefault() )
994 {
995 long start, end;
996 GetSelection(&start, &end);
997 SetStyle(start, end, m_defaultStyle);
998 }
999
1000 #if wxUSE_UNICODE_MSLU
1001 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
1002 // but EM_STREAMIN works
1003 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
1004 {
1005 done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly);
1006 }
1007 #endif // wxUSE_UNICODE_MSLU
1008
1009 #if !wxUSE_UNICODE
1010 // next check if the text we're inserting must be shown in a non
1011 // default charset -- this only works for RichEdit > 1.0
1012 if ( GetRichVersion() > 1 )
1013 {
1014 wxFont font = m_defaultStyle.GetFont();
1015 if ( !font.Ok() )
1016 font = GetFont();
1017
1018 if ( font.Ok() )
1019 {
1020 wxFontEncoding encoding = font.GetEncoding();
1021 if ( encoding != wxFONTENCODING_SYSTEM )
1022 {
1023 // we have to use EM_STREAMIN to force richedit control 2.0+
1024 // to show any text in the non default charset -- otherwise
1025 // it thinks it knows better than we do and always shows it
1026 // in the default one
1027 done = StreamIn(valueDos, encoding, selectionOnly);
1028 }
1029 }
1030 }
1031 #endif // !wxUSE_UNICODE
1032 }
1033
1034 if ( !done )
1035 #endif // wxUSE_RICHEDIT
1036 {
1037 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
1038 // call (this happens for plain EDITs with EM_REPLACESEL and under some
1039 // -- undetermined -- conditions with rich edit) and sometimes we don't
1040 // get any events at all (plain EDIT with WM_SETTEXT), so ensure that
1041 // we generate exactly one of them by ignoring all but the first one in
1042 // SendUpdateEvent() and generating one ourselves if we hadn't got any
1043 // notifications from Windows
1044 if ( !(flags & SetValue_SendEvent) )
1045 m_updatesCount = -2; // suppress any update event
1046
1047 UpdatesCountFilter ucf(m_updatesCount);
1048
1049 ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT,
1050 // EM_REPLACESEL takes 1 to indicate the operation should be redoable
1051 selectionOnly ? 1 : 0, (LPARAM)valueDos.c_str());
1052
1053 if ( !ucf.GotUpdate() && (flags & SetValue_SendEvent) )
1054 {
1055 SendUpdateEvent();
1056 }
1057 }
1058 }
1059
1060 void wxTextCtrl::AppendText(const wxString& text)
1061 {
1062 SetInsertionPointEnd();
1063
1064 WriteText(text);
1065
1066 #if wxUSE_RICHEDIT
1067 // don't do this if we're frozen, saves some time
1068 if ( !IsFrozen() && IsMultiLine() && GetRichVersion() > 1 )
1069 {
1070 // setting the caret to the end and showing it simply doesn't work for
1071 // RichEdit 2.0 -- force it to still do what we want
1072 ::SendMessage(GetHwnd(), EM_LINESCROLL, 0, GetNumberOfLines());
1073 }
1074 #endif // wxUSE_RICHEDIT
1075 }
1076
1077 void wxTextCtrl::Clear()
1078 {
1079 ::SetWindowText(GetHwnd(), wxEmptyString);
1080
1081 #if wxUSE_RICHEDIT
1082 if ( !IsRich() )
1083 #endif // wxUSE_RICHEDIT
1084 {
1085 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
1086 // but the normal ones don't -- make Clear() behaviour consistent by
1087 // always sending this event
1088
1089 // Windows already sends an update event for single-line
1090 // controls.
1091 if ( m_windowStyle & wxTE_MULTILINE )
1092 SendUpdateEvent();
1093 }
1094 }
1095
1096 #ifdef __WIN32__
1097
1098 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event)
1099 {
1100 SetFocus();
1101
1102 size_t lenOld = GetValue().length();
1103
1104 wxUint32 code = event.GetRawKeyCode();
1105 ::keybd_event((BYTE)code, 0, 0 /* key press */, 0);
1106 ::keybd_event((BYTE)code, 0, KEYEVENTF_KEYUP, 0);
1107
1108 // assume that any alphanumeric key changes the total number of characters
1109 // in the control - this should work in 99% of cases
1110 return GetValue().length() != lenOld;
1111 }
1112
1113 #endif // __WIN32__
1114
1115 // ----------------------------------------------------------------------------
1116 // Clipboard operations
1117 // ----------------------------------------------------------------------------
1118
1119 void wxTextCtrl::Copy()
1120 {
1121 if (CanCopy())
1122 {
1123 ::SendMessage(GetHwnd(), WM_COPY, 0, 0L);
1124 }
1125 }
1126
1127 void wxTextCtrl::Cut()
1128 {
1129 if (CanCut())
1130 {
1131 ::SendMessage(GetHwnd(), WM_CUT, 0, 0L);
1132 }
1133 }
1134
1135 void wxTextCtrl::Paste()
1136 {
1137 if (CanPaste())
1138 {
1139 ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
1140 }
1141 }
1142
1143 bool wxTextCtrl::HasSelection() const
1144 {
1145 long from, to;
1146 GetSelection(&from, &to);
1147 return from != to;
1148 }
1149
1150 bool wxTextCtrl::CanCopy() const
1151 {
1152 // Can copy if there's a selection
1153 return HasSelection();
1154 }
1155
1156 bool wxTextCtrl::CanCut() const
1157 {
1158 return CanCopy() && IsEditable();
1159 }
1160
1161 bool wxTextCtrl::CanPaste() const
1162 {
1163 if ( !IsEditable() )
1164 return false;
1165
1166 #if wxUSE_RICHEDIT
1167 if ( IsRich() )
1168 {
1169 UINT cf = 0; // 0 == any format
1170
1171 return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0;
1172 }
1173 #endif // wxUSE_RICHEDIT
1174
1175 // Standard edit control: check for straight text on clipboard
1176 if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
1177 return false;
1178
1179 bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0;
1180 ::CloseClipboard();
1181
1182 return isTextAvailable;
1183 }
1184
1185 // ----------------------------------------------------------------------------
1186 // Accessors
1187 // ----------------------------------------------------------------------------
1188
1189 void wxTextCtrl::SetEditable(bool editable)
1190 {
1191 HWND hWnd = GetHwnd();
1192 ::SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
1193 }
1194
1195 void wxTextCtrl::SetInsertionPoint(long pos)
1196 {
1197 DoSetSelection(pos, pos);
1198 }
1199
1200 void wxTextCtrl::SetInsertionPointEnd()
1201 {
1202 // we must not do anything if the caret is already there because calling
1203 // SetInsertionPoint() thaws the controls if Freeze() had been called even
1204 // if it doesn't actually move the caret anywhere and so the simple fact of
1205 // doing it results in horrible flicker when appending big amounts of text
1206 // to the control in a few chunks (see DoAddText() test in the text sample)
1207 const wxTextPos lastPosition = GetLastPosition();
1208 if ( GetInsertionPoint() == lastPosition )
1209 {
1210 return;
1211 }
1212
1213 long pos;
1214
1215 #if wxUSE_RICHEDIT
1216 if ( m_verRichEdit == 1 )
1217 {
1218 // we don't have to waste time calling GetLastPosition() in this case
1219 pos = -1;
1220 }
1221 else // !RichEdit 1.0
1222 #endif // wxUSE_RICHEDIT
1223 {
1224 pos = lastPosition;
1225 }
1226
1227 SetInsertionPoint(pos);
1228 }
1229
1230 long wxTextCtrl::GetInsertionPoint() const
1231 {
1232 #if wxUSE_RICHEDIT
1233 if ( IsRich() )
1234 {
1235 CHARRANGE range;
1236 range.cpMin = 0;
1237 range.cpMax = 0;
1238 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
1239 return range.cpMin;
1240 }
1241 #endif // wxUSE_RICHEDIT
1242
1243 DWORD Pos = (DWORD)::SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
1244 return Pos & 0xFFFF;
1245 }
1246
1247 wxTextPos wxTextCtrl::GetLastPosition() const
1248 {
1249 int numLines = GetNumberOfLines();
1250 long posStartLastLine = XYToPosition(0, numLines - 1);
1251
1252 long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine);
1253
1254 return posStartLastLine + lenLastLine;
1255 }
1256
1257 // If the return values from and to are the same, there is no
1258 // selection.
1259 void wxTextCtrl::GetSelection(long* from, long* to) const
1260 {
1261 #if wxUSE_RICHEDIT
1262 if ( IsRich() )
1263 {
1264 CHARRANGE charRange;
1265 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange);
1266
1267 *from = charRange.cpMin;
1268 *to = charRange.cpMax;
1269 }
1270 else
1271 #endif // !wxUSE_RICHEDIT
1272 {
1273 DWORD dwStart, dwEnd;
1274 ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
1275
1276 *from = dwStart;
1277 *to = dwEnd;
1278 }
1279 }
1280
1281 bool wxTextCtrl::IsEditable() const
1282 {
1283 // strangely enough, we may be called before the control is created: our
1284 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
1285 // us
1286 if ( !m_hWnd )
1287 return true;
1288
1289 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
1290
1291 return (style & ES_READONLY) == 0;
1292 }
1293
1294 // ----------------------------------------------------------------------------
1295 // selection
1296 // ----------------------------------------------------------------------------
1297
1298 void wxTextCtrl::SetSelection(long from, long to)
1299 {
1300 // if from and to are both -1, it means (in wxWidgets) that all text should
1301 // be selected - translate into Windows convention
1302 if ( (from == -1) && (to == -1) )
1303 {
1304 from = 0;
1305 to = -1;
1306 }
1307
1308 DoSetSelection(from, to);
1309 }
1310
1311 void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret)
1312 {
1313 HWND hWnd = GetHwnd();
1314
1315 #if wxUSE_RICHEDIT
1316 if ( IsRich() )
1317 {
1318 CHARRANGE range;
1319 range.cpMin = from;
1320 range.cpMax = to;
1321 ::SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
1322 }
1323 else
1324 #endif // wxUSE_RICHEDIT
1325 {
1326 ::SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to);
1327 }
1328
1329 if ( scrollCaret && !IsFrozen() )
1330 {
1331 #if wxUSE_RICHEDIT
1332 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1333 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1334 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1335 // option is set (but it does work ok in richedit 1.0 mode...)
1336 //
1337 // so to make it work we either need to give focus to it here which
1338 // will probably create many problems (dummy focus events; window
1339 // containing the text control being brought to foreground
1340 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1341 // create other problems too -- and in fact it does because if we turn
1342 // on/off this style while appending the text to the control, the
1343 // vertical scrollbar never appears in it even if we append tons of
1344 // text and to work around this the only solution I found was to use
1345 // ES_DISABLENOSCROLL
1346 //
1347 // this is very ugly but I don't see any other way to make this work
1348 long style = 0;
1349 if ( GetRichVersion() > 1 )
1350 {
1351 if ( !HasFlag(wxTE_NOHIDESEL) )
1352 {
1353 // setting ECO_NOHIDESEL also sets WS_VISIBLE and possibly
1354 // others, remember the style so we can reset it later if needed
1355 style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
1356 ::SendMessage(GetHwnd(), EM_SETOPTIONS,
1357 ECOOP_OR, ECO_NOHIDESEL);
1358 }
1359 //else: everything is already ok
1360 }
1361 #endif // wxUSE_RICHEDIT
1362
1363 ::SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
1364
1365 #if wxUSE_RICHEDIT
1366 // restore ECO_NOHIDESEL if we changed it
1367 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL) )
1368 {
1369 ::SendMessage(GetHwnd(), EM_SETOPTIONS,
1370 ECOOP_AND, ~ECO_NOHIDESEL);
1371 if ( style != ::GetWindowLong(GetHwnd(), GWL_STYLE) )
1372 ::SetWindowLong(GetHwnd(), GWL_STYLE, style);
1373 }
1374 #endif // wxUSE_RICHEDIT
1375 }
1376 }
1377
1378 // ----------------------------------------------------------------------------
1379 // Working with files
1380 // ----------------------------------------------------------------------------
1381
1382 bool wxTextCtrl::DoLoadFile(const wxString& file, int fileType)
1383 {
1384 if ( wxTextCtrlBase::DoLoadFile(file, fileType) )
1385 {
1386 // update the size limit if needed
1387 AdjustSpaceLimit();
1388
1389 return true;
1390 }
1391
1392 return false;
1393 }
1394
1395 // ----------------------------------------------------------------------------
1396 // Editing
1397 // ----------------------------------------------------------------------------
1398
1399 void wxTextCtrl::Replace(long from, long to, const wxString& value)
1400 {
1401 // Set selection and remove it
1402 DoSetSelection(from, to, false /* don't scroll caret into view */);
1403
1404 DoWriteText(value);
1405 }
1406
1407 void wxTextCtrl::Remove(long from, long to)
1408 {
1409 Replace(from, to, wxEmptyString);
1410 }
1411
1412 bool wxTextCtrl::IsModified() const
1413 {
1414 return ::SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0;
1415 }
1416
1417 void wxTextCtrl::MarkDirty()
1418 {
1419 ::SendMessage(GetHwnd(), EM_SETMODIFY, TRUE, 0L);
1420 }
1421
1422 void wxTextCtrl::DiscardEdits()
1423 {
1424 ::SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
1425 }
1426
1427 int wxTextCtrl::GetNumberOfLines() const
1428 {
1429 return (int)::SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
1430 }
1431
1432 // ----------------------------------------------------------------------------
1433 // Positions <-> coords
1434 // ----------------------------------------------------------------------------
1435
1436 long wxTextCtrl::XYToPosition(long x, long y) const
1437 {
1438 // This gets the char index for the _beginning_ of this line
1439 long charIndex = ::SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
1440
1441 return charIndex + x;
1442 }
1443
1444 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
1445 {
1446 HWND hWnd = GetHwnd();
1447
1448 // This gets the line number containing the character
1449 long lineNo;
1450 #if wxUSE_RICHEDIT
1451 if ( IsRich() )
1452 {
1453 lineNo = ::SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
1454 }
1455 else
1456 #endif // wxUSE_RICHEDIT
1457 {
1458 lineNo = ::SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
1459 }
1460
1461 if ( lineNo == -1 )
1462 {
1463 // no such line
1464 return false;
1465 }
1466
1467 // This gets the char index for the _beginning_ of this line
1468 long charIndex = ::SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
1469 if ( charIndex == -1 )
1470 {
1471 return false;
1472 }
1473
1474 // The X position must therefore be the different between pos and charIndex
1475 if ( x )
1476 *x = pos - charIndex;
1477 if ( y )
1478 *y = lineNo;
1479
1480 return true;
1481 }
1482
1483 wxTextCtrlHitTestResult
1484 wxTextCtrl::HitTest(const wxPoint& pt, long *posOut) const
1485 {
1486 // first get the position from Windows
1487 LPARAM lParam;
1488
1489 #if wxUSE_RICHEDIT
1490 POINTL ptl;
1491 if ( IsRich() )
1492 {
1493 // for rich edit controls the position is passed iva the struct fields
1494 ptl.x = pt.x;
1495 ptl.y = pt.y;
1496 lParam = (LPARAM)&ptl;
1497 }
1498 else
1499 #endif // wxUSE_RICHEDIT
1500 {
1501 // for the plain ones, we are limited to 16 bit positions which are
1502 // combined in a single 32 bit value
1503 lParam = MAKELPARAM(pt.x, pt.y);
1504 }
1505
1506 LRESULT pos = ::SendMessage(GetHwnd(), EM_CHARFROMPOS, 0, lParam);
1507
1508 if ( pos == -1 )
1509 {
1510 // this seems to indicate an error...
1511 return wxTE_HT_UNKNOWN;
1512 }
1513
1514 #if wxUSE_RICHEDIT
1515 if ( !IsRich() )
1516 #endif // wxUSE_RICHEDIT
1517 {
1518 // for plain EDIT controls the higher word contains something else
1519 pos = LOWORD(pos);
1520 }
1521
1522
1523 // next determine where it is relatively to our point: EM_CHARFROMPOS
1524 // always returns the closest character but we need to be more precise, so
1525 // double check that we really are where it pretends
1526 POINTL ptReal;
1527
1528 #if wxUSE_RICHEDIT
1529 // FIXME: we need to distinguish between richedit 2 and 3 here somehow but
1530 // we don't know how to do it
1531 if ( IsRich() )
1532 {
1533 ::SendMessage(GetHwnd(), EM_POSFROMCHAR, (WPARAM)&ptReal, pos);
1534 }
1535 else
1536 #endif // wxUSE_RICHEDIT
1537 {
1538 LRESULT lRc = ::SendMessage(GetHwnd(), EM_POSFROMCHAR, pos, 0);
1539
1540 if ( lRc == -1 )
1541 {
1542 // this is apparently returned when pos corresponds to the last
1543 // position
1544 ptReal.x =
1545 ptReal.y = 0;
1546 }
1547 else
1548 {
1549 ptReal.x = LOWORD(lRc);
1550 ptReal.y = HIWORD(lRc);
1551 }
1552 }
1553
1554 wxTextCtrlHitTestResult rc;
1555
1556 if ( pt.y > ptReal.y + GetCharHeight() )
1557 rc = wxTE_HT_BELOW;
1558 else if ( pt.x > ptReal.x + GetCharWidth() )
1559 rc = wxTE_HT_BEYOND;
1560 else
1561 rc = wxTE_HT_ON_TEXT;
1562
1563 if ( posOut )
1564 *posOut = pos;
1565
1566 return rc;
1567 }
1568
1569 // ----------------------------------------------------------------------------
1570 //
1571 // ----------------------------------------------------------------------------
1572
1573 void wxTextCtrl::ShowPosition(long pos)
1574 {
1575 HWND hWnd = GetHwnd();
1576
1577 // To scroll to a position, we pass the number of lines and characters
1578 // to scroll *by*. This means that we need to:
1579 // (1) Find the line position of the current line.
1580 // (2) Find the line position of pos.
1581 // (3) Scroll by (pos - current).
1582 // For now, ignore the horizontal scrolling.
1583
1584 // Is this where scrolling is relative to - the line containing the caret?
1585 // Or is the first visible line??? Try first visible line.
1586 // int currentLineLineNo1 = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1587
1588 int currentLineLineNo = (int)::SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
1589
1590 int specifiedLineLineNo = (int)::SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
1591
1592 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
1593
1594 if (linesToScroll != 0)
1595 (void)::SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
1596 }
1597
1598 long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const
1599 {
1600 return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0);
1601 }
1602
1603 int wxTextCtrl::GetLineLength(long lineNo) const
1604 {
1605 long pos = XYToPosition(0, lineNo);
1606
1607 return GetLengthOfLineContainingPos(pos);
1608 }
1609
1610 wxString wxTextCtrl::GetLineText(long lineNo) const
1611 {
1612 size_t len = (size_t)GetLineLength(lineNo) + 1;
1613
1614 // there must be at least enough place for the length WORD in the
1615 // buffer
1616 len += sizeof(WORD);
1617
1618 wxString str;
1619 {
1620 wxStringBufferLength tmp(str, len);
1621 wxChar *buf = tmp;
1622
1623 *(WORD *)buf = (WORD)len;
1624 len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
1625
1626 #if wxUSE_RICHEDIT
1627 if ( IsRich() )
1628 {
1629 // remove the '\r' returned by the rich edit control, the user code
1630 // should never see it
1631 if ( buf[len - 2] == _T('\r') && buf[len - 1] == _T('\n') )
1632 {
1633 buf[len - 2] = _T('\n');
1634 len--;
1635 }
1636 }
1637 #endif // wxUSE_RICHEDIT
1638
1639 // remove the '\n' at the end, if any (this is how this function is
1640 // supposed to work according to the docs)
1641 if ( buf[len - 1] == _T('\n') )
1642 {
1643 len--;
1644 }
1645
1646 buf[len] = 0;
1647 tmp.SetLength(len);
1648 }
1649
1650 return str;
1651 }
1652
1653 void wxTextCtrl::SetMaxLength(unsigned long len)
1654 {
1655 #if wxUSE_RICHEDIT
1656 if ( IsRich() )
1657 {
1658 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, len ? len : 0x7fffffff);
1659 }
1660 else
1661 #endif // wxUSE_RICHEDIT
1662 {
1663 if ( len >= 0xffff )
1664 {
1665 // this will set it to a platform-dependent maximum (much more
1666 // than 64Kb under NT)
1667 len = 0;
1668 }
1669
1670 ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0);
1671 }
1672 }
1673
1674 // ----------------------------------------------------------------------------
1675 // Undo/redo
1676 // ----------------------------------------------------------------------------
1677
1678 void wxTextCtrl::Undo()
1679 {
1680 if (CanUndo())
1681 {
1682 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
1683 }
1684 }
1685
1686 void wxTextCtrl::Redo()
1687 {
1688 if (CanRedo())
1689 {
1690 #if wxUSE_RICHEDIT
1691 if (GetRichVersion() > 1)
1692 ::SendMessage(GetHwnd(), EM_REDO, 0, 0);
1693 else
1694 #endif
1695 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1696 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
1697 }
1698 }
1699
1700 bool wxTextCtrl::CanUndo() const
1701 {
1702 return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
1703 }
1704
1705 bool wxTextCtrl::CanRedo() const
1706 {
1707 #if wxUSE_RICHEDIT
1708 if (GetRichVersion() > 1)
1709 return ::SendMessage(GetHwnd(), EM_CANREDO, 0, 0) != 0;
1710 else
1711 #endif
1712 return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
1713 }
1714
1715 // ----------------------------------------------------------------------------
1716 // caret handling (Windows only)
1717 // ----------------------------------------------------------------------------
1718
1719 bool wxTextCtrl::ShowNativeCaret(bool show)
1720 {
1721 if ( show != m_isNativeCaretShown )
1722 {
1723 if ( !(show ? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) )
1724 {
1725 // not an error, may simply indicate that it's not shown/hidden
1726 // yet (i.e. it had been hidden/showh 2 times before)
1727 return false;
1728 }
1729
1730 m_isNativeCaretShown = show;
1731 }
1732
1733 return true;
1734 }
1735
1736 // ----------------------------------------------------------------------------
1737 // implemenation details
1738 // ----------------------------------------------------------------------------
1739
1740 void wxTextCtrl::Command(wxCommandEvent & event)
1741 {
1742 SetValue(event.GetString());
1743 ProcessCommand (event);
1744 }
1745
1746 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
1747 {
1748 // By default, load the first file into the text window.
1749 if (event.GetNumberOfFiles() > 0)
1750 {
1751 LoadFile(event.GetFiles()[0]);
1752 }
1753 }
1754
1755 // ----------------------------------------------------------------------------
1756 // kbd input processing
1757 // ----------------------------------------------------------------------------
1758
1759 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
1760 {
1761 // check for our special keys here: if we don't do it and the parent frame
1762 // uses them as accelerators, they wouldn't work at all, so we disable
1763 // usual preprocessing for them
1764 if ( msg->message == WM_KEYDOWN )
1765 {
1766 const WPARAM vkey = msg->wParam;
1767 if ( HIWORD(msg->lParam) & KF_ALTDOWN )
1768 {
1769 // Alt-Backspace is accelerator for "Undo"
1770 if ( vkey == VK_BACK )
1771 return false;
1772 }
1773 else // no Alt
1774 {
1775 // we want to process some Ctrl-foo and Shift-bar but no key
1776 // combinations without either Ctrl or Shift nor with both of them
1777 // pressed
1778 const int ctrl = wxIsCtrlDown(),
1779 shift = wxIsShiftDown();
1780 switch ( ctrl + shift )
1781 {
1782 default:
1783 wxFAIL_MSG( _T("how many modifiers have we got?") );
1784 // fall through
1785
1786 case 0:
1787 if ( IsMultiLine() && vkey == VK_RETURN )
1788 return false;
1789 // fall through
1790 case 2:
1791 break;
1792
1793 case 1:
1794 // either Ctrl or Shift pressed
1795 if ( ctrl )
1796 {
1797 switch ( vkey )
1798 {
1799 case 'C':
1800 case 'V':
1801 case 'X':
1802 case VK_INSERT:
1803 case VK_DELETE:
1804 case VK_HOME:
1805 case VK_END:
1806 return false;
1807 }
1808 }
1809 else // Shift is pressed
1810 {
1811 if ( vkey == VK_INSERT || vkey == VK_DELETE )
1812 return false;
1813 }
1814 }
1815 }
1816 }
1817
1818 return wxControl::MSWShouldPreProcessMessage(msg);
1819 }
1820
1821 void wxTextCtrl::OnChar(wxKeyEvent& event)
1822 {
1823 switch ( event.GetKeyCode() )
1824 {
1825 case WXK_RETURN:
1826 {
1827 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1828 InitCommandEvent(event);
1829 event.SetString(GetValue());
1830 if ( GetEventHandler()->ProcessEvent(event) )
1831 if ( !HasFlag(wxTE_MULTILINE) )
1832 return;
1833 //else: multiline controls need Enter for themselves
1834 }
1835 break;
1836
1837 case WXK_TAB:
1838 // ok, so this is getting absolutely ridiculous but I don't see
1839 // any other way to fix this bug: when a multiline text control is
1840 // inside a wxFrame, we need to generate the navigation event as
1841 // otherwise nothing happens at all, but when the same control is
1842 // created inside a dialog, IsDialogMessage() *does* switch focus
1843 // all by itself and so if we do it here as well, it is advanced
1844 // twice and goes to the next control... to prevent this from
1845 // happening we're doing this ugly check, the logic being that if
1846 // we don't have focus then it had been already changed to the next
1847 // control
1848 //
1849 // the right thing to do would, of course, be to understand what
1850 // the hell is IsDialogMessage() doing but this is beyond my feeble
1851 // forces at the moment unfortunately
1852 if ( !(m_windowStyle & wxTE_PROCESS_TAB))
1853 {
1854 if ( FindFocus() == this )
1855 {
1856 int flags = 0;
1857 if (!event.ShiftDown())
1858 flags |= wxNavigationKeyEvent::IsForward ;
1859 if (event.ControlDown())
1860 flags |= wxNavigationKeyEvent::WinChange ;
1861 if (Navigate(flags))
1862 return;
1863 }
1864 }
1865 else
1866 {
1867 // Insert tab since calling the default Windows handler
1868 // doesn't seem to do it
1869 WriteText(wxT("\t"));
1870 return;
1871 }
1872 break;
1873 }
1874
1875 // no, we didn't process it
1876 event.Skip();
1877 }
1878
1879 WXLRESULT wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1880 {
1881 WXLRESULT lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
1882
1883 switch ( nMsg )
1884 {
1885 case WM_GETDLGCODE:
1886 {
1887 // we always want the chars and the arrows: the arrows for
1888 // navigation and the chars because we want Ctrl-C to work even
1889 // in a read only control
1890 long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
1891
1892 if ( IsEditable() )
1893 {
1894 // we may have several different cases:
1895 // 1. normal: both TAB and ENTER are used for navigation
1896 // 2. ctrl wants TAB for itself: ENTER is used to pass to
1897 // the next control in the dialog
1898 // 3. ctrl wants ENTER for itself: TAB is used for dialog
1899 // navigation
1900 // 4. ctrl wants both TAB and ENTER: Ctrl-ENTER is used to
1901 // go to the next control (we need some way to do it)
1902
1903 // multiline controls should always get ENTER for themselves
1904 if ( HasFlag(wxTE_PROCESS_ENTER) || HasFlag(wxTE_MULTILINE) )
1905 lDlgCode |= DLGC_WANTMESSAGE;
1906
1907 if ( HasFlag(wxTE_PROCESS_TAB) )
1908 lDlgCode |= DLGC_WANTTAB;
1909
1910 lRc |= lDlgCode;
1911 }
1912 else // !editable
1913 {
1914 // NB: use "=", not "|=" as the base class version returns
1915 // the same flags is this state as usual (i.e.
1916 // including DLGC_WANTMESSAGE). This is strange (how
1917 // does it work in the native Win32 apps?) but for now
1918 // live with it.
1919 lRc = lDlgCode;
1920 }
1921 }
1922 break;
1923
1924 case WM_CUT:
1925 case WM_COPY:
1926 case WM_PASTE:
1927 if ( HandleClipboardEvent(nMsg) )
1928 lRc = 0;
1929 break;
1930 }
1931
1932 return lRc;
1933 }
1934
1935 // ----------------------------------------------------------------------------
1936 // text control event processing
1937 // ----------------------------------------------------------------------------
1938
1939 bool wxTextCtrl::SendUpdateEvent()
1940 {
1941 switch ( m_updatesCount )
1942 {
1943 case 0:
1944 // remember that we've got an update
1945 m_updatesCount++;
1946 break;
1947
1948 case 1:
1949 // we had already sent one event since the last control modification
1950 return false;
1951
1952 default:
1953 wxFAIL_MSG( _T("unexpected wxTextCtrl::m_updatesCount value") );
1954 // fall through
1955
1956 case -1:
1957 // we hadn't updated the control ourselves, this event comes from
1958 // the user, don't need to ignore it nor update the count
1959 break;
1960
1961 case -2:
1962 // the control was updated programmatically and we do NOT want to
1963 // send events
1964 return false;
1965 }
1966
1967 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
1968 InitCommandEvent(event);
1969
1970 return ProcessCommand(event);
1971 }
1972
1973 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
1974 {
1975 switch ( param )
1976 {
1977 case EN_SETFOCUS:
1978 case EN_KILLFOCUS:
1979 {
1980 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1981 : wxEVT_SET_FOCUS,
1982 m_windowId);
1983 event.SetEventObject(this);
1984 GetEventHandler()->ProcessEvent(event);
1985 }
1986 break;
1987
1988 case EN_CHANGE:
1989 SendUpdateEvent();
1990 break;
1991
1992 case EN_MAXTEXT:
1993 // the text size limit has been hit -- try to increase it
1994 if ( !AdjustSpaceLimit() )
1995 {
1996 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId);
1997 InitCommandEvent(event);
1998 event.SetString(GetValue());
1999 ProcessCommand(event);
2000 }
2001 break;
2002
2003 // the other edit notification messages are not processed
2004 default:
2005 return false;
2006 }
2007
2008 // processed
2009 return true;
2010 }
2011
2012 WXHBRUSH wxTextCtrl::MSWControlColor(WXHDC hDC, WXHWND hWnd)
2013 {
2014 if ( !IsEnabled() && !HasFlag(wxTE_MULTILINE) )
2015 return MSWControlColorDisabled(hDC);
2016
2017 return wxTextCtrlBase::MSWControlColor(hDC, hWnd);
2018 }
2019
2020 bool wxTextCtrl::HasSpaceLimit(unsigned int *len) const
2021 {
2022 // HACK: we try to automatically extend the limit for the amount of text
2023 // to allow (interactively) entering more than 64Kb of text under
2024 // Win9x but we shouldn't reset the text limit which was previously
2025 // set explicitly with SetMaxLength()
2026 //
2027 // Unfortunately there is no EM_GETLIMITTEXTSETBYUSER and so we don't
2028 // know the limit we set (if any). We could solve this by storing the
2029 // limit we set in wxTextCtrl but to save space we prefer to simply
2030 // test here the actual limit value: we consider that SetMaxLength()
2031 // can only be called for small values while EN_MAXTEXT is only sent
2032 // for large values (in practice the default limit seems to be 30000
2033 // but make it smaller just to be on the safe side)
2034 *len = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
2035 return *len < 10001;
2036
2037 }
2038
2039 bool wxTextCtrl::AdjustSpaceLimit()
2040 {
2041 unsigned int limit;
2042 if ( HasSpaceLimit(&limit) )
2043 return false;
2044
2045 unsigned int len = ::GetWindowTextLength(GetHwnd());
2046 if ( len >= limit )
2047 {
2048 // increment in 32Kb chunks
2049 SetMaxLength(len + 0x8000);
2050 }
2051
2052 // we changed the limit
2053 return true;
2054 }
2055
2056 bool wxTextCtrl::AcceptsFocus() const
2057 {
2058 // we don't want focus if we can't be edited unless we're a multiline
2059 // control because then it might be still nice to get focus from keyboard
2060 // to be able to scroll it without mouse
2061 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
2062 }
2063
2064 wxSize wxTextCtrl::DoGetBestSize() const
2065 {
2066 int cx, cy;
2067 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
2068
2069 int wText = DEFAULT_ITEM_WIDTH;
2070
2071 int hText = cy;
2072 if ( m_windowStyle & wxTE_MULTILINE )
2073 {
2074 hText *= wxMax(wxMin(GetNumberOfLines(), 10), 2);
2075 }
2076 //else: for single line control everything is ok
2077
2078 // we have to add the adjustments for the control height only once, not
2079 // once per line, so do it after multiplication above
2080 hText += EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) - cy;
2081
2082 return wxSize(wText, hText);
2083 }
2084
2085 // ----------------------------------------------------------------------------
2086 // standard handlers for standard edit menu events
2087 // ----------------------------------------------------------------------------
2088
2089 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2090 {
2091 Cut();
2092 }
2093
2094 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2095 {
2096 Copy();
2097 }
2098
2099 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2100 {
2101 Paste();
2102 }
2103
2104 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2105 {
2106 Undo();
2107 }
2108
2109 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2110 {
2111 Redo();
2112 }
2113
2114 void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
2115 {
2116 long from, to;
2117 GetSelection(& from, & to);
2118 if (from != -1 && to != -1)
2119 Remove(from, to);
2120 }
2121
2122 void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2123 {
2124 SetSelection(-1, -1);
2125 }
2126
2127 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2128 {
2129 event.Enable( CanCut() );
2130 }
2131
2132 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2133 {
2134 event.Enable( CanCopy() );
2135 }
2136
2137 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2138 {
2139 event.Enable( CanPaste() );
2140 }
2141
2142 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2143 {
2144 event.Enable( CanUndo() );
2145 }
2146
2147 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2148 {
2149 event.Enable( CanRedo() );
2150 }
2151
2152 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
2153 {
2154 long from, to;
2155 GetSelection(& from, & to);
2156 event.Enable(from != -1 && to != -1 && from != to && IsEditable()) ;
2157 }
2158
2159 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2160 {
2161 event.Enable(GetLastPosition() > 0);
2162 }
2163
2164 void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event)
2165 {
2166 #if wxUSE_RICHEDIT
2167 if (IsRich())
2168 {
2169 if (!m_privateContextMenu)
2170 {
2171 m_privateContextMenu = new wxMenu;
2172 m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
2173 m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
2174 m_privateContextMenu->AppendSeparator();
2175 m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
2176 m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
2177 m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
2178 m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
2179 m_privateContextMenu->AppendSeparator();
2180 m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
2181 }
2182 PopupMenu(m_privateContextMenu);
2183 return;
2184 }
2185 else
2186 #endif
2187 event.Skip();
2188 }
2189
2190 void wxTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
2191 {
2192 // be sure the caret remains invisible if the user had hidden it
2193 if ( !m_isNativeCaretShown )
2194 {
2195 ::HideCaret(GetHwnd());
2196 }
2197 }
2198
2199 // ----------------------------------------------------------------------------
2200 // Default colors for MSW text control
2201 //
2202 // Set default background color to the native white instead of
2203 // the default wxSYS_COLOUR_BTNFACE (is triggered with wxNullColour).
2204 // ----------------------------------------------------------------------------
2205
2206 wxVisualAttributes wxTextCtrl::GetDefaultAttributes() const
2207 {
2208 wxVisualAttributes attrs;
2209 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
2210 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
2211 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); //white
2212
2213 return attrs;
2214 }
2215
2216 // the rest of the file only deals with the rich edit controls
2217 #if wxUSE_RICHEDIT
2218
2219 // ----------------------------------------------------------------------------
2220 // EN_LINK processing
2221 // ----------------------------------------------------------------------------
2222
2223 bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
2224 {
2225 NMHDR *hdr = (NMHDR* )lParam;
2226 switch ( hdr->code )
2227 {
2228 case EN_MSGFILTER:
2229 {
2230 const MSGFILTER *msgf = (MSGFILTER *)lParam;
2231 UINT msg = msgf->msg;
2232
2233 // this is a bit crazy but richedit 1.0 sends us all mouse
2234 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
2235 // generate the wxWin events for this message manually
2236 //
2237 // NB: in fact, this is still not totally correct as it does
2238 // send us WM_LBUTTONUP if the selection was cleared by the
2239 // last click -- so currently we get 2 events in this case,
2240 // but as I don't see any obvious way to check for this I
2241 // leave this code in place because it's still better than
2242 // not getting left up events at all
2243 if ( msg == WM_LBUTTONUP )
2244 {
2245 WXUINT flags = msgf->wParam;
2246 int x = GET_X_LPARAM(msgf->lParam),
2247 y = GET_Y_LPARAM(msgf->lParam);
2248
2249 HandleMouseEvent(msg, x, y, flags);
2250 }
2251 }
2252
2253 // return true to process the event (and false to ignore it)
2254 return true;
2255
2256 case EN_LINK:
2257 {
2258 const ENLINK *enlink = (ENLINK *)hdr;
2259
2260 switch ( enlink->msg )
2261 {
2262 case WM_SETCURSOR:
2263 // ok, so it is hardcoded - do we really nee to
2264 // customize it?
2265 {
2266 wxCursor cur(wxCURSOR_HAND);
2267 ::SetCursor(GetHcursorOf(cur));
2268 *result = TRUE;
2269 break;
2270 }
2271
2272 case WM_MOUSEMOVE:
2273 case WM_LBUTTONDOWN:
2274 case WM_LBUTTONUP:
2275 case WM_LBUTTONDBLCLK:
2276 case WM_RBUTTONDOWN:
2277 case WM_RBUTTONUP:
2278 case WM_RBUTTONDBLCLK:
2279 // send a mouse event
2280 {
2281 static const wxEventType eventsMouse[] =
2282 {
2283 wxEVT_MOTION,
2284 wxEVT_LEFT_DOWN,
2285 wxEVT_LEFT_UP,
2286 wxEVT_LEFT_DCLICK,
2287 wxEVT_RIGHT_DOWN,
2288 wxEVT_RIGHT_UP,
2289 wxEVT_RIGHT_DCLICK,
2290 };
2291
2292 // the event ids are consecutive
2293 wxMouseEvent
2294 evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]);
2295
2296 InitMouseEvent(evtMouse,
2297 GET_X_LPARAM(enlink->lParam),
2298 GET_Y_LPARAM(enlink->lParam),
2299 enlink->wParam);
2300
2301 wxTextUrlEvent event(m_windowId, evtMouse,
2302 enlink->chrg.cpMin,
2303 enlink->chrg.cpMax);
2304
2305 InitCommandEvent(event);
2306
2307 *result = ProcessCommand(event);
2308 }
2309 break;
2310 }
2311 }
2312 return true;
2313 }
2314
2315 // not processed, leave it to the base class
2316 return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result);
2317 }
2318
2319 // ----------------------------------------------------------------------------
2320 // colour setting for the rich edit controls
2321 // ----------------------------------------------------------------------------
2322
2323 bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
2324 {
2325 if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
2326 {
2327 // colour didn't really change
2328 return false;
2329 }
2330
2331 if ( IsRich() )
2332 {
2333 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
2334 // EM_SETBKGNDCOLOR additionally
2335 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
2336 }
2337
2338 return true;
2339 }
2340
2341 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
2342 {
2343 if ( !wxTextCtrlBase::SetForegroundColour(colour) )
2344 {
2345 // colour didn't really change
2346 return false;
2347 }
2348
2349 if ( IsRich() )
2350 {
2351 // change the colour of everything
2352 CHARFORMAT cf;
2353 wxZeroMemory(cf);
2354 cf.cbSize = sizeof(cf);
2355 cf.dwMask = CFM_COLOR;
2356 cf.crTextColor = wxColourToRGB(colour);
2357 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
2358 }
2359
2360 return true;
2361 }
2362
2363 // ----------------------------------------------------------------------------
2364 // styling support for rich edit controls
2365 // ----------------------------------------------------------------------------
2366
2367 #if wxUSE_RICHEDIT
2368
2369 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
2370 {
2371 if ( !IsRich() )
2372 {
2373 // can't do it with normal text control
2374 return false;
2375 }
2376
2377 // the richedit 1.0 doesn't handle setting background colour, so don't
2378 // even try to do anything if it's the only thing we want to change
2379 if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() &&
2380 !style.HasLeftIndent() && !style.HasRightIndent() && !style.HasAlignment() &&
2381 !style.HasTabs() )
2382 {
2383 // nothing to do: return true if there was really nothing to do and
2384 // false if we failed to set bg colour
2385 return !style.HasBackgroundColour();
2386 }
2387
2388 // order the range if needed
2389 if ( start > end )
2390 {
2391 long tmp = start;
2392 start = end;
2393 end = tmp;
2394 }
2395
2396 // we can only change the format of the selection, so select the range we
2397 // want and restore the old selection later
2398 long startOld, endOld;
2399 GetSelection(&startOld, &endOld);
2400
2401 // but do we really have to change the selection?
2402 bool changeSel = start != startOld || end != endOld;
2403
2404 if ( changeSel )
2405 {
2406 DoSetSelection(start, end, false /* don't scroll caret into view */);
2407 }
2408
2409 // initialize CHARFORMAT struct
2410 #if wxUSE_RICHEDIT2
2411 CHARFORMAT2 cf;
2412 #else
2413 CHARFORMAT cf;
2414 #endif
2415
2416 wxZeroMemory(cf);
2417
2418 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2419 // CHARFORMAT in that case
2420 #if wxUSE_RICHEDIT2
2421 if ( m_verRichEdit == 1 )
2422 {
2423 // this is the only thing the control is going to grok
2424 cf.cbSize = sizeof(CHARFORMAT);
2425 }
2426 else
2427 #endif
2428 {
2429 // CHARFORMAT or CHARFORMAT2
2430 cf.cbSize = sizeof(cf);
2431 }
2432
2433 if ( style.HasFont() )
2434 {
2435 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
2436 // but using it doesn't seem to hurt neither so leaving it for now
2437
2438 cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET |
2439 CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE;
2440
2441 // fill in data from LOGFONT but recalculate lfHeight because we need
2442 // the real height in twips and not the negative number which
2443 // wxFillLogFont() returns (this is correct in general and works with
2444 // the Windows font mapper, but not here)
2445 LOGFONT lf;
2446 wxFillLogFont(&lf, &style.GetFont());
2447 cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips
2448 cf.bCharSet = lf.lfCharSet;
2449 cf.bPitchAndFamily = lf.lfPitchAndFamily;
2450 wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
2451
2452 // also deal with underline/italic/bold attributes: note that we must
2453 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
2454 // style to allow clearing it
2455 if ( lf.lfItalic )
2456 {
2457 cf.dwEffects |= CFE_ITALIC;
2458 }
2459
2460 if ( lf.lfWeight == FW_BOLD )
2461 {
2462 cf.dwEffects |= CFE_BOLD;
2463 }
2464
2465 if ( lf.lfUnderline )
2466 {
2467 cf.dwEffects |= CFE_UNDERLINE;
2468 }
2469
2470 // strikeout fonts are not supported by wxWidgets
2471 }
2472
2473 if ( style.HasTextColour() )
2474 {
2475 cf.dwMask |= CFM_COLOR;
2476 cf.crTextColor = wxColourToRGB(style.GetTextColour());
2477 }
2478
2479 #if wxUSE_RICHEDIT2
2480 if ( m_verRichEdit != 1 && style.HasBackgroundColour() )
2481 {
2482 cf.dwMask |= CFM_BACKCOLOR;
2483 cf.crBackColor = wxColourToRGB(style.GetBackgroundColour());
2484 }
2485 #endif // wxUSE_RICHEDIT2
2486
2487 // do format the selection
2488 bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
2489 SCF_SELECTION, (LPARAM)&cf) != 0;
2490 if ( !ok )
2491 {
2492 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
2493 }
2494
2495 // now do the paragraph formatting
2496 PARAFORMAT2 pf;
2497 wxZeroMemory(pf);
2498 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2499 // PARAFORMAT in that case
2500 #if wxUSE_RICHEDIT2
2501 if ( m_verRichEdit == 1 )
2502 {
2503 // this is the only thing the control is going to grok
2504 pf.cbSize = sizeof(PARAFORMAT);
2505 }
2506 else
2507 #endif
2508 {
2509 // PARAFORMAT or PARAFORMAT2
2510 pf.cbSize = sizeof(pf);
2511 }
2512
2513 if (style.HasAlignment())
2514 {
2515 pf.dwMask |= PFM_ALIGNMENT;
2516 if (style.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
2517 pf.wAlignment = PFA_RIGHT;
2518 else if (style.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
2519 pf.wAlignment = PFA_CENTER;
2520 else if (style.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED)
2521 pf.wAlignment = PFA_JUSTIFY;
2522 else
2523 pf.wAlignment = PFA_LEFT;
2524 }
2525
2526 if (style.HasLeftIndent())
2527 {
2528 pf.dwMask |= PFM_STARTINDENT | PFM_OFFSET;
2529
2530 // Convert from 1/10 mm to TWIPS
2531 pf.dxStartIndent = (int) (((double) style.GetLeftIndent()) * mm2twips / 10.0) ;
2532 pf.dxOffset = (int) (((double) style.GetLeftSubIndent()) * mm2twips / 10.0) ;
2533 }
2534
2535 if (style.HasRightIndent())
2536 {
2537 pf.dwMask |= PFM_RIGHTINDENT;
2538
2539 // Convert from 1/10 mm to TWIPS
2540 pf.dxRightIndent = (int) (((double) style.GetRightIndent()) * mm2twips / 10.0) ;
2541 }
2542
2543 if (style.HasTabs())
2544 {
2545 pf.dwMask |= PFM_TABSTOPS;
2546
2547 const wxArrayInt& tabs = style.GetTabs();
2548
2549 pf.cTabCount = (SHORT)wxMin(tabs.GetCount(), MAX_TAB_STOPS);
2550 size_t i;
2551 for (i = 0; i < (size_t) pf.cTabCount; i++)
2552 {
2553 // Convert from 1/10 mm to TWIPS
2554 pf.rgxTabs[i] = (int) (((double) tabs[i]) * mm2twips / 10.0) ;
2555 }
2556 }
2557
2558 #if wxUSE_RICHEDIT2
2559 if ( m_verRichEdit > 1 )
2560 {
2561 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
2562 {
2563 // Use RTL paragraphs in RTL mode to get proper layout
2564 pf.dwMask |= PFM_RTLPARA;
2565 pf.wEffects |= PFE_RTLPARA;
2566 }
2567 }
2568 #endif // wxUSE_RICHEDIT2
2569
2570 if ( pf.dwMask )
2571 {
2572 // do format the selection
2573 bool ok = ::SendMessage(GetHwnd(), EM_SETPARAFORMAT,
2574 0, (LPARAM) &pf) != 0;
2575 if ( !ok )
2576 {
2577 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2578 }
2579 }
2580
2581 if ( changeSel )
2582 {
2583 // restore the original selection
2584 DoSetSelection(startOld, endOld, false);
2585 }
2586
2587 return ok;
2588 }
2589
2590 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2591 {
2592 if ( !wxTextCtrlBase::SetDefaultStyle(style) )
2593 return false;
2594
2595 if ( IsEditable() )
2596 {
2597 // we have to do this or the style wouldn't apply for the text typed by
2598 // the user
2599 wxTextPos posLast = GetLastPosition();
2600 SetStyle(posLast, posLast, m_defaultStyle);
2601 }
2602
2603 return true;
2604 }
2605
2606 bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
2607 {
2608 if ( !IsRich() )
2609 {
2610 // can't do it with normal text control
2611 return false;
2612 }
2613
2614 // initialize CHARFORMAT struct
2615 #if wxUSE_RICHEDIT2
2616 CHARFORMAT2 cf;
2617 #else
2618 CHARFORMAT cf;
2619 #endif
2620
2621 wxZeroMemory(cf);
2622
2623 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2624 // CHARFORMAT in that case
2625 #if wxUSE_RICHEDIT2
2626 if ( m_verRichEdit == 1 )
2627 {
2628 // this is the only thing the control is going to grok
2629 cf.cbSize = sizeof(CHARFORMAT);
2630 }
2631 else
2632 #endif
2633 {
2634 // CHARFORMAT or CHARFORMAT2
2635 cf.cbSize = sizeof(cf);
2636 }
2637 // we can only change the format of the selection, so select the range we
2638 // want and restore the old selection later
2639 long startOld, endOld;
2640 GetSelection(&startOld, &endOld);
2641
2642 // but do we really have to change the selection?
2643 bool changeSel = position != startOld || position != endOld;
2644
2645 if ( changeSel )
2646 {
2647 DoSetSelection(position, position+1, false /* don't scroll caret into view */);
2648 }
2649
2650 // get the selection formatting
2651 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT,
2652 SCF_SELECTION, (LPARAM)&cf) ;
2653
2654
2655 LOGFONT lf;
2656 lf.lfHeight = cf.yHeight;
2657 lf.lfWidth = 0;
2658 lf.lfCharSet = ANSI_CHARSET; // FIXME: how to get correct charset?
2659 lf.lfClipPrecision = 0;
2660 lf.lfEscapement = 0;
2661 wxStrcpy(lf.lfFaceName, cf.szFaceName);
2662
2663 //NOTE: we _MUST_ set each of these values to _something_ since we
2664 //do not call wxZeroMemory on the LOGFONT lf
2665 if (cf.dwEffects & CFE_ITALIC)
2666 lf.lfItalic = TRUE;
2667 else
2668 lf.lfItalic = FALSE;
2669
2670 lf.lfOrientation = 0;
2671 lf.lfPitchAndFamily = cf.bPitchAndFamily;
2672 lf.lfQuality = 0;
2673
2674 if (cf.dwEffects & CFE_STRIKEOUT)
2675 lf.lfStrikeOut = TRUE;
2676 else
2677 lf.lfStrikeOut = FALSE;
2678
2679 if (cf.dwEffects & CFE_UNDERLINE)
2680 lf.lfUnderline = TRUE;
2681 else
2682 lf.lfUnderline = FALSE;
2683
2684 if (cf.dwEffects & CFE_BOLD)
2685 lf.lfWeight = FW_BOLD;
2686 else
2687 lf.lfWeight = FW_NORMAL;
2688
2689 wxFont font = wxCreateFontFromLogFont(& lf);
2690 if (font.Ok())
2691 {
2692 style.SetFont(font);
2693 }
2694 style.SetTextColour(wxColour(cf.crTextColor));
2695
2696 #if wxUSE_RICHEDIT2
2697 if ( m_verRichEdit != 1 )
2698 {
2699 // cf.dwMask |= CFM_BACKCOLOR;
2700 style.SetBackgroundColour(wxColour(cf.crBackColor));
2701 }
2702 #endif // wxUSE_RICHEDIT2
2703
2704 // now get the paragraph formatting
2705 PARAFORMAT2 pf;
2706 wxZeroMemory(pf);
2707 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2708 // PARAFORMAT in that case
2709 #if wxUSE_RICHEDIT2
2710 if ( m_verRichEdit == 1 )
2711 {
2712 // this is the only thing the control is going to grok
2713 pf.cbSize = sizeof(PARAFORMAT);
2714 }
2715 else
2716 #endif
2717 {
2718 // PARAFORMAT or PARAFORMAT2
2719 pf.cbSize = sizeof(pf);
2720 }
2721
2722 // do format the selection
2723 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT, 0, (LPARAM) &pf) ;
2724
2725 style.SetLeftIndent( (int) ((double) pf.dxStartIndent * twips2mm * 10.0), (int) ((double) pf.dxOffset * twips2mm * 10.0) );
2726 style.SetRightIndent( (int) ((double) pf.dxRightIndent * twips2mm * 10.0) );
2727
2728 if (pf.wAlignment == PFA_CENTER)
2729 style.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
2730 else if (pf.wAlignment == PFA_RIGHT)
2731 style.SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
2732 else if (pf.wAlignment == PFA_JUSTIFY)
2733 style.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED);
2734 else
2735 style.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
2736
2737 wxArrayInt tabStops;
2738 size_t i;
2739 for (i = 0; i < (size_t) pf.cTabCount; i++)
2740 {
2741 tabStops.Add( (int) ((double) (pf.rgxTabs[i] & 0xFFFF) * twips2mm * 10.0) );
2742 }
2743
2744 if ( changeSel )
2745 {
2746 // restore the original selection
2747 DoSetSelection(startOld, endOld, false);
2748 }
2749
2750 return true;
2751 }
2752
2753 #endif
2754
2755 // ----------------------------------------------------------------------------
2756 // wxRichEditModule
2757 // ----------------------------------------------------------------------------
2758
2759 static const HINSTANCE INVALID_HINSTANCE = (HINSTANCE)-1;
2760
2761 bool wxRichEditModule::OnInit()
2762 {
2763 // don't do anything - we will load it when needed
2764 return true;
2765 }
2766
2767 void wxRichEditModule::OnExit()
2768 {
2769 for ( size_t i = 0; i < WXSIZEOF(ms_hRichEdit); i++ )
2770 {
2771 if ( ms_hRichEdit[i] && ms_hRichEdit[i] != INVALID_HINSTANCE )
2772 {
2773 ::FreeLibrary(ms_hRichEdit[i]);
2774 ms_hRichEdit[i] = NULL;
2775 }
2776 }
2777 #if wxUSE_INKEDIT
2778 if (ms_inkEditLib.IsLoaded())
2779 ms_inkEditLib.Unload();
2780 #endif
2781 }
2782
2783 /* static */
2784 bool wxRichEditModule::Load(Version version)
2785 {
2786 if ( ms_hRichEdit[version] == INVALID_HINSTANCE )
2787 {
2788 // we had already tried to load it and failed
2789 return false;
2790 }
2791
2792 if ( ms_hRichEdit[version] )
2793 {
2794 // we've already got this one
2795 return true;
2796 }
2797
2798 static const wxChar *dllnames[] =
2799 {
2800 _T("riched32"),
2801 _T("riched20"),
2802 _T("msftedit"),
2803 };
2804
2805 wxCOMPILE_TIME_ASSERT( WXSIZEOF(dllnames) == Version_Max,
2806 RichEditDllNamesVersionsMismatch );
2807
2808 ms_hRichEdit[version] = ::LoadLibrary(dllnames[version]);
2809
2810 if ( !ms_hRichEdit[version] )
2811 {
2812 ms_hRichEdit[version] = INVALID_HINSTANCE;
2813
2814 return false;
2815 }
2816
2817 return true;
2818 }
2819
2820 #if wxUSE_INKEDIT
2821 // load the InkEdit library
2822 bool wxRichEditModule::LoadInkEdit()
2823 {
2824 static wxDynamicLibrary ms_inkEditLib;
2825 static bool ms_inkEditLibLoadAttemped;
2826 if (ms_inkEditLibLoadAttemped)
2827 ms_inkEditLib.IsLoaded();
2828
2829 ms_inkEditLibLoadAttemped = true;
2830
2831 wxLogNull logNull;
2832 return ms_inkEditLib.Load(wxT("inked"));
2833 }
2834 #endif
2835
2836
2837 #endif // wxUSE_RICHEDIT
2838
2839 #endif // wxUSE_TEXTCTRL && !(__SMARTPHONE__ && __WXWINCE__)