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