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