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