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