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