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