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