]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
many fixes to wxTextCrtl, wxTreeCrtl, wxListBox,
[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->m_modified = TRUE;
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 bool wxTextCtrl::IsModified(void)
159 {
160 return m_modified;
161 };
162
163 void wxTextCtrl::DiscardEdits(void)
164 {
165 m_modified = FALSE;
166 };
167
168 /*
169 wxString wxTextCtrl::GetLineText( long lineNo ) const
170 {
171 };
172
173
174 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
175 {
176 };
177
178 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
179 {
180 };
181
182 long wxTextCtrl::XYToPosition( long x, long y )
183 {
184 };
185
186 int wxTextCtrl::GetNumberOfLines(void)
187 {
188 };
189
190 */
191 void wxTextCtrl::SetInsertionPoint( long pos )
192 {
193 int tmp = (int) pos;
194 if (m_windowStyle & wxTE_MULTILINE)
195 gtk_text_set_point( GTK_TEXT(m_widget), tmp );
196 else
197 gtk_entry_set_position( GTK_ENTRY(m_widget), tmp );
198 };
199
200 void wxTextCtrl::SetInsertionPointEnd(void)
201 {
202 int pos = 0;
203 if (m_windowStyle & wxTE_MULTILINE)
204 pos = gtk_text_get_length( GTK_TEXT(m_widget) );
205 else
206 pos = GTK_ENTRY(m_widget)->text_length;
207 SetInsertionPoint( pos-1 );
208 };
209
210 void wxTextCtrl::SetEditable( bool editable )
211 {
212 if (m_windowStyle & wxTE_MULTILINE)
213 gtk_text_set_editable( GTK_TEXT(m_widget), editable );
214 else
215 gtk_entry_set_editable( GTK_ENTRY(m_widget), editable );
216 };
217
218 void wxTextCtrl::SetSelection( long from, long to )
219 {
220 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
221 };
222
223 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
224 {
225 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
226 };
227
228 long wxTextCtrl::GetInsertionPoint(void) const
229 {
230 return (long) GTK_EDITABLE(m_widget)->current_pos;
231 };
232
233 long wxTextCtrl::GetLastPosition(void) const
234 {
235 int pos = 0;
236 if (m_windowStyle & wxTE_MULTILINE)
237 pos = gtk_text_get_length( GTK_TEXT(m_widget) );
238 else
239 pos = GTK_ENTRY(m_widget)->text_length;
240 return (long)pos-1;
241 };
242
243 void wxTextCtrl::Remove( long from, long to )
244 {
245 gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
246 };
247
248 void wxTextCtrl::Replace( long from, long to, const wxString &value )
249 {
250 gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
251 if (value.IsNull()) return;
252 gint pos = (gint)to;
253 gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &pos );
254 };
255
256 void wxTextCtrl::Cut(void)
257 {
258 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget), 0 );
259 };
260
261 void wxTextCtrl::Copy(void)
262 {
263 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget), 0 );
264 };
265
266 void wxTextCtrl::Paste(void)
267 {
268 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget), 0 );
269 };
270
271 void wxTextCtrl::Delete(void)
272 {
273 SetValue( "" );
274 };
275
276 void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) )
277 {
278 };
279
280 int wxTextCtrl::overflow(int c)
281 {
282 int len = pptr() - pbase();
283 char *txt = new char[len+1];
284 strncpy(txt, pbase(), len);
285 txt[len] = '\0';
286 (*this) << txt;
287 setp(pbase(), epptr());
288 delete[] txt;
289 return EOF;
290 };
291
292 int wxTextCtrl::sync(void)
293 {
294 int len = pptr() - pbase();
295 char *txt = new char[len+1];
296 strncpy(txt, pbase(), len);
297 txt[len] = '\0';
298 (*this) << txt;
299 setp(pbase(), epptr());
300 delete[] txt;
301 return 0;
302 };
303
304 int wxTextCtrl::underflow(void)
305 {
306 return EOF;
307 };
308
309 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
310 {
311 WriteText(s);
312 return *this;
313 }
314
315 wxTextCtrl& wxTextCtrl::operator<<(float f)
316 {
317 static char buf[100];
318 sprintf(buf, "%.2f", f);
319 WriteText(buf);
320 return *this;
321 }
322
323 wxTextCtrl& wxTextCtrl::operator<<(double d)
324 {
325 static char buf[100];
326 sprintf(buf, "%.2f", d);
327 WriteText(buf);
328 return *this;
329 }
330
331 wxTextCtrl& wxTextCtrl::operator<<(int i)
332 {
333 static char buf[100];
334 sprintf(buf, "%i", i);
335 WriteText(buf);
336 return *this;
337 }
338
339 wxTextCtrl& wxTextCtrl::operator<<(long i)
340 {
341 static char buf[100];
342 sprintf(buf, "%ld", i);
343 WriteText(buf);
344 return *this;
345 }
346
347 wxTextCtrl& wxTextCtrl::operator<<(const char c)
348 {
349 char buf[2];
350
351 buf[0] = c;
352 buf[1] = 0;
353 WriteText(buf);
354 return *this;
355 }
356