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