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