1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/textctrl.cpp 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  27 #if wxUSE_TEXTCTRL && !(defined(__SMARTPHONE__) && defined(__WXWINCE__)) 
  30     #include "wx/textctrl.h" 
  31     #include "wx/settings.h" 
  39     #include "wx/module.h" 
  42 #include "wx/sysopt.h" 
  45     #include "wx/clipbrd.h" 
  48 #include "wx/textfile.h" 
  52 #include "wx/msw/private.h" 
  53 #include "wx/msw/winundef.h" 
  54 #include "wx/msw/mslu.h" 
  60 #include <sys/types.h> 
  66 #include "wx/dynlib.h" 
  69 // old mingw32 has richedit stuff directly in windows.h and doesn't have 
  71 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__) 
  75 #endif // wxUSE_RICHEDIT 
  77 #include "wx/msw/missing.h" 
  79 // ---------------------------------------------------------------------------- 
  81 // ---------------------------------------------------------------------------- 
  85 // this module initializes RichEdit DLL(s) if needed 
  86 class wxRichEditModule 
: public wxModule
 
  91         Version_1
,          // riched32.dll 
  92         Version_2or3
,       // both use riched20.dll 
  93         Version_41
,         // msftedit.dll (XP SP1 and Windows 2003) 
  97     virtual bool OnInit(); 
  98     virtual void OnExit(); 
 100     // load the richedit DLL for the specified version of rich edit 
 101     static bool Load(Version version
); 
 104     // load the InkEdit library 
 105     static bool LoadInkEdit(); 
 109     // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs 
 110     static HINSTANCE ms_hRichEdit
[Version_Max
]; 
 113     static wxDynamicLibrary ms_inkEditLib
; 
 114     static bool             ms_inkEditLibLoadAttemped
; 
 117     DECLARE_DYNAMIC_CLASS(wxRichEditModule
) 
 120 HINSTANCE 
wxRichEditModule::ms_hRichEdit
[Version_Max
] = { NULL
, NULL
, NULL 
}; 
 123 wxDynamicLibrary 
wxRichEditModule::ms_inkEditLib
; 
 124 bool             wxRichEditModule::ms_inkEditLibLoadAttemped 
= false; 
 127 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule
, wxModule
) 
 129 #endif // wxUSE_RICHEDIT 
 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
 
 136     UpdatesCountFilter(int& count
) 
 139         wxASSERT_MSG( m_count 
== -1 || m_count 
== -2, 
 140                       _T("wrong initial m_updatesCount value") ); 
 144         //else: we don't want to count how many update events we get as we're going 
 145         //      to ignore all of them 
 148     ~UpdatesCountFilter() 
 153     // return true if an event has been received 
 154     bool GotUpdate() const 
 162     DECLARE_NO_COPY_CLASS(UpdatesCountFilter
) 
 165 // ---------------------------------------------------------------------------- 
 166 // event tables and other macros 
 167 // ---------------------------------------------------------------------------- 
 169 #if wxUSE_EXTENDED_RTTI 
 170 WX_DEFINE_FLAGS( wxTextCtrlStyle 
) 
 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
) 
 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
) 
 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
) 
 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
) 
 217 wxEND_FLAGS( wxTextCtrlStyle 
) 
 219 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl
, wxControl
,"wx/textctrl.h") 
 221 wxBEGIN_PROPERTIES_TABLE(wxTextCtrl
) 
 222     wxEVENT_PROPERTY( TextUpdated 
, wxEVT_COMMAND_TEXT_UPDATED 
, wxCommandEvent 
) 
 223     wxEVENT_PROPERTY( TextEnter 
, wxEVT_COMMAND_TEXT_ENTER 
, wxCommandEvent 
) 
 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() 
 230 wxBEGIN_HANDLERS_TABLE(wxTextCtrl
) 
 231 wxEND_HANDLERS_TABLE() 
 233 wxCONSTRUCTOR_6( wxTextCtrl 
, wxWindow
* , Parent 
, wxWindowID 
, Id 
, wxString 
, Value 
, wxPoint 
, Position 
, wxSize 
, Size 
, long , WindowStyle
) 
 235 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
) 
 239 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
) 
 240     EVT_CHAR(wxTextCtrl::OnChar
) 
 241     EVT_DROP_FILES(wxTextCtrl::OnDropFiles
) 
 244     EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
) 
 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
) 
 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
) 
 263     EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
) 
 266 // ---------------------------------------------------------------------------- 
 267 // function prototypes 
 268 // ---------------------------------------------------------------------------- 
 270 LRESULT APIENTRY _EXPORT 
wxTextCtrlWndProc(HWND hWnd
, 
 275 // --------------------------------------------------------------------------- 
 277 // --------------------------------------------------------------------------- 
 279 // the pointer to standard text control wnd proc 
 280 static WNDPROC gs_wndprocEdit 
= (WNDPROC
)NULL
; 
 282 // ============================================================================ 
 284 // ============================================================================ 
 286 // ---------------------------------------------------------------------------- 
 287 // wnd proc for subclassed edit control 
 288 // ---------------------------------------------------------------------------- 
 290 LRESULT APIENTRY _EXPORT 
wxTextCtrlWndProc(HWND hWnd
, 
 301                 wxWindow 
*win 
= wxFindWinFromHandle((WXHWND
)hWnd
); 
 302                 if( win
->HandleClipboardEvent( message 
) ) 
 307     return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit
, hWnd
, message
, wParam
, lParam
); 
 310 // ---------------------------------------------------------------------------- 
 312 // ---------------------------------------------------------------------------- 
 314 void wxTextCtrl::Init() 
 318 #endif // wxUSE_RICHEDIT 
 320 #if wxUSE_INKEDIT && wxUSE_RICHEDIT 
 324     m_privateContextMenu 
= NULL
; 
 326     m_isNativeCaretShown 
= true; 
 329 wxTextCtrl::~wxTextCtrl() 
 331     delete m_privateContextMenu
; 
 334 bool wxTextCtrl::Create(wxWindow 
*parent
, wxWindowID id
, 
 335                         const wxString
& value
, 
 339                         const wxValidator
& validator
, 
 340                         const wxString
& name
) 
 343     if ((style 
& wxBORDER_MASK
) == 0) 
 344         style 
|= wxBORDER_SIMPLE
; 
 347     // base initialization 
 348     if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) ) 
 351     // translate wxWin style flags to MSW ones 
 352     WXDWORD msStyle 
= MSWGetCreateWindowFlags(); 
 354     // do create the control - either an EDIT or RICHEDIT 
 355     wxString windowClass 
= wxT("EDIT"); 
 357 #if defined(__POCKETPC__) || defined(__SMARTPHONE__) 
 358     // A control that capitalizes the first letter 
 359     if (style 
& wxTE_CAPITALIZE
) 
 360         windowClass 
= wxT("CAPEDIT"); 
 364     if ( m_windowStyle 
& wxTE_AUTO_URL 
) 
 366         // automatic URL detection only works in RichEdit 2.0+ 
 367         m_windowStyle 
|= wxTE_RICH2
; 
 370     if ( m_windowStyle 
& wxTE_RICH2 
) 
 372         // using richedit 2.0 implies using wxTE_RICH 
 373         m_windowStyle 
