]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
wxMotif Solaris 2.6 compilation fixes
[wxWidgets.git] / src / gtk1 / textctrl.cpp
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 #include "wx/settings.h"
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <ctype.h>
22
23 #include "gdk/gdk.h"
24 #include "gtk/gtk.h"
25 #include "gdk/gdkkeysyms.h"
26
27 //-----------------------------------------------------------------------------
28 // data
29 //-----------------------------------------------------------------------------
30
31 extern bool g_blockEventsOnDrag;
32
33 //-----------------------------------------------------------------------------
34 // "changed"
35 //-----------------------------------------------------------------------------
36
37 static void
38 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
39 {
40 win->SetModified();
41
42 win->CalculateScrollbar();
43
44 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId );
45 event.SetString( copystring(win->GetValue()) );
46 event.SetEventObject( win );
47 win->GetEventHandler()->ProcessEvent( event );
48 delete[] event.GetString();
49 }
50
51 //-----------------------------------------------------------------------------
52 // "size_allocate"
53 //-----------------------------------------------------------------------------
54
55 static void
56 gtk_text_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* WXUNUSED(alloc), wxTextCtrl *win )
57 {
58 win->CalculateScrollbar();
59 }
60
61 //-----------------------------------------------------------------------------
62 // wxTextCtrl
63 //-----------------------------------------------------------------------------
64
65 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
66
67 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
68 EVT_CHAR(wxTextCtrl::OnChar)
69 END_EVENT_TABLE()
70
71 #ifndef NO_TEXT_WINDOW_STREAM
72 wxTextCtrl::wxTextCtrl() : streambuf()
73 {
74 if (allocate()) setp(base(),ebuf());
75
76 m_modified = FALSE;
77 }
78 #else
79 wxTextCtrl::wxTextCtrl()
80 {
81 m_modified = FALSE;
82 }
83 #endif
84
85 #ifndef NO_TEXT_WINDOW_STREAM
86 wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
87 const wxPoint &pos, const wxSize &size,
88 int style, const wxValidator& validator, const wxString &name ) : streambuf()
89 {
90 if (allocate()) setp(base(),ebuf());
91
92 m_modified = FALSE;
93 Create( parent, id, value, pos, size, style, validator, name );
94 }
95 #else
96 wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
97 const wxPoint &pos, const wxSize &size,
98 int style, const wxValidator& validator, const wxString &name )
99 {
100 m_modified = FALSE;
101 Create( parent, id, value, pos, size, style, validator, name );
102 }
103 #endif
104
105 bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
106 const wxPoint &pos, const wxSize &size,
107 int style, const wxValidator& validator, const wxString &name )
108 {
109 m_needParent = TRUE;
110 m_acceptsFocus = TRUE;
111
112 PreCreation( parent, id, pos, size, style, name );
113
114 SetValidator( validator );
115
116 m_vScrollbarVisible = TRUE;
117
118 bool multi_line = (style & wxTE_MULTILINE) != 0;
119 if ( multi_line )
120 {
121 // a multi-line edit control: create a vertical scrollbar by default and
122 // horizontal if requested
123 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
124
125 // create our control...
126 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
127
128 // ... and put into the upper left hand corner of the table
129 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
130 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
131
132 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
133 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
134 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
135 0, 0);
136
137 // put the horizontal scrollbar in the lower left hand corner
138 if (bHasHScrollbar)
139 {
140 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
141 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
142
143 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
144 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
145 GTK_FILL,
146 0, 0);
147 gtk_widget_show(hscrollbar);
148 }
149
150 // finally, put the vertical scrollbar in the upper right corner
151 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
152 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
153
154 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
155 GTK_FILL,
156 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
157 0, 0);
158 gtk_widget_show( m_vScrollbar );
159
160 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
161 GTK_SIGNAL_FUNC(gtk_text_size_callback), (gpointer)this );
162 }
163 else
164 {
165 // a single-line text control: no need for scrollbars
166 m_widget =
167 m_text = gtk_entry_new();
168 }
169
170 wxSize newSize = size;
171 if (newSize.x == -1) newSize.x = 80;
172 if (newSize.y == -1) newSize.y = 26;
173 SetSize( newSize.x, newSize.y );
174
175 m_parent->AddChild( this );
176
177 (m_parent->m_insertCallback)( m_parent, this );
178
179 PostCreation();
180
181 if (multi_line)
182 {
183 gtk_widget_realize(m_text);
184 gtk_widget_show(m_text);
185 }
186
187 // we want to be notified about text changes
188 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
189 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
190
191 if (!value.IsEmpty())
192 {
193 gint tmp = 0;
194 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
195
196 if (multi_line)
197 {
198 /* bring editable's cursor uptodate. bug in GTK. */
199
200 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
201 }
202 }
203
204 if (style & wxTE_PASSWORD)
205 {
206 if (!multi_line)
207 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
208 }
209
210 if (style & wxTE_READONLY)
211 {
212 if (!multi_line)
213 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
214 }
215 else
216 {
217 if (multi_line)
218 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
219 }
220
221 Show( TRUE );
222
223 SetBackgroundColour( parent->GetBackgroundColour() );
224 SetForegroundColour( parent->GetForegroundColour() );
225
226 return TRUE;
227 }
228
229 void wxTextCtrl::CalculateScrollbar()
230 {
231 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
232
233 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
234
235 if (adj->upper - adj->page_size < 0.8)
236 {
237 if (m_vScrollbarVisible)
238 {
239 gtk_widget_hide( m_vScrollbar );
240
241 m_vScrollbarVisible = FALSE;
242 }
243 }
244 else
245 {
246 if (!m_vScrollbarVisible)
247 {
248 gtk_widget_show( m_vScrollbar );
249
250 m_vScrollbarVisible = TRUE;
251 }
252 }
253 }
254
255 wxString wxTextCtrl::GetValue() const
256 {
257 wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" );
258
259 wxString tmp;
260 if (m_windowStyle & wxTE_MULTILINE)
261 {
262 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
263 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
264 tmp = text;
265 g_free( text );
266 }
267 else
268 {
269 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
270 }
271 return tmp;
272 }
273
274 void wxTextCtrl::SetValue( const wxString &value )
275 {
276 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
277
278 wxString tmp = "";
279 if (!value.IsNull()) tmp = value;
280 if (m_windowStyle & wxTE_MULTILINE)
281 {
282 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
283 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
284 len = 0;
285 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
286 }
287 else
288 {
289 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
290 }
291 }
292
293 void wxTextCtrl::WriteText( const wxString &text )
294 {
295 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
296
297 if (text.IsNull()) return;
298
299 if (m_windowStyle & wxTE_MULTILINE)
300 {
301 /* this moves the cursor pos to behind the inserted text */
302 gint len = GTK_EDITABLE(m_text)->current_pos;
303
304 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
305
306 /* bring editable's cursor uptodate. bug in GTK. */
307 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
308 }
309 else
310 {
311 /* this moves the cursor pos to behind the inserted text */
312 gint len = GTK_EDITABLE(m_text)->current_pos;
313 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
314
315 /* bring editable's cursor uptodate. bug in GTK. */
316 GTK_EDITABLE(m_text)->current_pos += text.Len();
317
318 /* bring entry's cursor uptodate. bug in GTK. */
319 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
320 }
321 }
322
323 void wxTextCtrl::AppendText( const wxString &text )
324 {
325 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
326
327 if (m_windowStyle & wxTE_MULTILINE)
328 {
329 /* we'll insert at the last position */
330 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
331 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
332
333 /* bring editable's cursor uptodate. bug in GTK. */
334 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
335 }
336 else
337 {
338 gtk_entry_append_text( GTK_ENTRY(m_text), text );
339 }
340 }
341
342 bool wxTextCtrl::LoadFile( const wxString &file )
343 {
344 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
345
346 if (!wxFileExists(file)) return FALSE;
347
348 Clear();
349
350 FILE *fp = (FILE*) NULL;
351 struct stat statb;
352
353 if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
354 !(fp = fopen ((char*) (const char*) file, "r")))
355 {
356 return FALSE;
357 }
358 else
359 {
360 gint len = statb.st_size;
361 char *text;
362 if (!(text = (char*)malloc ((unsigned) (len + 1))))
363 {
364 fclose (fp);
365 return FALSE;
366 }
367 if (fread (text, sizeof (char), len, fp) != (size_t) len)
368 {
369 }
370 fclose (fp);
371
372 text[len] = 0;
373
374 if (m_windowStyle & wxTE_MULTILINE)
375 {
376 gint pos = 0;
377 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, len, &pos );
378 }
379 else
380 {
381 gtk_entry_set_text( GTK_ENTRY(m_text), text );
382 }
383
384 free (text);
385 m_modified = FALSE;
386 return TRUE;
387 }
388 return FALSE;
389 }
390
391 bool wxTextCtrl::SaveFile( const wxString &file )
392 {
393 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
394
395 if (file == "") return FALSE;
396
397 FILE *fp;
398
399 if (!(fp = fopen ((char*) (const char*) file, "w")))
400 {
401 return FALSE;
402 }
403 else
404 {
405 char *text = (char*) NULL;
406 gint len = 0;
407
408 if (m_windowStyle & wxTE_MULTILINE)
409 {
410 len = gtk_text_get_length( GTK_TEXT(m_text) );
411 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
412 }
413 else
414 {
415 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
416 }
417
418 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
419 {
420 // Did not write whole file
421 }
422
423 // Make sure newline terminates the file
424 if (text[len - 1] != '\n')
425 fputc ('\n', fp);
426
427 fclose (fp);
428
429 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
430
431 m_modified = FALSE;
432 return TRUE;
433 }
434
435 return TRUE;
436 }
437
438 wxString wxTextCtrl::GetLineText( long lineNo ) const
439 {
440 if (m_windowStyle & wxTE_MULTILINE)
441 {
442 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
443 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
444
445 if (text)
446 {
447 wxString buf("");
448 long i;
449 int currentLine = 0;
450 for (i = 0; currentLine != lineNo && text[i]; i++ )
451 if (text[i] == '\n')
452 currentLine++;
453 // Now get the text
454 int j;
455 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
456 buf += text[i];
457
458 g_free( text );
459 return buf;
460 }
461 else
462 return wxEmptyString;
463 }
464 else
465 {
466 if (lineNo == 0) return GetValue();
467 return wxEmptyString;
468 }
469 }
470
471 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
472 {
473 /* If you implement this, don't forget to update the documentation!
474 * (file docs/latex/wx/text.tex) */
475 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
476 }
477
478 long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
479 {
480 if ( m_windowStyle & wxTE_MULTILINE )
481 {
482 wxString text = GetValue();
483
484 // cast to prevent warning. But pos really should've been unsigned.
485 if( (unsigned long)pos > text.Len() )
486 return FALSE;
487
488 *x=0; // First Col
489 *y=0; // First Line
490
491 const char* stop = text.c_str() + pos;
492 for ( const char *p = text.c_str(); p < stop; p++ )
493 {
494 if (*p == '\n')
495 {
496 (*y)++;
497 *x=0;
498 }
499 else
500 (*x)++;
501 }
502 }
503 else // single line control
504 {
505 if ( pos <= GTK_ENTRY(m_text)->text_length )
506 {
507 *y = 0;
508 *x = pos;
509 }
510 else
511 {
512 // index out of bounds
513 return FALSE;
514 }
515 }
516
517 return TRUE;
518 }
519
520 long wxTextCtrl::XYToPosition(long x, long y ) const
521 {
522 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
523
524 long pos=0;
525 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
526
527 pos += x;
528 return pos;
529 }
530
531 int wxTextCtrl::GetLineLength(long lineNo) const
532 {
533 wxString str = GetLineText (lineNo);
534 return (int) str.Length();
535 }
536
537 int wxTextCtrl::GetNumberOfLines() const
538 {
539 if (m_windowStyle & wxTE_MULTILINE)
540 {
541 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
542 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
543
544 if (text)
545 {
546 int currentLine = 0;
547 for (int i = 0; i < len; i++ )
548 {
549 if (text[i] == '\n')
550 currentLine++;
551 }
552 g_free( text );
553
554 // currentLine is 0 based, add 1 to get number of lines
555 return currentLine + 1;
556 }
557 else
558 {
559 return 0;
560 }
561 }
562 else
563 {
564 return 1;
565 }
566 }
567
568 void wxTextCtrl::SetInsertionPoint( long pos )
569 {
570 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
571
572 if (m_windowStyle & wxTE_MULTILINE)
573 {
574 /* seems to be broken in GTK 1.0.X:
575 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
576
577 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
578 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
579
580 /* we fake a set_point by inserting and deleting. as the user
581 isn't supposed to get to know about thos non-sense, we
582 disconnect so that no events are sent to the user program. */
583
584 gint tmp = (gint)pos;
585 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
586 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
587
588 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
589 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
590
591 /* bring editable's cursor uptodate. another bug in GTK. */
592
593 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
594 }
595 else
596 {
597 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
598
599 /* bring editable's cursor uptodate. bug in GTK. */
600
601 GTK_EDITABLE(m_text)->current_pos = pos;
602 }
603 }
604
605 void wxTextCtrl::SetInsertionPointEnd()
606 {
607 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
608
609 if (m_windowStyle & wxTE_MULTILINE)
610 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
611 else
612 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
613 }
614
615 void wxTextCtrl::SetEditable( bool editable )
616 {
617 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
618
619 if (m_windowStyle & wxTE_MULTILINE)
620 gtk_text_set_editable( GTK_TEXT(m_text), editable );
621 else
622 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
623 }
624
625 void wxTextCtrl::SetSelection( long from, long to )
626 {
627 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
628
629 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
630 }
631
632 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
633 {
634 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
635 }
636
637 long wxTextCtrl::GetInsertionPoint() const
638 {
639 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
640
641 return (long) GTK_EDITABLE(m_text)->current_pos;
642 }
643
644 long wxTextCtrl::GetLastPosition() const
645 {
646 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
647
648 int pos = 0;
649 if (m_windowStyle & wxTE_MULTILINE)
650 pos = gtk_text_get_length( GTK_TEXT(m_text) );
651 else
652 pos = GTK_ENTRY(m_text)->text_length;
653
654 return (long)pos;
655 }
656
657 void wxTextCtrl::Remove( long from, long to )
658 {
659 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
660
661 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
662 }
663
664 void wxTextCtrl::Replace( long from, long to, const wxString &value )
665 {
666 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
667
668 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
669 if (value.IsNull()) return;
670 gint pos = (gint)from;
671 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
672 }
673
674 void wxTextCtrl::Cut()
675 {
676 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
677
678 #if (GTK_MINOR_VERSION == 1)
679 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
680 #else
681 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
682 #endif
683 }
684
685 void wxTextCtrl::Copy()
686 {
687 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
688
689 #if (GTK_MINOR_VERSION == 1)
690 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
691 #else
692 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
693 #endif
694 }
695
696 void wxTextCtrl::Paste()
697 {
698 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
699
700 #if (GTK_MINOR_VERSION == 1)
701 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
702 #else
703 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
704 #endif
705 }
706
707 void wxTextCtrl::Clear()
708 {
709 SetValue( "" );
710 }
711
712 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
713 {
714 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
715
716 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
717 {
718 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
719 event.SetEventObject(this);
720 if (GetEventHandler()->ProcessEvent(event)) return;
721 }
722
723 key_event.Skip();
724 }
725
726 #ifndef NO_TEXT_WINDOW_STREAM
727 int wxTextCtrl::overflow( int WXUNUSED(c) )
728 {
729 int len = pptr() - pbase();
730 char *txt = new char[len+1];
731 strncpy(txt, pbase(), len);
732 txt[len] = '\0';
733 (*this) << txt;
734 setp(pbase(), epptr());
735 delete[] txt;
736 return EOF;
737 }
738
739 int wxTextCtrl::sync()
740 {
741 int len = pptr() - pbase();
742 char *txt = new char[len+1];
743 strncpy(txt, pbase(), len);
744 txt[len] = '\0';
745 (*this) << txt;
746 setp(pbase(), epptr());
747 delete[] txt;
748 return 0;
749 }
750
751 int wxTextCtrl::underflow()
752 {
753 return EOF;
754 }
755
756 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
757 {
758 AppendText(s);
759 return *this;
760 }
761
762 wxTextCtrl& wxTextCtrl::operator<<(float f)
763 {
764 static char buf[100];
765 sprintf(buf, "%.2f", f);
766 AppendText(buf);
767 return *this;
768 }
769
770 wxTextCtrl& wxTextCtrl::operator<<(double d)
771 {
772 static char buf[100];
773 sprintf(buf, "%.2f", d);
774 AppendText(buf);
775 return *this;
776 }
777
778 wxTextCtrl& wxTextCtrl::operator<<(int i)
779 {
780 static char buf[100];
781 sprintf(buf, "%i", i);
782 AppendText(buf);
783 return *this;
784 }
785
786 wxTextCtrl& wxTextCtrl::operator<<(long i)
787 {
788 static char buf[100];
789 sprintf(buf, "%ld", i);
790 AppendText(buf);
791 return *this;
792 }
793
794 wxTextCtrl& wxTextCtrl::operator<<(const char c)
795 {
796 char buf[2];
797
798 buf[0] = c;
799 buf[1] = 0;
800 AppendText(buf);
801 return *this;
802 }
803 #endif
804
805 GtkWidget* wxTextCtrl::GetConnectWidget()
806 {
807 return GTK_WIDGET(m_text);
808 }
809
810 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
811 {
812 if (m_windowStyle & wxTE_MULTILINE)
813 return (window == GTK_TEXT(m_text)->text_area);
814 else
815 return (window == GTK_ENTRY(m_text)->text_area);
816 }
817
818 void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) )
819 {
820 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
821
822 // doesn't work
823 }
824
825 void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
826 {
827 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
828
829 // doesn't work
830 }
831
832 void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
833 {
834 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
835
836 wxControl::SetBackgroundColour( colour );
837
838 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
839 if (sysbg.Red() == colour.Red() &&
840 sysbg.Green() == colour.Green() &&
841 sysbg.Blue() == colour.Blue())
842 {
843 return;
844 }
845
846 if (!m_backgroundColour.Ok()) return;
847
848 if (m_windowStyle & wxTE_MULTILINE)
849 {
850 GdkWindow *window = GTK_TEXT(m_text)->text_area;
851 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
852 gdk_window_set_background( window, m_backgroundColour.GetColor() );
853 gdk_window_clear( window );
854 }
855 }
856
857 void wxTextCtrl::ApplyWidgetStyle()
858 {
859 if (m_windowStyle & wxTE_MULTILINE)
860 {
861 // how ?
862 }
863 else
864 {
865 SetWidgetStyle();
866 gtk_widget_set_style( m_text, m_widgetStyle );
867 }
868 }
869