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