]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/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();
56 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &tmp
);
59 wxSize newSize
= size
;
60 if (newSize
.x
== -1) newSize
.x
= 80;
61 if (newSize
.y
== -1) newSize
.y
= 26;
62 SetSize( newSize
.x
, newSize
.y
);
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;
106 if (m_windowStyle
& wxTE_MULTILINE
)
108 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
109 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), text
, text
.Length(), &len
);
113 gtk_entry_append_text( GTK_ENTRY(m_widget
), text
);
118 wxString wxTextCtrl::GetLineText( const long lineNo ) const
122 bool wxTextCtrl::LoadFile( const wxString &file )
126 bool wxTextCtrl::SaveFile( const wxString &file )
130 void wxTextCtrl::DiscardEdits(void)
134 bool wxTextCtrl::IsModified(void)
138 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
142 long wxTextCtrl::PositionToXY( const long pos, long *x, long *y ) const
146 long wxTextCtrl::XYToPosition( const long x, const long y )
150 int wxTextCtrl::GetNumberOfLines(void)
155 void wxTextCtrl::SetInsertionPoint( const long pos
)
158 if (m_windowStyle
& wxTE_MULTILINE
)
159 gtk_text_set_point( GTK_TEXT(m_widget
), tmp
);
161 gtk_entry_set_position( GTK_ENTRY(m_widget
), tmp
);
164 void wxTextCtrl::SetInsertionPointEnd(void)
167 if (m_windowStyle
& wxTE_MULTILINE
)
168 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
170 pos
= GTK_ENTRY(m_widget
)->text_length
;
171 SetInsertionPoint( pos
-1 );
174 void wxTextCtrl::SetEditable( const bool editable
)
176 if (m_windowStyle
& wxTE_MULTILINE
)
177 gtk_text_set_editable( GTK_TEXT(m_widget
), editable
);
179 gtk_entry_set_editable( GTK_ENTRY(m_widget
), editable
);
182 void wxTextCtrl::SetSelection( const long from
, const long to
)
184 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
187 void wxTextCtrl::ShowPosition( const long WXUNUSED(pos
) )
191 long wxTextCtrl::GetInsertionPoint(void) const
193 return (long) GTK_EDITABLE(m_widget
)->current_pos
;
196 long wxTextCtrl::GetLastPosition(void) const
199 if (m_windowStyle
& wxTE_MULTILINE
)
200 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
202 pos
= GTK_ENTRY(m_widget
)->text_length
;
206 void wxTextCtrl::Remove( const long from
, const long to
)
208 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
211 void wxTextCtrl::Replace( const long from
, const long to
, const wxString
&value
)
213 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
214 if (value
.IsNull()) return;
216 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &pos
);
219 void wxTextCtrl::Cut(void)
221 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget
), 0 );
224 void wxTextCtrl::Copy(void)
226 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget
), 0 );
229 void wxTextCtrl::Paste(void)
231 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget
), 0 );
234 void wxTextCtrl::Delete(void)
239 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
243 int wxTextCtrl::overflow(int c
)
245 // Make sure there is a holding area
246 if ( allocate()==EOF
)
248 wxError("Streambuf allocation failed","Internal error");
252 // Verify that there are no characters in get area
253 if ( gptr() && gptr() < egptr() )
255 wxError("Who's trespassing my get area?","Internal error");
262 // Make sure there is a put area
265 /* This doesn't seem to be fatal so comment out error message */
266 // wxError("Put area not opened","Internal error");
267 setp( base(), base() );
270 // Determine how many characters have been inserted but no consumed
271 int plen
= pptr() - pbase();
273 // Now Jerry relies on the fact that the buffer is at least 2 chars
274 // long, but the holding area "may be as small as 1" ???
275 // And we need an additional \0, so let's keep this inefficient but
278 // If c!=EOF, it is a character that must also be comsumed
279 int xtra
= c
==EOF
? 0 : 1;
281 // Write temporary C-string to wxTextWindow
283 char *txt
= new char[plen
+xtra
+1];
284 memcpy(txt
, pbase(), plen
);
285 txt
[plen
] = (char)c
; // append c
286 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
287 // If the put area already contained \0, output will be truncated there
293 setp(pbase(), epptr());
295 #if defined(__WATCOMC__)
297 #elif defined(zapeof) // HP-UX (all cfront based?)
300 return c
!=EOF
? c
: 0; // this should make everybody happy
304 int len = pptr() - pbase();
305 char *txt = new char[len+1];
306 strncpy(txt, pbase(), len);
309 setp(pbase(), epptr());
315 int wxTextCtrl::sync(void)
317 // Verify that there are no characters in get area
318 if ( gptr() && gptr() < egptr() )
320 wxError("Who's trespassing my get area?","Internal error");
324 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
328 int len = pptr() - pbase();
329 char *txt = new char[len+1];
330 strncpy(txt, pbase(), len);
333 setp(pbase(), epptr());
339 int wxTextCtrl::underflow(void)
344 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
350 wxTextCtrl
& wxTextCtrl::operator<<(const float f
)
352 static char buf
[100];
353 sprintf(buf
, "%.2f", f
);
358 wxTextCtrl
& wxTextCtrl::operator<<(const double d
)
360 static char buf
[100];
361 sprintf(buf
, "%.2f", d
);
366 wxTextCtrl
& wxTextCtrl::operator<<(const int i
)
368 static char buf
[100];
369 sprintf(buf
, "%i", i
);
374 wxTextCtrl
& wxTextCtrl::operator<<(const long i
)
376 static char buf
[100];
377 sprintf(buf
, "%ld", i
);
382 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)