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