|= wxTE_RICH
; 
 376     // we need to load the richedit DLL before creating the rich edit control 
 377     if ( m_windowStyle 
& wxTE_RICH 
) 
 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) 
 384         // notice that 1.0 has no Unicode support at all so in Unicode build we 
 385         // must use another version 
 389 #else // !wxUSE_UNICODE 
 390         m_verRichEdit 
= m_windowStyle 
& wxTE_RICH2 
? 2 : 1; 
 391 #endif // wxUSE_UNICODE/!wxUSE_UNICODE 
 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. 
 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)) 
 403             if (wxRichEditModule::LoadInkEdit()) 
 405                 windowClass 
= INKEDIT_CLASS
; 
 407 #if wxUSE_INKEDIT && wxUSE_RICHEDIT 
 411                 // Fake rich text version for other calls 
 419             if ( m_verRichEdit 
== 2 ) 
 421                 if ( wxRichEditModule::Load(wxRichEditModule::Version_41
) ) 
 423                     // yes, class name for version 4.1 really is 5.0 
 424                     windowClass 
= _T("RICHEDIT50W"); 
 426                 else if ( wxRichEditModule::Load(wxRichEditModule::Version_2or3
) ) 
 428                     windowClass 
= _T("RichEdit20") 
 433 #endif // Unicode/ANSI 
 435                 else // failed to load msftedit.dll and riched20.dll 
 441             if ( m_verRichEdit 
== 1 ) 
 443                 if ( wxRichEditModule::Load(wxRichEditModule::Version_1
) ) 
 445                     windowClass 
= _T("RICHEDIT"); 
 447                 else // failed to load any richedit control DLL 
 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 
 454                         wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll")); 
 464 #endif // wxUSE_RICHEDIT 
 466     // we need to turn '\n's into "\r\n"s for the multiline controls 
 468     if ( m_windowStyle 
& wxTE_MULTILINE 
) 
 470         valueWin 
= wxTextFile::Translate(value
, wxTextFileType_Dos
); 
 477     if ( !MSWCreateControl(windowClass
, msStyle
, pos
, size
, valueWin
) ) 
 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); 
 490             // Make sure the mouse can be used for input 
 491             ::SendMessage(GetHwnd(), EM_SETUSEMOUSEFORINPUT
, 1, 0); 
 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
; 
 499         if (GetRichVersion() == 1 && !IsInkEdit()) 
 501             // we also need EN_MSGFILTER for richedit 1.0 for the reasons 
 502             // explained in its handler 
 503            mask 
|= ENM_MOUSEEVENTS
; 
 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 
 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 
 516         else if ( m_windowStyle 
& wxTE_AUTO_URL 
) 
 520             ::SendMessage(GetHwnd(), EM_AUTOURLDETECT
, TRUE
, 0); 
 523         ::SendMessage(GetHwnd(), EM_SETEVENTMASK
, 0, mask
); 
 525 #endif // wxUSE_RICHEDIT 
 527     gs_wndprocEdit 
= wxSetWindowProc((HWND
)GetHwnd(), 
 533 // Make sure the window style (etc.) reflects the HWND style (roughly) 
 534 void wxTextCtrl::AdoptAttributesFromHWND() 
 536     wxWindow::AdoptAttributesFromHWND(); 
 538     HWND hWnd 
= GetHwnd(); 
 539     long style 
= ::GetWindowLong(hWnd
, GWL_STYLE
); 
 541     // retrieve the style to see whether this is an edit or richedit ctrl 
 543     wxString classname 
= wxGetWindowClass(GetHWND()); 
 545     if ( classname
.IsSameAs(_T("EDIT"), false /* no case */) ) 
 552         if ( wxSscanf(classname
, _T("RichEdit%d0%c"), &m_verRichEdit
, &c
) != 2 ) 
 554             wxLogDebug(_T("Unknown edit control '%s'."), classname
.c_str()); 
 559 #endif // wxUSE_RICHEDIT 
 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
; 
 575 WXDWORD 
wxTextCtrl::MSWGetStyle(long style
, WXDWORD 
*exstyle
) const 
 577     long msStyle 
= wxControl::MSWGetStyle(style
, exstyle
); 
 579     // styles which we alaways add by default 
 580     if ( style 
& wxTE_MULTILINE 
) 
 582         wxASSERT_MSG( !(style 
& wxTE_PROCESS_ENTER
), 
 583                       wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") ); 
 585         msStyle 
|= ES_MULTILINE 
| ES_WANTRETURN
; 
 586         if ( !(style 
& wxTE_NO_VSCROLL
) ) 
 588             // always adjust the vertical scrollbar automatically if we have it 
 589             msStyle 
|= WS_VSCROLL 
| ES_AUTOVSCROLL
; 
 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 
) 
 597                 msStyle 
|= ES_DISABLENOSCROLL
; 
 599 #endif // wxUSE_RICHEDIT 
 602         style 
|= wxTE_PROCESS_ENTER
; 
 606         // there is really no reason to not have this style for single line 
 608         msStyle 
|= ES_AUTOHSCROLL
; 
 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 
) 
 615         // automatically scroll the control horizontally as necessary 
 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 
 621         msStyle 
|= WS_HSCROLL 
| ES_AUTOHSCROLL
; 
 624     if ( style 
& wxTE_READONLY 
) 
 625         msStyle 
|= ES_READONLY
; 
 627     if ( style 
& wxTE_PASSWORD 
) 
 628         msStyle 
|= ES_PASSWORD
; 
 630     if ( style 
& wxTE_NOHIDESEL 
) 
 631         msStyle 
|= ES_NOHIDESEL
; 
 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 
) 
 639         msStyle 
|= ES_LEFT
; // ES_LEFT is 0 as well but for consistency... 
 644 void wxTextCtrl::SetWindowStyleFlag(long style
) 
 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 
 651             ((style 
& wxTE_NOHIDESEL
) != (GetWindowStyle() & wxTE_NOHIDESEL
)) ) 
 653         bool set 
= (style 
& wxTE_NOHIDESEL
) != 0; 
 655         ::SendMessage(GetHwnd(), EM_SETOPTIONS
, set 
? ECOOP_OR 
: ECOOP_AND
, 
 656                       set 
? ECO_NOHIDESEL 
: ~ECO_NOHIDESEL
); 
 658 #endif // wxUSE_RICHEDIT 
 660     wxControl::SetWindowStyleFlag(style
); 
 663 // ---------------------------------------------------------------------------- 
 664 // set/get the controls text 
 665 // ---------------------------------------------------------------------------- 
 667 bool wxTextCtrl::IsEmpty() const 
 669     // this is an optimization for multiline controls containing a lot of text 
 670     if ( IsMultiLine() && GetNumberOfLines() != 1 ) 
 673     return wxTextCtrlBase::IsEmpty(); 
 676 wxString 
wxTextCtrl::GetValue() const 
 678     // range 0..-1 is special for GetRange() and means to retrieve all text 
 679     return GetRange(0, -1); 
 682 wxString 
wxTextCtrl::GetRange(long from
, long to
) const 
 686     if ( from 
>= to 
&& to 
!= -1 ) 
 688         // nothing to retrieve 
 695         int len 
