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