]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/textctrl.cpp
regenerated configure from new configure.in
[wxWidgets.git] / src / gtk / textctrl.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "textctrl.h"
12#endif
13
14#include "wx/textctrl.h"
15#include "wx/utils.h"
16#include <wx/intl.h>
17
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <ctype.h>
21
22//-----------------------------------------------------------------------------
23// "changed"
24//-----------------------------------------------------------------------------
25
26static void gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
27{
28 win->SetModified();
29
30 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId );
31 wxString val( win->GetValue() );
32 if (!val.IsNull()) event.m_commandString = WXSTRINGCAST val;
33 event.SetEventObject( win );
34 win->GetEventHandler()->ProcessEvent( event );
35}
36
37//-----------------------------------------------------------------------------
38// wxTextCtrl
39//-----------------------------------------------------------------------------
40
41IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
42
43BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
44 EVT_CHAR(wxTextCtrl::OnChar)
45END_EVENT_TABLE()
46
47wxTextCtrl::wxTextCtrl() : streambuf()
48{
49 if (allocate()) setp(base(),ebuf());
50
51 m_modified = FALSE;
52}
53
54wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
55 const wxPoint &pos, const wxSize &size,
56 int style, const wxValidator& validator, const wxString &name ) : streambuf()
57{
58 if (allocate()) setp(base(),ebuf());
59
60 m_modified = FALSE;
61 Create( parent, id, value, pos, size, style, validator, name );
62}
63
64bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
65 const wxPoint &pos, const wxSize &size,
66 int style, const wxValidator& validator, const wxString &name )
67{
68 m_needParent = TRUE;
69
70 PreCreation( parent, id, pos, size, style, name );
71
72 SetValidator( validator );
73
74 bool bMultiLine = (style & wxTE_MULTILINE) != 0;
75 if ( bMultiLine )
76 {
77 // a multi-line edit control: create a vertical scrollbar by default and
78 // horizontal if requested
79 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
80
81 // create our control...
82 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
83
84 // ... and put into the upper left hand corner of the table
85 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
86 gtk_table_attach(GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
87 GTK_FILL | GTK_EXPAND,
88 GTK_FILL | GTK_EXPAND | GTK_SHRINK,
89 0, 0);
90
91 // put the horizontal scrollbar in the lower left hand corner
92 if (bHasHScrollbar)
93 {
94 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
95 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
96 GTK_EXPAND | GTK_FILL,
97 GTK_FILL,
98 0, 0);
99 gtk_widget_show(hscrollbar);
100 }
101
102 // finally, put the vertical scrollbar in the upper right corner
103 GtkWidget *vscrollbar = gtk_vscrollbar_new(GTK_TEXT(m_text)->vadj);
104 gtk_table_attach(GTK_TABLE(m_widget), vscrollbar, 1, 2, 0, 1,
105 GTK_FILL,
106 GTK_EXPAND | GTK_FILL | GTK_SHRINK,
107 0, 0);
108 gtk_widget_show( vscrollbar );
109 }
110 else
111 {
112 // a single-line text control: no need for scrollbars
113 m_widget =
114 m_text = gtk_entry_new();
115 }
116
117 wxSize newSize = size;
118 if (newSize.x == -1) newSize.x = 80;
119 if (newSize.y == -1) newSize.y = 26;
120 SetSize( newSize.x, newSize.y );
121
122 PostCreation();
123
124 if (bMultiLine)
125 {
126 gtk_widget_realize(m_text);
127 gtk_widget_show(m_text);
128 }
129
130 // we want to be notified about text changes
131 gtk_signal_connect(GTK_OBJECT(m_text), "changed",
132 GTK_SIGNAL_FUNC(gtk_text_changed_callback),
133 (gpointer)this);
134
135 if (!value.IsNull())
136 {
137 gint tmp = 0;
138 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
139 }
140
141 if (style & wxTE_READONLY)
142 {
143 }
144 else
145 {
146 if (bMultiLine)
147 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
148 }
149
150 Show( TRUE );
151
152 SetBackgroundColour( parent->GetBackgroundColour() );
153 SetForegroundColour( parent->GetForegroundColour() );
154
155 return TRUE;
156}
157
158wxString wxTextCtrl::GetValue() const
159{
160 wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" );
161
162 wxString tmp;
163 if (m_windowStyle & wxTE_MULTILINE)
164 {
165 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
166 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
167 tmp = text;
168 g_free( text );
169 }
170 else
171 {
172 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
173 }
174 return tmp;
175}
176
177void wxTextCtrl::SetValue( const wxString &value )
178{
179 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
180
181 wxString tmp = "";
182 if (!value.IsNull()) tmp = value;
183 if (m_windowStyle & wxTE_MULTILINE)
184 {
185 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
186 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
187 len = 0;
188 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
189 }
190 else
191 {
192 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
193 }
194}
195
196void wxTextCtrl::WriteText( const wxString &text )
197{
198 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
199
200 if (text.IsNull()) return;
201
202 if (m_windowStyle & wxTE_MULTILINE)
203 {
204 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
205 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
206 }
207 else
208 {
209 gtk_entry_append_text( GTK_ENTRY(m_text), text );
210 }
211}
212
213bool wxTextCtrl::LoadFile( const wxString &file )
214{
215 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
216
217 if (!wxFileExists(file)) return FALSE;
218
219 Clear();
220
221 FILE *fp = NULL;
222 struct stat statb;
223
224 if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
225 !(fp = fopen ((char*) (const char*) file, "r")))
226 {
227 return FALSE;
228 }
229 else
230 {
231 gint len = statb.st_size;
232 char *text;
233 if (!(text = (char*)malloc ((unsigned) (len + 1))))
234 {
235 fclose (fp);
236 return FALSE;
237 }
238 if (fread (text, sizeof (char), len, fp) != (size_t) len)
239 {
240 }
241 fclose (fp);
242
243 text[len] = 0;
244
245 if (m_windowStyle & wxTE_MULTILINE)
246 {
247 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, 0, &len );
248 }
249 else
250 {
251 gtk_entry_set_text( GTK_ENTRY(m_text), text );
252 }
253
254 free (text);
255 m_modified = FALSE;
256 return TRUE;
257 }
258 return FALSE;
259}
260
261bool wxTextCtrl::SaveFile( const wxString &file )
262{
263 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
264
265 if (file == "") return FALSE;
266
267 FILE *fp;
268
269 if (!(fp = fopen ((char*) (const char*) file, "w")))
270 {
271 return FALSE;
272 }
273 else
274 {
275 char *text = NULL;
276 gint len = 0;
277
278 if (m_windowStyle & wxTE_MULTILINE)
279 {
280 len = gtk_text_get_length( GTK_TEXT(m_text) );
281 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
282 }
283 else
284 {
285 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
286 }
287
288 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
289 {
290 // Did not write whole file
291 }
292
293 // Make sure newline terminates the file
294 if (text[len - 1] != '\n')
295 fputc ('\n', fp);
296
297 fclose (fp);
298
299 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
300
301 m_modified = FALSE;
302 return TRUE;
303 }
304
305 return TRUE;
306}
307
308wxString wxTextCtrl::GetLineText( long lineNo ) const
309{
310 if (m_windowStyle & wxTE_MULTILINE)
311 {
312 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
313 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
314
315 if (text)
316 {
317 wxString buf("");
318 long i;
319 int currentLine = 0;
320 for (i = 0; currentLine != lineNo && text[i]; i++ )
321 if (text[i] == '\n')
322 currentLine++;
323 // Now get the text
324 int j;
325 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
326 buf += text[i];
327
328 g_free( text );
329 return buf;
330 }
331 else
332 return wxEmptyString;
333 }
334 else
335 {
336 if (lineNo == 0) return GetValue();
337 return wxEmptyString;
338 }
339}
340
341void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
342{
343 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
344}
345
346long wxTextCtrl::PositionToXY( long WXUNUSED(pos), long *WXUNUSED(x), long *WXUNUSED(y) ) const
347{
348 wxFAIL_MSG( "wxTextCtrl::XYToPosition not implemented" );
349
350 return 0;
351}
352
353long wxTextCtrl::XYToPosition( long WXUNUSED(x), long WXUNUSED(y) ) const
354{
355 wxFAIL_MSG( "wxTextCtrl::XYToPosition not implemented" );
356
357 return 0;
358}
359
360int wxTextCtrl::GetLineLength(long lineNo) const
361{
362 wxString str = GetLineText (lineNo);
363 return (int) str.Length();
364}
365
366int wxTextCtrl::GetNumberOfLines() const
367{
368 if (m_windowStyle & wxTE_MULTILINE)
369 {
370 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
371 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
372
373 if (text)
374 {
375 int currentLine = 0;
376 for (int i = 0; i < len; i++ )
377 if (text[i] == '\n')
378 currentLine++;
379
380 g_free( text );
381 return currentLine;
382 }
383 else
384 return 0;
385 }
386 else
387 {
388 return 1;
389 }
390}
391
392void wxTextCtrl::SetInsertionPoint( long pos )
393{
394 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
395
396 int tmp = (int) pos;
397 if (m_windowStyle & wxTE_MULTILINE)
398 gtk_text_set_point( GTK_TEXT(m_text), tmp );
399 else
400 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
401}
402
403void wxTextCtrl::SetInsertionPointEnd()
404{
405 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
406
407 int pos = 0;
408 if (m_windowStyle & wxTE_MULTILINE)
409 pos = gtk_text_get_length( GTK_TEXT(m_text) );
410 else
411 pos = GTK_ENTRY(m_text)->text_length;
412 SetInsertionPoint( pos-1 );
413}
414
415void wxTextCtrl::SetEditable( bool editable )
416{
417 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
418
419 if (m_windowStyle & wxTE_MULTILINE)
420 gtk_text_set_editable( GTK_TEXT(m_text), editable );
421 else
422 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
423}
424
425void wxTextCtrl::SetSelection( long from, long to )
426{
427 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
428
429 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
430}
431
432void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
433{
434 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
435}
436
437long wxTextCtrl::GetInsertionPoint() const
438{
439 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
440
441 return (long) GTK_EDITABLE(m_text)->current_pos;
442}
443
444long wxTextCtrl::GetLastPosition() const
445{
446 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
447
448 int pos = 0;
449 if (m_windowStyle & wxTE_MULTILINE)
450 pos = gtk_text_get_length( GTK_TEXT(m_text) );
451 else
452 pos = GTK_ENTRY(m_text)->text_length;
453 return (long)pos-1;
454}
455
456void wxTextCtrl::Remove( long from, long to )
457{
458 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
459
460 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
461}
462
463void wxTextCtrl::Replace( long from, long to, const wxString &value )
464{
465 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
466
467 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
468 if (value.IsNull()) return;
469 gint pos = (gint)to;
470 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
471}
472
473void wxTextCtrl::Cut()
474{
475 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
476
477#if (GTK_MINOR_VERSION == 1)
478 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
479#else
480 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
481#endif
482}
483
484void wxTextCtrl::Copy()
485{
486 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
487
488#if (GTK_MINOR_VERSION == 1)
489 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
490#else
491 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
492#endif
493}
494
495void wxTextCtrl::Paste()
496{
497 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
498
499#if (GTK_MINOR_VERSION == 1)
500 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
501#else
502 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
503#endif
504}
505
506void wxTextCtrl::Clear()
507{
508 SetValue( "" );
509}
510
511void wxTextCtrl::OnChar( wxKeyEvent &key_event )
512{
513 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
514 {
515 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
516 event.SetEventObject(this);
517 if (GetEventHandler()->ProcessEvent(event)) return;
518 }
519 else if (key_event.KeyCode() == WXK_TAB)
520 {
521 wxNavigationKeyEvent event;
522 event.SetDirection( key_event.m_shiftDown );
523 event.SetWindowChange(FALSE);
524 event.SetEventObject(this);
525
526 if (GetEventHandler()->ProcessEvent(event)) return;
527 }
528 key_event.Skip();
529}
530
531int wxTextCtrl::overflow( int WXUNUSED(c) )
532{
533 int len = pptr() - pbase();
534 char *txt = new char[len+1];
535 strncpy(txt, pbase(), len);
536 txt[len] = '\0';
537 (*this) << txt;
538 setp(pbase(), epptr());
539 delete[] txt;
540 return EOF;
541}
542
543int wxTextCtrl::sync()
544{
545 int len = pptr() - pbase();
546 char *txt = new char[len+1];
547 strncpy(txt, pbase(), len);
548 txt[len] = '\0';
549 (*this) << txt;
550 setp(pbase(), epptr());
551 delete[] txt;
552 return 0;
553}
554
555int wxTextCtrl::underflow()
556{
557 return EOF;
558}
559
560wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
561{
562 WriteText(s);
563 return *this;
564}
565
566wxTextCtrl& wxTextCtrl::operator<<(float f)
567{
568 static char buf[100];
569 sprintf(buf, "%.2f", f);
570 WriteText(buf);
571 return *this;
572}
573
574wxTextCtrl& wxTextCtrl::operator<<(double d)
575{
576 static char buf[100];
577 sprintf(buf, "%.2f", d);
578 WriteText(buf);
579 return *this;
580}
581
582wxTextCtrl& wxTextCtrl::operator<<(int i)
583{
584 static char buf[100];
585 sprintf(buf, "%i", i);
586 WriteText(buf);
587 return *this;
588}
589
590wxTextCtrl& wxTextCtrl::operator<<(long i)
591{
592 static char buf[100];
593 sprintf(buf, "%ld", i);
594 WriteText(buf);
595 return *this;
596}
597
598wxTextCtrl& wxTextCtrl::operator<<(const char c)
599{
600 char buf[2];
601
602 buf[0] = c;
603 buf[1] = 0;
604 WriteText(buf);
605 return *this;
606}
607
608GtkWidget* wxTextCtrl::GetConnectWidget()
609{
610 return GTK_WIDGET(m_text);
611}
612
613bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
614{
615 if (m_windowStyle & wxTE_MULTILINE)
616 return (window == GTK_TEXT(m_text)->text_area);
617 else
618 return (window == GTK_ENTRY(m_text)->text_area);
619}
620
621void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) )
622{
623 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
624
625 // doesn't work
626}
627
628void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
629{
630 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
631
632 // doesn't work
633}
634
635void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
636{
637 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
638
639 wxControl::SetBackgroundColour( colour );
640
641 if (!m_backgroundColour.Ok()) return;
642
643 if (m_windowStyle & wxTE_MULTILINE)
644 {
645 GdkWindow *window = GTK_TEXT(m_text)->text_area;
646 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
647 gdk_window_set_background( window, m_backgroundColour.GetColor() );
648 gdk_window_clear( window );
649 }
650}
651
652void wxTextCtrl::ApplyWidgetStyle()
653{
654 if (m_windowStyle & wxTE_MULTILINE)
655 {
656 }
657 else
658 {
659 SetWidgetStyle();
660 gtk_widget_set_style( m_text, m_widgetStyle );
661 }
662}
663