]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
fix for using wxDataObjectComposite with the clipboard
[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
VZ
440
441 // for compatibility, don't move the cursor when doing SetValue()
442 SetInsertionPoint(0);
aac7e7fe
VZ
443 }
444}
a1b82138 445
9d7de3c2 446#if wxUSE_RICHEDIT && !wxUSE_UNICODE
f6bcfd97 447
aac7e7fe
VZ
448DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb)
449{
450 *pcb = 0;
f6bcfd97 451
aac7e7fe
VZ
452 wchar_t *wbuf = (wchar_t *)buf;
453 const wchar_t *wpc = *(const wchar_t **)dwCookie;
454 while ( cb && *wpc )
455 {
456 *wbuf++ = *wpc++;
457
458 cb -= sizeof(wchar_t);
459 (*pcb) += sizeof(wchar_t);
07cf98cb 460 }
aac7e7fe
VZ
461
462 *(const wchar_t **)dwCookie = wpc;
463
464 return 0;
2bda0e17
KB
465}
466
aac7e7fe
VZ
467extern long wxEncodingToCodepage(wxFontEncoding encoding); // from strconv.cpp
468
469bool wxTextCtrl::StreamIn(const wxString& value, wxFontEncoding encoding)
470{
471 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
472 // text in the non default charset - otherwise it thinks it knows better
473 // than we do and always shows it in the default one
474
475 // first get the Windows code page for this encoding
476 long codepage = wxEncodingToCodepage(encoding);
477 if ( codepage == -1 )
478 {
479 // unknown encoding
480 return FALSE;
481 }
482
483 // next translate to Unicode using this code page
484 int len = ::MultiByteToWideChar(codepage, 0, value, -1, NULL, 0);
485 wxWCharBuffer wchBuf(len);
486 if ( !::MultiByteToWideChar(codepage, 0, value, -1,
487 (wchar_t *)wchBuf.data(), len) )
488 {
489 wxLogLastError(_T("MultiByteToWideChar"));
490 }
491
492 // finally, stream it in the control
493 const wchar_t *wpc = wchBuf;
494
495 EDITSTREAM eds;
496 wxZeroMemory(eds);
497 eds.dwCookie = (DWORD)&wpc;
7a25a27c
VZ
498 // the cast below is needed for broken (very) old mingw32 headers
499 eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn;
92209a39 500
aac7e7fe
VZ
501 if ( !::SendMessage(GetHwnd(), EM_STREAMIN,
502 SF_TEXT | SF_UNICODE | SFF_SELECTION,
503 (LPARAM)&eds) || eds.dwError )
504 {
505 wxLogLastError(_T("EM_STREAMIN"));
506
507 return FALSE;
508 }
509
510 return TRUE;
511}
512
513#endif // wxUSE_RICHEDIT
514
a1b82138 515void wxTextCtrl::WriteText(const wxString& value)
2bda0e17 516{
a1b82138 517 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
2bda0e17 518
4bc1afd5 519#if wxUSE_RICHEDIT
aac7e7fe
VZ
520 // there are several complications with the rich edit controls here
521 bool done = FALSE;
522 if ( IsRich() )
4bc1afd5 523 {
aac7e7fe
VZ
524 // first, ensure that the new text will be in the default style
525 if ( !m_defaultStyle.IsDefault() )
526 {
527 long start, end;
528 GetSelection(&start, &end);
529 SetStyle(start, end, m_defaultStyle );
530 }
531
9d7de3c2 532#if !wxUSE_UNICODE
aac7e7fe
VZ
533 // next check if the text we're inserting must be shown in a non
534 // default charset -- this only works for RichEdit > 1.0
535 if ( GetRichVersion() > 1 )
536 {
537 wxFont font = m_defaultStyle.GetFont();
538 if ( !font.Ok() )
539 font = GetFont();
540
541 if ( font.Ok() )
542 {
543 wxFontEncoding encoding = font.GetEncoding();
544 if ( encoding != wxFONTENCODING_SYSTEM )
545 {
546 done = StreamIn(valueDos, encoding);
547 }
548 }
549 }
9d7de3c2 550#endif // !wxUSE_UNICODE
4bc1afd5 551 }
4bc1afd5 552
aac7e7fe
VZ
553 if ( !done )
554#endif // wxUSE_RICHEDIT
555 {
556 ::SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
557 }
2bda0e17 558
a1b82138 559 AdjustSpaceLimit();
2bda0e17
KB
560}
561
a1b82138
VZ
562void wxTextCtrl::AppendText(const wxString& text)
563{
564 SetInsertionPointEnd();
aac7e7fe 565
a1b82138
VZ
566 WriteText(text);
567}
568
569void wxTextCtrl::Clear()
570{
aac7e7fe 571 ::SetWindowText(GetHwnd(), wxT(""));
a1b82138
VZ
572}
573
574// ----------------------------------------------------------------------------
2bda0e17 575// Clipboard operations
a1b82138
VZ
576// ----------------------------------------------------------------------------
577
cd471848 578void wxTextCtrl::Copy()
2bda0e17 579{
e702ff0f
JS
580 if (CanCopy())
581 {
789295bf 582 HWND hWnd = GetHwnd();
e702ff0f
JS
583 SendMessage(hWnd, WM_COPY, 0, 0L);
584 }
2bda0e17
KB
585}
586
cd471848 587void wxTextCtrl::Cut()
2bda0e17 588{
e702ff0f
JS
589 if (CanCut())
590 {
789295bf 591 HWND hWnd = GetHwnd();
e702ff0f
JS
592 SendMessage(hWnd, WM_CUT, 0, 0L);
593 }
2bda0e17
KB
594}
595
cd471848 596void wxTextCtrl::Paste()
2bda0e17 597{
e702ff0f
JS
598 if (CanPaste())
599 {
789295bf 600 HWND hWnd = GetHwnd();
e702ff0f
JS
601 SendMessage(hWnd, WM_PASTE, 0, 0L);
602 }
2bda0e17
KB
603}
604
a1b82138
VZ
605bool wxTextCtrl::CanCopy() const
606{
607 // Can copy if there's a selection
608 long from, to;
609 GetSelection(& from, & to);
dbf28859 610 return (from != to) ;
a1b82138
VZ
611}
612
613bool wxTextCtrl::CanCut() const
614{
615 // Can cut if there's a selection
616 long from, to;
617 GetSelection(& from, & to);
dbf28859 618 return (from != to) && (IsEditable());
a1b82138
VZ
619}
620
621bool wxTextCtrl::CanPaste() const
622{
aac7e7fe
VZ
623 if ( !IsEditable() )
624 return FALSE;
625
a1b82138 626#if wxUSE_RICHEDIT
aac7e7fe 627 if ( IsRich() )
a1b82138 628 {
aac7e7fe
VZ
629 UINT cf = 0; // 0 == any format
630
631 return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0;
a1b82138 632 }
aac7e7fe 633#endif // wxUSE_RICHEDIT
a1b82138
VZ
634
635 // Standard edit control: check for straight text on clipboard
aac7e7fe
VZ
636 if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
637 return FALSE;
638
639 bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0;
640 ::CloseClipboard();
a1b82138
VZ
641
642 return isTextAvailable;
643}
644
645// ----------------------------------------------------------------------------
646// Accessors
647// ----------------------------------------------------------------------------
648
debe6624 649void wxTextCtrl::SetEditable(bool editable)
2bda0e17 650{
a1b82138
VZ
651 HWND hWnd = GetHwnd();
652 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
2bda0e17
KB
653}
654
debe6624 655void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 656{
aac7e7fe 657 SetSelection(pos, pos);
a1b82138 658
decb3a6a 659#if wxUSE_RICHEDIT
aac7e7fe 660 if ( !IsRich() )
decb3a6a
JS
661#endif
662 {
663 static const wxChar *nothing = _T("");
aac7e7fe 664 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)nothing);
decb3a6a 665 }
2bda0e17
KB
666}
667
cd471848 668void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 669{
a1b82138
VZ
670 long pos = GetLastPosition();
671 SetInsertionPoint(pos);
2bda0e17
KB
672}
673
cd471848 674long wxTextCtrl::GetInsertionPoint() const
2bda0e17 675{
57c208c5 676#if wxUSE_RICHEDIT
aac7e7fe 677 if ( IsRich() )
a1b82138
VZ
678 {
679 CHARRANGE range;
680 range.cpMin = 0;
681 range.cpMax = 0;
682 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
683 return range.cpMin;
684 }
aac7e7fe 685#endif // wxUSE_RICHEDIT
2bda0e17 686
a1b82138
VZ
687 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
688 return Pos & 0xFFFF;
2bda0e17
KB
689}
690
cd471848 691long wxTextCtrl::GetLastPosition() const
2bda0e17 692{
789295bf 693 HWND hWnd = GetHwnd();
2bda0e17
KB
694
695 // Will always return a number > 0 (according to docs)
696 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
697
698 // This gets the char index for the _beginning_ of the last line
699 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
39136494 700
2bda0e17
KB
701 // Get number of characters in the last line. We'll add this to the character
702 // index for the last line, 1st position.
703 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
704
705 return (long)(charIndex + lineLength);
706}
707
a1b82138
VZ
708// If the return values from and to are the same, there is no
709// selection.
710void wxTextCtrl::GetSelection(long* from, long* to) const
711{
712#if wxUSE_RICHEDIT
aac7e7fe 713 if ( IsRich() )
a1b82138
VZ
714 {
715 CHARRANGE charRange;
aac7e7fe 716 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange);
a1b82138
VZ
717
718 *from = charRange.cpMin;
719 *to = charRange.cpMax;
a1b82138 720 }
4bc1afd5 721 else
aac7e7fe 722#endif // !wxUSE_RICHEDIT
4bc1afd5
VZ
723 {
724 DWORD dwStart, dwEnd;
aac7e7fe 725 ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
a1b82138 726
4bc1afd5
VZ
727 *from = dwStart;
728 *to = dwEnd;
729 }
a1b82138
VZ
730}
731
eef97940 732wxString wxTextCtrl::GetStringSelection() const
18414479
VZ
733{
734 // the base class version works correctly for the rich text controls
735 // because there the lines are terminated with just '\r' which means that
736 // the string length is not changed in the result of the translations doen
737 // in GetValue() but for the normal ones when we replace "\r\n" with '\n'
738 // we break the indices
739#if wxUSE_RICHEDIT
aac7e7fe 740 if ( IsRich() )
eef97940 741 return wxTextCtrlBase::GetStringSelection();
18414479
VZ
742#endif // wxUSE_RICHEDIT
743
744 long from, to;
745 GetSelection(&from, &to);
746
747 wxString str;
748 if ( from < to )
749 {
750 str = wxGetWindowText(GetHWND()).Mid(from, to - from);
751
752 // and now that we have the correct selection, convert it to the
753 // correct format
754 str = wxTextFile::Translate(str, wxTextFileType_Unix);
755 }
756
757 return str;
758}
759
a1b82138
VZ
760bool wxTextCtrl::IsEditable() const
761{
762 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
763
764 return ((style & ES_READONLY) == 0);
765}
766
767// ----------------------------------------------------------------------------
aac7e7fe 768// selection
a1b82138
VZ
769// ----------------------------------------------------------------------------
770
aac7e7fe 771void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 772{
aac7e7fe 773 DoSetSelection(from, to);
2bda0e17
KB
774}
775
aac7e7fe 776void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret)
2bda0e17 777{
aac7e7fe
VZ
778 // if from and to are both -1, it means (in wxWindows) that all text should
779 // be selected - translate into Windows convention
780 if ( (from == -1) && (to == -1) )
781 {
782 from = 0;
783 to = -1;
784 }
785
789295bf 786 HWND hWnd = GetHwnd();
39136494 787
2bda0e17 788#ifdef __WIN32__
aac7e7fe
VZ
789#if wxUSE_RICHEDIT
790 if ( IsRich() )
791 {
792 CHARRANGE range;
793 range.cpMin = from;
794 range.cpMax = to;
795 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
796 }
797 else
798#endif // wxUSE_RICHEDIT
799 {
800 SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to);
801 }
a1b82138 802
aac7e7fe 803 if ( scrollCaret )
2bda0e17 804 {
aac7e7fe 805 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
2bda0e17 806 }
aac7e7fe
VZ
807#else // Win16
808 // WPARAM is 0: selection is scrolled into view
809 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(from, to));
810#endif // Win32/16
811}
812
813// ----------------------------------------------------------------------------
814// Editing
815// ----------------------------------------------------------------------------
39136494 816
aac7e7fe
VZ
817void wxTextCtrl::Replace(long from, long to, const wxString& value)
818{
819 // Set selection and remove it
820 DoSetSelection(from, to, FALSE);
821
822 SendMessage(GetHwnd(), EM_REPLACESEL,
2bda0e17 823#ifdef __WIN32__
aac7e7fe 824 TRUE,
2bda0e17 825#else
aac7e7fe 826 FALSE,
2bda0e17 827#endif
aac7e7fe
VZ
828 (LPARAM)value.c_str());
829}
830
831void wxTextCtrl::Remove(long from, long to)
832{
833 Replace(from, to, _T(""));
2bda0e17
KB
834}
835
836bool wxTextCtrl::LoadFile(const wxString& file)
837{
a1b82138 838 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 839 {
a1b82138
VZ
840 // update the size limit if needed
841 AdjustSpaceLimit();
2bda0e17 842
a1b82138 843 return TRUE;
2bda0e17 844 }
2bda0e17 845
a1b82138 846 return FALSE;
2bda0e17
KB
847}
848
cd471848 849bool wxTextCtrl::IsModified() const
2bda0e17 850{
789295bf 851 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
2bda0e17
KB
852}
853
854// Makes 'unmodified'
cd471848 855void wxTextCtrl::DiscardEdits()
2bda0e17 856{
a1b82138 857 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
858}
859
cd471848 860int wxTextCtrl::GetNumberOfLines() const
2bda0e17 861{
789295bf 862 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
863}
864
debe6624 865long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 866{
789295bf 867 HWND hWnd = GetHwnd();
2bda0e17
KB
868
869 // This gets the char index for the _beginning_ of this line
870 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
871 return (long)(x + charIndex);
872}
873
0efe5ba7 874bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 875{
789295bf 876 HWND hWnd = GetHwnd();
2bda0e17
KB
877
878 // This gets the line number containing the character
0efe5ba7
VZ
879 int lineNo;
880#if wxUSE_RICHEDIT
aac7e7fe 881 if ( IsRich() )
0efe5ba7
VZ
882 {
883 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
884 }
885 else
886#endif // wxUSE_RICHEDIT
887 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
888
889 if ( lineNo == -1 )
890 {
891 // no such line
892 return FALSE;
893 }
894
2bda0e17
KB
895 // This gets the char index for the _beginning_ of this line
896 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
897 if ( charIndex == -1 )
898 {
899 return FALSE;
900 }
901
2bda0e17 902 // The X position must therefore be the different between pos and charIndex
0efe5ba7
VZ
903 if ( x )
904 *x = (long)(pos - charIndex);
905 if ( y )
906 *y = (long)lineNo;
907
908 return TRUE;
2bda0e17
KB
909}
910
debe6624 911void wxTextCtrl::ShowPosition(long pos)
2bda0e17 912{
789295bf 913 HWND hWnd = GetHwnd();
2bda0e17
KB
914
915 // To scroll to a position, we pass the number of lines and characters
916 // to scroll *by*. This means that we need to:
917 // (1) Find the line position of the current line.
918 // (2) Find the line position of pos.
919 // (3) Scroll by (pos - current).
920 // For now, ignore the horizontal scrolling.
921
922 // Is this where scrolling is relative to - the line containing the caret?
923 // Or is the first visible line??? Try first visible line.
924// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
925
926 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
927
928 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 929
2bda0e17
KB
930 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
931
2bda0e17 932 if (linesToScroll != 0)
de4d7713 933 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
934}
935
debe6624 936int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
937{
938 long charIndex = XYToPosition(0, lineNo);
4438caf4 939 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
2bda0e17
KB
940 return len;
941}
942
debe6624 943wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 944{
a1b82138 945 size_t len = (size_t)GetLineLength(lineNo) + 1;
488fe1fe 946
f6bcfd97
BP
947 // there must be at least enough place for the length WORD in the
948 // buffer
949 len += sizeof(WORD);
4438caf4 950
f6bcfd97
BP
951 wxString str;
952 wxChar *buf = str.GetWriteBuf(len);
953
33ac7e6f 954 *(WORD *)buf = (WORD)len;
f6bcfd97
BP
955 len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
956 buf[len] = 0;
4438caf4 957
f6bcfd97 958 str.UngetWriteBuf(len);
4438caf4
VZ
959
960 return str;
2bda0e17
KB
961}
962
d7eee191
VZ
963void wxTextCtrl::SetMaxLength(unsigned long len)
964{
965 ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0);
966}
967
a1b82138 968// ----------------------------------------------------------------------------
ca8b28f2 969// Undo/redo
a1b82138
VZ
970// ----------------------------------------------------------------------------
971
ca8b28f2
JS
972void wxTextCtrl::Undo()
973{
974 if (CanUndo())
975 {
789295bf 976 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
977 }
978}
979
980void wxTextCtrl::Redo()
981{
982 if (CanRedo())
983 {
984 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 985 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
986 }
987}
988
989bool wxTextCtrl::CanUndo() const
990{
789295bf 991 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
992}
993
994bool wxTextCtrl::CanRedo() const
995{
789295bf 996 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
997}
998
a1b82138
VZ
999// ----------------------------------------------------------------------------
1000// implemenation details
1001// ----------------------------------------------------------------------------
39136494 1002
2bda0e17
KB
1003void wxTextCtrl::Command(wxCommandEvent & event)
1004{
a1b82138
VZ
1005 SetValue(event.GetString());
1006 ProcessCommand (event);
2bda0e17
KB
1007}
1008
1009void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
1010{
a1b82138
VZ
1011 // By default, load the first file into the text window.
1012 if (event.GetNumberOfFiles() > 0)
1013 {
1014 LoadFile(event.GetFiles()[0]);
1015 }
2bda0e17
KB
1016}
1017
a37d422a
VZ
1018// ----------------------------------------------------------------------------
1019// kbd input processing
1020// ----------------------------------------------------------------------------
1021
1022bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* pMsg)
1023{
1024 MSG *msg = (MSG *)pMsg;
1025
1026 // check for our special keys here: if we don't do it and the parent frame
1027 // uses them as accelerators, they wouldn't work at all, so we disable
1028 // usual preprocessing for them
1029 if ( msg->message == WM_KEYDOWN )
1030 {
1031 WORD vkey = msg->wParam;
1032 if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN )
1033 {
1034 if ( vkey == VK_BACK )
1035 return FALSE;
1036 }
1037 else // no Alt
1038 {
1039 if ( wxIsCtrlDown() )
1040 {
1041 switch ( vkey )
1042 {
1043 case 'C':
1044 case 'V':
1045 case 'X':
1046 case VK_INSERT:
1047 case VK_DELETE:
1048 case VK_HOME:
1049 case VK_END:
1050 return FALSE;
1051 }
1052 }
1053 else if ( wxIsShiftDown() )
1054 {
1055 if ( vkey == VK_INSERT || vkey == VK_DELETE )
1056 return FALSE;
1057 }
1058 }
1059 }
1060
1061 return wxControl::MSWShouldPreProcessMessage(pMsg);
1062}
1063
2bda0e17
KB
1064void wxTextCtrl::OnChar(wxKeyEvent& event)
1065{
42e69d6b 1066 switch ( event.KeyCode() )
cd471848 1067 {
cd471848 1068 case WXK_RETURN:
39136494 1069 if ( !(m_windowStyle & wxTE_MULTILINE) )
cd471848
VZ
1070 {
1071 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
bfbd6dc1 1072 InitCommandEvent(event);
f6bcfd97 1073 event.SetString(GetValue());
cd471848
VZ
1074 if ( GetEventHandler()->ProcessEvent(event) )
1075 return;
1076 }
5fb9fcfc
VZ
1077 //else: multiline controls need Enter for themselves
1078
1079 break;
4d91c1d1 1080
cd471848 1081 case WXK_TAB:
319fefa9
VZ
1082 // always produce navigation event - even if we process TAB
1083 // ourselves the fact that we got here means that the user code
1084 // decided to skip processing of this TAB - probably to let it
1085 // do its default job.
cd471848 1086 {
5fb9fcfc
VZ
1087 wxNavigationKeyEvent eventNav;
1088 eventNav.SetDirection(!event.ShiftDown());
8614c467 1089 eventNav.SetWindowChange(event.ControlDown());
5fb9fcfc 1090 eventNav.SetEventObject(this);
39136494 1091
d9506e77 1092 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
cd471848
VZ
1093 return;
1094 }
341c92a8 1095 break;
cd471848 1096 }
39136494 1097
8614c467 1098 // no, we didn't process it
42e69d6b 1099 event.Skip();
2bda0e17
KB
1100}
1101
debe6624 1102bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 1103{
789295bf
VZ
1104 switch (param)
1105 {
1106 case EN_SETFOCUS:
1107 case EN_KILLFOCUS:
1108 {
1109 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
d7eee191
VZ
1110 : wxEVT_SET_FOCUS,
1111 m_windowId);
789295bf
VZ
1112 event.SetEventObject( this );
1113 GetEventHandler()->ProcessEvent(event);
1114 }
1115 break;
ae29de83 1116
789295bf
VZ
1117 case EN_CHANGE:
1118 {
1119 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
bfbd6dc1
VZ
1120 InitCommandEvent(event);
1121 event.SetString(GetValue());
1122 ProcessCommand(event);
789295bf
VZ
1123 }
1124 break;
2bda0e17 1125
b12915c1 1126 case EN_MAXTEXT:
789295bf 1127 // the text size limit has been hit - increase it
d7eee191
VZ
1128 if ( !AdjustSpaceLimit() )
1129 {
1130 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId);
1131 InitCommandEvent(event);
1132 event.SetString(GetValue());
1133 ProcessCommand(event);
1134 }
789295bf
VZ
1135 break;
1136
1137 // the other notification messages are not processed
1138 case EN_UPDATE:
b12915c1 1139 case EN_ERRSPACE:
789295bf
VZ
1140 case EN_HSCROLL:
1141 case EN_VSCROLL:
f6bcfd97 1142 return FALSE;
789295bf
VZ
1143 default:
1144 return FALSE;
1145 }
1146
1147 // processed
1148 return TRUE;
2bda0e17
KB
1149}
1150
33ac7e6f 1151WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
1152#if wxUSE_CTL3D
1153 WXUINT message,
1154 WXWPARAM wParam,
1155 WXLPARAM lParam
1156#else
33ac7e6f
KB
1157 WXUINT WXUNUSED(message),
1158 WXWPARAM WXUNUSED(wParam),
788722ac
JS
1159 WXLPARAM WXUNUSED(lParam)
1160#endif
1161 )
f6bcfd97
BP
1162{
1163#if wxUSE_CTL3D
1164 if ( m_useCtl3D )
1165 {
1166 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
1167 return (WXHBRUSH) hbrush;
1168 }
1169#endif // wxUSE_CTL3D
1170
1171 HDC hdc = (HDC)pDC;
1172 if (GetParent()->GetTransparentBackground())
1173 SetBkMode(hdc, TRANSPARENT);
1174 else
1175 SetBkMode(hdc, OPAQUE);
1176
1177 wxColour colBack = GetBackgroundColour();
1178
1179 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
1180 colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
1181
1182 ::SetBkColor(hdc, wxColourToRGB(colBack));
1183 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
1184
1185 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
1186
1187 return (WXHBRUSH)brush->GetResourceHandle();
1188}
1189
1190// In WIN16, need to override normal erasing because
1191// Ctl3D doesn't use the wxWindows background colour.
1192#ifdef __WIN16__
1193void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
1194{
1195 wxColour col(m_backgroundColour);
1196
1197#if wxUSE_CTL3D
1198 if (m_useCtl3D)
1199 col = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW);
1200#endif
1201
1202 RECT rect;
1203 ::GetClientRect(GetHwnd(), &rect);
1204
1205 COLORREF ref = PALETTERGB(col.Red(),
1206 col.Green(),
1207 col.Blue());
1208 HBRUSH hBrush = ::CreateSolidBrush(ref);
1209 if ( !hBrush )
1210 wxLogLastError(wxT("CreateSolidBrush"));
1211
1212 HDC hdc = (HDC)event.GetDC()->GetHDC();
1213
1214 int mode = ::SetMapMode(hdc, MM_TEXT);
1215
1216 ::FillRect(hdc, &rect, hBrush);
1217 ::DeleteObject(hBrush);
1218 ::SetMapMode(hdc, mode);
1219
1220}
aac7e7fe 1221#endif // Win16
f6bcfd97 1222
d7eee191 1223bool wxTextCtrl::AdjustSpaceLimit()
789295bf 1224{
25889d3c 1225#ifndef __WIN16__
d7eee191
VZ
1226 unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
1227
1228 // HACK: we try to automatically extend the limit for the amount of text
1229 // to allow (interactively) entering more than 64Kb of text under
1230 // Win9x but we shouldn't reset the text limit which was previously
1231 // set explicitly with SetMaxLength()
1232 //
1233 // we could solve this by storing the limit we set in wxTextCtrl but
1234 // to save space we prefer to simply test here the actual limit
1235 // value: we consider that SetMaxLength() can only be called for
1236 // values < 32Kb
1237 if ( limit < 0x8000 )
1238 {
1239 // we've got more text than limit set by SetMaxLength()
1240 return FALSE;
1241 }
1242
1243 unsigned int len = ::GetWindowTextLength(GetHwnd());
17d8ee1c 1244 if ( len >= limit )
789295bf
VZ
1245 {
1246 limit = len + 0x8000; // 32Kb
1247
5ea105e0 1248#if wxUSE_RICHEDIT
aac7e7fe 1249 if ( IsRich() )
b12915c1
VZ
1250 {
1251 // as a nice side effect, this also allows passing limit > 64Kb
1252 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
1253 }
789295bf 1254 else
b12915c1
VZ
1255#endif // wxUSE_RICHEDIT
1256 {
1257 if ( limit > 0xffff )
1258 {
1259 // this will set it to a platform-dependent maximum (much more
1260 // than 64Kb under NT)
1261 limit = 0;
1262 }
1263
789295bf 1264 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
b12915c1 1265 }
789295bf 1266 }
b12915c1 1267#endif // !Win16
d7eee191
VZ
1268
1269 // we changed the limit
1270 return TRUE;
789295bf 1271}
2bda0e17 1272
a1b82138 1273bool wxTextCtrl::AcceptsFocus() const
2bda0e17 1274{
a1b82138
VZ
1275 // we don't want focus if we can't be edited
1276 return IsEditable() && wxControl::AcceptsFocus();
1277}
c085e333 1278
f68586e5 1279wxSize wxTextCtrl::DoGetBestSize() const
a1b82138
VZ
1280{
1281 int cx, cy;
1282 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1283
1284 int wText = DEFAULT_ITEM_WIDTH;
1285
1286 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1287 if ( m_windowStyle & wxTE_MULTILINE )
1288 {
3988b155 1289 hText *= wxMax(GetNumberOfLines(), 5);
a1b82138
VZ
1290 }
1291 //else: for single line control everything is ok
1292
1293 return wxSize(wText, hText);
2bda0e17 1294}
a1b82138
VZ
1295
1296// ----------------------------------------------------------------------------
1297// standard handlers for standard edit menu events
1298// ----------------------------------------------------------------------------
2bda0e17 1299
bfbd6dc1 1300void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1301{
1302 Cut();
1303}
1304
bfbd6dc1 1305void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1306{
1307 Copy();
1308}
1309
bfbd6dc1 1310void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1311{
1312 Paste();
1313}
1314
bfbd6dc1 1315void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1316{
1317 Undo();
1318}
1319
bfbd6dc1 1320void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1321{
1322 Redo();
1323}
1324
1325void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1326{
1327 event.Enable( CanCut() );
1328}
1329
1330void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1331{
1332 event.Enable( CanCopy() );
1333}
1334
1335void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1336{
1337 event.Enable( CanPaste() );
1338}
1339
1340void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1341{
1342 event.Enable( CanUndo() );
1343}
1344
1345void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1346{
1347 event.Enable( CanRedo() );
1348}
1349
4bc1afd5
VZ
1350// the rest of the file only deals with the rich edit controls
1351#if wxUSE_RICHEDIT
1352
c57e3339
VZ
1353// ----------------------------------------------------------------------------
1354// EN_LINK processing
1355// ----------------------------------------------------------------------------
1356
1357bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1358{
1359 NMHDR *hdr = (NMHDR* )lParam;
1360 if ( hdr->code == EN_LINK )
1361 {
1362 ENLINK *enlink = (ENLINK *)hdr;
1363
1364 switch ( enlink->msg )
1365 {
1366 case WM_SETCURSOR:
1367 // ok, so it is hardcoded - do we really nee to customize it?
1368 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND)));
1369 *result = TRUE;
1370 break;
1371
1372 case WM_MOUSEMOVE:
1373 case WM_LBUTTONDOWN:
1374 case WM_LBUTTONUP:
1375 case WM_LBUTTONDBLCLK:
1376 case WM_RBUTTONDOWN:
1377 case WM_RBUTTONUP:
1378 case WM_RBUTTONDBLCLK:
1379 // send a mouse event
1380 {
1381 static const wxEventType eventsMouse[] =
1382 {
1383 wxEVT_MOTION,
1384 wxEVT_LEFT_DOWN,
1385 wxEVT_LEFT_UP,
1386 wxEVT_LEFT_DCLICK,
1387 wxEVT_RIGHT_DOWN,
1388 wxEVT_RIGHT_UP,
1389 wxEVT_RIGHT_DCLICK,
1390 };
1391
1392 // the event ids are consecutive
1393 wxMouseEvent
1394 evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]);
1395
1396 InitMouseEvent(evtMouse,
1397 GET_X_LPARAM(enlink->lParam),
1398 GET_Y_LPARAM(enlink->lParam),
1399 enlink->wParam);
1400
1401 wxTextUrlEvent event(m_windowId, evtMouse,
1402 enlink->chrg.cpMin,
1403 enlink->chrg.cpMax);
1404
1405 InitCommandEvent(event);
1406
1407 *result = ProcessCommand(event);
1408 }
1409 break;
1410 }
1411
1412 return TRUE;
1413 }
1414
1415 // not processed
1416 return FALSE;
1417}
1418
f6bcfd97
BP
1419// ----------------------------------------------------------------------------
1420// colour setting for the rich edit controls
1421// ----------------------------------------------------------------------------
1422
f6bcfd97
BP
1423// Watcom C++ doesn't define this
1424#ifndef SCF_ALL
1425#define SCF_ALL 0x0004
1426#endif
1427
1428bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
1429{
1430 if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
1431 {
1432 // colour didn't really change
1433 return FALSE;
1434 }
1435
1436 if ( IsRich() )
1437 {
1438 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1439 // EM_SETBKGNDCOLOR additionally
1440 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
1441 }
1442
1443 return TRUE;
1444}
1445
1446bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1447{
1448 if ( !wxTextCtrlBase::SetForegroundColour(colour) )
1449 {
1450 // colour didn't really change
1451 return FALSE;
1452 }
1453
1454 if ( IsRich() )
1455 {
1456 // change the colour of everything
1457 CHARFORMAT cf;
1458 wxZeroMemory(cf);
1459 cf.cbSize = sizeof(cf);
1460 cf.dwMask = CFM_COLOR;
1461 cf.crTextColor = wxColourToRGB(colour);
1462 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
1463 }
1464
1465 return TRUE;
1466}
1467
4bc1afd5
VZ
1468// ----------------------------------------------------------------------------
1469// styling support for rich edit controls
1470// ----------------------------------------------------------------------------
1471
1472bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
1473{
1474 if ( !IsRich() )
1475 {
1476 // can't do it with normal text control
1477 return FALSE;
1478 }
1479
1480 // the rich text control doesn't handle setting background colour, so don't
1481 // even try if it's the only thing we want to change
be329a3d
RD
1482 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1483 !style.HasFont() && !style.HasTextColour() )
4bc1afd5 1484 {
be329a3d
RD
1485 // nothing to do: return TRUE if there was really nothing to do and
1486 // FALSE if we failed to set bg colour
4bc1afd5
VZ
1487 return !style.HasBackgroundColour();
1488 }
1489
1490 // order the range if needed
1491 if ( start > end )
1492 {
1493 long tmp = start;
1494 start = end;
1495 end = tmp;
1496 }
1497
1498 // we can only change the format of the selection, so select the range we
1499 // want and restore the old selection later
1500 long startOld, endOld;
1501 GetSelection(&startOld, &endOld);
1502
1503 // but do we really have to change the selection?
1504 bool changeSel = start != startOld || end != endOld;
1505
1506 if ( changeSel )
aac7e7fe
VZ
1507 {
1508 DoSetSelection(start, end, FALSE);
1509 }
4bc1afd5
VZ
1510
1511 // initialize CHARFORMAT struct
be329a3d
RD
1512#if wxUSE_RICHEDIT2
1513 CHARFORMAT2 cf;
1514#else
4bc1afd5 1515 CHARFORMAT cf;
be329a3d 1516#endif
4bc1afd5
VZ
1517 wxZeroMemory(cf);
1518 cf.cbSize = sizeof(cf);
1519
1520 if ( style.HasFont() )
1521 {
aac7e7fe
VZ
1522 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1523 // but using it doesn't seem to hurt neither so leaving it for now
1524
784164e1
VZ
1525 cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET |
1526 CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE;
4bc1afd5
VZ
1527
1528 // fill in data from LOGFONT but recalculate lfHeight because we need
1529 // the real height in twips and not the negative number which
1530 // wxFillLogFont() returns (this is correct in general and works with
1531 // the Windows font mapper, but not here)
1532 LOGFONT lf;
1533 wxFillLogFont(&lf, &style.GetFont());
1534 cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips
1535 cf.bCharSet = lf.lfCharSet;
1536 cf.bPitchAndFamily = lf.lfPitchAndFamily;
1537 wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
1538
784164e1
VZ
1539 // also deal with underline/italic/bold attributes: note that we must
1540 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1541 // style to allow clearing it
4bc1afd5
VZ
1542 if ( lf.lfItalic )
1543 {
4bc1afd5
VZ
1544 cf.dwEffects |= CFE_ITALIC;
1545 }
1546
1547 if ( lf.lfWeight == FW_BOLD )
1548 {
4bc1afd5
VZ
1549 cf.dwEffects |= CFE_BOLD;
1550 }
1551
1552 if ( lf.lfUnderline )
1553 {
4bc1afd5
VZ
1554 cf.dwEffects |= CFE_UNDERLINE;
1555 }
1556
1557 // strikeout fonts are not supported by wxWindows
1558 }
1559
1560 if ( style.HasTextColour() )
1561 {
1562 cf.dwMask |= CFM_COLOR;
1563 cf.crTextColor = wxColourToRGB(style.GetTextColour());
1564 }
1565
be329a3d
RD
1566#if wxUSE_RICHEDIT2
1567 if ( wxRichEditModule::GetLoadedVersion() > 1 && style.HasBackgroundColour() )
1568 {
1569 cf.dwMask |= CFM_BACKCOLOR;
1570 cf.crBackColor = wxColourToRGB(style.GetBackgroundColour());
1571 }
784164e1
VZ
1572#endif // wxUSE_RICHEDIT2
1573
4bc1afd5
VZ
1574 // do format the selection
1575 bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
1576 SCF_SELECTION, (LPARAM)&cf) != 0;
1577 if ( !ok )
1578 {
1579 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1580 }
1581
1582 if ( changeSel )
1583 {
1584 // restore the original selection
aac7e7fe 1585 DoSetSelection(startOld, endOld, FALSE);
4bc1afd5
VZ
1586 }
1587
1588 return ok;
1589}
f6bcfd97 1590
b12915c1
VZ
1591// ----------------------------------------------------------------------------
1592// wxRichEditModule
1593// ----------------------------------------------------------------------------
1594
b12915c1
VZ
1595bool wxRichEditModule::OnInit()
1596{
1597 // don't do anything - we will load it when needed
1598 return TRUE;
1599}
1600
1601void wxRichEditModule::OnExit()
1602{
1603 if ( ms_hRichEdit )
1604 {
1605 FreeLibrary(ms_hRichEdit);
1606 }
1607}
1608
1609/* static */
1610bool wxRichEditModule::Load(int version)
1611{
1612 wxCHECK_MSG( version >= 1 && version <= 3, FALSE,
1613 _T("incorrect richedit control version requested") );
1614
1615 if ( version <= ms_verRichEdit )
1616 {
1617 // we've already got this or better
1618 return TRUE;
1619 }
1620
1621 if ( ms_hRichEdit )
1622 {
1623 ::FreeLibrary(ms_hRichEdit);
1624 }
1625
1626 // always try load riched20.dll first - like this we won't have to reload
1627 // it later if we're first asked for RE 1 and then for RE 2 or 3
1628 wxString dllname = _T("riched20.dll");
1629 ms_hRichEdit = ::LoadLibrary(dllname);
1630 ms_verRichEdit = 2; // no way to tell if it's 2 or 3, assume 2
1631
1632 if ( !ms_hRichEdit && (version == 1) )
1633 {
1634 // fall back to RE 1
1635 dllname = _T("riched32.dll");
1636 ms_hRichEdit = ::LoadLibrary(dllname);
1637 ms_verRichEdit = 1;
1638 }
1639
1640 if ( !ms_hRichEdit )
1641 {
1642 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
1643
1644 ms_verRichEdit = -1;
1645
1646 return FALSE;
1647 }
1648
1649 return TRUE;
1650}
1651
1652#endif // wxUSE_RICHEDIT
1653
1e6feb95 1654#endif // wxUSE_TEXTCTRL