1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "textctrl.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/textctrl.h"
25 #include "wx/settings.h"
33 #include "wx/clipbrd.h"
36 #include "wx/msw/private.h"
47 #include <sys/types.h>
53 #if defined(__BORLANDC__) && !defined(__WIN32__)
56 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
59 #define farmalloc malloc
66 #if wxUSE_RICHEDIT && !defined(__GNUWIN32__)
70 #if !USE_SHARED_LIBRARY
72 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
74 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
75 EVT_CHAR(wxTextCtrl::OnChar
)
76 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
78 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
79 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
80 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
81 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
82 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
84 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
85 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
86 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
87 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
88 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
91 #endif // USE_SHARED_LIBRARY
94 wxTextCtrl::wxTextCtrl()
95 #ifndef NO_TEXT_WINDOW_STREAM
104 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
105 const wxString
& value
,
107 const wxSize
& size
, long style
,
108 const wxValidator
& validator
,
109 const wxString
& name
)
112 SetValidator(validator
);
113 if (parent
) parent
->AddChild(this);
115 m_windowStyle
= style
;
117 // Should this be taken from the system colours?
118 // SetBackgroundColour(wxColour(255, 255, 255));
120 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
122 SetForegroundColour(parent
->GetForegroundColour()) ;
125 m_windowId
= (int)NewControlId();
134 long msStyle
= ES_LEFT
| WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
135 if (m_windowStyle
& wxTE_MULTILINE
)
137 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
138 _T("wxTE_PROCESS_ENTER style is ignored for multiline controls") );
140 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
| WS_VSCROLL
; // WS_BORDER
141 m_windowStyle
|= wxTE_PROCESS_ENTER
;
144 msStyle
|= ES_AUTOHSCROLL
;
146 if (m_windowStyle
& wxTE_READONLY
)
147 msStyle
|= ES_READONLY
;
149 if (m_windowStyle
& wxHSCROLL
)
150 msStyle
|= (WS_HSCROLL
| ES_AUTOHSCROLL
) ;
151 if (m_windowStyle
& wxTE_PASSWORD
) // hidden input
152 msStyle
|= ES_PASSWORD
;
154 // we always want the characters and the arrows
155 m_lDlgCode
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
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
164 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
165 m_lDlgCode
|= DLGC_WANTMESSAGE
;
166 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
167 m_lDlgCode
|= DLGC_WANTTAB
;
169 const wxChar
*windowClass
= _T("EDIT");
172 if ( m_windowStyle
& wxTE_MULTILINE
)
174 msStyle
|= ES_AUTOVSCROLL
;
176 windowClass
= _T("RichEdit") ;
183 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
185 // If we're in Win95, and we want a simple 2D border,
186 // then make it an EDIT control instead.
188 if (m_windowStyle
& wxSIMPLE_BORDER
)
190 windowClass
= _T("EDIT");
195 // Even with extended styles, need to combine with WS_BORDER
196 // for them to look right.
197 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
198 msStyle
|= WS_BORDER
;
200 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, windowClass
, NULL
,
202 0, 0, 0, 0, (HWND
) ((wxWindow
*)parent
)->GetHWND(), (HMENU
)m_windowId
,
203 wxGetInstance(), NULL
);
205 wxCHECK_MSG( m_hWnd
, FALSE
, _T("Failed to create text ctrl") );
210 Ctl3dSubclassCtl((HWND
)m_hWnd
);
218 // Have to enable events
219 ::SendMessage((HWND
)m_hWnd
, EM_SETEVENTMASK
, 0,
220 ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
);
224 SubclassWin(GetHWND());
226 if ( parent
->GetFont().Ok() && parent
->GetFont().Ok() )
228 SetFont(parent
->GetFont());
232 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
235 SetSize(x
, y
, width
, height
);
237 // Causes a crash for Symantec C++ and WIN32 for some reason
238 #if !(defined(__SC__) && defined(__WIN32__))
239 if ( !value
.IsEmpty() )
248 // Make sure the window style (etc.) reflects the HWND style (roughly)
249 void wxTextCtrl::AdoptAttributesFromHWND()
251 wxWindow::AdoptAttributesFromHWND();
253 HWND hWnd
= GetHwnd();
254 long style
= GetWindowLong((HWND
) hWnd
, GWL_STYLE
);
256 // retrieve the style to see whether this is an edit or richedit ctrl
261 GetClassName((HWND
) hWnd
, buf
, 256);
264 GetClassNameW((HWND
) hWnd
, buf
, 256);
267 GetClassName((HWND
) hWnd
, buf
, 256);
269 GetClassNameA((HWND
) hWnd
, buf
, 256);
277 if (str
== _T("EDIT"))
283 if (style
& ES_MULTILINE
)
284 m_windowStyle
|= wxTE_MULTILINE
;
285 if (style
& ES_PASSWORD
)
286 m_windowStyle
|= wxTE_PASSWORD
;
287 if (style
& ES_READONLY
)
288 m_windowStyle
|= wxTE_READONLY
;
289 if (style
& ES_WANTRETURN
)
290 m_windowStyle
|= wxTE_PROCESS_ENTER
;
293 void wxTextCtrl::SetupColours()
295 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
296 SetForegroundColour(GetParent()->GetForegroundColour());
299 wxString
wxTextCtrl::GetValue() const
301 return wxGetWindowText(GetHWND());
304 void wxTextCtrl::SetValue(const wxString
& value
)
306 // If newlines are denoted by just 10, must stick 13 in front.
308 int len
= value
.Length();
310 for (i
= 0; i
< len
; i
++)
312 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
317 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
319 for (i
= 0; i
< len
; i
++)
321 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
330 SetWindowText(GetHwnd(), tmp
);
334 SetWindowText(GetHwnd(), (const wxChar
*)value
);
339 wxSize
wxTextCtrl::DoGetBestSize()
342 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
344 int wText
= DEFAULT_ITEM_WIDTH
;
345 int hText
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
347 return wxSize(wText
, hText
);
350 // Clipboard operations
351 void wxTextCtrl::Copy()
355 HWND hWnd
= GetHwnd();
356 SendMessage(hWnd
, WM_COPY
, 0, 0L);
360 void wxTextCtrl::Cut()
364 HWND hWnd
= GetHwnd();
365 SendMessage(hWnd
, WM_CUT
, 0, 0L);
369 void wxTextCtrl::Paste()
373 HWND hWnd
= GetHwnd();
374 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
378 void wxTextCtrl::SetEditable(bool editable
)
380 HWND hWnd
= GetHwnd();
381 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
384 void wxTextCtrl::SetInsertionPoint(long pos
)
386 HWND hWnd
= GetHwnd();
394 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
395 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
400 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
401 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
404 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
407 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
410 void wxTextCtrl::SetInsertionPointEnd()
412 long pos
= GetLastPosition();
413 SetInsertionPoint(pos
);
416 long wxTextCtrl::GetInsertionPoint() const
424 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
429 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
433 long wxTextCtrl::GetLastPosition() const
435 HWND hWnd
= GetHwnd();
437 // Will always return a number > 0 (according to docs)
438 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
440 // This gets the char index for the _beginning_ of the last line
441 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
443 // Get number of characters in the last line. We'll add this to the character
444 // index for the last line, 1st position.
445 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
447 return (long)(charIndex
+ lineLength
);
450 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
453 HWND hWnd
= GetHwnd();
454 long fromChar
= from
;
457 // Set selection and remove it
459 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
461 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
463 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
465 // Now replace with 'value', by pasting.
466 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const wxChar
*)value
, 0, 0);
468 // Paste into edit control
469 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
471 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
475 void wxTextCtrl::Remove(long from
, long to
)
477 HWND hWnd
= GetHwnd();
478 long fromChar
= from
;
481 // Cut all selected text
483 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
485 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
487 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
490 void wxTextCtrl::SetSelection(long from
, long to
)
492 HWND hWnd
= GetHwnd();
493 long fromChar
= from
;
495 // if from and to are both -1, it means
496 // (in wxWindows) that all text should be selected.
497 // This translates into Windows convention
498 if ((from
== -1) && (to
== -1))
505 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
506 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
508 // WPARAM is 0: selection is scrolled into view
509 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
513 bool wxTextCtrl::LoadFile(const wxString
& file
)
515 if (!wxFileExists(WXSTRINGCAST file
))
522 // ifstream input(WXSTRINGCAST file, ios::nocreate | ios::in);
523 ifstream
input(MBSTRINGCAST file
.mb_str(wxConvFile
), ios::in
);
527 // Previously a SETSEL/REPLACESEL call-pair were done to insert
528 // line by line into the control. Apart from being very slow this
529 // was limited to 32K of text by the external interface presenting
530 // positions as signed shorts. Now load in one chunk...
531 // Note use of 'farmalloc' as in Borland 3.1 'size_t' is 16-bits...
534 struct _stat stat_buf
;
535 if (stat(MBSTRINGCAST file
.mb_str(wxConvFile
), &stat_buf
) < 0)
538 struct stat stat_buf
;
539 if (stat(file
.mb_str(wxConvFile
), &stat_buf
) < 0)
543 // wxChar *tmp_buffer = (wxChar*)farmalloc(stat_buf.st_size+1);
544 // This may need to be a bigger buffer than the file size suggests,
545 // if it's a UNIX file. Give it an extra 1000 just in case.
546 wxChar
*tmp_buffer
= (wxChar
*)farmalloc((size_t)(stat_buf
.st_size
+1+1000));
547 char *read_buffer
= new char[512];
550 while (!input
.eof() && input
.peek() != EOF
)
552 input
.getline(read_buffer
, 500);
553 int len
= strlen(read_buffer
);
555 wxBuffer
[len
+1] = 10;
558 pos
+= wxConvCurrent
->MB2WC(tmp_buffer
+pos
, read_buffer
, (size_t)-1);
560 strcpy(tmp_buffer
+pos
, read_buffer
);
561 pos
+= strlen(read_buffer
);
565 delete[] read_buffer
;
567 SetWindowText(GetHwnd(), tmp_buffer
);
568 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
571 // update the size limit if needed
579 // If file is null, try saved file name first
580 // Returns TRUE if succeeds.
581 bool wxTextCtrl::SaveFile(const wxString
& file
)
583 wxString
theFile(file
);
585 if (theFile
== _T(""))
586 theFile
= m_fileName
;
588 if (theFile
== _T(""))
591 m_fileName
= theFile
;
593 ofstream
output(MBSTRINGCAST theFile
.mb_str(wxConvFile
));
597 // This will only save 64K max
598 unsigned long nbytes
= SendMessage(GetHwnd(), WM_GETTEXTLENGTH
, 0, 0);
599 char *tmp_buffer
= (char*)farmalloc((size_t)(nbytes
+1));
600 SendMessage(GetHwnd(), WM_GETTEXT
, (WPARAM
)(nbytes
+1), (LPARAM
)tmp_buffer
);
601 char *pstr
= tmp_buffer
;
603 // Convert \r\n to just \n
612 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
617 void wxTextCtrl::WriteText(const wxString
& text
)
620 int len
= text
.Length();
621 char *newtext
= new char[(len
*2)+1];
631 newtext
[j
] = text
[i
];
636 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)newtext
);
642 void wxTextCtrl::AppendText(const wxString
& text
)
644 SetInsertionPointEnd();
648 void wxTextCtrl::Clear()
650 SetWindowText(GetHwnd(), _T(""));
653 bool wxTextCtrl::IsModified() const
655 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
658 // Makes 'unmodified'
659 void wxTextCtrl::DiscardEdits()
661 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
665 * Some of the following functions are yet to be implemented
669 int wxTextCtrl::GetNumberOfLines() const
671 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
674 long wxTextCtrl::XYToPosition(long x
, long y
) const
676 HWND hWnd
= GetHwnd();
678 // This gets the char index for the _beginning_ of this line
679 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
680 return (long)(x
+ charIndex
);
683 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
685 HWND hWnd
= GetHwnd();
687 // This gets the line number containing the character
688 int lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0);
689 // This gets the char index for the _beginning_ of this line
690 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
691 // The X position must therefore be the different between pos and charIndex
692 *x
= (long)(pos
- charIndex
);
696 void wxTextCtrl::ShowPosition(long pos
)
698 HWND hWnd
= GetHwnd();
700 // To scroll to a position, we pass the number of lines and characters
701 // to scroll *by*. This means that we need to:
702 // (1) Find the line position of the current line.
703 // (2) Find the line position of pos.
704 // (3) Scroll by (pos - current).
705 // For now, ignore the horizontal scrolling.
707 // Is this where scrolling is relative to - the line containing the caret?
708 // Or is the first visible line??? Try first visible line.
709 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
711 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
713 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
715 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
717 if (linesToScroll
!= 0)
718 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
721 int wxTextCtrl::GetLineLength(long lineNo
) const
723 long charIndex
= XYToPosition(0, lineNo
);
724 int len
= (int)SendMessage(GetHwnd(), EM_LINELENGTH
, charIndex
, 0);
728 wxString
wxTextCtrl::GetLineText(long lineNo
) const
730 size_t len
= (size_t)GetLineLength(lineNo
);
731 char *buf
= (char *)malloc(len
);
733 int noChars
= (int)SendMessage(GetHwnd(), EM_GETLINE
, lineNo
, (LPARAM
)buf
);
743 bool wxTextCtrl::CanCopy() const
745 // Can copy if there's a selection
747 GetSelection(& from
, & to
);
748 return (from
!= to
) ;
751 bool wxTextCtrl::CanCut() const
753 // Can cut if there's a selection
755 GetSelection(& from
, & to
);
756 return (from
!= to
) ;
759 bool wxTextCtrl::CanPaste() const
764 int dataFormat
= 0; // 0 == any format
765 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
771 // Standard edit control: check for straight text on clipboard
772 bool isTextAvailable
= FALSE
;
773 if (::OpenClipboard((HWND
) wxTheApp
->GetTopWindow()->GetHWND()))
775 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
778 return isTextAvailable
;
782 void wxTextCtrl::Undo()
786 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
790 void wxTextCtrl::Redo()
794 // Same as Undo, since Undo undoes the undo, i.e. a redo.
795 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
799 bool wxTextCtrl::CanUndo() const
801 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
804 bool wxTextCtrl::CanRedo() const
806 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
809 // If the return values from and to are the same, there is no
811 void wxTextCtrl::GetSelection(long* from
, long* to
) const
817 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
819 *from
= charRange
.cpMin
;
820 *to
= charRange
.cpMax
;
825 DWORD dwStart
, dwEnd
;
826 WPARAM wParam
= (WPARAM
) (DWORD
*) & dwStart
; // receives starting position
827 LPARAM lParam
= (LPARAM
) (DWORD
*) & dwEnd
; // receives ending position
829 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
835 bool wxTextCtrl::IsEditable() const
837 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
839 return ((style
& ES_READONLY
) == 0);
842 void wxTextCtrl::Command(wxCommandEvent
& event
)
844 SetValue (event
.GetString());
845 ProcessCommand (event
);
848 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
850 // By default, load the first file into the text window.
851 if (event
.GetNumberOfFiles() > 0)
853 LoadFile(event
.GetFiles()[0]);
857 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
858 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
860 //=========================================================================
861 // Called then the buffer is full (gcc 2.6.3)
862 // or when "endl" is output (Borland 4.5)
863 //=========================================================================
864 // Class declaration using multiple inheritance doesn't work properly for
865 // Borland. See note in textctrl.h.
866 #ifndef NO_TEXT_WINDOW_STREAM
867 int wxTextCtrl::overflow(int c
)
869 // Make sure there is a holding area
870 // this is not needed in <iostream> usage as it automagically allocates
871 // it, but does someone want to emulate it for safety's sake?
873 if ( allocate()==EOF
)
875 wxLogError("Streambuf allocation failed");
880 // Verify that there are no characters in get area
881 if ( gptr() && gptr() < egptr() )
883 wxError("Who's trespassing my get area?","Internal error");
890 // Make sure there is a put area
893 /* This doesn't seem to be fatal so comment out error message */
894 // wxError("Put area not opened","Internal error");
897 setp( base(), base() );
899 setp( pbase(), pbase() );
903 // Determine how many characters have been inserted but no consumed
904 int plen
= pptr() - pbase();
906 // Now Jerry relies on the fact that the buffer is at least 2 chars
907 // long, but the holding area "may be as small as 1" ???
908 // And we need an additional \0, so let's keep this inefficient but
911 // If c!=EOF, it is a character that must also be comsumed
912 int xtra
= c
==EOF
? 0 : 1;
914 // Write temporary C-string to wxTextWindow
916 char *txt
= new char[plen
+xtra
+1];
917 memcpy(txt
, pbase(), plen
);
918 txt
[plen
] = (char)c
; // append c
919 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
920 // If the put area already contained \0, output will be truncated there
926 setp(pbase(), epptr());
928 #if defined(__WATCOMC__)
930 #elif defined(zapeof) // HP-UX (all cfront based?)
933 return c
!=EOF
? c
: 0; // this should make everybody happy
937 int len = pptr() - pbase();
938 char *txt = new char[len+1];
939 strncpy(txt, pbase(), len);
942 setp(pbase(), epptr());
948 //=========================================================================
949 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
950 //=========================================================================
951 int wxTextCtrl::sync()
953 // Verify that there are no characters in get area
954 if ( gptr() && gptr() < egptr() )
956 wxError("Who's trespassing my get area?","Internal error");
960 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
964 int len = pptr() - pbase();
965 char *txt = new char[len+1];
966 strncpy(txt, pbase(), len);
969 setp(pbase(), epptr());
975 //=========================================================================
976 // Should not be called by a "ostream". Used by a "istream"
977 //=========================================================================
978 int wxTextCtrl::underflow()
984 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
990 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
993 str
.Printf(_T("%.2f"), f
);
998 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
1001 str
.Printf(_T("%.2f"), d
);
1006 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
1009 str
.Printf(_T("%d"), i
);
1014 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
1017 str
.Printf(_T("%ld"), i
);
1022 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
1032 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
1033 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1038 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1039 return (WXHBRUSH
) hbrush
;
1043 if (GetParent()->GetTransparentBackground())
1044 SetBkMode((HDC
) pDC
, TRANSPARENT
);
1046 SetBkMode((HDC
) pDC
, OPAQUE
);
1048 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
1049 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
1051 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
1053 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
1054 // has a zero usage count.
1055 // NOT NOW - will be cleaned up at end of app.
1056 // backgroundBrush->RealizeResource();
1057 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
1060 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1062 switch ( event
.KeyCode() )
1065 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1067 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1068 event
.SetEventObject( this );
1069 if ( GetEventHandler()->ProcessEvent(event
) )
1072 //else: multiline controls need Enter for themselves
1077 // always produce navigation event - even if we process TAB
1078 // ourselves the fact that we got here means that the user code
1079 // decided to skip processing of this TAB - probably to let it
1080 // do its default job.
1082 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
1083 // handled by Windows
1085 wxNavigationKeyEvent eventNav
;
1086 eventNav
.SetDirection(!event
.ShiftDown());
1087 eventNav
.SetWindowChange(FALSE
);
1088 eventNav
.SetEventObject(this);
1090 if ( GetEventHandler()->ProcessEvent(eventNav
) )
1100 // don't just call event.Skip() because this will cause TABs and ENTERs
1101 // be passed upwards and we don't always want this - instead process it
1108 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1115 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1118 event
.SetEventObject( this );
1119 GetEventHandler()->ProcessEvent(event
);
1125 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1126 wxString
val(GetValue());
1127 if ( !val
.IsNull() )
1128 event
.m_commandString
= WXSTRINGCAST val
;
1129 event
.SetEventObject( this );
1130 ProcessCommand(event
);
1135 // the text size limit has been hit - increase it
1139 // the other notification messages are not processed
1152 void wxTextCtrl::AdjustSpaceLimit()
1155 unsigned int len
= ::GetWindowTextLength(GetHwnd()),
1156 limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1159 limit
= len
+ 0x8000; // 32Kb
1162 if ( m_isRich
|| limit
> 0xffff )
1164 if ( limit
> 0xffff )
1166 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, 0, limit
);
1168 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1173 // For Rich Edit controls. Do we need it?
1176 bool wxTextCtrl::MSWOnNotify(WXWPARAM wParam
, WXLPARAM lParam
)
1178 wxCommandEvent
event(0, m_windowId
);
1180 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1181 switch ( hdr1
->code
)
1183 // Insert case code here
1185 return wxControl::MSWOnNotify(wParam
, lParam
);
1189 event
.SetEventObject( this );
1190 event
.SetEventType(eventType
);
1192 if ( !GetEventHandler()->ProcessEvent(event
) )
1200 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
1205 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
1210 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
1215 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
1220 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
1225 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1227 event
.Enable( CanCut() );
1230 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1232 event
.Enable( CanCopy() );
1235 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1237 event
.Enable( CanPaste() );
1240 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1242 event
.Enable( CanUndo() );
1245 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1247 event
.Enable( CanRedo() );
1250 bool wxTextCtrl::AcceptsFocus() const
1252 // we don't want focus if we can't be edited
1253 return IsEditable() && wxControl::AcceptsFocus();