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