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