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