]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/textctrl.cpp
interchanged w and h in wxSplitterWindow::OnSashPositionChange (now
[wxWidgets.git] / src / gtk1 / textctrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
13289f04 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "textctrl.h"
12#endif
13
14#include "wx/textctrl.h"
15#include "wx/utils.h"
1a5a8367 16#include <wx/intl.h>
c801d85f
KB
17
18//-----------------------------------------------------------------------------
2f2aa628 19// "changed"
c801d85f
KB
20//-----------------------------------------------------------------------------
21
6de97a3b 22static void gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
484e45bf 23{
9406d962 24 win->SetModified();
903f689b
RR
25
26 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId );
27 wxString val( win->GetValue() );
28 if (!val.IsNull()) event.m_commandString = WXSTRINGCAST val;
29 event.SetEventObject( win );
30 win->GetEventHandler()->ProcessEvent( event );
6de97a3b 31}
112892b9 32
2f2aa628
RR
33//-----------------------------------------------------------------------------
34// wxTextCtrl
35//-----------------------------------------------------------------------------
36
37IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
38
c801d85f 39BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
903f689b 40 EVT_CHAR(wxTextCtrl::OnChar)
c801d85f
KB
41END_EVENT_TABLE()
42
03f38c58 43wxTextCtrl::wxTextCtrl() : streambuf()
c801d85f 44{
6de97a3b 45 if (allocate()) setp(base(),ebuf());
13289f04 46
112892b9 47 m_modified = FALSE;
6de97a3b 48}
c801d85f 49
debe6624 50wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 51 const wxPoint &pos, const wxSize &size,
6de97a3b 52 int style, const wxValidator& validator, const wxString &name ) : streambuf()
c801d85f 53{
6de97a3b 54 if (allocate()) setp(base(),ebuf());
13289f04 55
112892b9 56 m_modified = FALSE;
6de97a3b
RR
57 Create( parent, id, value, pos, size, style, validator, name );
58}
c801d85f 59
debe6624 60bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 61 const wxPoint &pos, const wxSize &size,
6de97a3b 62 int style, const wxValidator& validator, const wxString &name )
c801d85f
KB
63{
64 m_needParent = TRUE;
484e45bf 65
c801d85f 66 PreCreation( parent, id, pos, size, style, name );
484e45bf 67
6de97a3b
RR
68 SetValidator( validator );
69
13289f04 70 bool bMultiLine = (style & wxTE_MULTILINE) != 0;
5796ed40 71 if ( bMultiLine )
47908e25 72 {
13289f04
VZ
73 // a multi-line edit control: create a vertical scrollbar by default and
74 // horizontal if requested
75 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
76
77 // create our control...
c67daf87 78 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
13289f04
VZ
79
80 // ... and put into the upper left hand corner of the table
81 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
82 gtk_table_attach(GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
83 GTK_FILL | GTK_EXPAND,
84 GTK_FILL | GTK_EXPAND | GTK_SHRINK,
85 0, 0);
86
87 // put the horizontal scrollbar in the lower left hand corner
903f689b
RR
88 if (bHasHScrollbar)
89 {
13289f04
VZ
90 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
91 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
92 GTK_EXPAND | GTK_FILL,
93 GTK_FILL,
94 0, 0);
95 gtk_widget_show(hscrollbar);
96 }
97
98 // finally, put the vertical scrollbar in the upper right corner
99 GtkWidget *vscrollbar = gtk_vscrollbar_new(GTK_TEXT(m_text)->vadj);
100 gtk_table_attach(GTK_TABLE(m_widget), vscrollbar, 1, 2, 0, 1,
101 GTK_FILL,
102 GTK_EXPAND | GTK_FILL | GTK_SHRINK,
103 0, 0);
903f689b 104 gtk_widget_show( vscrollbar );
13289f04 105 }
903f689b
RR
106 else
107 {
13289f04
VZ
108 // a single-line text control: no need for scrollbars
109 m_widget =
110 m_text = gtk_entry_new();
111 }
484e45bf 112
c801d85f
KB
113 wxSize newSize = size;
114 if (newSize.x == -1) newSize.x = 80;
115 if (newSize.y == -1) newSize.y = 26;
116 SetSize( newSize.x, newSize.y );
484e45bf 117
c801d85f 118 PostCreation();
484e45bf 119
903f689b
RR
120 if (bMultiLine)
121 {
13289f04
VZ
122 gtk_widget_realize(m_text);
123 gtk_widget_show(m_text);
124 }
125
484e45bf 126 // we want to be notified about text changes
13289f04 127 gtk_signal_connect(GTK_OBJECT(m_text), "changed",
484e45bf
VZ
128 GTK_SIGNAL_FUNC(gtk_text_changed_callback),
129 (gpointer)this);
130
7f4dc78d
RR
131 if (!value.IsNull())
132 {
133 gint tmp = 0;
13289f04 134 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
6de97a3b 135 }
484e45bf 136
5796ed40 137 if (style & wxTE_READONLY)
112892b9
RR
138 {
139 }
140 else
141 {
903f689b 142 if (bMultiLine)
13289f04 143 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
6de97a3b 144 }
484e45bf 145
c801d85f 146 Show( TRUE );
484e45bf 147
f96aa4d9
RR
148 SetBackgroundColour( parent->GetBackgroundColour() );
149
c801d85f 150 return TRUE;
6de97a3b 151}
c801d85f 152
03f38c58 153wxString wxTextCtrl::GetValue() const
c801d85f
KB
154{
155 wxString tmp;
156 if (m_windowStyle & wxTE_MULTILINE)
157 {
13289f04
VZ
158 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
159 tmp = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
c801d85f
KB
160 }
161 else
162 {
13289f04 163 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
6de97a3b 164 }
c801d85f 165 return tmp;
6de97a3b 166}
c801d85f
KB
167
168void wxTextCtrl::SetValue( const wxString &value )
169{
f96aa4d9
RR
170 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
171
c801d85f
KB
172 wxString tmp = "";
173 if (!value.IsNull()) tmp = value;
174 if (m_windowStyle & wxTE_MULTILINE)
175 {
13289f04
VZ
176 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
177 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
c801d85f 178 len = 0;
13289f04 179 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
c801d85f
KB
180 }
181 else
182 {
13289f04 183 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
6de97a3b
RR
184 }
185}
c801d85f
KB
186
187void wxTextCtrl::WriteText( const wxString &text )
188{
f96aa4d9
RR
189 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
190
c801d85f 191 if (text.IsNull()) return;
484e45bf 192
c801d85f
KB
193 if (m_windowStyle & wxTE_MULTILINE)
194 {
13289f04
VZ
195 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
196 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
c801d85f
KB
197 }
198 else
199 {
13289f04 200 gtk_entry_append_text( GTK_ENTRY(m_text), text );
6de97a3b
RR
201 }
202}
c801d85f 203
112892b9 204bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) )
c801d85f 205{
6de97a3b 206 wxFAIL_MSG( "wxTextCtrl::LoadFile not implemented" );
2ad3a34e 207
112892b9 208 return FALSE;
6de97a3b 209}
c801d85f 210
112892b9 211bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) )
c801d85f 212{
6de97a3b 213 wxFAIL_MSG( "wxTextCtrl::SaveFile not implemented" );
2ad3a34e 214
112892b9 215 return FALSE;
6de97a3b 216}
c801d85f 217
112892b9 218/*
debe6624 219wxString wxTextCtrl::GetLineText( long lineNo ) const
c801d85f 220{
6de97a3b 221}
c801d85f 222
112892b9 223
c801d85f
KB
224void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
225{
6de97a3b 226}
c801d85f 227
debe6624 228long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
c801d85f 229{
6de97a3b 230}
c801d85f 231
debe6624 232long wxTextCtrl::XYToPosition( long x, long y )
c801d85f 233{
6de97a3b 234}
c801d85f 235
03f38c58 236int wxTextCtrl::GetNumberOfLines()
c801d85f 237{
6de97a3b 238}
c801d85f
KB
239
240*/
debe6624 241void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f
KB
242{
243 int tmp = (int) pos;
244 if (m_windowStyle & wxTE_MULTILINE)
13289f04 245 gtk_text_set_point( GTK_TEXT(m_text), tmp );
c801d85f 246 else
13289f04 247 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
6de97a3b 248}
c801d85f 249
03f38c58 250void wxTextCtrl::SetInsertionPointEnd()
c801d85f 251{
f96aa4d9
RR
252 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
253
c801d85f
KB
254 int pos = 0;
255 if (m_windowStyle & wxTE_MULTILINE)
13289f04 256 pos = gtk_text_get_length( GTK_TEXT(m_text) );
c801d85f 257 else
13289f04 258 pos = GTK_ENTRY(m_text)->text_length;
c801d85f 259 SetInsertionPoint( pos-1 );
6de97a3b 260}
c801d85f 261
debe6624 262void wxTextCtrl::SetEditable( bool editable )
c801d85f 263{
f96aa4d9
RR
264 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
265
c801d85f 266 if (m_windowStyle & wxTE_MULTILINE)
13289f04 267 gtk_text_set_editable( GTK_TEXT(m_text), editable );
c801d85f 268 else
13289f04 269 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
6de97a3b 270}
c801d85f 271
debe6624 272void wxTextCtrl::SetSelection( long from, long to )
c801d85f 273{
f96aa4d9
RR
274 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
275
13289f04 276 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 277}
c801d85f 278
debe6624 279void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
c801d85f 280{
f96aa4d9 281 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
6de97a3b 282}
c801d85f 283
03f38c58 284long wxTextCtrl::GetInsertionPoint() const
c801d85f 285{
f96aa4d9
RR
286 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
287
13289f04 288 return (long) GTK_EDITABLE(m_text)->current_pos;
6de97a3b 289}
c801d85f 290
03f38c58 291long wxTextCtrl::GetLastPosition() const
c801d85f 292{
f96aa4d9
RR
293 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
294
c801d85f
KB
295 int pos = 0;
296 if (m_windowStyle & wxTE_MULTILINE)
13289f04 297 pos = gtk_text_get_length( GTK_TEXT(m_text) );
c801d85f 298 else
13289f04 299 pos = GTK_ENTRY(m_text)->text_length;
c801d85f 300 return (long)pos-1;
6de97a3b 301}
c801d85f 302
debe6624 303void wxTextCtrl::Remove( long from, long to )
c801d85f 304{
f96aa4d9
RR
305 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
306
13289f04 307 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
6de97a3b 308}
c801d85f 309
debe6624 310void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 311{
f96aa4d9
RR
312 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
313
13289f04 314 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
c801d85f
KB
315 if (value.IsNull()) return;
316 gint pos = (gint)to;
13289f04 317 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
6de97a3b 318}
c801d85f 319
03f38c58 320void wxTextCtrl::Cut()
c801d85f 321{
f96aa4d9
RR
322 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
323
75ed1d15
GL
324#if (GTK_MINOR_VERSION == 1)
325 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
326#else
13289f04 327 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 328#endif
6de97a3b 329}
c801d85f 330
03f38c58 331void wxTextCtrl::Copy()
c801d85f 332{
f96aa4d9
RR
333 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
334
75ed1d15
GL
335#if (GTK_MINOR_VERSION == 1)
336 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
337#else
13289f04 338 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 339#endif
6de97a3b 340}
c801d85f 341
03f38c58 342void wxTextCtrl::Paste()
c801d85f 343{
f96aa4d9
RR
344 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
345
75ed1d15
GL
346#if (GTK_MINOR_VERSION == 1)
347 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
348#else
13289f04 349 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
75ed1d15 350#endif
6de97a3b 351}
c801d85f 352
03f38c58 353void wxTextCtrl::Clear()
c801d85f
KB
354{
355 SetValue( "" );
6de97a3b 356}
c801d85f 357
903f689b 358void wxTextCtrl::OnChar( wxKeyEvent &key_event )
c801d85f 359{
903f689b
RR
360 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
361 {
362 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
363 event.SetEventObject(this);
903f689b
RR
364 if (GetEventHandler()->ProcessEvent(event)) return;
365 }
366 else if (key_event.KeyCode() == WXK_TAB)
367 {
368 wxNavigationKeyEvent event;
369 event.SetDirection( key_event.m_shiftDown );
370 event.SetWindowChange(FALSE);
371 event.SetEventObject(this);
372
373 if (GetEventHandler()->ProcessEvent(event)) return;
374 }
375 key_event.Skip();
6de97a3b 376}
c801d85f 377
46dc76ba 378int wxTextCtrl::overflow( int WXUNUSED(c) )
c801d85f 379{
c801d85f
KB
380 int len = pptr() - pbase();
381 char *txt = new char[len+1];
382 strncpy(txt, pbase(), len);
383 txt[len] = '\0';
384 (*this) << txt;
385 setp(pbase(), epptr());
386 delete[] txt;
387 return EOF;
6de97a3b 388}
c801d85f 389
03f38c58 390int wxTextCtrl::sync()
c801d85f 391{
c801d85f
KB
392 int len = pptr() - pbase();
393 char *txt = new char[len+1];
394 strncpy(txt, pbase(), len);
395 txt[len] = '\0';
396 (*this) << txt;
397 setp(pbase(), epptr());
398 delete[] txt;
399 return 0;
6de97a3b 400}
c801d85f 401
03f38c58 402int wxTextCtrl::underflow()
c801d85f
KB
403{
404 return EOF;
6de97a3b 405}
c801d85f
KB
406
407wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
408{
409 WriteText(s);
410 return *this;
411}
412
debe6624 413wxTextCtrl& wxTextCtrl::operator<<(float f)
c801d85f
KB
414{
415 static char buf[100];
416 sprintf(buf, "%.2f", f);
417 WriteText(buf);
418 return *this;
419}
420
debe6624 421wxTextCtrl& wxTextCtrl::operator<<(double d)
c801d85f
KB
422{
423 static char buf[100];
424 sprintf(buf, "%.2f", d);
425 WriteText(buf);
426 return *this;
427}
428
debe6624 429wxTextCtrl& wxTextCtrl::operator<<(int i)
c801d85f
KB
430{
431 static char buf[100];
432 sprintf(buf, "%i", i);
433 WriteText(buf);
434 return *this;
435}
436
debe6624 437wxTextCtrl& wxTextCtrl::operator<<(long i)
c801d85f
KB
438{
439 static char buf[100];
440 sprintf(buf, "%ld", i);
441 WriteText(buf);
442 return *this;
443}
444
445wxTextCtrl& wxTextCtrl::operator<<(const char c)
446{
447 char buf[2];
448
449 buf[0] = c;
450 buf[1] = 0;
451 WriteText(buf);
452 return *this;
453}
454
03f38c58 455GtkWidget* wxTextCtrl::GetConnectWidget()
e3e65dac
RR
456{
457 return GTK_WIDGET(m_text);
6de97a3b 458}
e3e65dac 459
903f689b
RR
460bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
461{
462 if (m_windowStyle & wxTE_MULTILINE)
463 return (window == GTK_TEXT(m_text)->text_area);
464 else
465 return (window == GTK_ENTRY(m_text)->text_area);
466}
e3e65dac 467
868a2826
RR
468void wxTextCtrl::SetFont( const wxFont &font )
469{
f96aa4d9
RR
470 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
471
3f659fd6
RR
472 if (((wxFont*)&font)->Ok())
473 m_font = font;
474 else
475 m_font = *wxSWISS_FONT;
868a2826 476
f96aa4d9 477 GtkStyle *style = gtk_widget_get_style( m_text );
868a2826
RR
478 if (!m_hasOwnStyle)
479 {
480 m_hasOwnStyle = TRUE;
481 style = gtk_style_copy( gtk_widget_get_style( m_text ) );
482 }
868a2826
RR
483
484 gdk_font_unref( style->font );
485 style->font = gdk_font_ref( m_font.GetInternalFont( 1.0 ) );
486
487 gtk_widget_set_style( m_text, style );
488}
e3e65dac 489
68dda785
VZ
490void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
491{
f96aa4d9
RR
492 return;
493
68dda785 494 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
fc54776e
RR
495
496 m_backgroundColour = colour;
f96aa4d9 497 if (!m_backgroundColour.Ok()) return;
fc54776e 498
f96aa4d9
RR
499 if (m_windowStyle & wxTE_MULTILINE)
500 {
501 GdkWindow *window = GTK_TEXT(m_text)->text_area;
502 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
503 gdk_window_set_background( window, m_backgroundColour.GetColor() );
504 gdk_window_clear( window );
505 }
506 else
507 {
508 GtkStyle *style = gtk_widget_get_style( m_text );
509 if (!m_hasOwnStyle)
510 {
511 m_hasOwnStyle = TRUE;
512 style = gtk_style_copy( gtk_widget_get_style( m_text ) );
513 }
514
515 m_backgroundColour.CalcPixel( gdk_window_get_colormap( m_text->window ) );
516 style->base[GTK_STATE_NORMAL] = *m_backgroundColour.GetColor();
517 style->bg[GTK_STATE_NORMAL] = *m_backgroundColour.GetColor();
518
519 gtk_widget_set_style( m_text, style );
520 }
68dda785 521}
f96aa4d9 522