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