]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
unused parameter warnings suppressed
[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
b70ababc
JS
134 msStyle |= ES_MULTILINE | ES_WANTRETURN;
135 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
136 msStyle |= WS_VSCROLL;
a1b82138
VZ
137 m_windowStyle |= wxTE_PROCESS_ENTER;
138 }
139 else
140 msStyle |= ES_AUTOHSCROLL;
2bda0e17 141
2c738dd8
UM
142 if (m_windowStyle & wxHSCROLL)
143 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
144
a1b82138
VZ
145 if (m_windowStyle & wxTE_READONLY)
146 msStyle |= ES_READONLY;
2bda0e17 147
a1b82138
VZ
148 if (m_windowStyle & wxHSCROLL)
149 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
150 if (m_windowStyle & wxTE_PASSWORD) // hidden input
151 msStyle |= ES_PASSWORD;
2bda0e17 152
101f488c
VZ
153 // we always want the characters and the arrows
154 m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
155
156 // we may have several different cases:
157 // 1. normal case: both TAB and ENTER are used for dialog navigation
158 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
159 // control in the dialog
160 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
161 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
162 // the next control
163 if ( m_windowStyle & wxTE_PROCESS_ENTER )
164 m_lDlgCode |= DLGC_WANTMESSAGE;
165 if ( m_windowStyle & wxTE_PROCESS_TAB )
166 m_lDlgCode |= DLGC_WANTTAB;
167
a1b82138
VZ
168 // do create the control - either an EDIT or RICHEDIT
169 const wxChar *windowClass = _T("EDIT");
cd471848 170
57c208c5 171#if wxUSE_RICHEDIT
c49245f8 172 if ( m_windowStyle & wxTE_RICH )
a1b82138
VZ
173 {
174 msStyle |= ES_AUTOVSCROLL;
175 m_isRich = TRUE;
176 windowClass = _T("RICHEDIT");
177 }
178 else
179 m_isRich = FALSE;
cd471848 180#endif
2bda0e17 181
a1b82138
VZ
182 bool want3D;
183 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
2bda0e17 184
a1b82138
VZ
185 // Even with extended styles, need to combine with WS_BORDER for them to
186 // look right.
187 if ( want3D || wxStyleHasBorder(m_windowStyle) )
188 msStyle |= WS_BORDER;
2bda0e17 189
a1b82138
VZ
190 // NB: don't use pos and size as CreateWindowEx arguments because they
191 // might be -1 in which case we should use the default values (and
192 // SetSize called below takes care of it)
193 m_hWnd = (WXHWND)::CreateWindowEx(exStyle,
194 windowClass,
195 NULL,
196 msStyle,
197 0, 0, 0, 0,
198 GetHwndOf(parent),
199 (HMENU)m_windowId,
200 wxGetInstance(),
201 NULL);
2bda0e17 202
a1b82138 203 wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create text ctrl") );
c085e333 204
1f112209 205#if wxUSE_CTL3D
a1b82138
VZ
206 if ( want3D )
207 {
208 Ctl3dSubclassCtl(GetHwnd());
209 m_useCtl3D = TRUE;
210 }
2bda0e17
KB
211#endif
212
57c208c5 213#if wxUSE_RICHEDIT
a1b82138
VZ
214 if (m_isRich)
215 {
216 // Have to enable events
217 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0,
218 ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE);
219 }
2bda0e17
KB
220#endif
221
a1b82138 222 SubclassWin(GetHWND());
2bda0e17 223
a1b82138
VZ
224 // set font, position, size and initial value
225 wxFont& fontParent = parent->GetFont();
226 if ( fontParent.Ok() )
227 {
228 SetFont(fontParent);
229 }
230 else
231 {
232 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT));
233 }
2bda0e17 234
a1b82138 235 // Causes a crash for Symantec C++ and WIN32 for some reason
2bda0e17 236#if !(defined(__SC__) && defined(__WIN32__))
a1b82138
VZ
237 if ( !value.IsEmpty() )
238 {
239 SetValue(value);
240 }
2bda0e17
KB
241#endif
242
a1b82138
VZ
243 SetSize(pos.x, pos.y, size.x, size.y);
244
245 return TRUE;
2bda0e17
KB
246}
247
248// Make sure the window style (etc.) reflects the HWND style (roughly)
cd471848 249void wxTextCtrl::AdoptAttributesFromHWND()
2bda0e17 250{
c085e333 251 wxWindow::AdoptAttributesFromHWND();
2bda0e17 252
789295bf 253 HWND hWnd = GetHwnd();
a1b82138 254 long style = GetWindowLong(hWnd, GWL_STYLE);
2bda0e17 255
cd471848
VZ
256 // retrieve the style to see whether this is an edit or richedit ctrl
257#if wxUSE_RICHEDIT
837e5743 258 wxChar buf[256];
2bda0e17 259
a1b82138 260 GetClassName(hWnd, buf, WXSIZEOF(buf));
2bda0e17 261
a1b82138 262 if ( wxStricmp(buf, _T("EDIT")) == 0 )
c085e333
VZ
263 m_isRich = FALSE;
264 else
265 m_isRich = TRUE;
a1b82138 266#endif // wxUSE_RICHEDIT
c085e333
VZ
267
268 if (style & ES_MULTILINE)
269 m_windowStyle |= wxTE_MULTILINE;
270 if (style & ES_PASSWORD)
271 m_windowStyle |= wxTE_PASSWORD;
272 if (style & ES_READONLY)
273 m_windowStyle |= wxTE_READONLY;
274 if (style & ES_WANTRETURN)
275 m_windowStyle |= wxTE_PROCESS_ENTER;
2bda0e17
KB
276}
277
cd471848 278void wxTextCtrl::SetupColours()
2bda0e17 279{
a1b82138
VZ
280 // FIXME why is bg colour not inherited from parent?
281 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
282 SetForegroundColour(GetParent()->GetForegroundColour());
2bda0e17
KB
283}
284
a1b82138
VZ
285// ----------------------------------------------------------------------------
286// set/get the controls text
287// ----------------------------------------------------------------------------
288
cd471848 289wxString wxTextCtrl::GetValue() const
2bda0e17 290{
789295bf 291 return wxGetWindowText(GetHWND());
2bda0e17
KB
292}
293
294void wxTextCtrl::SetValue(const wxString& value)
295{
a1b82138 296 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
789295bf 297
a1b82138
VZ
298 SetWindowText(GetHwnd(), valueDos);
299
300 AdjustSpaceLimit();
2bda0e17
KB
301}
302
a1b82138 303void wxTextCtrl::WriteText(const wxString& value)
2bda0e17 304{
a1b82138 305 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
2bda0e17 306
a1b82138 307 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
2bda0e17 308
a1b82138 309 AdjustSpaceLimit();
2bda0e17
KB
310}
311
a1b82138
VZ
312void wxTextCtrl::AppendText(const wxString& text)
313{
314 SetInsertionPointEnd();
315 WriteText(text);
316}
317
318void wxTextCtrl::Clear()
319{
320 SetWindowText(GetHwnd(), _T(""));
321}
322
323// ----------------------------------------------------------------------------
2bda0e17 324// Clipboard operations
a1b82138
VZ
325// ----------------------------------------------------------------------------
326
cd471848 327void wxTextCtrl::Copy()
2bda0e17 328{
e702ff0f
JS
329 if (CanCopy())
330 {
789295bf 331 HWND hWnd = GetHwnd();
e702ff0f
JS
332 SendMessage(hWnd, WM_COPY, 0, 0L);
333 }
2bda0e17
KB
334}
335
cd471848 336void wxTextCtrl::Cut()
2bda0e17 337{
e702ff0f
JS
338 if (CanCut())
339 {
789295bf 340 HWND hWnd = GetHwnd();
e702ff0f
JS
341 SendMessage(hWnd, WM_CUT, 0, 0L);
342 }
2bda0e17
KB
343}
344
cd471848 345void wxTextCtrl::Paste()
2bda0e17 346{
e702ff0f
JS
347 if (CanPaste())
348 {
789295bf 349 HWND hWnd = GetHwnd();
e702ff0f
JS
350 SendMessage(hWnd, WM_PASTE, 0, 0L);
351 }
2bda0e17
KB
352}
353
a1b82138
VZ
354bool wxTextCtrl::CanCopy() const
355{
356 // Can copy if there's a selection
357 long from, to;
358 GetSelection(& from, & to);
359 return (from != to);
360}
361
362bool wxTextCtrl::CanCut() const
363{
364 // Can cut if there's a selection
365 long from, to;
366 GetSelection(& from, & to);
367 return (from != to);
368}
369
370bool wxTextCtrl::CanPaste() const
371{
372#if wxUSE_RICHEDIT
373 if (m_isRich)
374 {
375 int dataFormat = 0; // 0 == any format
376 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
377 }
378#endif
379 if (!IsEditable())
380 return FALSE;
381
382 // Standard edit control: check for straight text on clipboard
383 bool isTextAvailable = FALSE;
384 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
385 {
386 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
387 ::CloseClipboard();
388 }
389
390 return isTextAvailable;
391}
392
393// ----------------------------------------------------------------------------
394// Accessors
395// ----------------------------------------------------------------------------
396
debe6624 397void wxTextCtrl::SetEditable(bool editable)
2bda0e17 398{
a1b82138
VZ
399 HWND hWnd = GetHwnd();
400 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
2bda0e17
KB
401}
402
debe6624 403void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 404{
a1b82138 405 HWND hWnd = GetHwnd();
2bda0e17 406#ifdef __WIN32__
57c208c5 407#if wxUSE_RICHEDIT
a1b82138
VZ
408 if ( m_isRich)
409 {
410 CHARRANGE range;
411 range.cpMin = pos;
412 range.cpMax = pos;
413 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
414 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
415 }
416 else
417#endif // wxUSE_RICHEDIT
418 {
419 SendMessage(hWnd, EM_SETSEL, pos, pos);
420 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
421 }
422#else // Win16
423 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
424#endif // Win32/16
425
426 static const char *nothing = "";
427 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
2bda0e17
KB
428}
429
cd471848 430void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 431{
a1b82138
VZ
432 long pos = GetLastPosition();
433 SetInsertionPoint(pos);
2bda0e17
KB
434}
435
cd471848 436long wxTextCtrl::GetInsertionPoint() const
2bda0e17 437{
57c208c5 438#if wxUSE_RICHEDIT
a1b82138
VZ
439 if (m_isRich)
440 {
441 CHARRANGE range;
442 range.cpMin = 0;
443 range.cpMax = 0;
444 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
445 return range.cpMin;
446 }
2bda0e17
KB
447#endif
448
a1b82138
VZ
449 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
450 return Pos & 0xFFFF;
2bda0e17
KB
451}
452
cd471848 453long wxTextCtrl::GetLastPosition() const
2bda0e17 454{
789295bf 455 HWND hWnd = GetHwnd();
2bda0e17
KB
456
457 // Will always return a number > 0 (according to docs)
458 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
459
460 // This gets the char index for the _beginning_ of the last line
461 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
39136494 462
2bda0e17
KB
463 // Get number of characters in the last line. We'll add this to the character
464 // index for the last line, 1st position.
465 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
466
467 return (long)(charIndex + lineLength);
468}
469
a1b82138
VZ
470// If the return values from and to are the same, there is no
471// selection.
472void wxTextCtrl::GetSelection(long* from, long* to) const
473{
474#if wxUSE_RICHEDIT
475 if (m_isRich)
476 {
477 CHARRANGE charRange;
478 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
479
480 *from = charRange.cpMin;
481 *to = charRange.cpMax;
482
483 return;
484 }
485#endif
486 DWORD dwStart, dwEnd;
487 WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position
488 LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position
489
490 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
491
492 *from = dwStart;
493 *to = dwEnd;
494}
495
496bool wxTextCtrl::IsEditable() const
497{
498 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
499
500 return ((style & ES_READONLY) == 0);
501}
502
503// ----------------------------------------------------------------------------
504// Editing
505// ----------------------------------------------------------------------------
506
debe6624 507void wxTextCtrl::Replace(long from, long to, const wxString& value)
2bda0e17 508{
acbd13a3 509#if wxUSE_CLIPBOARD
789295bf 510 HWND hWnd = GetHwnd();
2bda0e17
KB
511 long fromChar = from;
512 long toChar = to;
39136494 513
2bda0e17
KB
514 // Set selection and remove it
515#ifdef __WIN32__
516 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
517#else
518 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
519#endif
520 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
521
522 // Now replace with 'value', by pasting.
837e5743 523 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
2bda0e17
KB
524
525 // Paste into edit control
526 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
acbd13a3
JS
527#else
528 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
529#endif
2bda0e17
KB
530}
531
debe6624 532void wxTextCtrl::Remove(long from, long to)
2bda0e17 533{
789295bf 534 HWND hWnd = GetHwnd();
2bda0e17
KB
535 long fromChar = from;
536 long toChar = to;
39136494 537
2bda0e17
KB
538 // Cut all selected text
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
debe6624 547void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 548{
789295bf 549 HWND hWnd = GetHwnd();
2bda0e17
KB
550 long fromChar = from;
551 long toChar = to;
a1b82138
VZ
552
553 // if from and to are both -1, it means (in wxWindows) that all text should
554 // be selected. Translate into Windows convention
2bda0e17
KB
555 if ((from == -1) && (to == -1))
556 {
557 fromChar = 0;
558 toChar = -1;
559 }
39136494 560
2bda0e17
KB
561#ifdef __WIN32__
562 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
563 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
564#else
565 // WPARAM is 0: selection is scrolled into view
566 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
567#endif
568}
569
570bool wxTextCtrl::LoadFile(const wxString& file)
571{
a1b82138 572 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 573 {
a1b82138
VZ
574 // update the size limit if needed
575 AdjustSpaceLimit();
2bda0e17 576
a1b82138 577 return TRUE;
2bda0e17 578 }
2bda0e17 579
a1b82138 580 return FALSE;
2bda0e17
KB
581}
582
cd471848 583bool wxTextCtrl::IsModified() const
2bda0e17 584{
789295bf 585 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
2bda0e17
KB
586}
587
588// Makes 'unmodified'
cd471848 589void wxTextCtrl::DiscardEdits()
2bda0e17 590{
a1b82138 591 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
592}
593
cd471848 594int wxTextCtrl::GetNumberOfLines() const
2bda0e17 595{
789295bf 596 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
597}
598
debe6624 599long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 600{
789295bf 601 HWND hWnd = GetHwnd();
2bda0e17
KB
602
603 // This gets the char index for the _beginning_ of this line
604 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
605 return (long)(x + charIndex);
606}
607
0efe5ba7 608bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 609{
789295bf 610 HWND hWnd = GetHwnd();
2bda0e17
KB
611
612 // This gets the line number containing the character
0efe5ba7
VZ
613 int lineNo;
614#if wxUSE_RICHEDIT
615 if ( m_isRich )
616 {
617 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
618 }
619 else
620#endif // wxUSE_RICHEDIT
621 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
622
623 if ( lineNo == -1 )
624 {
625 // no such line
626 return FALSE;
627 }
628
2bda0e17
KB
629 // This gets the char index for the _beginning_ of this line
630 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
631 if ( charIndex == -1 )
632 {
633 return FALSE;
634 }
635
2bda0e17 636 // The X position must therefore be the different between pos and charIndex
0efe5ba7
VZ
637 if ( x )
638 *x = (long)(pos - charIndex);
639 if ( y )
640 *y = (long)lineNo;
641
642 return TRUE;
2bda0e17
KB
643}
644
debe6624 645void wxTextCtrl::ShowPosition(long pos)
2bda0e17 646{
789295bf 647 HWND hWnd = GetHwnd();
2bda0e17
KB
648
649 // To scroll to a position, we pass the number of lines and characters
650 // to scroll *by*. This means that we need to:
651 // (1) Find the line position of the current line.
652 // (2) Find the line position of pos.
653 // (3) Scroll by (pos - current).
654 // For now, ignore the horizontal scrolling.
655
656 // Is this where scrolling is relative to - the line containing the caret?
657 // Or is the first visible line??? Try first visible line.
658// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
659
660 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
661
662 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 663
2bda0e17
KB
664 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
665
2bda0e17 666 if (linesToScroll != 0)
de4d7713 667 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
668}
669
debe6624 670int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
671{
672 long charIndex = XYToPosition(0, lineNo);
4438caf4 673 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
2bda0e17
KB
674 return len;
675}
676
debe6624 677wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 678{
a1b82138 679 size_t len = (size_t)GetLineLength(lineNo) + 1;
4438caf4
VZ
680 char *buf = (char *)malloc(len);
681 *(WORD *)buf = len;
682 int noChars = (int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
683 buf[noChars] = 0;
684
685 wxString str(buf);
686
687 free(buf);
688
689 return str;
2bda0e17
KB
690}
691
a1b82138 692// ----------------------------------------------------------------------------
ca8b28f2 693// Undo/redo
a1b82138
VZ
694// ----------------------------------------------------------------------------
695
ca8b28f2
JS
696void wxTextCtrl::Undo()
697{
698 if (CanUndo())
699 {
789295bf 700 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
701 }
702}
703
704void wxTextCtrl::Redo()
705{
706 if (CanRedo())
707 {
708 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 709 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
710 }
711}
712
713bool wxTextCtrl::CanUndo() const
714{
789295bf 715 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
716}
717
718bool wxTextCtrl::CanRedo() const
719{
789295bf 720 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
721}
722
a1b82138
VZ
723// ----------------------------------------------------------------------------
724// implemenation details
725// ----------------------------------------------------------------------------
39136494 726
2bda0e17
KB
727void wxTextCtrl::Command(wxCommandEvent & event)
728{
a1b82138
VZ
729 SetValue(event.GetString());
730 ProcessCommand (event);
2bda0e17
KB
731}
732
733void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
734{
a1b82138
VZ
735 // By default, load the first file into the text window.
736 if (event.GetNumberOfFiles() > 0)
737 {
738 LoadFile(event.GetFiles()[0]);
739 }
2bda0e17
KB
740}
741
debe6624 742WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
a1b82138
VZ
743 WXUINT message, WXWPARAM wParam,
744 WXLPARAM lParam)
2bda0e17 745{
1f112209 746#if wxUSE_CTL3D
a1b82138
VZ
747 if ( m_useCtl3D )
748 {
749 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
750 return (WXHBRUSH) hbrush;
751 }
2bda0e17
KB
752#endif
753
a1b82138
VZ
754 HDC hdc = (HDC)pDC;
755 SetBkMode(hdc, GetParent()->GetTransparentBackground() ? TRANSPARENT
756 : OPAQUE);
2bda0e17 757
a1b82138
VZ
758 ::SetBkColor(hdc, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
759 ::SetTextColor(hdc, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
2bda0e17 760
a1b82138 761 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
2bda0e17 762
a1b82138 763 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
2bda0e17
KB
764}
765
766void 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 814bool 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
858void 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 879bool 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
a1b82138
VZ
885wxSize wxTextCtrl::DoGetBestSize()
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
906void wxTextCtrl::OnCut(wxCommandEvent& event)
907{
908 Cut();
909}
910
911void wxTextCtrl::OnCopy(wxCommandEvent& event)
912{
913 Copy();
914}
915
916void wxTextCtrl::OnPaste(wxCommandEvent& event)
917{
918 Paste();
919}
920
921void wxTextCtrl::OnUndo(wxCommandEvent& event)
922{
923 Undo();
924}
925
926void wxTextCtrl::OnRedo(wxCommandEvent& event)
927{
928 Redo();
929}
930
931void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
932{
933 event.Enable( CanCut() );
934}
935
936void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
937{
938 event.Enable( CanCopy() );
939}
940
941void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
942{
943 event.Enable( CanPaste() );
944}
945
946void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
947{
948 event.Enable( CanUndo() );
949}
950
951void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
952{
953 event.Enable( CanRedo() );
954}
955