]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textctrl.cpp
keep selected image in sync with the normal one if it hasn't been changed (patch...
[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
9 // Licence: wxWindows licence
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 #include "wx/menu.h"
42 #endif
43
44 #include "wx/module.h"
45
46 #if wxUSE_CLIPBOARD
47 #include "wx/clipbrd.h"
48 #endif
49
50 #include "wx/textfile.h"
51
52 #include <windowsx.h>
53
54 #include "wx/msw/private.h"
55
56 #include <string.h>
57 #include <stdlib.h>
58 #include <sys/types.h>
59
60 #if wxUSE_RICHEDIT
61
62 // old mingw32 has richedit stuff directly in windows.h and doesn't have
63 // richedit.h at all
64 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
65 #include <richedit.h>
66 #endif
67
68 #include "wx/msw/missing.h"
69
70 #endif // wxUSE_RICHEDIT
71
72 // ----------------------------------------------------------------------------
73 // private functions
74 // ----------------------------------------------------------------------------
75
76 #if wxUSE_RICHEDIT
77
78 DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb);
79
80 #endif // wxUSE_RICHEDIT
81
82 // ----------------------------------------------------------------------------
83 // private classes
84 // ----------------------------------------------------------------------------
85
86 #if wxUSE_RICHEDIT
87
88 // this module initializes RichEdit DLL(s) if needed
89 class wxRichEditModule : public wxModule
90 {
91 public:
92 virtual bool OnInit();
93 virtual void OnExit();
94
95 // load the richedit DLL of at least of required version
96 static bool Load(int version = 1);
97
98 private:
99 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
100 static HINSTANCE ms_hRichEdit[2];
101
102 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
103 };
104
105 HINSTANCE wxRichEditModule::ms_hRichEdit[2] = { NULL, NULL };
106
107 IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
108
109 #endif // wxUSE_RICHEDIT
110
111 // ----------------------------------------------------------------------------
112 // event tables and other macros
113 // ----------------------------------------------------------------------------
114
115 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
116
117 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
118 EVT_CHAR(wxTextCtrl::OnChar)
119 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
120
121 #if wxUSE_RICHEDIT
122 EVT_RIGHT_UP(wxTextCtrl::OnRightClick)
123 #endif
124
125 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
126 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
127 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
128 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
129 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
130 EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete)
131 EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll)
132
133 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
134 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
135 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
136 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
137 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
138 EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete)
139 EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll)
140 #ifdef __WIN16__
141 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
142 #endif
143 END_EVENT_TABLE()
144
145 // ============================================================================
146 // implementation
147 // ============================================================================
148
149 // ----------------------------------------------------------------------------
150 // creation
151 // ----------------------------------------------------------------------------
152
153 void wxTextCtrl::Init()
154 {
155 #if wxUSE_RICHEDIT
156 m_verRichEdit = 0;
157 #endif // wxUSE_RICHEDIT
158
159 m_privateContextMenu = NULL;
160 m_suppressNextUpdate = FALSE;
161 }
162
163 wxTextCtrl::~wxTextCtrl()
164 {
165 if (m_privateContextMenu)
166 {
167 delete m_privateContextMenu;
168 m_privateContextMenu = NULL;
169 }
170 }
171
172 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
173 const wxString& value,
174 const wxPoint& pos,
175 const wxSize& size,
176 long style,
177 const wxValidator& validator,
178 const wxString& name)
179 {
180 // base initialization
181 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
182 return FALSE;
183
184 if ( parent )
185 parent->AddChild(this);
186
187 // translate wxWin style flags to MSW ones
188 WXDWORD msStyle = MSWGetCreateWindowFlags();
189
190 // do create the control - either an EDIT or RICHEDIT
191 wxString windowClass = wxT("EDIT");
192
193 #if wxUSE_RICHEDIT
194 if ( m_windowStyle & wxTE_AUTO_URL )
195 {
196 // automatic URL detection only works in RichEdit 2.0+
197 m_windowStyle |= wxTE_RICH2;
198 }
199
200 if ( m_windowStyle & wxTE_RICH2 )
201 {
202 // using richedit 2.0 implies using wxTE_RICH
203 m_windowStyle |= wxTE_RICH;
204 }
205
206 // we need to load the richedit DLL before creating the rich edit control
207 if ( m_windowStyle & wxTE_RICH )
208 {
209 static bool s_errorGiven = FALSE;// MT-FIXME
210
211 // Which version do we need? Use 1.0 by default because it is much more
212 // like the the standard EDIT or 2.0 if explicitly requested, but use
213 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
214 //
215 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
216 // (and thus EDIT) much better than RichEdit 2.0 so we probably
217 // should use 3.0 if available as it is the best of both worlds -
218 // but as I can't test it right now I don't do it (VZ)
219 #if wxUSE_UNICODE
220 const int verRichEdit = 2;
221 #else // !wxUSE_UNICODE
222 int verRichEdit = m_windowStyle & wxTE_RICH2 ? 2 : 1;
223 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
224
225 // only give the error msg once if the DLL can't be loaded
226 if ( !s_errorGiven )
227 {
228 // try to load the RichEdit DLL (will do nothing if already done)
229 if ( !wxRichEditModule::Load(verRichEdit) )
230 {
231 #if !wxUSE_UNICODE
232 // try another version?
233 verRichEdit = 3 - verRichEdit; // 1 <-> 2
234
235 if ( !wxRichEditModule::Load(verRichEdit) )
236 #endif // wxUSE_UNICODE
237 {
238 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
239
240 s_errorGiven = TRUE;
241 }
242 }
243 }
244
245 // have we managed to load any richedit version?
246 if ( !s_errorGiven )
247 {
248 m_verRichEdit = verRichEdit;
249 if ( m_verRichEdit == 1 )
250 {
251 windowClass = wxT("RICHEDIT");
252 }
253 else
254 {
255 #ifndef RICHEDIT_CLASS
256 wxString RICHEDIT_CLASS;
257 RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), m_verRichEdit);
258 #if wxUSE_UNICODE
259 RICHEDIT_CLASS += _T('W');
260 #else // ANSI
261 RICHEDIT_CLASS += _T('A');
262 #endif // Unicode/ANSI
263 #endif // !RICHEDIT_CLASS
264
265 windowClass = RICHEDIT_CLASS;
266 }
267 }
268 }
269 #endif // wxUSE_RICHEDIT
270
271 // we need to turn '\n's into "\r\n"s for the multiline controls
272 wxString valueWin;
273 if ( m_windowStyle & wxTE_MULTILINE )
274 {
275 valueWin = wxTextFile::Translate(value, wxTextFileType_Dos);
276 }
277 else // single line
278 {
279 valueWin = value;
280 }
281
282 if ( !MSWCreateControl(windowClass, msStyle, pos, size, valueWin) )
283 return FALSE;
284
285 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
286
287 #if wxUSE_RICHEDIT
288 if ( IsRich() )
289 {
290 // enable the events we're interested in: we want to get EN_CHANGE as
291 // for the normal controls
292 LPARAM mask = ENM_CHANGE;
293
294 if ( GetRichVersion() == 1 )
295 {
296 // we also need EN_MSGFILTER for richedit 1.0 for the reasons
297 // explained in its handler
298 mask |= ENM_MOUSEEVENTS;
299 }
300 else if ( m_windowStyle & wxTE_AUTO_URL )
301 {
302 mask |= ENM_LINK;
303
304 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0);
305 }
306
307 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask);
308 }
309 #endif // wxUSE_RICHEDIT
310
311 return TRUE;
312 }
313
314 // Make sure the window style (etc.) reflects the HWND style (roughly)
315 void wxTextCtrl::AdoptAttributesFromHWND()
316 {
317 wxWindow::AdoptAttributesFromHWND();
318
319 HWND hWnd = GetHwnd();
320 long style = ::GetWindowLong(hWnd, GWL_STYLE);
321
322 // retrieve the style to see whether this is an edit or richedit ctrl
323 #if wxUSE_RICHEDIT
324 wxString classname = wxGetWindowClass(GetHWND());
325
326 if ( classname.IsSameAs(_T("EDIT"), FALSE /* no case */) )
327 {
328 m_verRichEdit = 0;
329 }
330 else // rich edit?
331 {
332 wxChar c;
333 if ( wxSscanf(classname, _T("RichEdit%d0%c"), &m_verRichEdit, &c) != 2 )
334 {
335 wxLogDebug(_T("Unknown edit control '%s'."), classname.c_str());
336
337 m_verRichEdit = 0;
338 }
339 }
340 #endif // wxUSE_RICHEDIT
341
342 if (style & ES_MULTILINE)
343 m_windowStyle |= wxTE_MULTILINE;
344 if (style & ES_PASSWORD)
345 m_windowStyle |= wxTE_PASSWORD;
346 if (style & ES_READONLY)
347 m_windowStyle |= wxTE_READONLY;
348 if (style & ES_WANTRETURN)
349 m_windowStyle |= wxTE_PROCESS_ENTER;
350 if (style & ES_CENTER)
351 m_windowStyle |= wxTE_CENTRE;
352 if (style & ES_RIGHT)
353 m_windowStyle |= wxTE_RIGHT;
354 }
355
356 WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
357 {
358 long msStyle = wxControl::MSWGetStyle(style, exstyle);
359
360 // styles which we alaways add by default
361 if ( style & wxTE_MULTILINE )
362 {
363 wxASSERT_MSG( !(style & wxTE_PROCESS_ENTER),
364 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
365
366 msStyle |= ES_MULTILINE | ES_WANTRETURN;
367 if ( !(style & wxTE_NO_VSCROLL) )
368 {
369 // always adjust the vertical scrollbar automatically if we have it
370 msStyle |= WS_VSCROLL | ES_AUTOVSCROLL;
371
372 #if wxUSE_RICHEDIT
373 // we have to use this style for the rich edit controls because
374 // without it the vertical scrollbar never appears at all in
375 // richedit 3.0 because of our ECO_NOHIDESEL hack (search for it)
376 if ( style & wxTE_RICH2 )
377 {
378 msStyle |= ES_DISABLENOSCROLL;
379 }
380 #endif // wxUSE_RICHEDIT
381 }
382
383 style |= wxTE_PROCESS_ENTER;
384 }
385 else // !multiline
386 {
387 // there is really no reason to not have this style for single line
388 // text controls
389 msStyle |= ES_AUTOHSCROLL;
390 }
391
392 // styles which we add depending on the specified wxWindows styles
393 if ( style & wxHSCROLL )
394 {
395 // automatically scroll the control horizontally as necessary
396 msStyle |= WS_HSCROLL;// | ES_AUTOHSCROLL;
397 }
398
399 if ( style & wxTE_READONLY )
400 msStyle |= ES_READONLY;
401
402 if ( style & wxTE_PASSWORD )
403 msStyle |= ES_PASSWORD;
404
405 if ( style & wxTE_NOHIDESEL )
406 msStyle |= ES_NOHIDESEL;
407
408 // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0
409 if ( style & wxTE_CENTRE )
410 msStyle |= ES_CENTER;
411 else if ( style & wxTE_RIGHT )
412 msStyle |= ES_RIGHT;
413 else
414 msStyle |= ES_LEFT; // ES_LEFT if 0 as well but for consistency...
415
416 return msStyle;
417 }
418
419 void wxTextCtrl::SetWindowStyleFlag(long style)
420 {
421 #if wxUSE_RICHEDIT
422 // we have to deal with some styles separately because they can't be
423 // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed
424 // using richedit-specific EM_SETOPTIONS
425 if ( IsRich() &&
426 ((style & wxTE_NOHIDESEL) != (GetWindowStyle() & wxTE_NOHIDESEL)) )
427 {
428 bool set = (style & wxTE_NOHIDESEL) != 0;
429
430 ::SendMessage(GetHwnd(), EM_SETOPTIONS, set ? ECOOP_OR : ECOOP_AND,
431 set ? ECO_NOHIDESEL : ~ECO_NOHIDESEL);
432 }
433 #endif // wxUSE_RICHEDIT
434
435 wxControl::SetWindowStyleFlag(style);
436 }
437
438 // ----------------------------------------------------------------------------
439 // set/get the controls text
440 // ----------------------------------------------------------------------------
441
442 wxString wxTextCtrl::GetValue() const
443 {
444 // range 0..-1 is special for GetRange() and means to retrieve all text
445 return GetRange(0, -1);
446 }
447
448 wxString wxTextCtrl::GetRange(long from, long to) const
449 {
450 wxString str;
451
452 if ( from >= to && to != -1 )
453 {
454 // nothing to retrieve
455 return str;
456 }
457
458 #if wxUSE_RICHEDIT
459 if ( IsRich() )
460 {
461 int len = GetWindowTextLength(GetHwnd());
462 if ( len > from )
463 {
464 // alloc one extra WORD as needed by the control
465 wxChar *p = str.GetWriteBuf(++len);
466
467 TEXTRANGE textRange;
468 textRange.chrg.cpMin = from;
469 textRange.chrg.cpMax = to == -1 ? len : to;
470 textRange.lpstrText = p;
471
472 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
473
474 if ( m_verRichEdit > 1 )
475 {
476 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
477 // neither Unix nor Windows style - convert it to something
478 // reasonable
479 for ( ; *p; p++ )
480 {
481 if ( *p == _T('\r') )
482 *p = _T('\n');
483 }
484 }
485
486 str.UngetWriteBuf();
487
488 if ( m_verRichEdit == 1 )
489 {
490 // convert to the canonical form - see comment below
491 str = wxTextFile::Translate(str, wxTextFileType_Unix);
492 }
493 }
494 //else: no text at all, leave the string empty
495 }
496 else
497 #endif // wxUSE_RICHEDIT
498 {
499 // retrieve all text
500 str = wxGetWindowText(GetHWND());
501
502 // need only a range?
503 if ( from < to )
504 {
505 str = str.Mid(from, to - from);
506 }
507
508 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
509 // canonical one (same one as above) for consistency with the other kinds
510 // of controls and, more importantly, with the other ports
511 str = wxTextFile::Translate(str, wxTextFileType_Unix);
512 }
513
514 return str;
515 }
516
517 void wxTextCtrl::SetValue(const wxString& value)
518 {
519 // if the text is long enough, it's faster to just set it instead of first
520 // comparing it with the old one (chances are that it will be different
521 // anyhow, this comparison is there to avoid flicker for small single-line
522 // edit controls mostly)
523 if ( (value.length() > 0x400) || (value != GetValue()) )
524 {
525 DoWriteText(value, FALSE /* not selection only */);
526 }
527
528 // we should reset the modified flag even if the value didn't really change
529
530 // mark the control as being not dirty - we changed its text, not the
531 // user
532 DiscardEdits();
533
534 // for compatibility, don't move the cursor when doing SetValue()
535 SetInsertionPoint(0);
536 }
537
538 #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
539
540 DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb)
541 {
542 *pcb = 0;
543
544 wchar_t *wbuf = (wchar_t *)buf;
545 const wchar_t *wpc = *(const wchar_t **)dwCookie;
546 while ( cb && *wpc )
547 {
548 *wbuf++ = *wpc++;
549
550 cb -= sizeof(wchar_t);
551 (*pcb) += sizeof(wchar_t);
552 }
553
554 *(const wchar_t **)dwCookie = wpc;
555
556 return 0;
557 }
558
559 // from utils.cpp
560 extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding);
561
562 #if wxUSE_UNICODE_MSLU
563 bool wxTextCtrl::StreamIn(const wxString& value,
564 wxFontEncoding WXUNUSED(encoding),
565 bool selectionOnly)
566 {
567 const wchar_t *wpc = value.c_str();
568 #else // !wxUSE_UNICODE_MSLU
569 bool wxTextCtrl::StreamIn(const wxString& value,
570 wxFontEncoding encoding,
571 bool selectionOnly)
572 {
573 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
574 // text in the non default charset -- otherwise it thinks it knows better
575 // than we do and always shows it in the default one
576
577 // first get the Windows code page for this encoding
578 long codepage = wxEncodingToCodepage(encoding);
579 if ( codepage == -1 )
580 {
581 // unknown encoding
582 return FALSE;
583 }
584
585 // next translate to Unicode using this code page
586 int len = ::MultiByteToWideChar(codepage, 0, value, -1, NULL, 0);
587
588 #if wxUSE_WCHAR_T
589 wxWCharBuffer wchBuf(len);
590 #else
591 wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t));
592 #endif
593
594 if ( !::MultiByteToWideChar(codepage, 0, value, -1,
595 (wchar_t *)(const wchar_t *)wchBuf, len) )
596 {
597 wxLogLastError(_T("MultiByteToWideChar"));
598 }
599
600 // finally, stream it in the control
601 const wchar_t *wpc = wchBuf;
602 #endif // wxUSE_UNICODE_MSLU
603
604 EDITSTREAM eds;
605 wxZeroMemory(eds);
606 eds.dwCookie = (DWORD)&wpc;
607 // the cast below is needed for broken (very) old mingw32 headers
608 eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn;
609
610 // we're going to receive 2 EN_CHANGE notifications if we got any selection
611 // (same problem as in DoWriteText())
612 if ( selectionOnly && HasSelection() )
613 {
614 // so suppress one of them
615 m_suppressNextUpdate = TRUE;
616 }
617
618 ::SendMessage(GetHwnd(), EM_STREAMIN,
619 SF_TEXT |
620 SF_UNICODE |
621 (selectionOnly ? SFF_SELECTION : 0),
622 (LPARAM)&eds);
623
624 if ( eds.dwError )
625 {
626 wxLogLastError(_T("EM_STREAMIN"));
627 }
628
629 #if !wxUSE_WCHAR_T
630 free(wchBuf);
631 #endif // !wxUSE_WCHAR_T
632
633 return TRUE;
634 }
635
636 #endif // wxUSE_RICHEDIT
637
638 void wxTextCtrl::WriteText(const wxString& value)
639 {
640 DoWriteText(value);
641 }
642
643 void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly)
644 {
645 wxString valueDos;
646 if ( m_windowStyle & wxTE_MULTILINE )
647 valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
648 else
649 valueDos = value;
650
651 #if wxUSE_RICHEDIT
652 // there are several complications with the rich edit controls here
653 bool done = FALSE;
654 if ( IsRich() )
655 {
656 // first, ensure that the new text will be in the default style
657 if ( !m_defaultStyle.IsDefault() )
658 {
659 long start, end;
660 GetSelection(&start, &end);
661 SetStyle(start, end, m_defaultStyle);
662 }
663
664 #if wxUSE_UNICODE_MSLU
665 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
666 // but EM_STREAMIN works
667 if ( wxUsingUnicowsDll() && GetRichVersion() > 1 )
668 {
669 done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly);
670 }
671 #endif // wxUSE_UNICODE_MSLU
672
673 #if !wxUSE_UNICODE
674 // next check if the text we're inserting must be shown in a non
675 // default charset -- this only works for RichEdit > 1.0
676 if ( GetRichVersion() > 1 )
677 {
678 wxFont font = m_defaultStyle.GetFont();
679 if ( !font.Ok() )
680 font = GetFont();
681
682 if ( font.Ok() )
683 {
684 wxFontEncoding encoding = font.GetEncoding();
685 if ( encoding != wxFONTENCODING_SYSTEM )
686 {
687 done = StreamIn(valueDos, encoding, selectionOnly);
688 }
689 }
690 }
691 #endif // !wxUSE_UNICODE
692 }
693
694 if ( !done )
695 #endif // wxUSE_RICHEDIT
696 {
697 // in some cases we get 2 EN_CHANGE notifications after the SendMessage
698 // call below which is confusing for the client code and so should be
699 // avoided
700 //
701 // these cases are: (a) plain EDIT controls if EM_REPLACESEL is used
702 // and there is a non empty selection currently and (b) rich text
703 // controls in any case
704 if (
705 #if wxUSE_RICHEDIT
706 IsRich() ||
707 #endif // wxUSE_RICHEDIT
708 (selectionOnly && HasSelection()) )
709 {
710 m_suppressNextUpdate = TRUE;
711 }
712
713 ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT,
714 0, (LPARAM)valueDos.c_str());
715
716 // OTOH, non rich text controls don't generate any events at all when
717 // we use WM_SETTEXT -- have to emulate them here
718 if ( !selectionOnly
719 #if wxUSE_RICHEDIT
720 && !IsRich()
721 #endif // wxUSE_RICHEDIT
722 )
723 {
724 // Windows already sends an update event for single-line
725 // controls.
726 if ( m_windowStyle & wxTE_MULTILINE )
727 SendUpdateEvent();
728 }
729 }
730
731 AdjustSpaceLimit();
732 }
733
734 void wxTextCtrl::AppendText(const wxString& text)
735 {
736 SetInsertionPointEnd();
737
738 WriteText(text);
739
740 #if wxUSE_RICHEDIT
741 if ( IsMultiLine() && GetRichVersion() > 1 )
742 {
743 // setting the caret to the end and showing it simply doesn't work for
744 // RichEdit 2.0 -- force it to still do what we want
745 ::SendMessage(GetHwnd(), EM_LINESCROLL, 0, GetNumberOfLines());
746 }
747 #endif // wxUSE_RICHEDIT
748 }
749
750 void wxTextCtrl::Clear()
751 {
752 ::SetWindowText(GetHwnd(), wxEmptyString);
753
754 #if wxUSE_RICHEDIT
755 if ( !IsRich() )
756 #endif // wxUSE_RICHEDIT
757 {
758 // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves
759 // but the normal ones don't -- make Clear() behaviour consistent by
760 // always sending this event
761
762 // Windows already sends an update event for single-line
763 // controls.
764 if ( m_windowStyle & wxTE_MULTILINE )
765 SendUpdateEvent();
766 }
767 }
768
769 #ifdef __WIN32__
770
771 bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event)
772 {
773 SetFocus();
774
775 size_t lenOld = GetValue().length();
776
777 wxUint32 code = event.GetRawKeyCode();
778 ::keybd_event(code, 0, 0 /* key press */, 0);
779 ::keybd_event(code, 0, KEYEVENTF_KEYUP, 0);
780
781 // assume that any alphanumeric key changes the total number of characters
782 // in the control - this should work in 99% of cases
783 return GetValue().length() != lenOld;
784 }
785
786 #endif // __WIN32__
787
788 // ----------------------------------------------------------------------------
789 // Clipboard operations
790 // ----------------------------------------------------------------------------
791
792 void wxTextCtrl::Copy()
793 {
794 if (CanCopy())
795 {
796 ::SendMessage(GetHwnd(), WM_COPY, 0, 0L);
797 }
798 }
799
800 void wxTextCtrl::Cut()
801 {
802 if (CanCut())
803 {
804 ::SendMessage(GetHwnd(), WM_CUT, 0, 0L);
805 }
806 }
807
808 void wxTextCtrl::Paste()
809 {
810 if (CanPaste())
811 {
812 ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
813 }
814 }
815
816 bool wxTextCtrl::HasSelection() const
817 {
818 long from, to;
819 GetSelection(&from, &to);
820 return from != to;
821 }
822
823 bool wxTextCtrl::CanCopy() const
824 {
825 // Can copy if there's a selection
826 return HasSelection();
827 }
828
829 bool wxTextCtrl::CanCut() const
830 {
831 return CanCopy() && IsEditable();
832 }
833
834 bool wxTextCtrl::CanPaste() const
835 {
836 if ( !IsEditable() )
837 return FALSE;
838
839 #if wxUSE_RICHEDIT
840 if ( IsRich() )
841 {
842 UINT cf = 0; // 0 == any format
843
844 return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0;
845 }
846 #endif // wxUSE_RICHEDIT
847
848 // Standard edit control: check for straight text on clipboard
849 if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
850 return FALSE;
851
852 bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0;
853 ::CloseClipboard();
854
855 return isTextAvailable;
856 }
857
858 // ----------------------------------------------------------------------------
859 // Accessors
860 // ----------------------------------------------------------------------------
861
862 void wxTextCtrl::SetEditable(bool editable)
863 {
864 HWND hWnd = GetHwnd();
865 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
866 }
867
868 void wxTextCtrl::SetInsertionPoint(long pos)
869 {
870 DoSetSelection(pos, pos);
871 }
872
873 void wxTextCtrl::SetInsertionPointEnd()
874 {
875 // we must not do anything if the caret is already there because calling
876 // SetInsertionPoint() thaws the controls if Freeze() had been called even
877 // if it doesn't actually move the caret anywhere and so the simple fact of
878 // doing it results in horrible flicker when appending big amounts of text
879 // to the control in a few chunks (see DoAddText() test in the text sample)
880 if ( GetInsertionPoint() == GetLastPosition() )
881 return;
882
883 long pos;
884
885 #if wxUSE_RICHEDIT
886 if ( m_verRichEdit == 1 )
887 {
888 // we don't have to waste time calling GetLastPosition() in this case
889 pos = -1;
890 }
891 else // !RichEdit 1.0
892 #endif // wxUSE_RICHEDIT
893 {
894 pos = GetLastPosition();
895 }
896
897 SetInsertionPoint(pos);
898 }
899
900 long wxTextCtrl::GetInsertionPoint() const
901 {
902 #if wxUSE_RICHEDIT
903 if ( IsRich() )
904 {
905 CHARRANGE range;
906 range.cpMin = 0;
907 range.cpMax = 0;
908 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
909 return range.cpMin;
910 }
911 #endif // wxUSE_RICHEDIT
912
913 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
914 return Pos & 0xFFFF;
915 }
916
917 long wxTextCtrl::GetLastPosition() const
918 {
919 int numLines = GetNumberOfLines();
920 long posStartLastLine = XYToPosition(0, numLines - 1);
921
922 long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine);
923
924 return posStartLastLine + lenLastLine;
925 }
926
927 // If the return values from and to are the same, there is no
928 // selection.
929 void wxTextCtrl::GetSelection(long* from, long* to) const
930 {
931 #if wxUSE_RICHEDIT
932 if ( IsRich() )
933 {
934 CHARRANGE charRange;
935 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange);
936
937 *from = charRange.cpMin;
938 *to = charRange.cpMax;
939 }
940 else
941 #endif // !wxUSE_RICHEDIT
942 {
943 DWORD dwStart, dwEnd;
944 ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
945
946 *from = dwStart;
947 *to = dwEnd;
948 }
949 }
950
951 bool wxTextCtrl::IsEditable() const
952 {
953 // strangely enough, we may be called before the control is created: our
954 // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls
955 // us
956 if ( !m_hWnd )
957 return TRUE;
958
959 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
960
961 return (style & ES_READONLY) == 0;
962 }
963
964 // ----------------------------------------------------------------------------
965 // selection
966 // ----------------------------------------------------------------------------
967
968 void wxTextCtrl::SetSelection(long from, long to)
969 {
970 // if from and to are both -1, it means (in wxWindows) that all text should
971 // be selected - translate into Windows convention
972 if ( (from == -1) && (to == -1) )
973 {
974 from = 0;
975 to = -1;
976 }
977
978 DoSetSelection(from, to);
979 }
980
981 void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret)
982 {
983 HWND hWnd = GetHwnd();
984
985 #ifdef __WIN32__
986 #if wxUSE_RICHEDIT
987 if ( IsRich() )
988 {
989 CHARRANGE range;
990 range.cpMin = from;
991 range.cpMax = to;
992 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
993 }
994 else
995 #endif // wxUSE_RICHEDIT
996 {
997 SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to);
998 }
999
1000 if ( scrollCaret )
1001 {
1002 #if wxUSE_RICHEDIT
1003 // richedit 3.0 (i.e. the version living in riched20.dll distributed
1004 // with Windows 2000 and beyond) doesn't honour EM_SCROLLCARET when
1005 // emulating richedit 2.0 unless the control has focus or ECO_NOHIDESEL
1006 // option is set (but it does work ok in richedit 1.0 mode...)
1007 //
1008 // so to make it work we either need to give focus to it here which
1009 // will probably create many problems (dummy focus events; window
1010 // containing the text control being brought to foreground
1011 // unexpectedly; ...) or to temporarily set ECO_NOHIDESEL which may
1012 // create other problems too -- and in fact it does because if we turn
1013 // on/off this style while appending the text to the control, the
1014 // vertical scrollbar never appears in it even if we append tons of
1015 // text and to work around this the only solution I found was to use
1016 // ES_DISABLENOSCROLL
1017 //
1018 // this is very ugly but I don't see any other way to make this work
1019 if ( GetRichVersion() > 1 )
1020 {
1021 if ( !HasFlag(wxTE_NOHIDESEL) )
1022 {
1023 ::SendMessage(GetHwnd(), EM_SETOPTIONS,
1024 ECOOP_OR, ECO_NOHIDESEL);
1025 }
1026 //else: everything is already ok
1027 }
1028 #endif // wxUSE_RICHEDIT
1029
1030 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
1031
1032 #if wxUSE_RICHEDIT
1033 // restore ECO_NOHIDESEL if we changed it
1034 if ( GetRichVersion() > 1 && !HasFlag(wxTE_NOHIDESEL) )
1035 {
1036 ::SendMessage(GetHwnd(), EM_SETOPTIONS,
1037 ECOOP_AND, ~ECO_NOHIDESEL);
1038 }
1039 #endif // wxUSE_RICHEDIT
1040 }
1041 #else // Win16
1042 // WPARAM is 0: selection is scrolled into view
1043 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(from, to));
1044 #endif // Win32/16
1045 }
1046
1047 // ----------------------------------------------------------------------------
1048 // Editing
1049 // ----------------------------------------------------------------------------
1050
1051 void wxTextCtrl::Replace(long from, long to, const wxString& value)
1052 {
1053 // Set selection and remove it
1054 DoSetSelection(from, to, FALSE /* don't scroll caret into view */);
1055
1056 SendMessage(GetHwnd(), EM_REPLACESEL,
1057 #ifdef __WIN32__
1058 TRUE,
1059 #else
1060 FALSE,
1061 #endif
1062 (LPARAM)value.c_str());
1063 }
1064
1065 void wxTextCtrl::Remove(long from, long to)
1066 {
1067 Replace(from, to, wxEmptyString);
1068 }
1069
1070 bool wxTextCtrl::LoadFile(const wxString& file)
1071 {
1072 if ( wxTextCtrlBase::LoadFile(file) )
1073 {
1074 // update the size limit if needed
1075 AdjustSpaceLimit();
1076
1077 return TRUE;
1078 }
1079
1080 return FALSE;
1081 }
1082
1083 bool wxTextCtrl::IsModified() const
1084 {
1085 return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0;
1086 }
1087
1088 // Makes 'unmodified'
1089 void wxTextCtrl::DiscardEdits()
1090 {
1091 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
1092 }
1093
1094 int wxTextCtrl::GetNumberOfLines() const
1095 {
1096 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
1097 }
1098
1099 long wxTextCtrl::XYToPosition(long x, long y) const
1100 {
1101 // This gets the char index for the _beginning_ of this line
1102 long charIndex = SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
1103
1104 return charIndex + x;
1105 }
1106
1107 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
1108 {
1109 HWND hWnd = GetHwnd();
1110
1111 // This gets the line number containing the character
1112 long lineNo;
1113 #if wxUSE_RICHEDIT
1114 if ( IsRich() )
1115 {
1116 lineNo = SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
1117 }
1118 else
1119 #endif // wxUSE_RICHEDIT
1120 {
1121 lineNo = SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
1122 }
1123
1124 if ( lineNo == -1 )
1125 {
1126 // no such line
1127 return FALSE;
1128 }
1129
1130 // This gets the char index for the _beginning_ of this line
1131 long charIndex = SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
1132 if ( charIndex == -1 )
1133 {
1134 return FALSE;
1135 }
1136
1137 // The X position must therefore be the different between pos and charIndex
1138 if ( x )
1139 *x = pos - charIndex;
1140 if ( y )
1141 *y = lineNo;
1142
1143 return TRUE;
1144 }
1145
1146 void wxTextCtrl::ShowPosition(long pos)
1147 {
1148 HWND hWnd = GetHwnd();
1149
1150 // To scroll to a position, we pass the number of lines and characters
1151 // to scroll *by*. This means that we need to:
1152 // (1) Find the line position of the current line.
1153 // (2) Find the line position of pos.
1154 // (3) Scroll by (pos - current).
1155 // For now, ignore the horizontal scrolling.
1156
1157 // Is this where scrolling is relative to - the line containing the caret?
1158 // Or is the first visible line??? Try first visible line.
1159 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
1160
1161 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
1162
1163 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
1164
1165 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
1166
1167 if (linesToScroll != 0)
1168 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
1169 }
1170
1171 long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const
1172 {
1173 return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0);
1174 }
1175
1176 int wxTextCtrl::GetLineLength(long lineNo) const
1177 {
1178 long pos = XYToPosition(0, lineNo);
1179
1180 return GetLengthOfLineContainingPos(pos);
1181 }
1182
1183 wxString wxTextCtrl::GetLineText(long lineNo) const
1184 {
1185 size_t len = (size_t)GetLineLength(lineNo) + 1;
1186
1187 // there must be at least enough place for the length WORD in the
1188 // buffer
1189 len += sizeof(WORD);
1190
1191 wxString str;
1192 wxChar *buf = str.GetWriteBuf(len);
1193
1194 *(WORD *)buf = (WORD)len;
1195 len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
1196 buf[len] = 0;
1197
1198 str.UngetWriteBuf(len);
1199
1200 return str;
1201 }
1202
1203 void wxTextCtrl::SetMaxLength(unsigned long len)
1204 {
1205 ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0);
1206 }
1207
1208 // ----------------------------------------------------------------------------
1209 // Undo/redo
1210 // ----------------------------------------------------------------------------
1211
1212 void wxTextCtrl::Undo()
1213 {
1214 if (CanUndo())
1215 {
1216 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
1217 }
1218 }
1219
1220 void wxTextCtrl::Redo()
1221 {
1222 if (CanRedo())
1223 {
1224 // Same as Undo, since Undo undoes the undo, i.e. a redo.
1225 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
1226 }
1227 }
1228
1229 bool wxTextCtrl::CanUndo() const
1230 {
1231 return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
1232 }
1233
1234 bool wxTextCtrl::CanRedo() const
1235 {
1236 return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
1237 }
1238
1239 // ----------------------------------------------------------------------------
1240 // implemenation details
1241 // ----------------------------------------------------------------------------
1242
1243 void wxTextCtrl::Command(wxCommandEvent & event)
1244 {
1245 SetValue(event.GetString());
1246 ProcessCommand (event);
1247 }
1248
1249 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
1250 {
1251 // By default, load the first file into the text window.
1252 if (event.GetNumberOfFiles() > 0)
1253 {
1254 LoadFile(event.GetFiles()[0]);
1255 }
1256 }
1257
1258 // ----------------------------------------------------------------------------
1259 // kbd input processing
1260 // ----------------------------------------------------------------------------
1261
1262 bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* pMsg)
1263 {
1264 MSG *msg = (MSG *)pMsg;
1265
1266 // check for our special keys here: if we don't do it and the parent frame
1267 // uses them as accelerators, they wouldn't work at all, so we disable
1268 // usual preprocessing for them
1269 if ( msg->message == WM_KEYDOWN )
1270 {
1271 WORD vkey = (WORD) msg->wParam;
1272 if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN )
1273 {
1274 if ( vkey == VK_BACK )
1275 return FALSE;
1276 }
1277 else // no Alt
1278 {
1279 // we want to process some Ctrl-foo and Shift-bar but no key
1280 // combinations without either Ctrl or Shift nor with both of them
1281 // pressed
1282 const int ctrl = wxIsCtrlDown(),
1283 shift = wxIsShiftDown();
1284 switch ( ctrl + shift )
1285 {
1286 default:
1287 wxFAIL_MSG( _T("how many modifiers have we got?") );
1288 // fall through
1289
1290 case 0:
1291 case 2:
1292 break;
1293
1294 case 1:
1295 // either Ctrl or Shift pressed
1296 if ( ctrl )
1297 {
1298 switch ( vkey )
1299 {
1300 case 'C':
1301 case 'V':
1302 case 'X':
1303 case VK_INSERT:
1304 case VK_DELETE:
1305 case VK_HOME:
1306 case VK_END:
1307 return FALSE;
1308 }
1309 }
1310 else // Shift is pressed
1311 {
1312 if ( vkey == VK_INSERT || vkey == VK_DELETE )
1313 return FALSE;
1314 }
1315 }
1316 }
1317 }
1318
1319 return wxControl::MSWShouldPreProcessMessage(pMsg);
1320 }
1321
1322 void wxTextCtrl::OnChar(wxKeyEvent& event)
1323 {
1324 switch ( event.GetKeyCode() )
1325 {
1326 case WXK_RETURN:
1327 if ( !(m_windowStyle & wxTE_MULTILINE) )
1328 {
1329 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1330 InitCommandEvent(event);
1331 event.SetString(GetValue());
1332 if ( GetEventHandler()->ProcessEvent(event) )
1333 return;
1334 }
1335 //else: multiline controls need Enter for themselves
1336
1337 break;
1338
1339 case WXK_TAB:
1340 // always produce navigation event - even if we process TAB
1341 // ourselves the fact that we got here means that the user code
1342 // decided to skip processing of this TAB - probably to let it
1343 // do its default job.
1344 {
1345 wxNavigationKeyEvent eventNav;
1346 eventNav.SetDirection(!event.ShiftDown());
1347 eventNav.SetWindowChange(event.ControlDown());
1348 eventNav.SetEventObject(this);
1349
1350 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
1351 return;
1352 }
1353 break;
1354 }
1355
1356 // no, we didn't process it
1357 event.Skip();
1358 }
1359
1360 long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1361 {
1362 long lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
1363
1364 if ( nMsg == WM_GETDLGCODE )
1365 {
1366 // we always want the chars and the arrows: the arrows for navigation
1367 // and the chars because we want Ctrl-C to work even in a read only
1368 // control
1369 long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
1370
1371 if ( IsEditable() )
1372 {
1373 // we may have several different cases:
1374 // 1. normal case: both TAB and ENTER are used for dlg navigation
1375 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the
1376 // next control in the dialog
1377 // 3. ctrl which wants ENTER for itself: TAB is used for dialog
1378 // navigation
1379 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to go
1380 // to the next control
1381
1382 // the multiline edit control should always get <Return> for itself
1383 if ( HasFlag(wxTE_PROCESS_ENTER) || HasFlag(wxTE_MULTILINE) )
1384 lDlgCode |= DLGC_WANTMESSAGE;
1385
1386 if ( HasFlag(wxTE_PROCESS_TAB) )
1387 lDlgCode |= DLGC_WANTTAB;
1388
1389 lRc |= lDlgCode;
1390 }
1391 else // !editable
1392 {
1393 // NB: use "=", not "|=" as the base class version returns the
1394 // same flags is this state as usual (i.e. including
1395 // DLGC_WANTMESSAGE). This is strange (how does it work in the
1396 // native Win32 apps?) but for now live with it.
1397 lRc = lDlgCode;
1398 }
1399 }
1400
1401 return lRc;
1402 }
1403
1404 // ----------------------------------------------------------------------------
1405 // text control event processing
1406 // ----------------------------------------------------------------------------
1407
1408 bool wxTextCtrl::SendUpdateEvent()
1409 {
1410 // is event reporting suspended?
1411 if ( m_suppressNextUpdate )
1412 {
1413 // do process the next one
1414 m_suppressNextUpdate = FALSE;
1415
1416 return FALSE;
1417 }
1418
1419 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
1420 InitCommandEvent(event);
1421 event.SetString(GetValue());
1422
1423 return ProcessCommand(event);
1424 }
1425
1426 bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
1427 {
1428 switch ( param )
1429 {
1430 case EN_SETFOCUS:
1431 case EN_KILLFOCUS:
1432 {
1433 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
1434 : wxEVT_SET_FOCUS,
1435 m_windowId);
1436 event.SetEventObject(this);
1437 GetEventHandler()->ProcessEvent(event);
1438 }
1439 break;
1440
1441 case EN_CHANGE:
1442 SendUpdateEvent();
1443 break;
1444
1445 case EN_MAXTEXT:
1446 // the text size limit has been hit -- try to increase it
1447 if ( !AdjustSpaceLimit() )
1448 {
1449 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId);
1450 InitCommandEvent(event);
1451 event.SetString(GetValue());
1452 ProcessCommand(event);
1453 }
1454 break;
1455
1456 // the other edit notification messages are not processed
1457 default:
1458 return FALSE;
1459 }
1460
1461 // processed
1462 return TRUE;
1463 }
1464
1465 WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
1466 #if wxUSE_CTL3D
1467 WXUINT message,
1468 WXWPARAM wParam,
1469 WXLPARAM lParam
1470 #else
1471 WXUINT WXUNUSED(message),
1472 WXWPARAM WXUNUSED(wParam),
1473 WXLPARAM WXUNUSED(lParam)
1474 #endif
1475 )
1476 {
1477 #if wxUSE_CTL3D
1478 if ( m_useCtl3D )
1479 {
1480 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
1481 return (WXHBRUSH) hbrush;
1482 }
1483 #endif // wxUSE_CTL3D
1484
1485 HDC hdc = (HDC)pDC;
1486 wxColour colBack = GetBackgroundColour();
1487
1488 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
1489 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1490
1491 ::SetBkColor(hdc, wxColourToRGB(colBack));
1492 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
1493
1494 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
1495
1496 return (WXHBRUSH)brush->GetResourceHandle();
1497 }
1498
1499 // In WIN16, need to override normal erasing because
1500 // Ctl3D doesn't use the wxWindows background colour.
1501 #ifdef __WIN16__
1502 void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
1503 {
1504 wxColour col(m_backgroundColour);
1505
1506 #if wxUSE_CTL3D
1507 if (m_useCtl3D)
1508 col = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1509 #endif
1510
1511 RECT rect;
1512 ::GetClientRect(GetHwnd(), &rect);
1513
1514 COLORREF ref = wxColourToRGB(col);
1515 HBRUSH hBrush = ::CreateSolidBrush(ref);
1516 if ( !hBrush )
1517 wxLogLastError(wxT("CreateSolidBrush"));
1518
1519 HDC hdc = (HDC)event.GetDC()->GetHDC();
1520
1521 int mode = ::SetMapMode(hdc, MM_TEXT);
1522
1523 ::FillRect(hdc, &rect, hBrush);
1524 ::DeleteObject(hBrush);
1525 ::SetMapMode(hdc, mode);
1526
1527 }
1528 #endif // Win16
1529
1530 bool wxTextCtrl::AdjustSpaceLimit()
1531 {
1532 #ifndef __WIN16__
1533 unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
1534
1535 // HACK: we try to automatically extend the limit for the amount of text
1536 // to allow (interactively) entering more than 64Kb of text under
1537 // Win9x but we shouldn't reset the text limit which was previously
1538 // set explicitly with SetMaxLength()
1539 //
1540 // we could solve this by storing the limit we set in wxTextCtrl but
1541 // to save space we prefer to simply test here the actual limit
1542 // value: we consider that SetMaxLength() can only be called for
1543 // values < 32Kb
1544 if ( limit < 0x8000 )
1545 {
1546 // we've got more text than limit set by SetMaxLength()
1547 return FALSE;
1548 }
1549
1550 unsigned int len = ::GetWindowTextLength(GetHwnd());
1551 if ( len >= limit )
1552 {
1553 limit = len + 0x8000; // 32Kb
1554
1555 #if wxUSE_RICHEDIT
1556 if ( IsRich() )
1557 {
1558 // as a nice side effect, this also allows passing limit > 64Kb
1559 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
1560 }
1561 else
1562 #endif // wxUSE_RICHEDIT
1563 {
1564 if ( limit > 0xffff )
1565 {
1566 // this will set it to a platform-dependent maximum (much more
1567 // than 64Kb under NT)
1568 limit = 0;
1569 }
1570
1571 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
1572 }
1573 }
1574 #endif // !Win16
1575
1576 // we changed the limit
1577 return TRUE;
1578 }
1579
1580 bool wxTextCtrl::AcceptsFocus() const
1581 {
1582 // we don't want focus if we can't be edited unless we're a multiline
1583 // control because then it might be still nice to get focus from keyboard
1584 // to be able to scroll it without mouse
1585 return (IsEditable() || IsMultiLine()) && wxControl::AcceptsFocus();
1586 }
1587
1588 wxSize wxTextCtrl::DoGetBestSize() const
1589 {
1590 int cx, cy;
1591 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1592
1593 int wText = DEFAULT_ITEM_WIDTH;
1594
1595 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1596 if ( m_windowStyle & wxTE_MULTILINE )
1597 {
1598 hText *= wxMax(GetNumberOfLines(), 5);
1599 }
1600 //else: for single line control everything is ok
1601
1602 return wxSize(wText, hText);
1603 }
1604
1605 // ----------------------------------------------------------------------------
1606 // standard handlers for standard edit menu events
1607 // ----------------------------------------------------------------------------
1608
1609 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1610 {
1611 Cut();
1612 }
1613
1614 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1615 {
1616 Copy();
1617 }
1618
1619 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1620 {
1621 Paste();
1622 }
1623
1624 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1625 {
1626 Undo();
1627 }
1628
1629 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1630 {
1631 Redo();
1632 }
1633
1634 void wxTextCtrl::OnDelete(wxCommandEvent& event)
1635 {
1636 long from, to;
1637 GetSelection(& from, & to);
1638 if (from != -1 && to != -1)
1639 Remove(from, to);
1640 }
1641
1642 void wxTextCtrl::OnSelectAll(wxCommandEvent& event)
1643 {
1644 SetSelection(-1, -1);
1645 }
1646
1647 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1648 {
1649 event.Enable( CanCut() );
1650 }
1651
1652 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1653 {
1654 event.Enable( CanCopy() );
1655 }
1656
1657 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1658 {
1659 event.Enable( CanPaste() );
1660 }
1661
1662 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1663 {
1664 event.Enable( CanUndo() );
1665 }
1666
1667 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1668 {
1669 event.Enable( CanRedo() );
1670 }
1671
1672 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
1673 {
1674 long from, to;
1675 GetSelection(& from, & to);
1676 event.Enable(from != -1 && to != -1 && from != to && IsEditable()) ;
1677 }
1678
1679 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
1680 {
1681 event.Enable(GetLastPosition() > 0);
1682 }
1683
1684 void wxTextCtrl::OnRightClick(wxMouseEvent& event)
1685 {
1686 #if wxUSE_RICHEDIT
1687 if (IsRich())
1688 {
1689 if (!m_privateContextMenu)
1690 {
1691 m_privateContextMenu = new wxMenu;
1692 m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
1693 m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
1694 m_privateContextMenu->AppendSeparator();
1695 m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
1696 m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
1697 m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
1698 m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
1699 m_privateContextMenu->AppendSeparator();
1700 m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
1701 }
1702 PopupMenu(m_privateContextMenu, event.GetPosition());
1703 return;
1704 }
1705 else
1706 #endif
1707 event.Skip();
1708 }
1709
1710 // the rest of the file only deals with the rich edit controls
1711 #if wxUSE_RICHEDIT
1712
1713 // ----------------------------------------------------------------------------
1714 // EN_LINK processing
1715 // ----------------------------------------------------------------------------
1716
1717 bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1718 {
1719 NMHDR *hdr = (NMHDR* )lParam;
1720 switch ( hdr->code )
1721 {
1722 case EN_MSGFILTER:
1723 {
1724 const MSGFILTER *msgf = (MSGFILTER *)lParam;
1725 UINT msg = msgf->msg;
1726
1727 // this is a bit crazy but richedit 1.0 sends us all mouse
1728 // events _except_ WM_LBUTTONUP (don't ask me why) so we have
1729 // generate the wxWin events for this message manually
1730 //
1731 // NB: in fact, this is still not totally correct as it does
1732 // send us WM_LBUTTONUP if the selection was cleared by the
1733 // last click -- so currently we get 2 events in this case,
1734 // but as I don't see any obvious way to check for this I
1735 // leave this code in place because it's still better than
1736 // not getting left up events at all
1737 if ( msg == WM_LBUTTONUP )
1738 {
1739 WXUINT flags = msgf->wParam;
1740 int x = GET_X_LPARAM(msgf->lParam),
1741 y = GET_Y_LPARAM(msgf->lParam);
1742
1743 HandleMouseEvent(msg, x, y, flags);
1744 }
1745 }
1746
1747 // return TRUE to process the event (and FALSE to ignore it)
1748 return TRUE;
1749
1750 case EN_LINK:
1751 {
1752 const ENLINK *enlink = (ENLINK *)hdr;
1753
1754 switch ( enlink->msg )
1755 {
1756 case WM_SETCURSOR:
1757 // ok, so it is hardcoded - do we really nee to
1758 // customize it?
1759 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND)));
1760 *result = TRUE;
1761 break;
1762
1763 case WM_MOUSEMOVE:
1764 case WM_LBUTTONDOWN:
1765 case WM_LBUTTONUP:
1766 case WM_LBUTTONDBLCLK:
1767 case WM_RBUTTONDOWN:
1768 case WM_RBUTTONUP:
1769 case WM_RBUTTONDBLCLK:
1770 // send a mouse event
1771 {
1772 static const wxEventType eventsMouse[] =
1773 {
1774 wxEVT_MOTION,
1775 wxEVT_LEFT_DOWN,
1776 wxEVT_LEFT_UP,
1777 wxEVT_LEFT_DCLICK,
1778 wxEVT_RIGHT_DOWN,
1779 wxEVT_RIGHT_UP,
1780 wxEVT_RIGHT_DCLICK,
1781 };
1782
1783 // the event ids are consecutive
1784 wxMouseEvent
1785 evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]);
1786
1787 InitMouseEvent(evtMouse,
1788 GET_X_LPARAM(enlink->lParam),
1789 GET_Y_LPARAM(enlink->lParam),
1790 enlink->wParam);
1791
1792 wxTextUrlEvent event(m_windowId, evtMouse,
1793 enlink->chrg.cpMin,
1794 enlink->chrg.cpMax);
1795
1796 InitCommandEvent(event);
1797
1798 *result = ProcessCommand(event);
1799 }
1800 break;
1801 }
1802 }
1803 return TRUE;
1804 }
1805
1806 // not processed, leave it to the base class
1807 return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result);
1808 }
1809
1810 // ----------------------------------------------------------------------------
1811 // colour setting for the rich edit controls
1812 // ----------------------------------------------------------------------------
1813
1814 bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
1815 {
1816 if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
1817 {
1818 // colour didn't really change
1819 return FALSE;
1820 }
1821
1822 if ( IsRich() )
1823 {
1824 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1825 // EM_SETBKGNDCOLOR additionally
1826 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
1827 }
1828
1829 return TRUE;
1830 }
1831
1832 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1833 {
1834 if ( !wxTextCtrlBase::SetForegroundColour(colour) )
1835 {
1836 // colour didn't really change
1837 return FALSE;
1838 }
1839
1840 if ( IsRich() )
1841 {
1842 // change the colour of everything
1843 CHARFORMAT cf;
1844 wxZeroMemory(cf);
1845 cf.cbSize = sizeof(cf);
1846 cf.dwMask = CFM_COLOR;
1847 cf.crTextColor = wxColourToRGB(colour);
1848 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
1849 }
1850
1851 return TRUE;
1852 }
1853
1854 // ----------------------------------------------------------------------------
1855 // styling support for rich edit controls
1856 // ----------------------------------------------------------------------------
1857
1858 #if wxUSE_RICHEDIT
1859
1860 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
1861 {
1862 if ( !IsRich() )
1863 {
1864 // can't do it with normal text control
1865 return FALSE;
1866 }
1867
1868 // the richedit 1.0 doesn't handle setting background colour, so don't
1869 // even try to do anything if it's the only thing we want to change
1870 if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() &&
1871 !style.HasLeftIndent() && !style.HasRightIndent() && !style.HasAlignment() &&
1872 !style.HasTabs() )
1873 {
1874 // nothing to do: return TRUE if there was really nothing to do and
1875 // FALSE if we failed to set bg colour
1876 return !style.HasBackgroundColour();
1877 }
1878
1879 // order the range if needed
1880 if ( start > end )
1881 {
1882 long tmp = start;
1883 start = end;
1884 end = tmp;
1885 }
1886
1887 // we can only change the format of the selection, so select the range we
1888 // want and restore the old selection later
1889 long startOld, endOld;
1890 GetSelection(&startOld, &endOld);
1891
1892 // but do we really have to change the selection?
1893 bool changeSel = start != startOld || end != endOld;
1894
1895 if ( changeSel )
1896 {
1897 DoSetSelection(start, end, FALSE /* don't scroll caret into view */);
1898 }
1899
1900 // initialize CHARFORMAT struct
1901 #if wxUSE_RICHEDIT2
1902 CHARFORMAT2 cf;
1903 #else
1904 CHARFORMAT cf;
1905 #endif
1906
1907 wxZeroMemory(cf);
1908
1909 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1910 // CHARFORMAT in that case
1911 #if wxUSE_RICHEDIT2
1912 if ( m_verRichEdit == 1 )
1913 {
1914 // this is the only thing the control is going to grok
1915 cf.cbSize = sizeof(CHARFORMAT);
1916 }
1917 else
1918 #endif
1919 {
1920 // CHARFORMAT or CHARFORMAT2
1921 cf.cbSize = sizeof(cf);
1922 }
1923
1924 if ( style.HasFont() )
1925 {
1926 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1927 // but using it doesn't seem to hurt neither so leaving it for now
1928
1929 cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET |
1930 CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE;
1931
1932 // fill in data from LOGFONT but recalculate lfHeight because we need
1933 // the real height in twips and not the negative number which
1934 // wxFillLogFont() returns (this is correct in general and works with
1935 // the Windows font mapper, but not here)
1936 LOGFONT lf;
1937 wxFillLogFont(&lf, &style.GetFont());
1938 cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips
1939 cf.bCharSet = lf.lfCharSet;
1940 cf.bPitchAndFamily = lf.lfPitchAndFamily;
1941 wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
1942
1943 // also deal with underline/italic/bold attributes: note that we must
1944 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1945 // style to allow clearing it
1946 if ( lf.lfItalic )
1947 {
1948 cf.dwEffects |= CFE_ITALIC;
1949 }
1950
1951 if ( lf.lfWeight == FW_BOLD )
1952 {
1953 cf.dwEffects |= CFE_BOLD;
1954 }
1955
1956 if ( lf.lfUnderline )
1957 {
1958 cf.dwEffects |= CFE_UNDERLINE;
1959 }
1960
1961 // strikeout fonts are not supported by wxWindows
1962 }
1963
1964 if ( style.HasTextColour() )
1965 {
1966 cf.dwMask |= CFM_COLOR;
1967 cf.crTextColor = wxColourToRGB(style.GetTextColour());
1968 }
1969
1970 #if wxUSE_RICHEDIT2
1971 if ( m_verRichEdit != 1 && style.HasBackgroundColour() )
1972 {
1973 cf.dwMask |= CFM_BACKCOLOR;
1974 cf.crBackColor = wxColourToRGB(style.GetBackgroundColour());
1975 }
1976 #endif // wxUSE_RICHEDIT2
1977
1978 // do format the selection
1979 bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
1980 SCF_SELECTION, (LPARAM)&cf) != 0;
1981 if ( !ok )
1982 {
1983 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1984 }
1985
1986 // now do the paragraph formatting
1987 PARAFORMAT2 pf;
1988 wxZeroMemory(pf);
1989 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
1990 // PARAFORMAT in that case
1991 #if wxUSE_RICHEDIT2
1992 if ( m_verRichEdit == 1 )
1993 {
1994 // this is the only thing the control is going to grok
1995 pf.cbSize = sizeof(PARAFORMAT);
1996 }
1997 else
1998 #endif
1999 {
2000 // PARAFORMAT or PARAFORMAT2
2001 pf.cbSize = sizeof(pf);
2002 }
2003
2004 if (style.HasAlignment())
2005 {
2006 pf.dwMask |= PFM_ALIGNMENT;
2007 if (style.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
2008 pf.wAlignment = PFA_RIGHT;
2009 else if (style.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
2010 pf.wAlignment = PFA_CENTER;
2011 else if (style.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED)
2012 pf.wAlignment = PFA_JUSTIFY;
2013 else
2014 pf.wAlignment = PFA_LEFT;
2015 }
2016
2017 if (style.HasLeftIndent())
2018 {
2019 pf.dwMask |= PFM_STARTINDENT;
2020
2021 // Convert from 1/10 mm to TWIPS
2022 pf.dxStartIndent = (int) (((double) style.GetLeftIndent()) * mm2twips / 10.0) ;
2023
2024 // TODO: do we need to specify dxOffset?
2025 }
2026
2027 if (style.HasRightIndent())
2028 {
2029 pf.dwMask |= PFM_RIGHTINDENT;
2030
2031 // Convert from 1/10 mm to TWIPS
2032 pf.dxRightIndent = (int) (((double) style.GetRightIndent()) * mm2twips / 10.0) ;
2033 }
2034
2035 if (style.HasTabs())
2036 {
2037 pf.dwMask |= PFM_TABSTOPS;
2038
2039 const wxArrayInt& tabs = style.GetTabs();
2040
2041 pf.cTabCount = wxMin(tabs.GetCount(), MAX_TAB_STOPS);
2042 size_t i;
2043 for (i = 0; i < (size_t) pf.cTabCount; i++)
2044 {
2045 // Convert from 1/10 mm to TWIPS
2046 pf.rgxTabs[i] = (int) (((double) tabs[i]) * mm2twips / 10.0) ;
2047 }
2048 }
2049
2050 if (pf.dwMask != 0)
2051 {
2052 // do format the selection
2053 bool ok = ::SendMessage(GetHwnd(), EM_SETPARAFORMAT,
2054 0, (LPARAM) &pf) != 0;
2055 if ( !ok )
2056 {
2057 wxLogDebug(_T("SendMessage(EM_SETPARAFORMAT, 0) failed"));
2058 }
2059 }
2060
2061 if ( changeSel )
2062 {
2063 // restore the original selection
2064 DoSetSelection(startOld, endOld, FALSE);
2065 }
2066
2067 return ok;
2068 }
2069
2070 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2071 {
2072 if ( !wxTextCtrlBase::SetDefaultStyle(style) )
2073 return FALSE;
2074
2075 // we have to do this or the style wouldn't apply for the text typed by the
2076 // user
2077 long posLast = GetLastPosition();
2078 SetStyle(posLast, posLast, m_defaultStyle);
2079
2080 return TRUE;
2081 }
2082
2083 bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
2084 {
2085 if ( !IsRich() )
2086 {
2087 // can't do it with normal text control
2088 return FALSE;
2089 }
2090
2091 // initialize CHARFORMAT struct
2092 #if wxUSE_RICHEDIT2
2093 CHARFORMAT2 cf;
2094 #else
2095 CHARFORMAT cf;
2096 #endif
2097
2098 wxZeroMemory(cf);
2099
2100 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
2101 // CHARFORMAT in that case
2102 #if wxUSE_RICHEDIT2
2103 if ( m_verRichEdit == 1 )
2104 {
2105 // this is the only thing the control is going to grok
2106 cf.cbSize = sizeof(CHARFORMAT);
2107 }
2108 else
2109 #endif
2110 {
2111 // CHARFORMAT or CHARFORMAT2
2112 cf.cbSize = sizeof(cf);
2113 }
2114 // we can only change the format of the selection, so select the range we
2115 // want and restore the old selection later
2116 long startOld, endOld;
2117 GetSelection(&startOld, &endOld);
2118
2119 // but do we really have to change the selection?
2120 bool changeSel = position != startOld || position != endOld;
2121
2122 if ( changeSel )
2123 {
2124 DoSetSelection(position, position, FALSE /* don't scroll caret into view */);
2125 }
2126
2127 // get the selection formatting
2128 (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT,
2129 SCF_SELECTION, (LPARAM)&cf) ;
2130
2131 LOGFONT lf;
2132 lf.lfHeight = cf.yHeight;
2133 lf.lfWidth = 0;
2134 lf.lfCharSet = ANSI_CHARSET; // FIXME: how to get correct charset?
2135 lf.lfClipPrecision = 0;
2136 lf.lfEscapement = 0;
2137 wxStrcpy(lf.lfFaceName, cf.szFaceName);
2138 if (cf.dwEffects & CFE_ITALIC)
2139 lf.lfItalic = TRUE;
2140 lf.lfOrientation = 0;
2141 lf.lfPitchAndFamily = cf.bPitchAndFamily;
2142 lf.lfQuality = 0;
2143 if (cf.dwEffects & CFE_STRIKEOUT)
2144 lf.lfStrikeOut = TRUE;
2145 if (cf.dwEffects & CFE_UNDERLINE)
2146 lf.lfUnderline = TRUE;
2147 if (cf.dwEffects & CFE_BOLD)
2148 lf.lfWeight = FW_BOLD;
2149
2150 wxFont font = wxCreateFontFromLogFont(& lf);
2151 if (font.Ok())
2152 {
2153 style.SetFont(font);
2154 }
2155 style.SetTextColour(wxColour(cf.crTextColor));
2156
2157 #if wxUSE_RICHEDIT2
2158 if ( m_verRichEdit != 1 )
2159 {
2160 // cf.dwMask |= CFM_BACKCOLOR;
2161 style.SetBackgroundColour(wxColour(cf.crBackColor));
2162 }
2163 #endif // wxUSE_RICHEDIT2
2164
2165 // now get the paragraph formatting
2166 PARAFORMAT2 pf;
2167 wxZeroMemory(pf);
2168 // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
2169 // PARAFORMAT in that case
2170 #if wxUSE_RICHEDIT2
2171 if ( m_verRichEdit == 1 )
2172 {
2173 // this is the only thing the control is going to grok
2174 pf.cbSize = sizeof(PARAFORMAT);
2175 }
2176 else
2177 #endif
2178 {
2179 // PARAFORMAT or PARAFORMAT2
2180 pf.cbSize = sizeof(pf);
2181 }
2182
2183 // do format the selection
2184 (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT, 0, (LPARAM) &pf) ;
2185
2186 style.SetLeftIndent( (int) ((double) pf.dxStartIndent * twips2mm * 10.0) );
2187 style.SetRightIndent( (int) ((double) pf.dxRightIndent * twips2mm * 10.0) );
2188
2189 if (pf.wAlignment == PFA_CENTER)
2190 style.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
2191 else if (pf.wAlignment == PFA_RIGHT)
2192 style.SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
2193 else if (pf.wAlignment == PFA_JUSTIFY)
2194 style.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED);
2195 else
2196 style.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
2197
2198 wxArrayInt tabStops;
2199 size_t i;
2200 for (i = 0; i < (size_t) pf.cTabCount; i++)
2201 {
2202 tabStops[i] = (int) ((double) (pf.rgxTabs[i] & 0xFFFF) * twips2mm * 10.0) ;
2203 }
2204
2205 if ( changeSel )
2206 {
2207 // restore the original selection
2208 DoSetSelection(startOld, endOld, FALSE);
2209 }
2210
2211 return TRUE;
2212 }
2213
2214 #endif
2215
2216 // ----------------------------------------------------------------------------
2217 // wxRichEditModule
2218 // ----------------------------------------------------------------------------
2219
2220 bool wxRichEditModule::OnInit()
2221 {
2222 // don't do anything - we will load it when needed
2223 return TRUE;
2224 }
2225
2226 void wxRichEditModule::OnExit()
2227 {
2228 for ( size_t i = 0; i < WXSIZEOF(ms_hRichEdit); i++ )
2229 {
2230 if ( ms_hRichEdit[i] )
2231 {
2232 ::FreeLibrary(ms_hRichEdit[i]);
2233 }
2234 }
2235 }
2236
2237 /* static */
2238 bool wxRichEditModule::Load(int version)
2239 {
2240 // we don't support loading richedit 3.0 as I don't know how to distinguish
2241 // it from 2.0 anyhow
2242 wxCHECK_MSG( version == 1 || version == 2, FALSE,
2243 _T("incorrect richedit control version requested") );
2244
2245 // make it the index in the array
2246 version--;
2247
2248 if ( ms_hRichEdit[version] == (HINSTANCE)-1 )
2249 {
2250 // we had already tried to load it and failed
2251 return FALSE;
2252 }
2253
2254 if ( ms_hRichEdit[version] )
2255 {
2256 // we've already got this one
2257 return TRUE;
2258 }
2259
2260 wxString dllname = version ? _T("riched20") : _T("riched32");
2261 dllname += _T(".dll");
2262
2263 ms_hRichEdit[version] = ::LoadLibrary(dllname);
2264
2265 if ( !ms_hRichEdit[version] )
2266 {
2267 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
2268
2269 ms_hRichEdit[version] = (HINSTANCE)-1;
2270
2271 return FALSE;
2272 }
2273
2274 return TRUE;
2275 }
2276
2277 #endif // wxUSE_RICHEDIT
2278
2279 #endif // wxUSE_TEXTCTRL