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