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