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