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