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