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