]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
Tom's patch - hope I got it right this time
[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
07cf98cb
VZ
299 if ( valueDos != GetValue() )
300 {
301 SetWindowText(GetHwnd(), valueDos);
a1b82138 302
07cf98cb
VZ
303 AdjustSpaceLimit();
304 }
2bda0e17
KB
305}
306
a1b82138 307void wxTextCtrl::WriteText(const wxString& value)
2bda0e17 308{
a1b82138 309 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
2bda0e17 310
a1b82138 311 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
2bda0e17 312
a1b82138 313 AdjustSpaceLimit();
2bda0e17
KB
314}
315
a1b82138
VZ
316void wxTextCtrl::AppendText(const wxString& text)
317{
318 SetInsertionPointEnd();
319 WriteText(text);
320}
321
322void wxTextCtrl::Clear()
323{
223d09f6 324 SetWindowText(GetHwnd(), wxT(""));
a1b82138
VZ
325}
326
327// ----------------------------------------------------------------------------
2bda0e17 328// Clipboard operations
a1b82138
VZ
329// ----------------------------------------------------------------------------
330
cd471848 331void wxTextCtrl::Copy()
2bda0e17 332{
e702ff0f
JS
333 if (CanCopy())
334 {
789295bf 335 HWND hWnd = GetHwnd();
e702ff0f
JS
336 SendMessage(hWnd, WM_COPY, 0, 0L);
337 }
2bda0e17
KB
338}
339
cd471848 340void wxTextCtrl::Cut()
2bda0e17 341{
e702ff0f
JS
342 if (CanCut())
343 {
789295bf 344 HWND hWnd = GetHwnd();
e702ff0f
JS
345 SendMessage(hWnd, WM_CUT, 0, 0L);
346 }
2bda0e17
KB
347}
348
cd471848 349void wxTextCtrl::Paste()
2bda0e17 350{
e702ff0f
JS
351 if (CanPaste())
352 {
789295bf 353 HWND hWnd = GetHwnd();
e702ff0f
JS
354 SendMessage(hWnd, WM_PASTE, 0, 0L);
355 }
2bda0e17
KB
356}
357
a1b82138
VZ
358bool wxTextCtrl::CanCopy() const
359{
360 // Can copy if there's a selection
361 long from, to;
362 GetSelection(& from, & to);
363 return (from != to);
364}
365
366bool wxTextCtrl::CanCut() const
367{
368 // Can cut if there's a selection
369 long from, to;
370 GetSelection(& from, & to);
371 return (from != to);
372}
373
374bool wxTextCtrl::CanPaste() const
375{
376#if wxUSE_RICHEDIT
377 if (m_isRich)
378 {
379 int dataFormat = 0; // 0 == any format
380 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
381 }
382#endif
383 if (!IsEditable())
384 return FALSE;
385
386 // Standard edit control: check for straight text on clipboard
387 bool isTextAvailable = FALSE;
388 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
389 {
390 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
391 ::CloseClipboard();
392 }
393
394 return isTextAvailable;
395}
396
397// ----------------------------------------------------------------------------
398// Accessors
399// ----------------------------------------------------------------------------
400
debe6624 401void wxTextCtrl::SetEditable(bool editable)
2bda0e17 402{
a1b82138
VZ
403 HWND hWnd = GetHwnd();
404 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
2bda0e17
KB
405}
406
debe6624 407void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 408{
a1b82138 409 HWND hWnd = GetHwnd();
2bda0e17 410#ifdef __WIN32__
57c208c5 411#if wxUSE_RICHEDIT
a1b82138
VZ
412 if ( m_isRich)
413 {
414 CHARRANGE range;
415 range.cpMin = pos;
416 range.cpMax = pos;
417 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
418 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
419 }
420 else
421#endif // wxUSE_RICHEDIT
422 {
423 SendMessage(hWnd, EM_SETSEL, pos, pos);
424 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
425 }
426#else // Win16
427 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
428#endif // Win32/16
429
430 static const char *nothing = "";
431 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
2bda0e17
KB
432}
433
cd471848 434void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 435{
a1b82138
VZ
436 long pos = GetLastPosition();
437 SetInsertionPoint(pos);
2bda0e17
KB
438}
439
cd471848 440long wxTextCtrl::GetInsertionPoint() const
2bda0e17 441{
57c208c5 442#if wxUSE_RICHEDIT
a1b82138
VZ
443 if (m_isRich)
444 {
445 CHARRANGE range;
446 range.cpMin = 0;
447 range.cpMax = 0;
448 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
449 return range.cpMin;
450 }
2bda0e17
KB
451#endif
452
a1b82138
VZ
453 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
454 return Pos & 0xFFFF;
2bda0e17
KB
455}
456
cd471848 457long wxTextCtrl::GetLastPosition() const
2bda0e17 458{
789295bf 459 HWND hWnd = GetHwnd();
2bda0e17
KB
460
461 // Will always return a number > 0 (according to docs)
462 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
463
464 // This gets the char index for the _beginning_ of the last line
465 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
39136494 466
2bda0e17
KB
467 // Get number of characters in the last line. We'll add this to the character
468 // index for the last line, 1st position.
469 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
470
471 return (long)(charIndex + lineLength);
472}
473
a1b82138
VZ
474// If the return values from and to are the same, there is no
475// selection.
476void wxTextCtrl::GetSelection(long* from, long* to) const
477{
478#if wxUSE_RICHEDIT
479 if (m_isRich)
480 {
481 CHARRANGE charRange;
482 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
483
484 *from = charRange.cpMin;
485 *to = charRange.cpMax;
486
487 return;
488 }
489#endif
490 DWORD dwStart, dwEnd;
491 WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position
492 LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position
493
494 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
495
496 *from = dwStart;
497 *to = dwEnd;
498}
499
500bool wxTextCtrl::IsEditable() const
501{
502 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
503
504 return ((style & ES_READONLY) == 0);
505}
506
507// ----------------------------------------------------------------------------
508// Editing
509// ----------------------------------------------------------------------------
510
debe6624 511void wxTextCtrl::Replace(long from, long to, const wxString& value)
2bda0e17 512{
acbd13a3 513#if wxUSE_CLIPBOARD
789295bf 514 HWND hWnd = GetHwnd();
2bda0e17
KB
515 long fromChar = from;
516 long toChar = to;
39136494 517
2bda0e17
KB
518 // Set selection and remove it
519#ifdef __WIN32__
520 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
521#else
522 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
523#endif
524 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
525
526 // Now replace with 'value', by pasting.
837e5743 527 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
2bda0e17
KB
528
529 // Paste into edit control
530 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
acbd13a3
JS
531#else
532 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
533#endif
2bda0e17
KB
534}
535
debe6624 536void wxTextCtrl::Remove(long from, long to)
2bda0e17 537{
789295bf 538 HWND hWnd = GetHwnd();
2bda0e17
KB
539 long fromChar = from;
540 long toChar = to;
39136494 541
2bda0e17
KB
542 // Cut all selected text
543#ifdef __WIN32__
544 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
545#else
546 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
547#endif
548 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
549}
550
debe6624 551void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 552{
789295bf 553 HWND hWnd = GetHwnd();
2bda0e17
KB
554 long fromChar = from;
555 long toChar = to;
a1b82138
VZ
556
557 // if from and to are both -1, it means (in wxWindows) that all text should
558 // be selected. Translate into Windows convention
2bda0e17
KB
559 if ((from == -1) && (to == -1))
560 {
561 fromChar = 0;
562 toChar = -1;
563 }
39136494 564
2bda0e17
KB
565#ifdef __WIN32__
566 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
567 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
568#else
569 // WPARAM is 0: selection is scrolled into view
570 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
571#endif
572}
573
574bool wxTextCtrl::LoadFile(const wxString& file)
575{
a1b82138 576 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 577 {
a1b82138
VZ
578 // update the size limit if needed
579 AdjustSpaceLimit();
2bda0e17 580
a1b82138 581 return TRUE;
2bda0e17 582 }
2bda0e17 583
a1b82138 584 return FALSE;
2bda0e17
KB
585}
586
cd471848 587bool wxTextCtrl::IsModified() const
2bda0e17 588{
789295bf 589 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
2bda0e17
KB
590}
591
592// Makes 'unmodified'
cd471848 593void wxTextCtrl::DiscardEdits()
2bda0e17 594{
a1b82138 595 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
596}
597
cd471848 598int wxTextCtrl::GetNumberOfLines() const
2bda0e17 599{
789295bf 600 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
601}
602
debe6624 603long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 604{
789295bf 605 HWND hWnd = GetHwnd();
2bda0e17
KB
606
607 // This gets the char index for the _beginning_ of this line
608 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
609 return (long)(x + charIndex);
610}
611
0efe5ba7 612bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 613{
789295bf 614 HWND hWnd = GetHwnd();
2bda0e17
KB
615
616 // This gets the line number containing the character
0efe5ba7
VZ
617 int lineNo;
618#if wxUSE_RICHEDIT
619 if ( m_isRich )
620 {
621 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
622 }
623 else
624#endif // wxUSE_RICHEDIT
625 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
626
627 if ( lineNo == -1 )
628 {
629 // no such line
630 return FALSE;
631 }
632
2bda0e17
KB
633 // This gets the char index for the _beginning_ of this line
634 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
635 if ( charIndex == -1 )
636 {
637 return FALSE;
638 }
639
2bda0e17 640 // The X position must therefore be the different between pos and charIndex
0efe5ba7
VZ
641 if ( x )
642 *x = (long)(pos - charIndex);
643 if ( y )
644 *y = (long)lineNo;
645
646 return TRUE;
2bda0e17
KB
647}
648
debe6624 649void wxTextCtrl::ShowPosition(long pos)
2bda0e17 650{
789295bf 651 HWND hWnd = GetHwnd();
2bda0e17
KB
652
653 // To scroll to a position, we pass the number of lines and characters
654 // to scroll *by*. This means that we need to:
655 // (1) Find the line position of the current line.
656 // (2) Find the line position of pos.
657 // (3) Scroll by (pos - current).
658 // For now, ignore the horizontal scrolling.
659
660 // Is this where scrolling is relative to - the line containing the caret?
661 // Or is the first visible line??? Try first visible line.
662// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
663
664 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
665
666 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 667
2bda0e17
KB
668 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
669
2bda0e17 670 if (linesToScroll != 0)
de4d7713 671 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
672}
673
debe6624 674int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
675{
676 long charIndex = XYToPosition(0, lineNo);
4438caf4 677 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
2bda0e17
KB
678 return len;
679}
680
debe6624 681wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 682{
a1b82138 683 size_t len = (size_t)GetLineLength(lineNo) + 1;
4438caf4
VZ
684 char *buf = (char *)malloc(len);
685 *(WORD *)buf = len;
686 int noChars = (int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
687 buf[noChars] = 0;
688
689 wxString str(buf);
690
691 free(buf);
692
693 return str;
2bda0e17
KB
694}
695
a1b82138 696// ----------------------------------------------------------------------------
ca8b28f2 697// Undo/redo
a1b82138
VZ
698// ----------------------------------------------------------------------------
699
ca8b28f2
JS
700void wxTextCtrl::Undo()
701{
702 if (CanUndo())
703 {
789295bf 704 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
705 }
706}
707
708void wxTextCtrl::Redo()
709{
710 if (CanRedo())
711 {
712 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 713 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
714 }
715}
716
717bool wxTextCtrl::CanUndo() const
718{
789295bf 719 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
720}
721
722bool wxTextCtrl::CanRedo() const
723{
789295bf 724 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
725}
726
a1b82138
VZ
727// ----------------------------------------------------------------------------
728// implemenation details
729// ----------------------------------------------------------------------------
39136494 730
2bda0e17
KB
731void wxTextCtrl::Command(wxCommandEvent & event)
732{
a1b82138
VZ
733 SetValue(event.GetString());
734 ProcessCommand (event);
2bda0e17
KB
735}
736
737void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
738{
a1b82138
VZ
739 // By default, load the first file into the text window.
740 if (event.GetNumberOfFiles() > 0)
741 {
742 LoadFile(event.GetFiles()[0]);
743 }
2bda0e17
KB
744}
745
debe6624 746WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
a1b82138
VZ
747 WXUINT message, WXWPARAM wParam,
748 WXLPARAM lParam)
2bda0e17 749{
1f112209 750#if wxUSE_CTL3D
a1b82138
VZ
751 if ( m_useCtl3D )
752 {
753 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
754 return (WXHBRUSH) hbrush;
755 }
2bda0e17
KB
756#endif
757
a1b82138
VZ
758 HDC hdc = (HDC)pDC;
759 SetBkMode(hdc, GetParent()->GetTransparentBackground() ? TRANSPARENT
760 : OPAQUE);
2bda0e17 761
a1b82138
VZ
762 ::SetBkColor(hdc, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
763 ::SetTextColor(hdc, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
2bda0e17 764
a1b82138 765 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
2bda0e17 766
a1b82138 767 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
2bda0e17
KB
768}
769
770void wxTextCtrl::OnChar(wxKeyEvent& event)
771{
42e69d6b 772 switch ( event.KeyCode() )
cd471848 773 {
cd471848 774 case WXK_RETURN:
39136494 775 if ( !(m_windowStyle & wxTE_MULTILINE) )
cd471848
VZ
776 {
777 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
778 event.SetEventObject( this );
779 if ( GetEventHandler()->ProcessEvent(event) )
780 return;
781 }
5fb9fcfc
VZ
782 //else: multiline controls need Enter for themselves
783
784 break;
4d91c1d1 785
cd471848 786 case WXK_TAB:
319fefa9
VZ
787 // always produce navigation event - even if we process TAB
788 // ourselves the fact that we got here means that the user code
789 // decided to skip processing of this TAB - probably to let it
790 // do its default job.
5fb9fcfc
VZ
791 //
792 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
793 // handled by Windows
cd471848 794 {
5fb9fcfc
VZ
795 wxNavigationKeyEvent eventNav;
796 eventNav.SetDirection(!event.ShiftDown());
797 eventNav.SetWindowChange(FALSE);
798 eventNav.SetEventObject(this);
39136494 799
5fb9fcfc 800 if ( GetEventHandler()->ProcessEvent(eventNav) )
cd471848
VZ
801 return;
802 }
341c92a8 803 break;
4d91c1d1
VZ
804
805 default:
806 event.Skip();
39136494 807 return;
cd471848 808 }
39136494 809
cd471848
VZ
810 // don't just call event.Skip() because this will cause TABs and ENTERs
811 // be passed upwards and we don't always want this - instead process it
812 // right here
42e69d6b
VZ
813
814 // FIXME
815 event.Skip();
2bda0e17
KB
816}
817
debe6624 818bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 819{
789295bf
VZ
820 switch (param)
821 {
822 case EN_SETFOCUS:
823 case EN_KILLFOCUS:
824 {
825 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
826 : wxEVT_SET_FOCUS,
827 m_windowId);
828 event.SetEventObject( this );
829 GetEventHandler()->ProcessEvent(event);
830 }
831 break;
ae29de83 832
789295bf
VZ
833 case EN_CHANGE:
834 {
835 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
836 wxString val(GetValue());
837 if ( !val.IsNull() )
838 event.m_commandString = WXSTRINGCAST val;
839 event.SetEventObject( this );
840 ProcessCommand(event);
841 }
842 break;
2bda0e17 843
789295bf
VZ
844 case EN_ERRSPACE:
845 // the text size limit has been hit - increase it
846 AdjustSpaceLimit();
847 break;
848
849 // the other notification messages are not processed
850 case EN_UPDATE:
851 case EN_MAXTEXT:
852 case EN_HSCROLL:
853 case EN_VSCROLL:
854 default:
855 return FALSE;
856 }
857
858 // processed
859 return TRUE;
2bda0e17
KB
860}
861
789295bf
VZ
862void wxTextCtrl::AdjustSpaceLimit()
863{
25889d3c 864#ifndef __WIN16__
789295bf
VZ
865 unsigned int len = ::GetWindowTextLength(GetHwnd()),
866 limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
867 if ( len > limit )
868 {
869 limit = len + 0x8000; // 32Kb
870
5ea105e0 871#if wxUSE_RICHEDIT
789295bf 872 if ( m_isRich || limit > 0xffff )
5ea105e0
RR
873#else
874 if ( limit > 0xffff )
875#endif
789295bf
VZ
876 ::SendMessage(GetHwnd(), EM_LIMITTEXT, 0, limit);
877 else
878 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
879 }
25889d3c 880#endif
789295bf 881}
2bda0e17 882
a1b82138 883bool wxTextCtrl::AcceptsFocus() const
2bda0e17 884{
a1b82138
VZ
885 // we don't want focus if we can't be edited
886 return IsEditable() && wxControl::AcceptsFocus();
887}
c085e333 888
a1b82138
VZ
889wxSize wxTextCtrl::DoGetBestSize()
890{
891 int cx, cy;
892 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
893
894 int wText = DEFAULT_ITEM_WIDTH;
895
896 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
897 if ( m_windowStyle & wxTE_MULTILINE )
898 {
899 hText *= wxMin(GetNumberOfLines(), 5);
900 }
901 //else: for single line control everything is ok
902
903 return wxSize(wText, hText);
2bda0e17 904}
a1b82138
VZ
905
906// ----------------------------------------------------------------------------
907// standard handlers for standard edit menu events
908// ----------------------------------------------------------------------------
2bda0e17 909
e702ff0f
JS
910void wxTextCtrl::OnCut(wxCommandEvent& event)
911{
912 Cut();
913}
914
915void wxTextCtrl::OnCopy(wxCommandEvent& event)
916{
917 Copy();
918}
919
920void wxTextCtrl::OnPaste(wxCommandEvent& event)
921{
922 Paste();
923}
924
925void wxTextCtrl::OnUndo(wxCommandEvent& event)
926{
927 Undo();
928}
929
930void wxTextCtrl::OnRedo(wxCommandEvent& event)
931{
932 Redo();
933}
934
935void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
936{
937 event.Enable( CanCut() );
938}
939
940void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
941{
942 event.Enable( CanCopy() );
943}
944
945void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
946{
947 event.Enable( CanPaste() );
948}
949
950void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
951{
952 event.Enable( CanUndo() );
953}
954
955void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
956{
957 event.Enable( CanRedo() );
958}
959