]>
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
)
24 void gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
26 win
->m_modified
= TRUE
;
30 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
31 // EVT_CHAR(wxTextCtrl::OnChar)
34 wxTextCtrl::wxTextCtrl(void) : streambuf()
39 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, const wxWindowID id
, const wxString
&value
,
40 const wxPoint
&pos
, const wxSize
&size
,
41 const int style
, const wxString
&name
) : streambuf()
44 Create( parent
, id
, value
, pos
, size
, style
, name
);
47 bool wxTextCtrl::Create( wxWindow
*parent
, const wxWindowID id
, const wxString
&value
,
48 const wxPoint
&pos
, const wxSize
&size
,
49 const int style
, const wxString
&name
)
53 PreCreation( parent
, id
, pos
, size
, style
, name
);
55 if (style
& wxTE_MULTILINE
)
56 m_widget
= gtk_text_new( NULL
, NULL
);
58 m_widget
= gtk_entry_new();
60 wxSize newSize
= size
;
61 if (newSize
.x
== -1) newSize
.x
= 80;
62 if (newSize
.y
== -1) newSize
.y
= 26;
63 SetSize( newSize
.x
, newSize
.y
);
71 // Don't know why this is so
72 if (style
& wxTE_MULTILINE
)
73 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length()+1, &tmp
);
75 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &tmp
);
78 if (style
& wxREADONLY
)
83 if (style
& wxTE_MULTILINE
) gtk_text_set_editable( GTK_TEXT(m_widget
), 1 );
91 wxString
wxTextCtrl::GetValue(void) const
94 if (m_windowStyle
& wxTE_MULTILINE
)
96 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
97 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_widget
), 0, len
-1 );
101 tmp
= gtk_entry_get_text( GTK_ENTRY(m_widget
) );
106 void wxTextCtrl::SetValue( const wxString
&value
)
109 if (!value
.IsNull()) tmp
= value
;
110 if (m_windowStyle
& wxTE_MULTILINE
)
112 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
113 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), 0, len
-1 );
115 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), tmp
, tmp
.Length(), &len
);
119 gtk_entry_set_text( GTK_ENTRY(m_widget
), tmp
);
123 void wxTextCtrl::WriteText( const wxString
&text
)
125 if (text
.IsNull()) return;
127 if (m_windowStyle
& wxTE_MULTILINE
)
129 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) ) - 1;
130 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), text
, text
.Length(), &len
);
134 gtk_entry_append_text( GTK_ENTRY(m_widget
), text
);
138 bool wxTextCtrl::LoadFile( const wxString
&WXUNUSED(file
) )
143 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
148 bool wxTextCtrl::IsModified(void)
153 void wxTextCtrl::DiscardEdits(void)
158 wxString wxTextCtrl::GetLineText( const long lineNo ) const
163 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
167 long wxTextCtrl::PositionToXY( const long pos, long *x, long *y ) const
171 long wxTextCtrl::XYToPosition( const long x, const long y )
175 int wxTextCtrl::GetNumberOfLines(void)
180 void wxTextCtrl::SetInsertionPoint( const long pos
)
183 if (m_windowStyle
& wxTE_MULTILINE
)
184 gtk_text_set_point( GTK_TEXT(m_widget
), tmp
);
186 gtk_entry_set_position( GTK_ENTRY(m_widget
), tmp
);
189 void wxTextCtrl::SetInsertionPointEnd(void)
192 if (m_windowStyle
& wxTE_MULTILINE
)
193 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
195 pos
= GTK_ENTRY(m_widget
)->text_length
;
196 SetInsertionPoint( pos
-1 );
199 void wxTextCtrl::SetEditable( const bool editable
)
201 if (m_windowStyle
& wxTE_MULTILINE
)
202 gtk_text_set_editable( GTK_TEXT(m_widget
), editable
);
204 gtk_entry_set_editable( GTK_ENTRY(m_widget
), editable
);
207 void wxTextCtrl::SetSelection( const long from
, const long to
)
209 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
212 void wxTextCtrl::ShowPosition( const long WXUNUSED(pos
) )
216 long wxTextCtrl::GetInsertionPoint(void) const
218 return (long) GTK_EDITABLE(m_widget
)->current_pos
;
221 long wxTextCtrl::GetLastPosition(void) const
224 if (m_windowStyle
& wxTE_MULTILINE
)
225 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
227 pos
= GTK_ENTRY(m_widget
)->text_length
;
231 void wxTextCtrl::Remove( const long from
, const long to
)
233 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
236 void wxTextCtrl::Replace( const long from
, const long to
, const wxString
&value
)
238 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
239 if (value
.IsNull()) return;
241 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &pos
);
244 void wxTextCtrl::Cut(void)
246 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget
), 0 );
249 void wxTextCtrl::Copy(void)
251 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget
), 0 );
254 void wxTextCtrl::Paste(void)
256 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget
), 0 );
259 void wxTextCtrl::Delete(void)
264 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
268 int wxTextCtrl::overflow(int c
)
270 // Make sure there is a holding area
271 if ( allocate()==EOF
)
273 wxError("Streambuf allocation failed","Internal error");
277 // Verify that there are no characters in get area
278 if ( gptr() && gptr() < egptr() )
280 wxError("Who's trespassing my get area?","Internal error");
287 // Make sure there is a put area
290 /* This doesn't seem to be fatal so comment out error message */
291 // wxError("Put area not opened","Internal error");
292 setp( base(), base() );
295 // Determine how many characters have been inserted but no consumed
296 int plen
= pptr() - pbase();
298 // Now Jerry relies on the fact that the buffer is at least 2 chars
299 // long, but the holding area "may be as small as 1" ???
300 // And we need an additional \0, so let's keep this inefficient but
303 // If c!=EOF, it is a character that must also be comsumed
304 int xtra
= c
==EOF
? 0 : 1;
306 // Write temporary C-string to wxTextWindow
308 char *txt
= new char[plen
+xtra
+1];
309 memcpy(txt
, pbase(), plen
);
310 txt
[plen
] = (char)c
; // append c
311 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
312 // If the put area already contained \0, output will be truncated there
318 setp(pbase(), epptr());
320 #if defined(__WATCOMC__)
322 #elif defined(zapeof) // HP-UX (all cfront based?)
325 return c
!=EOF
? c
: 0; // this should make everybody happy
329 int len = pptr() - pbase();
330 char *txt = new char[len+1];
331 strncpy(txt, pbase(), len);
334 setp(pbase(), epptr());
340 int wxTextCtrl::sync(void)
342 // Verify that there are no characters in get area
343 if ( gptr() && gptr() < egptr() )
345 wxError("Who's trespassing my get area?","Internal error");
349 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
353 int len = pptr() - pbase();
354 char *txt = new char[len+1];
355 strncpy(txt, pbase(), len);
358 setp(pbase(), epptr());
364 int wxTextCtrl::underflow(void)
369 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
375 wxTextCtrl
& wxTextCtrl::operator<<(const float f
)
377 static char buf
[100];
378 sprintf(buf
, "%.2f", f
);
383 wxTextCtrl
& wxTextCtrl::operator<<(const double d
)
385 static char buf
[100];
386 sprintf(buf
, "%.2f", d
);
391 wxTextCtrl
& wxTextCtrl::operator<<(const int i
)
393 static char buf
[100];
394 sprintf(buf
, "%i", i
);
399 wxTextCtrl
& wxTextCtrl::operator<<(const long i
)
401 static char buf
[100];
402 sprintf(buf
, "%ld", i
);
407 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)