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