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