]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/textctrl.cpp
"-1" index error in wxString::Before fixed
[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->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 m_modified = FALSE;
37 };
38
39 wxTextCtrl::wxTextCtrl( wxWindow *parent, const wxWindowID id, const wxString &value,
40 const wxPoint &pos, const wxSize &size,
41 const int style, const wxString &name ) : streambuf()
42 {
43 m_modified = FALSE;
44 Create( parent, id, value, pos, size, style, name );
45 };
46
47 bool wxTextCtrl::Create( wxWindow *parent, const wxWindowID id, const wxString &value,
48 const wxPoint &pos, const wxSize &size,
49 const int style, const wxString &name )
50 {
51 m_needParent = TRUE;
52
53 PreCreation( parent, id, pos, size, style, name );
54
55 if (style & wxTE_MULTILINE)
56 m_widget = gtk_text_new( NULL, NULL );
57 else
58 m_widget = gtk_entry_new();
59
60 wxSize newSize = size;
61 if (newSize.x == -1) newSize.x = 80;
62 if (newSize.y == -1) newSize.y = 26;
63 SetSize( newSize.x, newSize.y );
64
65 PostCreation();
66
67 if (!value.IsNull())
68 {
69 gint tmp = 0;
70
71 // Don't know why this is so
72 if (style & wxTE_MULTILINE)
73 gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length()+1, &tmp );
74 else
75 gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &tmp );
76 };
77
78 if (style & wxREADONLY)
79 {
80 }
81 else
82 {
83 if (style & wxTE_MULTILINE) gtk_text_set_editable( GTK_TEXT(m_widget), 1 );
84 };
85
86 Show( TRUE );
87
88 return TRUE;
89 };
90
91 wxString wxTextCtrl::GetValue(void) const
92 {
93 wxString tmp;
94 if (m_windowStyle & wxTE_MULTILINE)
95 {
96 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
97 tmp = gtk_editable_get_chars( GTK_EDITABLE(m_widget), 0, len-1 );
98 }
99 else
100 {
101 tmp = gtk_entry_get_text( GTK_ENTRY(m_widget) );
102 };
103 return tmp;
104 };
105
106 void wxTextCtrl::SetValue( const wxString &value )
107 {
108 wxString tmp = "";
109 if (!value.IsNull()) tmp = value;
110 if (m_windowStyle & wxTE_MULTILINE)
111 {
112 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
113 gtk_editable_delete_text( GTK_EDITABLE(m_widget), 0, len-1 );
114 len = 0;
115 gtk_editable_insert_text( GTK_EDITABLE(m_widget), tmp, tmp.Length(), &len );
116 }
117 else
118 {
119 gtk_entry_set_text( GTK_ENTRY(m_widget), tmp );
120 };
121 };
122
123 void wxTextCtrl::WriteText( const wxString &text )
124 {
125 if (text.IsNull()) return;
126
127 if (m_windowStyle & wxTE_MULTILINE)
128 {
129 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
130 gtk_editable_insert_text( GTK_EDITABLE(m_widget), text, text.Length(), &len );
131 }
132 else
133 {
134 gtk_entry_append_text( GTK_ENTRY(m_widget), text );
135 };
136 };
137
138 bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) )
139 {
140 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
141
142 return FALSE;
143 };
144
145 bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) )
146 {
147 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
148
149 return FALSE;
150 };
151
152 bool wxTextCtrl::IsModified(void)
153 {
154 return m_modified;
155 };
156
157 void wxTextCtrl::DiscardEdits(void)
158 {
159 };
160
161 /*
162 wxString wxTextCtrl::GetLineText( const long lineNo ) const
163 {
164 };
165
166
167 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
168 {
169 };
170
171 long wxTextCtrl::PositionToXY( const long pos, long *x, long *y ) const
172 {
173 };
174
175 long wxTextCtrl::XYToPosition( const long x, const long y )
176 {
177 };
178
179 int wxTextCtrl::GetNumberOfLines(void)
180 {
181 };
182
183 */
184 void wxTextCtrl::SetInsertionPoint( const long pos )
185 {
186 int tmp = (int) pos;
187 if (m_windowStyle & wxTE_MULTILINE)
188 gtk_text_set_point( GTK_TEXT(m_widget), tmp );
189 else
190 gtk_entry_set_position( GTK_ENTRY(m_widget), tmp );
191 };
192
193 void wxTextCtrl::SetInsertionPointEnd(void)
194 {
195 int pos = 0;
196 if (m_windowStyle & wxTE_MULTILINE)
197 pos = gtk_text_get_length( GTK_TEXT(m_widget) );
198 else
199 pos = GTK_ENTRY(m_widget)->text_length;
200 SetInsertionPoint( pos-1 );
201 };
202
203 void wxTextCtrl::SetEditable( const bool editable )
204 {
205 if (m_windowStyle & wxTE_MULTILINE)
206 gtk_text_set_editable( GTK_TEXT(m_widget), editable );
207 else
208 gtk_entry_set_editable( GTK_ENTRY(m_widget), editable );
209 };
210
211 void wxTextCtrl::SetSelection( const long from, const long to )
212 {
213 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
214 };
215
216 void wxTextCtrl::ShowPosition( const long WXUNUSED(pos) )
217 {
218 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
219 };
220
221 long wxTextCtrl::GetInsertionPoint(void) const
222 {
223 return (long) GTK_EDITABLE(m_widget)->current_pos;
224 };
225
226 long wxTextCtrl::GetLastPosition(void) const
227 {
228 int pos = 0;
229 if (m_windowStyle & wxTE_MULTILINE)
230 pos = gtk_text_get_length( GTK_TEXT(m_widget) );
231 else
232 pos = GTK_ENTRY(m_widget)->text_length;
233 return (long)pos-1;
234 };
235
236 void wxTextCtrl::Remove( const long from, const long to )
237 {
238 gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
239 };
240
241 void wxTextCtrl::Replace( const long from, const long to, const wxString &value )
242 {
243 gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
244 if (value.IsNull()) return;
245 gint pos = (gint)to;
246 gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &pos );
247 };
248
249 void wxTextCtrl::Cut(void)
250 {
251 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget), 0 );
252 };
253
254 void wxTextCtrl::Copy(void)
255 {
256 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget), 0 );
257 };
258
259 void wxTextCtrl::Paste(void)
260 {
261 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget), 0 );
262 };
263
264 void wxTextCtrl::Delete(void)
265 {
266 SetValue( "" );
267 };
268
269 void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) )
270 {
271 };
272
273 int wxTextCtrl::overflow(int c)
274 {
275 // Make sure there is a holding area
276 if ( allocate()==EOF )
277 {
278 wxError("Streambuf allocation failed","Internal error");
279 return EOF;
280 }
281
282 // Verify that there are no characters in get area
283 if ( gptr() && gptr() < egptr() )
284 {
285 wxError("Who's trespassing my get area?","Internal error");
286 return EOF;
287 }
288
289 // Reset get area
290 setg(0,0,0);
291
292 // Make sure there is a put area
293 if ( ! pptr() )
294 {
295 /* This doesn't seem to be fatal so comment out error message */
296 // wxError("Put area not opened","Internal error");
297 setp( base(), base() );
298 }
299
300 // Determine how many characters have been inserted but no consumed
301 int plen = pptr() - pbase();
302
303 // Now Jerry relies on the fact that the buffer is at least 2 chars
304 // long, but the holding area "may be as small as 1" ???
305 // And we need an additional \0, so let's keep this inefficient but
306 // safe copy.
307
308 // If c!=EOF, it is a character that must also be comsumed
309 int xtra = c==EOF? 0 : 1;
310
311 // Write temporary C-string to wxTextWindow
312 {
313 char *txt = new char[plen+xtra+1];
314 memcpy(txt, pbase(), plen);
315 txt[plen] = (char)c; // append c
316 txt[plen+xtra] = '\0'; // append '\0' or overwrite c
317 // If the put area already contained \0, output will be truncated there
318 WriteText(txt);
319 delete[] txt;
320 }
321
322 // Reset put area
323 setp(pbase(), epptr());
324
325 #if defined(__WATCOMC__)
326 return __NOT_EOF;
327 #elif defined(zapeof) // HP-UX (all cfront based?)
328 return zapeof(c);
329 #else
330 return c!=EOF ? c : 0; // this should make everybody happy
331 #endif
332
333 /* OLD CODE
334 int len = pptr() - pbase();
335 char *txt = new char[len+1];
336 strncpy(txt, pbase(), len);
337 txt[len] = '\0';
338 (*this) << txt;
339 setp(pbase(), epptr());
340 delete[] txt;
341 return EOF;
342 */
343 };
344
345 int wxTextCtrl::sync(void)
346 {
347 // Verify that there are no characters in get area
348 if ( gptr() && gptr() < egptr() )
349 {
350 wxError("Who's trespassing my get area?","Internal error");
351 return EOF;
352 }
353
354 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
355
356 return 0;
357 /* OLD CODE
358 int len = pptr() - pbase();
359 char *txt = new char[len+1];
360 strncpy(txt, pbase(), len);
361 txt[len] = '\0';
362 (*this) << txt;
363 setp(pbase(), epptr());
364 delete[] txt;
365 return 0;
366 */
367 };
368
369 int wxTextCtrl::underflow(void)
370 {
371 return EOF;
372 };
373
374 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
375 {
376 WriteText(s);
377 return *this;
378 }
379
380 wxTextCtrl& wxTextCtrl::operator<<(const float f)
381 {
382 static char buf[100];
383 sprintf(buf, "%.2f", f);
384 WriteText(buf);
385 return *this;
386 }
387
388 wxTextCtrl& wxTextCtrl::operator<<(const double d)
389 {
390 static char buf[100];
391 sprintf(buf, "%.2f", d);
392 WriteText(buf);
393 return *this;
394 }
395
396 wxTextCtrl& wxTextCtrl::operator<<(const int i)
397 {
398 static char buf[100];
399 sprintf(buf, "%i", i);
400 WriteText(buf);
401 return *this;
402 }
403
404 wxTextCtrl& wxTextCtrl::operator<<(const long i)
405 {
406 static char buf[100];
407 sprintf(buf, "%ld", i);
408 WriteText(buf);
409 return *this;
410 }
411
412 wxTextCtrl& wxTextCtrl::operator<<(const char c)
413 {
414 char buf[2];
415
416 buf[0] = c;
417 buf[1] = 0;
418 WriteText(buf);
419 return *this;
420 }
421