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