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