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
26 // TODO: reimplement wxTextCtrl using GtkTextView
28 #define GTK_ENABLE_BROKEN // need this to get GtkText at all
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern void wxapp_install_idle_handler();
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern bool g_blockEventsOnDrag
;
46 extern wxCursor g_globalCursor
;
47 extern wxWindowGTK
*g_delayedFocus
;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 static void wxGtkTextInsert(GtkWidget
*text
,
54 const wxTextAttr
& attr
,
58 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
61 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
64 GdkColor
*colBg
= attr
.HasBackgroundColour()
65 ? attr
.GetBackgroundColour().GetColor()
68 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
71 // ----------------------------------------------------------------------------
72 // "insert_text" for GtkEntry
73 // ----------------------------------------------------------------------------
76 gtk_insert_text_callback(GtkEditable
*editable
,
77 const gchar
*new_text
,
83 wxapp_install_idle_handler();
85 // we should only be called if we have a max len limit at all
86 GtkEntry
*entry
= GTK_ENTRY (editable
);
88 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
90 // check that we don't overflow the max length limit
92 // FIXME: this doesn't work when we paste a string which is going to be
94 if ( entry
->text_length
== entry
->text_max_length
)
96 // we don't need to run the base class version at all
97 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
99 // remember that the next changed signal is to be ignored to avoid
100 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
101 win
->IgnoreNextTextUpdate();
103 // and generate the correct one ourselves
104 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
105 event
.SetEventObject(win
);
106 event
.SetString(win
->GetValue());
107 win
->GetEventHandler()->ProcessEvent( event
);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
116 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
118 if ( win
->IgnoreTextUpdate() )
121 if (!win
->m_hasVMT
) return;
124 wxapp_install_idle_handler();
127 win
->UpdateFontIfNeeded();
129 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
130 event
.SetEventObject( win
);
131 event
.SetString( win
->GetValue() );
132 win
->GetEventHandler()->ProcessEvent( event
);
135 //-----------------------------------------------------------------------------
136 // "changed" from vertical scrollbar
137 //-----------------------------------------------------------------------------
140 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
142 if (!win
->m_hasVMT
) return;
145 wxapp_install_idle_handler();
147 win
->CalculateScrollbar();
150 // ----------------------------------------------------------------------------
151 // redraw callback for multiline text
152 // ----------------------------------------------------------------------------
156 // redrawing a GtkText from inside a wxYield() call results in crashes (the
157 // text sample shows it in its "Add lines" command which shows wxProgressDialog
158 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
159 // don't do anything if we're inside wxYield()
161 extern bool wxIsInsideYield
;
163 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
165 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
168 void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
170 if ( !wxIsInsideYield
)
172 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
173 _T("infinite recursion in wxgtk_text_draw aborted") );
175 gs_gtk_text_draw(widget
, rect
);
179 #endif // __WXGTK20__
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
185 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
187 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
188 EVT_CHAR(wxTextCtrl::OnChar
)
190 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
191 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
192 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
193 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
194 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
196 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
197 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
198 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
199 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
200 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
203 void wxTextCtrl::Init()
207 m_updateFont
= FALSE
;
209 m_vScrollbar
= (GtkWidget
*)NULL
;
212 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
214 const wxString
&value
,
218 const wxValidator
& validator
,
219 const wxString
&name
)
223 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
226 bool wxTextCtrl::Create( wxWindow
*parent
,
228 const wxString
&value
,
232 const wxValidator
& validator
,
233 const wxString
&name
)
236 m_acceptsFocus
= TRUE
;
238 if (!PreCreation( parent
, pos
, size
) ||
239 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
241 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
246 m_vScrollbarVisible
= FALSE
;
248 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
252 /* a multi-line edit control: create a vertical scrollbar by default and
253 horizontal if requested */
254 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
256 bool bHasHScrollbar
= FALSE
;
259 /* create our control ... */
260 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
262 /* ... and put into the upper left hand corner of the table */
263 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
264 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
265 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
266 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
267 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
270 /* always wrap words */
271 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
274 /* put the horizontal scrollbar in the lower left hand corner */
277 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
278 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
279 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
280 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
283 gtk_widget_show(hscrollbar
);
285 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
286 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
290 /* finally, put the vertical scrollbar in the upper right corner */
291 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
292 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
293 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
295 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
300 /* a single-line text control: no need for scrollbars */
302 m_text
= gtk_entry_new();
305 m_parent
->DoAddChild( this );
307 m_focusWidget
= m_text
;
311 SetFont( parent
->GetFont() );
313 wxSize
size_best( DoGetBestSize() );
314 wxSize
new_size( size
);
315 if (new_size
.x
== -1)
316 new_size
.x
= size_best
.x
;
317 if (new_size
.y
== -1)
318 new_size
.y
= size_best
.y
;
319 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
320 SetSize( new_size
.x
, new_size
.y
);
323 gtk_widget_show(m_text
);
327 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
328 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
331 // only initialize gs_gtk_text_draw once, starting from the next the
332 // klass::draw will already be wxgtk_text_draw
333 if ( !gs_gtk_text_draw
)
336 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
338 gs_gtk_text_draw
= draw
;
340 draw
= wxgtk_text_draw
;
345 if (!value
.IsEmpty())
349 #if !GTK_CHECK_VERSION(1, 2, 0)
350 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
351 // gtk_editable_insert_text()
352 gtk_widget_realize(m_text
);
356 wxWX2MBbuf val
= value
.mbc_str();
357 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
359 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
360 #endif // Unicode/!Unicode
364 /* bring editable's cursor uptodate. bug in GTK. */
365 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
369 if (style
& wxTE_PASSWORD
)
372 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
375 if (style
& wxTE_READONLY
)
378 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
383 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
386 /* we want to be notified about text changes */
387 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
388 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
390 /* we don't set a valid background colour, because the window
391 manager should use a default one */
392 m_backgroundColour
= wxColour();
394 wxColour colFg
= parent
->GetForegroundColour();
395 SetForegroundColour( colFg
);
397 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
399 wxTextAttr
attrDef( colFg
, m_backgroundColour
, parent
->GetFont() );
400 SetDefaultStyle( attrDef
);
407 void wxTextCtrl::CalculateScrollbar()
409 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
411 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
413 if (adj
->upper
- adj
->page_size
< 0.8)
415 if (m_vScrollbarVisible
)
417 gtk_widget_hide( m_vScrollbar
);
418 m_vScrollbarVisible
= FALSE
;
423 if (!m_vScrollbarVisible
)
425 gtk_widget_show( m_vScrollbar
);
426 m_vScrollbarVisible
= TRUE
;
431 wxString
wxTextCtrl::GetValue() const
433 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
436 if (m_windowStyle
& wxTE_MULTILINE
)
438 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
439 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
440 tmp
= wxString(text
,*wxConvCurrent
);
445 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
450 void wxTextCtrl::SetValue( const wxString
&value
)
452 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
454 if (m_windowStyle
& wxTE_MULTILINE
)
456 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
457 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
460 wxWX2MBbuf tmpbuf
= value
.mbc_str();
461 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
463 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
468 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
471 // GRG, Jun/2000: Changed this after a lot of discussion in
472 // the lists. wxWindows 2.2 will have a set of flags to
473 // customize this behaviour.
474 SetInsertionPoint(0);
479 void wxTextCtrl::WriteText( const wxString
&text
)
481 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
487 wxWX2MBbuf buf
= text
.mbc_str();
488 const char *txt
= buf
;
489 size_t txtlen
= strlen(buf
);
491 const char *txt
= text
;
492 size_t txtlen
= text
.length();
495 if ( m_windowStyle
& wxTE_MULTILINE
)
497 // After cursor movements, gtk_text_get_point() is wrong by one.
498 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
500 // always use m_defaultStyle, even if it is empty as otherwise
501 // resetting the style and appending some more text wouldn't work: if
502 // we don't specify the style explicitly, the old style would be used
503 wxGtkTextInsert(m_text
, m_defaultStyle
, txt
, txtlen
);
505 // Bring editable's cursor back uptodate.
506 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
510 // This moves the cursor pos to behind the inserted text.
511 gint len
= GET_EDITABLE_POS(m_text
);
512 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
514 // Bring editable's cursor uptodate.
517 // Bring entry's cursor uptodate.
518 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
524 void wxTextCtrl::AppendText( const wxString
&text
)
526 SetInsertionPointEnd();
530 wxString
wxTextCtrl::GetLineText( long lineNo
) const
532 if (m_windowStyle
& wxTE_MULTILINE
)
534 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
535 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
539 wxString
buf(wxT(""));
542 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
547 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
555 return wxEmptyString
;
560 if (lineNo
== 0) return GetValue();
561 return wxEmptyString
;
565 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
567 /* If you implement this, don't forget to update the documentation!
568 * (file docs/latex/wx/text.tex) */
569 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
572 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
574 if ( m_windowStyle
& wxTE_MULTILINE
)
576 wxString text
= GetValue();
578 // cast to prevent warning. But pos really should've been unsigned.
579 if( (unsigned long)pos
> text
.Len() )
585 const wxChar
* stop
= text
.c_str() + pos
;
586 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
597 else // single line control
599 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
606 // index out of bounds
614 long wxTextCtrl::XYToPosition(long x
, long y
) const
616 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
619 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
625 int wxTextCtrl::GetLineLength(long lineNo
) const
627 wxString str
= GetLineText (lineNo
);
628 return (int) str
.Length();
631 int wxTextCtrl::GetNumberOfLines() const
633 if (m_windowStyle
& wxTE_MULTILINE
)
635 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
636 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
641 for (int i
= 0; i
< len
; i
++ )
648 // currentLine is 0 based, add 1 to get number of lines
649 return currentLine
+ 1;
662 void wxTextCtrl::SetInsertionPoint( long pos
)
664 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
666 if (m_windowStyle
& wxTE_MULTILINE
)
668 /* seems to be broken in GTK 1.0.X:
669 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
671 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
672 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
674 /* we fake a set_point by inserting and deleting. as the user
675 isn't supposed to get to know about thos non-sense, we
676 disconnect so that no events are sent to the user program. */
678 gint tmp
= (gint
)pos
;
679 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
680 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
682 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
683 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
685 /* bring editable's cursor uptodate. another bug in GTK. */
687 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
691 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
693 /* bring editable's cursor uptodate. bug in GTK. */
695 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
699 void wxTextCtrl::SetInsertionPointEnd()
701 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
703 if (m_windowStyle
& wxTE_MULTILINE
)
704 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
706 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
709 void wxTextCtrl::SetEditable( bool editable
)
711 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
713 if (m_windowStyle
& wxTE_MULTILINE
)
714 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
716 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
719 bool wxTextCtrl::Enable( bool enable
)
721 if (!wxWindowBase::Enable(enable
))
727 if (m_windowStyle
& wxTE_MULTILINE
)
729 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
730 OnParentEnable(enable
);
734 gtk_widget_set_sensitive( m_text
, enable
);
740 // wxGTK-specific: called recursively by Enable,
741 // to give widgets an oppprtunity to correct their colours after they
742 // have been changed by Enable
743 void wxTextCtrl::OnParentEnable( bool enable
)
745 // If we have a custom background colour, we use this colour in both
746 // disabled and enabled mode, or we end up with a different colour under the
748 wxColour oldColour
= GetBackgroundColour();
751 // Need to set twice or it'll optimize the useful stuff out
752 if (oldColour
== * wxWHITE
)
753 SetBackgroundColour(*wxBLACK
);
755 SetBackgroundColour(*wxWHITE
);
756 SetBackgroundColour(oldColour
);
760 void wxTextCtrl::DiscardEdits()
765 // ----------------------------------------------------------------------------
766 // max text length support
767 // ----------------------------------------------------------------------------
769 void wxTextCtrl::IgnoreNextTextUpdate()
771 m_ignoreNextUpdate
= TRUE
;
774 bool wxTextCtrl::IgnoreTextUpdate()
776 if ( m_ignoreNextUpdate
)
778 m_ignoreNextUpdate
= FALSE
;
786 void wxTextCtrl::SetMaxLength(unsigned long len
)
788 if ( !HasFlag(wxTE_MULTILINE
) )
790 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
792 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
793 // we had tried to enter more text than allowed by max text length and
794 // the text wasn't really changed
796 // to detect this and generate TEXT_MAXLEN event instead of
797 // TEXT_CHANGED one in this case we also catch "insert_text" signal
799 // when max len is set to 0 we disconnect our handler as it means that
800 // we shouldn't check anything any more
803 gtk_signal_connect( GTK_OBJECT(m_text
),
805 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
810 gtk_signal_disconnect_by_func
813 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
820 void wxTextCtrl::SetSelection( long from
, long to
)
822 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
824 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
825 !GTK_TEXT(m_text
)->line_start_cache
)
827 // tell the programmer that it didn't work
828 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
832 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
835 void wxTextCtrl::ShowPosition( long pos
)
837 if (m_windowStyle
& wxTE_MULTILINE
)
839 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
840 float totalLines
= (float) GetNumberOfLines();
843 PositionToXY(pos
, &posX
, &posY
);
844 float posLine
= (float) posY
;
845 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
846 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
850 long wxTextCtrl::GetInsertionPoint() const
852 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
854 return (long) GET_EDITABLE_POS(m_text
);
857 long wxTextCtrl::GetLastPosition() const
859 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
862 if (m_windowStyle
& wxTE_MULTILINE
)
863 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
865 pos
= GTK_ENTRY(m_text
)->text_length
;
870 void wxTextCtrl::Remove( long from
, long to
)
872 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
874 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
877 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
879 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
881 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
883 if (!value
.IsEmpty())
885 gint pos
= (gint
)from
;
887 wxWX2MBbuf buf
= value
.mbc_str();
888 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
890 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
895 void wxTextCtrl::Cut()
897 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
899 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
902 void wxTextCtrl::Copy()
904 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
906 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
909 void wxTextCtrl::Paste()
911 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
913 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
917 void wxTextCtrl::Undo()
920 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
923 void wxTextCtrl::Redo()
926 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
929 bool wxTextCtrl::CanUndo() const
932 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
936 bool wxTextCtrl::CanRedo() const
939 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
943 // If the return values from and to are the same, there is no
945 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
947 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
951 if ( !gtk_editable_get_selection_bounds(GTK_EDITABLE(m_text
), &from
, &to
) )
953 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
957 to
= GetInsertionPoint();
959 else // got selection
962 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
963 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
968 // exchange them to be compatible with wxMSW
981 bool wxTextCtrl::IsEditable() const
983 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
986 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
988 return GTK_EDITABLE(m_text
)->editable
;
992 bool wxTextCtrl::IsModified() const
997 void wxTextCtrl::Clear()
1002 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
1004 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
1006 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
1008 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1009 event
.SetEventObject(this);
1010 event
.SetString(GetValue());
1011 if (GetEventHandler()->ProcessEvent(event
)) return;
1014 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1016 // This will invoke the dialog default action, such
1017 // as the clicking the default button.
1019 wxWindow
*top_frame
= m_parent
;
1020 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1021 top_frame
= top_frame
->GetParent();
1023 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1025 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1027 if (window
->default_widget
)
1029 gtk_widget_activate (window
->default_widget
);
1038 GtkWidget
* wxTextCtrl::GetConnectWidget()
1040 return GTK_WIDGET(m_text
);
1043 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1045 if (m_windowStyle
& wxTE_MULTILINE
)
1046 return (window
== GTK_TEXT(m_text
)->text_area
);
1048 return (window
== GTK_ENTRY(m_text
)->text_area
);
1051 // the font will change for subsequent text insertiongs
1052 bool wxTextCtrl::SetFont( const wxFont
&font
)
1054 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1056 if ( !wxTextCtrlBase::SetFont(font
) )
1058 // font didn't change, nothing to do
1062 if ( m_windowStyle
& wxTE_MULTILINE
)
1064 m_updateFont
= TRUE
;
1066 m_defaultStyle
.SetFont(font
);
1068 ChangeFontGlobally();
1074 void wxTextCtrl::ChangeFontGlobally()
1076 // this method is very inefficient and hence should be called as rarely as
1078 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1079 _T("shouldn't be called for single line controls") );
1081 wxString value
= GetValue();
1082 if ( !value
.IsEmpty() )
1084 m_updateFont
= FALSE
;
1091 void wxTextCtrl::UpdateFontIfNeeded()
1094 ChangeFontGlobally();
1097 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1099 if ( !wxControl::SetForegroundColour(colour
) )
1102 // update default fg colour too
1103 m_defaultStyle
.SetTextColour(colour
);
1108 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1110 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1112 if ( !wxControl::SetBackgroundColour( colour
) )
1115 if (!m_widget
->window
)
1118 wxColour sysbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1119 if (sysbg
.Red() == colour
.Red() &&
1120 sysbg
.Green() == colour
.Green() &&
1121 sysbg
.Blue() == colour
.Blue())
1123 return FALSE
; // FIXME or TRUE?
1126 if (!m_backgroundColour
.Ok())
1129 if (m_windowStyle
& wxTE_MULTILINE
)
1131 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1134 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1135 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1136 gdk_window_clear( window
);
1139 // change active background color too
1140 m_defaultStyle
.SetBackgroundColour( colour
);
1145 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1147 /* VERY dirty way to do that - removes the required text and re-adds it
1148 with styling (FIXME) */
1149 if ( m_windowStyle
& wxTE_MULTILINE
)
1151 if ( style
.IsDefault() )
1157 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1159 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1160 _T("invalid range in wxTextCtrl::SetStyle") );
1162 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1163 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1164 wxString
tmp(text
,*wxConvCurrent
);
1167 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1168 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1171 wxWX2MBbuf buf
= tmp
.mbc_str();
1172 const char *txt
= buf
;
1173 size_t txtlen
= strlen(buf
);
1175 const char *txt
= tmp
;
1176 size_t txtlen
= tmp
.length();
1179 // use the attributes from style which are set in it and fall back
1180 // first to the default style and then to the text control default
1181 // colours for the others
1182 wxGtkTextInsert(m_text
,
1183 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1187 /* does not seem to help under GTK+ 1.2 !!!
1188 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1189 SetInsertionPoint( old_pos
);
1194 // cannot do this for GTK+'s Entry widget
1199 void wxTextCtrl::ApplyWidgetStyle()
1201 if (m_windowStyle
& wxTE_MULTILINE
)
1208 gtk_widget_set_style( m_text
, m_widgetStyle
);
1212 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1217 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1222 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1227 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1232 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1237 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1239 event
.Enable( CanCut() );
1242 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1244 event
.Enable( CanCopy() );
1247 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1249 event
.Enable( CanPaste() );
1252 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1254 event
.Enable( CanUndo() );
1257 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1259 event
.Enable( CanRedo() );
1262 void wxTextCtrl::OnInternalIdle()
1264 wxCursor cursor
= m_cursor
;
1265 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1269 GdkWindow
*window
= (GdkWindow
*) NULL
;
1270 if (HasFlag(wxTE_MULTILINE
))
1271 window
= GTK_TEXT(m_text
)->text_area
;
1273 window
= GTK_ENTRY(m_text
)->text_area
;
1276 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1278 if (!g_globalCursor
.Ok())
1279 cursor
= *wxSTANDARD_CURSOR
;
1281 window
= m_widget
->window
;
1282 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1283 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1286 if (g_delayedFocus
== this)
1288 if (GTK_WIDGET_REALIZED(m_widget
))
1290 gtk_widget_grab_focus( m_widget
);
1291 g_delayedFocus
= NULL
;
1298 wxSize
wxTextCtrl::DoGetBestSize() const
1300 // FIXME should be different for multi-line controls...
1301 wxSize
ret( wxControl::DoGetBestSize() );
1302 return wxSize(80, ret
.y
);
1305 // ----------------------------------------------------------------------------
1307 // ----------------------------------------------------------------------------
1309 void wxTextCtrl::Freeze()
1311 if ( HasFlag(wxTE_MULTILINE
) )
1313 gtk_text_freeze(GTK_TEXT(m_text
));
1317 void wxTextCtrl::Thaw()
1319 if ( HasFlag(wxTE_MULTILINE
) )
1321 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1323 gtk_text_thaw(GTK_TEXT(m_text
));
1327 // ----------------------------------------------------------------------------
1329 // ----------------------------------------------------------------------------
1331 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1333 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1336 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1338 float value
= adj
->value
+ diff
;
1343 float upper
= adj
->upper
- adj
->page_size
;
1344 if ( value
> upper
)
1347 // did we noticeably change the scroll position?
1348 if ( fabs(adj
->value
- value
) < 0.2 )
1350 // well, this is what Robert does in wxScrollBar, so it must be good...
1356 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1361 bool wxTextCtrl::ScrollLines(int lines
)
1363 GtkAdjustment
*adj
= GetVAdj();
1367 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1368 static const int KEY_SCROLL_PIXELS
= 10;
1370 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1373 bool wxTextCtrl::ScrollPages(int pages
)
1375 GtkAdjustment
*adj
= GetVAdj();
1379 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));