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