= GetWindowTextLength(GetHwnd()); 
 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 ) 
 707                 // we must have some encoding, otherwise any 8bit chars in the 
 708                 // control are simply *lost* (replaced by '?') 
 709                 wxFontEncoding encoding 
= wxFONTENCODING_SYSTEM
; 
 711                 wxFont font 
= m_defaultStyle
.GetFont(); 
 717                    encoding 
= font
.GetEncoding(); 
 720                 if ( encoding 
== wxFONTENCODING_SYSTEM 
) 
 722                     encoding 
= wxLocale::GetSystemEncoding(); 
 725                 if ( encoding 
== wxFONTENCODING_SYSTEM 
) 
 727                     encoding 
= wxFONTENCODING_ISO8859_1
; 
 730                 str 
= StreamOut(encoding
); 
 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
); 
 741             // StreamOut() wasn't used or failed, try to do it in normal way 
 743 #endif // !wxUSE_UNICODE 
 745                 // alloc one extra WORD as needed by the control 
 746                 wxStringBuffer 
tmp(str
, ++len
); 
 750                 textRange
.chrg
.cpMin 
= from
; 
 751                 textRange
.chrg
.cpMax 
= to
; 
 752                 textRange
.lpstrText 
= p
; 
 754                 (void)::SendMessage(GetHwnd(), EM_GETTEXTRANGE
, 
 755                                     0, (LPARAM
)&textRange
); 
 757                 if ( m_verRichEdit 
> 1 ) 
 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 
 764                         if ( *p 
== _T('\r') ) 
 770             if ( m_verRichEdit 
== 1 ) 
 772                 // convert to the canonical form - see comment below 
 773                 str 
= wxTextFile::Translate(str
, wxTextFileType_Unix
); 
 776         //else: no text at all, leave the string empty 
 779 #endif // wxUSE_RICHEDIT 
 782         str 
= wxGetWindowText(GetHWND()); 
 784         // need only a range? 
 787             str 
= str
.Mid(from
, to 
- from
); 
 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
); 
 799 void wxTextCtrl::DoSetValue(const wxString
& value
, int flags
) 
 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()) ) 
 807         DoWriteText(value
, flags 
/* doesn't include SelectionOnly here */); 
 809         // mark the control as being not dirty - we changed its text, not the 
 813         // for compatibility, don't move the cursor when doing SetValue() 
 814         SetInsertionPoint(0); 
 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()) 
 824         // still send an event for consistency 
 825         if (flags 
& SetValue_SendEvent
) 
 830 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU) 
 832 // TODO: using memcpy() would improve performance a lot for big amounts of text 
 835 wxRichEditStreamIn(DWORD dwCookie
, BYTE 
*buf
, LONG cb
, LONG 
*pcb
) 
 839     const wchar_t ** const ppws 
= (const wchar_t **)dwCookie
; 
 841     wchar_t *wbuf 
= (wchar_t *)buf
; 
 842     const wchar_t *wpc 
= *ppws
; 
 847         cb 
-= sizeof(wchar_t); 
 848         (*pcb
) += sizeof(wchar_t); 
 856 // helper struct used to pass parameters from wxTextCtrl to wxRichEditStreamOut 
 857 struct wxStreamOutData
 
 864 wxRichEditStreamOut(DWORD_PTR dwCookie
, BYTE 
*buf
, LONG cb
, LONG 
*pcb
) 
 868     wxStreamOutData 
*data 
= (wxStreamOutData 
*)dwCookie
; 
 870     const wchar_t *wbuf 
= (const wchar_t *)buf
; 
 871     wchar_t *wpc 
= data
->wpc
; 
 874         wchar_t wch 
= *wbuf
++; 
 876         // turn "\r\n" into "\n" on the fly 
 882         cb 
