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
);
332 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
333 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
335 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_in_event",
336 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
338 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_out_event",
339 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
343 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_in_event",
344 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
346 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_out_event",
347 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
350 if (!value
.IsEmpty())
354 #if GTK_MINOR_VERSION == 0
355 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
356 // gtk_editable_insert_text()
357 gtk_widget_realize(m_text
);
361 wxWX2MBbuf val
= value
.mbc_str();
362 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
364 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
365 #endif // Unicode/!Unicode
369 /* bring editable's cursor uptodate. bug in GTK. */
371 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
375 if (style
& wxTE_PASSWORD
)
378 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
381 if (style
& wxTE_READONLY
)
384 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
389 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
392 /* we want to be notified about text changes */
393 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
394 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
396 /* we don't set a valid background colour, because the window
397 manager should use a default one */
398 m_backgroundColour
= wxColour();
400 wxColour colFg
= parent
->GetForegroundColour();
401 SetForegroundColour( colFg
);
403 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
405 // FIXME: is the bg colour correct here?
406 wxTextAttr
attrDef( colFg
,
407 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
),
409 SetDefaultStyle( attrDef
);
416 void wxTextCtrl::CalculateScrollbar()
418 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
420 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
422 if (adj
->upper
- adj
->page_size
< 0.8)
424 if (m_vScrollbarVisible
)
426 gtk_widget_hide( m_vScrollbar
);
427 m_vScrollbarVisible
= FALSE
;
432 if (!m_vScrollbarVisible
)
434 gtk_widget_show( m_vScrollbar
);
435 m_vScrollbarVisible
= TRUE
;
440 wxString
wxTextCtrl::GetValue() const
442 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
445 if (m_windowStyle
& wxTE_MULTILINE
)
447 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
448 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
449 tmp
= wxString(text
,*wxConvCurrent
);
454 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
459 void wxTextCtrl::SetValue( const wxString
&value
)
461 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
463 if (m_windowStyle
& wxTE_MULTILINE
)
465 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
466 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
469 wxWX2MBbuf tmpbuf
= value
.mbc_str();
470 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
472 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
477 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
480 // GRG, Jun/2000: Changed this after a lot of discussion in
481 // the lists. wxWindows 2.2 will have a set of flags to
482 // customize this behaviour.
483 SetInsertionPoint(0);
488 void wxTextCtrl::WriteText( const wxString
&text
)
490 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
496 wxWX2MBbuf buf
= text
.mbc_str();
497 const char *txt
= buf
;
498 size_t txtlen
= strlen(buf
);
500 const char *txt
= text
;
501 size_t txtlen
= text
.length();
504 if ( m_windowStyle
& wxTE_MULTILINE
)
506 /* this moves the cursor pos to behind the inserted text */
507 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
509 // if we have any special style, use it
510 if ( !m_defaultStyle
.IsDefault() )
512 GdkFont
*font
= m_defaultStyle
.HasFont()
513 ? m_defaultStyle
.GetFont().GetInternalFont()
516 GdkColor
*colFg
= m_defaultStyle
.HasTextColour()
517 ? m_defaultStyle
.GetTextColour().GetColor()
520 GdkColor
*colBg
= m_defaultStyle
.HasBackgroundColour()
521 ? m_defaultStyle
.GetBackgroundColour().GetColor()
524 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
528 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
531 /* bring editable's cursor uptodate. bug in GTK. */
532 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
536 /* this moves the cursor pos to behind the inserted text */
537 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
538 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
540 /* bring editable's cursor uptodate. bug in GTK. */
541 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
543 /* bring entry's cursor uptodate. bug in GTK. */
544 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
548 void wxTextCtrl::AppendText( const wxString
&text
)
550 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
556 wxWX2MBbuf buf
= text
.mbc_str();
557 const char *txt
= buf
;
558 size_t txtlen
= strlen(buf
);
560 const char *txt
= text
;
561 size_t txtlen
= text
.length();
564 if (m_windowStyle
& wxTE_MULTILINE
)
566 if ( !m_defaultStyle
.IsDefault() )
568 GdkFont
*font
= m_defaultStyle
.HasFont()
569 ? m_defaultStyle
.GetFont().GetInternalFont()
572 GdkColor
*colFg
= m_defaultStyle
.HasTextColour()
573 ? m_defaultStyle
.GetTextColour().GetColor()
576 GdkColor
*colBg
= m_defaultStyle
.HasBackgroundColour()
577 ? m_defaultStyle
.GetBackgroundColour().GetColor()
580 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
584 /* we'll insert at the last position */
585 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
586 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
589 /* bring editable's cursor uptodate. bug in GTK. */
590 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
594 gtk_entry_append_text( GTK_ENTRY(m_text
), txt
);
598 wxString
wxTextCtrl::GetLineText( long lineNo
) const
600 if (m_windowStyle
& wxTE_MULTILINE
)
602 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
603 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
607 wxString
buf(wxT(""));
610 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
615 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
622 return wxEmptyString
;
626 if (lineNo
== 0) return GetValue();
627 return wxEmptyString
;
631 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
633 /* If you implement this, don't forget to update the documentation!
634 * (file docs/latex/wx/text.tex) */
635 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
638 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
640 if ( m_windowStyle
& wxTE_MULTILINE
)
642 wxString text
= GetValue();
644 // cast to prevent warning. But pos really should've been unsigned.
645 if( (unsigned long)pos
> text
.Len() )
651 const wxChar
* stop
= text
.c_str() + pos
;
652 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
663 else // single line control
665 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
672 // index out of bounds
680 long wxTextCtrl::XYToPosition(long x
, long y
) const
682 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
685 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
691 int wxTextCtrl::GetLineLength(long lineNo
) const
693 wxString str
= GetLineText (lineNo
);
694 return (int) str
.Length();
697 int wxTextCtrl::GetNumberOfLines() const
699 if (m_windowStyle
& wxTE_MULTILINE
)
701 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
702 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
707 for (int i
= 0; i
< len
; i
++ )
714 // currentLine is 0 based, add 1 to get number of lines
715 return currentLine
+ 1;
728 void wxTextCtrl::SetInsertionPoint( long pos
)
730 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
732 if (m_windowStyle
& wxTE_MULTILINE
)
734 /* seems to be broken in GTK 1.0.X:
735 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
737 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
738 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
740 /* we fake a set_point by inserting and deleting. as the user
741 isn't supposed to get to know about thos non-sense, we
742 disconnect so that no events are sent to the user program. */
744 gint tmp
= (gint
)pos
;
745 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
746 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
748 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
749 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
751 /* bring editable's cursor uptodate. another bug in GTK. */
753 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
757 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
759 /* bring editable's cursor uptodate. bug in GTK. */
761 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
765 void wxTextCtrl::SetInsertionPointEnd()
767 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
769 if (m_windowStyle
& wxTE_MULTILINE
)
770 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
772 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
775 void wxTextCtrl::SetEditable( bool editable
)
777 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
779 if (m_windowStyle
& wxTE_MULTILINE
)
780 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
782 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
785 bool wxTextCtrl::Enable( bool enable
)
787 if (!wxWindowBase::Enable(enable
))
793 if (m_windowStyle
& wxTE_MULTILINE
)
795 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
796 OnParentEnable(enable
);
800 gtk_widget_set_sensitive( m_text
, enable
);
806 // wxGTK-specific: called recursively by Enable,
807 // to give widgets an oppprtunity to correct their colours after they
808 // have been changed by Enable
809 void wxTextCtrl::OnParentEnable( bool enable
)
811 // If we have a custom background colour, we use this colour in both
812 // disabled and enabled mode, or we end up with a different colour under the
814 wxColour oldColour
= GetBackgroundColour();
817 // Need to set twice or it'll optimize the useful stuff out
818 if (oldColour
== * wxWHITE
)
819 SetBackgroundColour(*wxBLACK
);
821 SetBackgroundColour(*wxWHITE
);
822 SetBackgroundColour(oldColour
);
826 void wxTextCtrl::DiscardEdits()
831 void wxTextCtrl::SetSelection( long from
, long to
)
833 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
835 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
836 !GTK_TEXT(m_text
)->line_start_cache
)
838 // tell the programmer that it didn't work
839 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
843 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
846 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
848 // SetInsertionPoint( pos );
851 long wxTextCtrl::GetInsertionPoint() const
853 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
855 return (long) GTK_EDITABLE(m_text
)->current_pos
;
858 long wxTextCtrl::GetLastPosition() const
860 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
863 if (m_windowStyle
& wxTE_MULTILINE
)
864 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
866 pos
= GTK_ENTRY(m_text
)->text_length
;
871 void wxTextCtrl::Remove( long from
, long to
)
873 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
875 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
878 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
880 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
882 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
884 if (!value
.IsEmpty())
886 gint pos
= (gint
)from
;
888 wxWX2MBbuf buf
= value
.mbc_str();
889 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
891 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
896 void wxTextCtrl::Cut()
898 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
900 #if (GTK_MINOR_VERSION > 0)
901 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
903 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
907 void wxTextCtrl::Copy()
909 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
911 #if (GTK_MINOR_VERSION > 0)
912 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
914 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
918 void wxTextCtrl::Paste()
920 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
922 #if (GTK_MINOR_VERSION > 0)
923 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
925 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
930 void wxTextCtrl::Undo()
933 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
936 void wxTextCtrl::Redo()
939 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
942 bool wxTextCtrl::CanUndo() const
945 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
949 bool wxTextCtrl::CanRedo() const
952 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
956 // If the return values from and to are the same, there is no
958 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
960 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
963 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
966 to
= GetInsertionPoint();
968 else // got selection
970 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
971 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
975 // exchange them to be compatible with wxMSW
988 bool wxTextCtrl::IsEditable() const
990 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
992 return GTK_EDITABLE(m_text
)->editable
;
995 bool wxTextCtrl::IsModified() const
1000 void wxTextCtrl::Clear()
1002 SetValue( wxT("") );
1005 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1007 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1009 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1011 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1012 event
.SetEventObject(this);
1013 event
.SetString(GetValue());
1014 if (GetEventHandler()->ProcessEvent(event
)) return;
1017 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1019 wxWindow
*top_frame
= m_parent
;
1020 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1021 top_frame
= top_frame
->GetParent();
1022 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1024 if (window
->default_widget
)
1026 gtk_widget_activate (window
->default_widget
);
1034 GtkWidget
* wxTextCtrl::GetConnectWidget()
1036 return GTK_WIDGET(m_text
);
1039 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1041 if (m_windowStyle
& wxTE_MULTILINE
)
1042 return (window
== GTK_TEXT(m_text
)->text_area
);
1044 return (window
== GTK_ENTRY(m_text
)->text_area
);
1047 // the font will change for subsequent text insertiongs
1048 bool wxTextCtrl::SetFont( const wxFont
&font
)
1050 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1052 if ( !wxWindowBase::SetFont(font
) )
1054 // font didn't change, nothing to do
1058 if ( m_windowStyle
& wxTE_MULTILINE
)
1060 m_updateFont
= TRUE
;
1062 ChangeFontGlobally();
1068 void wxTextCtrl::ChangeFontGlobally()
1070 // this method is very inefficient and hence should be called as rarely as
1072 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1073 _T("shouldn't be called for single line controls") );
1075 wxString value
= GetValue();
1076 if ( !value
.IsEmpty() )
1081 m_updateFont
= FALSE
;
1085 void wxTextCtrl::UpdateFontIfNeeded()
1088 ChangeFontGlobally();
1091 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1093 if ( !wxControl::SetForegroundColour(colour
) )
1096 // update default fg colour too
1097 m_defaultStyle
.SetTextColour(colour
);
1102 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1104 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1106 wxControl::SetBackgroundColour( colour
);
1108 if (!m_widget
->window
)
1111 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1112 if (sysbg
.Red() == colour
.Red() &&
1113 sysbg
.Green() == colour
.Green() &&
1114 sysbg
.Blue() == colour
.Blue())
1116 return FALSE
; // FIXME or TRUE?
1119 if (!m_backgroundColour
.Ok())
1122 if (m_windowStyle
& wxTE_MULTILINE
)
1124 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1127 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1128 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1129 gdk_window_clear( window
);
1132 // change active background color too
1133 m_defaultStyle
.SetBackgroundColour( colour
);
1138 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
&style
)
1140 /* VERY dirty way to do that - removes the required text and re-adds it
1141 with styling (FIXME) */
1142 if ( m_windowStyle
& wxTE_MULTILINE
)
1144 if ( style
.IsDefault() )
1150 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1152 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1153 _T("invalid range in wxTextCtrl::SetStyle") );
1155 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1156 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1157 wxString
tmp(text
,*wxConvCurrent
);
1160 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1161 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1164 wxWX2MBbuf buf
= tmp
.mbc_str();
1165 const char *txt
= buf
;
1166 size_t txtlen
= strlen(buf
);
1168 const char *txt
= tmp
;
1169 size_t txtlen
= tmp
.length();
1172 GdkFont
*font
= style
.HasFont()
1173 ? style
.GetFont().GetInternalFont()
1176 GdkColor
*colFg
= style
.HasTextColour()
1177 ? style
.GetTextColour().GetColor()
1180 GdkColor
*colBg
= style
.HasBackgroundColour()
1181 ? style
.GetBackgroundColour().GetColor()
1184 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
1186 /* does not seem to help under GTK+ 1.2 !!!
1187 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1188 SetInsertionPoint( old_pos
);
1193 // cannot do this for GTK+'s Entry widget
1198 void wxTextCtrl::ApplyWidgetStyle()
1200 if (m_windowStyle
& wxTE_MULTILINE
)
1207 gtk_widget_set_style( m_text
, m_widgetStyle
);
1211 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1216 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1221 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1226 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1231 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1236 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1238 event
.Enable( CanCut() );
1241 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1243 event
.Enable( CanCopy() );
1246 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1248 event
.Enable( CanPaste() );
1251 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1253 event
.Enable( CanUndo() );
1256 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1258 event
.Enable( CanRedo() );
1261 void wxTextCtrl::OnInternalIdle()
1263 wxCursor cursor
= m_cursor
;
1264 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1268 GdkWindow
*window
= (GdkWindow
*) NULL
;
1269 if (HasFlag(wxTE_MULTILINE
))
1270 window
= GTK_TEXT(m_text
)->text_area
;
1272 window
= GTK_ENTRY(m_text
)->text_area
;
1275 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1277 if (!g_globalCursor
.Ok())
1278 cursor
= *wxSTANDARD_CURSOR
;
1280 window
= m_widget
->window
;
1281 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1282 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1288 wxSize
wxTextCtrl::DoGetBestSize() const
1290 // FIXME should be different for multi-line controls...
1291 wxSize
ret( wxControl::DoGetBestSize() );
1292 return wxSize(80, ret
.y
);