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