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 | |
0d0512bd | 60 | #if wxUSE_RICHEDIT |
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 | if ( parent ) |
119 | parent->AddChild(this); | |
2bda0e17 | 120 | |
a1b82138 VZ |
121 | // set colours |
122 | SetupColours(); | |
2bda0e17 | 123 | |
a1b82138 VZ |
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), | |
223d09f6 | 130 | wxT("wxTE_PROCESS_ENTER style is ignored for multiline " |
0d0512bd | 131 | "text controls (they always process it)") ); |
5fb9fcfc | 132 | |
b70ababc JS |
133 | msStyle |= ES_MULTILINE | ES_WANTRETURN; |
134 | if ((m_windowStyle & wxTE_NO_VSCROLL) == 0) | |
135 | msStyle |= WS_VSCROLL; | |
a1b82138 VZ |
136 | m_windowStyle |= wxTE_PROCESS_ENTER; |
137 | } | |
138 | else | |
139 | msStyle |= ES_AUTOHSCROLL; | |
2bda0e17 | 140 | |
2c738dd8 UM |
141 | if (m_windowStyle & wxHSCROLL) |
142 | msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL); | |
143 | ||
a1b82138 VZ |
144 | if (m_windowStyle & wxTE_READONLY) |
145 | msStyle |= ES_READONLY; | |
2bda0e17 | 146 | |
a1b82138 VZ |
147 | if (m_windowStyle & wxHSCROLL) |
148 | msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL); | |
149 | if (m_windowStyle & wxTE_PASSWORD) // hidden input | |
150 | msStyle |= ES_PASSWORD; | |
2bda0e17 | 151 | |
101f488c VZ |
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 | ||
a1b82138 | 167 | // do create the control - either an EDIT or RICHEDIT |
223d09f6 | 168 | const wxChar *windowClass = wxT("EDIT"); |
cd471848 | 169 | |
57c208c5 | 170 | #if wxUSE_RICHEDIT |
c49245f8 | 171 | if ( m_windowStyle & wxTE_RICH ) |
a1b82138 | 172 | { |
0d0512bd VZ |
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 | } | |
a1b82138 VZ |
199 | } |
200 | else | |
201 | m_isRich = FALSE; | |
cd471848 | 202 | #endif |
2bda0e17 | 203 | |
a1b82138 VZ |
204 | bool want3D; |
205 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); | |
2bda0e17 | 206 | |
a1b82138 VZ |
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; | |
2bda0e17 | 211 | |
a1b82138 VZ |
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); | |
2bda0e17 | 224 | |
223d09f6 | 225 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create text ctrl") ); |
c085e333 | 226 | |
1f112209 | 227 | #if wxUSE_CTL3D |
a1b82138 VZ |
228 | if ( want3D ) |
229 | { | |
230 | Ctl3dSubclassCtl(GetHwnd()); | |
231 | m_useCtl3D = TRUE; | |
232 | } | |
2bda0e17 KB |
233 | #endif |
234 | ||
57c208c5 | 235 | #if wxUSE_RICHEDIT |
a1b82138 VZ |
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 | } | |
2bda0e17 KB |
242 | #endif |
243 | ||
a1b82138 | 244 | SubclassWin(GetHWND()); |
2bda0e17 | 245 | |
a1b82138 VZ |
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 | } | |
2bda0e17 | 256 | |
a1b82138 | 257 | // Causes a crash for Symantec C++ and WIN32 for some reason |
2bda0e17 | 258 | #if !(defined(__SC__) && defined(__WIN32__)) |
a1b82138 VZ |
259 | if ( !value.IsEmpty() ) |
260 | { | |
261 | SetValue(value); | |
262 | } | |
2bda0e17 KB |
263 | #endif |
264 | ||
a1b82138 VZ |
265 | SetSize(pos.x, pos.y, size.x, size.y); |
266 | ||
267 | return TRUE; | |
2bda0e17 KB |
268 | } |
269 | ||
270 | // Make sure the window style (etc.) reflects the HWND style (roughly) | |
cd471848 | 271 | void wxTextCtrl::AdoptAttributesFromHWND() |
2bda0e17 | 272 | { |
c085e333 | 273 | wxWindow::AdoptAttributesFromHWND(); |
2bda0e17 | 274 | |
789295bf | 275 | HWND hWnd = GetHwnd(); |
a1b82138 | 276 | long style = GetWindowLong(hWnd, GWL_STYLE); |
2bda0e17 | 277 | |
cd471848 VZ |
278 | // retrieve the style to see whether this is an edit or richedit ctrl |
279 | #if wxUSE_RICHEDIT | |
837e5743 | 280 | wxChar buf[256]; |
2bda0e17 | 281 | |
a1b82138 | 282 | GetClassName(hWnd, buf, WXSIZEOF(buf)); |
2bda0e17 | 283 | |
223d09f6 | 284 | if ( wxStricmp(buf, wxT("EDIT")) == 0 ) |
c085e333 VZ |
285 | m_isRich = FALSE; |
286 | else | |
287 | m_isRich = TRUE; | |
a1b82138 | 288 | #endif // wxUSE_RICHEDIT |
c085e333 VZ |
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; | |
2bda0e17 KB |
298 | } |
299 | ||
cd471848 | 300 | void wxTextCtrl::SetupColours() |
2bda0e17 | 301 | { |
a1b82138 VZ |
302 | // FIXME why is bg colour not inherited from parent? |
303 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
304 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
2bda0e17 KB |
305 | } |
306 | ||
a1b82138 VZ |
307 | // ---------------------------------------------------------------------------- |
308 | // set/get the controls text | |
309 | // ---------------------------------------------------------------------------- | |
310 | ||
cd471848 | 311 | wxString wxTextCtrl::GetValue() const |
2bda0e17 | 312 | { |
789295bf | 313 | return wxGetWindowText(GetHWND()); |
2bda0e17 KB |
314 | } |
315 | ||
316 | void wxTextCtrl::SetValue(const wxString& value) | |
317 | { | |
a1b82138 | 318 | wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); |
789295bf | 319 | |
07cf98cb VZ |
320 | if ( valueDos != GetValue() ) |
321 | { | |
322 | SetWindowText(GetHwnd(), valueDos); | |
a1b82138 | 323 | |
07cf98cb VZ |
324 | AdjustSpaceLimit(); |
325 | } | |
2bda0e17 KB |
326 | } |
327 | ||
a1b82138 | 328 | void wxTextCtrl::WriteText(const wxString& value) |
2bda0e17 | 329 | { |
a1b82138 | 330 | wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos); |
2bda0e17 | 331 | |
a1b82138 | 332 | SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str()); |
2bda0e17 | 333 | |
a1b82138 | 334 | AdjustSpaceLimit(); |
2bda0e17 KB |
335 | } |
336 | ||
a1b82138 VZ |
337 | void wxTextCtrl::AppendText(const wxString& text) |
338 | { | |
339 | SetInsertionPointEnd(); | |
340 | WriteText(text); | |
341 | } | |
342 | ||
343 | void wxTextCtrl::Clear() | |
344 | { | |
223d09f6 | 345 | SetWindowText(GetHwnd(), wxT("")); |
a1b82138 VZ |
346 | } |
347 | ||
348 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 349 | // Clipboard operations |
a1b82138 VZ |
350 | // ---------------------------------------------------------------------------- |
351 | ||
cd471848 | 352 | void wxTextCtrl::Copy() |
2bda0e17 | 353 | { |
e702ff0f JS |
354 | if (CanCopy()) |
355 | { | |
789295bf | 356 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
357 | SendMessage(hWnd, WM_COPY, 0, 0L); |
358 | } | |
2bda0e17 KB |
359 | } |
360 | ||
cd471848 | 361 | void wxTextCtrl::Cut() |
2bda0e17 | 362 | { |
e702ff0f JS |
363 | if (CanCut()) |
364 | { | |
789295bf | 365 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
366 | SendMessage(hWnd, WM_CUT, 0, 0L); |
367 | } | |
2bda0e17 KB |
368 | } |
369 | ||
cd471848 | 370 | void wxTextCtrl::Paste() |
2bda0e17 | 371 | { |
e702ff0f JS |
372 | if (CanPaste()) |
373 | { | |
789295bf | 374 | HWND hWnd = GetHwnd(); |
e702ff0f JS |
375 | SendMessage(hWnd, WM_PASTE, 0, 0L); |
376 | } | |
2bda0e17 KB |
377 | } |
378 | ||
a1b82138 VZ |
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 | ||
debe6624 | 422 | void wxTextCtrl::SetEditable(bool editable) |
2bda0e17 | 423 | { |
a1b82138 VZ |
424 | HWND hWnd = GetHwnd(); |
425 | SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L); | |
2bda0e17 KB |
426 | } |
427 | ||
debe6624 | 428 | void wxTextCtrl::SetInsertionPoint(long pos) |
2bda0e17 | 429 | { |
a1b82138 | 430 | HWND hWnd = GetHwnd(); |
2bda0e17 | 431 | #ifdef __WIN32__ |
57c208c5 | 432 | #if wxUSE_RICHEDIT |
a1b82138 VZ |
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); | |
2bda0e17 KB |
453 | } |
454 | ||
cd471848 | 455 | void wxTextCtrl::SetInsertionPointEnd() |
2bda0e17 | 456 | { |
a1b82138 VZ |
457 | long pos = GetLastPosition(); |
458 | SetInsertionPoint(pos); | |
2bda0e17 KB |
459 | } |
460 | ||
cd471848 | 461 | long wxTextCtrl::GetInsertionPoint() const |
2bda0e17 | 462 | { |
57c208c5 | 463 | #if wxUSE_RICHEDIT |
a1b82138 VZ |
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 | } | |
2bda0e17 KB |
472 | #endif |
473 | ||
a1b82138 VZ |
474 | DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L); |
475 | return Pos & 0xFFFF; | |
2bda0e17 KB |
476 | } |
477 | ||
cd471848 | 478 | long wxTextCtrl::GetLastPosition() const |
2bda0e17 | 479 | { |
789295bf | 480 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
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); | |
39136494 | 487 | |
2bda0e17 KB |
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 | ||
a1b82138 VZ |
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 | ||
debe6624 | 532 | void wxTextCtrl::Replace(long from, long to, const wxString& value) |
2bda0e17 | 533 | { |
acbd13a3 | 534 | #if wxUSE_CLIPBOARD |
789295bf | 535 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
536 | long fromChar = from; |
537 | long toChar = to; | |
39136494 | 538 | |
2bda0e17 KB |
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. | |
837e5743 | 548 | wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0); |
2bda0e17 KB |
549 | |
550 | // Paste into edit control | |
551 | SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L); | |
acbd13a3 JS |
552 | #else |
553 | wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0."); | |
554 | #endif | |
2bda0e17 KB |
555 | } |
556 | ||
debe6624 | 557 | void wxTextCtrl::Remove(long from, long to) |
2bda0e17 | 558 | { |
789295bf | 559 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
560 | long fromChar = from; |
561 | long toChar = to; | |
39136494 | 562 | |
2bda0e17 KB |
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 | ||
debe6624 | 572 | void wxTextCtrl::SetSelection(long from, long to) |
2bda0e17 | 573 | { |
789295bf | 574 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
575 | long fromChar = from; |
576 | long toChar = to; | |
a1b82138 VZ |
577 | |
578 | // if from and to are both -1, it means (in wxWindows) that all text should | |
579 | // be selected. Translate into Windows convention | |
2bda0e17 KB |
580 | if ((from == -1) && (to == -1)) |
581 | { | |
582 | fromChar = 0; | |
583 | toChar = -1; | |
584 | } | |
39136494 | 585 | |
2bda0e17 KB |
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 | { | |
a1b82138 | 597 | if ( wxTextCtrlBase::LoadFile(file) ) |
cd471848 | 598 | { |
a1b82138 VZ |
599 | // update the size limit if needed |
600 | AdjustSpaceLimit(); | |
2bda0e17 | 601 | |
a1b82138 | 602 | return TRUE; |
2bda0e17 | 603 | } |
2bda0e17 | 604 | |
a1b82138 | 605 | return FALSE; |
2bda0e17 KB |
606 | } |
607 | ||
cd471848 | 608 | bool wxTextCtrl::IsModified() const |
2bda0e17 | 609 | { |
789295bf | 610 | return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0); |
2bda0e17 KB |
611 | } |
612 | ||
613 | // Makes 'unmodified' | |
cd471848 | 614 | void wxTextCtrl::DiscardEdits() |
2bda0e17 | 615 | { |
a1b82138 | 616 | SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L); |
2bda0e17 KB |
617 | } |
618 | ||
cd471848 | 619 | int wxTextCtrl::GetNumberOfLines() const |
2bda0e17 | 620 | { |
789295bf | 621 | return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); |
2bda0e17 KB |
622 | } |
623 | ||
debe6624 | 624 | long wxTextCtrl::XYToPosition(long x, long y) const |
2bda0e17 | 625 | { |
789295bf | 626 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
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 | ||
0efe5ba7 | 633 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
2bda0e17 | 634 | { |
789295bf | 635 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
636 | |
637 | // This gets the line number containing the character | |
0efe5ba7 VZ |
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 | ||
2bda0e17 KB |
654 | // This gets the char index for the _beginning_ of this line |
655 | int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0); | |
0efe5ba7 VZ |
656 | if ( charIndex == -1 ) |
657 | { | |
658 | return FALSE; | |
659 | } | |
660 | ||
2bda0e17 | 661 | // The X position must therefore be the different between pos and charIndex |
0efe5ba7 VZ |
662 | if ( x ) |
663 | *x = (long)(pos - charIndex); | |
664 | if ( y ) | |
665 | *y = (long)lineNo; | |
666 | ||
667 | return TRUE; | |
2bda0e17 KB |
668 | } |
669 | ||
debe6624 | 670 | void wxTextCtrl::ShowPosition(long pos) |
2bda0e17 | 671 | { |
789295bf | 672 | HWND hWnd = GetHwnd(); |
2bda0e17 KB |
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); | |
39136494 | 688 | |
2bda0e17 KB |
689 | int linesToScroll = specifiedLineLineNo - currentLineLineNo; |
690 | ||
2bda0e17 | 691 | if (linesToScroll != 0) |
de4d7713 | 692 | (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll); |
2bda0e17 KB |
693 | } |
694 | ||
debe6624 | 695 | int wxTextCtrl::GetLineLength(long lineNo) const |
2bda0e17 KB |
696 | { |
697 | long charIndex = XYToPosition(0, lineNo); | |
4438caf4 | 698 | int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0); |
2bda0e17 KB |
699 | return len; |
700 | } | |
701 | ||
debe6624 | 702 | wxString wxTextCtrl::GetLineText(long lineNo) const |
2bda0e17 | 703 | { |
a1b82138 | 704 | size_t len = (size_t)GetLineLength(lineNo) + 1; |
4438caf4 VZ |
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; | |
2bda0e17 KB |
715 | } |
716 | ||
a1b82138 | 717 | // ---------------------------------------------------------------------------- |
ca8b28f2 | 718 | // Undo/redo |
a1b82138 VZ |
719 | // ---------------------------------------------------------------------------- |
720 | ||
ca8b28f2 JS |
721 | void wxTextCtrl::Undo() |
722 | { | |
723 | if (CanUndo()) | |
724 | { | |
789295bf | 725 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
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. | |
789295bf | 734 | ::SendMessage(GetHwnd(), EM_UNDO, 0, 0); |
ca8b28f2 JS |
735 | } |
736 | } | |
737 | ||
738 | bool wxTextCtrl::CanUndo() const | |
739 | { | |
789295bf | 740 | return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0); |
ca8b28f2 JS |
741 | } |
742 | ||
743 | bool wxTextCtrl::CanRedo() const | |
744 | { | |
789295bf | 745 | return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0); |
ca8b28f2 JS |
746 | } |
747 | ||
a1b82138 VZ |
748 | // ---------------------------------------------------------------------------- |
749 | // implemenation details | |
750 | // ---------------------------------------------------------------------------- | |
39136494 | 751 | |
2bda0e17 KB |
752 | void wxTextCtrl::Command(wxCommandEvent & event) |
753 | { | |
a1b82138 VZ |
754 | SetValue(event.GetString()); |
755 | ProcessCommand (event); | |
2bda0e17 KB |
756 | } |
757 | ||
758 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
759 | { | |
a1b82138 VZ |
760 | // By default, load the first file into the text window. |
761 | if (event.GetNumberOfFiles() > 0) | |
762 | { | |
763 | LoadFile(event.GetFiles()[0]); | |
764 | } | |
2bda0e17 KB |
765 | } |
766 | ||
debe6624 | 767 | WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
a1b82138 VZ |
768 | WXUINT message, WXWPARAM wParam, |
769 | WXLPARAM lParam) | |
2bda0e17 | 770 | { |
1f112209 | 771 | #if wxUSE_CTL3D |
a1b82138 VZ |
772 | if ( m_useCtl3D ) |
773 | { | |
774 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
775 | return (WXHBRUSH) hbrush; | |
776 | } | |
2bda0e17 KB |
777 | #endif |
778 | ||
a1b82138 VZ |
779 | HDC hdc = (HDC)pDC; |
780 | SetBkMode(hdc, GetParent()->GetTransparentBackground() ? TRANSPARENT | |
781 | : OPAQUE); | |
2bda0e17 | 782 | |
a1b82138 VZ |
783 | ::SetBkColor(hdc, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); |
784 | ::SetTextColor(hdc, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
2bda0e17 | 785 | |
a1b82138 | 786 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); |
2bda0e17 | 787 | |
a1b82138 | 788 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); |
2bda0e17 KB |
789 | } |
790 | ||
791 | void wxTextCtrl::OnChar(wxKeyEvent& event) | |
792 | { | |
42e69d6b | 793 | switch ( event.KeyCode() ) |
cd471848 | 794 | { |
cd471848 | 795 | case WXK_RETURN: |
39136494 | 796 | if ( !(m_windowStyle & wxTE_MULTILINE) ) |
cd471848 VZ |
797 | { |
798 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
799 | event.SetEventObject( this ); | |
800 | if ( GetEventHandler()->ProcessEvent(event) ) | |
801 | return; | |
802 | } | |
5fb9fcfc VZ |
803 | //else: multiline controls need Enter for themselves |
804 | ||
805 | break; | |
4d91c1d1 | 806 | |
cd471848 | 807 | case WXK_TAB: |
319fefa9 VZ |
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. | |
5fb9fcfc VZ |
812 | // |
813 | // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is | |
814 | // handled by Windows | |
cd471848 | 815 | { |
5fb9fcfc VZ |
816 | wxNavigationKeyEvent eventNav; |
817 | eventNav.SetDirection(!event.ShiftDown()); | |
818 | eventNav.SetWindowChange(FALSE); | |
819 | eventNav.SetEventObject(this); | |
39136494 | 820 | |
5fb9fcfc | 821 | if ( GetEventHandler()->ProcessEvent(eventNav) ) |
cd471848 VZ |
822 | return; |
823 | } | |
341c92a8 | 824 | break; |
4d91c1d1 VZ |
825 | |
826 | default: | |
827 | event.Skip(); | |
39136494 | 828 | return; |
cd471848 | 829 | } |
39136494 | 830 | |
cd471848 VZ |
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 | |
42e69d6b VZ |
834 | |
835 | // FIXME | |
836 | event.Skip(); | |
2bda0e17 KB |
837 | } |
838 | ||
debe6624 | 839 | bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 840 | { |
789295bf VZ |
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; | |
ae29de83 | 853 | |
789295bf VZ |
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; | |
2bda0e17 | 864 | |
789295bf VZ |
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; | |
2bda0e17 KB |
881 | } |
882 | ||
789295bf VZ |
883 | void wxTextCtrl::AdjustSpaceLimit() |
884 | { | |
25889d3c | 885 | #ifndef __WIN16__ |
789295bf VZ |
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 | ||
5ea105e0 | 892 | #if wxUSE_RICHEDIT |
789295bf | 893 | if ( m_isRich || limit > 0xffff ) |
5ea105e0 RR |
894 | #else |
895 | if ( limit > 0xffff ) | |
896 | #endif | |
789295bf VZ |
897 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, 0, limit); |
898 | else | |
899 | ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0); | |
900 | } | |
25889d3c | 901 | #endif |
789295bf | 902 | } |
2bda0e17 | 903 | |
a1b82138 | 904 | bool wxTextCtrl::AcceptsFocus() const |
2bda0e17 | 905 | { |
a1b82138 VZ |
906 | // we don't want focus if we can't be edited |
907 | return IsEditable() && wxControl::AcceptsFocus(); | |
908 | } | |
c085e333 | 909 | |
f68586e5 | 910 | wxSize wxTextCtrl::DoGetBestSize() const |
a1b82138 VZ |
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); | |
2bda0e17 | 925 | } |
a1b82138 VZ |
926 | |
927 | // ---------------------------------------------------------------------------- | |
928 | // standard handlers for standard edit menu events | |
929 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 930 | |
e702ff0f JS |
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 |