1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "textctrl.h"
14 #include "wx/textctrl.h"
18 #include "wx/settings.h"
21 #include <sys/types.h>
27 #include "gdk/gdkkeysyms.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
41 extern wxCursor g_globalCursor
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
48 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
50 if (!win
->m_hasVMT
) return;
53 wxapp_install_idle_handler();
56 win
->UpdateFontIfNeeded();
58 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
59 event
.SetString( win
->GetValue() );
60 event
.SetEventObject( win
);
61 win
->GetEventHandler()->ProcessEvent( event
);
64 //-----------------------------------------------------------------------------
65 // "changed" from vertical scrollbar
66 //-----------------------------------------------------------------------------
69 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
71 if (!win
->m_hasVMT
) return;
74 wxapp_install_idle_handler();
76 win
->CalculateScrollbar();
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 wxWindow
*FindFocusedChild(wxWindow
*win
);
84 extern wxWindow
*g_focusWindow
;
85 extern bool g_blockEventsOnDrag
;
86 // extern bool g_isIdle;
88 static gint
gtk_text_focus_in_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
93 wxapp_install_idle_handler();
95 if (!win
->m_hasVMT
) return FALSE
;
96 if (g_blockEventsOnDrag
) return FALSE
;
100 wxPanel
*panel
= wxDynamicCast(win
->GetParent(), wxPanel
);
103 panel
->SetLastFocus(win
);
108 gdk_im_begin(win
->m_ic
, win
->m_wxwindow
->window
);
113 // caret needs to be informed about focus change
114 wxCaret
*caret
= win
->GetCaret();
119 #endif // wxUSE_CARET
122 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
123 event
.SetEventObject( win
);
125 if (win
->GetEventHandler()->ProcessEvent( event
))
127 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus_in_event" );
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 static gint
gtk_text_focus_out_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
142 wxapp_install_idle_handler();
145 if (!win
->m_hasVMT
) return FALSE
;
146 if (g_blockEventsOnDrag
) return FALSE
;
149 // if the focus goes out of our app alltogether, OnIdle() will send
150 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
151 // g_sendActivateEvent to -1
152 g_sendActivateEvent
= 0;
155 wxWindow
*winFocus
= FindFocusedChild(win
);
159 g_focusWindow
= (wxWindow
*)NULL
;
168 // caret needs to be informed about focus change
169 wxCaret
*caret
= win
->GetCaret();
172 caret
->OnKillFocus();
174 #endif // wxUSE_CARET
177 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
178 event
.SetEventObject( win
);
180 if (win
->GetEventHandler()->ProcessEvent( event
))
182 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus_out_event" );
189 //-----------------------------------------------------------------------------
191 //-----------------------------------------------------------------------------
193 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
195 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
196 EVT_CHAR(wxTextCtrl::OnChar
)
198 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
199 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
200 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
201 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
202 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
204 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
205 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
206 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
207 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
208 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
211 void wxTextCtrl::Init()
214 m_updateFont
= FALSE
;
216 m_vScrollbar
= (GtkWidget
*)NULL
;
219 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
221 const wxString
&value
,
225 const wxValidator
& validator
,
226 const wxString
&name
)
230 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
233 bool wxTextCtrl::Create( wxWindow
*parent
,
235 const wxString
&value
,
239 const wxValidator
& validator
,
240 const wxString
&name
)
243 m_acceptsFocus
= TRUE
;
245 if (!PreCreation( parent
, pos
, size
) ||
246 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
248 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
253 m_vScrollbarVisible
= FALSE
;
255 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
258 #if (GTK_MINOR_VERSION > 2)
259 /* a multi-line edit control: create a vertical scrollbar by default and
260 horizontal if requested */
261 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
263 bool bHasHScrollbar
= FALSE
;
266 /* create our control ... */
267 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
269 /* ... and put into the upper left hand corner of the table */
270 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
271 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
272 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
273 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
274 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
277 /* always wrap words */
278 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
280 #if (GTK_MINOR_VERSION > 2)
281 /* put the horizontal scrollbar in the lower left hand corner */
284 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
285 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
286 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
287 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
290 gtk_widget_show(hscrollbar
);
292 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
293 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
297 /* finally, put the vertical scrollbar in the upper right corner */
298 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
299 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
300 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
302 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
307 /* a single-line text control: no need for scrollbars */
309 m_text
= gtk_entry_new();
312 m_parent
->DoAddChild( this );
316 SetFont( parent
->GetFont() );
318 wxSize
size_best( DoGetBestSize() );
319 wxSize
new_size( size
);
320 if (new_size
.x
== -1)
321 new_size
.x
= size_best
.x
;
322 if (new_size
.y
== -1)
323 new_size
.y
= size_best
.y
;
324 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
325 SetSize( new_size
.x
, new_size
.y
);
328 gtk_widget_show(m_text
);
330 /* we want to be notified about text changes */
331 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
332 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
336 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
337 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
339 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_in_event",
340 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
342 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_out_event",
343 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
347 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_in_event",
348 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
350 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_out_event",
351 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
354 if (!value
.IsEmpty())
358 #if GTK_MINOR_VERSION == 0
359 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
360 // gtk_editable_insert_text()
361 gtk_widget_realize(m_text
);
365 wxWX2MBbuf val
= value
.mbc_str();
366 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
368 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
369 #endif // Unicode/!Unicode
373 /* bring editable's cursor uptodate. bug in GTK. */
375 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
379 if (style
& wxTE_PASSWORD
)
382 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
385 if (style
& wxTE_READONLY
)
388 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
393 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
396 SetBackgroundColour( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
) );
397 SetForegroundColour( parent
->GetForegroundColour() );
399 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
406 void wxTextCtrl::CalculateScrollbar()
408 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
410 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
412 if (adj
->upper
- adj
->page_size
< 0.8)
414 if (m_vScrollbarVisible
)
416 gtk_widget_hide( m_vScrollbar
);
417 m_vScrollbarVisible
= FALSE
;
422 if (!m_vScrollbarVisible
)
424 gtk_widget_show( m_vScrollbar
);
425 m_vScrollbarVisible
= TRUE
;
430 wxString
wxTextCtrl::GetValue() const
432 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
435 if (m_windowStyle
& wxTE_MULTILINE
)
437 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
438 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
439 tmp
= wxString(text
,*wxConvCurrent
);
444 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
449 void wxTextCtrl::SetValue( const wxString
&value
)
451 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
453 if (m_windowStyle
& wxTE_MULTILINE
)
455 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
456 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
459 wxWX2MBbuf tmpbuf
= value
.mbc_str();
460 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
462 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
467 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
470 // GRG, Jun/2000: Changed this after a lot of discussion in
471 // the lists. wxWindows 2.2 will have a set of flags to
472 // customize this behaviour.
473 SetInsertionPoint(0);
478 void wxTextCtrl::WriteText( const wxString
&text
)
480 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
482 if (text
.IsEmpty()) return;
484 if (m_windowStyle
& wxTE_MULTILINE
)
486 /* this moves the cursor pos to behind the inserted text */
487 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
490 wxWX2MBbuf buf
= text
.mbc_str();
491 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
493 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
496 /* bring editable's cursor uptodate. bug in GTK. */
497 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
501 /* this moves the cursor pos to behind the inserted text */
502 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
504 wxWX2MBbuf buf
= text
.mbc_str();
505 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
507 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
510 /* bring editable's cursor uptodate. bug in GTK. */
511 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
513 /* bring entry's cursor uptodate. bug in GTK. */
514 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
518 void wxTextCtrl::AppendText( const wxString
&text
)
520 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
522 if (text
.IsEmpty()) return;
524 if (m_windowStyle
& wxTE_MULTILINE
)
526 bool hasSpecialAttributes
= m_font
.Ok() ||
527 m_foregroundColour
.Ok() ||
528 m_backgroundColour
.Ok();
529 if ( hasSpecialAttributes
)
531 gtk_text_insert( GTK_TEXT(m_text
),
532 m_font
.GetInternalFont(),
533 m_foregroundColour
.GetColor(),
534 m_backgroundColour
.GetColor(),
535 text
.mbc_str(), text
.length());
540 /* we'll insert at the last position */
541 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
543 wxWX2MBbuf buf
= text
.mbc_str();
544 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
546 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
550 /* bring editable's cursor uptodate. bug in GTK. */
551 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
555 gtk_entry_append_text( GTK_ENTRY(m_text
), text
.mbc_str() );
559 wxString
wxTextCtrl::GetLineText( long lineNo
) const
561 if (m_windowStyle
& wxTE_MULTILINE
)
563 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
564 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
568 wxString
buf(wxT(""));
571 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
576 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
583 return wxEmptyString
;
587 if (lineNo
== 0) return GetValue();
588 return wxEmptyString
;
592 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
594 /* If you implement this, don't forget to update the documentation!
595 * (file docs/latex/wx/text.tex) */
596 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
599 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
601 if ( m_windowStyle
& wxTE_MULTILINE
)
603 wxString text
= GetValue();
605 // cast to prevent warning. But pos really should've been unsigned.
606 if( (unsigned long)pos
> text
.Len() )
612 const wxChar
* stop
= text
.c_str() + pos
;
613 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
624 else // single line control
626 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
633 // index out of bounds
641 long wxTextCtrl::XYToPosition(long x
, long y
) const
643 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
646 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
652 int wxTextCtrl::GetLineLength(long lineNo
) const
654 wxString str
= GetLineText (lineNo
);
655 return (int) str
.Length();
658 int wxTextCtrl::GetNumberOfLines() const
660 if (m_windowStyle
& wxTE_MULTILINE
)
662 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
663 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
668 for (int i
= 0; i
< len
; i
++ )
675 // currentLine is 0 based, add 1 to get number of lines
676 return currentLine
+ 1;
689 void wxTextCtrl::SetInsertionPoint( long pos
)
691 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
693 if (m_windowStyle
& wxTE_MULTILINE
)
695 /* seems to be broken in GTK 1.0.X:
696 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
698 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
699 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
701 /* we fake a set_point by inserting and deleting. as the user
702 isn't supposed to get to know about thos non-sense, we
703 disconnect so that no events are sent to the user program. */
705 gint tmp
= (gint
)pos
;
706 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
707 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
709 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
710 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
712 /* bring editable's cursor uptodate. another bug in GTK. */
714 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
718 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
720 /* bring editable's cursor uptodate. bug in GTK. */
722 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
726 void wxTextCtrl::SetInsertionPointEnd()
728 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
730 if (m_windowStyle
& wxTE_MULTILINE
)
731 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
733 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
736 void wxTextCtrl::SetEditable( bool editable
)
738 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
740 if (m_windowStyle
& wxTE_MULTILINE
)
741 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
743 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
746 bool wxTextCtrl::Enable( bool enable
)
748 if (!wxWindowBase::Enable(enable
))
754 if (m_windowStyle
& wxTE_MULTILINE
)
756 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
760 gtk_widget_set_sensitive( m_text
, enable
);
766 void wxTextCtrl::DiscardEdits()
771 void wxTextCtrl::SetSelection( long from
, long to
)
773 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
775 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
776 !GTK_TEXT(m_text
)->line_start_cache
)
778 // tell the programmer that it didn't work
779 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
783 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
786 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
788 // SetInsertionPoint( pos );
791 long wxTextCtrl::GetInsertionPoint() const
793 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
795 return (long) GTK_EDITABLE(m_text
)->current_pos
;
798 long wxTextCtrl::GetLastPosition() const
800 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
803 if (m_windowStyle
& wxTE_MULTILINE
)
804 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
806 pos
= GTK_ENTRY(m_text
)->text_length
;
811 void wxTextCtrl::Remove( long from
, long to
)
813 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
815 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
818 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
820 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
822 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
824 if (!value
.IsEmpty())
826 gint pos
= (gint
)from
;
828 wxWX2MBbuf buf
= value
.mbc_str();
829 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
831 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
836 void wxTextCtrl::Cut()
838 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
840 #if (GTK_MINOR_VERSION > 0)
841 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
843 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
847 void wxTextCtrl::Copy()
849 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
851 #if (GTK_MINOR_VERSION > 0)
852 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
854 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
858 void wxTextCtrl::Paste()
860 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
862 #if (GTK_MINOR_VERSION > 0)
863 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
865 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
869 bool wxTextCtrl::CanCopy() const
871 // Can copy if there's a selection
873 GetSelection(& from
, & to
);
874 return (from
!= to
) ;
877 bool wxTextCtrl::CanCut() const
879 // Can cut if there's a selection
881 GetSelection(& from
, & to
);
882 return (from
!= to
) && (IsEditable());
885 bool wxTextCtrl::CanPaste() const
887 return IsEditable() ;
891 void wxTextCtrl::Undo()
894 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
897 void wxTextCtrl::Redo()
900 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
903 bool wxTextCtrl::CanUndo() const
906 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
910 bool wxTextCtrl::CanRedo() const
913 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
917 // If the return values from and to are the same, there is no
919 void wxTextCtrl::GetSelection(long* from
, long* to
) const
921 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
923 if (!(GTK_EDITABLE(m_text
)->has_selection
))
925 long i
= GetInsertionPoint();
931 if (from
) *from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
932 if (to
) *to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
935 bool wxTextCtrl::IsEditable() const
937 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
939 return GTK_EDITABLE(m_text
)->editable
;
942 bool wxTextCtrl::IsModified() const
947 void wxTextCtrl::Clear()
952 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
954 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
956 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
958 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
959 event
.SetEventObject(this);
960 event
.SetString(GetValue());
961 if (GetEventHandler()->ProcessEvent(event
)) return;
964 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
966 wxWindow
*top_frame
= m_parent
;
967 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
968 top_frame
= top_frame
->GetParent();
969 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
971 if (window
->default_widget
)
973 gtk_widget_activate (window
->default_widget
);
981 GtkWidget
* wxTextCtrl::GetConnectWidget()
983 return GTK_WIDGET(m_text
);
986 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
988 if (m_windowStyle
& wxTE_MULTILINE
)
989 return (window
== GTK_TEXT(m_text
)->text_area
);
991 return (window
== GTK_ENTRY(m_text
)->text_area
);
994 // the font will change for subsequent text insertiongs
995 bool wxTextCtrl::SetFont( const wxFont
&font
)
997 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
999 if ( !wxWindowBase::SetFont(font
) )
1001 // font didn't change, nothing to do
1005 if ( m_windowStyle
& wxTE_MULTILINE
)
1007 m_updateFont
= TRUE
;
1009 ChangeFontGlobally();
1015 void wxTextCtrl::ChangeFontGlobally()
1017 // this method is very inefficient and hence should be called as rarely as
1019 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1020 _T("shouldn't be called for single line controls") );
1022 wxString value
= GetValue();
1023 if ( !value
.IsEmpty() )
1028 m_updateFont
= FALSE
;
1032 void wxTextCtrl::UpdateFontIfNeeded()
1035 ChangeFontGlobally();
1038 bool wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
1040 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1046 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1048 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1050 wxControl::SetBackgroundColour( colour
);
1052 if (!m_widget
->window
)
1055 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1056 if (sysbg
.Red() == colour
.Red() &&
1057 sysbg
.Green() == colour
.Green() &&
1058 sysbg
.Blue() == colour
.Blue())
1060 return FALSE
; // FIXME or TRUE?
1063 if (!m_backgroundColour
.Ok())
1066 if (m_windowStyle
& wxTE_MULTILINE
)
1068 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1071 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1072 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1073 gdk_window_clear( window
);
1079 void wxTextCtrl::ApplyWidgetStyle()
1081 if (m_windowStyle
& wxTE_MULTILINE
)
1088 gtk_widget_set_style( m_text
, m_widgetStyle
);
1092 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1097 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1102 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1107 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1112 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1117 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1119 event
.Enable( CanCut() );
1122 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1124 event
.Enable( CanCopy() );
1127 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1129 event
.Enable( CanPaste() );
1132 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1134 event
.Enable( CanUndo() );
1137 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1139 event
.Enable( CanRedo() );
1142 void wxTextCtrl::OnInternalIdle()
1144 wxCursor cursor
= m_cursor
;
1145 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1149 GdkWindow
*window
= (GdkWindow
*) NULL
;
1150 if (HasFlag(wxTE_MULTILINE
))
1151 window
= GTK_TEXT(m_text
)->text_area
;
1153 window
= GTK_ENTRY(m_text
)->text_area
;
1156 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1158 if (!g_globalCursor
.Ok())
1159 cursor
= *wxSTANDARD_CURSOR
;
1161 window
= m_widget
->window
;
1162 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1163 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1169 wxSize
wxTextCtrl::DoGetBestSize() const
1171 // FIXME should be different for multi-line controls...
1172 wxSize
ret( wxControl::DoGetBestSize() );
1173 return wxSize(80, ret
.y
);