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