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