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