]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
bfbd6dc1 | 2 | // Name: msw/textctrl.cpp |
2bda0e17 KB |
3 | // Purpose: wxTextCtrl |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
a1b82138 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
14f355c2 | 16 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
a1b82138 | 17 | #pragma implementation "textctrl.h" |
2bda0e17 KB |
18 | #endif |
19 | ||
a1b82138 VZ |
20 | // ---------------------------------------------------------------------------- |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
2bda0e17 KB |
24 | // For compilers that support precompilation, includes "wx.h". |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
a1b82138 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_TEXTCTRL |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
a1b82138 VZ |
34 | #include "wx/textctrl.h" |
35 | #include "wx/settings.h" | |
36 | #include "wx/brush.h" | |
37 | #include "wx/utils.h" | |
381dd4bf | 38 | #include "wx/intl.h" |
a1b82138 | 39 | #include "wx/log.h" |
381dd4bf | 40 | #include "wx/app.h" |
2b5f62a0 | 41 | #include "wx/menu.h" |
2bda0e17 KB |
42 | #endif |
43 | ||
f6bcfd97 BP |
44 | #include "wx/module.h" |
45 | ||
47d67540 | 46 | #if wxUSE_CLIPBOARD |
a1b82138 | 47 | #include "wx/clipbrd.h" |
2bda0e17 KB |
48 | #endif |
49 | ||
a1b82138 VZ |
50 | #include "wx/textfile.h" |
51 | ||
52 | #include <windowsx.h> | |
53 | ||
2bda0e17 | 54 | #include "wx/msw/private.h" |
2077b148 | 55 | #include "wx/msw/winundef.h" |
2bda0e17 | 56 | |
a1b82138 | 57 | #include <string.h> |
2bda0e17 | 58 | #include <stdlib.h> |
4676948b JS |
59 | |
60 | #ifndef __WXWINCE__ | |
a1b82138 | 61 | #include <sys/types.h> |
4676948b | 62 | #endif |
fbc535ff | 63 | |
a40c9444 VZ |
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__) | |
cd471848 | 69 | #include <richedit.h> |
2bda0e17 KB |
70 | #endif |
71 | ||
3ec4107d | 72 | #include "wx/msw/missing.h" |
a5aa8086 | 73 | |
a40c9444 VZ |
74 | #endif // wxUSE_RICHEDIT |
75 | ||
aac7e7fe VZ |
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 | ||
b12915c1 VZ |
86 | // ---------------------------------------------------------------------------- |
87 | // private classes | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | #if wxUSE_RICHEDIT | |
91 | ||
a5aa8086 | 92 | // this module initializes RichEdit DLL(s) if needed |
b12915c1 VZ |
93 | class wxRichEditModule : public wxModule |
94 | { | |
95 | public: | |
96 | virtual bool OnInit(); | |
97 | virtual void OnExit(); | |
98 | ||
b12915c1 VZ |
99 | // load the richedit DLL of at least of required version |
100 | static bool Load(int version = 1); | |
101 | ||
102 | private: | |
a5aa8086 VZ |
103 | // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs |
104 | static HINSTANCE ms_hRichEdit[2]; | |
b12915c1 VZ |
105 | |
106 | DECLARE_DYNAMIC_CLASS(wxRichEditModule) | |
107 | }; | |
108 | ||
a5aa8086 | 109 | HINSTANCE wxRichEditModule::ms_hRichEdit[2] = { NULL, NULL }; |
b12915c1 VZ |
110 | |
111 | IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule) | |
112 | ||
113 | #endif // wxUSE_RICHEDIT | |
e702ff0f | 114 | |
a1b82138 VZ |
115 | // ---------------------------------------------------------------------------- |
116 | // event tables and other macros | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
51741307 | 119 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
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 | ||
51741307 SC |
169 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl, wxControl,"wx/textctrl.h") |
170 | ||
171 | WX_BEGIN_PROPERTIES_TABLE(wxTextCtrl) | |
067e9be6 SC |
172 | WX_PROPERTY( Font , wxFont , SetFont , GetFont ,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) |
173 | WX_PROPERTY( Value , wxString , SetValue, GetValue, wxEmptyString , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
bc9fb572 | 174 | WX_PROPERTY_FLAGS( WindowStyle , wxTextCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
51741307 SC |
175 | WX_END_PROPERTIES_TABLE() |
176 | ||
177 | WX_BEGIN_HANDLERS_TABLE(wxTextCtrl) | |
178 | WX_END_HANDLERS_TABLE() | |
179 | ||
7f5d8b00 | 180 | WX_CONSTRUCTOR_6( wxTextCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size , long , WindowStyle) |
51741307 | 181 | #else |
2bda0e17 | 182 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) |
51741307 SC |
183 | #endif |
184 | ||
2bda0e17 KB |
185 | |
186 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
a1b82138 VZ |
187 | EVT_CHAR(wxTextCtrl::OnChar) |
188 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
189 | ||
2b5f62a0 VZ |
190 | #if wxUSE_RICHEDIT |
191 | EVT_RIGHT_UP(wxTextCtrl::OnRightClick) | |
192 | #endif | |
193 | ||
a1b82138 VZ |
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) | |
2b5f62a0 VZ |
199 | EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete) |
200 | EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll) | |
a1b82138 VZ |
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) | |
2b5f62a0 VZ |
207 | EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete) |
208 | EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll) | |
f6bcfd97 BP |
209 | #ifdef __WIN16__ |
210 | EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) | |
211 | #endif | |
e3a6a6b2 VZ |
212 | |
213 | EVT_SET_FOCUS(wxTextCtrl::OnSetFocus) | |
2bda0e17 | 214 | END_EVENT_TABLE() |
e702ff0f | 215 | |
a1b82138 VZ |
216 | // ============================================================================ |
217 | // implementation | |
218 | // ============================================================================ | |
219 | ||
220 | // ---------------------------------------------------------------------------- | |
221 | // creation | |
222 | // ---------------------------------------------------------------------------- | |
223 | ||
aac7e7fe | 224 | void wxTextCtrl::Init() |
2bda0e17 | 225 | { |
cd471848 | 226 | #if wxUSE_RICHEDIT |
aac7e7fe | 227 | m_verRichEdit = 0; |
2b5f62a0 | 228 | #endif // wxUSE_RICHEDIT |
5036ea90 | 229 | |
2b5f62a0 | 230 | m_privateContextMenu = NULL; |
5036ea90 | 231 | m_suppressNextUpdate = FALSE; |
e3a6a6b2 | 232 | m_isNativeCaretShown = true; |
2b5f62a0 VZ |
233 | } |
234 | ||
235 | wxTextCtrl::~wxTextCtrl() | |
236 | { | |
237 | if (m_privateContextMenu) | |
238 | { | |
239 | delete m_privateContextMenu; | |
240 | m_privateContextMenu = NULL; | |
241 | } | |
2bda0e17 KB |
242 | } |
243 | ||
debe6624 | 244 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, |
c085e333 VZ |
245 | const wxString& value, |
246 | const wxPoint& pos, | |
a1b82138 VZ |
247 | const wxSize& size, |
248 | long style, | |
c085e333 VZ |
249 | const wxValidator& validator, |
250 | const wxString& name) | |
2bda0e17 | 251 | { |
a1b82138 | 252 | // base initialization |
8d99be5f | 253 | if ( !CreateBase(parent, id, pos, size, style, validator, name) ) |
a1b82138 | 254 | return FALSE; |
2bda0e17 | 255 | |
a1b82138 VZ |
256 | if ( parent ) |
257 | parent->AddChild(this); | |
2bda0e17 | 258 | |
b2d5a7ee VZ |
259 | // translate wxWin style flags to MSW ones |
260 | WXDWORD msStyle = MSWGetCreateWindowFlags(); | |
cfdd3a98 | 261 | |
a1b82138 | 262 | // do create the control - either an EDIT or RICHEDIT |
b12915c1 | 263 | wxString windowClass = wxT("EDIT"); |
cd471848 | 264 | |
57c208c5 | 265 | #if wxUSE_RICHEDIT |
a5aa8086 VZ |
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 | |
c49245f8 | 279 | if ( m_windowStyle & wxTE_RICH ) |
a1b82138 | 280 | { |
a5aa8086 VZ |
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 | |
0d0512bd VZ |
296 | |
297 | // only give the error msg once if the DLL can't be loaded | |
298 | if ( !s_errorGiven ) | |
299 | { | |
a5aa8086 VZ |
300 | // try to load the RichEdit DLL (will do nothing if already done) |
301 | if ( !wxRichEditModule::Load(verRichEdit) ) | |
0d0512bd | 302 | { |
a5aa8086 VZ |
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")); | |
0d0512bd | 311 | |
a5aa8086 VZ |
312 | s_errorGiven = TRUE; |
313 | } | |
0d0512bd VZ |
314 | } |
315 | } | |
316 | ||
a5aa8086 | 317 | // have we managed to load any richedit version? |
aac7e7fe | 318 | if ( !s_errorGiven ) |
0d0512bd | 319 | { |
a5aa8086 | 320 | m_verRichEdit = verRichEdit; |
aac7e7fe | 321 | if ( m_verRichEdit == 1 ) |
b12915c1 VZ |
322 | { |
323 | windowClass = wxT("RICHEDIT"); | |
324 | } | |
325 | else | |
326 | { | |
327 | #ifndef RICHEDIT_CLASS | |
328 | wxString RICHEDIT_CLASS; | |
aac7e7fe | 329 | RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), m_verRichEdit); |
5fb2f4ba | 330 | #if wxUSE_UNICODE |
b12915c1 VZ |
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 | } | |
0d0512bd | 339 | } |
a1b82138 | 340 | } |
b12915c1 | 341 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 342 | |
c8b204e6 VZ |
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) ) | |
7da982c4 | 355 | return FALSE; |
2bda0e17 | 356 | |
a756f210 | 357 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
86f14582 | 358 | |
57c208c5 | 359 | #if wxUSE_RICHEDIT |
aac7e7fe | 360 | if ( IsRich() ) |
a1b82138 | 361 | { |
5036ea90 VZ |
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; | |
c57e3339 | 365 | |
1dae1d00 VZ |
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 ) | |
c57e3339 VZ |
373 | { |
374 | mask |= ENM_LINK; | |
375 | ||
376 | ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0); | |
377 | } | |
378 | ||
379 | ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask); | |
a1b82138 | 380 | } |
c57e3339 | 381 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 382 | |
a1b82138 | 383 | return TRUE; |
2bda0e17 KB |
384 | } |
385 | ||
386 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
cd471848 | 387 | void wxTextCtrl::AdoptAttributesFromHWND() |
2bda0e17 | 388 | { |
aac7e7fe | 389 | wxWindow::AdoptAttributesFromHWND(); |
2bda0e17 | 390 | |
aac7e7fe VZ |
391 | HWND hWnd = GetHwnd(); |
392 | long style = ::GetWindowLong(hWnd, GWL_STYLE); | |
2bda0e17 | 393 | |
aac7e7fe | 394 | // retrieve the style to see whether this is an edit or richedit ctrl |
cd471848 | 395 | #if wxUSE_RICHEDIT |
aac7e7fe | 396 | wxString classname = wxGetWindowClass(GetHWND()); |
2bda0e17 | 397 | |
aac7e7fe VZ |
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()); | |
2bda0e17 | 408 | |
aac7e7fe VZ |
409 | m_verRichEdit = 0; |
410 | } | |
411 | } | |
a1b82138 | 412 | #endif // wxUSE_RICHEDIT |
c085e333 | 413 | |
aac7e7fe VZ |
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; | |
e015d1f7 JS |
422 | if (style & ES_CENTER) |
423 | m_windowStyle |= wxTE_CENTRE; | |
424 | if (style & ES_RIGHT) | |
425 | m_windowStyle |= wxTE_RIGHT; | |
2bda0e17 KB |
426 | } |
427 | ||
b2d5a7ee VZ |
428 | WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const |
429 | { | |
430 | long msStyle = wxControl::MSWGetStyle(style, exstyle); | |
431 | ||
db50ec5a | 432 | // styles which we alaways add by default |
b2d5a7ee VZ |
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) ) | |
db50ec5a VZ |
440 | { |
441 | // always adjust the vertical scrollbar automatically if we have it | |
442 | msStyle |= WS_VSCROLL | ES_AUTOVSCROLL; | |
443 | ||
34433938 | 444 | #if wxUSE_RICHEDIT |
db50ec5a VZ |
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 | } | |
34433938 | 452 | #endif // wxUSE_RICHEDIT |
db50ec5a | 453 | } |
b2d5a7ee VZ |
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 | ||
db50ec5a | 464 | // styles which we add depending on the specified wxWindows styles |
b2d5a7ee | 465 | if ( style & wxHSCROLL ) |
db50ec5a VZ |
466 | { |
467 | // automatically scroll the control horizontally as necessary | |
468 | msStyle |= WS_HSCROLL;// | ES_AUTOHSCROLL; | |
469 | } | |
b2d5a7ee VZ |
470 | |
471 | if ( style & wxTE_READONLY ) | |
472 | msStyle |= ES_READONLY; | |
473 | ||
474 | if ( style & wxTE_PASSWORD ) | |
475 | msStyle |= ES_PASSWORD; | |
476 | ||
b2d5a7ee VZ |
477 | if ( style & wxTE_NOHIDESEL ) |
478 | msStyle |= ES_NOHIDESEL; | |
479 | ||
db50ec5a | 480 | // note that we can't do do "& wxTE_LEFT" as wxTE_LEFT == 0 |
e015d1f7 JS |
481 | if ( style & wxTE_CENTRE ) |
482 | msStyle |= ES_CENTER; | |
db50ec5a | 483 | else if ( style & wxTE_RIGHT ) |
e015d1f7 | 484 | msStyle |= ES_RIGHT; |
db50ec5a VZ |
485 | else |
486 | msStyle |= ES_LEFT; // ES_LEFT if 0 as well but for consistency... | |
e015d1f7 | 487 | |
b2d5a7ee VZ |
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 | ||
a1b82138 VZ |
510 | // ---------------------------------------------------------------------------- |
511 | // set/get the controls text | |
512 | // ---------------------------------------------------------------------------- | |
513 | ||
cd471848 | 514 | wxString wxTextCtrl::GetValue() const |
2bda0e17 | 515 | { |
a5aa8086 VZ |
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 | ||
b12915c1 | 530 | #if wxUSE_RICHEDIT |
aac7e7fe | 531 | if ( IsRich() ) |
b12915c1 | 532 | { |
3988b155 | 533 | int len = GetWindowTextLength(GetHwnd()); |
a5aa8086 | 534 | if ( len > from ) |
3988b155 | 535 | { |
de564874 MB |
536 | { |
537 | // alloc one extra WORD as needed by the control | |
538 | wxStringBuffer tmp(str, ++len); | |
539 | wxChar *p = tmp; | |
b12915c1 | 540 | |
de564874 MB |
541 | TEXTRANGE textRange; |
542 | textRange.chrg.cpMin = from; | |
543 | textRange.chrg.cpMax = to == -1 ? len : to; | |
544 | textRange.lpstrText = p; | |
b12915c1 | 545 | |
de564874 MB |
546 | (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, |
547 | 0, (LPARAM)&textRange); | |
b12915c1 | 548 | |
de564874 | 549 | if ( m_verRichEdit > 1 ) |
a5aa8086 | 550 | { |
de564874 MB |
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 | } | |
a5aa8086 | 559 | } |
3988b155 VZ |
560 | } |
561 | ||
a5aa8086 VZ |
562 | if ( m_verRichEdit == 1 ) |
563 | { | |
564 | // convert to the canonical form - see comment below | |
565 | str = wxTextFile::Translate(str, wxTextFileType_Unix); | |
566 | } | |
3988b155 VZ |
567 | } |
568 | //else: no text at all, leave the string empty | |
b12915c1 | 569 | } |
a5aa8086 | 570 | else |
b12915c1 | 571 | #endif // wxUSE_RICHEDIT |
a5aa8086 VZ |
572 | { |
573 | // retrieve all text | |
574 | str = wxGetWindowText(GetHWND()); | |
b12915c1 | 575 | |
a5aa8086 VZ |
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 | } | |
b12915c1 | 587 | |
a5aa8086 | 588 | return str; |
2bda0e17 KB |
589 | } |
590 | ||
591 | void wxTextCtrl::SetValue(const wxString& value) | |
592 | { | |
b12915c1 VZ |
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()) ) | |
07cf98cb | 598 | { |
79d26b32 | 599 | DoWriteText(value, FALSE /* not selection only */); |
da32743f | 600 | } |
199fbd70 VZ |
601 | else // same text |
602 | { | |
603 | // still send an event for consistency | |
604 | SendUpdateEvent(); | |
605 | } | |
af01f1ba | 606 | |
da32743f | 607 | // we should reset the modified flag even if the value didn't really change |
104d7404 | 608 | |
da32743f VZ |
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); | |
aac7e7fe | 615 | } |
a1b82138 | 616 | |
0b8e5844 | 617 | #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU) |
f6bcfd97 | 618 | |
aac7e7fe VZ |
619 | DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb) |
620 | { | |
621 | *pcb = 0; | |
f6bcfd97 | 622 | |
aac7e7fe VZ |
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); | |
07cf98cb | 631 | } |
aac7e7fe VZ |
632 | |
633 | *(const wchar_t **)dwCookie = wpc; | |
634 | ||
635 | return 0; | |
2bda0e17 KB |
636 | } |
637 | ||
886dd7d2 | 638 | // from utils.cpp |
bddd7a8d | 639 | extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding); |
aac7e7fe | 640 | |
0b8e5844 | 641 | #if wxUSE_UNICODE_MSLU |
79d26b32 VZ |
642 | bool wxTextCtrl::StreamIn(const wxString& value, |
643 | wxFontEncoding WXUNUSED(encoding), | |
644 | bool selectionOnly) | |
0b8e5844 VS |
645 | { |
646 | const wchar_t *wpc = value.c_str(); | |
79d26b32 VZ |
647 | #else // !wxUSE_UNICODE_MSLU |
648 | bool wxTextCtrl::StreamIn(const wxString& value, | |
649 | wxFontEncoding encoding, | |
650 | bool selectionOnly) | |
aac7e7fe VZ |
651 | { |
652 | // we have to use EM_STREAMIN to force richedit control 2.0+ to show any | |
2b5f62a0 | 653 | // text in the non default charset -- otherwise it thinks it knows better |
aac7e7fe VZ |
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); | |
855d6be7 VZ |
666 | |
667 | #if wxUSE_WCHAR_T | |
aac7e7fe | 668 | wxWCharBuffer wchBuf(len); |
855d6be7 VZ |
669 | #else |
670 | wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t)); | |
671 | #endif | |
672 | ||
aac7e7fe | 673 | if ( !::MultiByteToWideChar(codepage, 0, value, -1, |
855d6be7 | 674 | (wchar_t *)(const wchar_t *)wchBuf, len) ) |
aac7e7fe VZ |
675 | { |
676 | wxLogLastError(_T("MultiByteToWideChar")); | |
677 | } | |
678 | ||
679 | // finally, stream it in the control | |
680 | const wchar_t *wpc = wchBuf; | |
0b8e5844 | 681 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe VZ |
682 | |
683 | EDITSTREAM eds; | |
684 | wxZeroMemory(eds); | |
685 | eds.dwCookie = (DWORD)&wpc; | |
7a25a27c VZ |
686 | // the cast below is needed for broken (very) old mingw32 headers |
687 | eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn; | |
92209a39 | 688 | |
2b5f62a0 VZ |
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 | ||
07601f59 VZ |
697 | ::SendMessage(GetHwnd(), EM_STREAMIN, |
698 | SF_TEXT | | |
699 | SF_UNICODE | | |
700 | (selectionOnly ? SFF_SELECTION : 0), | |
701 | (LPARAM)&eds); | |
702 | ||
703 | if ( eds.dwError ) | |
aac7e7fe VZ |
704 | { |
705 | wxLogLastError(_T("EM_STREAMIN")); | |
aac7e7fe VZ |
706 | } |
707 | ||
855d6be7 VZ |
708 | #if !wxUSE_WCHAR_T |
709 | free(wchBuf); | |
710 | #endif // !wxUSE_WCHAR_T | |
711 | ||
aac7e7fe VZ |
712 | return TRUE; |
713 | } | |
714 | ||
715 | #endif // wxUSE_RICHEDIT | |
716 | ||
a1b82138 | 717 | void wxTextCtrl::WriteText(const wxString& value) |
79d26b32 VZ |
718 | { |
719 | DoWriteText(value); | |
720 | } | |
721 | ||
722 | void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly) | |
2bda0e17 | 723 | { |
a5aa8086 VZ |
724 | wxString valueDos; |
725 | if ( m_windowStyle & wxTE_MULTILINE ) | |
726 | valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); | |
727 | else | |
728 | valueDos = value; | |
2bda0e17 | 729 | |
4bc1afd5 | 730 | #if wxUSE_RICHEDIT |
aac7e7fe VZ |
731 | // there are several complications with the rich edit controls here |
732 | bool done = FALSE; | |
733 | if ( IsRich() ) | |
4bc1afd5 | 734 | { |
aac7e7fe VZ |
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); | |
0b8e5844 VS |
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 | |
136cb3c7 | 746 | if ( wxUsingUnicowsDll() && GetRichVersion() > 1 ) |
0b8e5844 | 747 | { |
79d26b32 | 748 | done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly); |
aac7e7fe | 749 | } |
0b8e5844 | 750 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe | 751 | |
b4da152e | 752 | #if !wxUSE_UNICODE |
aac7e7fe VZ |
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 | { | |
79d26b32 | 766 | done = StreamIn(valueDos, encoding, selectionOnly); |
aac7e7fe VZ |
767 | } |
768 | } | |
769 | } | |
9d7de3c2 | 770 | #endif // !wxUSE_UNICODE |
4bc1afd5 | 771 | } |
4bc1afd5 | 772 | |
aac7e7fe VZ |
773 | if ( !done ) |
774 | #endif // wxUSE_RICHEDIT | |
775 | { | |
2b5f62a0 VZ |
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 ( | |
5036ea90 | 784 | #if wxUSE_RICHEDIT |
2b5f62a0 VZ |
785 | IsRich() || |
786 | #endif // wxUSE_RICHEDIT | |
787 | (selectionOnly && HasSelection()) ) | |
79d26b32 | 788 | { |
5036ea90 | 789 | m_suppressNextUpdate = TRUE; |
79d26b32 VZ |
790 | } |
791 | ||
5036ea90 VZ |
792 | ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT, |
793 | 0, (LPARAM)valueDos.c_str()); | |
2b5f62a0 VZ |
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 | { | |
8d1e36f7 JS |
803 | // Windows already sends an update event for single-line |
804 | // controls. | |
805 | if ( m_windowStyle & wxTE_MULTILINE ) | |
806 | SendUpdateEvent(); | |
2b5f62a0 | 807 | } |
aac7e7fe | 808 | } |
2bda0e17 | 809 | |
a1b82138 | 810 | AdjustSpaceLimit(); |
2bda0e17 KB |
811 | } |
812 | ||
a1b82138 VZ |
813 | void wxTextCtrl::AppendText(const wxString& text) |
814 | { | |
815 | SetInsertionPointEnd(); | |
aac7e7fe | 816 | |
a1b82138 | 817 | WriteText(text); |
2b5f62a0 VZ |
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 | |
a1b82138 VZ |
827 | } |
828 | ||
829 | void wxTextCtrl::Clear() | |
830 | { | |
fda7962d | 831 | ::SetWindowText(GetHwnd(), wxEmptyString); |
5036ea90 VZ |
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 | |
8d1e36f7 JS |
840 | |
841 | // Windows already sends an update event for single-line | |
842 | // controls. | |
843 | if ( m_windowStyle & wxTE_MULTILINE ) | |
844 | SendUpdateEvent(); | |
5036ea90 | 845 | } |
a1b82138 VZ |
846 | } |
847 | ||
94af7d45 VZ |
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(); | |
566d84a7 RL |
857 | ::keybd_event(code, 0, 0 /* key press */, 0); |
858 | ::keybd_event(code, 0, KEYEVENTF_KEYUP, 0); | |
94af7d45 VZ |
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 | ||
a1b82138 | 867 | // ---------------------------------------------------------------------------- |
2bda0e17 | 868 | // Clipboard operations |
a1b82138 VZ |
869 | // ---------------------------------------------------------------------------- |
870 | ||
cd471848 | 871 | void wxTextCtrl::Copy() |
2bda0e17 | 872 | { |
e702ff0f JS |
873 | if (CanCopy()) |
874 | { | |
a5aa8086 | 875 | ::SendMessage(GetHwnd(), WM_COPY, 0, 0L); |
e702ff0f | 876 | } |
2bda0e17 KB |
877 | } |
878 | ||
cd471848 | 879 | void wxTextCtrl::Cut() |
2bda0e17 | 880 | { |
e702ff0f JS |
881 | if (CanCut()) |
882 | { | |
a5aa8086 | 883 | ::SendMessage(GetHwnd(), WM_CUT, 0, 0L); |
e702ff0f | 884 | } |
2bda0e17 KB |
885 | } |
886 | ||
cd471848 | 887 | void wxTextCtrl::Paste() |
2bda0e17 | 888 | { |
e702ff0f JS |
889 | if (CanPaste()) |
890 | { | |
a5aa8086 | 891 | ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L); |
e702ff0f | 892 | } |
2bda0e17 KB |
893 | } |
894 | ||
2b5f62a0 | 895 | bool wxTextCtrl::HasSelection() const |
a1b82138 | 896 | { |
a1b82138 | 897 | long from, to; |
a5aa8086 VZ |
898 | GetSelection(&from, &to); |
899 | return from != to; | |
a1b82138 VZ |
900 | } |
901 | ||
2b5f62a0 VZ |
902 | bool wxTextCtrl::CanCopy() const |
903 | { | |
904 | // Can copy if there's a selection | |
905 | return HasSelection(); | |
906 | } | |
907 | ||
a1b82138 VZ |
908 | bool wxTextCtrl::CanCut() const |
909 | { | |
a5aa8086 | 910 | return CanCopy() && IsEditable(); |
a1b82138 VZ |
911 | } |
912 | ||
913 | bool wxTextCtrl::CanPaste() const | |
914 | { | |
aac7e7fe VZ |
915 | if ( !IsEditable() ) |
916 | return FALSE; | |
917 | ||
a1b82138 | 918 | #if wxUSE_RICHEDIT |
aac7e7fe | 919 | if ( IsRich() ) |
a1b82138 | 920 | { |
aac7e7fe VZ |
921 | UINT cf = 0; // 0 == any format |
922 | ||
923 | return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0; | |
a1b82138 | 924 | } |
aac7e7fe | 925 | #endif // wxUSE_RICHEDIT |
a1b82138 VZ |
926 | |
927 | // Standard edit control: check for straight text on clipboard | |
aac7e7fe VZ |
928 | if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) |
929 | return FALSE; | |
930 | ||
931 | bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0; | |
932 | ::CloseClipboard(); | |
a1b82138 VZ |
933 | |
934 | return isTextAvailable; | |
935 | } | |
936 | ||
937 | // ---------------------------------------------------------------------------- | |
938 | // Accessors | |
939 | // ---------------------------------------------------------------------------- | |
940 | ||
debe6624 | 941 | void wxTextCtrl::SetEditable(bool editable) |
2bda0e17 | 942 | { |
a1b82138 VZ |
943 | HWND hWnd = GetHwnd(); |
944 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
2bda0e17 KB |
945 | } |
946 | ||
debe6624 | 947 | void wxTextCtrl::SetInsertionPoint(long pos) |
2bda0e17 | 948 | { |
a5aa8086 | 949 | DoSetSelection(pos, pos); |
2bda0e17 KB |
950 | } |
951 | ||
cd471848 | 952 | void wxTextCtrl::SetInsertionPointEnd() |
2bda0e17 | 953 | { |
a9d3434a VZ |
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 | ||
a5aa8086 VZ |
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 | ||
a1b82138 | 976 | SetInsertionPoint(pos); |
2bda0e17 KB |
977 | } |
978 | ||
cd471848 | 979 | long wxTextCtrl::GetInsertionPoint() const |
2bda0e17 | 980 | { |
57c208c5 | 981 | #if wxUSE_RICHEDIT |
aac7e7fe | 982 | if ( IsRich() ) |
a1b82138 VZ |
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 | } | |
aac7e7fe | 990 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 991 | |
a1b82138 VZ |
992 | DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); |
993 | return Pos & 0xFFFF; | |
2bda0e17 KB |
994 | } |
995 | ||
cd471848 | 996 | long wxTextCtrl::GetLastPosition() const |
2bda0e17 | 997 | { |
a5aa8086 VZ |
998 | int numLines = GetNumberOfLines(); |
999 | long posStartLastLine = XYToPosition(0, numLines - 1); | |
39136494 | 1000 | |
a5aa8086 | 1001 | long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine); |
2bda0e17 | 1002 | |
a5aa8086 | 1003 | return posStartLastLine + lenLastLine; |
2bda0e17 KB |
1004 | } |
1005 | ||
a1b82138 VZ |
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 | |
aac7e7fe | 1011 | if ( IsRich() ) |
a1b82138 VZ |
1012 | { |
1013 | CHARRANGE charRange; | |
aac7e7fe | 1014 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange); |
a1b82138 VZ |
1015 | |
1016 | *from = charRange.cpMin; | |
1017 | *to = charRange.cpMax; | |
a1b82138 | 1018 | } |
4bc1afd5 | 1019 | else |
aac7e7fe | 1020 | #endif // !wxUSE_RICHEDIT |
4bc1afd5 VZ |
1021 | { |
1022 | DWORD dwStart, dwEnd; | |
aac7e7fe | 1023 | ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); |
a1b82138 | 1024 | |
4bc1afd5 VZ |
1025 | *from = dwStart; |
1026 | *to = dwEnd; | |
1027 | } | |
a1b82138 VZ |
1028 | } |
1029 | ||
1030 | bool wxTextCtrl::IsEditable() const | |
1031 | { | |
51c14c62 VZ |
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 | ||
a1b82138 VZ |
1038 | long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); |
1039 | ||
a5aa8086 | 1040 | return (style & ES_READONLY) == 0; |
a1b82138 VZ |
1041 | } |
1042 | ||
1043 | // ---------------------------------------------------------------------------- | |
aac7e7fe | 1044 | // selection |
a1b82138 VZ |
1045 | // ---------------------------------------------------------------------------- |
1046 | ||
aac7e7fe | 1047 | void wxTextCtrl::SetSelection(long from, long to) |
2bda0e17 | 1048 | { |
aac7e7fe VZ |
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 | ||
a5aa8086 VZ |
1057 | DoSetSelection(from, to); |
1058 | } | |
1059 | ||
1060 | void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) | |
1061 | { | |
789295bf | 1062 | HWND hWnd = GetHwnd(); |
39136494 | 1063 | |
2bda0e17 | 1064 | #ifdef __WIN32__ |
aac7e7fe VZ |
1065 | #if wxUSE_RICHEDIT |
1066 | if ( IsRich() ) | |
1067 | { | |
db50ec5a VZ |
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 | |
98e19a58 VZ |
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 | |
db50ec5a VZ |
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 | |
98e19a58 VZ |
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 | } | |
aac7e7fe | 1107 | #endif // wxUSE_RICHEDIT |
a1b82138 | 1108 | |
aac7e7fe | 1109 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); |
98e19a58 VZ |
1110 | |
1111 | #if wxUSE_RICHEDIT | |
db50ec5a VZ |
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 | } | |
98e19a58 | 1118 | #endif // wxUSE_RICHEDIT |
db50ec5a | 1119 | } |
aac7e7fe VZ |
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 | // ---------------------------------------------------------------------------- | |
39136494 | 1129 | |
aac7e7fe VZ |
1130 | void wxTextCtrl::Replace(long from, long to, const wxString& value) |
1131 | { | |
1132 | // Set selection and remove it | |
f882d57e | 1133 | DoSetSelection(from, to, FALSE /* don't scroll caret into view */); |
aac7e7fe VZ |
1134 | |
1135 | SendMessage(GetHwnd(), EM_REPLACESEL, | |
2bda0e17 | 1136 | #ifdef __WIN32__ |
aac7e7fe | 1137 | TRUE, |
2bda0e17 | 1138 | #else |
aac7e7fe | 1139 | FALSE, |
2bda0e17 | 1140 | #endif |
aac7e7fe VZ |
1141 | (LPARAM)value.c_str()); |
1142 | } | |
1143 | ||
1144 | void wxTextCtrl::Remove(long from, long to) | |
1145 | { | |
fda7962d | 1146 | Replace(from, to, wxEmptyString); |
2bda0e17 KB |
1147 | } |
1148 | ||
1149 | bool wxTextCtrl::LoadFile(const wxString& file) | |
1150 | { | |
a1b82138 | 1151 | if ( wxTextCtrlBase::LoadFile(file) ) |
cd471848 | 1152 | { |
a1b82138 VZ |
1153 | // update the size limit if needed |
1154 | AdjustSpaceLimit(); | |
2bda0e17 | 1155 | |
a1b82138 | 1156 | return TRUE; |
2bda0e17 | 1157 | } |
2bda0e17 | 1158 | |
a1b82138 | 1159 | return FALSE; |
2bda0e17 KB |
1160 | } |
1161 | ||
cd471848 | 1162 | bool wxTextCtrl::IsModified() const |
2bda0e17 | 1163 | { |
a5aa8086 | 1164 | return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; |
2bda0e17 KB |
1165 | } |
1166 | ||
1167 | // Makes 'unmodified' | |
cd471848 | 1168 | void wxTextCtrl::DiscardEdits() |
2bda0e17 | 1169 | { |
a1b82138 | 1170 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); |
2bda0e17 KB |
1171 | } |
1172 | ||
cd471848 | 1173 | int wxTextCtrl::GetNumberOfLines() const |
2bda0e17 | 1174 | { |
789295bf | 1175 | return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); |
2bda0e17 KB |
1176 | } |
1177 | ||
debe6624 | 1178 | long wxTextCtrl::XYToPosition(long x, long y) const |
2bda0e17 | 1179 | { |
2bda0e17 | 1180 | // This gets the char index for the _beginning_ of this line |
a5aa8086 VZ |
1181 | long charIndex = SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0); |
1182 | ||
1183 | return charIndex + x; | |
2bda0e17 KB |
1184 | } |
1185 | ||
0efe5ba7 | 1186 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
2bda0e17 | 1187 | { |
789295bf | 1188 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
1189 | |
1190 | // This gets the line number containing the character | |
a5aa8086 | 1191 | long lineNo; |
0efe5ba7 | 1192 | #if wxUSE_RICHEDIT |
aac7e7fe | 1193 | if ( IsRich() ) |
0efe5ba7 | 1194 | { |
a5aa8086 | 1195 | lineNo = SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos); |
0efe5ba7 VZ |
1196 | } |
1197 | else | |
1198 | #endif // wxUSE_RICHEDIT | |
a5aa8086 VZ |
1199 | { |
1200 | lineNo = SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0); | |
1201 | } | |
0efe5ba7 VZ |
1202 | |
1203 | if ( lineNo == -1 ) | |
1204 | { | |
1205 | // no such line | |
1206 | return FALSE; | |
1207 | } | |
1208 | ||
2bda0e17 | 1209 | // This gets the char index for the _beginning_ of this line |
a5aa8086 | 1210 | long charIndex = SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); |
0efe5ba7 VZ |
1211 | if ( charIndex == -1 ) |
1212 | { | |
1213 | return FALSE; | |
1214 | } | |
1215 | ||
2bda0e17 | 1216 | // The X position must therefore be the different between pos and charIndex |
0efe5ba7 | 1217 | if ( x ) |
a5aa8086 | 1218 | *x = pos - charIndex; |
0efe5ba7 | 1219 | if ( y ) |
a5aa8086 | 1220 | *y = lineNo; |
0efe5ba7 VZ |
1221 | |
1222 | return TRUE; | |
2bda0e17 KB |
1223 | } |
1224 | ||
debe6624 | 1225 | void wxTextCtrl::ShowPosition(long pos) |
2bda0e17 | 1226 | { |
789295bf | 1227 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
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); | |
39136494 | 1243 | |
2bda0e17 KB |
1244 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; |
1245 | ||
2bda0e17 | 1246 | if (linesToScroll != 0) |
de4d7713 | 1247 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); |
2bda0e17 KB |
1248 | } |
1249 | ||
a5aa8086 VZ |
1250 | long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const |
1251 | { | |
1252 | return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0); | |
1253 | } | |
1254 | ||
debe6624 | 1255 | int wxTextCtrl::GetLineLength(long lineNo) const |
2bda0e17 | 1256 | { |
a5aa8086 VZ |
1257 | long pos = XYToPosition(0, lineNo); |
1258 | ||
1259 | return GetLengthOfLineContainingPos(pos); | |
2bda0e17 KB |
1260 | } |
1261 | ||
debe6624 | 1262 | wxString wxTextCtrl::GetLineText(long lineNo) const |
2bda0e17 | 1263 | { |
a1b82138 | 1264 | size_t len = (size_t)GetLineLength(lineNo) + 1; |
488fe1fe | 1265 | |
f6bcfd97 BP |
1266 | // there must be at least enough place for the length WORD in the |
1267 | // buffer | |
1268 | len += sizeof(WORD); | |
4438caf4 | 1269 | |
f6bcfd97 | 1270 | wxString str; |
de564874 MB |
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 | } | |
4438caf4 VZ |
1281 | |
1282 | return str; | |
2bda0e17 KB |
1283 | } |
1284 | ||
d7eee191 VZ |
1285 | void wxTextCtrl::SetMaxLength(unsigned long len) |
1286 | { | |
1287 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0); | |
1288 | } | |
1289 | ||
a1b82138 | 1290 | // ---------------------------------------------------------------------------- |
ca8b28f2 | 1291 | // Undo/redo |
a1b82138 VZ |
1292 | // ---------------------------------------------------------------------------- |
1293 | ||
ca8b28f2 JS |
1294 | void wxTextCtrl::Undo() |
1295 | { | |
1296 | if (CanUndo()) | |
1297 | { | |
789295bf | 1298 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
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. | |
789295bf | 1307 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
1308 | } |
1309 | } | |
1310 | ||
1311 | bool wxTextCtrl::CanUndo() const | |
1312 | { | |
a5aa8086 | 1313 | return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; |
ca8b28f2 JS |
1314 | } |
1315 | ||
1316 | bool wxTextCtrl::CanRedo() const | |
1317 | { | |
a5aa8086 | 1318 | return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; |
ca8b28f2 JS |
1319 | } |
1320 | ||
e3a6a6b2 VZ |
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 | ||
a1b82138 VZ |
1342 | // ---------------------------------------------------------------------------- |
1343 | // implemenation details | |
1344 | // ---------------------------------------------------------------------------- | |
39136494 | 1345 | |
2bda0e17 KB |
1346 | void wxTextCtrl::Command(wxCommandEvent & event) |
1347 | { | |
a1b82138 VZ |
1348 | SetValue(event.GetString()); |
1349 | ProcessCommand (event); | |
2bda0e17 KB |
1350 | } |
1351 | ||
1352 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
1353 | { | |
a1b82138 VZ |
1354 | // By default, load the first file into the text window. |
1355 | if (event.GetNumberOfFiles() > 0) | |
1356 | { | |
1357 | LoadFile(event.GetFiles()[0]); | |
1358 | } | |
2bda0e17 KB |
1359 | } |
1360 | ||
a37d422a VZ |
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 | { | |
574c939e | 1374 | WORD vkey = (WORD) msg->wParam; |
a37d422a VZ |
1375 | if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN ) |
1376 | { | |
1377 | if ( vkey == VK_BACK ) | |
1378 | return FALSE; | |
1379 | } | |
1380 | else // no Alt | |
1381 | { | |
cf6e951c VZ |
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 ) | |
a37d422a | 1388 | { |
cf6e951c VZ |
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 | } | |
a37d422a VZ |
1418 | } |
1419 | } | |
1420 | } | |
1421 | ||
1422 | return wxControl::MSWShouldPreProcessMessage(pMsg); | |
1423 | } | |
1424 | ||
2bda0e17 KB |
1425 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
1426 | { | |
77e00fe9 | 1427 | switch ( event.GetKeyCode() ) |
cd471848 | 1428 | { |
cd471848 | 1429 | case WXK_RETURN: |
39136494 | 1430 | if ( !(m_windowStyle & wxTE_MULTILINE) ) |
cd471848 VZ |
1431 | { |
1432 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
bfbd6dc1 | 1433 | InitCommandEvent(event); |
f6bcfd97 | 1434 | event.SetString(GetValue()); |
cd471848 VZ |
1435 | if ( GetEventHandler()->ProcessEvent(event) ) |
1436 | return; | |
1437 | } | |
5fb9fcfc VZ |
1438 | //else: multiline controls need Enter for themselves |
1439 | ||
1440 | break; | |
4d91c1d1 | 1441 | |
cd471848 | 1442 | case WXK_TAB: |
319fefa9 VZ |
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. | |
cd471848 | 1447 | { |
5fb9fcfc VZ |
1448 | wxNavigationKeyEvent eventNav; |
1449 | eventNav.SetDirection(!event.ShiftDown()); | |
8614c467 | 1450 | eventNav.SetWindowChange(event.ControlDown()); |
5fb9fcfc | 1451 | eventNav.SetEventObject(this); |
39136494 | 1452 | |
d9506e77 | 1453 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) |
cd471848 VZ |
1454 | return; |
1455 | } | |
341c92a8 | 1456 | break; |
cd471848 | 1457 | } |
39136494 | 1458 | |
8614c467 | 1459 | // no, we didn't process it |
42e69d6b | 1460 | event.Skip(); |
2bda0e17 KB |
1461 | } |
1462 | ||
0cf5b099 VZ |
1463 | long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
1464 | { | |
e7e91e03 VZ |
1465 | long lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam); |
1466 | ||
0cf5b099 VZ |
1467 | if ( nMsg == WM_GETDLGCODE ) |
1468 | { | |
2b5f62a0 VZ |
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 | ||
080c709f VZ |
1474 | if ( IsEditable() ) |
1475 | { | |
080c709f VZ |
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 | { | |
e52d9c78 VZ |
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. | |
2b5f62a0 | 1500 | lRc = lDlgCode; |
080c709f | 1501 | } |
0cf5b099 VZ |
1502 | } |
1503 | ||
e7e91e03 | 1504 | return lRc; |
129223d6 VZ |
1505 | } |
1506 | ||
1507 | // ---------------------------------------------------------------------------- | |
1508 | // text control event processing | |
1509 | // ---------------------------------------------------------------------------- | |
1510 | ||
5036ea90 VZ |
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 | ||
debe6624 | 1529 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 1530 | { |
5036ea90 | 1531 | switch ( param ) |
789295bf VZ |
1532 | { |
1533 | case EN_SETFOCUS: | |
1534 | case EN_KILLFOCUS: | |
1535 | { | |
1536 | wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS | |
d7eee191 VZ |
1537 | : wxEVT_SET_FOCUS, |
1538 | m_windowId); | |
5036ea90 | 1539 | event.SetEventObject(this); |
789295bf VZ |
1540 | GetEventHandler()->ProcessEvent(event); |
1541 | } | |
1542 | break; | |
ae29de83 | 1543 | |
789295bf | 1544 | case EN_CHANGE: |
5036ea90 | 1545 | SendUpdateEvent(); |
789295bf | 1546 | break; |
2bda0e17 | 1547 | |
b12915c1 | 1548 | case EN_MAXTEXT: |
5036ea90 | 1549 | // the text size limit has been hit -- try to increase it |
d7eee191 VZ |
1550 | if ( !AdjustSpaceLimit() ) |
1551 | { | |
1552 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId); | |
1553 | InitCommandEvent(event); | |
1554 | event.SetString(GetValue()); | |
1555 | ProcessCommand(event); | |
1556 | } | |
789295bf VZ |
1557 | break; |
1558 | ||
5036ea90 | 1559 | // the other edit notification messages are not processed |
789295bf VZ |
1560 | default: |
1561 | return FALSE; | |
1562 | } | |
1563 | ||
1564 | // processed | |
1565 | return TRUE; | |
2bda0e17 KB |
1566 | } |
1567 | ||
33ac7e6f | 1568 | WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
1569 | #if wxUSE_CTL3D |
1570 | WXUINT message, | |
1571 | WXWPARAM wParam, | |
1572 | WXLPARAM lParam | |
1573 | #else | |
33ac7e6f KB |
1574 | WXUINT WXUNUSED(message), |
1575 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
1576 | WXLPARAM WXUNUSED(lParam) |
1577 | #endif | |
1578 | ) | |
f6bcfd97 BP |
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; | |
f6bcfd97 BP |
1589 | wxColour colBack = GetBackgroundColour(); |
1590 | ||
1591 | if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0) | |
a756f210 | 1592 | colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
f6bcfd97 BP |
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) | |
a756f210 | 1611 | col = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); |
f6bcfd97 BP |
1612 | #endif |
1613 | ||
1614 | RECT rect; | |
1615 | ::GetClientRect(GetHwnd(), &rect); | |
1616 | ||
a5aa8086 | 1617 | COLORREF ref = wxColourToRGB(col); |
f6bcfd97 BP |
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 | } | |
aac7e7fe | 1631 | #endif // Win16 |
f6bcfd97 | 1632 | |
d7eee191 | 1633 | bool wxTextCtrl::AdjustSpaceLimit() |
789295bf | 1634 | { |
25889d3c | 1635 | #ifndef __WIN16__ |
d7eee191 VZ |
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()); | |
17d8ee1c | 1654 | if ( len >= limit ) |
789295bf VZ |
1655 | { |
1656 | limit = len + 0x8000; // 32Kb | |
1657 | ||
5ea105e0 | 1658 | #if wxUSE_RICHEDIT |
aac7e7fe | 1659 | if ( IsRich() ) |
b12915c1 VZ |
1660 | { |
1661 | // as a nice side effect, this also allows passing limit > 64Kb | |
1662 | ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit); | |
1663 | } | |
789295bf | 1664 | else |
b12915c1 VZ |
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 | ||
789295bf | 1674 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0); |
b12915c1 | 1675 | } |
789295bf | 1676 | } |
b12915c1 | 1677 | #endif // !Win16 |
d7eee191 VZ |
1678 | |
1679 | // we changed the limit | |
1680 | return TRUE; | |
789295bf | 1681 | } |
2bda0e17 | 1682 | |
a1b82138 | 1683 | bool wxTextCtrl::AcceptsFocus() const |
2bda0e17 | 1684 | { |
589c7163 VZ |
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(); | |
a1b82138 | 1689 | } |
c085e333 | 1690 | |
f68586e5 | 1691 | wxSize wxTextCtrl::DoGetBestSize() const |
a1b82138 VZ |
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 | { | |
3988b155 | 1701 | hText *= wxMax(GetNumberOfLines(), 5); |
a1b82138 VZ |
1702 | } |
1703 | //else: for single line control everything is ok | |
1704 | ||
1705 | return wxSize(wText, hText); | |
2bda0e17 | 1706 | } |
a1b82138 VZ |
1707 | |
1708 | // ---------------------------------------------------------------------------- | |
1709 | // standard handlers for standard edit menu events | |
1710 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1711 | |
bfbd6dc1 | 1712 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1713 | { |
1714 | Cut(); | |
1715 | } | |
1716 | ||
bfbd6dc1 | 1717 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1718 | { |
1719 | Copy(); | |
1720 | } | |
1721 | ||
bfbd6dc1 | 1722 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1723 | { |
1724 | Paste(); | |
1725 | } | |
1726 | ||
bfbd6dc1 | 1727 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1728 | { |
1729 | Undo(); | |
1730 | } | |
1731 | ||
bfbd6dc1 | 1732 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1733 | { |
1734 | Redo(); | |
1735 | } | |
1736 | ||
2eb10e2a | 1737 | void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event)) |
2b5f62a0 VZ |
1738 | { |
1739 | long from, to; | |
1740 | GetSelection(& from, & to); | |
1741 | if (from != -1 && to != -1) | |
1742 | Remove(from, to); | |
1743 | } | |
1744 | ||
2eb10e2a | 1745 | void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) |
2b5f62a0 VZ |
1746 | { |
1747 | SetSelection(-1, -1); | |
1748 | } | |
1749 | ||
e702ff0f JS |
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 | ||
2b5f62a0 VZ |
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 | ||
2eb10e2a | 1813 | void wxTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) |
e3a6a6b2 VZ |
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 | ||
4bc1afd5 VZ |
1822 | // the rest of the file only deals with the rich edit controls |
1823 | #if wxUSE_RICHEDIT | |
1824 | ||
c57e3339 VZ |
1825 | // ---------------------------------------------------------------------------- |
1826 | // EN_LINK processing | |
1827 | // ---------------------------------------------------------------------------- | |
1828 | ||
a64c3b74 | 1829 | bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
c57e3339 VZ |
1830 | { |
1831 | NMHDR *hdr = (NMHDR* )lParam; | |
1dae1d00 | 1832 | switch ( hdr->code ) |
c57e3339 | 1833 | { |
3bce6687 | 1834 | case EN_MSGFILTER: |
1dae1d00 VZ |
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 ) | |
c57e3339 | 1850 | { |
1dae1d00 VZ |
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); | |
c57e3339 | 1856 | } |
1dae1d00 | 1857 | } |
c57e3339 | 1858 | |
1dae1d00 VZ |
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; | |
c57e3339 | 1916 | } |
7f5d8b00 | 1917 | |
d86ab8e2 VZ |
1918 | // not processed, leave it to the base class |
1919 | return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result); | |
c57e3339 VZ |
1920 | } |
1921 | ||
f6bcfd97 BP |
1922 | // ---------------------------------------------------------------------------- |
1923 | // colour setting for the rich edit controls | |
1924 | // ---------------------------------------------------------------------------- | |
1925 | ||
f6bcfd97 BP |
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 | ||
4bc1afd5 VZ |
1966 | // ---------------------------------------------------------------------------- |
1967 | // styling support for rich edit controls | |
1968 | // ---------------------------------------------------------------------------- | |
1969 | ||
cc164686 RD |
1970 | #if wxUSE_RICHEDIT |
1971 | ||
4bc1afd5 VZ |
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 | ||
a5aa8086 VZ |
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 | |
e00a5d3c JS |
1982 | if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() && |
1983 | !style.HasLeftIndent() && !style.HasRightIndent() && !style.HasAlignment() && | |
1984 | !style.HasTabs() ) | |
4bc1afd5 | 1985 | { |
be329a3d RD |
1986 | // nothing to do: return TRUE if there was really nothing to do and |
1987 | // FALSE if we failed to set bg colour | |
4bc1afd5 VZ |
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 ) | |
aac7e7fe | 2008 | { |
f882d57e | 2009 | DoSetSelection(start, end, FALSE /* don't scroll caret into view */); |
aac7e7fe | 2010 | } |
4bc1afd5 VZ |
2011 | |
2012 | // initialize CHARFORMAT struct | |
be329a3d RD |
2013 | #if wxUSE_RICHEDIT2 |
2014 | CHARFORMAT2 cf; | |
2015 | #else | |
4bc1afd5 | 2016 | CHARFORMAT cf; |
be329a3d | 2017 | #endif |
a5aa8086 | 2018 | |
4bc1afd5 | 2019 | wxZeroMemory(cf); |
a5aa8086 VZ |
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 | } | |
4bc1afd5 VZ |
2035 | |
2036 | if ( style.HasFont() ) | |
2037 | { | |
aac7e7fe VZ |
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 | ||
784164e1 VZ |
2041 | cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET | |
2042 | CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE; | |
4bc1afd5 VZ |
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 | ||
784164e1 VZ |
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 | |
4bc1afd5 VZ |
2058 | if ( lf.lfItalic ) |
2059 | { | |
4bc1afd5 VZ |
2060 | cf.dwEffects |= CFE_ITALIC; |
2061 | } | |
2062 | ||
2063 | if ( lf.lfWeight == FW_BOLD ) | |
2064 | { | |
4bc1afd5 VZ |
2065 | cf.dwEffects |= CFE_BOLD; |
2066 | } | |
2067 | ||
2068 | if ( lf.lfUnderline ) | |
2069 | { | |
4bc1afd5 VZ |
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 | ||
be329a3d | 2082 | #if wxUSE_RICHEDIT2 |
a5aa8086 | 2083 | if ( m_verRichEdit != 1 && style.HasBackgroundColour() ) |
be329a3d RD |
2084 | { |
2085 | cf.dwMask |= CFM_BACKCOLOR; | |
2086 | cf.crBackColor = wxColourToRGB(style.GetBackgroundColour()); | |
2087 | } | |
784164e1 VZ |
2088 | #endif // wxUSE_RICHEDIT2 |
2089 | ||
4bc1afd5 VZ |
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 | ||
e00a5d3c JS |
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 | ||
4bc1afd5 VZ |
2173 | if ( changeSel ) |
2174 | { | |
2175 | // restore the original selection | |
aac7e7fe | 2176 | DoSetSelection(startOld, endOld, FALSE); |
4bc1afd5 VZ |
2177 | } |
2178 | ||
2179 | return ok; | |
2180 | } | |
f6bcfd97 | 2181 | |
5bf75ae7 VZ |
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 | ||
80185c6c | 2195 | bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) |
e00a5d3c | 2196 | { |
80185c6c JS |
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; | |
e00a5d3c JS |
2324 | } |
2325 | ||
cc164686 RD |
2326 | #endif |
2327 | ||
b12915c1 VZ |
2328 | // ---------------------------------------------------------------------------- |
2329 | // wxRichEditModule | |
2330 | // ---------------------------------------------------------------------------- | |
2331 | ||
b12915c1 VZ |
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 | { | |
136cb3c7 | 2340 | for ( size_t i = 0; i < WXSIZEOF(ms_hRichEdit); i++ ) |
b12915c1 | 2341 | { |
a5aa8086 VZ |
2342 | if ( ms_hRichEdit[i] ) |
2343 | { | |
2344 | ::FreeLibrary(ms_hRichEdit[i]); | |
8c74e477 | 2345 | ms_hRichEdit[i] = NULL; |
a5aa8086 | 2346 | } |
b12915c1 VZ |
2347 | } |
2348 | } | |
2349 | ||
2350 | /* static */ | |
2351 | bool wxRichEditModule::Load(int version) | |
2352 | { | |
a5aa8086 VZ |
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, | |
b12915c1 VZ |
2356 | _T("incorrect richedit control version requested") ); |
2357 | ||
a5aa8086 VZ |
2358 | // make it the index in the array |
2359 | version--; | |
2360 | ||
a5aa8086 | 2361 | if ( ms_hRichEdit[version] == (HINSTANCE)-1 ) |
b12915c1 | 2362 | { |
a5aa8086 VZ |
2363 | // we had already tried to load it and failed |
2364 | return FALSE; | |
b12915c1 VZ |
2365 | } |
2366 | ||
28978e0c VZ |
2367 | if ( ms_hRichEdit[version] ) |
2368 | { | |
2369 | // we've already got this one | |
2370 | return TRUE; | |
2371 | } | |
2372 | ||
a5aa8086 VZ |
2373 | wxString dllname = version ? _T("riched20") : _T("riched32"); |
2374 | dllname += _T(".dll"); | |
b12915c1 | 2375 | |
a5aa8086 | 2376 | ms_hRichEdit[version] = ::LoadLibrary(dllname); |
b12915c1 | 2377 | |
a5aa8086 | 2378 | if ( !ms_hRichEdit[version] ) |
b12915c1 VZ |
2379 | { |
2380 | wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str()); | |
2381 | ||
a5aa8086 | 2382 | ms_hRichEdit[version] = (HINSTANCE)-1; |
b12915c1 VZ |
2383 | |
2384 | return FALSE; | |
2385 | } | |
2386 | ||
2387 | return TRUE; | |
2388 | } | |
2389 | ||
2390 | #endif // wxUSE_RICHEDIT | |
2391 | ||
1e6feb95 | 2392 | #endif // wxUSE_TEXTCTRL |