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