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