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