]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
Added wxAcceleratorTable, wxFrame::SetAcceleratorTable and additions to process it...
[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 // 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 }
100
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 );
105
106 PostCreation();
107
108 if ( bMultiLine ) {
109 gtk_widget_realize(m_text);
110 gtk_widget_show(m_text);
111 }
112
113 // we want to be notified about text changes
114 gtk_signal_connect(GTK_OBJECT(m_text), "changed",
115 GTK_SIGNAL_FUNC(gtk_text_changed_callback),
116 (gpointer)this);
117
118 if (!value.IsNull())
119 {
120 gint tmp = 0;
121 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
122 };
123
124 if (style & wxREADONLY)
125 {
126 }
127 else
128 {
129 if ( bMultiLine )
130 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
131 };
132
133 Show( TRUE );
134
135 return TRUE;
136 };
137
138 wxString wxTextCtrl::GetValue(void) const
139 {
140 wxString tmp;
141 if (m_windowStyle & wxTE_MULTILINE)
142 {
143 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
144 tmp = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
145 }
146 else
147 {
148 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
149 };
150 return tmp;
151 };
152
153 void wxTextCtrl::SetValue( const wxString &value )
154 {
155 wxString tmp = "";
156 if (!value.IsNull()) tmp = value;
157 if (m_windowStyle & wxTE_MULTILINE)
158 {
159 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
160 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
161 len = 0;
162 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
163 }
164 else
165 {
166 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
167 };
168 };
169
170 void wxTextCtrl::WriteText( const wxString &text )
171 {
172 if (text.IsNull()) return;
173
174 if (m_windowStyle & wxTE_MULTILINE)
175 {
176 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
177 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
178 }
179 else
180 {
181 gtk_entry_append_text( GTK_ENTRY(m_text), text );
182 };
183 };
184
185 bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) )
186 {
187 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
188
189 return FALSE;
190 };
191
192 bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) )
193 {
194 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
195
196 return FALSE;
197 };
198
199 /*
200 wxString wxTextCtrl::GetLineText( long lineNo ) const
201 {
202 };
203
204
205 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
206 {
207 };
208
209 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
210 {
211 };
212
213 long wxTextCtrl::XYToPosition( long x, long y )
214 {
215 };
216
217 int wxTextCtrl::GetNumberOfLines(void)
218 {
219 };
220
221 */
222 void wxTextCtrl::SetInsertionPoint( long pos )
223 {
224 int tmp = (int) pos;
225 if (m_windowStyle & wxTE_MULTILINE)
226 gtk_text_set_point( GTK_TEXT(m_text), tmp );
227 else
228 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
229 };
230
231 void wxTextCtrl::SetInsertionPointEnd(void)
232 {
233 int pos = 0;
234 if (m_windowStyle & wxTE_MULTILINE)
235 pos = gtk_text_get_length( GTK_TEXT(m_text) );
236 else
237 pos = GTK_ENTRY(m_text)->text_length;
238 SetInsertionPoint( pos-1 );
239 };
240
241 void wxTextCtrl::SetEditable( bool editable )
242 {
243 if (m_windowStyle & wxTE_MULTILINE)
244 gtk_text_set_editable( GTK_TEXT(m_text), editable );
245 else
246 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
247 };
248
249 void wxTextCtrl::SetSelection( long from, long to )
250 {
251 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
252 };
253
254 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
255 {
256 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
257 };
258
259 long wxTextCtrl::GetInsertionPoint(void) const
260 {
261 return (long) GTK_EDITABLE(m_text)->current_pos;
262 };
263
264 long wxTextCtrl::GetLastPosition(void) const
265 {
266 int pos = 0;
267 if (m_windowStyle & wxTE_MULTILINE)
268 pos = gtk_text_get_length( GTK_TEXT(m_text) );
269 else
270 pos = GTK_ENTRY(m_text)->text_length;
271 return (long)pos-1;
272 };
273
274 void wxTextCtrl::Remove( long from, long to )
275 {
276 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
277 };
278
279 void wxTextCtrl::Replace( long from, long to, const wxString &value )
280 {
281 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
282 if (value.IsNull()) return;
283 gint pos = (gint)to;
284 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
285 };
286
287 void wxTextCtrl::Cut(void)
288 {
289 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
290 };
291
292 void wxTextCtrl::Copy(void)
293 {
294 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
295 };
296
297 void wxTextCtrl::Paste(void)
298 {
299 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
300 };
301
302 void wxTextCtrl::Delete(void)
303 {
304 SetValue( "" );
305 };
306
307 void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) )
308 {
309 };
310
311 int wxTextCtrl::overflow( int WXUNUSED(c) )
312 {
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;
321 };
322
323 int wxTextCtrl::sync(void)
324 {
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;
333 };
334
335 int wxTextCtrl::underflow(void)
336 {
337 return EOF;
338 };
339
340 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
341 {
342 WriteText(s);
343 return *this;
344 }
345
346 wxTextCtrl& wxTextCtrl::operator<<(float f)
347 {
348 static char buf[100];
349 sprintf(buf, "%.2f", f);
350 WriteText(buf);
351 return *this;
352 }
353
354 wxTextCtrl& wxTextCtrl::operator<<(double d)
355 {
356 static char buf[100];
357 sprintf(buf, "%.2f", d);
358 WriteText(buf);
359 return *this;
360 }
361
362 wxTextCtrl& wxTextCtrl::operator<<(int i)
363 {
364 static char buf[100];
365 sprintf(buf, "%i", i);
366 WriteText(buf);
367 return *this;
368 }
369
370 wxTextCtrl& wxTextCtrl::operator<<(long i)
371 {
372 static char buf[100];
373 sprintf(buf, "%ld", i);
374 WriteText(buf);
375 return *this;
376 }
377
378 wxTextCtrl& 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
388 GtkWidget* wxTextCtrl::GetDropTargetWidget(void)
389 {
390 return GTK_WIDGET(m_text);
391 };
392
393
394
395