]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/textctrl.cpp
New toolbar event processing
[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
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 if (style & wxTE_MULTILINE)
62 m_widget = gtk_text_new( NULL, NULL );
63 else
64 m_widget = gtk_entry_new();
65
66 wxSize newSize = size;
67 if (newSize.x == -1) newSize.x = 80;
68 if (newSize.y == -1) newSize.y = 26;
69 SetSize( newSize.x, newSize.y );
70
71 PostCreation();
72
73 // we want to be notified about text changes
74 gtk_signal_connect(GTK_OBJECT(m_widget), "changed",
75 GTK_SIGNAL_FUNC(gtk_text_changed_callback),
76 (gpointer)this);
77
78 if (!value.IsNull())
79 {
80 gint tmp = 0;
81 gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &tmp );
82 };
83
84 if (style & wxREADONLY)
85 {
86 }
87 else
88 {
89 if (style & wxTE_MULTILINE) gtk_text_set_editable( GTK_TEXT(m_widget), 1 );
90 };
91
92 Show( TRUE );
93
94 return TRUE;
95 };
96
97 wxString wxTextCtrl::GetValue(void) const
98 {
99 wxString tmp;
100 if (m_windowStyle & wxTE_MULTILINE)
101 {
102 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
103 tmp = gtk_editable_get_chars( GTK_EDITABLE(m_widget), 0, len );
104 }
105 else
106 {
107 tmp = gtk_entry_get_text( GTK_ENTRY(m_widget) );
108 };
109 return tmp;
110 };
111
112 void wxTextCtrl::SetValue( const wxString &value )
113 {
114 wxString tmp = "";
115 if (!value.IsNull()) tmp = value;
116 if (m_windowStyle & wxTE_MULTILINE)
117 {
118 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
119 gtk_editable_delete_text( GTK_EDITABLE(m_widget), 0, len );
120 len = 0;
121 gtk_editable_insert_text( GTK_EDITABLE(m_widget), tmp, tmp.Length(), &len );
122 }
123 else
124 {
125 gtk_entry_set_text( GTK_ENTRY(m_widget), tmp );
126 };
127 };
128
129 void wxTextCtrl::WriteText( const wxString &text )
130 {
131 if (text.IsNull()) return;
132
133 if (m_windowStyle & wxTE_MULTILINE)
134 {
135 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
136 gtk_editable_insert_text( GTK_EDITABLE(m_widget), text, text.Length(), &len );
137 }
138 else
139 {
140 gtk_entry_append_text( GTK_ENTRY(m_widget), text );
141 };
142 };
143
144 bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) )
145 {
146 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
147
148 return FALSE;
149 };
150
151 bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) )
152 {
153 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
154
155 return FALSE;
156 };
157
158 /*
159 wxString wxTextCtrl::GetLineText( long lineNo ) const
160 {
161 };
162
163
164 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
165 {
166 };
167
168 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
169 {
170 };
171
172 long wxTextCtrl::XYToPosition( long x, long y )
173 {
174 };
175
176 int wxTextCtrl::GetNumberOfLines(void)
177 {
178 };
179
180 */
181 void wxTextCtrl::SetInsertionPoint( long pos )
182 {
183 int tmp = (int) pos;
184 if (m_windowStyle & wxTE_MULTILINE)
185 gtk_text_set_point( GTK_TEXT(m_widget), tmp );
186 else
187 gtk_entry_set_position( GTK_ENTRY(m_widget), tmp );
188 };
189
190 void wxTextCtrl::SetInsertionPointEnd(void)
191 {
192 int pos = 0;
193 if (m_windowStyle & wxTE_MULTILINE)
194 pos = gtk_text_get_length( GTK_TEXT(m_widget) );
195 else
196 pos = GTK_ENTRY(m_widget)->text_length;
197 SetInsertionPoint( pos-1 );
198 };
199
200 void wxTextCtrl::SetEditable( bool editable )
201 {
202 if (m_windowStyle & wxTE_MULTILINE)
203 gtk_text_set_editable( GTK_TEXT(m_widget), editable );
204 else
205 gtk_entry_set_editable( GTK_ENTRY(m_widget), editable );
206 };
207
208 void wxTextCtrl::SetSelection( long from, long to )
209 {
210 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
211 };
212
213 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
214 {
215 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
216 };
217
218 long wxTextCtrl::GetInsertionPoint(void) const
219 {
220 return (long) GTK_EDITABLE(m_widget)->current_pos;
221 };
222
223 long wxTextCtrl::GetLastPosition(void) const
224 {
225 int pos = 0;
226 if (m_windowStyle & wxTE_MULTILINE)
227 pos = gtk_text_get_length( GTK_TEXT(m_widget) );
228 else
229 pos = GTK_ENTRY(m_widget)->text_length;
230 return (long)pos-1;
231 };
232
233 void wxTextCtrl::Remove( long from, long to )
234 {
235 gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
236 };
237
238 void wxTextCtrl::Replace( long from, long to, const wxString &value )
239 {
240 gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
241 if (value.IsNull()) return;
242 gint pos = (gint)to;
243 gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &pos );
244 };
245
246 void wxTextCtrl::Cut(void)
247 {
248 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget), 0 );
249 };
250
251 void wxTextCtrl::Copy(void)
252 {
253 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget), 0 );
254 };
255
256 void wxTextCtrl::Paste(void)
257 {
258 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget), 0 );
259 };
260
261 void wxTextCtrl::Delete(void)
262 {
263 SetValue( "" );
264 };
265
266 void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) )
267 {
268 };
269
270 int wxTextCtrl::overflow(int c)
271 {
272 int len = pptr() - pbase();
273 char *txt = new char[len+1];
274 strncpy(txt, pbase(), len);
275 txt[len] = '\0';
276 (*this) << txt;
277 setp(pbase(), epptr());
278 delete[] txt;
279 return EOF;
280 };
281
282 int wxTextCtrl::sync(void)
283 {
284 int len = pptr() - pbase();
285 char *txt = new char[len+1];
286 strncpy(txt, pbase(), len);
287 txt[len] = '\0';
288 (*this) << txt;
289 setp(pbase(), epptr());
290 delete[] txt;
291 return 0;
292 };
293
294 int wxTextCtrl::underflow(void)
295 {
296 return EOF;
297 };
298
299 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
300 {
301 WriteText(s);
302 return *this;
303 }
304
305 wxTextCtrl& wxTextCtrl::operator<<(float f)
306 {
307 static char buf[100];
308 sprintf(buf, "%.2f", f);
309 WriteText(buf);
310 return *this;
311 }
312
313 wxTextCtrl& wxTextCtrl::operator<<(double d)
314 {
315 static char buf[100];
316 sprintf(buf, "%.2f", d);
317 WriteText(buf);
318 return *this;
319 }
320
321 wxTextCtrl& wxTextCtrl::operator<<(int i)
322 {
323 static char buf[100];
324 sprintf(buf, "%i", i);
325 WriteText(buf);
326 return *this;
327 }
328
329 wxTextCtrl& wxTextCtrl::operator<<(long i)
330 {
331 static char buf[100];
332 sprintf(buf, "%ld", i);
333 WriteText(buf);
334 return *this;
335 }
336
337 wxTextCtrl& wxTextCtrl::operator<<(const char c)
338 {
339 char buf[2];
340
341 buf[0] = c;
342 buf[1] = 0;
343 WriteText(buf);
344 return *this;
345 }
346