| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: textctrl.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Created: 01/02/97 |
| 6 | // Id: |
| 7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifdef __GNUG__ |
| 12 | #pragma implementation "textctrl.h" |
| 13 | #endif |
| 14 | |
| 15 | #include "wx/textctrl.h" |
| 16 | #include "wx/utils.h" |
| 17 | #include <wx/intl.h> |
| 18 | |
| 19 | //----------------------------------------------------------------------------- |
| 20 | // wxTextCtrl |
| 21 | //----------------------------------------------------------------------------- |
| 22 | |
| 23 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) |
| 24 | |
| 25 | static void gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
| 26 | { |
| 27 | win->SetModified(); |
| 28 | |
| 29 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId ); |
| 30 | wxString val( win->GetValue() ); |
| 31 | if (!val.IsNull()) event.m_commandString = WXSTRINGCAST val; |
| 32 | event.SetEventObject( win ); |
| 33 | win->GetEventHandler()->ProcessEvent( event ); |
| 34 | } |
| 35 | |
| 36 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
| 37 | EVT_CHAR(wxTextCtrl::OnChar) |
| 38 | END_EVENT_TABLE() |
| 39 | |
| 40 | wxTextCtrl::wxTextCtrl(void) : streambuf() |
| 41 | { |
| 42 | if (allocate()) setp(base(),ebuf()); |
| 43 | |
| 44 | m_modified = FALSE; |
| 45 | } |
| 46 | |
| 47 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, |
| 48 | const wxPoint &pos, const wxSize &size, |
| 49 | int style, const wxValidator& validator, const wxString &name ) : streambuf() |
| 50 | { |
| 51 | if (allocate()) setp(base(),ebuf()); |
| 52 | |
| 53 | m_modified = FALSE; |
| 54 | Create( parent, id, value, pos, size, style, validator, name ); |
| 55 | } |
| 56 | |
| 57 | bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value, |
| 58 | const wxPoint &pos, const wxSize &size, |
| 59 | int style, const wxValidator& validator, const wxString &name ) |
| 60 | { |
| 61 | m_needParent = TRUE; |
| 62 | |
| 63 | PreCreation( parent, id, pos, size, style, name ); |
| 64 | |
| 65 | SetValidator( validator ); |
| 66 | |
| 67 | bool bMultiLine = (style & wxTE_MULTILINE) != 0; |
| 68 | if ( bMultiLine ) |
| 69 | { |
| 70 | // a multi-line edit control: create a vertical scrollbar by default and |
| 71 | // horizontal if requested |
| 72 | bool bHasHScrollbar = (style & wxHSCROLL) != 0; |
| 73 | |
| 74 | // create our control... |
| 75 | m_text = gtk_text_new( NULL, NULL ); |
| 76 | |
| 77 | // ... and put into the upper left hand corner of the table |
| 78 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); |
| 79 | gtk_table_attach(GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, |
| 80 | GTK_FILL | GTK_EXPAND, |
| 81 | GTK_FILL | GTK_EXPAND | GTK_SHRINK, |
| 82 | 0, 0); |
| 83 | |
| 84 | // put the horizontal scrollbar in the lower left hand corner |
| 85 | if (bHasHScrollbar) |
| 86 | { |
| 87 | GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj); |
| 88 | gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2, |
| 89 | GTK_EXPAND | GTK_FILL, |
| 90 | GTK_FILL, |
| 91 | 0, 0); |
| 92 | gtk_widget_show(hscrollbar); |
| 93 | } |
| 94 | |
| 95 | // finally, put the vertical scrollbar in the upper right corner |
| 96 | GtkWidget *vscrollbar = gtk_vscrollbar_new(GTK_TEXT(m_text)->vadj); |
| 97 | gtk_table_attach(GTK_TABLE(m_widget), vscrollbar, 1, 2, 0, 1, |
| 98 | GTK_FILL, |
| 99 | GTK_EXPAND | GTK_FILL | GTK_SHRINK, |
| 100 | 0, 0); |
| 101 | gtk_widget_show( vscrollbar ); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | // a single-line text control: no need for scrollbars |
| 106 | m_widget = |
| 107 | m_text = gtk_entry_new(); |
| 108 | } |
| 109 | |
| 110 | wxSize newSize = size; |
| 111 | if (newSize.x == -1) newSize.x = 80; |
| 112 | if (newSize.y == -1) newSize.y = 26; |
| 113 | SetSize( newSize.x, newSize.y ); |
| 114 | |
| 115 | PostCreation(); |
| 116 | |
| 117 | if (bMultiLine) |
| 118 | { |
| 119 | gtk_widget_realize(m_text); |
| 120 | gtk_widget_show(m_text); |
| 121 | } |
| 122 | |
| 123 | // we want to be notified about text changes |
| 124 | gtk_signal_connect(GTK_OBJECT(m_text), "changed", |
| 125 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), |
| 126 | (gpointer)this); |
| 127 | |
| 128 | if (!value.IsNull()) |
| 129 | { |
| 130 | gint tmp = 0; |
| 131 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); |
| 132 | } |
| 133 | |
| 134 | if (style & wxTE_READONLY) |
| 135 | { |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | if (bMultiLine) |
| 140 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); |
| 141 | } |
| 142 | |
| 143 | Show( TRUE ); |
| 144 | |
| 145 | return TRUE; |
| 146 | } |
| 147 | |
| 148 | wxString wxTextCtrl::GetValue(void) const |
| 149 | { |
| 150 | wxString tmp; |
| 151 | if (m_windowStyle & wxTE_MULTILINE) |
| 152 | { |
| 153 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
| 154 | tmp = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | tmp = gtk_entry_get_text( GTK_ENTRY(m_text) ); |
| 159 | } |
| 160 | return tmp; |
| 161 | } |
| 162 | |
| 163 | void wxTextCtrl::SetValue( const wxString &value ) |
| 164 | { |
| 165 | wxString tmp = ""; |
| 166 | if (!value.IsNull()) tmp = value; |
| 167 | if (m_windowStyle & wxTE_MULTILINE) |
| 168 | { |
| 169 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
| 170 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); |
| 171 | len = 0; |
| 172 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len ); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | gtk_entry_set_text( GTK_ENTRY(m_text), tmp ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void wxTextCtrl::WriteText( const wxString &text ) |
| 181 | { |
| 182 | if (text.IsNull()) return; |
| 183 | |
| 184 | if (m_windowStyle & wxTE_MULTILINE) |
| 185 | { |
| 186 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
| 187 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | gtk_entry_append_text( GTK_ENTRY(m_text), text ); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) ) |
| 196 | { |
| 197 | wxFAIL_MSG( "wxTextCtrl::LoadFile not implemented" ); |
| 198 | |
| 199 | return FALSE; |
| 200 | } |
| 201 | |
| 202 | bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) ) |
| 203 | { |
| 204 | wxFAIL_MSG( "wxTextCtrl::SaveFile not implemented" ); |
| 205 | |
| 206 | return FALSE; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
| 211 | { |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event ) |
| 216 | { |
| 217 | } |
| 218 | |
| 219 | long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const |
| 220 | { |
| 221 | } |
| 222 | |
| 223 | long wxTextCtrl::XYToPosition( long x, long y ) |
| 224 | { |
| 225 | } |
| 226 | |
| 227 | int wxTextCtrl::GetNumberOfLines(void) |
| 228 | { |
| 229 | } |
| 230 | |
| 231 | */ |
| 232 | void wxTextCtrl::SetInsertionPoint( long pos ) |
| 233 | { |
| 234 | int tmp = (int) pos; |
| 235 | if (m_windowStyle & wxTE_MULTILINE) |
| 236 | gtk_text_set_point( GTK_TEXT(m_text), tmp ); |
| 237 | else |
| 238 | gtk_entry_set_position( GTK_ENTRY(m_text), tmp ); |
| 239 | } |
| 240 | |
| 241 | void wxTextCtrl::SetInsertionPointEnd(void) |
| 242 | { |
| 243 | int pos = 0; |
| 244 | if (m_windowStyle & wxTE_MULTILINE) |
| 245 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
| 246 | else |
| 247 | pos = GTK_ENTRY(m_text)->text_length; |
| 248 | SetInsertionPoint( pos-1 ); |
| 249 | } |
| 250 | |
| 251 | void wxTextCtrl::SetEditable( bool editable ) |
| 252 | { |
| 253 | if (m_windowStyle & wxTE_MULTILINE) |
| 254 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); |
| 255 | else |
| 256 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); |
| 257 | } |
| 258 | |
| 259 | void wxTextCtrl::SetSelection( long from, long to ) |
| 260 | { |
| 261 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
| 262 | } |
| 263 | |
| 264 | void wxTextCtrl::ShowPosition( long WXUNUSED(pos) ) |
| 265 | { |
| 266 | wxFAIL_MSG(_("wxTextCtrl::ShowPosition not implemented")); |
| 267 | } |
| 268 | |
| 269 | long wxTextCtrl::GetInsertionPoint(void) const |
| 270 | { |
| 271 | return (long) GTK_EDITABLE(m_text)->current_pos; |
| 272 | } |
| 273 | |
| 274 | long wxTextCtrl::GetLastPosition(void) const |
| 275 | { |
| 276 | int pos = 0; |
| 277 | if (m_windowStyle & wxTE_MULTILINE) |
| 278 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
| 279 | else |
| 280 | pos = GTK_ENTRY(m_text)->text_length; |
| 281 | return (long)pos-1; |
| 282 | } |
| 283 | |
| 284 | void wxTextCtrl::Remove( long from, long to ) |
| 285 | { |
| 286 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
| 287 | } |
| 288 | |
| 289 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
| 290 | { |
| 291 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
| 292 | if (value.IsNull()) return; |
| 293 | gint pos = (gint)to; |
| 294 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); |
| 295 | } |
| 296 | |
| 297 | void wxTextCtrl::Cut(void) |
| 298 | { |
| 299 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 ); |
| 300 | } |
| 301 | |
| 302 | void wxTextCtrl::Copy(void) |
| 303 | { |
| 304 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 ); |
| 305 | } |
| 306 | |
| 307 | void wxTextCtrl::Paste(void) |
| 308 | { |
| 309 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 ); |
| 310 | } |
| 311 | |
| 312 | void wxTextCtrl::Delete(void) |
| 313 | { |
| 314 | SetValue( "" ); |
| 315 | } |
| 316 | |
| 317 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
| 318 | { |
| 319 | if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) |
| 320 | { |
| 321 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); |
| 322 | event.SetEventObject(this); |
| 323 | printf( "Hallo.\n" ); |
| 324 | if (GetEventHandler()->ProcessEvent(event)) return; |
| 325 | } |
| 326 | else if (key_event.KeyCode() == WXK_TAB) |
| 327 | { |
| 328 | wxNavigationKeyEvent event; |
| 329 | event.SetDirection( key_event.m_shiftDown ); |
| 330 | event.SetWindowChange(FALSE); |
| 331 | event.SetEventObject(this); |
| 332 | |
| 333 | if (GetEventHandler()->ProcessEvent(event)) return; |
| 334 | } |
| 335 | key_event.Skip(); |
| 336 | } |
| 337 | |
| 338 | int wxTextCtrl::overflow( int WXUNUSED(c) ) |
| 339 | { |
| 340 | int len = pptr() - pbase(); |
| 341 | char *txt = new char[len+1]; |
| 342 | strncpy(txt, pbase(), len); |
| 343 | txt[len] = '\0'; |
| 344 | (*this) << txt; |
| 345 | setp(pbase(), epptr()); |
| 346 | delete[] txt; |
| 347 | return EOF; |
| 348 | } |
| 349 | |
| 350 | int wxTextCtrl::sync(void) |
| 351 | { |
| 352 | int len = pptr() - pbase(); |
| 353 | char *txt = new char[len+1]; |
| 354 | strncpy(txt, pbase(), len); |
| 355 | txt[len] = '\0'; |
| 356 | (*this) << txt; |
| 357 | setp(pbase(), epptr()); |
| 358 | delete[] txt; |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int wxTextCtrl::underflow(void) |
| 363 | { |
| 364 | return EOF; |
| 365 | } |
| 366 | |
| 367 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) |
| 368 | { |
| 369 | WriteText(s); |
| 370 | return *this; |
| 371 | } |
| 372 | |
| 373 | wxTextCtrl& wxTextCtrl::operator<<(float f) |
| 374 | { |
| 375 | static char buf[100]; |
| 376 | sprintf(buf, "%.2f", f); |
| 377 | WriteText(buf); |
| 378 | return *this; |
| 379 | } |
| 380 | |
| 381 | wxTextCtrl& wxTextCtrl::operator<<(double d) |
| 382 | { |
| 383 | static char buf[100]; |
| 384 | sprintf(buf, "%.2f", d); |
| 385 | WriteText(buf); |
| 386 | return *this; |
| 387 | } |
| 388 | |
| 389 | wxTextCtrl& wxTextCtrl::operator<<(int i) |
| 390 | { |
| 391 | static char buf[100]; |
| 392 | sprintf(buf, "%i", i); |
| 393 | WriteText(buf); |
| 394 | return *this; |
| 395 | } |
| 396 | |
| 397 | wxTextCtrl& wxTextCtrl::operator<<(long i) |
| 398 | { |
| 399 | static char buf[100]; |
| 400 | sprintf(buf, "%ld", i); |
| 401 | WriteText(buf); |
| 402 | return *this; |
| 403 | } |
| 404 | |
| 405 | wxTextCtrl& wxTextCtrl::operator<<(const char c) |
| 406 | { |
| 407 | char buf[2]; |
| 408 | |
| 409 | buf[0] = c; |
| 410 | buf[1] = 0; |
| 411 | WriteText(buf); |
| 412 | return *this; |
| 413 | } |
| 414 | |
| 415 | GtkWidget* wxTextCtrl::GetConnectWidget(void) |
| 416 | { |
| 417 | return GTK_WIDGET(m_text); |
| 418 | } |
| 419 | |
| 420 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
| 421 | { |
| 422 | if (m_windowStyle & wxTE_MULTILINE) |
| 423 | return (window == GTK_TEXT(m_text)->text_area); |
| 424 | else |
| 425 | return (window == GTK_ENTRY(m_text)->text_area); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | |