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