]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "textctrl.h"
15 #include "wx/textctrl.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
25 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
26 // EVT_CHAR(wxTextCtrl::OnChar)
29 wxTextCtrl::wxTextCtrl(void) : streambuf()
33 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, const wxWindowID id
, const wxString
&value
,
34 const wxPoint
&pos
, const wxSize
&size
,
35 const int style
, const wxString
&name
) : streambuf()
37 Create( parent
, id
, value
, pos
, size
, style
, name
);
40 bool wxTextCtrl::Create( wxWindow
*parent
, const wxWindowID id
, const wxString
&value
,
41 const wxPoint
&pos
, const wxSize
&size
,
42 const int style
, const wxString
&name
)
46 PreCreation( parent
, id
, pos
, size
, style
, name
);
48 if (style
& wxTE_MULTILINE
)
49 m_widget
= gtk_text_new( NULL
, NULL
);
51 m_widget
= gtk_entry_new();
53 wxSize newSize
= size
;
54 if (newSize
.x
== -1) newSize
.x
= 80;
55 if (newSize
.y
== -1) newSize
.y
= 26;
56 SetSize( newSize
.x
, newSize
.y
);
63 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &tmp
);
71 wxString
wxTextCtrl::GetValue(void) const
74 if (m_windowStyle
& wxTE_MULTILINE
)
76 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
77 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_widget
), 0, len
-1 );
81 tmp
= gtk_entry_get_text( GTK_ENTRY(m_widget
) );
86 void wxTextCtrl::SetValue( const wxString
&value
)
89 if (!value
.IsNull()) tmp
= value
;
90 if (m_windowStyle
& wxTE_MULTILINE
)
92 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
93 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), 0, len
-1 );
95 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), tmp
, tmp
.Length(), &len
);
99 gtk_entry_set_text( GTK_ENTRY(m_widget
), tmp
);
103 void wxTextCtrl::WriteText( const wxString
&text
)
105 if (text
.IsNull()) return;
107 if (m_windowStyle
& wxTE_MULTILINE
)
109 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) ) - 1;
110 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), text
, text
.Length(), &len
);
114 gtk_entry_append_text( GTK_ENTRY(m_widget
), text
);
119 wxString wxTextCtrl::GetLineText( const long lineNo ) const
123 bool wxTextCtrl::LoadFile( const wxString &file )
127 bool wxTextCtrl::SaveFile( const wxString &file )
131 void wxTextCtrl::DiscardEdits(void)
135 bool wxTextCtrl::IsModified(void)
139 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
143 long wxTextCtrl::PositionToXY( const long pos, long *x, long *y ) const
147 long wxTextCtrl::XYToPosition( const long x, const long y )
151 int wxTextCtrl::GetNumberOfLines(void)
156 void wxTextCtrl::SetInsertionPoint( const long pos
)
159 if (m_windowStyle
& wxTE_MULTILINE
)
160 gtk_text_set_point( GTK_TEXT(m_widget
), tmp
);
162 gtk_entry_set_position( GTK_ENTRY(m_widget
), tmp
);
165 void wxTextCtrl::SetInsertionPointEnd(void)
168 if (m_windowStyle
& wxTE_MULTILINE
)
169 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
171 pos
= GTK_ENTRY(m_widget
)->text_length
;
172 SetInsertionPoint( pos
-1 );
175 void wxTextCtrl::SetEditable( const bool editable
)
177 if (m_windowStyle
& wxTE_MULTILINE
)
178 gtk_text_set_editable( GTK_TEXT(m_widget
), editable
);
180 gtk_entry_set_editable( GTK_ENTRY(m_widget
), editable
);
183 void wxTextCtrl::SetSelection( const long from
, const long to
)
185 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
188 void wxTextCtrl::ShowPosition( const long WXUNUSED(pos
) )
192 long wxTextCtrl::GetInsertionPoint(void) const
194 return (long) GTK_EDITABLE(m_widget
)->current_pos
;
197 long wxTextCtrl::GetLastPosition(void) const
200 if (m_windowStyle
& wxTE_MULTILINE
)
201 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
203 pos
= GTK_ENTRY(m_widget
)->text_length
;
207 void wxTextCtrl::Remove( const long from
, const long to
)
209 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
212 void wxTextCtrl::Replace( const long from
, const long to
, const wxString
&value
)
214 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
215 if (value
.IsNull()) return;
217 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &pos
);
220 void wxTextCtrl::Cut(void)
222 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget
), 0 );
225 void wxTextCtrl::Copy(void)
227 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget
), 0 );
230 void wxTextCtrl::Paste(void)
232 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget
), 0 );
235 void wxTextCtrl::Delete(void)
240 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
244 int wxTextCtrl::overflow(int c
)
246 // Make sure there is a holding area
247 if ( allocate()==EOF
)
249 wxError("Streambuf allocation failed","Internal error");
253 // Verify that there are no characters in get area
254 if ( gptr() && gptr() < egptr() )
256 wxError("Who's trespassing my get area?","Internal error");
263 // Make sure there is a put area
266 /* This doesn't seem to be fatal so comment out error message */
267 // wxError("Put area not opened","Internal error");
268 setp( base(), base() );
271 // Determine how many characters have been inserted but no consumed
272 int plen
= pptr() - pbase();
274 // Now Jerry relies on the fact that the buffer is at least 2 chars
275 // long, but the holding area "may be as small as 1" ???
276 // And we need an additional \0, so let's keep this inefficient but
279 // If c!=EOF, it is a character that must also be comsumed
280 int xtra
= c
==EOF
? 0 : 1;
282 // Write temporary C-string to wxTextWindow
284 char *txt
= new char[plen
+xtra
+1];
285 memcpy(txt
, pbase(), plen
);
286 txt
[plen
] = (char)c
; // append c
287 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
288 // If the put area already contained \0, output will be truncated there
294 setp(pbase(), epptr());
296 #if defined(__WATCOMC__)
298 #elif defined(zapeof) // HP-UX (all cfront based?)
301 return c
!=EOF
? c
: 0; // this should make everybody happy
305 int len = pptr() - pbase();
306 char *txt = new char[len+1];
307 strncpy(txt, pbase(), len);
310 setp(pbase(), epptr());
316 int wxTextCtrl::sync(void)
318 // Verify that there are no characters in get area
319 if ( gptr() && gptr() < egptr() )
321 wxError("Who's trespassing my get area?","Internal error");
325 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
329 int len = pptr() - pbase();
330 char *txt = new char[len+1];
331 strncpy(txt, pbase(), len);
334 setp(pbase(), epptr());
340 int wxTextCtrl::underflow(void)
345 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
351 wxTextCtrl
& wxTextCtrl::operator<<(const float f
)
353 static char buf
[100];
354 sprintf(buf
, "%.2f", f
);
359 wxTextCtrl
& wxTextCtrl::operator<<(const double d
)
361 static char buf
[100];
362 sprintf(buf
, "%.2f", d
);
367 wxTextCtrl
& wxTextCtrl::operator<<(const int i
)
369 static char buf
[100];
370 sprintf(buf
, "%i", i
);
375 wxTextCtrl
& wxTextCtrl::operator<<(const long i
)
377 static char buf
[100];
378 sprintf(buf
, "%ld", i
);
383 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)