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