]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/textctrl.cpp
interchanged w and h in wxSplitterWindow::OnSashPositionChange (now
[wxWidgets.git] / src / gtk1 / textctrl.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "textctrl.h"
12#endif
13
14#include "wx/textctrl.h"
15#include "wx/utils.h"
16#include <wx/intl.h>
17
18//-----------------------------------------------------------------------------
19// "changed"
20//-----------------------------------------------------------------------------
21
22static void gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
23{
24 win->SetModified();
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 );
31}
32
33//-----------------------------------------------------------------------------
34// wxTextCtrl
35//-----------------------------------------------------------------------------
36
37IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
38
39BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
40 EVT_CHAR(wxTextCtrl::OnChar)
41END_EVENT_TABLE()
42
43wxTextCtrl::wxTextCtrl() : streambuf()
44{
45 if (allocate()) setp(base(),ebuf());
46
47 m_modified = FALSE;
48}
49
50wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
51 const wxPoint &pos, const wxSize &size,
52 int style, const wxValidator& validator, const wxString &name ) : streambuf()
53{
54 if (allocate()) setp(base(),ebuf());
55
56 m_modified = FALSE;
57 Create( parent, id, value, pos, size, style, validator, name );
58}
59
60bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
61 const wxPoint &pos, const wxSize &size,
62 int style, const wxValidator& validator, const wxString &name )
63{
64 m_needParent = TRUE;
65
66 PreCreation( parent, id, pos, size, style, name );
67
68 SetValidator( validator );
69
70 bool bMultiLine = (style & wxTE_MULTILINE) != 0;
71 if ( bMultiLine )
72 {
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...
78 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
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
88 if (bHasHScrollbar)
89 {
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);
104 gtk_widget_show( vscrollbar );
105 }
106 else
107 {
108 // a single-line text control: no need for scrollbars
109 m_widget =
110 m_text = gtk_entry_new();
111 }
112
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 );
117
118 PostCreation();
119
120 if (bMultiLine)
121 {
122 gtk_widget_realize(m_text);
123 gtk_widget_show(m_text);
124 }
125
126 // we want to be notified about text changes
127 gtk_signal_connect(GTK_OBJECT(m_text), "changed",
128 GTK_SIGNAL_FUNC(gtk_text_changed_callback),
129 (gpointer)this);
130
131 if (!value.IsNull())
132 {
133 gint tmp = 0;
134 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
135 }
136
137 if (style & wxTE_READONLY)
138 {
139 }
140 else
141 {
142 if (bMultiLine)
143 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
144 }
145
146 Show( TRUE );
147
148 SetBackgroundColour( parent->GetBackgroundColour() );
149
150 return TRUE;
151}
152
153wxString wxTextCtrl::GetValue() const
154{
155 wxString tmp;
156 if (m_windowStyle & wxTE_MULTILINE)
157 {
158 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
159 tmp = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
160 }
161 else
162 {
163 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
164 }
165 return tmp;
166}
167
168void wxTextCtrl::SetValue( const wxString &value )
169{
170 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
171
172 wxString tmp = "";
173 if (!value.IsNull()) tmp = value;
174 if (m_windowStyle & wxTE_MULTILINE)
175 {
176 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
177 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
178 len = 0;
179 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
180 }
181 else
182 {
183 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
184 }
185}
186
187void wxTextCtrl::WriteText( const wxString &text )
188{
189 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
190
191 if (text.IsNull()) return;
192
193 if (m_windowStyle & wxTE_MULTILINE)
194 {
195 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
196 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
197 }
198 else
199 {
200 gtk_entry_append_text( GTK_ENTRY(m_text), text );
201 }
202}
203
204bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) )
205{
206 wxFAIL_MSG( "wxTextCtrl::LoadFile not implemented" );
207
208 return FALSE;
209}
210
211bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) )
212{
213 wxFAIL_MSG( "wxTextCtrl::SaveFile not implemented" );
214
215 return FALSE;
216}
217
218/*
219wxString wxTextCtrl::GetLineText( long lineNo ) const
220{
221}
222
223
224void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
225{
226}
227
228long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
229{
230}
231
232long wxTextCtrl::XYToPosition( long x, long y )
233{
234}
235
236int wxTextCtrl::GetNumberOfLines()
237{
238}
239
240*/
241void wxTextCtrl::SetInsertionPoint( long pos )
242{
243 int tmp = (int) pos;
244 if (m_windowStyle & wxTE_MULTILINE)
245 gtk_text_set_point( GTK_TEXT(m_text), tmp );
246 else
247 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
248}
249
250void wxTextCtrl::SetInsertionPointEnd()
251{
252 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
253
254 int pos = 0;
255 if (m_windowStyle & wxTE_MULTILINE)
256 pos = gtk_text_get_length( GTK_TEXT(m_text) );
257 else
258 pos = GTK_ENTRY(m_text)->text_length;
259 SetInsertionPoint( pos-1 );
260}
261
262void wxTextCtrl::SetEditable( bool editable )
263{
264 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
265
266 if (m_windowStyle & wxTE_MULTILINE)
267 gtk_text_set_editable( GTK_TEXT(m_text), editable );
268 else
269 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
270}
271
272void wxTextCtrl::SetSelection( long from, long to )
273{
274 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
275
276 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
277}
278
279void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
280{
281 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
282}
283
284long wxTextCtrl::GetInsertionPoint() const
285{
286 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
287
288 return (long) GTK_EDITABLE(m_text)->current_pos;
289}
290
291long wxTextCtrl::GetLastPosition() const
292{
293 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
294
295 int pos = 0;
296 if (m_windowStyle & wxTE_MULTILINE)
297 pos = gtk_text_get_length( GTK_TEXT(m_text) );
298 else
299 pos = GTK_ENTRY(m_text)->text_length;
300 return (long)pos-1;
301}
302
303void wxTextCtrl::Remove( long from, long to )
304{
305 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
306
307 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
308}
309
310void wxTextCtrl::Replace( long from, long to, const wxString &value )
311{
312 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
313
314 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
315 if (value.IsNull()) return;
316 gint pos = (gint)to;
317 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
318}
319
320void wxTextCtrl::Cut()
321{
322 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
323
324#if (GTK_MINOR_VERSION == 1)
325 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
326#else
327 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
328#endif
329}
330
331void wxTextCtrl::Copy()
332{
333 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
334
335#if (GTK_MINOR_VERSION == 1)
336 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
337#else
338 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
339#endif
340}
341
342void wxTextCtrl::Paste()
343{
344 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
345
346#if (GTK_MINOR_VERSION == 1)
347 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
348#else
349 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
350#endif
351}
352
353void wxTextCtrl::Clear()
354{
355 SetValue( "" );
356}
357
358void wxTextCtrl::OnChar( wxKeyEvent &key_event )
359{
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);
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();
376}
377
378int wxTextCtrl::overflow( int WXUNUSED(c) )
379{
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;
388}
389
390int wxTextCtrl::sync()
391{
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;
400}
401
402int wxTextCtrl::underflow()
403{
404 return EOF;
405}
406
407wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
408{
409 WriteText(s);
410 return *this;
411}
412
413wxTextCtrl& wxTextCtrl::operator<<(float f)
414{
415 static char buf[100];
416 sprintf(buf, "%.2f", f);
417 WriteText(buf);
418 return *this;
419}
420
421wxTextCtrl& wxTextCtrl::operator<<(double d)
422{
423 static char buf[100];
424 sprintf(buf, "%.2f", d);
425 WriteText(buf);
426 return *this;
427}
428
429wxTextCtrl& wxTextCtrl::operator<<(int i)
430{
431 static char buf[100];
432 sprintf(buf, "%i", i);
433 WriteText(buf);
434 return *this;
435}
436
437wxTextCtrl& wxTextCtrl::operator<<(long i)
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
455GtkWidget* wxTextCtrl::GetConnectWidget()
456{
457 return GTK_WIDGET(m_text);
458}
459
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}
467
468void wxTextCtrl::SetFont( const wxFont &font )
469{
470 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
471
472 if (((wxFont*)&font)->Ok())
473 m_font = font;
474 else
475 m_font = *wxSWISS_FONT;
476
477 GtkStyle *style = gtk_widget_get_style( m_text );
478 if (!m_hasOwnStyle)
479 {
480 m_hasOwnStyle = TRUE;
481 style = gtk_style_copy( gtk_widget_get_style( m_text ) );
482 }
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}
489
490void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
491{
492 return;
493
494 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
495
496 m_backgroundColour = colour;
497 if (!m_backgroundColour.Ok()) return;
498
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 }
521}
522