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