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
)
77 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
79 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
80 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
81 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
82 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
83 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
85 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
86 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
87 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
88 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
89 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
92 #endif // USE_SHARED_LIBRARY
95 wxTextCtrl::wxTextCtrl()
96 #ifndef NO_TEXT_WINDOW_STREAM
105 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
106 const wxString
& value
,
108 const wxSize
& size
, long style
,
109 const wxValidator
& validator
,
110 const wxString
& name
)
113 SetValidator(validator
);
114 if (parent
) parent
->AddChild(this);
116 m_windowStyle
= style
;
118 // Should this be taken from the system colours?
119 // SetBackgroundColour(wxColour(255, 255, 255));
121 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
123 SetForegroundColour(parent
->GetForegroundColour()) ;
126 m_windowId
= (int)NewControlId();
135 long msStyle
= ES_LEFT
| WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
136 if (m_windowStyle
& wxTE_MULTILINE
)
138 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
139 "wxTE_PROCESS_ENTER style is ignored for multiline controls" );
141 msStyle
|= ES_MULTILINE
| ES_WANTRETURN
| WS_VSCROLL
; // WS_BORDER
142 m_windowStyle
|= wxTE_PROCESS_ENTER
;
145 msStyle
|= ES_AUTOHSCROLL
;
147 if (m_windowStyle
& wxTE_READONLY
)
148 msStyle
|= ES_READONLY
;
150 if (m_windowStyle
& wxHSCROLL
)
151 msStyle
|= (WS_HSCROLL
| ES_AUTOHSCROLL
) ;
152 if (m_windowStyle
& wxTE_PASSWORD
) // hidden input
153 msStyle
|= ES_PASSWORD
;
155 const char *windowClass
= "EDIT";
158 if ( m_windowStyle
& wxTE_MULTILINE
)
160 msStyle
|= ES_AUTOVSCROLL
;
162 windowClass
= "RichEdit" ;
169 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
171 // If we're in Win95, and we want a simple 2D border,
172 // then make it an EDIT control instead.
174 if (m_windowStyle
& wxSIMPLE_BORDER
)
176 windowClass
= "EDIT";
181 // Even with extended styles, need to combine with WS_BORDER
182 // for them to look right.
183 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
184 msStyle
|= WS_BORDER
;
186 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, windowClass
, NULL
,
188 0, 0, 0, 0, (HWND
) ((wxWindow
*)parent
)->GetHWND(), (HMENU
)m_windowId
,
189 wxGetInstance(), NULL
);
191 wxCHECK_MSG( m_hWnd
, FALSE
, "Failed to create text ctrl" );
196 Ctl3dSubclassCtl((HWND
)m_hWnd
);
204 // Have to enable events
205 ::SendMessage((HWND
)m_hWnd
, EM_SETEVENTMASK
, 0,
206 ENM_CHANGE
| ENM_DROPFILES
| ENM_SELCHANGE
| ENM_UPDATE
);
210 SubclassWin(GetHWND());
212 if ( parent
->GetFont().Ok() && parent
->GetFont().Ok() )
214 SetFont(parent
->GetFont());
218 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT
));
221 SetSize(x
, y
, width
, height
);
223 // Causes a crash for Symantec C++ and WIN32 for some reason
224 #if !(defined(__SC__) && defined(__WIN32__))
225 if ( !value
.IsEmpty() )
234 // Make sure the window style (etc.) reflects the HWND style (roughly)
235 void wxTextCtrl::AdoptAttributesFromHWND()
237 wxWindow::AdoptAttributesFromHWND();
239 HWND hWnd
= (HWND
) GetHWND();
240 long style
= GetWindowLong((HWND
) hWnd
, GWL_STYLE
);
242 // retrieve the style to see whether this is an edit or richedit ctrl
247 GetClassName((HWND
) hWnd
, buf
, 256);
250 GetClassNameW((HWND
) hWnd
, buf
, 256);
253 GetClassName((HWND
) hWnd
, buf
, 256);
255 GetClassNameA((HWND
) hWnd
, buf
, 256);
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
;
279 void wxTextCtrl::SetupColours()
281 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
282 SetForegroundColour(GetParent()->GetForegroundColour());
285 wxString
wxTextCtrl::GetValue() const
287 int length
= GetWindowTextLength((HWND
) GetHWND());
288 char *s
= new char[length
+1];
289 GetWindowText((HWND
) GetHWND(), s
, length
+1);
295 void wxTextCtrl::SetValue(const wxString
& value
)
297 // If newlines are denoted by just 10, must stick 13 in front.
299 int len
= value
.Length();
301 for (i
= 0; i
< len
; i
++)
303 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
308 char *tmp
= new char[len
+ singletons
+ 1];
310 for (i
= 0; i
< len
; i
++)
312 if ((i
> 0) && (value
[i
] == 10) && (value
[i
-1] != 13))
321 SetWindowText((HWND
) GetHWND(), tmp
);
325 SetWindowText((HWND
) GetHWND(), (const char *)value
);
328 void wxTextCtrl::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
330 int currentX
, currentY
;
331 GetPosition(¤tX
, ¤tY
);
337 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
339 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
342 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
344 int cx
; // button font dimensions
347 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
349 int control_width
, control_height
, control_x
, control_y
;
351 // If we're prepared to use the existing size, then...
352 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
357 // Deal with default size (using -1 values)
359 w1
= DEFAULT_ITEM_WIDTH
;
366 // Calculations may have made text size too small
367 if (control_height
<= 0)
368 control_height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
370 if (control_width
<= 0)
371 control_width
= DEFAULT_ITEM_WIDTH
;
373 MoveWindow((HWND
) GetHWND(), (int)control_x
, (int)control_y
,
374 (int)control_width
, (int)control_height
, TRUE
);
377 // Clipboard operations
378 void wxTextCtrl::Copy()
382 HWND hWnd
= (HWND
) GetHWND();
383 SendMessage(hWnd
, WM_COPY
, 0, 0L);
387 void wxTextCtrl::Cut()
391 HWND hWnd
= (HWND
) GetHWND();
392 SendMessage(hWnd
, WM_CUT
, 0, 0L);
396 void wxTextCtrl::Paste()
400 HWND hWnd
= (HWND
) GetHWND();
401 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
405 void wxTextCtrl::SetEditable(bool editable
)
407 HWND hWnd
= (HWND
) GetHWND();
408 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
411 void wxTextCtrl::SetInsertionPoint(long pos
)
413 HWND hWnd
= (HWND
) GetHWND();
421 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
422 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
427 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
428 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
431 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
434 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
437 void wxTextCtrl::SetInsertionPointEnd()
439 long pos
= GetLastPosition();
440 SetInsertionPoint(pos
);
443 long wxTextCtrl::GetInsertionPoint() const
451 SendMessage((HWND
) GetHWND(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
456 DWORD Pos
=(DWORD
)SendMessage((HWND
) GetHWND(), EM_GETSEL
, 0, 0L);
460 long wxTextCtrl::GetLastPosition() const
462 HWND hWnd
= (HWND
) GetHWND();
464 // Will always return a number > 0 (according to docs)
465 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
467 // This gets the char index for the _beginning_ of the last line
468 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
470 // Get number of characters in the last line. We'll add this to the character
471 // index for the last line, 1st position.
472 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
474 return (long)(charIndex
+ lineLength
);
477 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
480 HWND hWnd
= (HWND
) GetHWND();
481 long fromChar
= from
;
484 // Set selection and remove it
486 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
488 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
490 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
492 // Now replace with 'value', by pasting.
493 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const char *)value
, 0, 0);
495 // Paste into edit control
496 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
498 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
502 void wxTextCtrl::Remove(long from
, long to
)
504 HWND hWnd
= (HWND
) GetHWND();
505 long fromChar
= from
;
508 // Cut all selected text
510 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
512 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
514 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
517 void wxTextCtrl::SetSelection(long from
, long to
)
519 HWND hWnd
= (HWND
) GetHWND();
520 long fromChar
= from
;
522 // if from and to are both -1, it means
523 // (in wxWindows) that all text should be selected.
524 // This translates into Windows convention
525 if ((from
== -1) && (to
== -1))
532 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
533 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
535 // WPARAM is 0: selection is scrolled into view
536 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
540 bool wxTextCtrl::LoadFile(const wxString
& file
)
542 if (!wxFileExists(WXSTRINGCAST file
))
549 // ifstream input(WXSTRINGCAST file, ios::nocreate | ios::in);
550 ifstream
input(WXSTRINGCAST file
, ios::in
);
554 // Previously a SETSEL/REPLACESEL call-pair were done to insert
555 // line by line into the control. Apart from being very slow this
556 // was limited to 32K of text by the external interface presenting
557 // positions as signed shorts. Now load in one chunk...
558 // Note use of 'farmalloc' as in Borland 3.1 'size_t' is 16-bits...
561 struct _stat stat_buf
;
562 if (stat((char*) (const char*) file
, &stat_buf
) < 0)
565 struct stat stat_buf
;
566 if (stat(file
, &stat_buf
) < 0)
570 // char *tmp_buffer = (char*)farmalloc(stat_buf.st_size+1);
571 // This may need to be a bigger buffer than the file size suggests,
572 // if it's a UNIX file. Give it an extra 1000 just in case.
573 char *tmp_buffer
= (char*)farmalloc((size_t)(stat_buf
.st_size
+1+1000));
576 while (!input
.eof() && input
.peek() != EOF
)
578 input
.getline(wxBuffer
, 500);
579 int len
= strlen(wxBuffer
);
581 wxBuffer
[len
+1] = 10;
583 strcpy(tmp_buffer
+pos
, wxBuffer
);
584 pos
+= strlen(wxBuffer
);
588 // SendMessage((HWND) GetHWND(), WM_SETTEXT, 0, (LPARAM)tmp_buffer);
589 SetWindowText((HWND
) GetHWND(), tmp_buffer
);
590 SendMessage((HWND
) GetHWND(), EM_SETMODIFY
, FALSE
, 0L);
598 // If file is null, try saved file name first
599 // Returns TRUE if succeeds.
600 bool wxTextCtrl::SaveFile(const wxString
& file
)
602 wxString
theFile(file
);
605 theFile
= m_fileName
;
610 m_fileName
= theFile
;
612 ofstream
output((char*) (const char*) theFile
);
616 // This will only save 64K max
617 unsigned long nbytes
= SendMessage((HWND
) GetHWND(), WM_GETTEXTLENGTH
, 0, 0);
618 char *tmp_buffer
= (char*)farmalloc((size_t)(nbytes
+1));
619 SendMessage((HWND
) GetHWND(), WM_GETTEXT
, (WPARAM
)(nbytes
+1), (LPARAM
)tmp_buffer
);
620 char *pstr
= tmp_buffer
;
622 // Convert \r\n to just \n
631 SendMessage((HWND
) GetHWND(), EM_SETMODIFY
, FALSE
, 0L);
636 void wxTextCtrl::WriteText(const wxString
& text
)
639 int len
= text
.Length();
640 char *newtext
= new char[(len
*2)+1];
650 newtext
[j
] = text
[i
];
655 SendMessage((HWND
) GetHWND(), EM_REPLACESEL
, 0, (LPARAM
)newtext
);
659 void wxTextCtrl::AppendText(const wxString
& text
)
661 SetInsertionPointEnd();
665 void wxTextCtrl::Clear()
667 SetWindowText((HWND
) GetHWND(), "");
670 bool wxTextCtrl::IsModified() const
672 return (SendMessage((HWND
) GetHWND(), EM_GETMODIFY
, 0, 0) != 0);
675 // Makes 'unmodified'
676 void wxTextCtrl::DiscardEdits()
678 SendMessage((HWND
) GetHWND(), EM_SETMODIFY
, FALSE
, 0L);
682 * Some of the following functions are yet to be implemented
686 int wxTextCtrl::GetNumberOfLines() const
688 return (int)SendMessage((HWND
) GetHWND(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
691 long wxTextCtrl::XYToPosition(long x
, long y
) const
693 HWND hWnd
= (HWND
) GetHWND();
695 // This gets the char index for the _beginning_ of this line
696 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
697 return (long)(x
+ charIndex
);
700 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
702 HWND hWnd
= (HWND
) GetHWND();
704 // This gets the line number containing the character
705 int lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0);
706 // This gets the char index for the _beginning_ of this line
707 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
708 // The X position must therefore be the different between pos and charIndex
709 *x
= (long)(pos
- charIndex
);
713 void wxTextCtrl::ShowPosition(long pos
)
715 HWND hWnd
= (HWND
) GetHWND();
717 // To scroll to a position, we pass the number of lines and characters
718 // to scroll *by*. This means that we need to:
719 // (1) Find the line position of the current line.
720 // (2) Find the line position of pos.
721 // (3) Scroll by (pos - current).
722 // For now, ignore the horizontal scrolling.
724 // Is this where scrolling is relative to - the line containing the caret?
725 // Or is the first visible line??? Try first visible line.
726 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
728 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
730 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
732 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
734 if (linesToScroll
!= 0)
735 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
738 int wxTextCtrl::GetLineLength(long lineNo
) const
740 long charIndex
= XYToPosition(0, lineNo
);
741 HWND hWnd
= (HWND
) GetHWND();
742 int len
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0);
746 wxString
wxTextCtrl::GetLineText(long lineNo
) const
748 HWND hWnd
= (HWND
) GetHWND();
749 *(WORD
*)wxBuffer
= 512;
750 int noChars
= (int)SendMessage(hWnd
, EM_GETLINE
, (WPARAM
)lineNo
, (LPARAM
)wxBuffer
);
751 wxBuffer
[noChars
] = 0;
752 return wxString(wxBuffer
);
755 bool wxTextCtrl::CanCopy() const
757 // Can copy if there's a selection
759 GetSelection(& from
, & to
);
760 return (from
!= to
) ;
763 bool wxTextCtrl::CanCut() const
765 // Can cut if there's a selection
767 GetSelection(& from
, & to
);
768 return (from
!= to
) ;
771 bool wxTextCtrl::CanPaste() const
776 int dataFormat
= 0; // 0 == any format
777 return (::SendMessage( (HWND
) GetHWND(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
783 // Standard edit control: check for straight text on clipboard
784 bool isTextAvailable
= FALSE
;
785 if (::OpenClipboard((HWND
) wxTheApp
->GetTopWindow()->GetHWND()))
787 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
790 return isTextAvailable
;
794 void wxTextCtrl::Undo()
798 ::SendMessage((HWND
) GetHWND(), EM_UNDO
, 0, 0);
802 void wxTextCtrl::Redo()
806 // Same as Undo, since Undo undoes the undo, i.e. a redo.
807 ::SendMessage((HWND
) GetHWND(), EM_UNDO
, 0, 0);
811 bool wxTextCtrl::CanUndo() const
813 return (::SendMessage((HWND
) GetHWND(), EM_CANUNDO
, 0, 0) != 0);
816 bool wxTextCtrl::CanRedo() const
818 return (::SendMessage((HWND
) GetHWND(), EM_CANUNDO
, 0, 0) != 0);
821 // If the return values from and to are the same, there is no
823 void wxTextCtrl::GetSelection(long* from
, long* to
) const
829 ::SendMessage((HWND
) GetHWND(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
831 *from
= charRange
.cpMin
;
832 *to
= charRange
.cpMax
;
837 DWORD dwStart
, dwEnd
;
838 WPARAM wParam
= (WPARAM
) (DWORD
*) & dwStart
; // receives starting position
839 LPARAM lParam
= (LPARAM
) (DWORD
*) & dwEnd
; // receives ending position
841 ::SendMessage((HWND
) GetHWND(), EM_GETSEL
, wParam
, lParam
);
847 bool wxTextCtrl::IsEditable() const
849 long style
= ::GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
850 return ((style
& ES_READONLY
) == 0);
853 void wxTextCtrl::Command(wxCommandEvent
& event
)
855 SetValue (event
.GetString());
856 ProcessCommand (event
);
859 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
861 // By default, load the first file into the text window.
862 if (event
.GetNumberOfFiles() > 0)
864 LoadFile(event
.GetFiles()[0]);
868 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
869 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
871 //=========================================================================
872 // Called then the buffer is full (gcc 2.6.3)
873 // or when "endl" is output (Borland 4.5)
874 //=========================================================================
875 // Class declaration using multiple inheritance doesn't work properly for
876 // Borland. See note in textctrl.h.
877 #ifndef NO_TEXT_WINDOW_STREAM
878 int wxTextCtrl::overflow(int c
)
880 // Make sure there is a holding area
881 // this is not needed in <iostream> usage as it automagically allocates
882 // it, but does someone want to emulate it for safety's sake?
884 if ( allocate()==EOF
)
886 wxLogError("Streambuf allocation failed");
891 // Verify that there are no characters in get area
892 if ( gptr() && gptr() < egptr() )
894 wxError("Who's trespassing my get area?","Internal error");
901 // Make sure there is a put area
904 /* This doesn't seem to be fatal so comment out error message */
905 // wxError("Put area not opened","Internal error");
908 setp( base(), base() );
910 setp( pbase(), pbase() );
914 // Determine how many characters have been inserted but no consumed
915 int plen
= pptr() - pbase();
917 // Now Jerry relies on the fact that the buffer is at least 2 chars
918 // long, but the holding area "may be as small as 1" ???
919 // And we need an additional \0, so let's keep this inefficient but
922 // If c!=EOF, it is a character that must also be comsumed
923 int xtra
= c
==EOF
? 0 : 1;
925 // Write temporary C-string to wxTextWindow
927 char *txt
= new char[plen
+xtra
+1];
928 memcpy(txt
, pbase(), plen
);
929 txt
[plen
] = (char)c
; // append c
930 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
931 // If the put area already contained \0, output will be truncated there
937 setp(pbase(), epptr());
939 #if defined(__WATCOMC__)
941 #elif defined(zapeof) // HP-UX (all cfront based?)
944 return c
!=EOF
? c
: 0; // this should make everybody happy
948 int len = pptr() - pbase();
949 char *txt = new char[len+1];
950 strncpy(txt, pbase(), len);
953 setp(pbase(), epptr());
959 //=========================================================================
960 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
961 //=========================================================================
962 int wxTextCtrl::sync()
964 // Verify that there are no characters in get area
965 if ( gptr() && gptr() < egptr() )
967 wxError("Who's trespassing my get area?","Internal error");
971 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
975 int len = pptr() - pbase();
976 char *txt = new char[len+1];
977 strncpy(txt, pbase(), len);
980 setp(pbase(), epptr());
986 //=========================================================================
987 // Should not be called by a "ostream". Used by a "istream"
988 //=========================================================================
989 int wxTextCtrl::underflow()
995 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
1001 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
1004 str
.Printf("%.2f", f
);
1009 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
1012 str
.Printf("%.2f", d
);
1017 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
1020 str
.Printf("%d", i
);
1025 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
1028 str
.Printf("%ld", i
);
1033 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
1043 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
1044 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1049 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1050 return (WXHBRUSH
) hbrush
;
1054 if (GetParent()->GetTransparentBackground())
1055 SetBkMode((HDC
) pDC
, TRANSPARENT
);
1057 SetBkMode((HDC
) pDC
, OPAQUE
);
1059 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
1060 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
1062 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
1064 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
1065 // has a zero usage count.
1066 // NOT NOW - will be cleaned up at end of app.
1067 // backgroundBrush->RealizeResource();
1068 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
1071 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1073 switch( event
.KeyCode() )
1076 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1078 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1079 event
.SetEventObject( this );
1080 if ( GetEventHandler()->ProcessEvent(event
) )
1083 //else: multiline controls need Enter for themselves
1088 // only produce navigation event if we don't process TAB ourself or
1089 // if it's a Shift-Tab keypress (we assume nobody will ever need
1090 // this key combo for himself)
1092 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
1093 // handled by Windows
1094 if ( event
.ShiftDown() || !(m_windowStyle
& wxTE_PROCESS_TAB
) )
1096 wxNavigationKeyEvent eventNav
;
1097 eventNav
.SetDirection(!event
.ShiftDown());
1098 eventNav
.SetWindowChange(FALSE
);
1099 eventNav
.SetEventObject(this);
1101 if ( GetEventHandler()->ProcessEvent(eventNav
) )
1111 // don't just call event.Skip() because this will cause TABs and ENTERs
1112 // be passed upwards and we don't always want this - instead process it
1118 long wxTextCtrl::MSWGetDlgCode()
1120 // we always want the characters and the arrows
1121 long lRc
= DLGC_WANTCHARS
| DLGC_WANTARROWS
;
1123 // we may have several different cases:
1124 // 1. normal case: both TAB and ENTER are used for dialog navigation
1125 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
1126 // control in the dialog
1127 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
1128 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
1130 if ( m_windowStyle
& wxTE_PROCESS_ENTER
)
1131 lRc
|= DLGC_WANTMESSAGE
;
1132 if ( m_windowStyle
& wxTE_PROCESS_TAB
)
1133 lRc
|= DLGC_WANTTAB
;
1138 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
1140 if ( m_windowStyle
& wxTE_MULTILINE
)
1142 // No flicker - only problem is we probably can't change the background
1146 ::GetClientRect((HWND) GetHWND(), &rect);
1148 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
1149 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
1151 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
1152 ::DeleteObject(hBrush);
1153 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
1156 // wxWindow::OnEraseBackground(event);
1159 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1163 wxDebugMsg("Edit control %d: ", (int)id);
1167 wxDebugMsg("EN_SETFOCUS\n");
1170 wxDebugMsg("EN_KILLFOCUS\n");
1173 wxDebugMsg("EN_CHANGE\n");
1176 wxDebugMsg("EN_UPDATE\n");
1179 wxDebugMsg("EN_ERRSPACE\n");
1182 wxDebugMsg("EN_MAXTEXT\n");
1185 wxDebugMsg("EN_HSCROLL\n");
1188 wxDebugMsg("EN_VSCROLL\n");
1191 wxDebugMsg("Unknown EDIT notification\n");
1200 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1203 event
.SetEventObject( this );
1204 GetEventHandler()->ProcessEvent(event
);
1210 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1211 wxString
val(GetValue());
1212 if ( !val
.IsNull() )
1213 event
.m_commandString
= WXSTRINGCAST val
;
1214 event
.SetEventObject( this );
1215 ProcessCommand(event
);
1219 // the other notification messages are not processed
1234 // For Rich Edit controls. Do we need it?
1237 bool wxTextCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
1239 wxCommandEvent
event(0, m_windowId
);
1241 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1242 switch ( hdr1
->code
)
1244 // Insert case code here
1246 return wxControl::MSWNotify(wParam
, lParam
);
1250 event
.SetEventObject( this );
1251 event
.SetEventType(eventType
);
1253 if ( !GetEventHandler()->ProcessEvent(event
) )
1261 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
1266 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
1271 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
1276 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
1281 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
1286 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1288 event
.Enable( CanCut() );
1291 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1293 event
.Enable( CanCopy() );
1296 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1298 event
.Enable( CanPaste() );
1301 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1303 event
.Enable( CanUndo() );
1306 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1308 event
.Enable( CanRedo() );