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