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