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