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