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