-= sizeof(wchar_t); 
 883         (*pcb
) += sizeof(wchar_t); 
 892 #if wxUSE_UNICODE_MSLU 
 893     #define UNUSED_IF_MSLU(param) 
 895     #define UNUSED_IF_MSLU(param) param 
 899 wxTextCtrl::StreamIn(const wxString
& value
, 
 900                      wxFontEncoding 
UNUSED_IF_MSLU(encoding
), 
 903 #if wxUSE_UNICODE_MSLU 
 904     const wchar_t *wpc 
= value
.c_str(); 
 905 #else // !wxUSE_UNICODE_MSLU 
 906     wxCSConv 
conv(encoding
); 
 908     const size_t len 
= conv
.MB2WC(NULL
, value
, value
.length()); 
 911     wxWCharBuffer 
wchBuf(len
); 
 912     wchar_t *wpc 
= wchBuf
.data(); 
 914     wchar_t *wchBuf 
= (wchar_t *)malloc((len 
+ 1)*sizeof(wchar_t)); 
 915     wchar_t *wpc 
= wchBuf
; 
 918     conv
.MB2WC(wpc
, value
, value
.length()); 
 919 #endif // wxUSE_UNICODE_MSLU 
 921     // finally, stream it in the control 
 924     eds
.dwCookie 
= (DWORD
)&wpc
; 
 925     // the cast below is needed for broken (very) old mingw32 headers 
 926     eds
.pfnCallback 
= (EDITSTREAMCALLBACK
)wxRichEditStreamIn
; 
 928     // same problem as in DoWriteText(): we can get multiple events here 
 929     UpdatesCountFilter 
ucf(m_updatesCount
); 
 931     ::SendMessage(GetHwnd(), EM_STREAMIN
, 
 934                   (selectionOnly 
? SFF_SELECTION 
: 0), 
 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?") ); 
 944         wxLogLastError(_T("EM_STREAMIN")); 
 949 #endif // !wxUSE_WCHAR_T 
 954 #if !wxUSE_UNICODE_MSLU 
 957 wxTextCtrl::StreamOut(wxFontEncoding encoding
, bool selectionOnly
) const 
 961     const int len 
= GetWindowTextLength(GetHwnd()); 
 964     wxWCharBuffer 
wchBuf(len
); 
 965     wchar_t *wpc 
= wchBuf
.data(); 
 967     wchar_t *wchBuf 
= (wchar_t *)malloc((len 
+ 1)*sizeof(wchar_t)); 
 968     wchar_t *wpc 
= wchBuf
; 
 971     wxStreamOutData data
; 
 977     eds
.dwCookie 
= (DWORD
)&data
; 
 978     eds
.pfnCallback 
= wxRichEditStreamOut
; 
 984         SF_TEXT 
| SF_UNICODE 
| (selectionOnly 
? SFF_SELECTION 
: 0), 
 990         wxLogLastError(_T("EM_STREAMOUT")); 
 992     else // streamed out ok 
 994         // NUL-terminate the string because its length could have been 
 995         // decreased by wxRichEditStreamOut 
 996         *(wchBuf
.data() + data
.len
) = L
'\0'; 
 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); 
1004             conv
.WC2MB(wxStringBuffer(out
, lenNeeded
), wchBuf
, lenNeeded
); 
1010 #endif // !wxUSE_WCHAR_T 
1015 #endif // !wxUSE_UNICODE_MSLU 
1017 #endif // wxUSE_RICHEDIT 
1019 void wxTextCtrl::WriteText(const wxString
& value
) 
1024 void wxTextCtrl::DoWriteText(const wxString
& value
, int flags
) 
1026     bool selectionOnly 
= (flags 
& SetValue_SelectionOnly
) != 0; 
1028     if ( m_windowStyle 
& wxTE_MULTILINE 
) 
1029         valueDos 
= wxTextFile::Translate(value
, wxTextFileType_Dos
); 
1034     // there are several complications with the rich edit controls here 
1038         // first, ensure that the new text will be in the default style 
1039         if ( !m_defaultStyle
.IsDefault() ) 
1042             GetSelection(&start
, &end
); 
1043             SetStyle(start
, end
, m_defaultStyle
); 
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 ) 
1051            done 
= StreamIn(valueDos
, wxFONTENCODING_SYSTEM
, selectionOnly
); 
1053 #endif // wxUSE_UNICODE_MSLU 
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 ) 
1060             wxFont font 
= m_defaultStyle
.GetFont(); 
1066                wxFontEncoding encoding 
= font
.GetEncoding(); 
1067                if ( encoding 
!= wxFONTENCODING_SYSTEM 
) 
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
); 
1077 #endif // !wxUSE_UNICODE 
1081 #endif // wxUSE_RICHEDIT 
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 
1093         UpdatesCountFilter 
ucf(m_updatesCount
); 
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()); 
1099         if ( !ucf
.GotUpdate() && (flags 
& SetValue_SendEvent
) ) 
1106 void wxTextCtrl::AppendText(const wxString
& text
) 
1108     SetInsertionPointEnd(); 
1113     // don't do this if we're frozen, saves some time 
1114     if ( !IsFrozen() && IsMultiLine() && GetRichVersion() > 1 ) 
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()); 
1120 #endif // wxUSE_RICHEDIT 
1123 void wxTextCtrl::Clear() 
1125     ::SetWindowText(GetHwnd(), wxEmptyString
); 
1129 #endif // wxUSE_RICHEDIT 
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 
1135         // Windows already sends an update event for single-line 
1137         if ( m_windowStyle 
& wxTE_MULTILINE 
) 
1144 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent
& event
) 
1148     size_t lenOld 
= GetValue().length(); 
1150     wxUint32 code 
= event
.GetRawKeyCode(); 
1151     ::keybd_event((BYTE
)code
, 0, 0 /* key press */, 0); 
1152     ::keybd_event((BYTE
)code
, 0, KEYEVENTF_KEYUP
, 0); 
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
; 
1161 // ---------------------------------------------------------------------------- 
1162 // Clipboard operations 
1163 // ---------------------------------------------------------------------------- 
1165 void wxTextCtrl::Copy() 
1169         ::SendMessage(GetHwnd(), WM_COPY
, 0, 0L); 
1173 void wxTextCtrl::Cut() 
1177         ::SendMessage(GetHwnd(), WM_CUT
, 0, 0L); 
1181 void wxTextCtrl::Paste() 
1185         ::SendMessage(GetHwnd(), WM_PASTE
, 0, 0L); 
1189 bool wxTextCtrl::HasSelection() const 
1192     GetSelection(&from
, &to
); 
1196 bool wxTextCtrl::CanCopy() const 
1198     // Can copy if there's a selection 
1199     return HasSelection(); 
1202 bool wxTextCtrl::CanCut() const 
1204     return CanCopy() && IsEditable(); 
1207 bool wxTextCtrl::CanPaste() const 
1209     if ( !IsEditable() ) 
1215         UINT cf 
= 0; // 0 == any format 
1217         return ::SendMessage(GetHwnd(), EM_CANPASTE
, cf
, 0) != 0; 
1219 #endif // wxUSE_RICHEDIT 
1221     // Standard edit control: check for straight text on clipboard 
1222     if ( !::OpenClipboard(GetHwndOf(wxTheApp
->GetTopWindow())) ) 
1225     bool isTextAvailable 
= ::IsClipboardFormatAvailable(CF_TEXT
) != 0; 
1228     return isTextAvailable
; 
1231 // ---------------------------------------------------------------------------- 
1233 // ---------------------------------------------------------------------------- 
1235 void wxTextCtrl::SetEditable(bool editable
) 
1237     HWND hWnd 
= GetHwnd(); 
1238     ::SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L); 
1241 void wxTextCtrl::SetInsertionPoint(long pos
) 
1243     DoSetSelection(pos
, pos
); 
1246 void wxTextCtrl::SetInsertionPointEnd() 
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 
) 
1262     if ( m_verRichEdit 
== 1 ) 
1264         // we don't have to waste time calling GetLastPosition() in this case 
1267     else // !RichEdit 1.0 
1268 #endif // wxUSE_RICHEDIT 
1273     SetInsertionPoint(pos
); 
1276 long wxTextCtrl::GetInsertionPoint() const 
1284         ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
); 
1287 #endif // wxUSE_RICHEDIT 
1289     DWORD Pos 
= (DWORD
)::SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L); 
1290     return Pos 
& 0xFFFF; 
1293 wxTextPos 
wxTextCtrl::GetLastPosition() const 
1295     int numLines 
= GetNumberOfLines(); 
1296     long posStartLastLine 
= XYToPosition(0, numLines 
- 1); 
1298     long lenLastLine 
= GetLengthOfLineContainingPos(posStartLastLine
); 
1300     return posStartLastLine 
+ lenLastLine
; 
1303 // If the return values from and to are the same, there is no 
1305 void wxTextCtrl::GetSelection(long* from
, long* to
) const 
1310         CHARRANGE charRange
; 
1311         ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &charRange
); 
1313         *from 
= charRange
.cpMin
; 
1314         *to 
= charRange
.cpMax
; 
1317 #endif // !wxUSE_RICHEDIT 
1319         DWORD dwStart
, dwEnd
; 
1320         ::SendMessage(GetHwnd(), EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
); 
1327 bool wxTextCtrl::IsEditable() const 
1329     // strangely enough, we may be called before the control is created: our 
1330     // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls 
1335     long style 
= ::GetWindowLong(GetHwnd(), GWL_STYLE
); 
1337     return (style 
& ES_READONLY
) == 0; 
1340 // ---------------------------------------------------------------------------- 
1342 // ---------------------------------------------------------------------------- 
1344 void wxTextCtrl::SetSelection(long from
, long to
) 
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) ) 
1354     DoSetSelection(from
, to
); 
1357 void wxTextCtrl::DoSetSelection(long from
, long to
, bool scrollCaret
) 
1359     HWND hWnd 
= GetHwnd(); 
1367         ::SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
); 
1370 #endif // wxUSE_RICHEDIT 
1372         ::SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
); 
1375     if ( scrollCaret 
&& !IsFrozen() ) 
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...) 
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 
1393         // this is very ugly but I don't see any other way to make this work 
1395         if ( GetRichVersion() > 1 ) 
1397             if ( !HasFlag(wxTE_NOHIDESEL
) ) 
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
); 
1405             //else: everything is already ok 
1407 #endif // wxUSE_RICHEDIT 
1409         ::SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0); 
1412         // restore ECO_NOHIDESEL if we changed it 
1413         if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL
) ) 
1415             ::SendMessage(GetHwnd(), EM_SETOPTIONS
, 
1416                           ECOOP_AND
, ~ECO_NOHIDESEL
); 
1417             if ( style 
!= ::GetWindowLong(GetHwnd(), GWL_STYLE
) ) 
1418                 ::SetWindowLong(GetHwnd(), GWL_STYLE
, style
); 
1420 #endif // wxUSE_RICHEDIT 
1424 // ---------------------------------------------------------------------------- 
1425 // Working with files 
1426 // ---------------------------------------------------------------------------- 
1428 bool wxTextCtrl::DoLoadFile(const wxString
& file
, int fileType
) 
1430     if ( wxTextCtrlBase::DoLoadFile(file
, fileType
) ) 
1432         // update the size limit if needed 
1441 // ---------------------------------------------------------------------------- 
1443 // ---------------------------------------------------------------------------- 
1445 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
) 
1447     // Set selection and remove it 
1448     DoSetSelection(from
, to
, false /* don't scroll caret into view */); 
1453 void wxTextCtrl::Remove(long from
, long to
) 
1455     Replace(from
, to
, wxEmptyString
); 
1458 bool wxTextCtrl::IsModified() const 
1460     return ::SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0; 
1463 void wxTextCtrl::MarkDirty() 
1465     ::SendMessage(GetHwnd(), EM_SETMODIFY
, TRUE
, 0L); 
1468 void wxTextCtrl::DiscardEdits() 
1470     ::SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L); 
1473 int wxTextCtrl::GetNumberOfLines() const 
1475     return (int)::SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0); 
1478 // ---------------------------------------------------------------------------- 
1479 // Positions <-> coords 
1480 // ---------------------------------------------------------------------------- 
1482 long wxTextCtrl::XYToPosition(long x
, long y
) const 
1484     // This gets the char index for the _beginning_ of this line 
1485     long charIndex 
= ::SendMessage(GetHwnd(), EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0); 
1487     return charIndex 
+ x
; 
1490 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const 
1492     HWND hWnd 
= GetHwnd(); 
1494     // This gets the line number containing the character 
1499         lineNo 
= ::SendMessage(hWnd
, EM_EXLINEFROMCHAR
, 0, (LPARAM
)pos
); 
1502 #endif // wxUSE_RICHEDIT 
1504         lineNo 
= ::SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, 0); 
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 ) 
1520     // The X position must therefore be the different between pos and charIndex 
1522         *x 
= pos 
- charIndex
; 
1529 wxTextCtrlHitTestResult
 
