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