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