]>
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c085e333 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
a1b82138 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
2bda0e17 | 16 | #ifdef __GNUG__ |
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" |
2bda0e17 KB |
41 | #endif |
42 | ||
f6bcfd97 BP |
43 | #include "wx/module.h" |
44 | ||
47d67540 | 45 | #if wxUSE_CLIPBOARD |
a1b82138 | 46 | #include "wx/clipbrd.h" |
2bda0e17 KB |
47 | #endif |
48 | ||
a1b82138 VZ |
49 | #include "wx/textfile.h" |
50 | ||
51 | #include <windowsx.h> | |
52 | ||
2bda0e17 KB |
53 | #include "wx/msw/private.h" |
54 | ||
a1b82138 | 55 | #include <string.h> |
2bda0e17 | 56 | #include <stdlib.h> |
a1b82138 | 57 | #include <sys/types.h> |
fbc535ff | 58 | |
ae090fdb | 59 | #if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)) |
cd471848 | 60 | #include <richedit.h> |
2bda0e17 KB |
61 | #endif |
62 | ||
e80591b9 VZ |
63 | // old mingw32 doesn't define this |
64 | #ifndef CFM_CHARSET | |
65 | #define CFM_CHARSET 0x08000000 | |
66 | #endif // CFM_CHARSET | |
67 | ||
784164e1 VZ |
68 | #ifndef CFM_BACKCOLOR |
69 | #define CFM_BACKCOLOR 0x04000000 | |
70 | #endif | |
71 | ||
733e8cf3 VZ |
72 | // cygwin does not have these defined for richedit |
73 | #ifndef ENM_LINK | |
74 | #define ENM_LINK 0x04000000 | |
75 | #endif | |
76 | ||
77 | #ifndef EM_AUTOURLDETECT | |
78 | #define EM_AUTOURLDETECT (WM_USER + 91) | |
79 | #endif | |
80 | ||
81 | #ifndef EN_LINK | |
82 | #define EN_LINK 0x070b | |
83 | ||
84 | typedef struct _enlink | |
85 | { | |
86 | NMHDR nmhdr; | |
87 | UINT msg; | |
88 | WPARAM wParam; | |
89 | LPARAM lParam; | |
90 | CHARRANGE chrg; | |
91 | } ENLINK; | |
92 | #endif // ENLINK | |
93 | ||
7a25a27c VZ |
94 | #ifndef SF_UNICODE |
95 | #define SF_UNICODE 0x0010 | |
96 | #endif | |
97 | ||
a5aa8086 VZ |
98 | // Watcom C++ doesn't define this |
99 | #ifndef SCF_ALL | |
100 | #define SCF_ALL 0x0004 | |
101 | #endif | |
102 | ||
aac7e7fe VZ |
103 | // ---------------------------------------------------------------------------- |
104 | // private functions | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | #if wxUSE_RICHEDIT | |
108 | ||
109 | DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb); | |
110 | ||
111 | #endif // wxUSE_RICHEDIT | |
112 | ||
b12915c1 VZ |
113 | // ---------------------------------------------------------------------------- |
114 | // private classes | |
115 | // ---------------------------------------------------------------------------- | |
116 | ||
117 | #if wxUSE_RICHEDIT | |
118 | ||
a5aa8086 | 119 | // this module initializes RichEdit DLL(s) if needed |
b12915c1 VZ |
120 | class wxRichEditModule : public wxModule |
121 | { | |
122 | public: | |
123 | virtual bool OnInit(); | |
124 | virtual void OnExit(); | |
125 | ||
b12915c1 VZ |
126 | // load the richedit DLL of at least of required version |
127 | static bool Load(int version = 1); | |
128 | ||
129 | private: | |
a5aa8086 VZ |
130 | // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs |
131 | static HINSTANCE ms_hRichEdit[2]; | |
b12915c1 VZ |
132 | |
133 | DECLARE_DYNAMIC_CLASS(wxRichEditModule) | |
134 | }; | |
135 | ||
a5aa8086 | 136 | HINSTANCE wxRichEditModule::ms_hRichEdit[2] = { NULL, NULL }; |
b12915c1 VZ |
137 | |
138 | IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule) | |
139 | ||
140 | #endif // wxUSE_RICHEDIT | |
e702ff0f | 141 | |
a1b82138 VZ |
142 | // ---------------------------------------------------------------------------- |
143 | // event tables and other macros | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
2bda0e17 KB |
146 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) |
147 | ||
148 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
a1b82138 VZ |
149 | EVT_CHAR(wxTextCtrl::OnChar) |
150 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
151 | ||
152 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
153 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
154 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
155 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
156 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
157 | ||
158 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
159 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
160 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
161 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
162 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
f6bcfd97 BP |
163 | #ifdef __WIN16__ |
164 | EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) | |
165 | #endif | |
2bda0e17 | 166 | END_EVENT_TABLE() |
e702ff0f | 167 | |
a1b82138 VZ |
168 | // ============================================================================ |
169 | // implementation | |
170 | // ============================================================================ | |
171 | ||
172 | // ---------------------------------------------------------------------------- | |
173 | // creation | |
174 | // ---------------------------------------------------------------------------- | |
175 | ||
aac7e7fe | 176 | void wxTextCtrl::Init() |
2bda0e17 | 177 | { |
cd471848 | 178 | #if wxUSE_RICHEDIT |
aac7e7fe | 179 | m_verRichEdit = 0; |
5036ea90 VZ |
180 | |
181 | m_suppressNextUpdate = FALSE; | |
182 | #endif // wxUSE_RICHEDIT | |
2bda0e17 KB |
183 | } |
184 | ||
debe6624 | 185 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, |
c085e333 VZ |
186 | const wxString& value, |
187 | const wxPoint& pos, | |
a1b82138 VZ |
188 | const wxSize& size, |
189 | long style, | |
c085e333 VZ |
190 | const wxValidator& validator, |
191 | const wxString& name) | |
2bda0e17 | 192 | { |
a1b82138 | 193 | // base initialization |
8d99be5f | 194 | if ( !CreateBase(parent, id, pos, size, style, validator, name) ) |
a1b82138 | 195 | return FALSE; |
2bda0e17 | 196 | |
a1b82138 VZ |
197 | if ( parent ) |
198 | parent->AddChild(this); | |
2bda0e17 | 199 | |
b2d5a7ee VZ |
200 | // translate wxWin style flags to MSW ones |
201 | WXDWORD msStyle = MSWGetCreateWindowFlags(); | |
cfdd3a98 | 202 | |
a1b82138 | 203 | // do create the control - either an EDIT or RICHEDIT |
b12915c1 | 204 | wxString windowClass = wxT("EDIT"); |
cd471848 | 205 | |
57c208c5 | 206 | #if wxUSE_RICHEDIT |
a5aa8086 VZ |
207 | if ( m_windowStyle & wxTE_AUTO_URL ) |
208 | { | |
209 | // automatic URL detection only works in RichEdit 2.0+ | |
210 | m_windowStyle |= wxTE_RICH2; | |
211 | } | |
212 | ||
213 | if ( m_windowStyle & wxTE_RICH2 ) | |
214 | { | |
215 | // using richedit 2.0 implies using wxTE_RICH | |
216 | m_windowStyle |= wxTE_RICH; | |
217 | } | |
218 | ||
219 | // we need to load the richedit DLL before creating the rich edit control | |
c49245f8 | 220 | if ( m_windowStyle & wxTE_RICH ) |
a1b82138 | 221 | { |
a5aa8086 VZ |
222 | static bool s_errorGiven = FALSE;// MT-FIXME |
223 | ||
224 | // Which version do we need? Use 1.0 by default because it is much more | |
225 | // like the the standard EDIT or 2.0 if explicitly requested, but use | |
226 | // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all | |
227 | // | |
228 | // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0 | |
229 | // (and thus EDIT) much better than RichEdit 2.0 so we probably | |
230 | // should use 3.0 if available as it is the best of both worlds - | |
231 | // but as I can't test it right now I don't do it (VZ) | |
232 | #if wxUSE_UNICODE | |
233 | const int verRichEdit = 2; | |
234 | #else // !wxUSE_UNICODE | |
235 | int verRichEdit = m_windowStyle & wxTE_RICH2 ? 2 : 1; | |
236 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
0d0512bd VZ |
237 | |
238 | // only give the error msg once if the DLL can't be loaded | |
239 | if ( !s_errorGiven ) | |
240 | { | |
a5aa8086 VZ |
241 | // try to load the RichEdit DLL (will do nothing if already done) |
242 | if ( !wxRichEditModule::Load(verRichEdit) ) | |
0d0512bd | 243 | { |
a5aa8086 VZ |
244 | #if !wxUSE_UNICODE |
245 | // try another version? | |
246 | verRichEdit = 3 - verRichEdit; // 1 <-> 2 | |
247 | ||
248 | if ( !wxRichEditModule::Load(verRichEdit) ) | |
249 | #endif // wxUSE_UNICODE | |
250 | { | |
251 | wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll")); | |
0d0512bd | 252 | |
a5aa8086 VZ |
253 | s_errorGiven = TRUE; |
254 | } | |
0d0512bd VZ |
255 | } |
256 | } | |
257 | ||
a5aa8086 | 258 | // have we managed to load any richedit version? |
aac7e7fe | 259 | if ( !s_errorGiven ) |
0d0512bd VZ |
260 | { |
261 | msStyle |= ES_AUTOVSCROLL; | |
aac7e7fe | 262 | |
a5aa8086 | 263 | m_verRichEdit = verRichEdit; |
aac7e7fe | 264 | if ( m_verRichEdit == 1 ) |
b12915c1 VZ |
265 | { |
266 | windowClass = wxT("RICHEDIT"); | |
267 | } | |
268 | else | |
269 | { | |
270 | #ifndef RICHEDIT_CLASS | |
271 | wxString RICHEDIT_CLASS; | |
aac7e7fe | 272 | RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), m_verRichEdit); |
5fb2f4ba | 273 | #if wxUSE_UNICODE |
b12915c1 VZ |
274 | RICHEDIT_CLASS += _T('W'); |
275 | #else // ANSI | |
276 | RICHEDIT_CLASS += _T('A'); | |
277 | #endif // Unicode/ANSI | |
278 | #endif // !RICHEDIT_CLASS | |
279 | ||
280 | windowClass = RICHEDIT_CLASS; | |
281 | } | |
0d0512bd | 282 | } |
a1b82138 | 283 | } |
b12915c1 | 284 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 285 | |
c8b204e6 VZ |
286 | // we need to turn '\n's into "\r\n"s for the multiline controls |
287 | wxString valueWin; | |
288 | if ( m_windowStyle & wxTE_MULTILINE ) | |
289 | { | |
290 | valueWin = wxTextFile::Translate(value, wxTextFileType_Dos); | |
291 | } | |
292 | else // single line | |
293 | { | |
294 | valueWin = value; | |
295 | } | |
296 | ||
297 | if ( !MSWCreateControl(windowClass, msStyle, pos, size, valueWin) ) | |
7da982c4 | 298 | return FALSE; |
2bda0e17 | 299 | |
a756f210 | 300 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
86f14582 | 301 | |
57c208c5 | 302 | #if wxUSE_RICHEDIT |
aac7e7fe | 303 | if ( IsRich() ) |
a1b82138 | 304 | { |
5036ea90 VZ |
305 | // enable the events we're interested in: we want to get EN_CHANGE as |
306 | // for the normal controls | |
307 | LPARAM mask = ENM_CHANGE; | |
c57e3339 | 308 | |
1dae1d00 VZ |
309 | if ( GetRichVersion() == 1 ) |
310 | { | |
311 | // we also need EN_MSGFILTER for richedit 1.0 for the reasons | |
312 | // explained in its handler | |
313 | mask |= ENM_MOUSEEVENTS; | |
314 | } | |
315 | else if ( m_windowStyle & wxTE_AUTO_URL ) | |
c57e3339 VZ |
316 | { |
317 | mask |= ENM_LINK; | |
318 | ||
319 | ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0); | |
320 | } | |
321 | ||
322 | ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask); | |
a1b82138 | 323 | } |
c57e3339 | 324 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 325 | |
a1b82138 | 326 | return TRUE; |
2bda0e17 KB |
327 | } |
328 | ||
329 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
cd471848 | 330 | void wxTextCtrl::AdoptAttributesFromHWND() |
2bda0e17 | 331 | { |
aac7e7fe | 332 | wxWindow::AdoptAttributesFromHWND(); |
2bda0e17 | 333 | |
aac7e7fe VZ |
334 | HWND hWnd = GetHwnd(); |
335 | long style = ::GetWindowLong(hWnd, GWL_STYLE); | |
2bda0e17 | 336 | |
aac7e7fe | 337 | // retrieve the style to see whether this is an edit or richedit ctrl |
cd471848 | 338 | #if wxUSE_RICHEDIT |
aac7e7fe | 339 | wxString classname = wxGetWindowClass(GetHWND()); |
2bda0e17 | 340 | |
aac7e7fe VZ |
341 | if ( classname.IsSameAs(_T("EDIT"), FALSE /* no case */) ) |
342 | { | |
343 | m_verRichEdit = 0; | |
344 | } | |
345 | else // rich edit? | |
346 | { | |
347 | wxChar c; | |
348 | if ( wxSscanf(classname, _T("RichEdit%d0%c"), &m_verRichEdit, &c) != 2 ) | |
349 | { | |
350 | wxLogDebug(_T("Unknown edit control '%s'."), classname.c_str()); | |
2bda0e17 | 351 | |
aac7e7fe VZ |
352 | m_verRichEdit = 0; |
353 | } | |
354 | } | |
a1b82138 | 355 | #endif // wxUSE_RICHEDIT |
c085e333 | 356 | |
aac7e7fe VZ |
357 | if (style & ES_MULTILINE) |
358 | m_windowStyle |= wxTE_MULTILINE; | |
359 | if (style & ES_PASSWORD) | |
360 | m_windowStyle |= wxTE_PASSWORD; | |
361 | if (style & ES_READONLY) | |
362 | m_windowStyle |= wxTE_READONLY; | |
363 | if (style & ES_WANTRETURN) | |
364 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
2bda0e17 KB |
365 | } |
366 | ||
b2d5a7ee VZ |
367 | WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const |
368 | { | |
3a01bb1b VZ |
369 | // default border for the text controls is the sunken one |
370 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
371 | { | |
372 | style |= wxBORDER_SUNKEN; | |
373 | } | |
374 | ||
b2d5a7ee VZ |
375 | long msStyle = wxControl::MSWGetStyle(style, exstyle); |
376 | ||
377 | // default styles | |
5b2f31eb | 378 | msStyle |= ES_LEFT; |
b2d5a7ee VZ |
379 | |
380 | if ( style & wxTE_MULTILINE ) | |
381 | { | |
382 | wxASSERT_MSG( !(style & wxTE_PROCESS_ENTER), | |
383 | wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") ); | |
384 | ||
385 | msStyle |= ES_MULTILINE | ES_WANTRETURN; | |
386 | if ( !(style & wxTE_NO_VSCROLL) ) | |
387 | msStyle |= WS_VSCROLL; | |
388 | ||
389 | style |= wxTE_PROCESS_ENTER; | |
390 | } | |
391 | else // !multiline | |
392 | { | |
393 | // there is really no reason to not have this style for single line | |
394 | // text controls | |
395 | msStyle |= ES_AUTOHSCROLL; | |
396 | } | |
397 | ||
398 | if ( style & wxHSCROLL ) | |
399 | msStyle |= WS_HSCROLL | ES_AUTOHSCROLL; | |
400 | ||
401 | if ( style & wxTE_READONLY ) | |
402 | msStyle |= ES_READONLY; | |
403 | ||
404 | if ( style & wxTE_PASSWORD ) | |
405 | msStyle |= ES_PASSWORD; | |
406 | ||
407 | if ( style & wxTE_AUTO_SCROLL ) | |
408 | msStyle |= ES_AUTOHSCROLL; | |
409 | ||
410 | if ( style & wxTE_NOHIDESEL ) | |
411 | msStyle |= ES_NOHIDESEL; | |
412 | ||
413 | return msStyle; | |
414 | } | |
415 | ||
416 | void wxTextCtrl::SetWindowStyleFlag(long style) | |
417 | { | |
418 | #if wxUSE_RICHEDIT | |
419 | // we have to deal with some styles separately because they can't be | |
420 | // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed | |
421 | // using richedit-specific EM_SETOPTIONS | |
422 | if ( IsRich() && | |
423 | ((style & wxTE_NOHIDESEL) != (GetWindowStyle() & wxTE_NOHIDESEL)) ) | |
424 | { | |
425 | bool set = (style & wxTE_NOHIDESEL) != 0; | |
426 | ||
427 | ::SendMessage(GetHwnd(), EM_SETOPTIONS, set ? ECOOP_OR : ECOOP_AND, | |
428 | set ? ECO_NOHIDESEL : ~ECO_NOHIDESEL); | |
429 | } | |
430 | #endif // wxUSE_RICHEDIT | |
431 | ||
432 | wxControl::SetWindowStyleFlag(style); | |
433 | } | |
434 | ||
a1b82138 VZ |
435 | // ---------------------------------------------------------------------------- |
436 | // set/get the controls text | |
437 | // ---------------------------------------------------------------------------- | |
438 | ||
cd471848 | 439 | wxString wxTextCtrl::GetValue() const |
2bda0e17 | 440 | { |
a5aa8086 VZ |
441 | // range 0..-1 is special for GetRange() and means to retrieve all text |
442 | return GetRange(0, -1); | |
443 | } | |
444 | ||
445 | wxString wxTextCtrl::GetRange(long from, long to) const | |
446 | { | |
447 | wxString str; | |
448 | ||
449 | if ( from >= to && to != -1 ) | |
450 | { | |
451 | // nothing to retrieve | |
452 | return str; | |
453 | } | |
454 | ||
b12915c1 | 455 | #if wxUSE_RICHEDIT |
aac7e7fe | 456 | if ( IsRich() ) |
b12915c1 | 457 | { |
3988b155 | 458 | int len = GetWindowTextLength(GetHwnd()); |
a5aa8086 | 459 | if ( len > from ) |
3988b155 VZ |
460 | { |
461 | // alloc one extra WORD as needed by the control | |
462 | wxChar *p = str.GetWriteBuf(++len); | |
b12915c1 | 463 | |
3988b155 | 464 | TEXTRANGE textRange; |
a5aa8086 VZ |
465 | textRange.chrg.cpMin = from; |
466 | textRange.chrg.cpMax = to == -1 ? len : to; | |
3988b155 | 467 | textRange.lpstrText = p; |
b12915c1 | 468 | |
3988b155 | 469 | (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange); |
b12915c1 | 470 | |
a5aa8086 | 471 | if ( m_verRichEdit > 1 ) |
3988b155 | 472 | { |
a5aa8086 VZ |
473 | // RichEdit 2.0 uses just CR ('\r') for the newlines which is |
474 | // neither Unix nor Windows style - convert it to something | |
475 | // reasonable | |
476 | for ( ; *p; p++ ) | |
477 | { | |
478 | if ( *p == _T('\r') ) | |
479 | *p = _T('\n'); | |
480 | } | |
3988b155 VZ |
481 | } |
482 | ||
483 | str.UngetWriteBuf(); | |
a5aa8086 VZ |
484 | |
485 | if ( m_verRichEdit == 1 ) | |
486 | { | |
487 | // convert to the canonical form - see comment below | |
488 | str = wxTextFile::Translate(str, wxTextFileType_Unix); | |
489 | } | |
3988b155 VZ |
490 | } |
491 | //else: no text at all, leave the string empty | |
b12915c1 | 492 | } |
a5aa8086 | 493 | else |
b12915c1 | 494 | #endif // wxUSE_RICHEDIT |
a5aa8086 VZ |
495 | { |
496 | // retrieve all text | |
497 | str = wxGetWindowText(GetHWND()); | |
b12915c1 | 498 | |
a5aa8086 VZ |
499 | // need only a range? |
500 | if ( from < to ) | |
501 | { | |
502 | str = str.Mid(from, to - from); | |
503 | } | |
504 | ||
505 | // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the | |
506 | // canonical one (same one as above) for consistency with the other kinds | |
507 | // of controls and, more importantly, with the other ports | |
508 | str = wxTextFile::Translate(str, wxTextFileType_Unix); | |
509 | } | |
b12915c1 | 510 | |
a5aa8086 | 511 | return str; |
2bda0e17 KB |
512 | } |
513 | ||
514 | void wxTextCtrl::SetValue(const wxString& value) | |
515 | { | |
b12915c1 VZ |
516 | // if the text is long enough, it's faster to just set it instead of first |
517 | // comparing it with the old one (chances are that it will be different | |
518 | // anyhow, this comparison is there to avoid flicker for small single-line | |
519 | // edit controls mostly) | |
520 | if ( (value.length() > 0x400) || (value != GetValue()) ) | |
07cf98cb | 521 | { |
79d26b32 | 522 | DoWriteText(value, FALSE /* not selection only */); |
af01f1ba | 523 | |
104d7404 VZ |
524 | // mark the control as being not dirty - we changed its text, not the |
525 | // user | |
526 | DiscardEdits(); | |
527 | ||
af01f1ba VZ |
528 | // for compatibility, don't move the cursor when doing SetValue() |
529 | SetInsertionPoint(0); | |
aac7e7fe VZ |
530 | } |
531 | } | |
a1b82138 | 532 | |
0b8e5844 | 533 | #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU) |
f6bcfd97 | 534 | |
aac7e7fe VZ |
535 | DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb) |
536 | { | |
537 | *pcb = 0; | |
f6bcfd97 | 538 | |
aac7e7fe VZ |
539 | wchar_t *wbuf = (wchar_t *)buf; |
540 | const wchar_t *wpc = *(const wchar_t **)dwCookie; | |
541 | while ( cb && *wpc ) | |
542 | { | |
543 | *wbuf++ = *wpc++; | |
544 | ||
545 | cb -= sizeof(wchar_t); | |
546 | (*pcb) += sizeof(wchar_t); | |
07cf98cb | 547 | } |
aac7e7fe VZ |
548 | |
549 | *(const wchar_t **)dwCookie = wpc; | |
550 | ||
551 | return 0; | |
2bda0e17 KB |
552 | } |
553 | ||
373658eb | 554 | extern long wxEncodingToCodepage(wxFontEncoding encoding); // from utils.cpp |
aac7e7fe | 555 | |
0b8e5844 | 556 | #if wxUSE_UNICODE_MSLU |
79d26b32 VZ |
557 | bool wxTextCtrl::StreamIn(const wxString& value, |
558 | wxFontEncoding WXUNUSED(encoding), | |
559 | bool selectionOnly) | |
0b8e5844 VS |
560 | { |
561 | const wchar_t *wpc = value.c_str(); | |
79d26b32 VZ |
562 | #else // !wxUSE_UNICODE_MSLU |
563 | bool wxTextCtrl::StreamIn(const wxString& value, | |
564 | wxFontEncoding encoding, | |
565 | bool selectionOnly) | |
aac7e7fe VZ |
566 | { |
567 | // we have to use EM_STREAMIN to force richedit control 2.0+ to show any | |
568 | // text in the non default charset - otherwise it thinks it knows better | |
569 | // than we do and always shows it in the default one | |
570 | ||
571 | // first get the Windows code page for this encoding | |
572 | long codepage = wxEncodingToCodepage(encoding); | |
573 | if ( codepage == -1 ) | |
574 | { | |
575 | // unknown encoding | |
576 | return FALSE; | |
577 | } | |
578 | ||
579 | // next translate to Unicode using this code page | |
580 | int len = ::MultiByteToWideChar(codepage, 0, value, -1, NULL, 0); | |
855d6be7 VZ |
581 | |
582 | #if wxUSE_WCHAR_T | |
aac7e7fe | 583 | wxWCharBuffer wchBuf(len); |
855d6be7 VZ |
584 | #else |
585 | wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t)); | |
586 | #endif | |
587 | ||
aac7e7fe | 588 | if ( !::MultiByteToWideChar(codepage, 0, value, -1, |
855d6be7 | 589 | (wchar_t *)(const wchar_t *)wchBuf, len) ) |
aac7e7fe VZ |
590 | { |
591 | wxLogLastError(_T("MultiByteToWideChar")); | |
592 | } | |
593 | ||
594 | // finally, stream it in the control | |
595 | const wchar_t *wpc = wchBuf; | |
0b8e5844 | 596 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe VZ |
597 | |
598 | EDITSTREAM eds; | |
599 | wxZeroMemory(eds); | |
600 | eds.dwCookie = (DWORD)&wpc; | |
7a25a27c VZ |
601 | // the cast below is needed for broken (very) old mingw32 headers |
602 | eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn; | |
92209a39 | 603 | |
aac7e7fe | 604 | if ( !::SendMessage(GetHwnd(), EM_STREAMIN, |
79d26b32 VZ |
605 | SF_TEXT | |
606 | SF_UNICODE | | |
607 | (selectionOnly ? SFF_SELECTION : 0), | |
aac7e7fe VZ |
608 | (LPARAM)&eds) || eds.dwError ) |
609 | { | |
610 | wxLogLastError(_T("EM_STREAMIN")); | |
aac7e7fe VZ |
611 | } |
612 | ||
855d6be7 VZ |
613 | #if !wxUSE_WCHAR_T |
614 | free(wchBuf); | |
615 | #endif // !wxUSE_WCHAR_T | |
616 | ||
aac7e7fe VZ |
617 | return TRUE; |
618 | } | |
619 | ||
620 | #endif // wxUSE_RICHEDIT | |
621 | ||
a1b82138 | 622 | void wxTextCtrl::WriteText(const wxString& value) |
79d26b32 VZ |
623 | { |
624 | DoWriteText(value); | |
625 | } | |
626 | ||
627 | void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly) | |
2bda0e17 | 628 | { |
a5aa8086 VZ |
629 | wxString valueDos; |
630 | if ( m_windowStyle & wxTE_MULTILINE ) | |
631 | valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); | |
632 | else | |
633 | valueDos = value; | |
2bda0e17 | 634 | |
4bc1afd5 | 635 | #if wxUSE_RICHEDIT |
aac7e7fe VZ |
636 | // there are several complications with the rich edit controls here |
637 | bool done = FALSE; | |
638 | if ( IsRich() ) | |
4bc1afd5 | 639 | { |
aac7e7fe VZ |
640 | // first, ensure that the new text will be in the default style |
641 | if ( !m_defaultStyle.IsDefault() ) | |
642 | { | |
643 | long start, end; | |
644 | GetSelection(&start, &end); | |
0b8e5844 VS |
645 | SetStyle(start, end, m_defaultStyle); |
646 | } | |
647 | ||
648 | #if wxUSE_UNICODE_MSLU | |
649 | // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x, | |
650 | // but EM_STREAMIN works | |
136cb3c7 | 651 | if ( wxUsingUnicowsDll() && GetRichVersion() > 1 ) |
0b8e5844 | 652 | { |
79d26b32 | 653 | done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly); |
aac7e7fe | 654 | } |
0b8e5844 | 655 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe | 656 | |
9d7de3c2 | 657 | #if !wxUSE_UNICODE |
aac7e7fe VZ |
658 | // next check if the text we're inserting must be shown in a non |
659 | // default charset -- this only works for RichEdit > 1.0 | |
660 | if ( GetRichVersion() > 1 ) | |
661 | { | |
662 | wxFont font = m_defaultStyle.GetFont(); | |
663 | if ( !font.Ok() ) | |
664 | font = GetFont(); | |
665 | ||
666 | if ( font.Ok() ) | |
667 | { | |
668 | wxFontEncoding encoding = font.GetEncoding(); | |
669 | if ( encoding != wxFONTENCODING_SYSTEM ) | |
670 | { | |
79d26b32 | 671 | done = StreamIn(valueDos, encoding, selectionOnly); |
aac7e7fe VZ |
672 | } |
673 | } | |
674 | } | |
9d7de3c2 | 675 | #endif // !wxUSE_UNICODE |
4bc1afd5 | 676 | } |
4bc1afd5 | 677 | |
aac7e7fe VZ |
678 | if ( !done ) |
679 | #endif // wxUSE_RICHEDIT | |
680 | { | |
5036ea90 VZ |
681 | #if wxUSE_RICHEDIT |
682 | // rich edit text control sends us 2 EN_CHANGE events when we send | |
683 | // WM_SETTEXT to it, we have to suppress one of them to make wxTextCtrl | |
684 | // behaviour consistent | |
685 | if ( IsRich() ) | |
79d26b32 | 686 | { |
5036ea90 | 687 | m_suppressNextUpdate = TRUE; |
79d26b32 | 688 | } |
5036ea90 | 689 | #endif // wxUSE_RICHEDIT |
79d26b32 | 690 | |
5036ea90 VZ |
691 | ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT, |
692 | 0, (LPARAM)valueDos.c_str()); | |
aac7e7fe | 693 | } |
2bda0e17 | 694 | |
a1b82138 | 695 | AdjustSpaceLimit(); |
2bda0e17 KB |
696 | } |
697 | ||
a1b82138 VZ |
698 | void wxTextCtrl::AppendText(const wxString& text) |
699 | { | |
700 | SetInsertionPointEnd(); | |
aac7e7fe | 701 | |
a1b82138 VZ |
702 | WriteText(text); |
703 | } | |
704 | ||
705 | void wxTextCtrl::Clear() | |
706 | { | |
aac7e7fe | 707 | ::SetWindowText(GetHwnd(), wxT("")); |
5036ea90 VZ |
708 | |
709 | #if wxUSE_RICHEDIT | |
710 | if ( !IsRich() ) | |
711 | #endif // wxUSE_RICHEDIT | |
712 | { | |
713 | // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves | |
714 | // but the normal ones don't -- make Clear() behaviour consistent by | |
715 | // always sending this event | |
716 | SendUpdateEvent(); | |
717 | } | |
a1b82138 VZ |
718 | } |
719 | ||
94af7d45 VZ |
720 | #ifdef __WIN32__ |
721 | ||
722 | bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event) | |
723 | { | |
724 | SetFocus(); | |
725 | ||
726 | size_t lenOld = GetValue().length(); | |
727 | ||
728 | wxUint32 code = event.GetRawKeyCode(); | |
566d84a7 RL |
729 | ::keybd_event(code, 0, 0 /* key press */, 0); |
730 | ::keybd_event(code, 0, KEYEVENTF_KEYUP, 0); | |
94af7d45 VZ |
731 | |
732 | // assume that any alphanumeric key changes the total number of characters | |
733 | // in the control - this should work in 99% of cases | |
734 | return GetValue().length() != lenOld; | |
735 | } | |
736 | ||
737 | #endif // __WIN32__ | |
738 | ||
a1b82138 | 739 | // ---------------------------------------------------------------------------- |
2bda0e17 | 740 | // Clipboard operations |
a1b82138 VZ |
741 | // ---------------------------------------------------------------------------- |
742 | ||
cd471848 | 743 | void wxTextCtrl::Copy() |
2bda0e17 | 744 | { |
e702ff0f JS |
745 | if (CanCopy()) |
746 | { | |
a5aa8086 | 747 | ::SendMessage(GetHwnd(), WM_COPY, 0, 0L); |
e702ff0f | 748 | } |
2bda0e17 KB |
749 | } |
750 | ||
cd471848 | 751 | void wxTextCtrl::Cut() |
2bda0e17 | 752 | { |
e702ff0f JS |
753 | if (CanCut()) |
754 | { | |
a5aa8086 | 755 | ::SendMessage(GetHwnd(), WM_CUT, 0, 0L); |
e702ff0f | 756 | } |
2bda0e17 KB |
757 | } |
758 | ||
cd471848 | 759 | void wxTextCtrl::Paste() |
2bda0e17 | 760 | { |
e702ff0f JS |
761 | if (CanPaste()) |
762 | { | |
a5aa8086 | 763 | ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L); |
e702ff0f | 764 | } |
2bda0e17 KB |
765 | } |
766 | ||
a1b82138 VZ |
767 | bool wxTextCtrl::CanCopy() const |
768 | { | |
769 | // Can copy if there's a selection | |
770 | long from, to; | |
a5aa8086 VZ |
771 | GetSelection(&from, &to); |
772 | return from != to; | |
a1b82138 VZ |
773 | } |
774 | ||
775 | bool wxTextCtrl::CanCut() const | |
776 | { | |
a5aa8086 | 777 | return CanCopy() && IsEditable(); |
a1b82138 VZ |
778 | } |
779 | ||
780 | bool wxTextCtrl::CanPaste() const | |
781 | { | |
aac7e7fe VZ |
782 | if ( !IsEditable() ) |
783 | return FALSE; | |
784 | ||
a1b82138 | 785 | #if wxUSE_RICHEDIT |
aac7e7fe | 786 | if ( IsRich() ) |
a1b82138 | 787 | { |
aac7e7fe VZ |
788 | UINT cf = 0; // 0 == any format |
789 | ||
790 | return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0; | |
a1b82138 | 791 | } |
aac7e7fe | 792 | #endif // wxUSE_RICHEDIT |
a1b82138 VZ |
793 | |
794 | // Standard edit control: check for straight text on clipboard | |
aac7e7fe VZ |
795 | if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) |
796 | return FALSE; | |
797 | ||
798 | bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0; | |
799 | ::CloseClipboard(); | |
a1b82138 VZ |
800 | |
801 | return isTextAvailable; | |
802 | } | |
803 | ||
804 | // ---------------------------------------------------------------------------- | |
805 | // Accessors | |
806 | // ---------------------------------------------------------------------------- | |
807 | ||
debe6624 | 808 | void wxTextCtrl::SetEditable(bool editable) |
2bda0e17 | 809 | { |
a1b82138 VZ |
810 | HWND hWnd = GetHwnd(); |
811 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
2bda0e17 KB |
812 | } |
813 | ||
debe6624 | 814 | void wxTextCtrl::SetInsertionPoint(long pos) |
2bda0e17 | 815 | { |
a5aa8086 | 816 | DoSetSelection(pos, pos); |
2bda0e17 KB |
817 | } |
818 | ||
cd471848 | 819 | void wxTextCtrl::SetInsertionPointEnd() |
2bda0e17 | 820 | { |
a5aa8086 VZ |
821 | long pos; |
822 | ||
823 | #if wxUSE_RICHEDIT | |
824 | if ( m_verRichEdit == 1 ) | |
825 | { | |
826 | // we don't have to waste time calling GetLastPosition() in this case | |
827 | pos = -1; | |
828 | } | |
829 | else // !RichEdit 1.0 | |
830 | #endif // wxUSE_RICHEDIT | |
831 | { | |
832 | pos = GetLastPosition(); | |
833 | } | |
834 | ||
a1b82138 | 835 | SetInsertionPoint(pos); |
2bda0e17 KB |
836 | } |
837 | ||
cd471848 | 838 | long wxTextCtrl::GetInsertionPoint() const |
2bda0e17 | 839 | { |
57c208c5 | 840 | #if wxUSE_RICHEDIT |
aac7e7fe | 841 | if ( IsRich() ) |
a1b82138 VZ |
842 | { |
843 | CHARRANGE range; | |
844 | range.cpMin = 0; | |
845 | range.cpMax = 0; | |
846 | SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range); | |
847 | return range.cpMin; | |
848 | } | |
aac7e7fe | 849 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 850 | |
a1b82138 VZ |
851 | DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); |
852 | return Pos & 0xFFFF; | |
2bda0e17 KB |
853 | } |
854 | ||
cd471848 | 855 | long wxTextCtrl::GetLastPosition() const |
2bda0e17 | 856 | { |
a5aa8086 VZ |
857 | int numLines = GetNumberOfLines(); |
858 | long posStartLastLine = XYToPosition(0, numLines - 1); | |
39136494 | 859 | |
a5aa8086 | 860 | long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine); |
2bda0e17 | 861 | |
a5aa8086 | 862 | return posStartLastLine + lenLastLine; |
2bda0e17 KB |
863 | } |
864 | ||
a1b82138 VZ |
865 | // If the return values from and to are the same, there is no |
866 | // selection. | |
867 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
868 | { | |
869 | #if wxUSE_RICHEDIT | |
aac7e7fe | 870 | if ( IsRich() ) |
a1b82138 VZ |
871 | { |
872 | CHARRANGE charRange; | |
aac7e7fe | 873 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange); |
a1b82138 VZ |
874 | |
875 | *from = charRange.cpMin; | |
876 | *to = charRange.cpMax; | |
a1b82138 | 877 | } |
4bc1afd5 | 878 | else |
aac7e7fe | 879 | #endif // !wxUSE_RICHEDIT |
4bc1afd5 VZ |
880 | { |
881 | DWORD dwStart, dwEnd; | |
aac7e7fe | 882 | ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); |
a1b82138 | 883 | |
4bc1afd5 VZ |
884 | *from = dwStart; |
885 | *to = dwEnd; | |
886 | } | |
a1b82138 VZ |
887 | } |
888 | ||
889 | bool wxTextCtrl::IsEditable() const | |
890 | { | |
51c14c62 VZ |
891 | // strangely enough, we may be called before the control is created: our |
892 | // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls | |
893 | // us | |
894 | if ( !m_hWnd ) | |
895 | return TRUE; | |
896 | ||
a1b82138 VZ |
897 | long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); |
898 | ||
a5aa8086 | 899 | return (style & ES_READONLY) == 0; |
a1b82138 VZ |
900 | } |
901 | ||
902 | // ---------------------------------------------------------------------------- | |
aac7e7fe | 903 | // selection |
a1b82138 VZ |
904 | // ---------------------------------------------------------------------------- |
905 | ||
aac7e7fe | 906 | void wxTextCtrl::SetSelection(long from, long to) |
2bda0e17 | 907 | { |
aac7e7fe VZ |
908 | // if from and to are both -1, it means (in wxWindows) that all text should |
909 | // be selected - translate into Windows convention | |
910 | if ( (from == -1) && (to == -1) ) | |
911 | { | |
912 | from = 0; | |
913 | to = -1; | |
914 | } | |
915 | ||
a5aa8086 VZ |
916 | DoSetSelection(from, to); |
917 | } | |
918 | ||
919 | void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) | |
920 | { | |
789295bf | 921 | HWND hWnd = GetHwnd(); |
39136494 | 922 | |
2bda0e17 | 923 | #ifdef __WIN32__ |
aac7e7fe VZ |
924 | #if wxUSE_RICHEDIT |
925 | if ( IsRich() ) | |
926 | { | |
927 | CHARRANGE range; | |
928 | range.cpMin = from; | |
929 | range.cpMax = to; | |
930 | SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); | |
931 | } | |
932 | else | |
933 | #endif // wxUSE_RICHEDIT | |
934 | { | |
935 | SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to); | |
936 | } | |
a1b82138 | 937 | |
aac7e7fe | 938 | if ( scrollCaret ) |
2bda0e17 | 939 | { |
aac7e7fe | 940 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); |
2bda0e17 | 941 | } |
aac7e7fe VZ |
942 | #else // Win16 |
943 | // WPARAM is 0: selection is scrolled into view | |
944 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(from, to)); | |
945 | #endif // Win32/16 | |
946 | } | |
947 | ||
948 | // ---------------------------------------------------------------------------- | |
949 | // Editing | |
950 | // ---------------------------------------------------------------------------- | |
39136494 | 951 | |
aac7e7fe VZ |
952 | void wxTextCtrl::Replace(long from, long to, const wxString& value) |
953 | { | |
954 | // Set selection and remove it | |
f882d57e | 955 | DoSetSelection(from, to, FALSE /* don't scroll caret into view */); |
aac7e7fe VZ |
956 | |
957 | SendMessage(GetHwnd(), EM_REPLACESEL, | |
2bda0e17 | 958 | #ifdef __WIN32__ |
aac7e7fe | 959 | TRUE, |
2bda0e17 | 960 | #else |
aac7e7fe | 961 | FALSE, |
2bda0e17 | 962 | #endif |
aac7e7fe VZ |
963 | (LPARAM)value.c_str()); |
964 | } | |
965 | ||
966 | void wxTextCtrl::Remove(long from, long to) | |
967 | { | |
968 | Replace(from, to, _T("")); | |
2bda0e17 KB |
969 | } |
970 | ||
971 | bool wxTextCtrl::LoadFile(const wxString& file) | |
972 | { | |
a1b82138 | 973 | if ( wxTextCtrlBase::LoadFile(file) ) |
cd471848 | 974 | { |
a1b82138 VZ |
975 | // update the size limit if needed |
976 | AdjustSpaceLimit(); | |
2bda0e17 | 977 | |
a1b82138 | 978 | return TRUE; |
2bda0e17 | 979 | } |
2bda0e17 | 980 | |
a1b82138 | 981 | return FALSE; |
2bda0e17 KB |
982 | } |
983 | ||
cd471848 | 984 | bool wxTextCtrl::IsModified() const |
2bda0e17 | 985 | { |
a5aa8086 | 986 | return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; |
2bda0e17 KB |
987 | } |
988 | ||
989 | // Makes 'unmodified' | |
cd471848 | 990 | void wxTextCtrl::DiscardEdits() |
2bda0e17 | 991 | { |
a1b82138 | 992 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); |
2bda0e17 KB |
993 | } |
994 | ||
cd471848 | 995 | int wxTextCtrl::GetNumberOfLines() const |
2bda0e17 | 996 | { |
789295bf | 997 | return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); |
2bda0e17 KB |
998 | } |
999 | ||
debe6624 | 1000 | long wxTextCtrl::XYToPosition(long x, long y) const |
2bda0e17 | 1001 | { |
2bda0e17 | 1002 | // This gets the char index for the _beginning_ of this line |
a5aa8086 VZ |
1003 | long charIndex = SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0); |
1004 | ||
1005 | return charIndex + x; | |
2bda0e17 KB |
1006 | } |
1007 | ||
0efe5ba7 | 1008 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
2bda0e17 | 1009 | { |
789295bf | 1010 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
1011 | |
1012 | // This gets the line number containing the character | |
a5aa8086 | 1013 | long lineNo; |
0efe5ba7 | 1014 | #if wxUSE_RICHEDIT |
aac7e7fe | 1015 | if ( IsRich() ) |
0efe5ba7 | 1016 | { |
a5aa8086 | 1017 | lineNo = SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos); |
0efe5ba7 VZ |
1018 | } |
1019 | else | |
1020 | #endif // wxUSE_RICHEDIT | |
a5aa8086 VZ |
1021 | { |
1022 | lineNo = SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0); | |
1023 | } | |
0efe5ba7 VZ |
1024 | |
1025 | if ( lineNo == -1 ) | |
1026 | { | |
1027 | // no such line | |
1028 | return FALSE; | |
1029 | } | |
1030 | ||
2bda0e17 | 1031 | // This gets the char index for the _beginning_ of this line |
a5aa8086 | 1032 | long charIndex = SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); |
0efe5ba7 VZ |
1033 | if ( charIndex == -1 ) |
1034 | { | |
1035 | return FALSE; | |
1036 | } | |
1037 | ||
2bda0e17 | 1038 | // The X position must therefore be the different between pos and charIndex |
0efe5ba7 | 1039 | if ( x ) |
a5aa8086 | 1040 | *x = pos - charIndex; |
0efe5ba7 | 1041 | if ( y ) |
a5aa8086 | 1042 | *y = lineNo; |
0efe5ba7 VZ |
1043 | |
1044 | return TRUE; | |
2bda0e17 KB |
1045 | } |
1046 | ||
debe6624 | 1047 | void wxTextCtrl::ShowPosition(long pos) |
2bda0e17 | 1048 | { |
789295bf | 1049 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
1050 | |
1051 | // To scroll to a position, we pass the number of lines and characters | |
1052 | // to scroll *by*. This means that we need to: | |
1053 | // (1) Find the line position of the current line. | |
1054 | // (2) Find the line position of pos. | |
1055 | // (3) Scroll by (pos - current). | |
1056 | // For now, ignore the horizontal scrolling. | |
1057 | ||
1058 | // Is this where scrolling is relative to - the line containing the caret? | |
1059 | // Or is the first visible line??? Try first visible line. | |
1060 | // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L); | |
1061 | ||
1062 | int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L); | |
1063 | ||
1064 | int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L); | |
39136494 | 1065 | |
2bda0e17 KB |
1066 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; |
1067 | ||
2bda0e17 | 1068 | if (linesToScroll != 0) |
de4d7713 | 1069 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); |
2bda0e17 KB |
1070 | } |
1071 | ||
a5aa8086 VZ |
1072 | long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const |
1073 | { | |
1074 | return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0); | |
1075 | } | |
1076 | ||
debe6624 | 1077 | int wxTextCtrl::GetLineLength(long lineNo) const |
2bda0e17 | 1078 | { |
a5aa8086 VZ |
1079 | long pos = XYToPosition(0, lineNo); |
1080 | ||
1081 | return GetLengthOfLineContainingPos(pos); | |
2bda0e17 KB |
1082 | } |
1083 | ||
debe6624 | 1084 | wxString wxTextCtrl::GetLineText(long lineNo) const |
2bda0e17 | 1085 | { |
a1b82138 | 1086 | size_t len = (size_t)GetLineLength(lineNo) + 1; |
488fe1fe | 1087 | |
f6bcfd97 BP |
1088 | // there must be at least enough place for the length WORD in the |
1089 | // buffer | |
1090 | len += sizeof(WORD); | |
4438caf4 | 1091 | |
f6bcfd97 BP |
1092 | wxString str; |
1093 | wxChar *buf = str.GetWriteBuf(len); | |
1094 | ||
33ac7e6f | 1095 | *(WORD *)buf = (WORD)len; |
f6bcfd97 BP |
1096 | len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf); |
1097 | buf[len] = 0; | |
4438caf4 | 1098 | |
f6bcfd97 | 1099 | str.UngetWriteBuf(len); |
4438caf4 VZ |
1100 | |
1101 | return str; | |
2bda0e17 KB |
1102 | } |
1103 | ||
d7eee191 VZ |
1104 | void wxTextCtrl::SetMaxLength(unsigned long len) |
1105 | { | |
1106 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0); | |
1107 | } | |
1108 | ||
a1b82138 | 1109 | // ---------------------------------------------------------------------------- |
ca8b28f2 | 1110 | // Undo/redo |
a1b82138 VZ |
1111 | // ---------------------------------------------------------------------------- |
1112 | ||
ca8b28f2 JS |
1113 | void wxTextCtrl::Undo() |
1114 | { | |
1115 | if (CanUndo()) | |
1116 | { | |
789295bf | 1117 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
1118 | } |
1119 | } | |
1120 | ||
1121 | void wxTextCtrl::Redo() | |
1122 | { | |
1123 | if (CanRedo()) | |
1124 | { | |
1125 | // Same as Undo, since Undo undoes the undo, i.e. a redo. | |
789295bf | 1126 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
1127 | } |
1128 | } | |
1129 | ||
1130 | bool wxTextCtrl::CanUndo() const | |
1131 | { | |
a5aa8086 | 1132 | return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; |
ca8b28f2 JS |
1133 | } |
1134 | ||
1135 | bool wxTextCtrl::CanRedo() const | |
1136 | { | |
a5aa8086 | 1137 | return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; |
ca8b28f2 JS |
1138 | } |
1139 | ||
a1b82138 VZ |
1140 | // ---------------------------------------------------------------------------- |
1141 | // implemenation details | |
1142 | // ---------------------------------------------------------------------------- | |
39136494 | 1143 | |
2bda0e17 KB |
1144 | void wxTextCtrl::Command(wxCommandEvent & event) |
1145 | { | |
a1b82138 VZ |
1146 | SetValue(event.GetString()); |
1147 | ProcessCommand (event); | |
2bda0e17 KB |
1148 | } |
1149 | ||
1150 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
1151 | { | |
a1b82138 VZ |
1152 | // By default, load the first file into the text window. |
1153 | if (event.GetNumberOfFiles() > 0) | |
1154 | { | |
1155 | LoadFile(event.GetFiles()[0]); | |
1156 | } | |
2bda0e17 KB |
1157 | } |
1158 | ||
a37d422a VZ |
1159 | // ---------------------------------------------------------------------------- |
1160 | // kbd input processing | |
1161 | // ---------------------------------------------------------------------------- | |
1162 | ||
1163 | bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* pMsg) | |
1164 | { | |
1165 | MSG *msg = (MSG *)pMsg; | |
1166 | ||
1167 | // check for our special keys here: if we don't do it and the parent frame | |
1168 | // uses them as accelerators, they wouldn't work at all, so we disable | |
1169 | // usual preprocessing for them | |
1170 | if ( msg->message == WM_KEYDOWN ) | |
1171 | { | |
574c939e | 1172 | WORD vkey = (WORD) msg->wParam; |
a37d422a VZ |
1173 | if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN ) |
1174 | { | |
1175 | if ( vkey == VK_BACK ) | |
1176 | return FALSE; | |
1177 | } | |
1178 | else // no Alt | |
1179 | { | |
1180 | if ( wxIsCtrlDown() ) | |
1181 | { | |
1182 | switch ( vkey ) | |
1183 | { | |
1184 | case 'C': | |
1185 | case 'V': | |
1186 | case 'X': | |
1187 | case VK_INSERT: | |
1188 | case VK_DELETE: | |
1189 | case VK_HOME: | |
1190 | case VK_END: | |
1191 | return FALSE; | |
1192 | } | |
1193 | } | |
1194 | else if ( wxIsShiftDown() ) | |
1195 | { | |
1196 | if ( vkey == VK_INSERT || vkey == VK_DELETE ) | |
1197 | return FALSE; | |
1198 | } | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | return wxControl::MSWShouldPreProcessMessage(pMsg); | |
1203 | } | |
1204 | ||
2bda0e17 KB |
1205 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
1206 | { | |
42e69d6b | 1207 | switch ( event.KeyCode() ) |
cd471848 | 1208 | { |
cd471848 | 1209 | case WXK_RETURN: |
39136494 | 1210 | if ( !(m_windowStyle & wxTE_MULTILINE) ) |
cd471848 VZ |
1211 | { |
1212 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
bfbd6dc1 | 1213 | InitCommandEvent(event); |
f6bcfd97 | 1214 | event.SetString(GetValue()); |
cd471848 VZ |
1215 | if ( GetEventHandler()->ProcessEvent(event) ) |
1216 | return; | |
1217 | } | |
5fb9fcfc VZ |
1218 | //else: multiline controls need Enter for themselves |
1219 | ||
1220 | break; | |
4d91c1d1 | 1221 | |
cd471848 | 1222 | case WXK_TAB: |
319fefa9 VZ |
1223 | // always produce navigation event - even if we process TAB |
1224 | // ourselves the fact that we got here means that the user code | |
1225 | // decided to skip processing of this TAB - probably to let it | |
1226 | // do its default job. | |
cd471848 | 1227 | { |
5fb9fcfc VZ |
1228 | wxNavigationKeyEvent eventNav; |
1229 | eventNav.SetDirection(!event.ShiftDown()); | |
8614c467 | 1230 | eventNav.SetWindowChange(event.ControlDown()); |
5fb9fcfc | 1231 | eventNav.SetEventObject(this); |
39136494 | 1232 | |
d9506e77 | 1233 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) |
cd471848 VZ |
1234 | return; |
1235 | } | |
341c92a8 | 1236 | break; |
cd471848 | 1237 | } |
39136494 | 1238 | |
8614c467 | 1239 | // no, we didn't process it |
42e69d6b | 1240 | event.Skip(); |
2bda0e17 KB |
1241 | } |
1242 | ||
0cf5b099 VZ |
1243 | long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
1244 | { | |
e7e91e03 VZ |
1245 | long lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam); |
1246 | ||
0cf5b099 VZ |
1247 | if ( nMsg == WM_GETDLGCODE ) |
1248 | { | |
1249 | // we always want the chars and the arrows | |
1250 | long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS; | |
1251 | ||
1252 | // we may have several different cases: | |
1253 | // 1. normal case: both TAB and ENTER are used for dialog navigation | |
2adfb497 VZ |
1254 | // 2. ctrl which wants TAB for itself: ENTER is used to pass to the |
1255 | // next control in the dialog | |
1256 | // 3. ctrl which wants ENTER for itself: TAB is used for dialog | |
1257 | // navigation | |
1258 | // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass | |
1259 | // to the next control | |
1260 | ||
1261 | // the multiline edit control should always get <Return> for itself | |
1262 | if ( HasFlag(wxTE_PROCESS_ENTER) || HasFlag(wxTE_MULTILINE) ) | |
0cf5b099 | 1263 | lDlgCode |= DLGC_WANTMESSAGE; |
2adfb497 VZ |
1264 | |
1265 | if ( HasFlag(wxTE_PROCESS_TAB) ) | |
0cf5b099 VZ |
1266 | lDlgCode |= DLGC_WANTTAB; |
1267 | ||
e7e91e03 | 1268 | lRc |= lDlgCode; |
0cf5b099 VZ |
1269 | } |
1270 | ||
e7e91e03 | 1271 | return lRc; |
129223d6 VZ |
1272 | } |
1273 | ||
1274 | // ---------------------------------------------------------------------------- | |
1275 | // text control event processing | |
1276 | // ---------------------------------------------------------------------------- | |
1277 | ||
5036ea90 VZ |
1278 | bool wxTextCtrl::SendUpdateEvent() |
1279 | { | |
1280 | // is event reporting suspended? | |
1281 | if ( m_suppressNextUpdate ) | |
1282 | { | |
1283 | // do process the next one | |
1284 | m_suppressNextUpdate = FALSE; | |
1285 | ||
1286 | return FALSE; | |
1287 | } | |
1288 | ||
1289 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
1290 | InitCommandEvent(event); | |
1291 | event.SetString(GetValue()); | |
1292 | ||
1293 | return ProcessCommand(event); | |
1294 | } | |
1295 | ||
debe6624 | 1296 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 1297 | { |
5036ea90 | 1298 | switch ( param ) |
789295bf VZ |
1299 | { |
1300 | case EN_SETFOCUS: | |
1301 | case EN_KILLFOCUS: | |
1302 | { | |
1303 | wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS | |
d7eee191 VZ |
1304 | : wxEVT_SET_FOCUS, |
1305 | m_windowId); | |
5036ea90 | 1306 | event.SetEventObject(this); |
789295bf VZ |
1307 | GetEventHandler()->ProcessEvent(event); |
1308 | } | |
1309 | break; | |
ae29de83 | 1310 | |
789295bf | 1311 | case EN_CHANGE: |
5036ea90 | 1312 | SendUpdateEvent(); |
789295bf | 1313 | break; |
2bda0e17 | 1314 | |
b12915c1 | 1315 | case EN_MAXTEXT: |
5036ea90 | 1316 | // the text size limit has been hit -- try to increase it |
d7eee191 VZ |
1317 | if ( !AdjustSpaceLimit() ) |
1318 | { | |
1319 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId); | |
1320 | InitCommandEvent(event); | |
1321 | event.SetString(GetValue()); | |
1322 | ProcessCommand(event); | |
1323 | } | |
789295bf VZ |
1324 | break; |
1325 | ||
5036ea90 | 1326 | // the other edit notification messages are not processed |
789295bf VZ |
1327 | default: |
1328 | return FALSE; | |
1329 | } | |
1330 | ||
1331 | // processed | |
1332 | return TRUE; | |
2bda0e17 KB |
1333 | } |
1334 | ||
33ac7e6f | 1335 | WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
1336 | #if wxUSE_CTL3D |
1337 | WXUINT message, | |
1338 | WXWPARAM wParam, | |
1339 | WXLPARAM lParam | |
1340 | #else | |
33ac7e6f KB |
1341 | WXUINT WXUNUSED(message), |
1342 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
1343 | WXLPARAM WXUNUSED(lParam) |
1344 | #endif | |
1345 | ) | |
f6bcfd97 BP |
1346 | { |
1347 | #if wxUSE_CTL3D | |
1348 | if ( m_useCtl3D ) | |
1349 | { | |
1350 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
1351 | return (WXHBRUSH) hbrush; | |
1352 | } | |
1353 | #endif // wxUSE_CTL3D | |
1354 | ||
1355 | HDC hdc = (HDC)pDC; | |
1356 | if (GetParent()->GetTransparentBackground()) | |
1357 | SetBkMode(hdc, TRANSPARENT); | |
1358 | else | |
1359 | SetBkMode(hdc, OPAQUE); | |
1360 | ||
1361 | wxColour colBack = GetBackgroundColour(); | |
1362 | ||
1363 | if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0) | |
a756f210 | 1364 | colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
f6bcfd97 BP |
1365 | |
1366 | ::SetBkColor(hdc, wxColourToRGB(colBack)); | |
1367 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
1368 | ||
1369 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
1370 | ||
1371 | return (WXHBRUSH)brush->GetResourceHandle(); | |
1372 | } | |
1373 | ||
1374 | // In WIN16, need to override normal erasing because | |
1375 | // Ctl3D doesn't use the wxWindows background colour. | |
1376 | #ifdef __WIN16__ | |
1377 | void wxTextCtrl::OnEraseBackground(wxEraseEvent& event) | |
1378 | { | |
1379 | wxColour col(m_backgroundColour); | |
1380 | ||
1381 | #if wxUSE_CTL3D | |
1382 | if (m_useCtl3D) | |
a756f210 | 1383 | col = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); |
f6bcfd97 BP |
1384 | #endif |
1385 | ||
1386 | RECT rect; | |
1387 | ::GetClientRect(GetHwnd(), &rect); | |
1388 | ||
a5aa8086 | 1389 | COLORREF ref = wxColourToRGB(col); |
f6bcfd97 BP |
1390 | HBRUSH hBrush = ::CreateSolidBrush(ref); |
1391 | if ( !hBrush ) | |
1392 | wxLogLastError(wxT("CreateSolidBrush")); | |
1393 | ||
1394 | HDC hdc = (HDC)event.GetDC()->GetHDC(); | |
1395 | ||
1396 | int mode = ::SetMapMode(hdc, MM_TEXT); | |
1397 | ||
1398 | ::FillRect(hdc, &rect, hBrush); | |
1399 | ::DeleteObject(hBrush); | |
1400 | ::SetMapMode(hdc, mode); | |
1401 | ||
1402 | } | |
aac7e7fe | 1403 | #endif // Win16 |
f6bcfd97 | 1404 | |
d7eee191 | 1405 | bool wxTextCtrl::AdjustSpaceLimit() |
789295bf | 1406 | { |
25889d3c | 1407 | #ifndef __WIN16__ |
d7eee191 VZ |
1408 | unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0); |
1409 | ||
1410 | // HACK: we try to automatically extend the limit for the amount of text | |
1411 | // to allow (interactively) entering more than 64Kb of text under | |
1412 | // Win9x but we shouldn't reset the text limit which was previously | |
1413 | // set explicitly with SetMaxLength() | |
1414 | // | |
1415 | // we could solve this by storing the limit we set in wxTextCtrl but | |
1416 | // to save space we prefer to simply test here the actual limit | |
1417 | // value: we consider that SetMaxLength() can only be called for | |
1418 | // values < 32Kb | |
1419 | if ( limit < 0x8000 ) | |
1420 | { | |
1421 | // we've got more text than limit set by SetMaxLength() | |
1422 | return FALSE; | |
1423 | } | |
1424 | ||
1425 | unsigned int len = ::GetWindowTextLength(GetHwnd()); | |
17d8ee1c | 1426 | if ( len >= limit ) |
789295bf VZ |
1427 | { |
1428 | limit = len + 0x8000; // 32Kb | |
1429 | ||
5ea105e0 | 1430 | #if wxUSE_RICHEDIT |
aac7e7fe | 1431 | if ( IsRich() ) |
b12915c1 VZ |
1432 | { |
1433 | // as a nice side effect, this also allows passing limit > 64Kb | |
1434 | ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit); | |
1435 | } | |
789295bf | 1436 | else |
b12915c1 VZ |
1437 | #endif // wxUSE_RICHEDIT |
1438 | { | |
1439 | if ( limit > 0xffff ) | |
1440 | { | |
1441 | // this will set it to a platform-dependent maximum (much more | |
1442 | // than 64Kb under NT) | |
1443 | limit = 0; | |
1444 | } | |
1445 | ||
789295bf | 1446 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0); |
b12915c1 | 1447 | } |
789295bf | 1448 | } |
b12915c1 | 1449 | #endif // !Win16 |
d7eee191 VZ |
1450 | |
1451 | // we changed the limit | |
1452 | return TRUE; | |
789295bf | 1453 | } |
2bda0e17 | 1454 | |
a1b82138 | 1455 | bool wxTextCtrl::AcceptsFocus() const |
2bda0e17 | 1456 | { |
a1b82138 VZ |
1457 | // we don't want focus if we can't be edited |
1458 | return IsEditable() && wxControl::AcceptsFocus(); | |
1459 | } | |
c085e333 | 1460 | |
f68586e5 | 1461 | wxSize wxTextCtrl::DoGetBestSize() const |
a1b82138 VZ |
1462 | { |
1463 | int cx, cy; | |
1464 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
1465 | ||
1466 | int wText = DEFAULT_ITEM_WIDTH; | |
1467 | ||
1468 | int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
1469 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1470 | { | |
3988b155 | 1471 | hText *= wxMax(GetNumberOfLines(), 5); |
a1b82138 VZ |
1472 | } |
1473 | //else: for single line control everything is ok | |
1474 | ||
1475 | return wxSize(wText, hText); | |
2bda0e17 | 1476 | } |
a1b82138 VZ |
1477 | |
1478 | // ---------------------------------------------------------------------------- | |
1479 | // standard handlers for standard edit menu events | |
1480 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1481 | |
bfbd6dc1 | 1482 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1483 | { |
1484 | Cut(); | |
1485 | } | |
1486 | ||
bfbd6dc1 | 1487 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1488 | { |
1489 | Copy(); | |
1490 | } | |
1491 | ||
bfbd6dc1 | 1492 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1493 | { |
1494 | Paste(); | |
1495 | } | |
1496 | ||
bfbd6dc1 | 1497 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1498 | { |
1499 | Undo(); | |
1500 | } | |
1501 | ||
bfbd6dc1 | 1502 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1503 | { |
1504 | Redo(); | |
1505 | } | |
1506 | ||
1507 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1508 | { | |
1509 | event.Enable( CanCut() ); | |
1510 | } | |
1511 | ||
1512 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1513 | { | |
1514 | event.Enable( CanCopy() ); | |
1515 | } | |
1516 | ||
1517 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1518 | { | |
1519 | event.Enable( CanPaste() ); | |
1520 | } | |
1521 | ||
1522 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1523 | { | |
1524 | event.Enable( CanUndo() ); | |
1525 | } | |
1526 | ||
1527 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1528 | { | |
1529 | event.Enable( CanRedo() ); | |
1530 | } | |
1531 | ||
4bc1afd5 VZ |
1532 | // the rest of the file only deals with the rich edit controls |
1533 | #if wxUSE_RICHEDIT | |
1534 | ||
c57e3339 VZ |
1535 | // ---------------------------------------------------------------------------- |
1536 | // EN_LINK processing | |
1537 | // ---------------------------------------------------------------------------- | |
1538 | ||
a64c3b74 | 1539 | bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
c57e3339 VZ |
1540 | { |
1541 | NMHDR *hdr = (NMHDR* )lParam; | |
1dae1d00 | 1542 | switch ( hdr->code ) |
c57e3339 | 1543 | { |
1dae1d00 VZ |
1544 | case EN_MSGFILTER: |
1545 | { | |
1546 | const MSGFILTER *msgf = (MSGFILTER *)lParam; | |
1547 | UINT msg = msgf->msg; | |
1548 | ||
1549 | // this is a bit crazy but richedit 1.0 sends us all mouse | |
1550 | // events _except_ WM_LBUTTONUP (don't ask me why) so we have | |
1551 | // generate the wxWin events for this message manually | |
1552 | // | |
1553 | // NB: in fact, this is still not totally correct as it does | |
1554 | // send us WM_LBUTTONUP if the selection was cleared by the | |
1555 | // last click -- so currently we get 2 events in this case, | |
1556 | // but as I don't see any obvious way to check for this I | |
1557 | // leave this code in place because it's still better than | |
1558 | // not getting left up events at all | |
1559 | if ( msg == WM_LBUTTONUP ) | |
c57e3339 | 1560 | { |
1dae1d00 VZ |
1561 | WXUINT flags = msgf->wParam; |
1562 | int x = GET_X_LPARAM(msgf->lParam), | |
1563 | y = GET_Y_LPARAM(msgf->lParam); | |
1564 | ||
1565 | HandleMouseEvent(msg, x, y, flags); | |
c57e3339 | 1566 | } |
1dae1d00 | 1567 | } |
c57e3339 | 1568 | |
1dae1d00 VZ |
1569 | // return TRUE to process the event (and FALSE to ignore it) |
1570 | return TRUE; | |
1571 | ||
1572 | case EN_LINK: | |
1573 | { | |
1574 | const ENLINK *enlink = (ENLINK *)hdr; | |
1575 | ||
1576 | switch ( enlink->msg ) | |
1577 | { | |
1578 | case WM_SETCURSOR: | |
1579 | // ok, so it is hardcoded - do we really nee to | |
1580 | // customize it? | |
1581 | ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND))); | |
1582 | *result = TRUE; | |
1583 | break; | |
1584 | ||
1585 | case WM_MOUSEMOVE: | |
1586 | case WM_LBUTTONDOWN: | |
1587 | case WM_LBUTTONUP: | |
1588 | case WM_LBUTTONDBLCLK: | |
1589 | case WM_RBUTTONDOWN: | |
1590 | case WM_RBUTTONUP: | |
1591 | case WM_RBUTTONDBLCLK: | |
1592 | // send a mouse event | |
1593 | { | |
1594 | static const wxEventType eventsMouse[] = | |
1595 | { | |
1596 | wxEVT_MOTION, | |
1597 | wxEVT_LEFT_DOWN, | |
1598 | wxEVT_LEFT_UP, | |
1599 | wxEVT_LEFT_DCLICK, | |
1600 | wxEVT_RIGHT_DOWN, | |
1601 | wxEVT_RIGHT_UP, | |
1602 | wxEVT_RIGHT_DCLICK, | |
1603 | }; | |
1604 | ||
1605 | // the event ids are consecutive | |
1606 | wxMouseEvent | |
1607 | evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]); | |
1608 | ||
1609 | InitMouseEvent(evtMouse, | |
1610 | GET_X_LPARAM(enlink->lParam), | |
1611 | GET_Y_LPARAM(enlink->lParam), | |
1612 | enlink->wParam); | |
1613 | ||
1614 | wxTextUrlEvent event(m_windowId, evtMouse, | |
1615 | enlink->chrg.cpMin, | |
1616 | enlink->chrg.cpMax); | |
1617 | ||
1618 | InitCommandEvent(event); | |
1619 | ||
1620 | *result = ProcessCommand(event); | |
1621 | } | |
1622 | break; | |
1623 | } | |
1624 | } | |
1625 | return TRUE; | |
c57e3339 VZ |
1626 | } |
1627 | ||
d86ab8e2 VZ |
1628 | // not processed, leave it to the base class |
1629 | return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result); | |
c57e3339 VZ |
1630 | } |
1631 | ||
f6bcfd97 BP |
1632 | // ---------------------------------------------------------------------------- |
1633 | // colour setting for the rich edit controls | |
1634 | // ---------------------------------------------------------------------------- | |
1635 | ||
f6bcfd97 BP |
1636 | bool wxTextCtrl::SetBackgroundColour(const wxColour& colour) |
1637 | { | |
1638 | if ( !wxTextCtrlBase::SetBackgroundColour(colour) ) | |
1639 | { | |
1640 | // colour didn't really change | |
1641 | return FALSE; | |
1642 | } | |
1643 | ||
1644 | if ( IsRich() ) | |
1645 | { | |
1646 | // rich edit doesn't use WM_CTLCOLOR, hence we need to send | |
1647 | // EM_SETBKGNDCOLOR additionally | |
1648 | ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour)); | |
1649 | } | |
1650 | ||
1651 | return TRUE; | |
1652 | } | |
1653 | ||
1654 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) | |
1655 | { | |
1656 | if ( !wxTextCtrlBase::SetForegroundColour(colour) ) | |
1657 | { | |
1658 | // colour didn't really change | |
1659 | return FALSE; | |
1660 | } | |
1661 | ||
1662 | if ( IsRich() ) | |
1663 | { | |
1664 | // change the colour of everything | |
1665 | CHARFORMAT cf; | |
1666 | wxZeroMemory(cf); | |
1667 | cf.cbSize = sizeof(cf); | |
1668 | cf.dwMask = CFM_COLOR; | |
1669 | cf.crTextColor = wxColourToRGB(colour); | |
1670 | ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf); | |
1671 | } | |
1672 | ||
1673 | return TRUE; | |
1674 | } | |
1675 | ||
4bc1afd5 VZ |
1676 | // ---------------------------------------------------------------------------- |
1677 | // styling support for rich edit controls | |
1678 | // ---------------------------------------------------------------------------- | |
1679 | ||
cc164686 RD |
1680 | #if wxUSE_RICHEDIT |
1681 | ||
4bc1afd5 VZ |
1682 | bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) |
1683 | { | |
1684 | if ( !IsRich() ) | |
1685 | { | |
1686 | // can't do it with normal text control | |
1687 | return FALSE; | |
1688 | } | |
1689 | ||
a5aa8086 VZ |
1690 | // the richedit 1.0 doesn't handle setting background colour, so don't |
1691 | // even try to do anything if it's the only thing we want to change | |
1692 | if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() ) | |
4bc1afd5 | 1693 | { |
be329a3d RD |
1694 | // nothing to do: return TRUE if there was really nothing to do and |
1695 | // FALSE if we failed to set bg colour | |
4bc1afd5 VZ |
1696 | return !style.HasBackgroundColour(); |
1697 | } | |
1698 | ||
1699 | // order the range if needed | |
1700 | if ( start > end ) | |
1701 | { | |
1702 | long tmp = start; | |
1703 | start = end; | |
1704 | end = tmp; | |
1705 | } | |
1706 | ||
1707 | // we can only change the format of the selection, so select the range we | |
1708 | // want and restore the old selection later | |
1709 | long startOld, endOld; | |
1710 | GetSelection(&startOld, &endOld); | |
1711 | ||
1712 | // but do we really have to change the selection? | |
1713 | bool changeSel = start != startOld || end != endOld; | |
1714 | ||
1715 | if ( changeSel ) | |
aac7e7fe | 1716 | { |
f882d57e | 1717 | DoSetSelection(start, end, FALSE /* don't scroll caret into view */); |
aac7e7fe | 1718 | } |
4bc1afd5 VZ |
1719 | |
1720 | // initialize CHARFORMAT struct | |
be329a3d RD |
1721 | #if wxUSE_RICHEDIT2 |
1722 | CHARFORMAT2 cf; | |
1723 | #else | |
4bc1afd5 | 1724 | CHARFORMAT cf; |
be329a3d | 1725 | #endif |
a5aa8086 | 1726 | |
4bc1afd5 | 1727 | wxZeroMemory(cf); |
a5aa8086 VZ |
1728 | |
1729 | // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple | |
1730 | // CHARFORMAT in that case | |
1731 | #if wxUSE_RICHEDIT2 | |
1732 | if ( m_verRichEdit == 1 ) | |
1733 | { | |
1734 | // this is the only thing the control is going to grok | |
1735 | cf.cbSize = sizeof(CHARFORMAT); | |
1736 | } | |
1737 | else | |
1738 | #endif | |
1739 | { | |
1740 | // CHARFORMAT or CHARFORMAT2 | |
1741 | cf.cbSize = sizeof(cf); | |
1742 | } | |
4bc1afd5 VZ |
1743 | |
1744 | if ( style.HasFont() ) | |
1745 | { | |
aac7e7fe VZ |
1746 | // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0 |
1747 | // but using it doesn't seem to hurt neither so leaving it for now | |
1748 | ||
784164e1 VZ |
1749 | cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET | |
1750 | CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE; | |
4bc1afd5 VZ |
1751 | |
1752 | // fill in data from LOGFONT but recalculate lfHeight because we need | |
1753 | // the real height in twips and not the negative number which | |
1754 | // wxFillLogFont() returns (this is correct in general and works with | |
1755 | // the Windows font mapper, but not here) | |
1756 | LOGFONT lf; | |
1757 | wxFillLogFont(&lf, &style.GetFont()); | |
1758 | cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips | |
1759 | cf.bCharSet = lf.lfCharSet; | |
1760 | cf.bPitchAndFamily = lf.lfPitchAndFamily; | |
1761 | wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) ); | |
1762 | ||
784164e1 VZ |
1763 | // also deal with underline/italic/bold attributes: note that we must |
1764 | // always set CFM_ITALIC &c bits in dwMask, even if we don't set the | |
1765 | // style to allow clearing it | |
4bc1afd5 VZ |
1766 | if ( lf.lfItalic ) |
1767 | { | |
4bc1afd5 VZ |
1768 | cf.dwEffects |= CFE_ITALIC; |
1769 | } | |
1770 | ||
1771 | if ( lf.lfWeight == FW_BOLD ) | |
1772 | { | |
4bc1afd5 VZ |
1773 | cf.dwEffects |= CFE_BOLD; |
1774 | } | |
1775 | ||
1776 | if ( lf.lfUnderline ) | |
1777 | { | |
4bc1afd5 VZ |
1778 | cf.dwEffects |= CFE_UNDERLINE; |
1779 | } | |
1780 | ||
1781 | // strikeout fonts are not supported by wxWindows | |
1782 | } | |
1783 | ||
1784 | if ( style.HasTextColour() ) | |
1785 | { | |
1786 | cf.dwMask |= CFM_COLOR; | |
1787 | cf.crTextColor = wxColourToRGB(style.GetTextColour()); | |
1788 | } | |
1789 | ||
be329a3d | 1790 | #if wxUSE_RICHEDIT2 |
a5aa8086 | 1791 | if ( m_verRichEdit != 1 && style.HasBackgroundColour() ) |
be329a3d RD |
1792 | { |
1793 | cf.dwMask |= CFM_BACKCOLOR; | |
1794 | cf.crBackColor = wxColourToRGB(style.GetBackgroundColour()); | |
1795 | } | |
784164e1 VZ |
1796 | #endif // wxUSE_RICHEDIT2 |
1797 | ||
4bc1afd5 VZ |
1798 | // do format the selection |
1799 | bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, | |
1800 | SCF_SELECTION, (LPARAM)&cf) != 0; | |
1801 | if ( !ok ) | |
1802 | { | |
1803 | wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed")); | |
1804 | } | |
1805 | ||
1806 | if ( changeSel ) | |
1807 | { | |
1808 | // restore the original selection | |
aac7e7fe | 1809 | DoSetSelection(startOld, endOld, FALSE); |
4bc1afd5 VZ |
1810 | } |
1811 | ||
1812 | return ok; | |
1813 | } | |
f6bcfd97 | 1814 | |
5bf75ae7 VZ |
1815 | bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style) |
1816 | { | |
1817 | if ( !wxTextCtrlBase::SetDefaultStyle(style) ) | |
1818 | return FALSE; | |
1819 | ||
1820 | // we have to do this or the style wouldn't apply for the text typed by the | |
1821 | // user | |
1822 | long posLast = GetLastPosition(); | |
1823 | SetStyle(posLast, posLast, m_defaultStyle); | |
1824 | ||
1825 | return TRUE; | |
1826 | } | |
1827 | ||
cc164686 RD |
1828 | #endif |
1829 | ||
b12915c1 VZ |
1830 | // ---------------------------------------------------------------------------- |
1831 | // wxRichEditModule | |
1832 | // ---------------------------------------------------------------------------- | |
1833 | ||
b12915c1 VZ |
1834 | bool wxRichEditModule::OnInit() |
1835 | { | |
1836 | // don't do anything - we will load it when needed | |
1837 | return TRUE; | |
1838 | } | |
1839 | ||
1840 | void wxRichEditModule::OnExit() | |
1841 | { | |
136cb3c7 | 1842 | for ( size_t i = 0; i < WXSIZEOF(ms_hRichEdit); i++ ) |
b12915c1 | 1843 | { |
a5aa8086 VZ |
1844 | if ( ms_hRichEdit[i] ) |
1845 | { | |
1846 | ::FreeLibrary(ms_hRichEdit[i]); | |
1847 | } | |
b12915c1 VZ |
1848 | } |
1849 | } | |
1850 | ||
1851 | /* static */ | |
1852 | bool wxRichEditModule::Load(int version) | |
1853 | { | |
a5aa8086 VZ |
1854 | // we don't support loading richedit 3.0 as I don't know how to distinguish |
1855 | // it from 2.0 anyhow | |
1856 | wxCHECK_MSG( version == 1 || version == 2, FALSE, | |
b12915c1 VZ |
1857 | _T("incorrect richedit control version requested") ); |
1858 | ||
a5aa8086 VZ |
1859 | // make it the index in the array |
1860 | version--; | |
1861 | ||
a5aa8086 | 1862 | if ( ms_hRichEdit[version] == (HINSTANCE)-1 ) |
b12915c1 | 1863 | { |
a5aa8086 VZ |
1864 | // we had already tried to load it and failed |
1865 | return FALSE; | |
b12915c1 VZ |
1866 | } |
1867 | ||
28978e0c VZ |
1868 | if ( ms_hRichEdit[version] ) |
1869 | { | |
1870 | // we've already got this one | |
1871 | return TRUE; | |
1872 | } | |
1873 | ||
a5aa8086 VZ |
1874 | wxString dllname = version ? _T("riched20") : _T("riched32"); |
1875 | dllname += _T(".dll"); | |
b12915c1 | 1876 | |
a5aa8086 | 1877 | ms_hRichEdit[version] = ::LoadLibrary(dllname); |
b12915c1 | 1878 | |
a5aa8086 | 1879 | if ( !ms_hRichEdit[version] ) |
b12915c1 VZ |
1880 | { |
1881 | wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str()); | |
1882 | ||
a5aa8086 | 1883 | ms_hRichEdit[version] = (HINSTANCE)-1; |
b12915c1 VZ |
1884 | |
1885 | return FALSE; | |
1886 | } | |
1887 | ||
1888 | return TRUE; | |
1889 | } | |
1890 | ||
1891 | #endif // wxUSE_RICHEDIT | |
1892 | ||
1e6feb95 | 1893 | #endif // wxUSE_TEXTCTRL |