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