]>
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 | |
3bce6687 JS |
556 | #ifdef __WXWINE__ |
557 | bool wxTextCtrl::StreamIn(const wxString& value, | |
558 | wxFontEncoding WXUNUSED(encoding), | |
559 | bool selectionOnly) | |
560 | { | |
561 | return FALSE; | |
562 | } | |
563 | #else | |
564 | ||
0b8e5844 | 565 | #if wxUSE_UNICODE_MSLU |
79d26b32 VZ |
566 | bool wxTextCtrl::StreamIn(const wxString& value, |
567 | wxFontEncoding WXUNUSED(encoding), | |
568 | bool selectionOnly) | |
0b8e5844 VS |
569 | { |
570 | const wchar_t *wpc = value.c_str(); | |
79d26b32 VZ |
571 | #else // !wxUSE_UNICODE_MSLU |
572 | bool wxTextCtrl::StreamIn(const wxString& value, | |
573 | wxFontEncoding encoding, | |
574 | bool selectionOnly) | |
aac7e7fe VZ |
575 | { |
576 | // we have to use EM_STREAMIN to force richedit control 2.0+ to show any | |
577 | // text in the non default charset - otherwise it thinks it knows better | |
578 | // than we do and always shows it in the default one | |
579 | ||
580 | // first get the Windows code page for this encoding | |
581 | long codepage = wxEncodingToCodepage(encoding); | |
582 | if ( codepage == -1 ) | |
583 | { | |
584 | // unknown encoding | |
585 | return FALSE; | |
586 | } | |
587 | ||
588 | // next translate to Unicode using this code page | |
589 | int len = ::MultiByteToWideChar(codepage, 0, value, -1, NULL, 0); | |
855d6be7 VZ |
590 | |
591 | #if wxUSE_WCHAR_T | |
aac7e7fe | 592 | wxWCharBuffer wchBuf(len); |
855d6be7 VZ |
593 | #else |
594 | wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t)); | |
595 | #endif | |
596 | ||
aac7e7fe | 597 | if ( !::MultiByteToWideChar(codepage, 0, value, -1, |
855d6be7 | 598 | (wchar_t *)(const wchar_t *)wchBuf, len) ) |
aac7e7fe VZ |
599 | { |
600 | wxLogLastError(_T("MultiByteToWideChar")); | |
601 | } | |
602 | ||
603 | // finally, stream it in the control | |
604 | const wchar_t *wpc = wchBuf; | |
0b8e5844 | 605 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe VZ |
606 | |
607 | EDITSTREAM eds; | |
608 | wxZeroMemory(eds); | |
609 | eds.dwCookie = (DWORD)&wpc; | |
7a25a27c VZ |
610 | // the cast below is needed for broken (very) old mingw32 headers |
611 | eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn; | |
92209a39 | 612 | |
aac7e7fe | 613 | if ( !::SendMessage(GetHwnd(), EM_STREAMIN, |
79d26b32 VZ |
614 | SF_TEXT | |
615 | SF_UNICODE | | |
616 | (selectionOnly ? SFF_SELECTION : 0), | |
aac7e7fe VZ |
617 | (LPARAM)&eds) || eds.dwError ) |
618 | { | |
619 | wxLogLastError(_T("EM_STREAMIN")); | |
aac7e7fe VZ |
620 | } |
621 | ||
855d6be7 VZ |
622 | #if !wxUSE_WCHAR_T |
623 | free(wchBuf); | |
624 | #endif // !wxUSE_WCHAR_T | |
625 | ||
aac7e7fe VZ |
626 | return TRUE; |
627 | } | |
628 | ||
3bce6687 JS |
629 | #endif |
630 | // __WXWINE__ | |
631 | ||
aac7e7fe VZ |
632 | #endif // wxUSE_RICHEDIT |
633 | ||
a1b82138 | 634 | void wxTextCtrl::WriteText(const wxString& value) |
79d26b32 VZ |
635 | { |
636 | DoWriteText(value); | |
637 | } | |
638 | ||
639 | void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly) | |
2bda0e17 | 640 | { |
a5aa8086 VZ |
641 | wxString valueDos; |
642 | if ( m_windowStyle & wxTE_MULTILINE ) | |
643 | valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); | |
644 | else | |
645 | valueDos = value; | |
2bda0e17 | 646 | |
4bc1afd5 | 647 | #if wxUSE_RICHEDIT |
aac7e7fe VZ |
648 | // there are several complications with the rich edit controls here |
649 | bool done = FALSE; | |
650 | if ( IsRich() ) | |
4bc1afd5 | 651 | { |
aac7e7fe VZ |
652 | // first, ensure that the new text will be in the default style |
653 | if ( !m_defaultStyle.IsDefault() ) | |
654 | { | |
655 | long start, end; | |
656 | GetSelection(&start, &end); | |
0b8e5844 VS |
657 | SetStyle(start, end, m_defaultStyle); |
658 | } | |
659 | ||
660 | #if wxUSE_UNICODE_MSLU | |
661 | // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x, | |
662 | // but EM_STREAMIN works | |
136cb3c7 | 663 | if ( wxUsingUnicowsDll() && GetRichVersion() > 1 ) |
0b8e5844 | 664 | { |
79d26b32 | 665 | done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly); |
aac7e7fe | 666 | } |
0b8e5844 | 667 | #endif // wxUSE_UNICODE_MSLU |
aac7e7fe | 668 | |
3bce6687 | 669 | #if !wxUSE_UNICODE && !defined(__WXWINE__) |
aac7e7fe VZ |
670 | // next check if the text we're inserting must be shown in a non |
671 | // default charset -- this only works for RichEdit > 1.0 | |
672 | if ( GetRichVersion() > 1 ) | |
673 | { | |
674 | wxFont font = m_defaultStyle.GetFont(); | |
675 | if ( !font.Ok() ) | |
676 | font = GetFont(); | |
677 | ||
678 | if ( font.Ok() ) | |
679 | { | |
680 | wxFontEncoding encoding = font.GetEncoding(); | |
681 | if ( encoding != wxFONTENCODING_SYSTEM ) | |
682 | { | |
79d26b32 | 683 | done = StreamIn(valueDos, encoding, selectionOnly); |
aac7e7fe VZ |
684 | } |
685 | } | |
686 | } | |
9d7de3c2 | 687 | #endif // !wxUSE_UNICODE |
4bc1afd5 | 688 | } |
4bc1afd5 | 689 | |
aac7e7fe VZ |
690 | if ( !done ) |
691 | #endif // wxUSE_RICHEDIT | |
692 | { | |
5036ea90 VZ |
693 | #if wxUSE_RICHEDIT |
694 | // rich edit text control sends us 2 EN_CHANGE events when we send | |
695 | // WM_SETTEXT to it, we have to suppress one of them to make wxTextCtrl | |
696 | // behaviour consistent | |
697 | if ( IsRich() ) | |
79d26b32 | 698 | { |
5036ea90 | 699 | m_suppressNextUpdate = TRUE; |
79d26b32 | 700 | } |
5036ea90 | 701 | #endif // wxUSE_RICHEDIT |
79d26b32 | 702 | |
5036ea90 VZ |
703 | ::SendMessage(GetHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT, |
704 | 0, (LPARAM)valueDos.c_str()); | |
aac7e7fe | 705 | } |
2bda0e17 | 706 | |
a1b82138 | 707 | AdjustSpaceLimit(); |
2bda0e17 KB |
708 | } |
709 | ||
a1b82138 VZ |
710 | void wxTextCtrl::AppendText(const wxString& text) |
711 | { | |
712 | SetInsertionPointEnd(); | |
aac7e7fe | 713 | |
a1b82138 VZ |
714 | WriteText(text); |
715 | } | |
716 | ||
717 | void wxTextCtrl::Clear() | |
718 | { | |
aac7e7fe | 719 | ::SetWindowText(GetHwnd(), wxT("")); |
5036ea90 VZ |
720 | |
721 | #if wxUSE_RICHEDIT | |
722 | if ( !IsRich() ) | |
723 | #endif // wxUSE_RICHEDIT | |
724 | { | |
725 | // rich edit controls send EN_UPDATE from WM_SETTEXT handler themselves | |
726 | // but the normal ones don't -- make Clear() behaviour consistent by | |
727 | // always sending this event | |
728 | SendUpdateEvent(); | |
729 | } | |
a1b82138 VZ |
730 | } |
731 | ||
94af7d45 VZ |
732 | #ifdef __WIN32__ |
733 | ||
734 | bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event) | |
735 | { | |
736 | SetFocus(); | |
737 | ||
738 | size_t lenOld = GetValue().length(); | |
739 | ||
740 | wxUint32 code = event.GetRawKeyCode(); | |
566d84a7 RL |
741 | ::keybd_event(code, 0, 0 /* key press */, 0); |
742 | ::keybd_event(code, 0, KEYEVENTF_KEYUP, 0); | |
94af7d45 VZ |
743 | |
744 | // assume that any alphanumeric key changes the total number of characters | |
745 | // in the control - this should work in 99% of cases | |
746 | return GetValue().length() != lenOld; | |
747 | } | |
748 | ||
749 | #endif // __WIN32__ | |
750 | ||
a1b82138 | 751 | // ---------------------------------------------------------------------------- |
2bda0e17 | 752 | // Clipboard operations |
a1b82138 VZ |
753 | // ---------------------------------------------------------------------------- |
754 | ||
cd471848 | 755 | void wxTextCtrl::Copy() |
2bda0e17 | 756 | { |
e702ff0f JS |
757 | if (CanCopy()) |
758 | { | |
a5aa8086 | 759 | ::SendMessage(GetHwnd(), WM_COPY, 0, 0L); |
e702ff0f | 760 | } |
2bda0e17 KB |
761 | } |
762 | ||
cd471848 | 763 | void wxTextCtrl::Cut() |
2bda0e17 | 764 | { |
e702ff0f JS |
765 | if (CanCut()) |
766 | { | |
a5aa8086 | 767 | ::SendMessage(GetHwnd(), WM_CUT, 0, 0L); |
e702ff0f | 768 | } |
2bda0e17 KB |
769 | } |
770 | ||
cd471848 | 771 | void wxTextCtrl::Paste() |
2bda0e17 | 772 | { |
e702ff0f JS |
773 | if (CanPaste()) |
774 | { | |
a5aa8086 | 775 | ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L); |
e702ff0f | 776 | } |
2bda0e17 KB |
777 | } |
778 | ||
a1b82138 VZ |
779 | bool wxTextCtrl::CanCopy() const |
780 | { | |
781 | // Can copy if there's a selection | |
782 | long from, to; | |
a5aa8086 VZ |
783 | GetSelection(&from, &to); |
784 | return from != to; | |
a1b82138 VZ |
785 | } |
786 | ||
787 | bool wxTextCtrl::CanCut() const | |
788 | { | |
a5aa8086 | 789 | return CanCopy() && IsEditable(); |
a1b82138 VZ |
790 | } |
791 | ||
792 | bool wxTextCtrl::CanPaste() const | |
793 | { | |
aac7e7fe VZ |
794 | if ( !IsEditable() ) |
795 | return FALSE; | |
796 | ||
a1b82138 | 797 | #if wxUSE_RICHEDIT |
aac7e7fe | 798 | if ( IsRich() ) |
a1b82138 | 799 | { |
aac7e7fe VZ |
800 | UINT cf = 0; // 0 == any format |
801 | ||
802 | return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0; | |
a1b82138 | 803 | } |
aac7e7fe | 804 | #endif // wxUSE_RICHEDIT |
a1b82138 VZ |
805 | |
806 | // Standard edit control: check for straight text on clipboard | |
aac7e7fe VZ |
807 | if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) |
808 | return FALSE; | |
809 | ||
810 | bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0; | |
811 | ::CloseClipboard(); | |
a1b82138 VZ |
812 | |
813 | return isTextAvailable; | |
814 | } | |
815 | ||
816 | // ---------------------------------------------------------------------------- | |
817 | // Accessors | |
818 | // ---------------------------------------------------------------------------- | |
819 | ||
debe6624 | 820 | void wxTextCtrl::SetEditable(bool editable) |
2bda0e17 | 821 | { |
a1b82138 VZ |
822 | HWND hWnd = GetHwnd(); |
823 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
2bda0e17 KB |
824 | } |
825 | ||
debe6624 | 826 | void wxTextCtrl::SetInsertionPoint(long pos) |
2bda0e17 | 827 | { |
a5aa8086 | 828 | DoSetSelection(pos, pos); |
2bda0e17 KB |
829 | } |
830 | ||
cd471848 | 831 | void wxTextCtrl::SetInsertionPointEnd() |
2bda0e17 | 832 | { |
a5aa8086 VZ |
833 | long pos; |
834 | ||
835 | #if wxUSE_RICHEDIT | |
836 | if ( m_verRichEdit == 1 ) | |
837 | { | |
838 | // we don't have to waste time calling GetLastPosition() in this case | |
839 | pos = -1; | |
840 | } | |
841 | else // !RichEdit 1.0 | |
842 | #endif // wxUSE_RICHEDIT | |
843 | { | |
844 | pos = GetLastPosition(); | |
845 | } | |
846 | ||
a1b82138 | 847 | SetInsertionPoint(pos); |
2bda0e17 KB |
848 | } |
849 | ||
cd471848 | 850 | long wxTextCtrl::GetInsertionPoint() const |
2bda0e17 | 851 | { |
57c208c5 | 852 | #if wxUSE_RICHEDIT |
aac7e7fe | 853 | if ( IsRich() ) |
a1b82138 VZ |
854 | { |
855 | CHARRANGE range; | |
856 | range.cpMin = 0; | |
857 | range.cpMax = 0; | |
858 | SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range); | |
859 | return range.cpMin; | |
860 | } | |
aac7e7fe | 861 | #endif // wxUSE_RICHEDIT |
2bda0e17 | 862 | |
a1b82138 VZ |
863 | DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); |
864 | return Pos & 0xFFFF; | |
2bda0e17 KB |
865 | } |
866 | ||
cd471848 | 867 | long wxTextCtrl::GetLastPosition() const |
2bda0e17 | 868 | { |
a5aa8086 VZ |
869 | int numLines = GetNumberOfLines(); |
870 | long posStartLastLine = XYToPosition(0, numLines - 1); | |
39136494 | 871 | |
a5aa8086 | 872 | long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine); |
2bda0e17 | 873 | |
a5aa8086 | 874 | return posStartLastLine + lenLastLine; |
2bda0e17 KB |
875 | } |
876 | ||
a1b82138 VZ |
877 | // If the return values from and to are the same, there is no |
878 | // selection. | |
879 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
880 | { | |
881 | #if wxUSE_RICHEDIT | |
aac7e7fe | 882 | if ( IsRich() ) |
a1b82138 VZ |
883 | { |
884 | CHARRANGE charRange; | |
aac7e7fe | 885 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange); |
a1b82138 VZ |
886 | |
887 | *from = charRange.cpMin; | |
888 | *to = charRange.cpMax; | |
a1b82138 | 889 | } |
4bc1afd5 | 890 | else |
aac7e7fe | 891 | #endif // !wxUSE_RICHEDIT |
4bc1afd5 VZ |
892 | { |
893 | DWORD dwStart, dwEnd; | |
aac7e7fe | 894 | ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); |
a1b82138 | 895 | |
4bc1afd5 VZ |
896 | *from = dwStart; |
897 | *to = dwEnd; | |
898 | } | |
a1b82138 VZ |
899 | } |
900 | ||
901 | bool wxTextCtrl::IsEditable() const | |
902 | { | |
51c14c62 VZ |
903 | // strangely enough, we may be called before the control is created: our |
904 | // own Create() calls MSWGetStyle() which calls AcceptsFocus() which calls | |
905 | // us | |
906 | if ( !m_hWnd ) | |
907 | return TRUE; | |
908 | ||
a1b82138 VZ |
909 | long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); |
910 | ||
a5aa8086 | 911 | return (style & ES_READONLY) == 0; |
a1b82138 VZ |
912 | } |
913 | ||
914 | // ---------------------------------------------------------------------------- | |
aac7e7fe | 915 | // selection |
a1b82138 VZ |
916 | // ---------------------------------------------------------------------------- |
917 | ||
aac7e7fe | 918 | void wxTextCtrl::SetSelection(long from, long to) |
2bda0e17 | 919 | { |
aac7e7fe VZ |
920 | // if from and to are both -1, it means (in wxWindows) that all text should |
921 | // be selected - translate into Windows convention | |
922 | if ( (from == -1) && (to == -1) ) | |
923 | { | |
924 | from = 0; | |
925 | to = -1; | |
926 | } | |
927 | ||
a5aa8086 VZ |
928 | DoSetSelection(from, to); |
929 | } | |
930 | ||
931 | void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) | |
932 | { | |
789295bf | 933 | HWND hWnd = GetHwnd(); |
39136494 | 934 | |
2bda0e17 | 935 | #ifdef __WIN32__ |
aac7e7fe VZ |
936 | #if wxUSE_RICHEDIT |
937 | if ( IsRich() ) | |
938 | { | |
939 | CHARRANGE range; | |
940 | range.cpMin = from; | |
941 | range.cpMax = to; | |
942 | SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); | |
943 | } | |
944 | else | |
945 | #endif // wxUSE_RICHEDIT | |
946 | { | |
947 | SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to); | |
948 | } | |
a1b82138 | 949 | |
aac7e7fe | 950 | if ( scrollCaret ) |
2bda0e17 | 951 | { |
aac7e7fe | 952 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); |
2bda0e17 | 953 | } |
aac7e7fe VZ |
954 | #else // Win16 |
955 | // WPARAM is 0: selection is scrolled into view | |
956 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(from, to)); | |
957 | #endif // Win32/16 | |
958 | } | |
959 | ||
960 | // ---------------------------------------------------------------------------- | |
961 | // Editing | |
962 | // ---------------------------------------------------------------------------- | |
39136494 | 963 | |
aac7e7fe VZ |
964 | void wxTextCtrl::Replace(long from, long to, const wxString& value) |
965 | { | |
966 | // Set selection and remove it | |
f882d57e | 967 | DoSetSelection(from, to, FALSE /* don't scroll caret into view */); |
aac7e7fe VZ |
968 | |
969 | SendMessage(GetHwnd(), EM_REPLACESEL, | |
2bda0e17 | 970 | #ifdef __WIN32__ |
aac7e7fe | 971 | TRUE, |
2bda0e17 | 972 | #else |
aac7e7fe | 973 | FALSE, |
2bda0e17 | 974 | #endif |
aac7e7fe VZ |
975 | (LPARAM)value.c_str()); |
976 | } | |
977 | ||
978 | void wxTextCtrl::Remove(long from, long to) | |
979 | { | |
980 | Replace(from, to, _T("")); | |
2bda0e17 KB |
981 | } |
982 | ||
983 | bool wxTextCtrl::LoadFile(const wxString& file) | |
984 | { | |
a1b82138 | 985 | if ( wxTextCtrlBase::LoadFile(file) ) |
cd471848 | 986 | { |
a1b82138 VZ |
987 | // update the size limit if needed |
988 | AdjustSpaceLimit(); | |
2bda0e17 | 989 | |
a1b82138 | 990 | return TRUE; |
2bda0e17 | 991 | } |
2bda0e17 | 992 | |
a1b82138 | 993 | return FALSE; |
2bda0e17 KB |
994 | } |
995 | ||
cd471848 | 996 | bool wxTextCtrl::IsModified() const |
2bda0e17 | 997 | { |
a5aa8086 | 998 | return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; |
2bda0e17 KB |
999 | } |
1000 | ||
1001 | // Makes 'unmodified' | |
cd471848 | 1002 | void wxTextCtrl::DiscardEdits() |
2bda0e17 | 1003 | { |
a1b82138 | 1004 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); |
2bda0e17 KB |
1005 | } |
1006 | ||
cd471848 | 1007 | int wxTextCtrl::GetNumberOfLines() const |
2bda0e17 | 1008 | { |
789295bf | 1009 | return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); |
2bda0e17 KB |
1010 | } |
1011 | ||
debe6624 | 1012 | long wxTextCtrl::XYToPosition(long x, long y) const |
2bda0e17 | 1013 | { |
2bda0e17 | 1014 | // This gets the char index for the _beginning_ of this line |
a5aa8086 VZ |
1015 | long charIndex = SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0); |
1016 | ||
1017 | return charIndex + x; | |
2bda0e17 KB |
1018 | } |
1019 | ||
0efe5ba7 | 1020 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
2bda0e17 | 1021 | { |
789295bf | 1022 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
1023 | |
1024 | // This gets the line number containing the character | |
a5aa8086 | 1025 | long lineNo; |
0efe5ba7 | 1026 | #if wxUSE_RICHEDIT |
aac7e7fe | 1027 | if ( IsRich() ) |
0efe5ba7 | 1028 | { |
a5aa8086 | 1029 | lineNo = SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos); |
0efe5ba7 VZ |
1030 | } |
1031 | else | |
1032 | #endif // wxUSE_RICHEDIT | |
a5aa8086 VZ |
1033 | { |
1034 | lineNo = SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0); | |
1035 | } | |
0efe5ba7 VZ |
1036 | |
1037 | if ( lineNo == -1 ) | |
1038 | { | |
1039 | // no such line | |
1040 | return FALSE; | |
1041 | } | |
1042 | ||
2bda0e17 | 1043 | // This gets the char index for the _beginning_ of this line |
a5aa8086 | 1044 | long charIndex = SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); |
0efe5ba7 VZ |
1045 | if ( charIndex == -1 ) |
1046 | { | |
1047 | return FALSE; | |
1048 | } | |
1049 | ||
2bda0e17 | 1050 | // The X position must therefore be the different between pos and charIndex |
0efe5ba7 | 1051 | if ( x ) |
a5aa8086 | 1052 | *x = pos - charIndex; |
0efe5ba7 | 1053 | if ( y ) |
a5aa8086 | 1054 | *y = lineNo; |
0efe5ba7 VZ |
1055 | |
1056 | return TRUE; | |
2bda0e17 KB |
1057 | } |
1058 | ||
debe6624 | 1059 | void wxTextCtrl::ShowPosition(long pos) |
2bda0e17 | 1060 | { |
789295bf | 1061 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
1062 | |
1063 | // To scroll to a position, we pass the number of lines and characters | |
1064 | // to scroll *by*. This means that we need to: | |
1065 | // (1) Find the line position of the current line. | |
1066 | // (2) Find the line position of pos. | |
1067 | // (3) Scroll by (pos - current). | |
1068 | // For now, ignore the horizontal scrolling. | |
1069 | ||
1070 | // Is this where scrolling is relative to - the line containing the caret? | |
1071 | // Or is the first visible line??? Try first visible line. | |
1072 | // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L); | |
1073 | ||
1074 | int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L); | |
1075 | ||
1076 | int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L); | |
39136494 | 1077 | |
2bda0e17 KB |
1078 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; |
1079 | ||
2bda0e17 | 1080 | if (linesToScroll != 0) |
de4d7713 | 1081 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); |
2bda0e17 KB |
1082 | } |
1083 | ||
a5aa8086 VZ |
1084 | long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const |
1085 | { | |
1086 | return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0); | |
1087 | } | |
1088 | ||
debe6624 | 1089 | int wxTextCtrl::GetLineLength(long lineNo) const |
2bda0e17 | 1090 | { |
a5aa8086 VZ |
1091 | long pos = XYToPosition(0, lineNo); |
1092 | ||
1093 | return GetLengthOfLineContainingPos(pos); | |
2bda0e17 KB |
1094 | } |
1095 | ||
debe6624 | 1096 | wxString wxTextCtrl::GetLineText(long lineNo) const |
2bda0e17 | 1097 | { |
a1b82138 | 1098 | size_t len = (size_t)GetLineLength(lineNo) + 1; |
488fe1fe | 1099 | |
f6bcfd97 BP |
1100 | // there must be at least enough place for the length WORD in the |
1101 | // buffer | |
1102 | len += sizeof(WORD); | |
4438caf4 | 1103 | |
f6bcfd97 BP |
1104 | wxString str; |
1105 | wxChar *buf = str.GetWriteBuf(len); | |
1106 | ||
33ac7e6f | 1107 | *(WORD *)buf = (WORD)len; |
f6bcfd97 BP |
1108 | len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf); |
1109 | buf[len] = 0; | |
4438caf4 | 1110 | |
f6bcfd97 | 1111 | str.UngetWriteBuf(len); |
4438caf4 VZ |
1112 | |
1113 | return str; | |
2bda0e17 KB |
1114 | } |
1115 | ||
d7eee191 VZ |
1116 | void wxTextCtrl::SetMaxLength(unsigned long len) |
1117 | { | |
1118 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0); | |
1119 | } | |
1120 | ||
a1b82138 | 1121 | // ---------------------------------------------------------------------------- |
ca8b28f2 | 1122 | // Undo/redo |
a1b82138 VZ |
1123 | // ---------------------------------------------------------------------------- |
1124 | ||
ca8b28f2 JS |
1125 | void wxTextCtrl::Undo() |
1126 | { | |
1127 | if (CanUndo()) | |
1128 | { | |
789295bf | 1129 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
1130 | } |
1131 | } | |
1132 | ||
1133 | void wxTextCtrl::Redo() | |
1134 | { | |
1135 | if (CanRedo()) | |
1136 | { | |
1137 | // Same as Undo, since Undo undoes the undo, i.e. a redo. | |
789295bf | 1138 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
1139 | } |
1140 | } | |
1141 | ||
1142 | bool wxTextCtrl::CanUndo() const | |
1143 | { | |
a5aa8086 | 1144 | return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; |
ca8b28f2 JS |
1145 | } |
1146 | ||
1147 | bool wxTextCtrl::CanRedo() const | |
1148 | { | |
a5aa8086 | 1149 | return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; |
ca8b28f2 JS |
1150 | } |
1151 | ||
a1b82138 VZ |
1152 | // ---------------------------------------------------------------------------- |
1153 | // implemenation details | |
1154 | // ---------------------------------------------------------------------------- | |
39136494 | 1155 | |
2bda0e17 KB |
1156 | void wxTextCtrl::Command(wxCommandEvent & event) |
1157 | { | |
a1b82138 VZ |
1158 | SetValue(event.GetString()); |
1159 | ProcessCommand (event); | |
2bda0e17 KB |
1160 | } |
1161 | ||
1162 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
1163 | { | |
a1b82138 VZ |
1164 | // By default, load the first file into the text window. |
1165 | if (event.GetNumberOfFiles() > 0) | |
1166 | { | |
1167 | LoadFile(event.GetFiles()[0]); | |
1168 | } | |
2bda0e17 KB |
1169 | } |
1170 | ||
a37d422a VZ |
1171 | // ---------------------------------------------------------------------------- |
1172 | // kbd input processing | |
1173 | // ---------------------------------------------------------------------------- | |
1174 | ||
1175 | bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* pMsg) | |
1176 | { | |
1177 | MSG *msg = (MSG *)pMsg; | |
1178 | ||
1179 | // check for our special keys here: if we don't do it and the parent frame | |
1180 | // uses them as accelerators, they wouldn't work at all, so we disable | |
1181 | // usual preprocessing for them | |
1182 | if ( msg->message == WM_KEYDOWN ) | |
1183 | { | |
574c939e | 1184 | WORD vkey = (WORD) msg->wParam; |
a37d422a VZ |
1185 | if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN ) |
1186 | { | |
1187 | if ( vkey == VK_BACK ) | |
1188 | return FALSE; | |
1189 | } | |
1190 | else // no Alt | |
1191 | { | |
cf6e951c VZ |
1192 | // we want to process some Ctrl-foo and Shift-bar but no key |
1193 | // combinations without either Ctrl or Shift nor with both of them | |
1194 | // pressed | |
1195 | const int ctrl = wxIsCtrlDown(), | |
1196 | shift = wxIsShiftDown(); | |
1197 | switch ( ctrl + shift ) | |
a37d422a | 1198 | { |
cf6e951c VZ |
1199 | default: |
1200 | wxFAIL_MSG( _T("how many modifiers have we got?") ); | |
1201 | // fall through | |
1202 | ||
1203 | case 0: | |
1204 | case 2: | |
1205 | break; | |
1206 | ||
1207 | case 1: | |
1208 | // either Ctrl or Shift pressed | |
1209 | if ( ctrl ) | |
1210 | { | |
1211 | switch ( vkey ) | |
1212 | { | |
1213 | case 'C': | |
1214 | case 'V': | |
1215 | case 'X': | |
1216 | case VK_INSERT: | |
1217 | case VK_DELETE: | |
1218 | case VK_HOME: | |
1219 | case VK_END: | |
1220 | return FALSE; | |
1221 | } | |
1222 | } | |
1223 | else // Shift is pressed | |
1224 | { | |
1225 | if ( vkey == VK_INSERT || vkey == VK_DELETE ) | |
1226 | return FALSE; | |
1227 | } | |
a37d422a VZ |
1228 | } |
1229 | } | |
1230 | } | |
1231 | ||
1232 | return wxControl::MSWShouldPreProcessMessage(pMsg); | |
1233 | } | |
1234 | ||
2bda0e17 KB |
1235 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
1236 | { | |
42e69d6b | 1237 | switch ( event.KeyCode() ) |
cd471848 | 1238 | { |
cd471848 | 1239 | case WXK_RETURN: |
39136494 | 1240 | if ( !(m_windowStyle & wxTE_MULTILINE) ) |
cd471848 VZ |
1241 | { |
1242 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
bfbd6dc1 | 1243 | InitCommandEvent(event); |
f6bcfd97 | 1244 | event.SetString(GetValue()); |
cd471848 VZ |
1245 | if ( GetEventHandler()->ProcessEvent(event) ) |
1246 | return; | |
1247 | } | |
5fb9fcfc VZ |
1248 | //else: multiline controls need Enter for themselves |
1249 | ||
1250 | break; | |
4d91c1d1 | 1251 | |
cd471848 | 1252 | case WXK_TAB: |
319fefa9 VZ |
1253 | // always produce navigation event - even if we process TAB |
1254 | // ourselves the fact that we got here means that the user code | |
1255 | // decided to skip processing of this TAB - probably to let it | |
1256 | // do its default job. | |
cd471848 | 1257 | { |
5fb9fcfc VZ |
1258 | wxNavigationKeyEvent eventNav; |
1259 | eventNav.SetDirection(!event.ShiftDown()); | |
8614c467 | 1260 | eventNav.SetWindowChange(event.ControlDown()); |
5fb9fcfc | 1261 | eventNav.SetEventObject(this); |
39136494 | 1262 | |
d9506e77 | 1263 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) |
cd471848 VZ |
1264 | return; |
1265 | } | |
341c92a8 | 1266 | break; |
cd471848 | 1267 | } |
39136494 | 1268 | |
8614c467 | 1269 | // no, we didn't process it |
42e69d6b | 1270 | event.Skip(); |
2bda0e17 KB |
1271 | } |
1272 | ||
0cf5b099 VZ |
1273 | long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
1274 | { | |
e7e91e03 VZ |
1275 | long lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam); |
1276 | ||
0cf5b099 VZ |
1277 | if ( nMsg == WM_GETDLGCODE ) |
1278 | { | |
1279 | // we always want the chars and the arrows | |
1280 | long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS; | |
1281 | ||
1282 | // we may have several different cases: | |
1283 | // 1. normal case: both TAB and ENTER are used for dialog navigation | |
2adfb497 VZ |
1284 | // 2. ctrl which wants TAB for itself: ENTER is used to pass to the |
1285 | // next control in the dialog | |
1286 | // 3. ctrl which wants ENTER for itself: TAB is used for dialog | |
1287 | // navigation | |
1288 | // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass | |
1289 | // to the next control | |
1290 | ||
1291 | // the multiline edit control should always get <Return> for itself | |
1292 | if ( HasFlag(wxTE_PROCESS_ENTER) || HasFlag(wxTE_MULTILINE) ) | |
0cf5b099 | 1293 | lDlgCode |= DLGC_WANTMESSAGE; |
2adfb497 VZ |
1294 | |
1295 | if ( HasFlag(wxTE_PROCESS_TAB) ) | |
0cf5b099 VZ |
1296 | lDlgCode |= DLGC_WANTTAB; |
1297 | ||
e7e91e03 | 1298 | lRc |= lDlgCode; |
0cf5b099 VZ |
1299 | } |
1300 | ||
e7e91e03 | 1301 | return lRc; |
129223d6 VZ |
1302 | } |
1303 | ||
1304 | // ---------------------------------------------------------------------------- | |
1305 | // text control event processing | |
1306 | // ---------------------------------------------------------------------------- | |
1307 | ||
5036ea90 VZ |
1308 | bool wxTextCtrl::SendUpdateEvent() |
1309 | { | |
1310 | // is event reporting suspended? | |
1311 | if ( m_suppressNextUpdate ) | |
1312 | { | |
1313 | // do process the next one | |
1314 | m_suppressNextUpdate = FALSE; | |
1315 | ||
1316 | return FALSE; | |
1317 | } | |
1318 | ||
1319 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
1320 | InitCommandEvent(event); | |
1321 | event.SetString(GetValue()); | |
1322 | ||
1323 | return ProcessCommand(event); | |
1324 | } | |
1325 | ||
debe6624 | 1326 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 1327 | { |
5036ea90 | 1328 | switch ( param ) |
789295bf VZ |
1329 | { |
1330 | case EN_SETFOCUS: | |
1331 | case EN_KILLFOCUS: | |
1332 | { | |
1333 | wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS | |
d7eee191 VZ |
1334 | : wxEVT_SET_FOCUS, |
1335 | m_windowId); | |
5036ea90 | 1336 | event.SetEventObject(this); |
789295bf VZ |
1337 | GetEventHandler()->ProcessEvent(event); |
1338 | } | |
1339 | break; | |
ae29de83 | 1340 | |
789295bf | 1341 | case EN_CHANGE: |
5036ea90 | 1342 | SendUpdateEvent(); |
789295bf | 1343 | break; |
2bda0e17 | 1344 | |
b12915c1 | 1345 | case EN_MAXTEXT: |
5036ea90 | 1346 | // the text size limit has been hit -- try to increase it |
d7eee191 VZ |
1347 | if ( !AdjustSpaceLimit() ) |
1348 | { | |
1349 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId); | |
1350 | InitCommandEvent(event); | |
1351 | event.SetString(GetValue()); | |
1352 | ProcessCommand(event); | |
1353 | } | |
789295bf VZ |
1354 | break; |
1355 | ||
5036ea90 | 1356 | // the other edit notification messages are not processed |
789295bf VZ |
1357 | default: |
1358 | return FALSE; | |
1359 | } | |
1360 | ||
1361 | // processed | |
1362 | return TRUE; | |
2bda0e17 KB |
1363 | } |
1364 | ||
33ac7e6f | 1365 | WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
1366 | #if wxUSE_CTL3D |
1367 | WXUINT message, | |
1368 | WXWPARAM wParam, | |
1369 | WXLPARAM lParam | |
1370 | #else | |
33ac7e6f KB |
1371 | WXUINT WXUNUSED(message), |
1372 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
1373 | WXLPARAM WXUNUSED(lParam) |
1374 | #endif | |
1375 | ) | |
f6bcfd97 BP |
1376 | { |
1377 | #if wxUSE_CTL3D | |
1378 | if ( m_useCtl3D ) | |
1379 | { | |
1380 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
1381 | return (WXHBRUSH) hbrush; | |
1382 | } | |
1383 | #endif // wxUSE_CTL3D | |
1384 | ||
1385 | HDC hdc = (HDC)pDC; | |
1386 | if (GetParent()->GetTransparentBackground()) | |
1387 | SetBkMode(hdc, TRANSPARENT); | |
1388 | else | |
1389 | SetBkMode(hdc, OPAQUE); | |
1390 | ||
1391 | wxColour colBack = GetBackgroundColour(); | |
1392 | ||
1393 | if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0) | |
a756f210 | 1394 | colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
f6bcfd97 BP |
1395 | |
1396 | ::SetBkColor(hdc, wxColourToRGB(colBack)); | |
1397 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
1398 | ||
1399 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
1400 | ||
1401 | return (WXHBRUSH)brush->GetResourceHandle(); | |
1402 | } | |
1403 | ||
1404 | // In WIN16, need to override normal erasing because | |
1405 | // Ctl3D doesn't use the wxWindows background colour. | |
1406 | #ifdef __WIN16__ | |
1407 | void wxTextCtrl::OnEraseBackground(wxEraseEvent& event) | |
1408 | { | |
1409 | wxColour col(m_backgroundColour); | |
1410 | ||
1411 | #if wxUSE_CTL3D | |
1412 | if (m_useCtl3D) | |
a756f210 | 1413 | col = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); |
f6bcfd97 BP |
1414 | #endif |
1415 | ||
1416 | RECT rect; | |
1417 | ::GetClientRect(GetHwnd(), &rect); | |
1418 | ||
a5aa8086 | 1419 | COLORREF ref = wxColourToRGB(col); |
f6bcfd97 BP |
1420 | HBRUSH hBrush = ::CreateSolidBrush(ref); |
1421 | if ( !hBrush ) | |
1422 | wxLogLastError(wxT("CreateSolidBrush")); | |
1423 | ||
1424 | HDC hdc = (HDC)event.GetDC()->GetHDC(); | |
1425 | ||
1426 | int mode = ::SetMapMode(hdc, MM_TEXT); | |
1427 | ||
1428 | ::FillRect(hdc, &rect, hBrush); | |
1429 | ::DeleteObject(hBrush); | |
1430 | ::SetMapMode(hdc, mode); | |
1431 | ||
1432 | } | |
aac7e7fe | 1433 | #endif // Win16 |
f6bcfd97 | 1434 | |
d7eee191 | 1435 | bool wxTextCtrl::AdjustSpaceLimit() |
789295bf | 1436 | { |
25889d3c | 1437 | #ifndef __WIN16__ |
d7eee191 VZ |
1438 | unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0); |
1439 | ||
1440 | // HACK: we try to automatically extend the limit for the amount of text | |
1441 | // to allow (interactively) entering more than 64Kb of text under | |
1442 | // Win9x but we shouldn't reset the text limit which was previously | |
1443 | // set explicitly with SetMaxLength() | |
1444 | // | |
1445 | // we could solve this by storing the limit we set in wxTextCtrl but | |
1446 | // to save space we prefer to simply test here the actual limit | |
1447 | // value: we consider that SetMaxLength() can only be called for | |
1448 | // values < 32Kb | |
1449 | if ( limit < 0x8000 ) | |
1450 | { | |
1451 | // we've got more text than limit set by SetMaxLength() | |
1452 | return FALSE; | |
1453 | } | |
1454 | ||
1455 | unsigned int len = ::GetWindowTextLength(GetHwnd()); | |
17d8ee1c | 1456 | if ( len >= limit ) |
789295bf VZ |
1457 | { |
1458 | limit = len + 0x8000; // 32Kb | |
1459 | ||
5ea105e0 | 1460 | #if wxUSE_RICHEDIT |
aac7e7fe | 1461 | if ( IsRich() ) |
b12915c1 VZ |
1462 | { |
1463 | // as a nice side effect, this also allows passing limit > 64Kb | |
1464 | ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit); | |
1465 | } | |
789295bf | 1466 | else |
b12915c1 VZ |
1467 | #endif // wxUSE_RICHEDIT |
1468 | { | |
1469 | if ( limit > 0xffff ) | |
1470 | { | |
1471 | // this will set it to a platform-dependent maximum (much more | |
1472 | // than 64Kb under NT) | |
1473 | limit = 0; | |
1474 | } | |
1475 | ||
789295bf | 1476 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0); |
b12915c1 | 1477 | } |
789295bf | 1478 | } |
b12915c1 | 1479 | #endif // !Win16 |
d7eee191 VZ |
1480 | |
1481 | // we changed the limit | |
1482 | return TRUE; | |
789295bf | 1483 | } |
2bda0e17 | 1484 | |
a1b82138 | 1485 | bool wxTextCtrl::AcceptsFocus() const |
2bda0e17 | 1486 | { |
a1b82138 VZ |
1487 | // we don't want focus if we can't be edited |
1488 | return IsEditable() && wxControl::AcceptsFocus(); | |
1489 | } | |
c085e333 | 1490 | |
f68586e5 | 1491 | wxSize wxTextCtrl::DoGetBestSize() const |
a1b82138 VZ |
1492 | { |
1493 | int cx, cy; | |
1494 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
1495 | ||
1496 | int wText = DEFAULT_ITEM_WIDTH; | |
1497 | ||
1498 | int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
1499 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1500 | { | |
3988b155 | 1501 | hText *= wxMax(GetNumberOfLines(), 5); |
a1b82138 VZ |
1502 | } |
1503 | //else: for single line control everything is ok | |
1504 | ||
1505 | return wxSize(wText, hText); | |
2bda0e17 | 1506 | } |
a1b82138 VZ |
1507 | |
1508 | // ---------------------------------------------------------------------------- | |
1509 | // standard handlers for standard edit menu events | |
1510 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1511 | |
bfbd6dc1 | 1512 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1513 | { |
1514 | Cut(); | |
1515 | } | |
1516 | ||
bfbd6dc1 | 1517 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1518 | { |
1519 | Copy(); | |
1520 | } | |
1521 | ||
bfbd6dc1 | 1522 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1523 | { |
1524 | Paste(); | |
1525 | } | |
1526 | ||
bfbd6dc1 | 1527 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1528 | { |
1529 | Undo(); | |
1530 | } | |
1531 | ||
bfbd6dc1 | 1532 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) |
e702ff0f JS |
1533 | { |
1534 | Redo(); | |
1535 | } | |
1536 | ||
1537 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1538 | { | |
1539 | event.Enable( CanCut() ); | |
1540 | } | |
1541 | ||
1542 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1543 | { | |
1544 | event.Enable( CanCopy() ); | |
1545 | } | |
1546 | ||
1547 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1548 | { | |
1549 | event.Enable( CanPaste() ); | |
1550 | } | |
1551 | ||
1552 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1553 | { | |
1554 | event.Enable( CanUndo() ); | |
1555 | } | |
1556 | ||
1557 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1558 | { | |
1559 | event.Enable( CanRedo() ); | |
1560 | } | |
1561 | ||
4bc1afd5 VZ |
1562 | // the rest of the file only deals with the rich edit controls |
1563 | #if wxUSE_RICHEDIT | |
1564 | ||
c57e3339 VZ |
1565 | // ---------------------------------------------------------------------------- |
1566 | // EN_LINK processing | |
1567 | // ---------------------------------------------------------------------------- | |
1568 | ||
a64c3b74 | 1569 | bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
c57e3339 | 1570 | { |
3bce6687 | 1571 | #ifndef __WXWINE__ |
c57e3339 | 1572 | NMHDR *hdr = (NMHDR* )lParam; |
1dae1d00 | 1573 | switch ( hdr->code ) |
c57e3339 | 1574 | { |
3bce6687 | 1575 | case EN_MSGFILTER: |
1dae1d00 VZ |
1576 | { |
1577 | const MSGFILTER *msgf = (MSGFILTER *)lParam; | |
1578 | UINT msg = msgf->msg; | |
1579 | ||
1580 | // this is a bit crazy but richedit 1.0 sends us all mouse | |
1581 | // events _except_ WM_LBUTTONUP (don't ask me why) so we have | |
1582 | // generate the wxWin events for this message manually | |
1583 | // | |
1584 | // NB: in fact, this is still not totally correct as it does | |
1585 | // send us WM_LBUTTONUP if the selection was cleared by the | |
1586 | // last click -- so currently we get 2 events in this case, | |
1587 | // but as I don't see any obvious way to check for this I | |
1588 | // leave this code in place because it's still better than | |
1589 | // not getting left up events at all | |
1590 | if ( msg == WM_LBUTTONUP ) | |
c57e3339 | 1591 | { |
1dae1d00 VZ |
1592 | WXUINT flags = msgf->wParam; |
1593 | int x = GET_X_LPARAM(msgf->lParam), | |
1594 | y = GET_Y_LPARAM(msgf->lParam); | |
1595 | ||
1596 | HandleMouseEvent(msg, x, y, flags); | |
c57e3339 | 1597 | } |
1dae1d00 | 1598 | } |
c57e3339 | 1599 | |
1dae1d00 VZ |
1600 | // return TRUE to process the event (and FALSE to ignore it) |
1601 | return TRUE; | |
1602 | ||
1603 | case EN_LINK: | |
1604 | { | |
1605 | const ENLINK *enlink = (ENLINK *)hdr; | |
1606 | ||
1607 | switch ( enlink->msg ) | |
1608 | { | |
1609 | case WM_SETCURSOR: | |
1610 | // ok, so it is hardcoded - do we really nee to | |
1611 | // customize it? | |
1612 | ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND))); | |
1613 | *result = TRUE; | |
1614 | break; | |
1615 | ||
1616 | case WM_MOUSEMOVE: | |
1617 | case WM_LBUTTONDOWN: | |
1618 | case WM_LBUTTONUP: | |
1619 | case WM_LBUTTONDBLCLK: | |
1620 | case WM_RBUTTONDOWN: | |
1621 | case WM_RBUTTONUP: | |
1622 | case WM_RBUTTONDBLCLK: | |
1623 | // send a mouse event | |
1624 | { | |
1625 | static const wxEventType eventsMouse[] = | |
1626 | { | |
1627 | wxEVT_MOTION, | |
1628 | wxEVT_LEFT_DOWN, | |
1629 | wxEVT_LEFT_UP, | |
1630 | wxEVT_LEFT_DCLICK, | |
1631 | wxEVT_RIGHT_DOWN, | |
1632 | wxEVT_RIGHT_UP, | |
1633 | wxEVT_RIGHT_DCLICK, | |
1634 | }; | |
1635 | ||
1636 | // the event ids are consecutive | |
1637 | wxMouseEvent | |
1638 | evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]); | |
1639 | ||
1640 | InitMouseEvent(evtMouse, | |
1641 | GET_X_LPARAM(enlink->lParam), | |
1642 | GET_Y_LPARAM(enlink->lParam), | |
1643 | enlink->wParam); | |
1644 | ||
1645 | wxTextUrlEvent event(m_windowId, evtMouse, | |
1646 | enlink->chrg.cpMin, | |
1647 | enlink->chrg.cpMax); | |
1648 | ||
1649 | InitCommandEvent(event); | |
1650 | ||
1651 | *result = ProcessCommand(event); | |
1652 | } | |
1653 | break; | |
1654 | } | |
1655 | } | |
1656 | return TRUE; | |
c57e3339 | 1657 | } |
3bce6687 JS |
1658 | #endif |
1659 | ||
d86ab8e2 VZ |
1660 | // not processed, leave it to the base class |
1661 | return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result); | |
c57e3339 VZ |
1662 | } |
1663 | ||
f6bcfd97 BP |
1664 | // ---------------------------------------------------------------------------- |
1665 | // colour setting for the rich edit controls | |
1666 | // ---------------------------------------------------------------------------- | |
1667 | ||
f6bcfd97 BP |
1668 | bool wxTextCtrl::SetBackgroundColour(const wxColour& colour) |
1669 | { | |
1670 | if ( !wxTextCtrlBase::SetBackgroundColour(colour) ) | |
1671 | { | |
1672 | // colour didn't really change | |
1673 | return FALSE; | |
1674 | } | |
1675 | ||
1676 | if ( IsRich() ) | |
1677 | { | |
1678 | // rich edit doesn't use WM_CTLCOLOR, hence we need to send | |
1679 | // EM_SETBKGNDCOLOR additionally | |
1680 | ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour)); | |
1681 | } | |
1682 | ||
1683 | return TRUE; | |
1684 | } | |
1685 | ||
1686 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) | |
1687 | { | |
1688 | if ( !wxTextCtrlBase::SetForegroundColour(colour) ) | |
1689 | { | |
1690 | // colour didn't really change | |
1691 | return FALSE; | |
1692 | } | |
1693 | ||
1694 | if ( IsRich() ) | |
1695 | { | |
1696 | // change the colour of everything | |
1697 | CHARFORMAT cf; | |
1698 | wxZeroMemory(cf); | |
1699 | cf.cbSize = sizeof(cf); | |
1700 | cf.dwMask = CFM_COLOR; | |
1701 | cf.crTextColor = wxColourToRGB(colour); | |
1702 | ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf); | |
1703 | } | |
1704 | ||
1705 | return TRUE; | |
1706 | } | |
1707 | ||
4bc1afd5 VZ |
1708 | // ---------------------------------------------------------------------------- |
1709 | // styling support for rich edit controls | |
1710 | // ---------------------------------------------------------------------------- | |
1711 | ||
cc164686 RD |
1712 | #if wxUSE_RICHEDIT |
1713 | ||
4bc1afd5 VZ |
1714 | bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) |
1715 | { | |
3bce6687 JS |
1716 | #ifdef __WXWINE__ |
1717 | return FALSE; | |
1718 | #else | |
4bc1afd5 VZ |
1719 | if ( !IsRich() ) |
1720 | { | |
1721 | // can't do it with normal text control | |
1722 | return FALSE; | |
1723 | } | |
1724 | ||
a5aa8086 VZ |
1725 | // the richedit 1.0 doesn't handle setting background colour, so don't |
1726 | // even try to do anything if it's the only thing we want to change | |
1727 | if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() ) | |
4bc1afd5 | 1728 | { |
be329a3d RD |
1729 | // nothing to do: return TRUE if there was really nothing to do and |
1730 | // FALSE if we failed to set bg colour | |
4bc1afd5 VZ |
1731 | return !style.HasBackgroundColour(); |
1732 | } | |
1733 | ||
1734 | // order the range if needed | |
1735 | if ( start > end ) | |
1736 | { | |
1737 | long tmp = start; | |
1738 | start = end; | |
1739 | end = tmp; | |
1740 | } | |
1741 | ||
1742 | // we can only change the format of the selection, so select the range we | |
1743 | // want and restore the old selection later | |
1744 | long startOld, endOld; | |
1745 | GetSelection(&startOld, &endOld); | |
1746 | ||
1747 | // but do we really have to change the selection? | |
1748 | bool changeSel = start != startOld || end != endOld; | |
1749 | ||
1750 | if ( changeSel ) | |
aac7e7fe | 1751 | { |
f882d57e | 1752 | DoSetSelection(start, end, FALSE /* don't scroll caret into view */); |
aac7e7fe | 1753 | } |
4bc1afd5 VZ |
1754 | |
1755 | // initialize CHARFORMAT struct | |
be329a3d RD |
1756 | #if wxUSE_RICHEDIT2 |
1757 | CHARFORMAT2 cf; | |
1758 | #else | |
4bc1afd5 | 1759 | CHARFORMAT cf; |
be329a3d | 1760 | #endif |
a5aa8086 | 1761 | |
4bc1afd5 | 1762 | wxZeroMemory(cf); |
a5aa8086 VZ |
1763 | |
1764 | // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple | |
1765 | // CHARFORMAT in that case | |
1766 | #if wxUSE_RICHEDIT2 | |
1767 | if ( m_verRichEdit == 1 ) | |
1768 | { | |
1769 | // this is the only thing the control is going to grok | |
1770 | cf.cbSize = sizeof(CHARFORMAT); | |
1771 | } | |
1772 | else | |
1773 | #endif | |
1774 | { | |
1775 | // CHARFORMAT or CHARFORMAT2 | |
1776 | cf.cbSize = sizeof(cf); | |
1777 | } | |
4bc1afd5 VZ |
1778 | |
1779 | if ( style.HasFont() ) | |
1780 | { | |
aac7e7fe VZ |
1781 | // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0 |
1782 | // but using it doesn't seem to hurt neither so leaving it for now | |
1783 | ||
784164e1 VZ |
1784 | cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET | |
1785 | CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE; | |
4bc1afd5 VZ |
1786 | |
1787 | // fill in data from LOGFONT but recalculate lfHeight because we need | |
1788 | // the real height in twips and not the negative number which | |
1789 | // wxFillLogFont() returns (this is correct in general and works with | |
1790 | // the Windows font mapper, but not here) | |
1791 | LOGFONT lf; | |
1792 | wxFillLogFont(&lf, &style.GetFont()); | |
1793 | cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips | |
1794 | cf.bCharSet = lf.lfCharSet; | |
1795 | cf.bPitchAndFamily = lf.lfPitchAndFamily; | |
1796 | wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) ); | |
1797 | ||
784164e1 VZ |
1798 | // also deal with underline/italic/bold attributes: note that we must |
1799 | // always set CFM_ITALIC &c bits in dwMask, even if we don't set the | |
1800 | // style to allow clearing it | |
4bc1afd5 VZ |
1801 | if ( lf.lfItalic ) |
1802 | { | |
4bc1afd5 VZ |
1803 | cf.dwEffects |= CFE_ITALIC; |
1804 | } | |
1805 | ||
1806 | if ( lf.lfWeight == FW_BOLD ) | |
1807 | { | |
4bc1afd5 VZ |
1808 | cf.dwEffects |= CFE_BOLD; |
1809 | } | |
1810 | ||
1811 | if ( lf.lfUnderline ) | |
1812 | { | |
4bc1afd5 VZ |
1813 | cf.dwEffects |= CFE_UNDERLINE; |
1814 | } | |
1815 | ||
1816 | // strikeout fonts are not supported by wxWindows | |
1817 | } | |
1818 | ||
1819 | if ( style.HasTextColour() ) | |
1820 | { | |
1821 | cf.dwMask |= CFM_COLOR; | |
1822 | cf.crTextColor = wxColourToRGB(style.GetTextColour()); | |
1823 | } | |
1824 | ||
be329a3d | 1825 | #if wxUSE_RICHEDIT2 |
a5aa8086 | 1826 | if ( m_verRichEdit != 1 && style.HasBackgroundColour() ) |
be329a3d RD |
1827 | { |
1828 | cf.dwMask |= CFM_BACKCOLOR; | |
1829 | cf.crBackColor = wxColourToRGB(style.GetBackgroundColour()); | |
1830 | } | |
784164e1 VZ |
1831 | #endif // wxUSE_RICHEDIT2 |
1832 | ||
4bc1afd5 VZ |
1833 | // do format the selection |
1834 | bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, | |
1835 | SCF_SELECTION, (LPARAM)&cf) != 0; | |
1836 | if ( !ok ) | |
1837 | { | |
1838 | wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed")); | |
1839 | } | |
1840 | ||
1841 | if ( changeSel ) | |
1842 | { | |
1843 | // restore the original selection | |
aac7e7fe | 1844 | DoSetSelection(startOld, endOld, FALSE); |
4bc1afd5 VZ |
1845 | } |
1846 | ||
1847 | return ok; | |
3bce6687 | 1848 | #endif |
4bc1afd5 | 1849 | } |
f6bcfd97 | 1850 | |
5bf75ae7 VZ |
1851 | bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style) |
1852 | { | |
1853 | if ( !wxTextCtrlBase::SetDefaultStyle(style) ) | |
1854 | return FALSE; | |
1855 | ||
1856 | // we have to do this or the style wouldn't apply for the text typed by the | |
1857 | // user | |
1858 | long posLast = GetLastPosition(); | |
1859 | SetStyle(posLast, posLast, m_defaultStyle); | |
1860 | ||
1861 | return TRUE; | |
1862 | } | |
1863 | ||
cc164686 RD |
1864 | #endif |
1865 | ||
b12915c1 VZ |
1866 | // ---------------------------------------------------------------------------- |
1867 | // wxRichEditModule | |
1868 | // ---------------------------------------------------------------------------- | |
1869 | ||
b12915c1 VZ |
1870 | bool wxRichEditModule::OnInit() |
1871 | { | |
1872 | // don't do anything - we will load it when needed | |
1873 | return TRUE; | |
1874 | } | |
1875 | ||
1876 | void wxRichEditModule::OnExit() | |
1877 | { | |
136cb3c7 | 1878 | for ( size_t i = 0; i < WXSIZEOF(ms_hRichEdit); i++ ) |
b12915c1 | 1879 | { |
a5aa8086 VZ |
1880 | if ( ms_hRichEdit[i] ) |
1881 | { | |
1882 | ::FreeLibrary(ms_hRichEdit[i]); | |
1883 | } | |
b12915c1 VZ |
1884 | } |
1885 | } | |
1886 | ||
1887 | /* static */ | |
1888 | bool wxRichEditModule::Load(int version) | |
1889 | { | |
a5aa8086 VZ |
1890 | // we don't support loading richedit 3.0 as I don't know how to distinguish |
1891 | // it from 2.0 anyhow | |
1892 | wxCHECK_MSG( version == 1 || version == 2, FALSE, | |
b12915c1 VZ |
1893 | _T("incorrect richedit control version requested") ); |
1894 | ||
a5aa8086 VZ |
1895 | // make it the index in the array |
1896 | version--; | |
1897 | ||
a5aa8086 | 1898 | if ( ms_hRichEdit[version] == (HINSTANCE)-1 ) |
b12915c1 | 1899 | { |
a5aa8086 VZ |
1900 | // we had already tried to load it and failed |
1901 | return FALSE; | |
b12915c1 VZ |
1902 | } |
1903 | ||
28978e0c VZ |
1904 | if ( ms_hRichEdit[version] ) |
1905 | { | |
1906 | // we've already got this one | |
1907 | return TRUE; | |
1908 | } | |
1909 | ||
a5aa8086 VZ |
1910 | wxString dllname = version ? _T("riched20") : _T("riched32"); |
1911 | dllname += _T(".dll"); | |
b12915c1 | 1912 | |
a5aa8086 | 1913 | ms_hRichEdit[version] = ::LoadLibrary(dllname); |
b12915c1 | 1914 | |
a5aa8086 | 1915 | if ( !ms_hRichEdit[version] ) |
b12915c1 VZ |
1916 | { |
1917 | wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str()); | |
1918 | ||
a5aa8086 | 1919 | ms_hRichEdit[version] = (HINSTANCE)-1; |
b12915c1 VZ |
1920 | |
1921 | return FALSE; | |
1922 | } | |
1923 | ||
1924 | return TRUE; | |
1925 | } | |
1926 | ||
1927 | #endif // wxUSE_RICHEDIT | |
1928 | ||
1e6feb95 | 1929 | #endif // wxUSE_TEXTCTRL |