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