]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: textctrl.cpp | |
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 | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | #ifdef __GNUG__ | |
17 | #pragma implementation "textctrl.h" | |
18 | #endif | |
19 | ||
20 | // ---------------------------------------------------------------------------- | |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/textctrl.h" | |
33 | #include "wx/settings.h" | |
34 | #include "wx/brush.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/intl.h" | |
37 | #include "wx/log.h" | |
38 | #include "wx/app.h" | |
39 | #endif | |
40 | ||
41 | #if wxUSE_CLIPBOARD | |
42 | #include "wx/clipbrd.h" | |
43 | #endif | |
44 | ||
45 | #include "wx/textfile.h" | |
46 | ||
47 | #include <windowsx.h> | |
48 | ||
49 | #include "wx/msw/private.h" | |
50 | ||
51 | #include <string.h> | |
52 | #include <stdlib.h> | |
53 | #include <sys/types.h> | |
54 | ||
55 | #if wxUSE_IOSTREAMH | |
56 | # include <fstream.h> | |
57 | #else | |
58 | # include <fstream> | |
59 | #endif | |
60 | ||
61 | #if wxUSE_RICHEDIT | |
62 | #include <richedit.h> | |
63 | #endif | |
64 | ||
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // event tables and other macros | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
70 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) | |
71 | ||
72 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
73 | EVT_CHAR(wxTextCtrl::OnChar) | |
74 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
75 | ||
76 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
77 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
78 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
79 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
80 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
81 | ||
82 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
83 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
84 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
85 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
86 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
87 | END_EVENT_TABLE() | |
88 | ||
89 | ||
90 | // ============================================================================ | |
91 | // implementation | |
92 | // ============================================================================ | |
93 | ||
94 | // ---------------------------------------------------------------------------- | |
95 | // creation | |
96 | // ---------------------------------------------------------------------------- | |
97 | ||
98 | wxTextCtrl::wxTextCtrl() | |
99 | { | |
100 | #if wxUSE_RICHEDIT | |
101 | m_isRich = FALSE; | |
102 | #endif | |
103 | } | |
104 | ||
105 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, | |
106 | const wxString& value, | |
107 | const wxPoint& pos, | |
108 | const wxSize& size, | |
109 | long style, | |
110 | const wxValidator& validator, | |
111 | const wxString& name) | |
112 | { | |
113 | // base initialization | |
114 | if ( !CreateBase(parent, id, pos, size, style, validator, name) ) | |
115 | return FALSE; | |
116 | ||
117 | if ( parent ) | |
118 | parent->AddChild(this); | |
119 | ||
120 | // set colours | |
121 | SetupColours(); | |
122 | ||
123 | // translate wxWin style flags to MSW ones, checking for consistency while | |
124 | // doing it | |
125 | long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP; | |
126 | if ( m_windowStyle & wxTE_MULTILINE ) | |
127 | { | |
128 | wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER), | |
129 | wxT("wxTE_PROCESS_ENTER style is ignored for multiline " | |
130 | "text controls (they always process it)") ); | |
131 | ||
132 | msStyle |= ES_MULTILINE | ES_WANTRETURN; | |
133 | if ((m_windowStyle & wxTE_NO_VSCROLL) == 0) | |
134 | msStyle |= WS_VSCROLL; | |
135 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
136 | } | |
137 | else | |
138 | msStyle |= ES_AUTOHSCROLL; | |
139 | ||
140 | if (m_windowStyle & wxHSCROLL) | |
141 | msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL); | |
142 | ||
143 | if (m_windowStyle & wxTE_READONLY) | |
144 | msStyle |= ES_READONLY; | |
145 | ||
146 | if (m_windowStyle & wxHSCROLL) | |
147 | msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL); | |
148 | if (m_windowStyle & wxTE_PASSWORD) // hidden input | |
149 | msStyle |= ES_PASSWORD; | |
150 | ||
151 | // we always want the characters and the arrows | |
152 | m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS; | |
153 | ||
154 | // we may have several different cases: | |
155 | // 1. normal case: both TAB and ENTER are used for dialog navigation | |
156 | // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next | |
157 | // control in the dialog | |
158 | // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation | |
159 | // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to | |
160 | // the next control | |
161 | if ( m_windowStyle & wxTE_PROCESS_ENTER ) | |
162 | m_lDlgCode |= DLGC_WANTMESSAGE; | |
163 | if ( m_windowStyle & wxTE_PROCESS_TAB ) | |
164 | m_lDlgCode |= DLGC_WANTTAB; | |
165 | ||
166 | // do create the control - either an EDIT or RICHEDIT | |
167 | const wxChar *windowClass = wxT("EDIT"); | |
168 | ||
169 | #if wxUSE_RICHEDIT | |
170 | if ( m_windowStyle & wxTE_RICH ) | |
171 | { | |
172 | static bool s_errorGiven = FALSE; // MT-FIXME | |
173 | ||
174 | // only give the error msg once if the DLL can't be loaded | |
175 | if ( !s_errorGiven ) | |
176 | { | |
177 | // first try to load the RichEdit DLL (will do nothing if already | |
178 | // done) | |
179 | if ( !wxTheApp->InitRichEdit() ) | |
180 | { | |
181 | wxLogError(_("Impossible to create a rich edit control, " | |
182 | "using simple text control instead.")); | |
183 | ||
184 | s_errorGiven = TRUE; | |
185 | } | |
186 | } | |
187 | ||
188 | if ( s_errorGiven ) | |
189 | { | |
190 | m_isRich = FALSE; | |
191 | } | |
192 | else | |
193 | { | |
194 | msStyle |= ES_AUTOVSCROLL; | |
195 | m_isRich = TRUE; | |
196 | windowClass = wxT("RICHEDIT"); | |
197 | } | |
198 | } | |
199 | else | |
200 | m_isRich = FALSE; | |
201 | #endif | |
202 | ||
203 | bool want3D; | |
204 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); | |
205 | ||
206 | // Even with extended styles, need to combine with WS_BORDER for them to | |
207 | // look right. | |
208 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) | |
209 | msStyle |= WS_BORDER; | |
210 | ||
211 | // NB: don't use pos and size as CreateWindowEx arguments because they | |
212 | // might be -1 in which case we should use the default values (and | |
213 | // SetSize called below takes care of it) | |
214 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, | |
215 | windowClass, | |
216 | NULL, | |
217 | msStyle, | |
218 | 0, 0, 0, 0, | |
219 | GetHwndOf(parent), | |
220 | (HMENU)m_windowId, | |
221 | wxGetInstance(), | |
222 | NULL); | |
223 | ||
224 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create text ctrl") ); | |
225 | ||
226 | #if wxUSE_CTL3D | |
227 | if ( want3D ) | |
228 | { | |
229 | Ctl3dSubclassCtl(GetHwnd()); | |
230 | m_useCtl3D = TRUE; | |
231 | } | |
232 | #endif | |
233 | ||
234 | #if wxUSE_RICHEDIT | |
235 | if (m_isRich) | |
236 | { | |
237 | // Have to enable events | |
238 | ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, | |
239 | ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE); | |
240 | } | |
241 | #endif | |
242 | ||
243 | SubclassWin(GetHWND()); | |
244 | ||
245 | // set font, position, size and initial value | |
246 | wxFont& fontParent = parent->GetFont(); | |
247 | if ( fontParent.Ok() ) | |
248 | { | |
249 | SetFont(fontParent); | |
250 | } | |
251 | else | |
252 | { | |
253 | SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT)); | |
254 | } | |
255 | ||
256 | // Causes a crash for Symantec C++ and WIN32 for some reason | |
257 | #if !(defined(__SC__) && defined(__WIN32__)) | |
258 | if ( !value.IsEmpty() ) | |
259 | { | |
260 | SetValue(value); | |
261 | } | |
262 | #endif | |
263 | ||
264 | SetSize(pos.x, pos.y, size.x, size.y); | |
265 | ||
266 | return TRUE; | |
267 | } | |
268 | ||
269 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
270 | void wxTextCtrl::AdoptAttributesFromHWND() | |
271 | { | |
272 | wxWindow::AdoptAttributesFromHWND(); | |
273 | ||
274 | HWND hWnd = GetHwnd(); | |
275 | long style = GetWindowLong(hWnd, GWL_STYLE); | |
276 | ||
277 | // retrieve the style to see whether this is an edit or richedit ctrl | |
278 | #if wxUSE_RICHEDIT | |
279 | wxChar buf[256]; | |
280 | ||
281 | GetClassName(hWnd, buf, WXSIZEOF(buf)); | |
282 | ||
283 | if ( wxStricmp(buf, wxT("EDIT")) == 0 ) | |
284 | m_isRich = FALSE; | |
285 | else | |
286 | m_isRich = TRUE; | |
287 | #endif // wxUSE_RICHEDIT | |
288 | ||
289 | if (style & ES_MULTILINE) | |
290 | m_windowStyle |= wxTE_MULTILINE; | |
291 | if (style & ES_PASSWORD) | |
292 | m_windowStyle |= wxTE_PASSWORD; | |
293 | if (style & ES_READONLY) | |
294 | m_windowStyle |= wxTE_READONLY; | |
295 | if (style & ES_WANTRETURN) | |
296 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
297 | } | |
298 | ||
299 | void wxTextCtrl::SetupColours() | |
300 | { | |
301 | // FIXME why is bg colour not inherited from parent? | |
302 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
303 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
304 | } | |
305 | ||
306 | // ---------------------------------------------------------------------------- | |
307 | // set/get the controls text | |
308 | // ---------------------------------------------------------------------------- | |
309 | ||
310 | wxString wxTextCtrl::GetValue() const | |
311 | { | |
312 | return wxGetWindowText(GetHWND()); | |
313 | } | |
314 | ||
315 | void wxTextCtrl::SetValue(const wxString& value) | |
316 | { | |
317 | wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); | |
318 | ||
319 | if ( valueDos != GetValue() ) | |
320 | { | |
321 | SetWindowText(GetHwnd(), valueDos); | |
322 | ||
323 | AdjustSpaceLimit(); | |
324 | } | |
325 | } | |
326 | ||
327 | void wxTextCtrl::WriteText(const wxString& value) | |
328 | { | |
329 | wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); | |
330 | ||
331 | SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str()); | |
332 | ||
333 | AdjustSpaceLimit(); | |
334 | } | |
335 | ||
336 | void wxTextCtrl::AppendText(const wxString& text) | |
337 | { | |
338 | SetInsertionPointEnd(); | |
339 | WriteText(text); | |
340 | } | |
341 | ||
342 | void wxTextCtrl::Clear() | |
343 | { | |
344 | SetWindowText(GetHwnd(), wxT("")); | |
345 | } | |
346 | ||
347 | // ---------------------------------------------------------------------------- | |
348 | // Clipboard operations | |
349 | // ---------------------------------------------------------------------------- | |
350 | ||
351 | void wxTextCtrl::Copy() | |
352 | { | |
353 | if (CanCopy()) | |
354 | { | |
355 | HWND hWnd = GetHwnd(); | |
356 | SendMessage(hWnd, WM_COPY, 0, 0L); | |
357 | } | |
358 | } | |
359 | ||
360 | void wxTextCtrl::Cut() | |
361 | { | |
362 | if (CanCut()) | |
363 | { | |
364 | HWND hWnd = GetHwnd(); | |
365 | SendMessage(hWnd, WM_CUT, 0, 0L); | |
366 | } | |
367 | } | |
368 | ||
369 | void wxTextCtrl::Paste() | |
370 | { | |
371 | if (CanPaste()) | |
372 | { | |
373 | HWND hWnd = GetHwnd(); | |
374 | SendMessage(hWnd, WM_PASTE, 0, 0L); | |
375 | } | |
376 | } | |
377 | ||
378 | bool wxTextCtrl::CanCopy() const | |
379 | { | |
380 | // Can copy if there's a selection | |
381 | long from, to; | |
382 | GetSelection(& from, & to); | |
383 | return (from != to); | |
384 | } | |
385 | ||
386 | bool wxTextCtrl::CanCut() const | |
387 | { | |
388 | // Can cut if there's a selection | |
389 | long from, to; | |
390 | GetSelection(& from, & to); | |
391 | return (from != to); | |
392 | } | |
393 | ||
394 | bool wxTextCtrl::CanPaste() const | |
395 | { | |
396 | #if wxUSE_RICHEDIT | |
397 | if (m_isRich) | |
398 | { | |
399 | int dataFormat = 0; // 0 == any format | |
400 | return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0); | |
401 | } | |
402 | #endif | |
403 | if (!IsEditable()) | |
404 | return FALSE; | |
405 | ||
406 | // Standard edit control: check for straight text on clipboard | |
407 | bool isTextAvailable = FALSE; | |
408 | if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) ) | |
409 | { | |
410 | isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0); | |
411 | ::CloseClipboard(); | |
412 | } | |
413 | ||
414 | return isTextAvailable; | |
415 | } | |
416 | ||
417 | // ---------------------------------------------------------------------------- | |
418 | // Accessors | |
419 | // ---------------------------------------------------------------------------- | |
420 | ||
421 | void wxTextCtrl::SetEditable(bool editable) | |
422 | { | |
423 | HWND hWnd = GetHwnd(); | |
424 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
425 | } | |
426 | ||
427 | void wxTextCtrl::SetInsertionPoint(long pos) | |
428 | { | |
429 | HWND hWnd = GetHwnd(); | |
430 | #ifdef __WIN32__ | |
431 | #if wxUSE_RICHEDIT | |
432 | if ( m_isRich) | |
433 | { | |
434 | CHARRANGE range; | |
435 | range.cpMin = pos; | |
436 | range.cpMax = pos; | |
437 | SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range); | |
438 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
439 | } | |
440 | else | |
441 | #endif // wxUSE_RICHEDIT | |
442 | { | |
443 | SendMessage(hWnd, EM_SETSEL, pos, pos); | |
444 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
445 | } | |
446 | #else // Win16 | |
447 | SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos)); | |
448 | #endif // Win32/16 | |
449 | ||
450 | static const char *nothing = ""; | |
451 | SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing); | |
452 | } | |
453 | ||
454 | void wxTextCtrl::SetInsertionPointEnd() | |
455 | { | |
456 | long pos = GetLastPosition(); | |
457 | SetInsertionPoint(pos); | |
458 | } | |
459 | ||
460 | long wxTextCtrl::GetInsertionPoint() const | |
461 | { | |
462 | #if wxUSE_RICHEDIT | |
463 | if (m_isRich) | |
464 | { | |
465 | CHARRANGE range; | |
466 | range.cpMin = 0; | |
467 | range.cpMax = 0; | |
468 | SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range); | |
469 | return range.cpMin; | |
470 | } | |
471 | #endif | |
472 | ||
473 | DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); | |
474 | return Pos & 0xFFFF; | |
475 | } | |
476 | ||
477 | long wxTextCtrl::GetLastPosition() const | |
478 | { | |
479 | HWND hWnd = GetHwnd(); | |
480 | ||
481 | // Will always return a number > 0 (according to docs) | |
482 | int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L); | |
483 | ||
484 | // This gets the char index for the _beginning_ of the last line | |
485 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L); | |
486 | ||
487 | // Get number of characters in the last line. We'll add this to the character | |
488 | // index for the last line, 1st position. | |
489 | int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L); | |
490 | ||
491 | return (long)(charIndex + lineLength); | |
492 | } | |
493 | ||
494 | // If the return values from and to are the same, there is no | |
495 | // selection. | |
496 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
497 | { | |
498 | #if wxUSE_RICHEDIT | |
499 | if (m_isRich) | |
500 | { | |
501 | CHARRANGE charRange; | |
502 | ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange); | |
503 | ||
504 | *from = charRange.cpMin; | |
505 | *to = charRange.cpMax; | |
506 | ||
507 | return; | |
508 | } | |
509 | #endif | |
510 | DWORD dwStart, dwEnd; | |
511 | WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position | |
512 | LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position | |
513 | ||
514 | ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam); | |
515 | ||
516 | *from = dwStart; | |
517 | *to = dwEnd; | |
518 | } | |
519 | ||
520 | bool wxTextCtrl::IsEditable() const | |
521 | { | |
522 | long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); | |
523 | ||
524 | return ((style & ES_READONLY) == 0); | |
525 | } | |
526 | ||
527 | // ---------------------------------------------------------------------------- | |
528 | // Editing | |
529 | // ---------------------------------------------------------------------------- | |
530 | ||
531 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
532 | { | |
533 | #if wxUSE_CLIPBOARD | |
534 | HWND hWnd = GetHwnd(); | |
535 | long fromChar = from; | |
536 | long toChar = to; | |
537 | ||
538 | // Set selection and remove it | |
539 | #ifdef __WIN32__ | |
540 | SendMessage(hWnd, EM_SETSEL, fromChar, toChar); | |
541 | #else | |
542 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
543 | #endif | |
544 | SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0); | |
545 | ||
546 | // Now replace with 'value', by pasting. | |
547 | wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0); | |
548 | ||
549 | // Paste into edit control | |
550 | SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
551 | #else | |
552 | wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0."); | |
553 | #endif | |
554 | } | |
555 | ||
556 | void wxTextCtrl::Remove(long from, long to) | |
557 | { | |
558 | HWND hWnd = GetHwnd(); | |
559 | long fromChar = from; | |
560 | long toChar = to; | |
561 | ||
562 | // Cut all selected text | |
563 | #ifdef __WIN32__ | |
564 | SendMessage(hWnd, EM_SETSEL, fromChar, toChar); | |
565 | #else | |
566 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
567 | #endif | |
568 | SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0); | |
569 | } | |
570 | ||
571 | void wxTextCtrl::SetSelection(long from, long to) | |
572 | { | |
573 | HWND hWnd = GetHwnd(); | |
574 | long fromChar = from; | |
575 | long toChar = to; | |
576 | ||
577 | // if from and to are both -1, it means (in wxWindows) that all text should | |
578 | // be selected. Translate into Windows convention | |
579 | if ((from == -1) && (to == -1)) | |
580 | { | |
581 | fromChar = 0; | |
582 | toChar = -1; | |
583 | } | |
584 | ||
585 | #ifdef __WIN32__ | |
586 | SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar); | |
587 | SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0); | |
588 | #else | |
589 | // WPARAM is 0: selection is scrolled into view | |
590 | SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar)); | |
591 | #endif | |
592 | } | |
593 | ||
594 | bool wxTextCtrl::LoadFile(const wxString& file) | |
595 | { | |
596 | if ( wxTextCtrlBase::LoadFile(file) ) | |
597 | { | |
598 | // update the size limit if needed | |
599 | AdjustSpaceLimit(); | |
600 | ||
601 | return TRUE; | |
602 | } | |
603 | ||
604 | return FALSE; | |
605 | } | |
606 | ||
607 | bool wxTextCtrl::IsModified() const | |
608 | { | |
609 | return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0); | |
610 | } | |
611 | ||
612 | // Makes 'unmodified' | |
613 | void wxTextCtrl::DiscardEdits() | |
614 | { | |
615 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); | |
616 | } | |
617 | ||
618 | int wxTextCtrl::GetNumberOfLines() const | |
619 | { | |
620 | return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); | |
621 | } | |
622 | ||
623 | long wxTextCtrl::XYToPosition(long x, long y) const | |
624 | { | |
625 | HWND hWnd = GetHwnd(); | |
626 | ||
627 | // This gets the char index for the _beginning_ of this line | |
628 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0); | |
629 | return (long)(x + charIndex); | |
630 | } | |
631 | ||
632 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
633 | { | |
634 | HWND hWnd = GetHwnd(); | |
635 | ||
636 | // This gets the line number containing the character | |
637 | int lineNo; | |
638 | #if wxUSE_RICHEDIT | |
639 | if ( m_isRich ) | |
640 | { | |
641 | lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos); | |
642 | } | |
643 | else | |
644 | #endif // wxUSE_RICHEDIT | |
645 | lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0); | |
646 | ||
647 | if ( lineNo == -1 ) | |
648 | { | |
649 | // no such line | |
650 | return FALSE; | |
651 | } | |
652 | ||
653 | // This gets the char index for the _beginning_ of this line | |
654 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); | |
655 | if ( charIndex == -1 ) | |
656 | { | |
657 | return FALSE; | |
658 | } | |
659 | ||
660 | // The X position must therefore be the different between pos and charIndex | |
661 | if ( x ) | |
662 | *x = (long)(pos - charIndex); | |
663 | if ( y ) | |
664 | *y = (long)lineNo; | |
665 | ||
666 | return TRUE; | |
667 | } | |
668 | ||
669 | void wxTextCtrl::ShowPosition(long pos) | |
670 | { | |
671 | HWND hWnd = GetHwnd(); | |
672 | ||
673 | // To scroll to a position, we pass the number of lines and characters | |
674 | // to scroll *by*. This means that we need to: | |
675 | // (1) Find the line position of the current line. | |
676 | // (2) Find the line position of pos. | |
677 | // (3) Scroll by (pos - current). | |
678 | // For now, ignore the horizontal scrolling. | |
679 | ||
680 | // Is this where scrolling is relative to - the line containing the caret? | |
681 | // Or is the first visible line??? Try first visible line. | |
682 | // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L); | |
683 | ||
684 | int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L); | |
685 | ||
686 | int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L); | |
687 | ||
688 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; | |
689 | ||
690 | if (linesToScroll != 0) | |
691 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); | |
692 | } | |
693 | ||
694 | int wxTextCtrl::GetLineLength(long lineNo) const | |
695 | { | |
696 | long charIndex = XYToPosition(0, lineNo); | |
697 | int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0); | |
698 | return len; | |
699 | } | |
700 | ||
701 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
702 | { | |
703 | size_t len = (size_t)GetLineLength(lineNo) + 1; | |
704 | char *buf = (char *)malloc(len); | |
705 | *(WORD *)buf = len; | |
706 | int noChars = (int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf); | |
707 | buf[noChars] = 0; | |
708 | ||
709 | wxString str(buf); | |
710 | ||
711 | free(buf); | |
712 | ||
713 | return str; | |
714 | } | |
715 | ||
716 | // ---------------------------------------------------------------------------- | |
717 | // Undo/redo | |
718 | // ---------------------------------------------------------------------------- | |
719 | ||
720 | void wxTextCtrl::Undo() | |
721 | { | |
722 | if (CanUndo()) | |
723 | { | |
724 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); | |
725 | } | |
726 | } | |
727 | ||
728 | void wxTextCtrl::Redo() | |
729 | { | |
730 | if (CanRedo()) | |
731 | { | |
732 | // Same as Undo, since Undo undoes the undo, i.e. a redo. | |
733 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); | |
734 | } | |
735 | } | |
736 | ||
737 | bool wxTextCtrl::CanUndo() const | |
738 | { | |
739 | return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0); | |
740 | } | |
741 | ||
742 | bool wxTextCtrl::CanRedo() const | |
743 | { | |
744 | return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0); | |
745 | } | |
746 | ||
747 | // ---------------------------------------------------------------------------- | |
748 | // implemenation details | |
749 | // ---------------------------------------------------------------------------- | |
750 | ||
751 | void wxTextCtrl::Command(wxCommandEvent & event) | |
752 | { | |
753 | SetValue(event.GetString()); | |
754 | ProcessCommand (event); | |
755 | } | |
756 | ||
757 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
758 | { | |
759 | // By default, load the first file into the text window. | |
760 | if (event.GetNumberOfFiles() > 0) | |
761 | { | |
762 | LoadFile(event.GetFiles()[0]); | |
763 | } | |
764 | } | |
765 | ||
766 | void wxTextCtrl::OnChar(wxKeyEvent& event) | |
767 | { | |
768 | switch ( event.KeyCode() ) | |
769 | { | |
770 | case WXK_RETURN: | |
771 | if ( !(m_windowStyle & wxTE_MULTILINE) ) | |
772 | { | |
773 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
774 | event.SetEventObject( this ); | |
775 | if ( GetEventHandler()->ProcessEvent(event) ) | |
776 | return; | |
777 | } | |
778 | //else: multiline controls need Enter for themselves | |
779 | ||
780 | break; | |
781 | ||
782 | case WXK_TAB: | |
783 | // always produce navigation event - even if we process TAB | |
784 | // ourselves the fact that we got here means that the user code | |
785 | // decided to skip processing of this TAB - probably to let it | |
786 | // do its default job. | |
787 | // | |
788 | // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is | |
789 | // handled by Windows | |
790 | { | |
791 | wxNavigationKeyEvent eventNav; | |
792 | eventNav.SetDirection(!event.ShiftDown()); | |
793 | eventNav.SetWindowChange(FALSE); | |
794 | eventNav.SetEventObject(this); | |
795 | ||
796 | if ( GetEventHandler()->ProcessEvent(eventNav) ) | |
797 | return; | |
798 | } | |
799 | break; | |
800 | ||
801 | default: | |
802 | event.Skip(); | |
803 | return; | |
804 | } | |
805 | ||
806 | // don't just call event.Skip() because this will cause TABs and ENTERs | |
807 | // be passed upwards and we don't always want this - instead process it | |
808 | // right here | |
809 | ||
810 | // FIXME | |
811 | event.Skip(); | |
812 | } | |
813 | ||
814 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
815 | { | |
816 | switch (param) | |
817 | { | |
818 | case EN_SETFOCUS: | |
819 | case EN_KILLFOCUS: | |
820 | { | |
821 | wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS | |
822 | : wxEVT_SET_FOCUS, | |
823 | m_windowId); | |
824 | event.SetEventObject( this ); | |
825 | GetEventHandler()->ProcessEvent(event); | |
826 | } | |
827 | break; | |
828 | ||
829 | case EN_CHANGE: | |
830 | { | |
831 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); | |
832 | wxString val(GetValue()); | |
833 | if ( !val.IsNull() ) | |
834 | event.m_commandString = WXSTRINGCAST val; | |
835 | event.SetEventObject( this ); | |
836 | ProcessCommand(event); | |
837 | } | |
838 | break; | |
839 | ||
840 | case EN_ERRSPACE: | |
841 | // the text size limit has been hit - increase it | |
842 | AdjustSpaceLimit(); | |
843 | break; | |
844 | ||
845 | // the other notification messages are not processed | |
846 | case EN_UPDATE: | |
847 | case EN_MAXTEXT: | |
848 | case EN_HSCROLL: | |
849 | case EN_VSCROLL: | |
850 | default: | |
851 | return FALSE; | |
852 | } | |
853 | ||
854 | // processed | |
855 | return TRUE; | |
856 | } | |
857 | ||
858 | void wxTextCtrl::AdjustSpaceLimit() | |
859 | { | |
860 | #ifndef __WIN16__ | |
861 | unsigned int len = ::GetWindowTextLength(GetHwnd()), | |
862 | limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0); | |
863 | if ( len >= limit ) | |
864 | { | |
865 | limit = len + 0x8000; // 32Kb | |
866 | ||
867 | #if wxUSE_RICHEDIT | |
868 | if ( m_isRich || limit > 0xffff ) | |
869 | #else | |
870 | if ( limit > 0xffff ) | |
871 | #endif | |
872 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, 0, limit); | |
873 | else | |
874 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0); | |
875 | } | |
876 | #endif | |
877 | } | |
878 | ||
879 | bool wxTextCtrl::AcceptsFocus() const | |
880 | { | |
881 | // we don't want focus if we can't be edited | |
882 | return IsEditable() && wxControl::AcceptsFocus(); | |
883 | } | |
884 | ||
885 | wxSize wxTextCtrl::DoGetBestSize() const | |
886 | { | |
887 | int cx, cy; | |
888 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
889 | ||
890 | int wText = DEFAULT_ITEM_WIDTH; | |
891 | ||
892 | int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
893 | if ( m_windowStyle & wxTE_MULTILINE ) | |
894 | { | |
895 | hText *= wxMin(GetNumberOfLines(), 5); | |
896 | } | |
897 | //else: for single line control everything is ok | |
898 | ||
899 | return wxSize(wText, hText); | |
900 | } | |
901 | ||
902 | // ---------------------------------------------------------------------------- | |
903 | // standard handlers for standard edit menu events | |
904 | // ---------------------------------------------------------------------------- | |
905 | ||
906 | void wxTextCtrl::OnCut(wxCommandEvent& event) | |
907 | { | |
908 | Cut(); | |
909 | } | |
910 | ||
911 | void wxTextCtrl::OnCopy(wxCommandEvent& event) | |
912 | { | |
913 | Copy(); | |
914 | } | |
915 | ||
916 | void wxTextCtrl::OnPaste(wxCommandEvent& event) | |
917 | { | |
918 | Paste(); | |
919 | } | |
920 | ||
921 | void wxTextCtrl::OnUndo(wxCommandEvent& event) | |
922 | { | |
923 | Undo(); | |
924 | } | |
925 | ||
926 | void wxTextCtrl::OnRedo(wxCommandEvent& event) | |
927 | { | |
928 | Redo(); | |
929 | } | |
930 | ||
931 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
932 | { | |
933 | event.Enable( CanCut() ); | |
934 | } | |
935 | ||
936 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
937 | { | |
938 | event.Enable( CanCopy() ); | |
939 | } | |
940 | ||
941 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
942 | { | |
943 | event.Enable( CanPaste() ); | |
944 | } | |
945 | ||
946 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
947 | { | |
948 | event.Enable( CanUndo() ); | |
949 | } | |
950 | ||
951 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
952 | { | |
953 | event.Enable( CanRedo() ); | |
954 | } | |
955 |