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