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