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