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 void wxTextCtrl::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
341 int currentX
, currentY
;
342 GetPosition(¤tX
, ¤tY
);
348 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
350 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
353 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
355 int cx
; // button font dimensions
358 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
360 int control_width
, control_height
, control_x
, control_y
;
362 // If we're prepared to use the existing size, then...
363 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
368 // Deal with default size (using -1 values)
370 w1
= DEFAULT_ITEM_WIDTH
;
377 // Calculations may have made text size too small
378 if (control_height
<= 0)
379 control_height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
381 if (control_width
<= 0)
382 control_width
= DEFAULT_ITEM_WIDTH
;
384 MoveWindow(GetHwnd(), (int)control_x
, (int)control_y
,
385 (int)control_width
, (int)control_height
, TRUE
);
388 // Clipboard operations
389 void wxTextCtrl::Copy()
393 HWND hWnd
= GetHwnd();
394 SendMessage(hWnd
, WM_COPY
, 0, 0L);
398 void wxTextCtrl::Cut()
402 HWND hWnd
= GetHwnd();
403 SendMessage(hWnd
, WM_CUT
, 0, 0L);
407 void wxTextCtrl::Paste()
411 HWND hWnd
= GetHwnd();
412 SendMessage(hWnd
, WM_PASTE
, 0, 0L);
416 void wxTextCtrl::SetEditable(bool editable
)
418 HWND hWnd
= GetHwnd();
419 SendMessage(hWnd
, EM_SETREADONLY
, (WPARAM
)!editable
, (LPARAM
)0L);
422 void wxTextCtrl::SetInsertionPoint(long pos
)
424 HWND hWnd
= GetHwnd();
432 SendMessage(hWnd
, EM_EXSETSEL
, 0, (LPARAM
) &range
);
433 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
438 SendMessage(hWnd
, EM_SETSEL
, pos
, pos
);
439 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
442 SendMessage(hWnd
, EM_SETSEL
, 0, MAKELPARAM(pos
, pos
));
445 SendMessage(hWnd
, EM_REPLACESEL
, 0, (LPARAM
)nothing
);
448 void wxTextCtrl::SetInsertionPointEnd()
450 long pos
= GetLastPosition();
451 SetInsertionPoint(pos
);
454 long wxTextCtrl::GetInsertionPoint() const
462 SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) &range
);
467 DWORD Pos
=(DWORD
)SendMessage(GetHwnd(), EM_GETSEL
, 0, 0L);
471 long wxTextCtrl::GetLastPosition() const
473 HWND hWnd
= GetHwnd();
475 // Will always return a number > 0 (according to docs)
476 int noLines
= (int)SendMessage(hWnd
, EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0L);
478 // This gets the char index for the _beginning_ of the last line
479 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)(noLines
-1), (LPARAM
)0L);
481 // Get number of characters in the last line. We'll add this to the character
482 // index for the last line, 1st position.
483 int lineLength
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0L);
485 return (long)(charIndex
+ lineLength
);
488 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
491 HWND hWnd
= GetHwnd();
492 long fromChar
= from
;
495 // Set selection and remove it
497 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
499 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
501 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
503 // Now replace with 'value', by pasting.
504 wxSetClipboardData(wxDF_TEXT
, (wxObject
*) (const wxChar
*)value
, 0, 0);
506 // Paste into edit control
507 SendMessage(hWnd
, WM_PASTE
, (WPARAM
)0, (LPARAM
)0L);
509 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
513 void wxTextCtrl::Remove(long from
, long to
)
515 HWND hWnd
= GetHwnd();
516 long fromChar
= from
;
519 // Cut all selected text
521 SendMessage(hWnd
, EM_SETSEL
, fromChar
, toChar
);
523 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
525 SendMessage(hWnd
, WM_CUT
, (WPARAM
)0, (LPARAM
)0);
528 void wxTextCtrl::SetSelection(long from
, long to
)
530 HWND hWnd
= GetHwnd();
531 long fromChar
= from
;
533 // if from and to are both -1, it means
534 // (in wxWindows) that all text should be selected.
535 // This translates into Windows convention
536 if ((from
== -1) && (to
== -1))
543 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)fromChar
, (LPARAM
)toChar
);
544 SendMessage(hWnd
, EM_SCROLLCARET
, (WPARAM
)0, (LPARAM
)0);
546 // WPARAM is 0: selection is scrolled into view
547 SendMessage(hWnd
, EM_SETSEL
, (WPARAM
)0, (LPARAM
)MAKELONG(fromChar
, toChar
));
551 bool wxTextCtrl::LoadFile(const wxString
& file
)
553 if (!wxFileExists(WXSTRINGCAST file
))
560 // ifstream input(WXSTRINGCAST file, ios::nocreate | ios::in);
561 ifstream
input(MBSTRINGCAST file
.mb_str(wxConvFile
), ios::in
);
565 // Previously a SETSEL/REPLACESEL call-pair were done to insert
566 // line by line into the control. Apart from being very slow this
567 // was limited to 32K of text by the external interface presenting
568 // positions as signed shorts. Now load in one chunk...
569 // Note use of 'farmalloc' as in Borland 3.1 'size_t' is 16-bits...
572 struct _stat stat_buf
;
573 if (stat(MBSTRINGCAST file
.mb_str(wxConvFile
), &stat_buf
) < 0)
576 struct stat stat_buf
;
577 if (stat(file
.mb_str(wxConvFile
), &stat_buf
) < 0)
581 // wxChar *tmp_buffer = (wxChar*)farmalloc(stat_buf.st_size+1);
582 // This may need to be a bigger buffer than the file size suggests,
583 // if it's a UNIX file. Give it an extra 1000 just in case.
584 wxChar
*tmp_buffer
= (wxChar
*)farmalloc((size_t)(stat_buf
.st_size
+1+1000));
585 char *read_buffer
= new char[512];
588 while (!input
.eof() && input
.peek() != EOF
)
590 input
.getline(read_buffer
, 500);
591 int len
= strlen(read_buffer
);
593 wxBuffer
[len
+1] = 10;
596 pos
+= wxConvCurrent
->MB2WC(tmp_buffer
+pos
, read_buffer
, (size_t)-1);
598 strcpy(tmp_buffer
+pos
, read_buffer
);
599 pos
+= strlen(read_buffer
);
603 delete[] read_buffer
;
605 SetWindowText(GetHwnd(), tmp_buffer
);
606 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
609 // update the size limit if needed
617 // If file is null, try saved file name first
618 // Returns TRUE if succeeds.
619 bool wxTextCtrl::SaveFile(const wxString
& file
)
621 wxString
theFile(file
);
623 if (theFile
== _T(""))
624 theFile
= m_fileName
;
626 if (theFile
== _T(""))
629 m_fileName
= theFile
;
631 ofstream
output(MBSTRINGCAST theFile
.mb_str(wxConvFile
));
635 // This will only save 64K max
636 unsigned long nbytes
= SendMessage(GetHwnd(), WM_GETTEXTLENGTH
, 0, 0);
637 char *tmp_buffer
= (char*)farmalloc((size_t)(nbytes
+1));
638 SendMessage(GetHwnd(), WM_GETTEXT
, (WPARAM
)(nbytes
+1), (LPARAM
)tmp_buffer
);
639 char *pstr
= tmp_buffer
;
641 // Convert \r\n to just \n
650 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
655 void wxTextCtrl::WriteText(const wxString
& text
)
658 int len
= text
.Length();
659 char *newtext
= new char[(len
*2)+1];
669 newtext
[j
] = text
[i
];
674 SendMessage(GetHwnd(), EM_REPLACESEL
, 0, (LPARAM
)newtext
);
680 void wxTextCtrl::AppendText(const wxString
& text
)
682 SetInsertionPointEnd();
686 void wxTextCtrl::Clear()
688 SetWindowText(GetHwnd(), _T(""));
691 bool wxTextCtrl::IsModified() const
693 return (SendMessage(GetHwnd(), EM_GETMODIFY
, 0, 0) != 0);
696 // Makes 'unmodified'
697 void wxTextCtrl::DiscardEdits()
699 SendMessage(GetHwnd(), EM_SETMODIFY
, FALSE
, 0L);
703 * Some of the following functions are yet to be implemented
707 int wxTextCtrl::GetNumberOfLines() const
709 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT
, (WPARAM
)0, (LPARAM
)0);
712 long wxTextCtrl::XYToPosition(long x
, long y
) const
714 HWND hWnd
= GetHwnd();
716 // This gets the char index for the _beginning_ of this line
717 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)y
, (LPARAM
)0);
718 return (long)(x
+ charIndex
);
721 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
723 HWND hWnd
= GetHwnd();
725 // This gets the line number containing the character
726 int lineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0);
727 // This gets the char index for the _beginning_ of this line
728 int charIndex
= (int)SendMessage(hWnd
, EM_LINEINDEX
, (WPARAM
)lineNo
, (LPARAM
)0);
729 // The X position must therefore be the different between pos and charIndex
730 *x
= (long)(pos
- charIndex
);
734 void wxTextCtrl::ShowPosition(long pos
)
736 HWND hWnd
= GetHwnd();
738 // To scroll to a position, we pass the number of lines and characters
739 // to scroll *by*. This means that we need to:
740 // (1) Find the line position of the current line.
741 // (2) Find the line position of pos.
742 // (3) Scroll by (pos - current).
743 // For now, ignore the horizontal scrolling.
745 // Is this where scrolling is relative to - the line containing the caret?
746 // Or is the first visible line??? Try first visible line.
747 // int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
749 int currentLineLineNo
= (int)SendMessage(hWnd
, EM_GETFIRSTVISIBLELINE
, (WPARAM
)0, (LPARAM
)0L);
751 int specifiedLineLineNo
= (int)SendMessage(hWnd
, EM_LINEFROMCHAR
, (WPARAM
)pos
, (LPARAM
)0L);
753 int linesToScroll
= specifiedLineLineNo
- currentLineLineNo
;
755 if (linesToScroll
!= 0)
756 (void)SendMessage(hWnd
, EM_LINESCROLL
, (WPARAM
)0, (LPARAM
)linesToScroll
);
759 int wxTextCtrl::GetLineLength(long lineNo
) const
761 long charIndex
= XYToPosition(0, lineNo
);
762 HWND hWnd
= GetHwnd();
763 int len
= (int)SendMessage(hWnd
, EM_LINELENGTH
, (WPARAM
)charIndex
, (LPARAM
)0);
767 wxString
wxTextCtrl::GetLineText(long lineNo
) const
769 HWND hWnd
= GetHwnd();
770 *(WORD
*)wxBuffer
= 512;
771 int noChars
= (int)SendMessage(hWnd
, EM_GETLINE
, (WPARAM
)lineNo
, (LPARAM
)wxBuffer
);
772 wxBuffer
[noChars
] = 0;
773 return wxString(wxBuffer
);
776 bool wxTextCtrl::CanCopy() const
778 // Can copy if there's a selection
780 GetSelection(& from
, & to
);
781 return (from
!= to
) ;
784 bool wxTextCtrl::CanCut() const
786 // Can cut if there's a selection
788 GetSelection(& from
, & to
);
789 return (from
!= to
) ;
792 bool wxTextCtrl::CanPaste() const
797 int dataFormat
= 0; // 0 == any format
798 return (::SendMessage( GetHwnd(), EM_CANPASTE
, (WPARAM
) (UINT
) dataFormat
, 0) != 0);
804 // Standard edit control: check for straight text on clipboard
805 bool isTextAvailable
= FALSE
;
806 if (::OpenClipboard((HWND
) wxTheApp
->GetTopWindow()->GetHWND()))
808 isTextAvailable
= (::IsClipboardFormatAvailable(CF_TEXT
) != 0);
811 return isTextAvailable
;
815 void wxTextCtrl::Undo()
819 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
823 void wxTextCtrl::Redo()
827 // Same as Undo, since Undo undoes the undo, i.e. a redo.
828 ::SendMessage(GetHwnd(), EM_UNDO
, 0, 0);
832 bool wxTextCtrl::CanUndo() const
834 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
837 bool wxTextCtrl::CanRedo() const
839 return (::SendMessage(GetHwnd(), EM_CANUNDO
, 0, 0) != 0);
842 // If the return values from and to are the same, there is no
844 void wxTextCtrl::GetSelection(long* from
, long* to
) const
850 ::SendMessage(GetHwnd(), EM_EXGETSEL
, 0, (LPARAM
) (CHARRANGE
*) & charRange
);
852 *from
= charRange
.cpMin
;
853 *to
= charRange
.cpMax
;
858 DWORD dwStart
, dwEnd
;
859 WPARAM wParam
= (WPARAM
) (DWORD
*) & dwStart
; // receives starting position
860 LPARAM lParam
= (LPARAM
) (DWORD
*) & dwEnd
; // receives ending position
862 ::SendMessage(GetHwnd(), EM_GETSEL
, wParam
, lParam
);
868 bool wxTextCtrl::IsEditable() const
870 long style
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
872 return ((style
& ES_READONLY
) == 0);
875 void wxTextCtrl::Command(wxCommandEvent
& event
)
877 SetValue (event
.GetString());
878 ProcessCommand (event
);
881 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
883 // By default, load the first file into the text window.
884 if (event
.GetNumberOfFiles() > 0)
886 LoadFile(event
.GetFiles()[0]);
890 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
891 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
893 //=========================================================================
894 // Called then the buffer is full (gcc 2.6.3)
895 // or when "endl" is output (Borland 4.5)
896 //=========================================================================
897 // Class declaration using multiple inheritance doesn't work properly for
898 // Borland. See note in textctrl.h.
899 #ifndef NO_TEXT_WINDOW_STREAM
900 int wxTextCtrl::overflow(int c
)
902 // Make sure there is a holding area
903 // this is not needed in <iostream> usage as it automagically allocates
904 // it, but does someone want to emulate it for safety's sake?
906 if ( allocate()==EOF
)
908 wxLogError("Streambuf allocation failed");
913 // Verify that there are no characters in get area
914 if ( gptr() && gptr() < egptr() )
916 wxError("Who's trespassing my get area?","Internal error");
923 // Make sure there is a put area
926 /* This doesn't seem to be fatal so comment out error message */
927 // wxError("Put area not opened","Internal error");
930 setp( base(), base() );
932 setp( pbase(), pbase() );
936 // Determine how many characters have been inserted but no consumed
937 int plen
= pptr() - pbase();
939 // Now Jerry relies on the fact that the buffer is at least 2 chars
940 // long, but the holding area "may be as small as 1" ???
941 // And we need an additional \0, so let's keep this inefficient but
944 // If c!=EOF, it is a character that must also be comsumed
945 int xtra
= c
==EOF
? 0 : 1;
947 // Write temporary C-string to wxTextWindow
949 char *txt
= new char[plen
+xtra
+1];
950 memcpy(txt
, pbase(), plen
);
951 txt
[plen
] = (char)c
; // append c
952 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
953 // If the put area already contained \0, output will be truncated there
959 setp(pbase(), epptr());
961 #if defined(__WATCOMC__)
963 #elif defined(zapeof) // HP-UX (all cfront based?)
966 return c
!=EOF
? c
: 0; // this should make everybody happy
970 int len = pptr() - pbase();
971 char *txt = new char[len+1];
972 strncpy(txt, pbase(), len);
975 setp(pbase(), epptr());
981 //=========================================================================
982 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
983 //=========================================================================
984 int wxTextCtrl::sync()
986 // Verify that there are no characters in get area
987 if ( gptr() && gptr() < egptr() )
989 wxError("Who's trespassing my get area?","Internal error");
993 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
997 int len = pptr() - pbase();
998 char *txt = new char[len+1];
999 strncpy(txt, pbase(), len);
1002 setp(pbase(), epptr());
1008 //=========================================================================
1009 // Should not be called by a "ostream". Used by a "istream"
1010 //=========================================================================
1011 int wxTextCtrl::underflow()
1017 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
1023 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
1026 str
.Printf(_T("%.2f"), f
);
1031 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
1034 str
.Printf(_T("%.2f"), d
);
1039 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
1042 str
.Printf(_T("%d"), i
);
1047 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
1050 str
.Printf(_T("%ld"), i
);
1055 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
1065 WXHBRUSH
wxTextCtrl::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
1066 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
1071 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
1072 return (WXHBRUSH
) hbrush
;
1076 if (GetParent()->GetTransparentBackground())
1077 SetBkMode((HDC
) pDC
, TRANSPARENT
);
1079 SetBkMode((HDC
) pDC
, OPAQUE
);
1081 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
1082 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
1084 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
1086 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
1087 // has a zero usage count.
1088 // NOT NOW - will be cleaned up at end of app.
1089 // backgroundBrush->RealizeResource();
1090 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
1093 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1095 switch ( event
.KeyCode() )
1098 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1100 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1101 event
.SetEventObject( this );
1102 if ( GetEventHandler()->ProcessEvent(event
) )
1105 //else: multiline controls need Enter for themselves
1110 // always produce navigation event - even if we process TAB
1111 // ourselves the fact that we got here means that the user code
1112 // decided to skip processing of this TAB - probably to let it
1113 // do its default job.
1115 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
1116 // handled by Windows
1118 wxNavigationKeyEvent eventNav
;
1119 eventNav
.SetDirection(!event
.ShiftDown());
1120 eventNav
.SetWindowChange(FALSE
);
1121 eventNav
.SetEventObject(this);
1123 if ( GetEventHandler()->ProcessEvent(eventNav
) )
1133 // don't just call event.Skip() because this will cause TABs and ENTERs
1134 // be passed upwards and we don't always want this - instead process it
1141 bool wxTextCtrl::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
1148 wxFocusEvent
event(param
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
1151 event
.SetEventObject( this );
1152 GetEventHandler()->ProcessEvent(event
);
1158 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1159 wxString
val(GetValue());
1160 if ( !val
.IsNull() )
1161 event
.m_commandString
= WXSTRINGCAST val
;
1162 event
.SetEventObject( this );
1163 ProcessCommand(event
);
1168 // the text size limit has been hit - increase it
1172 // the other notification messages are not processed
1185 void wxTextCtrl::AdjustSpaceLimit()
1188 unsigned int len
= ::GetWindowTextLength(GetHwnd()),
1189 limit
= ::SendMessage(GetHwnd(), EM_GETLIMITTEXT
, 0, 0);
1192 limit
= len
+ 0x8000; // 32Kb
1195 if ( m_isRich
|| limit
> 0xffff )
1197 if ( limit
> 0xffff )
1199 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, 0, limit
);
1201 ::SendMessage(GetHwnd(), EM_LIMITTEXT
, limit
, 0);
1206 // For Rich Edit controls. Do we need it?
1209 bool wxTextCtrl::MSWOnNotify(WXWPARAM wParam
, WXLPARAM lParam
)
1211 wxCommandEvent
event(0, m_windowId
);
1213 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1214 switch ( hdr1
->code
)
1216 // Insert case code here
1218 return wxControl::MSWOnNotify(wParam
, lParam
);
1222 event
.SetEventObject( this );
1223 event
.SetEventType(eventType
);
1225 if ( !GetEventHandler()->ProcessEvent(event
) )
1233 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
1238 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
1243 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
1248 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
1253 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
1258 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1260 event
.Enable( CanCut() );
1263 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1265 event
.Enable( CanCopy() );
1268 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1270 event
.Enable( CanPaste() );
1273 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1275 event
.Enable( CanUndo() );
1278 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1280 event
.Enable( CanRedo() );