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