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