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