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 const wxChar
*windowClass
= _T("EDIT");
157 if ( m_windowStyle
& wxTE_MULTILINE
)
159 msStyle
|= ES_AUTOVSCROLL
;
161 windowClass
= _T("RichEdit") ;
168 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
170 // If we're in Win95, and we want a simple 2D border,
171 // then make it an EDIT control instead.
173 if (m_windowStyle
& wxSIMPLE_BORDER
)
175 windowClass
= _T("EDIT");
180 // Even with extended styles, need to combine with WS_BORDER
181 // for them to look right.
182 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
183 msStyle
|= WS_BORDER
;
185 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, windowClass
, NULL
,
187 0, 0, 0, 0, (HWND
) ((wxWindow
*)parent
)->GetHWND(), (HMENU
)m_windowId
,
188 wxGetInstance(), NULL
);
190 wxCHECK_MSG( m_hWnd
, FALSE
, _T("Failed to create text ctrl") );
195 Ctl3dSubclassCtl((HWND
)m_hWnd
);
203 // Have to enable events
204 ::SendMessage((HWND
)m_hWnd
, EM_SETEVENTMASK
, 0,
205 ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
);
209 SubclassWin(GetHWND());
211 if ( parent
->GetFont().Ok() && parent
->GetFont().Ok() )
213 SetFont(parent
->GetFont());
217 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
220 SetSize(x
, y
, width
, height
);
222 // Causes a crash for Symantec C++ and WIN32 for some reason
223 #if !(defined(__SC__) && defined(__WIN32__))
224 if ( !value
.IsEmpty() )
233 // Make sure the window style (etc.) reflects the HWND style (roughly)
234 void wxTextCtrl::AdoptAttributesFromHWND()
236 wxWindow::AdoptAttributesFromHWND();
238 HWND hWnd
= GetHwnd();
239 long style
= GetWindowLong((HWND
) hWnd
, GWL_STYLE
);
241 // retrieve the style to see whether this is an edit or richedit ctrl
246 GetClassName((HWND
) hWnd
, buf
, 256);
249 GetClassNameW((HWND
) hWnd
, buf
, 256);
252 GetClassName((HWND
) hWnd
, buf
, 256);
254 GetClassNameA((HWND
) hWnd
, buf
, 256);
262 if (str
== _T("EDIT"))
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
;
278 void wxTextCtrl::SetupColours()
280 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
281 SetForegroundColour(GetParent()->GetForegroundColour());
284 wxString
wxTextCtrl::GetValue() const
286 return wxGetWindowText(GetHWND());
289 void wxTextCtrl::SetValue(const wxString
& value
)
291 // If newlines are denoted by just 10, must stick 13 in front.
293 int len
= value
.Length();
295 for (i
= 0; i
< len
; i
++)
297 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
302 wxChar
*tmp
= new wxChar
[len
+ singletons
+ 1];
304 for (i
= 0; i
< len
; i
++)
306 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
315 SetWindowText(GetHwnd(), tmp
);
319 SetWindowText(GetHwnd(), (const wxChar
*)value
);
324 void wxTextCtrl::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
326 int currentX
, currentY
;
327 GetPosition(¤tX
, ¤tY
);
333 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
335 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
338 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
340 int cx
; // button font dimensions
343 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
345 int control_width
, control_height
, control_x
, control_y
;
347 // If we're prepared to use the existing size, then...
348 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
353 // Deal with default size (using -1 values)
355 w1
= DEFAULT_ITEM_WIDTH
;
362 // Calculations may have made text size too small
363 if (control_height
<= 0)
364 control_height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
366 if (control_width
<= 0)
367 control_width
= DEFAULT_ITEM_WIDTH
;
369 MoveWindow(GetHwnd(), (int)control_x
, (int)control_y
,
370 (int)control_width
, (int)control_height
, TRUE
);
373 // Clipboard operations
374 void wxTextCtrl::Copy()
378 HWND hWnd
= GetHwnd();
379 SendMessage(hWnd
, WM_COPY
, 0, 0L);
383 void wxTextCtrl::Cut()
387 HWND hWnd
= GetHwnd();
388 SendMessage(hWnd
, WM_CUT
, 0, 0L);
392 void wxTextCtrl::Paste()
396 HWND hWnd
= GetHwnd();
397 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
401 void wxTextCtrl::SetEditable(bool editable
)
403 HWND hWnd
= GetHwnd();
404 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
407 void wxTextCtrl::SetInsertionPoint(long pos
)
409 HWND hWnd
= GetHwnd();
417 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
418 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
423 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
424 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
427 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
430 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
433 void wxTextCtrl::SetInsertionPointEnd()
435 long pos
= GetLastPosition();
436 SetInsertionPoint(pos
);
439 long wxTextCtrl::GetInsertionPoint() const
447 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
452 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
456 long wxTextCtrl::GetLastPosition() const
458 HWND hWnd
= GetHwnd();
460 // Will always return a number > 0 (according to docs)
461 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
463 // This gets the char index for the _beginning_ of the last line
464 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
466 // Get number of characters in the last line. We'll add this to the character
467 // index for the last line, 1st position.
468 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
470 return (long)(charIndex
+ lineLength
);
473 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
476 HWND hWnd
= GetHwnd();
477 long fromChar
= from
;
480 // Set selection and remove it
482 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
484 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
486 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
488 // Now replace with 'value', by pasting.
489 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const wxChar
*)value
, 0, 0);
491 // Paste into edit control
492 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
494 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
498 void wxTextCtrl::Remove(long from
, long to
)
500 HWND hWnd
= GetHwnd();
501 long fromChar
= from
;
504 // Cut all selected text
506 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
508 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
510 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
513 void wxTextCtrl::SetSelection(long from
, long to
)
515 HWND hWnd
= GetHwnd();
516 long fromChar
= from
;
518 // if from and to are both -1, it means
519 // (in wxWindows) that all text should be selected.
520 // This translates into Windows convention
521 if ((from
== -1) && (to
== -1))
528 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
529 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
531 // WPARAM is 0: selection is scrolled into view
532 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
536 bool wxTextCtrl::LoadFile(const wxString
& file
)
538 if (!wxFileExists(WXSTRINGCAST file
))
545 // ifstream input(WXSTRINGCAST file, ios::nocreate | ios::in);
546 ifstream
input(MBSTRINGCAST file
.mb_str(wxConvFile
), ios::in
);
550 // Previously a SETSEL/REPLACESEL call-pair were done to insert
551 // line by line into the control. Apart from being very slow this
552 // was limited to 32K of text by the external interface presenting
553 // positions as signed shorts. Now load in one chunk...
554 // Note use of 'farmalloc' as in Borland 3.1 'size_t' is 16-bits...
557 struct _stat stat_buf
;
558 if (stat(MBSTRINGCAST file
.mb_str(wxConvFile
), &stat_buf
) < 0)
561 struct stat stat_buf
;
562 if (stat(file
.mb_str(wxConvFile
), &stat_buf
) < 0)
566 // wxChar *tmp_buffer = (wxChar*)farmalloc(stat_buf.st_size+1);
567 // This may need to be a bigger buffer than the file size suggests,
568 // if it's a UNIX file. Give it an extra 1000 just in case.
569 wxChar
*tmp_buffer
= (wxChar
*)farmalloc((size_t)(stat_buf
.st_size
+1+1000));
570 char *read_buffer
= new char[512];
573 while (!input
.eof() && input
.peek() != EOF
)
575 input
.getline(read_buffer
, 500);
576 int len
= strlen(read_buffer
);
578 wxBuffer
[len
+1] = 10;
581 pos
+= wxConvCurrent
->MB2WC(tmp_buffer
+pos
, read_buffer
, (size_t)-1);
583 strcpy(tmp_buffer
+pos
, read_buffer
);
584 pos
+= strlen(read_buffer
);
588 delete[] read_buffer
;
590 SetWindowText(GetHwnd(), tmp_buffer
);
591 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
594 // update the size limit if needed
602 // If file is null, try saved file name first
603 // Returns TRUE if succeeds.
604 bool wxTextCtrl::SaveFile(const wxString
& file
)
606 wxString
theFile(file
);
608 if (theFile
== _T(""))
609 theFile
= m_fileName
;
611 if (theFile
== _T(""))
614 m_fileName
= theFile
;
616 ofstream
output(MBSTRINGCAST theFile
.mb_str(wxConvFile
));
620 // This will only save 64K max
621 unsigned long nbytes
= SendMessage(GetHwnd(), WM_GETTEXTLENGTH
, 0, 0);
622 char *tmp_buffer
= (char*)farmalloc((size_t)(nbytes
+1));
623 SendMessage(GetHwnd(), WM_GETTEXT
, (WPARAM
)(nbytes
+1), (LPARAM
)tmp_buffer
);
624 char *pstr
= tmp_buffer
;
626 // Convert \r\n to just \n
635 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
640 void wxTextCtrl::WriteText(const wxString
& text
)
643 int len
= text
.Length();
644 char *newtext
= new char[(len
*2)+1];
654 newtext
[j
] = text
[i
];
659 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)newtext
);
665 void wxTextCtrl::AppendText(const wxString
& text
)
667 SetInsertionPointEnd();
671 void wxTextCtrl::Clear()
673 SetWindowText(GetHwnd(), _T(""));
676 bool wxTextCtrl::IsModified() const
678 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
681 // Makes 'unmodified'
682 void wxTextCtrl::DiscardEdits()
684 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
688 * Some of the following functions are yet to be implemented
692 int wxTextCtrl::GetNumberOfLines() const
694 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
697 long wxTextCtrl::XYToPosition(long x
, long y
) const
699 HWND hWnd
= GetHwnd();
701 // This gets the char index for the _beginning_ of this line
702 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
703 return (long)(x
+ charIndex
);
706 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
708 HWND hWnd
= GetHwnd();
710 // This gets the line number containing the character
711 int lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0);
712 // This gets the char index for the _beginning_ of this line
713 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
714 // The X position must therefore be the different between pos and charIndex
715 *x
= (long)(pos
- charIndex
);
719 void wxTextCtrl::ShowPosition(long pos
)
721 HWND hWnd
= GetHwnd();
723 // To scroll to a position, we pass the number of lines and characters
724 // to scroll *by*. This means that we need to:
725 // (1) Find the line position of the current line.
726 // (2) Find the line position of pos.
727 // (3) Scroll by (pos - current).
728 // For now, ignore the horizontal scrolling.
730 // Is this where scrolling is relative to - the line containing the caret?
731 // Or is the first visible line??? Try first visible line.
732 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
734 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
736 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
738 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
740 if (linesToScroll
!= 0)
741 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
744 int wxTextCtrl::GetLineLength(long lineNo
) const
746 long charIndex
= XYToPosition(0, lineNo
);
747 HWND hWnd
= GetHwnd();
748 int len
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0);
752 wxString
wxTextCtrl::GetLineText(long lineNo
) const
754 HWND hWnd
= GetHwnd();
755 *(WORD
*)wxBuffer
= 512;
756 int noChars
= (int)SendMessage(hWnd
, EM_GETLINE
, (WPARAM
)lineNo
, (LPARAM
)wxBuffer
);
757 wxBuffer
[noChars
] = 0;
758 return wxString(wxBuffer
);
761 bool wxTextCtrl::CanCopy() const
763 // Can copy if there's a selection
765 GetSelection(& from
, & to
);
766 return (from
!= to
) ;
769 bool wxTextCtrl::CanCut() const
771 // Can cut if there's a selection
773 GetSelection(& from
, & to
);
774 return (from
!= to
) ;
777 bool wxTextCtrl::CanPaste() const
782 int dataFormat
= 0; // 0 == any format
783 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
789 // Standard edit control: check for straight text on clipboard
790 bool isTextAvailable
= FALSE
;
791 if (::OpenClipboard((HWND
) wxTheApp
->GetTopWindow()->GetHWND()))
793 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
796 return isTextAvailable
;
800 void wxTextCtrl::Undo()
804 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
808 void wxTextCtrl::Redo()
812 // Same as Undo, since Undo undoes the undo, i.e. a redo.
813 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
817 bool wxTextCtrl::CanUndo() const
819 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
822 bool wxTextCtrl::CanRedo() const
824 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
827 // If the return values from and to are the same, there is no
829 void wxTextCtrl::GetSelection(long* from
, long* to
) const
835 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
837 *from
= charRange
.cpMin
;
838 *to
= charRange
.cpMax
;
843 DWORD dwStart
, dwEnd
;
844 WPARAM wParam
= (WPARAM
) (DWORD
*) & dwStart
; // receives starting position
845 LPARAM lParam
= (LPARAM
) (DWORD
*) & dwEnd
; // receives ending position
847 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
853 bool wxTextCtrl::IsEditable() const
855 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
857 return ((style
& ES_READONLY
) == 0);
860 void wxTextCtrl::Command(wxCommandEvent
& event
)
862 SetValue (event
.GetString());
863 ProcessCommand (event
);
866 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
868 // By default, load the first file into the text window.
869 if (event
.GetNumberOfFiles() > 0)
871 LoadFile(event
.GetFiles()[0]);
875 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
876 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
878 //=========================================================================
879 // Called then the buffer is full (gcc 2.6.3)
880 // or when "endl" is output (Borland 4.5)
881 //=========================================================================
882 // Class declaration using multiple inheritance doesn't work properly for
883 // Borland. See note in textctrl.h.
884 #ifndef NO_TEXT_WINDOW_STREAM
885 int wxTextCtrl::overflow(int c
)
887 // Make sure there is a holding area
888 // this is not needed in <iostream> usage as it automagically allocates
889 // it, but does someone want to emulate it for safety's sake?
891 if ( allocate()==EOF
)
893 wxLogError("Streambuf allocation failed");
898 // Verify that there are no characters in get area
899 if ( gptr() && gptr() < egptr() )
901 wxError("Who's trespassing my get area?","Internal error");
908 // Make sure there is a put area
911 /* This doesn't seem to be fatal so comment out error message */
912 // wxError("Put area not opened","Internal error");
915 setp( base(), base() );
917 setp( pbase(), pbase() );
921 // Determine how many characters have been inserted but no consumed
922 int plen
= pptr() - pbase();
924 // Now Jerry relies on the fact that the buffer is at least 2 chars
925 // long, but the holding area "may be as small as 1" ???
926 // And we need an additional \0, so let's keep this inefficient but
929 // If c!=EOF, it is a character that must also be comsumed
930 int xtra
= c
==EOF
? 0 : 1;
932 // Write temporary C-string to wxTextWindow
934 char *txt
= new char[plen
+xtra
+1];
935 memcpy(txt
, pbase(), plen
);
936 txt
[plen
] = (char)c
; // append c
937 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
938 // If the put area already contained \0, output will be truncated there
944 setp(pbase(), epptr());
946 #if defined(__WATCOMC__)
948 #elif defined(zapeof) // HP-UX (all cfront based?)
951 return c
!=EOF
? c
: 0; // this should make everybody happy
955 int len = pptr() - pbase();
956 char *txt = new char[len+1];
957 strncpy(txt, pbase(), len);
960 setp(pbase(), epptr());
966 //=========================================================================
967 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
968 //=========================================================================
969 int wxTextCtrl::sync()
971 // Verify that there are no characters in get area
972 if ( gptr() && gptr() < egptr() )
974 wxError("Who's trespassing my get area?","Internal error");
978 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
982 int len = pptr() - pbase();
983 char *txt = new char[len+1];
984 strncpy(txt, pbase(), len);
987 setp(pbase(), epptr());
993 //=========================================================================
994 // Should not be called by a "ostream". Used by a "istream"
995 //=========================================================================
996 int wxTextCtrl::underflow()
1002 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
1008 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
1011 str
.Printf(_T("%.2f"), f
);
1016 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
1019 str
.Printf(_T("%.2f"), d
);
1024 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
1027 str
.Printf(_T("%d"), i
);
1032 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
1035 str
.Printf(_T("%ld"), i
);
1040 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
1050 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
1051 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1056 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1057 return (WXHBRUSH
) hbrush
;
1061 if (GetParent()->GetTransparentBackground())
1062 SetBkMode((HDC
) pDC
, TRANSPARENT
);
1064 SetBkMode((HDC
) pDC
, OPAQUE
);
1066 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
1067 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
1069 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
1071 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
1072 // has a zero usage count.
1073 // NOT NOW - will be cleaned up at end of app.
1074 // backgroundBrush->RealizeResource();
1075 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
1078 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1080 switch ( event
.KeyCode() )
1083 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1085 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1086 event
.SetEventObject( this );
1087 if ( GetEventHandler()->ProcessEvent(event
) )
1090 //else: multiline controls need Enter for themselves
1095 // only produce navigation event if we don't process TAB ourself or
1096 // if it's a Shift-Tab keypress (we assume nobody will ever need
1097 // this key combo for himself)
1099 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
1100 // handled by Windows
1101 if ( event
.ShiftDown() || !(m_windowStyle
& wxTE_PROCESS_TAB
) )
1103 wxNavigationKeyEvent eventNav
;
1104 eventNav
.SetDirection(!event
.ShiftDown());
1105 eventNav
.SetWindowChange(FALSE
);
1106 eventNav
.SetEventObject(this);
1108 if ( GetEventHandler()->ProcessEvent(eventNav
) )
1118 // don't just call event.Skip() because this will cause TABs and ENTERs
1119 // be passed upwards and we don't always want this - instead process it
1126 long wxTextCtrl::MSWGetDlgCode()
1128 // we always want the characters and the arrows
1129 long lRc
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1131 // we may have several different cases:
1132 // 1. normal case: both TAB and ENTER are used for dialog navigation
1133 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
1134 // control in the dialog
1135 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
1136 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
1138 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
1139 lRc
|= DLGC_WANTMESSAGE
;
1140 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
1141 lRc
|= DLGC_WANTTAB
;
1146 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1153 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1156 event
.SetEventObject( this );
1157 GetEventHandler()->ProcessEvent(event
);
1163 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1164 wxString
val(GetValue());
1165 if ( !val
.IsNull() )
1166 event
.m_commandString
= WXSTRINGCAST val
;
1167 event
.SetEventObject( this );
1168 ProcessCommand(event
);
1173 // the text size limit has been hit - increase it
1177 // the other notification messages are not processed
1190 void wxTextCtrl::AdjustSpaceLimit()
1193 unsigned int len
= ::GetWindowTextLength(GetHwnd()),
1194 limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1197 limit
= len
+ 0x8000; // 32Kb
1200 if ( m_isRich
|| limit
> 0xffff )
1202 if ( limit
> 0xffff )
1204 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, 0, limit
);
1206 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1211 // For Rich Edit controls. Do we need it?
1214 bool wxTextCtrl::MSWOnNotify(WXWPARAM wParam
, WXLPARAM lParam
)
1216 wxCommandEvent
event(0, m_windowId
);
1218 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1219 switch ( hdr1
->code
)
1221 // Insert case code here
1223 return wxControl::MSWOnNotify(wParam
, lParam
);
1227 event
.SetEventObject( this );
1228 event
.SetEventType(eventType
);
1230 if ( !GetEventHandler()->ProcessEvent(event
) )
1238 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
1243 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
1248 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
1253 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
1258 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
1263 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1265 event
.Enable( CanCut() );
1268 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1270 event
.Enable( CanCopy() );
1273 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1275 event
.Enable( CanPaste() );
1278 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1280 event
.Enable( CanUndo() );
1283 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1285 event
.Enable( CanRedo() );