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