]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
added wxListView class: this is going to be a wxListCtrl with human (inter)face
[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
1e6feb95
VZ
31#if wxUSE_TEXTCTRL
32
2bda0e17 33#ifndef WX_PRECOMP
a1b82138
VZ
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
36 #include "wx/brush.h"
37 #include "wx/utils.h"
381dd4bf 38 #include "wx/intl.h"
a1b82138 39 #include "wx/log.h"
381dd4bf 40 #include "wx/app.h"
2bda0e17
KB
41#endif
42
f6bcfd97
BP
43#include "wx/module.h"
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
KB
53#include "wx/msw/private.h"
54
a1b82138 55#include <string.h>
2bda0e17 56#include <stdlib.h>
a1b82138 57#include <sys/types.h>
fbc535ff 58
ae090fdb 59#if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__))
cd471848 60 #include <richedit.h>
2bda0e17
KB
61#endif
62
e80591b9
VZ
63// old mingw32 doesn't define this
64#ifndef CFM_CHARSET
65 #define CFM_CHARSET 0x08000000
66#endif // CFM_CHARSET
67
b12915c1
VZ
68// ----------------------------------------------------------------------------
69// private classes
70// ----------------------------------------------------------------------------
71
72#if wxUSE_RICHEDIT
73
74// this module initializes RichEdit DLL if needed
75class wxRichEditModule : public wxModule
76{
77public:
78 virtual bool OnInit();
79 virtual void OnExit();
80
81 // get the version currently loaded, -1 if none
82 static int GetLoadedVersion() { return ms_verRichEdit; }
83
84 // load the richedit DLL of at least of required version
85 static bool Load(int version = 1);
86
87private:
88 // the handle to richedit DLL and the version of the DLL loaded
89 static HINSTANCE ms_hRichEdit;
90
91 // the DLL version loaded or -1 if none
92 static int ms_verRichEdit;
93
94 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
95};
96
97HINSTANCE wxRichEditModule::ms_hRichEdit = (HINSTANCE)NULL;
98int wxRichEditModule::ms_verRichEdit = -1;
99
100IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
101
102#endif // wxUSE_RICHEDIT
e702ff0f 103
a1b82138
VZ
104// ----------------------------------------------------------------------------
105// event tables and other macros
106// ----------------------------------------------------------------------------
107
2bda0e17
KB
108IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
109
110BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
a1b82138
VZ
111 EVT_CHAR(wxTextCtrl::OnChar)
112 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
113
114 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
115 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
116 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
117 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
118 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
119
120 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
121 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
122 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
123 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
124 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
f6bcfd97
BP
125#ifdef __WIN16__
126 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
127#endif
2bda0e17 128END_EVENT_TABLE()
e702ff0f 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
4bc1afd5
VZ
453#if wxUSE_RICHEDIT
454 // ensure that the new text will be in the default style
455 if ( IsRich() &&
456 (m_defaultStyle.HasFont() || m_defaultStyle.HasTextColour()) )
457 {
458 long start, end;
459 GetSelection(&start, &end);
460 SetStyle(start, end, m_defaultStyle );
461 }
462#endif // wxUSE_RICHEDIT
463
a1b82138 464 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
2bda0e17 465
a1b82138 466 AdjustSpaceLimit();
2bda0e17
KB
467}
468
a1b82138
VZ
469void wxTextCtrl::AppendText(const wxString& text)
470{
471 SetInsertionPointEnd();
472 WriteText(text);
473}
474
475void wxTextCtrl::Clear()
476{
223d09f6 477 SetWindowText(GetHwnd(), wxT(""));
a1b82138
VZ
478}
479
480// ----------------------------------------------------------------------------
2bda0e17 481// Clipboard operations
a1b82138
VZ
482// ----------------------------------------------------------------------------
483
cd471848 484void wxTextCtrl::Copy()
2bda0e17 485{
e702ff0f
JS
486 if (CanCopy())
487 {
789295bf 488 HWND hWnd = GetHwnd();
e702ff0f
JS
489 SendMessage(hWnd, WM_COPY, 0, 0L);
490 }
2bda0e17
KB
491}
492
cd471848 493void wxTextCtrl::Cut()
2bda0e17 494{
e702ff0f
JS
495 if (CanCut())
496 {
789295bf 497 HWND hWnd = GetHwnd();
e702ff0f
JS
498 SendMessage(hWnd, WM_CUT, 0, 0L);
499 }
2bda0e17
KB
500}
501
cd471848 502void wxTextCtrl::Paste()
2bda0e17 503{
e702ff0f
JS
504 if (CanPaste())
505 {
789295bf 506 HWND hWnd = GetHwnd();
e702ff0f
JS
507 SendMessage(hWnd, WM_PASTE, 0, 0L);
508 }
2bda0e17
KB
509}
510
a1b82138
VZ
511bool wxTextCtrl::CanCopy() const
512{
513 // Can copy if there's a selection
514 long from, to;
515 GetSelection(& from, & to);
dbf28859 516 return (from != to) ;
a1b82138
VZ
517}
518
519bool wxTextCtrl::CanCut() const
520{
521 // Can cut if there's a selection
522 long from, to;
523 GetSelection(& from, & to);
dbf28859 524 return (from != to) && (IsEditable());
a1b82138
VZ
525}
526
527bool wxTextCtrl::CanPaste() const
528{
529#if wxUSE_RICHEDIT
530 if (m_isRich)
531 {
532 int dataFormat = 0; // 0 == any format
533 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
534 }
535#endif
536 if (!IsEditable())
537 return FALSE;
538
539 // Standard edit control: check for straight text on clipboard
540 bool isTextAvailable = FALSE;
541 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
542 {
543 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
544 ::CloseClipboard();
545 }
546
547 return isTextAvailable;
548}
549
550// ----------------------------------------------------------------------------
551// Accessors
552// ----------------------------------------------------------------------------
553
debe6624 554void wxTextCtrl::SetEditable(bool editable)
2bda0e17 555{
a1b82138
VZ
556 HWND hWnd = GetHwnd();
557 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
2bda0e17
KB
558}
559
debe6624 560void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 561{
a1b82138 562 HWND hWnd = GetHwnd();
2bda0e17 563#ifdef __WIN32__
57c208c5 564#if wxUSE_RICHEDIT
a1b82138
VZ
565 if ( m_isRich)
566 {
567 CHARRANGE range;
568 range.cpMin = pos;
569 range.cpMax = pos;
570 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
571 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
572 }
573 else
574#endif // wxUSE_RICHEDIT
575 {
576 SendMessage(hWnd, EM_SETSEL, pos, pos);
577 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
578 }
579#else // Win16
580 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
581#endif // Win32/16
582
c4608a8a 583 static const wxChar *nothing = _T("");
a1b82138 584 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
2bda0e17
KB
585}
586
cd471848 587void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 588{
a1b82138
VZ
589 long pos = GetLastPosition();
590 SetInsertionPoint(pos);
2bda0e17
KB
591}
592
cd471848 593long wxTextCtrl::GetInsertionPoint() const
2bda0e17 594{
57c208c5 595#if wxUSE_RICHEDIT
a1b82138
VZ
596 if (m_isRich)
597 {
598 CHARRANGE range;
599 range.cpMin = 0;
600 range.cpMax = 0;
601 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
602 return range.cpMin;
603 }
2bda0e17
KB
604#endif
605
a1b82138
VZ
606 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
607 return Pos & 0xFFFF;
2bda0e17
KB
608}
609
cd471848 610long wxTextCtrl::GetLastPosition() const
2bda0e17 611{
789295bf 612 HWND hWnd = GetHwnd();
2bda0e17
KB
613
614 // Will always return a number > 0 (according to docs)
615 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
616
617 // This gets the char index for the _beginning_ of the last line
618 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
39136494 619
2bda0e17
KB
620 // Get number of characters in the last line. We'll add this to the character
621 // index for the last line, 1st position.
622 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
623
624 return (long)(charIndex + lineLength);
625}
626
a1b82138
VZ
627// If the return values from and to are the same, there is no
628// selection.
629void wxTextCtrl::GetSelection(long* from, long* to) const
630{
631#if wxUSE_RICHEDIT
632 if (m_isRich)
633 {
634 CHARRANGE charRange;
635 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
636
637 *from = charRange.cpMin;
638 *to = charRange.cpMax;
a1b82138 639 }
4bc1afd5
VZ
640 else
641#endif // rich/!rich
642 {
643 DWORD dwStart, dwEnd;
644 WPARAM wParam = (WPARAM) &dwStart; // receives starting position
645 LPARAM lParam = (LPARAM) &dwEnd; // receives ending position
a1b82138 646
4bc1afd5 647 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
a1b82138 648
4bc1afd5
VZ
649 *from = dwStart;
650 *to = dwEnd;
651 }
a1b82138
VZ
652}
653
654bool wxTextCtrl::IsEditable() const
655{
656 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
657
658 return ((style & ES_READONLY) == 0);
659}
660
661// ----------------------------------------------------------------------------
662// Editing
663// ----------------------------------------------------------------------------
664
debe6624 665void wxTextCtrl::Replace(long from, long to, const wxString& value)
2bda0e17 666{
acbd13a3 667#if wxUSE_CLIPBOARD
789295bf 668 HWND hWnd = GetHwnd();
2bda0e17
KB
669 long fromChar = from;
670 long toChar = to;
39136494 671
2bda0e17
KB
672 // Set selection and remove it
673#ifdef __WIN32__
674 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
675#else
676 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
677#endif
678 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
679
680 // Now replace with 'value', by pasting.
837e5743 681 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
2bda0e17
KB
682
683 // Paste into edit control
684 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
acbd13a3
JS
685#else
686 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
687#endif
2bda0e17
KB
688}
689
debe6624 690void wxTextCtrl::Remove(long from, long to)
2bda0e17 691{
789295bf 692 HWND hWnd = GetHwnd();
2bda0e17
KB
693 long fromChar = from;
694 long toChar = to;
39136494 695
2bda0e17
KB
696 // Cut all selected text
697#ifdef __WIN32__
698 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
699#else
700 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
701#endif
702 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
703}
704
debe6624 705void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 706{
789295bf 707 HWND hWnd = GetHwnd();
2bda0e17
KB
708 long fromChar = from;
709 long toChar = to;
a1b82138
VZ
710
711 // if from and to are both -1, it means (in wxWindows) that all text should
712 // be selected. Translate into Windows convention
2bda0e17
KB
713 if ((from == -1) && (to == -1))
714 {
715 fromChar = 0;
716 toChar = -1;
717 }
39136494 718
2bda0e17
KB
719#ifdef __WIN32__
720 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
721 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
722#else
723 // WPARAM is 0: selection is scrolled into view
724 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
725#endif
726}
727
728bool wxTextCtrl::LoadFile(const wxString& file)
729{
a1b82138 730 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 731 {
a1b82138
VZ
732 // update the size limit if needed
733 AdjustSpaceLimit();
2bda0e17 734
a1b82138 735 return TRUE;
2bda0e17 736 }
2bda0e17 737
a1b82138 738 return FALSE;
2bda0e17
KB
739}
740
cd471848 741bool wxTextCtrl::IsModified() const
2bda0e17 742{
789295bf 743 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
2bda0e17
KB
744}
745
746// Makes 'unmodified'
cd471848 747void wxTextCtrl::DiscardEdits()
2bda0e17 748{
a1b82138 749 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
750}
751
cd471848 752int wxTextCtrl::GetNumberOfLines() const
2bda0e17 753{
789295bf 754 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
755}
756
debe6624 757long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 758{
789295bf 759 HWND hWnd = GetHwnd();
2bda0e17
KB
760
761 // This gets the char index for the _beginning_ of this line
762 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
763 return (long)(x + charIndex);
764}
765
0efe5ba7 766bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 767{
789295bf 768 HWND hWnd = GetHwnd();
2bda0e17
KB
769
770 // This gets the line number containing the character
0efe5ba7
VZ
771 int lineNo;
772#if wxUSE_RICHEDIT
773 if ( m_isRich )
774 {
775 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
776 }
777 else
778#endif // wxUSE_RICHEDIT
779 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
780
781 if ( lineNo == -1 )
782 {
783 // no such line
784 return FALSE;
785 }
786
2bda0e17
KB
787 // This gets the char index for the _beginning_ of this line
788 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
789 if ( charIndex == -1 )
790 {
791 return FALSE;
792 }
793
2bda0e17 794 // The X position must therefore be the different between pos and charIndex
0efe5ba7
VZ
795 if ( x )
796 *x = (long)(pos - charIndex);
797 if ( y )
798 *y = (long)lineNo;
799
800 return TRUE;
2bda0e17
KB
801}
802
debe6624 803void wxTextCtrl::ShowPosition(long pos)
2bda0e17 804{
789295bf 805 HWND hWnd = GetHwnd();
2bda0e17
KB
806
807 // To scroll to a position, we pass the number of lines and characters
808 // to scroll *by*. This means that we need to:
809 // (1) Find the line position of the current line.
810 // (2) Find the line position of pos.
811 // (3) Scroll by (pos - current).
812 // For now, ignore the horizontal scrolling.
813
814 // Is this where scrolling is relative to - the line containing the caret?
815 // Or is the first visible line??? Try first visible line.
816// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
817
818 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
819
820 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 821
2bda0e17
KB
822 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
823
2bda0e17 824 if (linesToScroll != 0)
de4d7713 825 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
826}
827
debe6624 828int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
829{
830 long charIndex = XYToPosition(0, lineNo);
4438caf4 831 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
2bda0e17
KB
832 return len;
833}
834
debe6624 835wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 836{
a1b82138 837 size_t len = (size_t)GetLineLength(lineNo) + 1;
488fe1fe 838
f6bcfd97
BP
839 // there must be at least enough place for the length WORD in the
840 // buffer
841 len += sizeof(WORD);
4438caf4 842
f6bcfd97
BP
843 wxString str;
844 wxChar *buf = str.GetWriteBuf(len);
845
33ac7e6f 846 *(WORD *)buf = (WORD)len;
f6bcfd97
BP
847 len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
848 buf[len] = 0;
4438caf4 849
f6bcfd97 850 str.UngetWriteBuf(len);
4438caf4
VZ
851
852 return str;
2bda0e17
KB
853}
854
a1b82138 855// ----------------------------------------------------------------------------
ca8b28f2 856// Undo/redo
a1b82138
VZ
857// ----------------------------------------------------------------------------
858
ca8b28f2
JS
859void wxTextCtrl::Undo()
860{
861 if (CanUndo())
862 {
789295bf 863 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
864 }
865}
866
867void wxTextCtrl::Redo()
868{
869 if (CanRedo())
870 {
871 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 872 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
873 }
874}
875
876bool wxTextCtrl::CanUndo() const
877{
789295bf 878 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
879}
880
881bool wxTextCtrl::CanRedo() const
882{
789295bf 883 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
884}
885
a1b82138
VZ
886// ----------------------------------------------------------------------------
887// implemenation details
888// ----------------------------------------------------------------------------
39136494 889
2bda0e17
KB
890void wxTextCtrl::Command(wxCommandEvent & event)
891{
a1b82138
VZ
892 SetValue(event.GetString());
893 ProcessCommand (event);
2bda0e17
KB
894}
895
896void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
897{
a1b82138
VZ
898 // By default, load the first file into the text window.
899 if (event.GetNumberOfFiles() > 0)
900 {
901 LoadFile(event.GetFiles()[0]);
902 }
2bda0e17
KB
903}
904
2bda0e17
KB
905void wxTextCtrl::OnChar(wxKeyEvent& event)
906{
42e69d6b 907 switch ( event.KeyCode() )
cd471848 908 {
cd471848 909 case WXK_RETURN:
39136494 910 if ( !(m_windowStyle & wxTE_MULTILINE) )
cd471848
VZ
911 {
912 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
bfbd6dc1 913 InitCommandEvent(event);
f6bcfd97 914 event.SetString(GetValue());
cd471848
VZ
915 if ( GetEventHandler()->ProcessEvent(event) )
916 return;
917 }
5fb9fcfc
VZ
918 //else: multiline controls need Enter for themselves
919
920 break;
4d91c1d1 921
cd471848 922 case WXK_TAB:
319fefa9
VZ
923 // always produce navigation event - even if we process TAB
924 // ourselves the fact that we got here means that the user code
925 // decided to skip processing of this TAB - probably to let it
926 // do its default job.
cd471848 927 {
5fb9fcfc
VZ
928 wxNavigationKeyEvent eventNav;
929 eventNav.SetDirection(!event.ShiftDown());
8614c467 930 eventNav.SetWindowChange(event.ControlDown());
5fb9fcfc 931 eventNav.SetEventObject(this);
39136494 932
d9506e77 933 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
cd471848
VZ
934 return;
935 }
341c92a8 936 break;
cd471848 937 }
39136494 938
8614c467 939 // no, we didn't process it
42e69d6b 940 event.Skip();
2bda0e17
KB
941}
942
debe6624 943bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 944{
789295bf
VZ
945 switch (param)
946 {
947 case EN_SETFOCUS:
948 case EN_KILLFOCUS:
949 {
950 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
951 : wxEVT_SET_FOCUS,
952 m_windowId);
953 event.SetEventObject( this );
954 GetEventHandler()->ProcessEvent(event);
955 }
956 break;
ae29de83 957
789295bf
VZ
958 case EN_CHANGE:
959 {
960 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
bfbd6dc1
VZ
961 InitCommandEvent(event);
962 event.SetString(GetValue());
963 ProcessCommand(event);
789295bf
VZ
964 }
965 break;
2bda0e17 966
b12915c1 967 case EN_MAXTEXT:
789295bf
VZ
968 // the text size limit has been hit - increase it
969 AdjustSpaceLimit();
970 break;
971
972 // the other notification messages are not processed
973 case EN_UPDATE:
b12915c1 974 case EN_ERRSPACE:
789295bf
VZ
975 case EN_HSCROLL:
976 case EN_VSCROLL:
f6bcfd97 977 return FALSE;
789295bf
VZ
978 default:
979 return FALSE;
980 }
981
982 // processed
983 return TRUE;
2bda0e17
KB
984}
985
33ac7e6f 986WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
987#if wxUSE_CTL3D
988 WXUINT message,
989 WXWPARAM wParam,
990 WXLPARAM lParam
991#else
33ac7e6f
KB
992 WXUINT WXUNUSED(message),
993 WXWPARAM WXUNUSED(wParam),
788722ac
JS
994 WXLPARAM WXUNUSED(lParam)
995#endif
996 )
f6bcfd97
BP
997{
998#if wxUSE_CTL3D
999 if ( m_useCtl3D )
1000 {
1001 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
1002 return (WXHBRUSH) hbrush;
1003 }
1004#endif // wxUSE_CTL3D
1005
1006 HDC hdc = (HDC)pDC;
1007 if (GetParent()->GetTransparentBackground())
1008 SetBkMode(hdc, TRANSPARENT);
1009 else
1010 SetBkMode(hdc, OPAQUE);
1011
1012 wxColour colBack = GetBackgroundColour();
1013
1014 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
1015 colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
1016
1017 ::SetBkColor(hdc, wxColourToRGB(colBack));
1018 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
1019
1020 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
1021
1022 return (WXHBRUSH)brush->GetResourceHandle();
1023}
1024
1025// In WIN16, need to override normal erasing because
1026// Ctl3D doesn't use the wxWindows background colour.
1027#ifdef __WIN16__
1028void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
1029{
1030 wxColour col(m_backgroundColour);
1031
1032#if wxUSE_CTL3D
1033 if (m_useCtl3D)
1034 col = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW);
1035#endif
1036
1037 RECT rect;
1038 ::GetClientRect(GetHwnd(), &rect);
1039
1040 COLORREF ref = PALETTERGB(col.Red(),
1041 col.Green(),
1042 col.Blue());
1043 HBRUSH hBrush = ::CreateSolidBrush(ref);
1044 if ( !hBrush )
1045 wxLogLastError(wxT("CreateSolidBrush"));
1046
1047 HDC hdc = (HDC)event.GetDC()->GetHDC();
1048
1049 int mode = ::SetMapMode(hdc, MM_TEXT);
1050
1051 ::FillRect(hdc, &rect, hBrush);
1052 ::DeleteObject(hBrush);
1053 ::SetMapMode(hdc, mode);
1054
1055}
1056#endif
1057
789295bf
VZ
1058void wxTextCtrl::AdjustSpaceLimit()
1059{
25889d3c 1060#ifndef __WIN16__
789295bf
VZ
1061 unsigned int len = ::GetWindowTextLength(GetHwnd()),
1062 limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
17d8ee1c 1063 if ( len >= limit )
789295bf
VZ
1064 {
1065 limit = len + 0x8000; // 32Kb
1066
5ea105e0 1067#if wxUSE_RICHEDIT
b12915c1
VZ
1068 if ( m_isRich )
1069 {
1070 // as a nice side effect, this also allows passing limit > 64Kb
1071 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
1072 }
789295bf 1073 else
b12915c1
VZ
1074#endif // wxUSE_RICHEDIT
1075 {
1076 if ( limit > 0xffff )
1077 {
1078 // this will set it to a platform-dependent maximum (much more
1079 // than 64Kb under NT)
1080 limit = 0;
1081 }
1082
789295bf 1083 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
b12915c1 1084 }
789295bf 1085 }
b12915c1 1086#endif // !Win16
789295bf 1087}
2bda0e17 1088
a1b82138 1089bool wxTextCtrl::AcceptsFocus() const
2bda0e17 1090{
a1b82138
VZ
1091 // we don't want focus if we can't be edited
1092 return IsEditable() && wxControl::AcceptsFocus();
1093}
c085e333 1094
f68586e5 1095wxSize wxTextCtrl::DoGetBestSize() const
a1b82138
VZ
1096{
1097 int cx, cy;
1098 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1099
1100 int wText = DEFAULT_ITEM_WIDTH;
1101
1102 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1103 if ( m_windowStyle & wxTE_MULTILINE )
1104 {
3988b155 1105 hText *= wxMax(GetNumberOfLines(), 5);
a1b82138
VZ
1106 }
1107 //else: for single line control everything is ok
1108
1109 return wxSize(wText, hText);
2bda0e17 1110}
a1b82138
VZ
1111
1112// ----------------------------------------------------------------------------
1113// standard handlers for standard edit menu events
1114// ----------------------------------------------------------------------------
2bda0e17 1115
bfbd6dc1 1116void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1117{
1118 Cut();
1119}
1120
bfbd6dc1 1121void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1122{
1123 Copy();
1124}
1125
bfbd6dc1 1126void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1127{
1128 Paste();
1129}
1130
bfbd6dc1 1131void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1132{
1133 Undo();
1134}
1135
bfbd6dc1 1136void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1137{
1138 Redo();
1139}
1140
1141void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1142{
1143 event.Enable( CanCut() );
1144}
1145
1146void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1147{
1148 event.Enable( CanCopy() );
1149}
1150
1151void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1152{
1153 event.Enable( CanPaste() );
1154}
1155
1156void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1157{
1158 event.Enable( CanUndo() );
1159}
1160
1161void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1162{
1163 event.Enable( CanRedo() );
1164}
1165
4bc1afd5
VZ
1166// the rest of the file only deals with the rich edit controls
1167#if wxUSE_RICHEDIT
1168
f6bcfd97
BP
1169// ----------------------------------------------------------------------------
1170// colour setting for the rich edit controls
1171// ----------------------------------------------------------------------------
1172
f6bcfd97
BP
1173// Watcom C++ doesn't define this
1174#ifndef SCF_ALL
1175#define SCF_ALL 0x0004
1176#endif
1177
1178bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
1179{
1180 if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
1181 {
1182 // colour didn't really change
1183 return FALSE;
1184 }
1185
1186 if ( IsRich() )
1187 {
1188 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1189 // EM_SETBKGNDCOLOR additionally
1190 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
1191 }
1192
1193 return TRUE;
1194}
1195
1196bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1197{
1198 if ( !wxTextCtrlBase::SetForegroundColour(colour) )
1199 {
1200 // colour didn't really change
1201 return FALSE;
1202 }
1203
1204 if ( IsRich() )
1205 {
1206 // change the colour of everything
1207 CHARFORMAT cf;
1208 wxZeroMemory(cf);
1209 cf.cbSize = sizeof(cf);
1210 cf.dwMask = CFM_COLOR;
1211 cf.crTextColor = wxColourToRGB(colour);
1212 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
1213 }
1214
1215 return TRUE;
1216}
1217
4bc1afd5
VZ
1218// ----------------------------------------------------------------------------
1219// styling support for rich edit controls
1220// ----------------------------------------------------------------------------
1221
1222bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
1223{
1224 if ( !IsRich() )
1225 {
1226 // can't do it with normal text control
1227 return FALSE;
1228 }
1229
1230 // the rich text control doesn't handle setting background colour, so don't
1231 // even try if it's the only thing we want to change
be329a3d
RD
1232 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1233 !style.HasFont() && !style.HasTextColour() )
4bc1afd5 1234 {
be329a3d
RD
1235 // nothing to do: return TRUE if there was really nothing to do and
1236 // FALSE if we failed to set bg colour
4bc1afd5
VZ
1237 return !style.HasBackgroundColour();
1238 }
1239
1240 // order the range if needed
1241 if ( start > end )
1242 {
1243 long tmp = start;
1244 start = end;
1245 end = tmp;
1246 }
1247
1248 // we can only change the format of the selection, so select the range we
1249 // want and restore the old selection later
1250 long startOld, endOld;
1251 GetSelection(&startOld, &endOld);
1252
1253 // but do we really have to change the selection?
1254 bool changeSel = start != startOld || end != endOld;
1255
1256 if ( changeSel )
1257 SendMessage(GetHwnd(), EM_SETSEL, (WPARAM) start, (LPARAM) end);
1258
1259 // initialize CHARFORMAT struct
be329a3d
RD
1260#if wxUSE_RICHEDIT2
1261 CHARFORMAT2 cf;
1262#else
4bc1afd5 1263 CHARFORMAT cf;
be329a3d 1264#endif
4bc1afd5
VZ
1265 wxZeroMemory(cf);
1266 cf.cbSize = sizeof(cf);
1267
1268 if ( style.HasFont() )
1269 {
1270 cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET;
1271
1272 // fill in data from LOGFONT but recalculate lfHeight because we need
1273 // the real height in twips and not the negative number which
1274 // wxFillLogFont() returns (this is correct in general and works with
1275 // the Windows font mapper, but not here)
1276 LOGFONT lf;
1277 wxFillLogFont(&lf, &style.GetFont());
1278 cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips
1279 cf.bCharSet = lf.lfCharSet;
1280 cf.bPitchAndFamily = lf.lfPitchAndFamily;
1281 wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
1282
1283 // also deal with underline/italic/bold attributes
1284 if ( lf.lfItalic )
1285 {
1286 cf.dwMask |= CFM_ITALIC;
1287 cf.dwEffects |= CFE_ITALIC;
1288 }
1289
1290 if ( lf.lfWeight == FW_BOLD )
1291 {
1292 cf.dwMask |= CFM_BOLD;
1293 cf.dwEffects |= CFE_BOLD;
1294 }
1295
1296 if ( lf.lfUnderline )
1297 {
1298 cf.dwMask |= CFM_UNDERLINE;
1299 cf.dwEffects |= CFE_UNDERLINE;
1300 }
1301
1302 // strikeout fonts are not supported by wxWindows
1303 }
1304
1305 if ( style.HasTextColour() )
1306 {
1307 cf.dwMask |= CFM_COLOR;
1308 cf.crTextColor = wxColourToRGB(style.GetTextColour());
1309 }
1310
be329a3d 1311#if wxUSE_RICHEDIT2
bc00e715
JS
1312#ifndef CFM_BACKCOLOR
1313#define CFM_BACKCOLOR 0x04000000
1314#endif
1315
be329a3d
RD
1316 if ( wxRichEditModule::GetLoadedVersion() > 1 && style.HasBackgroundColour() )
1317 {
1318 cf.dwMask |= CFM_BACKCOLOR;
1319 cf.crBackColor = wxColourToRGB(style.GetBackgroundColour());
1320 }
1321#endif
4bc1afd5
VZ
1322 // do format the selection
1323 bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
1324 SCF_SELECTION, (LPARAM)&cf) != 0;
1325 if ( !ok )
1326 {
1327 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1328 }
1329
1330 if ( changeSel )
1331 {
1332 // restore the original selection
1333 SendMessage(GetHwnd(), EM_SETSEL, (WPARAM)startOld, (LPARAM)endOld);
1334 }
1335
1336 return ok;
1337}
f6bcfd97 1338
b12915c1
VZ
1339// ----------------------------------------------------------------------------
1340// wxRichEditModule
1341// ----------------------------------------------------------------------------
1342
b12915c1
VZ
1343bool wxRichEditModule::OnInit()
1344{
1345 // don't do anything - we will load it when needed
1346 return TRUE;
1347}
1348
1349void wxRichEditModule::OnExit()
1350{
1351 if ( ms_hRichEdit )
1352 {
1353 FreeLibrary(ms_hRichEdit);
1354 }
1355}
1356
1357/* static */
1358bool wxRichEditModule::Load(int version)
1359{
1360 wxCHECK_MSG( version >= 1 && version <= 3, FALSE,
1361 _T("incorrect richedit control version requested") );
1362
1363 if ( version <= ms_verRichEdit )
1364 {
1365 // we've already got this or better
1366 return TRUE;
1367 }
1368
1369 if ( ms_hRichEdit )
1370 {
1371 ::FreeLibrary(ms_hRichEdit);
1372 }
1373
1374 // always try load riched20.dll first - like this we won't have to reload
1375 // it later if we're first asked for RE 1 and then for RE 2 or 3
1376 wxString dllname = _T("riched20.dll");
1377 ms_hRichEdit = ::LoadLibrary(dllname);
1378 ms_verRichEdit = 2; // no way to tell if it's 2 or 3, assume 2
1379
1380 if ( !ms_hRichEdit && (version == 1) )
1381 {
1382 // fall back to RE 1
1383 dllname = _T("riched32.dll");
1384 ms_hRichEdit = ::LoadLibrary(dllname);
1385 ms_verRichEdit = 1;
1386 }
1387
1388 if ( !ms_hRichEdit )
1389 {
1390 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
1391
1392 ms_verRichEdit = -1;
1393
1394 return FALSE;
1395 }
1396
1397 return TRUE;
1398}
1399
1400#endif // wxUSE_RICHEDIT
1401
1e6feb95 1402#endif // wxUSE_TEXTCTRL