]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
fixed wxComboBox sizing problem again
[wxWidgets.git] / src / msw / textctrl.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
bfbd6dc1 2// Name: msw/textctrl.cpp
2bda0e17
KB
3// Purpose: wxTextCtrl
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
c085e333 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
a1b82138
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
2bda0e17 16#ifdef __GNUG__
a1b82138 17 #pragma implementation "textctrl.h"
2bda0e17
KB
18#endif
19
a1b82138
VZ
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
2bda0e17
KB
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
a1b82138 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
a1b82138
VZ
32 #include "wx/textctrl.h"
33 #include "wx/settings.h"
34 #include "wx/brush.h"
35 #include "wx/utils.h"
381dd4bf 36 #include "wx/intl.h"
a1b82138 37 #include "wx/log.h"
381dd4bf 38 #include "wx/app.h"
2bda0e17
KB
39#endif
40
f6bcfd97
BP
41#include "wx/module.h"
42
47d67540 43#if wxUSE_CLIPBOARD
a1b82138 44 #include "wx/clipbrd.h"
2bda0e17
KB
45#endif
46
a1b82138
VZ
47#include "wx/textfile.h"
48
49#include <windowsx.h>
50
2bda0e17
KB
51#include "wx/msw/private.h"
52
a1b82138 53#include <string.h>
2bda0e17 54#include <stdlib.h>
a1b82138 55#include <sys/types.h>
fbc535ff
JS
56
57#if wxUSE_IOSTREAMH
3f4a0c5b 58# include <fstream.h>
fbc535ff 59#else
3f4a0c5b 60# include <fstream>
fbc535ff 61#endif
2bda0e17 62
c42404a5 63#if wxUSE_RICHEDIT && !defined(__GNUWIN32_OLD__)
cd471848 64 #include <richedit.h>
2bda0e17
KB
65#endif
66
b12915c1
VZ
67// ----------------------------------------------------------------------------
68// private classes
69// ----------------------------------------------------------------------------
70
71#if wxUSE_RICHEDIT
72
73// this module initializes RichEdit DLL if needed
74class wxRichEditModule : public wxModule
75{
76public:
77 virtual bool OnInit();
78 virtual void OnExit();
79
80 // get the version currently loaded, -1 if none
81 static int GetLoadedVersion() { return ms_verRichEdit; }
82
83 // load the richedit DLL of at least of required version
84 static bool Load(int version = 1);
85
86private:
87 // the handle to richedit DLL and the version of the DLL loaded
88 static HINSTANCE ms_hRichEdit;
89
90 // the DLL version loaded or -1 if none
91 static int ms_verRichEdit;
92
93 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
94};
95
96HINSTANCE wxRichEditModule::ms_hRichEdit = (HINSTANCE)NULL;
97int wxRichEditModule::ms_verRichEdit = -1;
98
99IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
100
101#endif // wxUSE_RICHEDIT
e702ff0f 102
a1b82138
VZ
103// ----------------------------------------------------------------------------
104// event tables and other macros
105// ----------------------------------------------------------------------------
106
2bda0e17
KB
107IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
108
109BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
a1b82138
VZ
110 EVT_CHAR(wxTextCtrl::OnChar)
111 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
112
113 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
114 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
115 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
116 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
117 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
118
119 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
120 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
121 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
122 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
123 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
f6bcfd97
BP
124#ifdef __WIN16__
125 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
126#endif
2bda0e17 127END_EVENT_TABLE()
e702ff0f 128
2bda0e17 129
a1b82138
VZ
130// ============================================================================
131// implementation
132// ============================================================================
133
134// ----------------------------------------------------------------------------
135// creation
136// ----------------------------------------------------------------------------
137
cd471848 138wxTextCtrl::wxTextCtrl()
2bda0e17 139{
cd471848 140#if wxUSE_RICHEDIT
a1b82138 141 m_isRich = FALSE;
cd471848 142#endif
2bda0e17
KB
143}
144
debe6624 145bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
146 const wxString& value,
147 const wxPoint& pos,
a1b82138
VZ
148 const wxSize& size,
149 long style,
c085e333
VZ
150 const wxValidator& validator,
151 const wxString& name)
2bda0e17 152{
a1b82138 153 // base initialization
8d99be5f 154 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
a1b82138 155 return FALSE;
2bda0e17 156
a1b82138
VZ
157 if ( parent )
158 parent->AddChild(this);
2bda0e17 159
a1b82138
VZ
160 // translate wxWin style flags to MSW ones, checking for consistency while
161 // doing it
b0766406
JS
162 long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
163
164 if ( m_windowStyle & wxCLIP_SIBLINGS )
165 msStyle |= WS_CLIPSIBLINGS;
166
a1b82138
VZ
167 if ( m_windowStyle & wxTE_MULTILINE )
168 {
169 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
f6bcfd97 170 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
5fb9fcfc 171
b70ababc
JS
172 msStyle |= ES_MULTILINE | ES_WANTRETURN;
173 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
174 msStyle |= WS_VSCROLL;
a1b82138
VZ
175 m_windowStyle |= wxTE_PROCESS_ENTER;
176 }
177 else
178 msStyle |= ES_AUTOHSCROLL;
2bda0e17 179
2c738dd8
UM
180 if (m_windowStyle & wxHSCROLL)
181 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
182
a1b82138
VZ
183 if (m_windowStyle & wxTE_READONLY)
184 msStyle |= ES_READONLY;
2bda0e17 185
a1b82138
VZ
186 if (m_windowStyle & wxTE_PASSWORD) // hidden input
187 msStyle |= ES_PASSWORD;
2bda0e17 188
cfdd3a98
UM
189 if (m_windowStyle & wxTE_AUTO_SCROLL)
190 msStyle |= ES_AUTOHSCROLL;
191
192
101f488c
VZ
193 // we always want the characters and the arrows
194 m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
195
196 // we may have several different cases:
197 // 1. normal case: both TAB and ENTER are used for dialog navigation
198 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
199 // control in the dialog
200 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
201 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
202 // the next control
203 if ( m_windowStyle & wxTE_PROCESS_ENTER )
204 m_lDlgCode |= DLGC_WANTMESSAGE;
205 if ( m_windowStyle & wxTE_PROCESS_TAB )
206 m_lDlgCode |= DLGC_WANTTAB;
207
a1b82138 208 // do create the control - either an EDIT or RICHEDIT
b12915c1 209 wxString windowClass = wxT("EDIT");
cd471848 210
57c208c5 211#if wxUSE_RICHEDIT
c49245f8 212 if ( m_windowStyle & wxTE_RICH )
a1b82138 213 {
0d0512bd
VZ
214 static bool s_errorGiven = FALSE; // MT-FIXME
215
216 // only give the error msg once if the DLL can't be loaded
217 if ( !s_errorGiven )
218 {
219 // first try to load the RichEdit DLL (will do nothing if already
220 // done)
b12915c1 221 if ( !wxRichEditModule::Load() )
0d0512bd 222 {
f6bcfd97 223 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
0d0512bd
VZ
224
225 s_errorGiven = TRUE;
226 }
227 }
228
229 if ( s_errorGiven )
230 {
231 m_isRich = FALSE;
232 }
233 else
234 {
235 msStyle |= ES_AUTOVSCROLL;
236 m_isRich = TRUE;
b12915c1
VZ
237
238 int ver = wxRichEditModule::GetLoadedVersion();
239 if ( ver == 1 )
240 {
241 windowClass = wxT("RICHEDIT");
242 }
243 else
244 {
245#ifndef RICHEDIT_CLASS
246 wxString RICHEDIT_CLASS;
247 RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), ver);
5fb2f4ba 248#if wxUSE_UNICODE
b12915c1
VZ
249 RICHEDIT_CLASS += _T('W');
250#else // ANSI
251 RICHEDIT_CLASS += _T('A');
252#endif // Unicode/ANSI
253#endif // !RICHEDIT_CLASS
254
255 windowClass = RICHEDIT_CLASS;
256 }
0d0512bd 257 }
a1b82138
VZ
258 }
259 else
260 m_isRich = FALSE;
b12915c1 261#endif // wxUSE_RICHEDIT
2bda0e17 262
a1b82138
VZ
263 bool want3D;
264 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
2bda0e17 265
a1b82138
VZ
266 // Even with extended styles, need to combine with WS_BORDER for them to
267 // look right.
268 if ( want3D || wxStyleHasBorder(m_windowStyle) )
269 msStyle |= WS_BORDER;
2bda0e17 270
a1b82138
VZ
271 // NB: don't use pos and size as CreateWindowEx arguments because they
272 // might be -1 in which case we should use the default values (and
273 // SetSize called below takes care of it)
274 m_hWnd = (WXHWND)::CreateWindowEx(exStyle,
5fb2f4ba 275 windowClass.c_str(),
a1b82138
VZ
276 NULL,
277 msStyle,
278 0, 0, 0, 0,
279 GetHwndOf(parent),
280 (HMENU)m_windowId,
281 wxGetInstance(),
282 NULL);
2bda0e17 283
223d09f6 284 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create text ctrl") );
c085e333 285
1f112209 286#if wxUSE_CTL3D
a1b82138
VZ
287 if ( want3D )
288 {
289 Ctl3dSubclassCtl(GetHwnd());
290 m_useCtl3D = TRUE;
291 }
2bda0e17
KB
292#endif
293
57c208c5 294#if wxUSE_RICHEDIT
a1b82138
VZ
295 if (m_isRich)
296 {
297 // Have to enable events
298 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0,
299 ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE);
300 }
2bda0e17
KB
301#endif
302
a1b82138 303 SubclassWin(GetHWND());
2bda0e17 304
a1b82138
VZ
305 // set font, position, size and initial value
306 wxFont& fontParent = parent->GetFont();
307 if ( fontParent.Ok() )
308 {
309 SetFont(fontParent);
310 }
311 else
312 {
313 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT));
314 }
2bda0e17 315
a1b82138 316 // Causes a crash for Symantec C++ and WIN32 for some reason
2bda0e17 317#if !(defined(__SC__) && defined(__WIN32__))
a1b82138
VZ
318 if ( !value.IsEmpty() )
319 {
320 SetValue(value);
321 }
2bda0e17
KB
322#endif
323
d47ebd1e
JS
324 // set colours
325 SetupColours();
326
a1b82138
VZ
327 SetSize(pos.x, pos.y, size.x, size.y);
328
329 return TRUE;
2bda0e17
KB
330}
331
332// Make sure the window style (etc.) reflects the HWND style (roughly)
cd471848 333void wxTextCtrl::AdoptAttributesFromHWND()
2bda0e17 334{
c085e333 335 wxWindow::AdoptAttributesFromHWND();
2bda0e17 336
789295bf 337 HWND hWnd = GetHwnd();
a1b82138 338 long style = GetWindowLong(hWnd, GWL_STYLE);
2bda0e17 339
cd471848
VZ
340 // retrieve the style to see whether this is an edit or richedit ctrl
341#if wxUSE_RICHEDIT
837e5743 342 wxChar buf[256];
2bda0e17 343
a1b82138 344 GetClassName(hWnd, buf, WXSIZEOF(buf));
2bda0e17 345
223d09f6 346 if ( wxStricmp(buf, wxT("EDIT")) == 0 )
c085e333
VZ
347 m_isRich = FALSE;
348 else
349 m_isRich = TRUE;
a1b82138 350#endif // wxUSE_RICHEDIT
c085e333
VZ
351
352 if (style & ES_MULTILINE)
353 m_windowStyle |= wxTE_MULTILINE;
354 if (style & ES_PASSWORD)
355 m_windowStyle |= wxTE_PASSWORD;
356 if (style & ES_READONLY)
357 m_windowStyle |= wxTE_READONLY;
358 if (style & ES_WANTRETURN)
359 m_windowStyle |= wxTE_PROCESS_ENTER;
2bda0e17
KB
360}
361
cd471848 362void wxTextCtrl::SetupColours()
2bda0e17 363{
d47ebd1e 364 wxColour bkgndColour;
f6bcfd97 365// if (IsEditable() || (m_windowStyle & wxTE_MULTILINE))
d47ebd1e 366 bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW);
f6bcfd97
BP
367// else
368// bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
d47ebd1e
JS
369
370 SetBackgroundColour(bkgndColour);
a1b82138 371 SetForegroundColour(GetParent()->GetForegroundColour());
2bda0e17
KB
372}
373
a1b82138
VZ
374// ----------------------------------------------------------------------------
375// set/get the controls text
376// ----------------------------------------------------------------------------
377
cd471848 378wxString wxTextCtrl::GetValue() const
2bda0e17 379{
b12915c1
VZ
380 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
381 // retrieving more than 64Kb under Win9x
382#if wxUSE_RICHEDIT
383 if ( m_isRich )
384 {
5fb2f4ba 385 wxString str;
b12915c1 386
3988b155
VZ
387 int len = GetWindowTextLength(GetHwnd());
388 if ( len )
389 {
390 // alloc one extra WORD as needed by the control
391 wxChar *p = str.GetWriteBuf(++len);
b12915c1 392
3988b155
VZ
393 TEXTRANGE textRange;
394 textRange.chrg.cpMin = 0;
395 textRange.chrg.cpMax = -1;
396 textRange.lpstrText = p;
b12915c1 397
3988b155 398 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
b12915c1 399
3988b155
VZ
400 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
401 // the newlines which is neither Unix nor Windows style (Win95 with
402 // riched20.dll shows this behaviour) - convert it to something
403 // reasonable
404 for ( ; *p; p++ )
405 {
406 if ( *p == _T('\r') )
407 *p = _T('\n');
408 }
409
410 str.UngetWriteBuf();
411 }
412 //else: no text at all, leave the string empty
b12915c1
VZ
413
414 return str;
415 }
416#endif // wxUSE_RICHEDIT
417
418 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
419 // same one as above for consitency
420 wxString str = wxGetWindowText(GetHWND());
421
422 return wxTextFile::Translate(str, wxTextFileType_Unix);
2bda0e17
KB
423}
424
425void wxTextCtrl::SetValue(const wxString& value)
426{
b12915c1
VZ
427 // if the text is long enough, it's faster to just set it instead of first
428 // comparing it with the old one (chances are that it will be different
429 // anyhow, this comparison is there to avoid flicker for small single-line
430 // edit controls mostly)
431 if ( (value.length() > 0x400) || (value != GetValue()) )
07cf98cb 432 {
b12915c1 433 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
c4608a8a 434
5fb2f4ba 435 SetWindowText(GetHwnd(), valueDos.c_str());
a1b82138 436
f6bcfd97
BP
437 // for compatibility with the GTK and because it is more logical, we
438 // move the cursor to the end of the text after SetValue()
439
440 // GRG, Jun/2000: Changed this back after a lot of discussion
441 // in the lists. wxWindows 2.2 will have a set of flags to
442 // customize this behaviour.
443 //SetInsertionPointEnd();
444
07cf98cb
VZ
445 AdjustSpaceLimit();
446 }
2bda0e17
KB
447}
448
a1b82138 449void wxTextCtrl::WriteText(const wxString& value)
2bda0e17 450{
a1b82138 451 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
2bda0e17 452
a1b82138 453 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
2bda0e17 454
a1b82138 455 AdjustSpaceLimit();
2bda0e17
KB
456}
457
a1b82138
VZ
458void wxTextCtrl::AppendText(const wxString& text)
459{
460 SetInsertionPointEnd();
461 WriteText(text);
462}
463
464void wxTextCtrl::Clear()
465{
223d09f6 466 SetWindowText(GetHwnd(), wxT(""));
a1b82138
VZ
467}
468
469// ----------------------------------------------------------------------------
2bda0e17 470// Clipboard operations
a1b82138
VZ
471// ----------------------------------------------------------------------------
472
cd471848 473void wxTextCtrl::Copy()
2bda0e17 474{
e702ff0f
JS
475 if (CanCopy())
476 {
789295bf 477 HWND hWnd = GetHwnd();
e702ff0f
JS
478 SendMessage(hWnd, WM_COPY, 0, 0L);
479 }
2bda0e17
KB
480}
481
cd471848 482void wxTextCtrl::Cut()
2bda0e17 483{
e702ff0f
JS
484 if (CanCut())
485 {
789295bf 486 HWND hWnd = GetHwnd();
e702ff0f
JS
487 SendMessage(hWnd, WM_CUT, 0, 0L);
488 }
2bda0e17
KB
489}
490
cd471848 491void wxTextCtrl::Paste()
2bda0e17 492{
e702ff0f
JS
493 if (CanPaste())
494 {
789295bf 495 HWND hWnd = GetHwnd();
e702ff0f
JS
496 SendMessage(hWnd, WM_PASTE, 0, 0L);
497 }
2bda0e17
KB
498}
499
a1b82138
VZ
500bool wxTextCtrl::CanCopy() const
501{
502 // Can copy if there's a selection
503 long from, to;
504 GetSelection(& from, & to);
dbf28859 505 return (from != to) ;
a1b82138
VZ
506}
507
508bool wxTextCtrl::CanCut() const
509{
510 // Can cut if there's a selection
511 long from, to;
512 GetSelection(& from, & to);
dbf28859 513 return (from != to) && (IsEditable());
a1b82138
VZ
514}
515
516bool wxTextCtrl::CanPaste() const
517{
518#if wxUSE_RICHEDIT
519 if (m_isRich)
520 {
521 int dataFormat = 0; // 0 == any format
522 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
523 }
524#endif
525 if (!IsEditable())
526 return FALSE;
527
528 // Standard edit control: check for straight text on clipboard
529 bool isTextAvailable = FALSE;
530 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
531 {
532 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
533 ::CloseClipboard();
534 }
535
536 return isTextAvailable;
537}
538
539// ----------------------------------------------------------------------------
540// Accessors
541// ----------------------------------------------------------------------------
542
debe6624 543void wxTextCtrl::SetEditable(bool editable)
2bda0e17 544{
a1b82138
VZ
545 HWND hWnd = GetHwnd();
546 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
2bda0e17
KB
547}
548
debe6624 549void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 550{
a1b82138 551 HWND hWnd = GetHwnd();
2bda0e17 552#ifdef __WIN32__
57c208c5 553#if wxUSE_RICHEDIT
a1b82138
VZ
554 if ( m_isRich)
555 {
556 CHARRANGE range;
557 range.cpMin = pos;
558 range.cpMax = pos;
559 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
560 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
561 }
562 else
563#endif // wxUSE_RICHEDIT
564 {
565 SendMessage(hWnd, EM_SETSEL, pos, pos);
566 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
567 }
568#else // Win16
569 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
570#endif // Win32/16
571
c4608a8a 572 static const wxChar *nothing = _T("");
a1b82138 573 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
2bda0e17
KB
574}
575
cd471848 576void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 577{
a1b82138
VZ
578 long pos = GetLastPosition();
579 SetInsertionPoint(pos);
2bda0e17
KB
580}
581
cd471848 582long wxTextCtrl::GetInsertionPoint() const
2bda0e17 583{
57c208c5 584#if wxUSE_RICHEDIT
a1b82138
VZ
585 if (m_isRich)
586 {
587 CHARRANGE range;
588 range.cpMin = 0;
589 range.cpMax = 0;
590 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
591 return range.cpMin;
592 }
2bda0e17
KB
593#endif
594
a1b82138
VZ
595 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
596 return Pos & 0xFFFF;
2bda0e17
KB
597}
598
cd471848 599long wxTextCtrl::GetLastPosition() const
2bda0e17 600{
789295bf 601 HWND hWnd = GetHwnd();
2bda0e17
KB
602
603 // Will always return a number > 0 (according to docs)
604 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
605
606 // This gets the char index for the _beginning_ of the last line
607 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
39136494 608
2bda0e17
KB
609 // Get number of characters in the last line. We'll add this to the character
610 // index for the last line, 1st position.
611 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
612
613 return (long)(charIndex + lineLength);
614}
615
a1b82138
VZ
616// If the return values from and to are the same, there is no
617// selection.
618void wxTextCtrl::GetSelection(long* from, long* to) const
619{
620#if wxUSE_RICHEDIT
621 if (m_isRich)
622 {
623 CHARRANGE charRange;
624 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
625
626 *from = charRange.cpMin;
627 *to = charRange.cpMax;
628
629 return;
630 }
631#endif
632 DWORD dwStart, dwEnd;
633 WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position
634 LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position
635
636 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
637
638 *from = dwStart;
639 *to = dwEnd;
640}
641
642bool wxTextCtrl::IsEditable() const
643{
644 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
645
646 return ((style & ES_READONLY) == 0);
647}
648
649// ----------------------------------------------------------------------------
650// Editing
651// ----------------------------------------------------------------------------
652
debe6624 653void wxTextCtrl::Replace(long from, long to, const wxString& value)
2bda0e17 654{
acbd13a3 655#if wxUSE_CLIPBOARD
789295bf 656 HWND hWnd = GetHwnd();
2bda0e17
KB
657 long fromChar = from;
658 long toChar = to;
39136494 659
2bda0e17
KB
660 // Set selection and remove it
661#ifdef __WIN32__
662 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
663#else
664 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
665#endif
666 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
667
668 // Now replace with 'value', by pasting.
837e5743 669 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
2bda0e17
KB
670
671 // Paste into edit control
672 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
acbd13a3
JS
673#else
674 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
675#endif
2bda0e17
KB
676}
677
debe6624 678void wxTextCtrl::Remove(long from, long to)
2bda0e17 679{
789295bf 680 HWND hWnd = GetHwnd();
2bda0e17
KB
681 long fromChar = from;
682 long toChar = to;
39136494 683
2bda0e17
KB
684 // Cut all selected text
685#ifdef __WIN32__
686 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
687#else
688 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
689#endif
690 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
691}
692
debe6624 693void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 694{
789295bf 695 HWND hWnd = GetHwnd();
2bda0e17
KB
696 long fromChar = from;
697 long toChar = to;
a1b82138
VZ
698
699 // if from and to are both -1, it means (in wxWindows) that all text should
700 // be selected. Translate into Windows convention
2bda0e17
KB
701 if ((from == -1) && (to == -1))
702 {
703 fromChar = 0;
704 toChar = -1;
705 }
39136494 706
2bda0e17
KB
707#ifdef __WIN32__
708 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
709 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
710#else
711 // WPARAM is 0: selection is scrolled into view
712 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
713#endif
714}
715
716bool wxTextCtrl::LoadFile(const wxString& file)
717{
a1b82138 718 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 719 {
a1b82138
VZ
720 // update the size limit if needed
721 AdjustSpaceLimit();
2bda0e17 722
a1b82138 723 return TRUE;
2bda0e17 724 }
2bda0e17 725
a1b82138 726 return FALSE;
2bda0e17
KB
727}
728
cd471848 729bool wxTextCtrl::IsModified() const
2bda0e17 730{
789295bf 731 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
2bda0e17
KB
732}
733
734// Makes 'unmodified'
cd471848 735void wxTextCtrl::DiscardEdits()
2bda0e17 736{
a1b82138 737 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
738}
739
cd471848 740int wxTextCtrl::GetNumberOfLines() const
2bda0e17 741{
789295bf 742 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
743}
744
debe6624 745long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 746{
789295bf 747 HWND hWnd = GetHwnd();
2bda0e17
KB
748
749 // This gets the char index for the _beginning_ of this line
750 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
751 return (long)(x + charIndex);
752}
753
0efe5ba7 754bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 755{
789295bf 756 HWND hWnd = GetHwnd();
2bda0e17
KB
757
758 // This gets the line number containing the character
0efe5ba7
VZ
759 int lineNo;
760#if wxUSE_RICHEDIT
761 if ( m_isRich )
762 {
763 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
764 }
765 else
766#endif // wxUSE_RICHEDIT
767 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
768
769 if ( lineNo == -1 )
770 {
771 // no such line
772 return FALSE;
773 }
774
2bda0e17
KB
775 // This gets the char index for the _beginning_ of this line
776 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
777 if ( charIndex == -1 )
778 {
779 return FALSE;
780 }
781
2bda0e17 782 // The X position must therefore be the different between pos and charIndex
0efe5ba7
VZ
783 if ( x )
784 *x = (long)(pos - charIndex);
785 if ( y )
786 *y = (long)lineNo;
787
788 return TRUE;
2bda0e17
KB
789}
790
debe6624 791void wxTextCtrl::ShowPosition(long pos)
2bda0e17 792{
789295bf 793 HWND hWnd = GetHwnd();
2bda0e17
KB
794
795 // To scroll to a position, we pass the number of lines and characters
796 // to scroll *by*. This means that we need to:
797 // (1) Find the line position of the current line.
798 // (2) Find the line position of pos.
799 // (3) Scroll by (pos - current).
800 // For now, ignore the horizontal scrolling.
801
802 // Is this where scrolling is relative to - the line containing the caret?
803 // Or is the first visible line??? Try first visible line.
804// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
805
806 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
807
808 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 809
2bda0e17
KB
810 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
811
2bda0e17 812 if (linesToScroll != 0)
de4d7713 813 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
814}
815
debe6624 816int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
817{
818 long charIndex = XYToPosition(0, lineNo);
4438caf4 819 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
2bda0e17
KB
820 return len;
821}
822
debe6624 823wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 824{
a1b82138 825 size_t len = (size_t)GetLineLength(lineNo) + 1;
488fe1fe 826
f6bcfd97
BP
827 // there must be at least enough place for the length WORD in the
828 // buffer
829 len += sizeof(WORD);
4438caf4 830
f6bcfd97
BP
831 wxString str;
832 wxChar *buf = str.GetWriteBuf(len);
833
33ac7e6f 834 *(WORD *)buf = (WORD)len;
f6bcfd97
BP
835 len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
836 buf[len] = 0;
4438caf4 837
f6bcfd97 838 str.UngetWriteBuf(len);
4438caf4
VZ
839
840 return str;
2bda0e17
KB
841}
842
a1b82138 843// ----------------------------------------------------------------------------
ca8b28f2 844// Undo/redo
a1b82138
VZ
845// ----------------------------------------------------------------------------
846
ca8b28f2
JS
847void wxTextCtrl::Undo()
848{
849 if (CanUndo())
850 {
789295bf 851 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
852 }
853}
854
855void wxTextCtrl::Redo()
856{
857 if (CanRedo())
858 {
859 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 860 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
861 }
862}
863
864bool wxTextCtrl::CanUndo() const
865{
789295bf 866 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
867}
868
869bool wxTextCtrl::CanRedo() const
870{
789295bf 871 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
872}
873
a1b82138
VZ
874// ----------------------------------------------------------------------------
875// implemenation details
876// ----------------------------------------------------------------------------
39136494 877
2bda0e17
KB
878void wxTextCtrl::Command(wxCommandEvent & event)
879{
a1b82138
VZ
880 SetValue(event.GetString());
881 ProcessCommand (event);
2bda0e17
KB
882}
883
884void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
885{
a1b82138
VZ
886 // By default, load the first file into the text window.
887 if (event.GetNumberOfFiles() > 0)
888 {
889 LoadFile(event.GetFiles()[0]);
890 }
2bda0e17
KB
891}
892
2bda0e17
KB
893void wxTextCtrl::OnChar(wxKeyEvent& event)
894{
42e69d6b 895 switch ( event.KeyCode() )
cd471848 896 {
cd471848 897 case WXK_RETURN:
39136494 898 if ( !(m_windowStyle & wxTE_MULTILINE) )
cd471848
VZ
899 {
900 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
bfbd6dc1 901 InitCommandEvent(event);
f6bcfd97 902 event.SetString(GetValue());
cd471848
VZ
903 if ( GetEventHandler()->ProcessEvent(event) )
904 return;
905 }
5fb9fcfc
VZ
906 //else: multiline controls need Enter for themselves
907
908 break;
4d91c1d1 909
cd471848 910 case WXK_TAB:
319fefa9
VZ
911 // always produce navigation event - even if we process TAB
912 // ourselves the fact that we got here means that the user code
913 // decided to skip processing of this TAB - probably to let it
914 // do its default job.
cd471848 915 {
5fb9fcfc
VZ
916 wxNavigationKeyEvent eventNav;
917 eventNav.SetDirection(!event.ShiftDown());
8614c467 918 eventNav.SetWindowChange(event.ControlDown());
5fb9fcfc 919 eventNav.SetEventObject(this);
39136494 920
d9506e77 921 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
cd471848
VZ
922 return;
923 }
341c92a8 924 break;
cd471848 925 }
39136494 926
8614c467 927 // no, we didn't process it
42e69d6b 928 event.Skip();
2bda0e17
KB
929}
930
debe6624 931bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 932{
789295bf
VZ
933 switch (param)
934 {
935 case EN_SETFOCUS:
936 case EN_KILLFOCUS:
937 {
938 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
939 : wxEVT_SET_FOCUS,
940 m_windowId);
941 event.SetEventObject( this );
942 GetEventHandler()->ProcessEvent(event);
943 }
944 break;
ae29de83 945
789295bf
VZ
946 case EN_CHANGE:
947 {
948 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
bfbd6dc1
VZ
949 InitCommandEvent(event);
950 event.SetString(GetValue());
951 ProcessCommand(event);
789295bf
VZ
952 }
953 break;
2bda0e17 954
b12915c1 955 case EN_MAXTEXT:
789295bf
VZ
956 // the text size limit has been hit - increase it
957 AdjustSpaceLimit();
958 break;
959
960 // the other notification messages are not processed
961 case EN_UPDATE:
b12915c1 962 case EN_ERRSPACE:
789295bf
VZ
963 case EN_HSCROLL:
964 case EN_VSCROLL:
f6bcfd97 965 return FALSE;
789295bf
VZ
966 default:
967 return FALSE;
968 }
969
970 // processed
971 return TRUE;
2bda0e17
KB
972}
973
33ac7e6f 974WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
975#if wxUSE_CTL3D
976 WXUINT message,
977 WXWPARAM wParam,
978 WXLPARAM lParam
979#else
33ac7e6f
KB
980 WXUINT WXUNUSED(message),
981 WXWPARAM WXUNUSED(wParam),
788722ac
JS
982 WXLPARAM WXUNUSED(lParam)
983#endif
984 )
f6bcfd97
BP
985{
986#if wxUSE_CTL3D
987 if ( m_useCtl3D )
988 {
989 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
990 return (WXHBRUSH) hbrush;
991 }
992#endif // wxUSE_CTL3D
993
994 HDC hdc = (HDC)pDC;
995 if (GetParent()->GetTransparentBackground())
996 SetBkMode(hdc, TRANSPARENT);
997 else
998 SetBkMode(hdc, OPAQUE);
999
1000 wxColour colBack = GetBackgroundColour();
1001
1002 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
1003 colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
1004
1005 ::SetBkColor(hdc, wxColourToRGB(colBack));
1006 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
1007
1008 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
1009
1010 return (WXHBRUSH)brush->GetResourceHandle();
1011}
1012
1013// In WIN16, need to override normal erasing because
1014// Ctl3D doesn't use the wxWindows background colour.
1015#ifdef __WIN16__
1016void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
1017{
1018 wxColour col(m_backgroundColour);
1019
1020#if wxUSE_CTL3D
1021 if (m_useCtl3D)
1022 col = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW);
1023#endif
1024
1025 RECT rect;
1026 ::GetClientRect(GetHwnd(), &rect);
1027
1028 COLORREF ref = PALETTERGB(col.Red(),
1029 col.Green(),
1030 col.Blue());
1031 HBRUSH hBrush = ::CreateSolidBrush(ref);
1032 if ( !hBrush )
1033 wxLogLastError(wxT("CreateSolidBrush"));
1034
1035 HDC hdc = (HDC)event.GetDC()->GetHDC();
1036
1037 int mode = ::SetMapMode(hdc, MM_TEXT);
1038
1039 ::FillRect(hdc, &rect, hBrush);
1040 ::DeleteObject(hBrush);
1041 ::SetMapMode(hdc, mode);
1042
1043}
1044#endif
1045
789295bf
VZ
1046void wxTextCtrl::AdjustSpaceLimit()
1047{
25889d3c 1048#ifndef __WIN16__
789295bf
VZ
1049 unsigned int len = ::GetWindowTextLength(GetHwnd()),
1050 limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
17d8ee1c 1051 if ( len >= limit )
789295bf
VZ
1052 {
1053 limit = len + 0x8000; // 32Kb
1054
5ea105e0 1055#if wxUSE_RICHEDIT
b12915c1
VZ
1056 if ( m_isRich )
1057 {
1058 // as a nice side effect, this also allows passing limit > 64Kb
1059 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
1060 }
789295bf 1061 else
b12915c1
VZ
1062#endif // wxUSE_RICHEDIT
1063 {
1064 if ( limit > 0xffff )
1065 {
1066 // this will set it to a platform-dependent maximum (much more
1067 // than 64Kb under NT)
1068 limit = 0;
1069 }
1070
789295bf 1071 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
b12915c1 1072 }
789295bf 1073 }
b12915c1 1074#endif // !Win16
789295bf 1075}
2bda0e17 1076
a1b82138 1077bool wxTextCtrl::AcceptsFocus() const
2bda0e17 1078{
a1b82138
VZ
1079 // we don't want focus if we can't be edited
1080 return IsEditable() && wxControl::AcceptsFocus();
1081}
c085e333 1082
f68586e5 1083wxSize wxTextCtrl::DoGetBestSize() const
a1b82138
VZ
1084{
1085 int cx, cy;
1086 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1087
1088 int wText = DEFAULT_ITEM_WIDTH;
1089
1090 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1091 if ( m_windowStyle & wxTE_MULTILINE )
1092 {
3988b155 1093 hText *= wxMax(GetNumberOfLines(), 5);
a1b82138
VZ
1094 }
1095 //else: for single line control everything is ok
1096
1097 return wxSize(wText, hText);
2bda0e17 1098}
a1b82138
VZ
1099
1100// ----------------------------------------------------------------------------
1101// standard handlers for standard edit menu events
1102// ----------------------------------------------------------------------------
2bda0e17 1103
bfbd6dc1 1104void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1105{
1106 Cut();
1107}
1108
bfbd6dc1 1109void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1110{
1111 Copy();
1112}
1113
bfbd6dc1 1114void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1115{
1116 Paste();
1117}
1118
bfbd6dc1 1119void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1120{
1121 Undo();
1122}
1123
bfbd6dc1 1124void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1125{
1126 Redo();
1127}
1128
1129void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1130{
1131 event.Enable( CanCut() );
1132}
1133
1134void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1135{
1136 event.Enable( CanCopy() );
1137}
1138
1139void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1140{
1141 event.Enable( CanPaste() );
1142}
1143
1144void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1145{
1146 event.Enable( CanUndo() );
1147}
1148
1149void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1150{
1151 event.Enable( CanRedo() );
1152}
1153
f6bcfd97
BP
1154// ----------------------------------------------------------------------------
1155// colour setting for the rich edit controls
1156// ----------------------------------------------------------------------------
1157
1158#if wxUSE_RICHEDIT
1159
1160// Watcom C++ doesn't define this
1161#ifndef SCF_ALL
1162#define SCF_ALL 0x0004
1163#endif
1164
1165bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
1166{
1167 if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
1168 {
1169 // colour didn't really change
1170 return FALSE;
1171 }
1172
1173 if ( IsRich() )
1174 {
1175 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1176 // EM_SETBKGNDCOLOR additionally
1177 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
1178 }
1179
1180 return TRUE;
1181}
1182
1183bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1184{
1185 if ( !wxTextCtrlBase::SetForegroundColour(colour) )
1186 {
1187 // colour didn't really change
1188 return FALSE;
1189 }
1190
1191 if ( IsRich() )
1192 {
1193 // change the colour of everything
1194 CHARFORMAT cf;
1195 wxZeroMemory(cf);
1196 cf.cbSize = sizeof(cf);
1197 cf.dwMask = CFM_COLOR;
1198 cf.crTextColor = wxColourToRGB(colour);
1199 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
1200 }
1201
1202 return TRUE;
1203}
1204
1205#endif // wxUSE_RICHEDIT
1206
b12915c1
VZ
1207// ----------------------------------------------------------------------------
1208// wxRichEditModule
1209// ----------------------------------------------------------------------------
1210
1211#if wxUSE_RICHEDIT
1212
1213bool wxRichEditModule::OnInit()
1214{
1215 // don't do anything - we will load it when needed
1216 return TRUE;
1217}
1218
1219void wxRichEditModule::OnExit()
1220{
1221 if ( ms_hRichEdit )
1222 {
1223 FreeLibrary(ms_hRichEdit);
1224 }
1225}
1226
1227/* static */
1228bool wxRichEditModule::Load(int version)
1229{
1230 wxCHECK_MSG( version >= 1 && version <= 3, FALSE,
1231 _T("incorrect richedit control version requested") );
1232
1233 if ( version <= ms_verRichEdit )
1234 {
1235 // we've already got this or better
1236 return TRUE;
1237 }
1238
1239 if ( ms_hRichEdit )
1240 {
1241 ::FreeLibrary(ms_hRichEdit);
1242 }
1243
1244 // always try load riched20.dll first - like this we won't have to reload
1245 // it later if we're first asked for RE 1 and then for RE 2 or 3
1246 wxString dllname = _T("riched20.dll");
1247 ms_hRichEdit = ::LoadLibrary(dllname);
1248 ms_verRichEdit = 2; // no way to tell if it's 2 or 3, assume 2
1249
1250 if ( !ms_hRichEdit && (version == 1) )
1251 {
1252 // fall back to RE 1
1253 dllname = _T("riched32.dll");
1254 ms_hRichEdit = ::LoadLibrary(dllname);
1255 ms_verRichEdit = 1;
1256 }
1257
1258 if ( !ms_hRichEdit )
1259 {
1260 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
1261
1262 ms_verRichEdit = -1;
1263
1264 return FALSE;
1265 }
1266
1267 return TRUE;
1268}
1269
1270#endif // wxUSE_RICHEDIT
1271