]>
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
)
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
) );
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
) )
140 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
145 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
147 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
152 bool wxTextCtrl::IsModified(void)
157 void wxTextCtrl::DiscardEdits(void)
162 wxString wxTextCtrl::GetLineText( const long lineNo ) const
167 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
171 long wxTextCtrl::PositionToXY( const long pos, long *x, long *y ) const
175 long wxTextCtrl::XYToPosition( const long x, const long y )
179 int wxTextCtrl::GetNumberOfLines(void)
184 void wxTextCtrl::SetInsertionPoint( const long pos
)
187 if (m_windowStyle
& wxTE_MULTILINE
)
188 gtk_text_set_point( GTK_TEXT(m_widget
), tmp
);
190 gtk_entry_set_position( GTK_ENTRY(m_widget
), tmp
);
193 void wxTextCtrl::SetInsertionPointEnd(void)
196 if (m_windowStyle
& wxTE_MULTILINE
)
197 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
199 pos
= GTK_ENTRY(m_widget
)->text_length
;
200 SetInsertionPoint( pos
-1 );
203 void wxTextCtrl::SetEditable( const bool editable
)
205 if (m_windowStyle
& wxTE_MULTILINE
)
206 gtk_text_set_editable( GTK_TEXT(m_widget
), editable
);
208 gtk_entry_set_editable( GTK_ENTRY(m_widget
), editable
);
211 void wxTextCtrl::SetSelection( const long from
, const long to
)
213 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
216 void wxTextCtrl::ShowPosition( const long WXUNUSED(pos
) )
218 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
221 long wxTextCtrl::GetInsertionPoint(void) const
223 return (long) GTK_EDITABLE(m_widget
)->current_pos
;
226 long wxTextCtrl::GetLastPosition(void) const
229 if (m_windowStyle
& wxTE_MULTILINE
)
230 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
232 pos
= GTK_ENTRY(m_widget
)->text_length
;
236 void wxTextCtrl::Remove( const long from
, const long to
)
238 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
241 void wxTextCtrl::Replace( const long from
, const long to
, const wxString
&value
)
243 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
244 if (value
.IsNull()) return;
246 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &pos
);
249 void wxTextCtrl::Cut(void)
251 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget
), 0 );
254 void wxTextCtrl::Copy(void)
256 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget
), 0 );
259 void wxTextCtrl::Paste(void)
261 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget
), 0 );
264 void wxTextCtrl::Delete(void)
269 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
273 int wxTextCtrl::overflow(int c
)
275 // Make sure there is a holding area
276 if ( allocate()==EOF
)
278 wxError("Streambuf allocation failed","Internal error");
282 // Verify that there are no characters in get area
283 if ( gptr() && gptr() < egptr() )
285 wxError("Who's trespassing my get area?","Internal error");
292 // Make sure there is a put area
295 /* This doesn't seem to be fatal so comment out error message */
296 // wxError("Put area not opened","Internal error");
297 setp( base(), base() );
300 // Determine how many characters have been inserted but no consumed
301 int plen
= pptr() - pbase();
303 // Now Jerry relies on the fact that the buffer is at least 2 chars
304 // long, but the holding area "may be as small as 1" ???
305 // And we need an additional \0, so let's keep this inefficient but
308 // If c!=EOF, it is a character that must also be comsumed
309 int xtra
= c
==EOF
? 0 : 1;
311 // Write temporary C-string to wxTextWindow
313 char *txt
= new char[plen
+xtra
+1];
314 memcpy(txt
, pbase(), plen
);
315 txt
[plen
] = (char)c
; // append c
316 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
317 // If the put area already contained \0, output will be truncated there
323 setp(pbase(), epptr());
325 #if defined(__WATCOMC__)
327 #elif defined(zapeof) // HP-UX (all cfront based?)
330 return c
!=EOF
? c
: 0; // this should make everybody happy
334 int len = pptr() - pbase();
335 char *txt = new char[len+1];
336 strncpy(txt, pbase(), len);
339 setp(pbase(), epptr());
345 int wxTextCtrl::sync(void)
347 // Verify that there are no characters in get area
348 if ( gptr() && gptr() < egptr() )
350 wxError("Who's trespassing my get area?","Internal error");
354 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
358 int len = pptr() - pbase();
359 char *txt = new char[len+1];
360 strncpy(txt, pbase(), len);
363 setp(pbase(), epptr());
369 int wxTextCtrl::underflow(void)
374 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
380 wxTextCtrl
& wxTextCtrl::operator<<(const float f
)
382 static char buf
[100];
383 sprintf(buf
, "%.2f", f
);
388 wxTextCtrl
& wxTextCtrl::operator<<(const double d
)
390 static char buf
[100];
391 sprintf(buf
, "%.2f", d
);
396 wxTextCtrl
& wxTextCtrl::operator<<(const int i
)
398 static char buf
[100];
399 sprintf(buf
, "%i", i
);
404 wxTextCtrl
& wxTextCtrl::operator<<(const long i
)
406 static char buf
[100];
407 sprintf(buf
, "%ld", i
);
412 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)