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>
24 #include <math.h> // for fabs
28 #include "gdk/gdkkeysyms.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern bool g_blockEventsOnDrag
;
42 extern wxCursor g_globalCursor
;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
49 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
51 if (!win
->m_hasVMT
) return;
54 wxapp_install_idle_handler();
57 win
->UpdateFontIfNeeded();
59 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
60 event
.SetString( win
->GetValue() );
61 event
.SetEventObject( win
);
62 win
->GetEventHandler()->ProcessEvent( event
);
65 //-----------------------------------------------------------------------------
66 // "changed" from vertical scrollbar
67 //-----------------------------------------------------------------------------
70 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
72 if (!win
->m_hasVMT
) return;
75 wxapp_install_idle_handler();
77 win
->CalculateScrollbar();
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
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
))
133 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
137 static gint
gtk_text_focus_out_callback( GtkWidget
*widget
, GdkEvent
*WXUNUSED(event
), wxWindow
*win
)
141 wxapp_install_idle_handler();
144 if (!win
->m_hasVMT
) return FALSE
;
145 if (g_blockEventsOnDrag
) return FALSE
;
148 // if the focus goes out of our app alltogether, OnIdle() will send
149 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
150 // g_sendActivateEvent to -1
151 g_sendActivateEvent
= 0;
154 wxWindow
*winFocus
= wxFindFocusedChild(win
);
158 g_focusWindow
= (wxWindow
*)NULL
;
167 // caret needs to be informed about focus change
168 wxCaret
*caret
= win
->GetCaret();
171 caret
->OnKillFocus();
173 #endif // wxUSE_CARET
176 wxFocusEvent
event( wxEVT_KILL_FOCUS
, win
->GetId() );
177 event
.SetEventObject( win
);
179 if (win
->GetEventHandler()->ProcessEvent( event
))
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
193 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
194 EVT_CHAR(wxTextCtrl::OnChar
)
196 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
197 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
198 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
199 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
200 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
202 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
203 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
204 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
205 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
206 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
209 void wxTextCtrl::Init()
212 m_updateFont
= FALSE
;
214 m_vScrollbar
= (GtkWidget
*)NULL
;
217 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
219 const wxString
&value
,
223 const wxValidator
& validator
,
224 const wxString
&name
)
228 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
231 bool wxTextCtrl::Create( wxWindow
*parent
,
233 const wxString
&value
,
237 const wxValidator
& validator
,
238 const wxString
&name
)
241 m_acceptsFocus
= TRUE
;
243 if (!PreCreation( parent
, pos
, size
) ||
244 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
246 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
251 m_vScrollbarVisible
= FALSE
;
253 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
256 #if (GTK_MINOR_VERSION > 2)
257 /* a multi-line edit control: create a vertical scrollbar by default and
258 horizontal if requested */
259 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
261 bool bHasHScrollbar
= FALSE
;
264 /* create our control ... */
265 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
267 /* ... and put into the upper left hand corner of the table */
268 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
269 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
270 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
271 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
272 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
275 /* always wrap words */
276 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
278 #if (GTK_MINOR_VERSION > 2)
279 /* put the horizontal scrollbar in the lower left hand corner */
282 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
283 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
284 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
285 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
288 gtk_widget_show(hscrollbar
);
290 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
291 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
295 /* finally, put the vertical scrollbar in the upper right corner */
296 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
297 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
298 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
300 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
305 /* a single-line text control: no need for scrollbars */
307 m_text
= gtk_entry_new();
310 m_parent
->DoAddChild( this );
314 SetFont( parent
->GetFont() );
316 wxSize
size_best( DoGetBestSize() );
317 wxSize
new_size( size
);
318 if (new_size
.x
== -1)
319 new_size
.x
= size_best
.x
;
320 if (new_size
.y
== -1)
321 new_size
.y
= size_best
.y
;
322 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
323 SetSize( new_size
.x
, new_size
.y
);
326 gtk_widget_show(m_text
);
330 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
331 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
333 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_in_event",
334 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
336 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text
)), "focus_out_event",
337 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
341 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_in_event",
342 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback
), (gpointer
)this );
344 gtk_signal_connect( GTK_OBJECT(m_text
), "focus_out_event",
345 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback
), (gpointer
)this );
348 if (!value
.IsEmpty())
352 #if GTK_MINOR_VERSION == 0
353 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
354 // gtk_editable_insert_text()
355 gtk_widget_realize(m_text
);
359 wxWX2MBbuf val
= value
.mbc_str();
360 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
362 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
363 #endif // Unicode/!Unicode
367 /* bring editable's cursor uptodate. bug in GTK. */
369 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
373 if (style
& wxTE_PASSWORD
)
376 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
379 if (style
& wxTE_READONLY
)
382 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
387 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
390 /* we want to be notified about text changes */
391 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
392 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
394 /* we don't set a valid background colour, because the window
395 manager should use a default one */
396 m_backgroundColour
= wxColour();
398 wxColour colFg
= parent
->GetForegroundColour();
399 SetForegroundColour( colFg
);
401 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
403 // FIXME: is the bg colour correct here?
404 wxTextAttr
attrDef( colFg
,
405 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
),
407 SetDefaultStyle( attrDef
);
414 void wxTextCtrl::CalculateScrollbar()
416 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
418 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
420 if (adj
->upper
- adj
->page_size
< 0.8)
422 if (m_vScrollbarVisible
)
424 gtk_widget_hide( m_vScrollbar
);
425 m_vScrollbarVisible
= FALSE
;
430 if (!m_vScrollbarVisible
)
432 gtk_widget_show( m_vScrollbar
);
433 m_vScrollbarVisible
= TRUE
;
438 wxString
wxTextCtrl::GetValue() const
440 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
443 if (m_windowStyle
& wxTE_MULTILINE
)
445 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
446 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
447 tmp
= wxString(text
,*wxConvCurrent
);
452 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
457 void wxTextCtrl::SetValue( const wxString
&value
)
459 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
461 if (m_windowStyle
& wxTE_MULTILINE
)
463 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
464 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
467 wxWX2MBbuf tmpbuf
= value
.mbc_str();
468 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
470 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
475 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
478 // GRG, Jun/2000: Changed this after a lot of discussion in
479 // the lists. wxWindows 2.2 will have a set of flags to
480 // customize this behaviour.
481 SetInsertionPoint(0);
486 void wxTextCtrl::WriteText( const wxString
&text
)
488 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
494 wxWX2MBbuf buf
= text
.mbc_str();
495 const char *txt
= buf
;
496 size_t txtlen
= strlen(buf
);
498 const char *txt
= text
;
499 size_t txtlen
= text
.length();
502 if ( m_windowStyle
& wxTE_MULTILINE
)
504 /* this moves the cursor pos to behind the inserted text */
505 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
507 // if we have any special style, use it
508 if ( !m_defaultStyle
.IsDefault() )
510 GdkFont
*font
= m_defaultStyle
.HasFont()
511 ? m_defaultStyle
.GetFont().GetInternalFont()
514 GdkColor
*colFg
= m_defaultStyle
.HasTextColour()
515 ? m_defaultStyle
.GetTextColour().GetColor()
518 GdkColor
*colBg
= m_defaultStyle
.HasBackgroundColour()
519 ? m_defaultStyle
.GetBackgroundColour().GetColor()
522 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
526 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
529 /* bring editable's cursor uptodate. bug in GTK. */
530 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
534 /* this moves the cursor pos to behind the inserted text */
535 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
536 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
538 /* bring editable's cursor uptodate. bug in GTK. */
539 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
541 /* bring entry's cursor uptodate. bug in GTK. */
542 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
546 void wxTextCtrl::AppendText( const wxString
&text
)
548 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
554 wxWX2MBbuf buf
= text
.mbc_str();
555 const char *txt
= buf
;
556 size_t txtlen
= strlen(buf
);
558 const char *txt
= text
;
559 size_t txtlen
= text
.length();
562 if (m_windowStyle
& wxTE_MULTILINE
)
564 if ( !m_defaultStyle
.IsDefault() )
566 wxFont font
= m_defaultStyle
.HasFont() ? m_defaultStyle
.GetFont()
568 GdkFont
*fnt
= font
.Ok() ? font
.GetInternalFont() : NULL
;
570 wxColour col
= m_defaultStyle
.HasTextColour()
571 ? m_defaultStyle
.GetTextColour()
572 : m_foregroundColour
;
573 GdkColor
*colFg
= col
.Ok() ? col
.GetColor() : NULL
;
575 col
= m_defaultStyle
.HasBackgroundColour()
576 ? m_defaultStyle
.GetBackgroundColour()
577 : m_backgroundColour
;
578 GdkColor
*colBg
= col
.Ok() ? col
.GetColor() : NULL
;
580 gtk_text_insert( GTK_TEXT(m_text
), fnt
, 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 m_defaultStyle
.SetFont(font
);
1064 ChangeFontGlobally();
1070 void wxTextCtrl::ChangeFontGlobally()
1072 // this method is very inefficient and hence should be called as rarely as
1074 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1075 _T("shouldn't be called for single line controls") );
1077 wxString value
= GetValue();
1078 if ( !value
.IsEmpty() )
1080 m_updateFont
= FALSE
;
1087 void wxTextCtrl::UpdateFontIfNeeded()
1090 ChangeFontGlobally();
1093 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1095 if ( !wxControl::SetForegroundColour(colour
) )
1098 // update default fg colour too
1099 m_defaultStyle
.SetTextColour(colour
);
1104 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1106 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1108 if ( !wxControl::SetBackgroundColour( colour
) )
1111 if (!m_widget
->window
)
1114 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1115 if (sysbg
.Red() == colour
.Red() &&
1116 sysbg
.Green() == colour
.Green() &&
1117 sysbg
.Blue() == colour
.Blue())
1119 return FALSE
; // FIXME or TRUE?
1122 if (!m_backgroundColour
.Ok())
1125 if (m_windowStyle
& wxTE_MULTILINE
)
1127 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1130 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1131 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1132 gdk_window_clear( window
);
1135 // change active background color too
1136 m_defaultStyle
.SetBackgroundColour( colour
);
1141 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
&style
)
1143 /* VERY dirty way to do that - removes the required text and re-adds it
1144 with styling (FIXME) */
1145 if ( m_windowStyle
& wxTE_MULTILINE
)
1147 if ( style
.IsDefault() )
1153 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1155 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1156 _T("invalid range in wxTextCtrl::SetStyle") );
1158 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1159 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1160 wxString
tmp(text
,*wxConvCurrent
);
1163 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1164 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1167 wxWX2MBbuf buf
= tmp
.mbc_str();
1168 const char *txt
= buf
;
1169 size_t txtlen
= strlen(buf
);
1171 const char *txt
= tmp
;
1172 size_t txtlen
= tmp
.length();
1175 GdkFont
*font
= style
.HasFont()
1176 ? style
.GetFont().GetInternalFont()
1179 GdkColor
*colFg
= style
.HasTextColour()
1180 ? style
.GetTextColour().GetColor()
1183 GdkColor
*colBg
= style
.HasBackgroundColour()
1184 ? style
.GetBackgroundColour().GetColor()
1187 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
1189 /* does not seem to help under GTK+ 1.2 !!!
1190 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1191 SetInsertionPoint( old_pos
);
1196 // cannot do this for GTK+'s Entry widget
1201 void wxTextCtrl::ApplyWidgetStyle()
1203 if (m_windowStyle
& wxTE_MULTILINE
)
1210 gtk_widget_set_style( m_text
, m_widgetStyle
);
1214 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1219 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1224 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1229 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1234 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1239 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1241 event
.Enable( CanCut() );
1244 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1246 event
.Enable( CanCopy() );
1249 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1251 event
.Enable( CanPaste() );
1254 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1256 event
.Enable( CanUndo() );
1259 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1261 event
.Enable( CanRedo() );
1264 void wxTextCtrl::OnInternalIdle()
1266 wxCursor cursor
= m_cursor
;
1267 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1271 GdkWindow
*window
= (GdkWindow
*) NULL
;
1272 if (HasFlag(wxTE_MULTILINE
))
1273 window
= GTK_TEXT(m_text
)->text_area
;
1275 window
= GTK_ENTRY(m_text
)->text_area
;
1278 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1280 if (!g_globalCursor
.Ok())
1281 cursor
= *wxSTANDARD_CURSOR
;
1283 window
= m_widget
->window
;
1284 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1285 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1291 wxSize
wxTextCtrl::DoGetBestSize() const
1293 // FIXME should be different for multi-line controls...
1294 wxSize
ret( wxControl::DoGetBestSize() );
1295 return wxSize(80, ret
.y
);
1298 // ----------------------------------------------------------------------------
1300 // ----------------------------------------------------------------------------
1302 void wxTextCtrl::Freeze()
1304 if ( HasFlag(wxTE_MULTILINE
) )
1306 gtk_text_freeze(GTK_TEXT(m_text
));
1310 void wxTextCtrl::Thaw()
1312 if ( HasFlag(wxTE_MULTILINE
) )
1314 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1316 gtk_text_thaw(GTK_TEXT(m_text
));
1320 // ----------------------------------------------------------------------------
1322 // ----------------------------------------------------------------------------
1324 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1326 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1329 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1331 float value
= adj
->value
+ diff
;
1336 float upper
= adj
->upper
- adj
->page_size
;
1337 if ( value
> upper
)
1340 // did we noticeably change the scroll position?
1341 if ( fabs(adj
->value
- value
) < 0.2 )
1343 // well, this is what Robert does in wxScrollBar, so it must be good...
1349 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1354 bool wxTextCtrl::ScrollLines(int lines
)
1356 GtkAdjustment
*adj
= GetVAdj();
1360 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1361 static const int KEY_SCROLL_PIXELS
= 10;
1363 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1366 bool wxTextCtrl::ScrollPages(int pages
)
1368 GtkAdjustment
*adj
= GetVAdj();
1372 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));