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