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