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