1530 wxTextCtrl::HitTest(const wxPoint
& pt
, long *posOut
) const 
1532     // first get the position from Windows 
1539         // for rich edit controls the position is passed iva the struct fields 
1542         lParam 
= (LPARAM
)&ptl
; 
1545 #endif // wxUSE_RICHEDIT 
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
); 
1552     LRESULT pos 
= ::SendMessage(GetHwnd(), EM_CHARFROMPOS
, 0, lParam
); 
1556         // this seems to indicate an error... 
1557         return wxTE_HT_UNKNOWN
; 
1562 #endif // wxUSE_RICHEDIT 
1564         // for plain EDIT controls the higher word contains something else 
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 
1575     // FIXME: we need to distinguish between richedit 2 and 3 here somehow but 
1576     //        we don't know how to do it 
1579         ::SendMessage(GetHwnd(), EM_POSFROMCHAR
, (WPARAM
)&ptReal
, pos
); 
1582 #endif // wxUSE_RICHEDIT 
1584         LRESULT lRc 
= ::SendMessage(GetHwnd(), EM_POSFROMCHAR
, pos
, 0); 
1588             // this is apparently returned when pos corresponds to the last 
1595             ptReal
.x 
= LOWORD(lRc
); 
1596             ptReal
.y 
= HIWORD(lRc
); 
1600     wxTextCtrlHitTestResult rc
; 
1602     if ( pt
.y 
> ptReal
.y 
+ GetCharHeight() ) 
1604     else if ( pt
.x 
> ptReal
.x 
+ GetCharWidth() ) 
1605         rc 
= wxTE_HT_BEYOND
; 
1607         rc 
= wxTE_HT_ON_TEXT
; 
1615 // ---------------------------------------------------------------------------- 
1617 // ---------------------------------------------------------------------------- 
1619 void wxTextCtrl::ShowPosition(long pos
) 
1621     HWND hWnd 
= GetHwnd(); 
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. 
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); 
1634     int currentLineLineNo 
= (int)::SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L); 
1636     int specifiedLineLineNo 
= (int)::SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L); 
1638     int linesToScroll 
= specifiedLineLineNo 
- currentLineLineNo
; 
1640     if (linesToScroll 
!= 0) 
1641       (void)::SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
); 
1644 long wxTextCtrl::GetLengthOfLineContainingPos(long pos
) const 
1646     return ::SendMessage(GetHwnd(), EM_LINELENGTH
, (WPARAM
)pos
, 0); 
1649 int wxTextCtrl::GetLineLength(long lineNo
) const 
1651     long pos 
= XYToPosition(0, lineNo
); 
1653     return GetLengthOfLineContainingPos(pos
); 
1656 wxString 
wxTextCtrl::GetLineText(long lineNo
) const 
1658     size_t len 
= (size_t)GetLineLength(lineNo
) + 1; 
1660     // there must be at least enough place for the length WORD in the 
1662     len 
+= sizeof(WORD
); 
1666         wxStringBufferLength 
tmp(str
, len
); 
1669         *(WORD 
*)buf 
= (WORD
)len
; 
1670         len 
= (size_t)::SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
); 
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') ) 
1679                 buf
[len 
- 2] = _T('\n'); 
1683 #endif // wxUSE_RICHEDIT 
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') ) 
1699 void wxTextCtrl::SetMaxLength(unsigned long len
) 
1704         ::SendMessage(GetHwnd(), EM_EXLIMITTEXT
, 0, len 
? len 
: 0x7fffffff); 
1707 #endif // wxUSE_RICHEDIT 
1709         if ( len 
>= 0xffff ) 
1711             // this will set it to a platform-dependent maximum (much more 
1712             // than 64Kb under NT) 
1716         ::SendMessage(GetHwnd(), EM_LIMITTEXT
, len
, 0); 
1720 // ---------------------------------------------------------------------------- 
1722 // ---------------------------------------------------------------------------- 
1724 void wxTextCtrl::Undo() 
1728         ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0); 
1732 void wxTextCtrl::Redo() 
1737         if (GetRichVersion() > 1) 
1738             ::SendMessage(GetHwnd(), EM_REDO
, 0, 0); 
1741         // Same as Undo, since Undo undoes the undo, i.e. a redo. 
1742         ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0); 
1746 bool wxTextCtrl::CanUndo() const 
1748     return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0; 
1751 bool wxTextCtrl::CanRedo() const 
1754     if (GetRichVersion() > 1) 
1755         return ::SendMessage(GetHwnd(), EM_CANREDO
, 0, 0) != 0; 
1758     return ::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0; 
1761 // ---------------------------------------------------------------------------- 
1762 // caret handling (Windows only) 
1763 // ---------------------------------------------------------------------------- 
1765 bool wxTextCtrl::ShowNativeCaret(bool show
) 
1767     if ( show 
!= m_isNativeCaretShown 
) 
1769         if ( !(show 
? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) ) 
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) 
1776         m_isNativeCaretShown 
= show
; 
1782 // ---------------------------------------------------------------------------- 
1783 // implemenation details 
1784 // ---------------------------------------------------------------------------- 
1786 void wxTextCtrl::Command(wxCommandEvent 
& event
) 
1788     SetValue(event
.GetString()); 
1789     ProcessCommand (event
); 
1792 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
) 
1794     // By default, load the first file into the text window. 
1795     if (event
.GetNumberOfFiles() > 0) 
1797         LoadFile(event
.GetFiles()[0]); 
1801 // ---------------------------------------------------------------------------- 
1802 // kbd input processing 
1803 // ---------------------------------------------------------------------------- 
1805 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
) 
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 
) 
1812         const WPARAM vkey 
= msg
->wParam
; 
1813         if ( HIWORD(msg
->lParam
) & KF_ALTDOWN 
) 
1815             // Alt-Backspace is accelerator for "Undo" 
1816             if ( vkey 
== VK_BACK 
) 
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 
1824             const int ctrl 
= wxIsCtrlDown(), 
1825                       shift 
= wxIsShiftDown(); 
1826             switch ( ctrl 
+ shift 
) 
1829                     wxFAIL_MSG( _T("how many modifiers have we got?") ); 
1833                     if ( IsMultiLine() && vkey 
== VK_RETURN 
) 
1840                     // either Ctrl or Shift pressed 
1855                     else // Shift is pressed 
1857                         if ( vkey 
== VK_INSERT 
|| vkey 
== VK_DELETE 
) 
1864     return wxControl::MSWShouldPreProcessMessage(msg
); 
1867 void wxTextCtrl::OnChar(wxKeyEvent
& event
) 
1869     switch ( event
.GetKeyCode() ) 
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
) ) 
1879                 //else: multiline controls need Enter for themselves 
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 
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
)) 
1900                 if ( FindFocus() == this ) 
1903                     if (!event
.ShiftDown()) 
1904                         flags 
|= wxNavigationKeyEvent::IsForward 
; 
1905                     if (event
.ControlDown()) 
1906                         flags 
|= wxNavigationKeyEvent::WinChange 
; 
1907                     if (Navigate(flags
)) 
1913                 // Insert tab since calling the default Windows handler 
1914                 // doesn't seem to do it 
1915                 WriteText(wxT("\t")); 
1921     // no, we didn't process it 
1925 WXLRESULT 
wxTextCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
) 
1927     WXLRESULT lRc 
= wxTextCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
); 
1929     if ( nMsg 
== WM_GETDLGCODE 
) 
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 
1934         long lDlgCode 
= DLGC_WANTCHARS 
| DLGC_WANTARROWS
; 
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 
1944             // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go 
1945             //    to the next control 
1947             // the multiline edit control should always get <Return> for itself 
1948             if ( HasFlag(wxTE_PROCESS_ENTER
) || HasFlag(wxTE_MULTILINE
) ) 
1949                 lDlgCode 
|= DLGC_WANTMESSAGE
; 
1951             if ( HasFlag(wxTE_PROCESS_TAB
) ) 
1952                 lDlgCode 
|= DLGC_WANTTAB
; 
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. 
1969 // ---------------------------------------------------------------------------- 
1970 // text control event processing 
1971 // ---------------------------------------------------------------------------- 
1973 bool wxTextCtrl::SendUpdateEvent() 
1975     switch ( m_updatesCount 
) 
1978             // remember that we've got an update 
1983             // we had already sent one event since the last control modification 
1987             wxFAIL_MSG( _T("unexpected wxTextCtrl::m_updatesCount value") ); 
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 
1996             // the control was updated programmatically and we do NOT want to 
2001     wxCommandEvent 
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId()); 
2002     InitCommandEvent(event
); 
2004     return ProcessCommand(event
); 
2007 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD 
WXUNUSED(id
)) 
2014                 wxFocusEvent 
event(param 
== EN_KILLFOCUS 
? wxEVT_KILL_FOCUS
 
2017                 event
.SetEventObject(this); 
2018                 GetEventHandler()->ProcessEvent(event
); 
2027             // the text size limit has been hit -- try to increase it 
2028             if ( !AdjustSpaceLimit() ) 
2030                 wxCommandEvent 
event(wxEVT_COMMAND_TEXT_MAXLEN
, m_windowId
); 
2031                 InitCommandEvent(event
); 
2032                 event
.SetString(GetValue()); 
2033                 ProcessCommand(event
); 
2037             // the other edit notification messages are not processed 
2046 WXHBRUSH 
wxTextCtrl::MSWControlColor(WXHDC hDC
, WXHWND hWnd
) 
2048     if ( !IsEnabled() && !HasFlag(wxTE_MULTILINE
) ) 
2049         return MSWControlColorDisabled(hDC
); 
2051     return wxTextCtrlBase::MSWControlColor(hDC
, hWnd
); 
2054 bool wxTextCtrl::HasSpaceLimit(unsigned int *len
) const 
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() 
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; 
2073 bool wxTextCtrl::AdjustSpaceLimit() 
2076     if ( HasSpaceLimit(&limit
) ) 
2079     unsigned int len 
= ::GetWindowTextLength(GetHwnd()); 
2082         // increment in 32Kb chunks 
2083         SetMaxLength(len 
+ 0x8000); 
2086     // we changed the limit 
2090 bool wxTextCtrl::AcceptsFocus() const 
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(); 
2098 wxSize 
wxTextCtrl::DoGetBestSize() const 
2101     wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont()); 
2103     int wText 
= DEFAULT_ITEM_WIDTH
; 
2106     if ( m_windowStyle 
& wxTE_MULTILINE 
) 
2108         hText 
*= wxMax(wxMin(GetNumberOfLines(), 10), 2); 
2110     //else: for single line control everything is ok 
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
; 
2116     return wxSize(wText
, hText
); 
2119 // ---------------------------------------------------------------------------- 
2120 // standard handlers for standard edit menu events 
2121 // ---------------------------------------------------------------------------- 
2123 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
)) 
2128 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
)) 
2133 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
)) 
2138 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
)) 
2143 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
)) 
2148 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
)) 
2151     GetSelection(& from
, & to
); 
2152     if (from 
!= -1 && to 
!= -1) 
2156 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
)) 
2158     SetSelection(-1, -1); 
2161 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
) 
2163     event
.Enable( CanCut() ); 
2166 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
) 
2168     event
.Enable( CanCopy() ); 
2171 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
) 
2173     event
.Enable( CanPaste() ); 
2176 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
) 
2178     event
.Enable( CanUndo() ); 
2181 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
) 
2183     event
.Enable( CanRedo() ); 
2186 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
) 
2189     GetSelection(& from
, & to
); 
2190     event
.Enable(from 
!= -1 && to 
!= -1 && from 
!= to 
&& IsEditable()) ; 
2193 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
) 
2195     event
.Enable(GetLastPosition() > 0); 
2198 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
) 
2203         if (!m_privateContextMenu
) 
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")); 
2216         PopupMenu(m_privateContextMenu
); 
2224 void wxTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
)) 
2226     // be sure the caret remains invisible if the user had hidden it 
2227     if ( !m_isNativeCaretShown 
) 
2229         ::HideCaret(GetHwnd()); 
2233 // ---------------------------------------------------------------------------- 
2234 // Default colors for MSW text control 
2236 // Set default background color to the native white instead of 
2237 // the default wxSYS_COLOUR_BTNFACE (is triggered with wxNullColour). 
2238 // ---------------------------------------------------------------------------- 
2240 wxVisualAttributes 
wxTextCtrl::GetDefaultAttributes() const 
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 
2250 // the rest of the file only deals with the rich edit controls 
2253 // ---------------------------------------------------------------------------- 
2254 // EN_LINK processing 
2255 // ---------------------------------------------------------------------------- 
2257 bool wxTextCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM 
*result
) 
2259     NMHDR 
*hdr 
= (NMHDR
* )lParam
; 
2260     switch ( hdr
->code 
) 
2264                 const MSGFILTER 
*msgf 
= (MSGFILTER 
*)lParam
; 
2265                 UINT msg 
= msgf
->msg
; 
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 
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 
) 
2279                     WXUINT flags 
= msgf
->wParam
; 
2280                     int x 
= GET_X_LPARAM(msgf
->lParam
), 
2281                         y 
= GET_Y_LPARAM(msgf
->lParam
); 
2283                     HandleMouseEvent(msg
, x
, y
, flags
); 
2287             // return true to process the event (and false to ignore it) 
2292                 const ENLINK 
*enlink 
= (ENLINK 
*)hdr
; 
2294                 switch ( enlink
->msg 
) 
2297                         // ok, so it is hardcoded - do we really nee to 
2300                             wxCursor 
cur(wxCURSOR_HAND
); 
2301                             ::SetCursor(GetHcursorOf(cur
)); 
2307                     case WM_LBUTTONDOWN
: 
2309                     case WM_LBUTTONDBLCLK
: 
2310                     case WM_RBUTTONDOWN
: 
2312                     case WM_RBUTTONDBLCLK
: 
2313                         // send a mouse event 
2315                             static const wxEventType eventsMouse
[] = 
2326                             // the event ids are consecutive 
2328                                 evtMouse(eventsMouse
[enlink
->msg 
- WM_MOUSEMOVE
]); 
2330                             InitMouseEvent(evtMouse
, 
2331                                            GET_X_LPARAM(enlink
->lParam
), 
2332                                            GET_Y_LPARAM(enlink
->lParam
), 
2335                             wxTextUrlEvent 
event(m_windowId
, evtMouse
, 
2337                                                  enlink
->chrg
.cpMax
); 
2339                             InitCommandEvent(event
); 
2341                             *result 
= ProcessCommand(event
); 
2349     // not processed, leave it to the base class 
2350     return wxTextCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
); 
2353 // ---------------------------------------------------------------------------- 
2354 // colour setting for the rich edit controls 
2355 // ---------------------------------------------------------------------------- 
2357 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
) 
2359     if ( !wxTextCtrlBase::SetBackgroundColour(colour
) ) 
2361         // colour didn't really change 
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
)); 
2375 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
) 
2377     if ( !wxTextCtrlBase::SetForegroundColour(colour
) ) 
2379         // colour didn't really change 
2385         // change the colour of everything 
2388         cf
.cbSize 
= sizeof(cf
); 
2389         cf
.dwMask 
= CFM_COLOR
; 
2390         cf
.crTextColor 
= wxColourToRGB(colour
); 
2391         ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, SCF_ALL
, (LPARAM
)&cf
); 
2397 // ---------------------------------------------------------------------------- 
2398 // styling support for rich edit controls 
2399 // ---------------------------------------------------------------------------- 
2403 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
) 
2407         // can't do it with normal text control 
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() && 
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(); 
2422     // order the range if needed 
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
); 
2435     // but do we really have to change the selection? 
2436     bool changeSel 
= start 
!= startOld 
|| end 
!= endOld
; 
2440         DoSetSelection(start
, end
, false /* don't scroll caret into view */); 
2443     // initialize CHARFORMAT struct 
2452     // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple 
2453     // CHARFORMAT in that case 
2455     if ( m_verRichEdit 
== 1 ) 
2457         // this is the only thing the control is going to grok 
2458         cf
.cbSize 
= sizeof(CHARFORMAT
); 
2463         // CHARFORMAT or CHARFORMAT2 
2464         cf
.cbSize 
= sizeof(cf
); 
2467     if ( style
.HasFont() ) 
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 
2472         cf
.dwMask 
|= CFM_FACE 
| CFM_SIZE 
| CFM_CHARSET 
| 
2473                      CFM_ITALIC 
| CFM_BOLD 
| CFM_UNDERLINE
; 
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) 
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
) ); 
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 
2491             cf
.dwEffects 
|= CFE_ITALIC
; 
2494         if ( lf
.lfWeight 
== FW_BOLD 
) 
2496             cf
.dwEffects 
|= CFE_BOLD
; 
2499         if ( lf
.lfUnderline 
) 
2501             cf
.dwEffects 
|= CFE_UNDERLINE
; 
2504         // strikeout fonts are not supported by wxWidgets 
2507     if ( style
.HasTextColour() ) 
2509         cf
.dwMask 
|= CFM_COLOR
; 
2510         cf
.crTextColor 
= wxColourToRGB(style
.GetTextColour()); 
2514     if ( m_verRichEdit 
!= 1 && style
.HasBackgroundColour() ) 
2516         cf
.dwMask 
|= CFM_BACKCOLOR
; 
2517         cf
.crBackColor 
= wxColourToRGB(style
.GetBackgroundColour()); 
2519 #endif // wxUSE_RICHEDIT2 
2521     // do format the selection 
2522     bool ok 
= ::SendMessage(GetHwnd(), EM_SETCHARFORMAT
, 
2523                             SCF_SELECTION
, (LPARAM
)&cf
) != 0; 
2526         wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed")); 
2529     // now do the paragraph formatting 
2532     // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple 
2533     // PARAFORMAT in that case 
2535     if ( m_verRichEdit 
== 1 ) 
2537         // this is the only thing the control is going to grok 
2538         pf
.cbSize 
= sizeof(PARAFORMAT
); 
2543         // PARAFORMAT or PARAFORMAT2 
2544         pf
.cbSize 
= sizeof(pf
); 
2547     if (style
.HasAlignment()) 
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
; 
2557             pf
.wAlignment 
= PFA_LEFT
; 
2560     if (style
.HasLeftIndent()) 
2562         pf
.dwMask 
|= PFM_STARTINDENT 
| PFM_OFFSET
; 
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) ; 
2569     if (style
.HasRightIndent()) 
2571         pf
.dwMask 
|= PFM_RIGHTINDENT
; 
2573         // Convert from 1/10 mm to TWIPS 
2574         pf
.dxRightIndent 
= (int) (((double) style
.GetRightIndent()) * mm2twips 
/ 10.0) ; 
2577     if (style
.HasTabs()) 
2579         pf
.dwMask 
|= PFM_TABSTOPS
; 
2581         const wxArrayInt
& tabs 
= style
.GetTabs(); 
2583         pf
.cTabCount 
= (SHORT
)wxMin(tabs
.GetCount(), MAX_TAB_STOPS
); 
2585         for (i 
= 0; i 
< (size_t) pf
.cTabCount
; i
++) 
2587             // Convert from 1/10 mm to TWIPS 
2588             pf
.rgxTabs
[i
] = (int) (((double) tabs
[i
]) * mm2twips 
/ 10.0) ; 
2593     if ( m_verRichEdit 
> 1 ) 
2595         if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft 
) 
2597             // Use RTL paragraphs in RTL mode to get proper layout 
2598             pf
.dwMask 
|= PFM_RTLPARA
; 
2599             pf
.wEffects 
|= PFE_RTLPARA
; 
2602 #endif // wxUSE_RICHEDIT2 
2606         // do format the selection 
2607         bool ok 
= ::SendMessage(GetHwnd(), EM_SETPARAFORMAT
, 
2608                                 0, (LPARAM
) &pf
) != 0; 
2611             wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed")); 
2617         // restore the original selection 
2618         DoSetSelection(startOld
, endOld
, false); 
2624 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
) 
2626     if ( !wxTextCtrlBase::SetDefaultStyle(style
) ) 
2631         // we have to do this or the style wouldn't apply for the text typed by 
2633         wxTextPos posLast 
= GetLastPosition(); 
2634         SetStyle(posLast
, posLast
, m_defaultStyle
); 
2640 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
) 
2644         // can't do it with normal text control 
2648     // initialize CHARFORMAT struct 
2657     // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple 
2658     // CHARFORMAT in that case 
2660     if ( m_verRichEdit 
== 1 ) 
2662         // this is the only thing the control is going to grok 
2663         cf
.cbSize 
= sizeof(CHARFORMAT
); 
2668         // CHARFORMAT or CHARFORMAT2 
2669         cf
.cbSize 
= sizeof(cf
); 
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
); 
2676     // but do we really have to change the selection? 
2677     bool changeSel 
= position 
!= startOld 
|| position 
!= endOld
; 
2681         DoSetSelection(position
, position
+1, false /* don't scroll caret into view */); 
2684     // get the selection formatting 
2685     (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT
, 
2686                             SCF_SELECTION
, (LPARAM
)&cf
) ; 
2690     lf
.lfHeight 
= cf
.yHeight
; 
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
); 
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
) 
2702         lf
.lfItalic 
= FALSE
; 
2704     lf
.lfOrientation 
= 0; 
2705     lf
.lfPitchAndFamily 
= cf
.bPitchAndFamily
; 
2708     if (cf
.dwEffects 
& CFE_STRIKEOUT
) 
2709         lf
.lfStrikeOut 
= TRUE
; 
2711         lf
.lfStrikeOut 
= FALSE
; 
2713     if (cf
.dwEffects 
& CFE_UNDERLINE
) 
2714         lf
.lfUnderline 
= TRUE
; 
2716         lf
.lfUnderline 
= FALSE
; 
2718     if (cf
.dwEffects 
& CFE_BOLD
) 
2719         lf
.lfWeight 
= FW_BOLD
; 
2721         lf
.lfWeight 
= FW_NORMAL
; 
2723     wxFont font 
= wxCreateFontFromLogFont(& lf
); 
2726         style
.SetFont(font
); 
2728     style
.SetTextColour(wxColour(cf
.crTextColor
)); 
2731     if ( m_verRichEdit 
!= 1 ) 
2733         // cf.dwMask |= CFM_BACKCOLOR; 
2734         style
.SetBackgroundColour(wxColour(cf
.crBackColor
)); 
2736 #endif // wxUSE_RICHEDIT2 
2738     // now get the paragraph formatting 
2741     // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple 
2742     // PARAFORMAT in that case 
2744     if ( m_verRichEdit 
== 1 ) 
2746         // this is the only thing the control is going to grok 
2747         pf
.cbSize 
= sizeof(PARAFORMAT
); 
2752         // PARAFORMAT or PARAFORMAT2 
2753         pf
.cbSize 
= sizeof(pf
); 
2756     // do format the selection 
2757     (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT
, 0, (LPARAM
) &pf
) ; 
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) ); 
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
); 
2769         style
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
); 
2771     wxArrayInt tabStops
; 
2773     for (i 
= 0; i 
< (size_t) pf
.cTabCount
; i
++) 
2775         tabStops
.Add( (int) ((double) (pf
.rgxTabs
[i
] & 0xFFFF) * twips2mm 
* 10.0) ); 
2780         // restore the original selection 
2781         DoSetSelection(startOld
, endOld
, false); 
2789 // ---------------------------------------------------------------------------- 
2791 // ---------------------------------------------------------------------------- 
2793 static const HINSTANCE INVALID_HINSTANCE 
= (HINSTANCE
)-1; 
2795 bool wxRichEditModule::OnInit() 
2797     // don't do anything - we will load it when needed 
2801 void wxRichEditModule::OnExit() 
2803     for ( size_t i 
= 0; i 
< WXSIZEOF(ms_hRichEdit
); i
++ ) 
2805         if ( ms_hRichEdit
[i
] && ms_hRichEdit
[i
] != INVALID_HINSTANCE 
) 
2807             ::FreeLibrary(ms_hRichEdit
[i
]); 
2808             ms_hRichEdit
[i
] = NULL
; 
2812     if (ms_inkEditLib
.IsLoaded()) 
2813         ms_inkEditLib
.Unload(); 
2818 bool wxRichEditModule::Load(Version version
) 
2820     if ( ms_hRichEdit
[version
] == INVALID_HINSTANCE 
) 
2822         // we had already tried to load it and failed 
2826     if ( ms_hRichEdit
[version
] ) 
2828         // we've already got this one 
2832     static const wxChar 
*dllnames
[] = 
2839     wxCOMPILE_TIME_ASSERT( WXSIZEOF(dllnames
) == Version_Max
, 
2840                             RichEditDllNamesVersionsMismatch 
); 
2842     ms_hRichEdit
[version
] = ::LoadLibrary(dllnames
[version
]); 
2844     if ( !ms_hRichEdit
[version
] ) 
2846         ms_hRichEdit
[version
] = INVALID_HINSTANCE
; 
2855 // load the InkEdit library 
2856 bool wxRichEditModule::LoadInkEdit() 
2858     static wxDynamicLibrary ms_inkEditLib
; 
2859     static bool             ms_inkEditLibLoadAttemped
; 
2860     if (ms_inkEditLibLoadAttemped
) 
2861         ms_inkEditLib
.IsLoaded(); 
2863     ms_inkEditLibLoadAttemped 
= true; 
2866     return ms_inkEditLib
.Load(wxT("inked")); 
2871 #endif // wxUSE_RICHEDIT 
2873 #endif // wxUSE_TEXTCTRL && !(__SMARTPHONE__ && __WXWINCE__)