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