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 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
154 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
156 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
157 EVT_CHAR(wxTextCtrl::OnChar
)
159 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
160 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
161 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
162 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
163 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
165 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
166 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
167 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
168 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
169 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
172 void wxTextCtrl::Init()
176 m_updateFont
= FALSE
;
178 m_vScrollbar
= (GtkWidget
*)NULL
;
181 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
183 const wxString
&value
,
187 const wxValidator
& validator
,
188 const wxString
&name
)
192 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
195 bool wxTextCtrl::Create( wxWindow
*parent
,
197 const wxString
&value
,
201 const wxValidator
& validator
,
202 const wxString
&name
)
205 m_acceptsFocus
= TRUE
;
207 if (!PreCreation( parent
, pos
, size
) ||
208 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
210 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
215 m_vScrollbarVisible
= FALSE
;
217 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
221 /* a multi-line edit control: create a vertical scrollbar by default and
222 horizontal if requested */
223 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
225 bool bHasHScrollbar
= FALSE
;
228 /* create our control ... */
229 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
231 /* ... and put into the upper left hand corner of the table */
232 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
233 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
234 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
235 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
236 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
239 /* always wrap words */
240 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
243 /* put the horizontal scrollbar in the lower left hand corner */
246 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
247 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
248 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
249 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
252 gtk_widget_show(hscrollbar
);
254 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
255 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
259 /* finally, put the vertical scrollbar in the upper right corner */
260 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
261 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
262 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
264 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
269 /* a single-line text control: no need for scrollbars */
271 m_text
= gtk_entry_new();
274 m_parent
->DoAddChild( this );
276 m_focusWidget
= m_text
;
280 SetFont( parent
->GetFont() );
282 wxSize
size_best( DoGetBestSize() );
283 wxSize
new_size( size
);
284 if (new_size
.x
== -1)
285 new_size
.x
= size_best
.x
;
286 if (new_size
.y
== -1)
287 new_size
.y
= size_best
.y
;
288 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
289 SetSize( new_size
.x
, new_size
.y
);
292 gtk_widget_show(m_text
);
296 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
297 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
300 if (!value
.IsEmpty())
304 #if !GTK_CHECK_VERSION(1, 2, 0)
305 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
306 // gtk_editable_insert_text()
307 gtk_widget_realize(m_text
);
311 wxWX2MBbuf val
= value
.mbc_str();
312 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
314 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
315 #endif // Unicode/!Unicode
319 /* bring editable's cursor uptodate. bug in GTK. */
320 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
324 if (style
& wxTE_PASSWORD
)
327 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
330 if (style
& wxTE_READONLY
)
333 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
338 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
341 /* we want to be notified about text changes */
342 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
343 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
345 /* we don't set a valid background colour, because the window
346 manager should use a default one */
347 m_backgroundColour
= wxColour();
349 wxColour colFg
= parent
->GetForegroundColour();
350 SetForegroundColour( colFg
);
352 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
354 wxTextAttr
attrDef( colFg
, m_backgroundColour
, parent
->GetFont() );
355 SetDefaultStyle( attrDef
);
362 void wxTextCtrl::CalculateScrollbar()
364 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
366 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
368 if (adj
->upper
- adj
->page_size
< 0.8)
370 if (m_vScrollbarVisible
)
372 gtk_widget_hide( m_vScrollbar
);
373 m_vScrollbarVisible
= FALSE
;
378 if (!m_vScrollbarVisible
)
380 gtk_widget_show( m_vScrollbar
);
381 m_vScrollbarVisible
= TRUE
;
386 wxString
wxTextCtrl::GetValue() const
388 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
391 if (m_windowStyle
& wxTE_MULTILINE
)
393 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
394 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
395 tmp
= wxString(text
,*wxConvCurrent
);
400 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
405 void wxTextCtrl::SetValue( const wxString
&value
)
407 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
409 if (m_windowStyle
& wxTE_MULTILINE
)
411 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
412 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
415 wxWX2MBbuf tmpbuf
= value
.mbc_str();
416 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
418 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
423 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
426 // GRG, Jun/2000: Changed this after a lot of discussion in
427 // the lists. wxWindows 2.2 will have a set of flags to
428 // customize this behaviour.
429 SetInsertionPoint(0);
434 void wxTextCtrl::WriteText( const wxString
&text
)
436 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
442 wxWX2MBbuf buf
= text
.mbc_str();
443 const char *txt
= buf
;
444 size_t txtlen
= strlen(buf
);
446 const char *txt
= text
;
447 size_t txtlen
= text
.length();
450 if ( m_windowStyle
& wxTE_MULTILINE
)
452 // After cursor movements, gtk_text_get_point() is wrong by one.
453 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
455 // if we have any special style, use it
456 if ( !m_defaultStyle
.IsDefault() )
460 wxGtkTextInsert(m_text
, m_defaultStyle
, txt
, txtlen
);
464 gint len
= GET_EDITABLE_POS(m_text
);
465 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
468 // Bring editable's cursor back uptodate.
469 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
473 // This moves the cursor pos to behind the inserted text.
474 gint len
= GET_EDITABLE_POS(m_text
);
475 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
477 // Bring editable's cursor uptodate.
480 // Bring entry's cursor uptodate.
481 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
487 void wxTextCtrl::AppendText( const wxString
&text
)
489 SetInsertionPointEnd();
493 wxString
wxTextCtrl::GetLineText( long lineNo
) const
495 if (m_windowStyle
& wxTE_MULTILINE
)
497 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
498 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
502 wxString
buf(wxT(""));
505 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
510 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
518 return wxEmptyString
;
523 if (lineNo
== 0) return GetValue();
524 return wxEmptyString
;
528 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
530 /* If you implement this, don't forget to update the documentation!
531 * (file docs/latex/wx/text.tex) */
532 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
535 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
537 if ( m_windowStyle
& wxTE_MULTILINE
)
539 wxString text
= GetValue();
541 // cast to prevent warning. But pos really should've been unsigned.
542 if( (unsigned long)pos
> text
.Len() )
548 const wxChar
* stop
= text
.c_str() + pos
;
549 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
560 else // single line control
562 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
569 // index out of bounds
577 long wxTextCtrl::XYToPosition(long x
, long y
) const
579 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
582 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
588 int wxTextCtrl::GetLineLength(long lineNo
) const
590 wxString str
= GetLineText (lineNo
);
591 return (int) str
.Length();
594 int wxTextCtrl::GetNumberOfLines() const
596 if (m_windowStyle
& wxTE_MULTILINE
)
598 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
599 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
604 for (int i
= 0; i
< len
; i
++ )
611 // currentLine is 0 based, add 1 to get number of lines
612 return currentLine
+ 1;
625 void wxTextCtrl::SetInsertionPoint( long pos
)
627 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
629 if (m_windowStyle
& wxTE_MULTILINE
)
631 /* seems to be broken in GTK 1.0.X:
632 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
634 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
635 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
637 /* we fake a set_point by inserting and deleting. as the user
638 isn't supposed to get to know about thos non-sense, we
639 disconnect so that no events are sent to the user program. */
641 gint tmp
= (gint
)pos
;
642 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
643 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
645 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
646 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
648 /* bring editable's cursor uptodate. another bug in GTK. */
650 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
654 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
656 /* bring editable's cursor uptodate. bug in GTK. */
658 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
662 void wxTextCtrl::SetInsertionPointEnd()
664 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
666 if (m_windowStyle
& wxTE_MULTILINE
)
667 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
669 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
672 void wxTextCtrl::SetEditable( bool editable
)
674 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
676 if (m_windowStyle
& wxTE_MULTILINE
)
677 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
679 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
682 bool wxTextCtrl::Enable( bool enable
)
684 if (!wxWindowBase::Enable(enable
))
690 if (m_windowStyle
& wxTE_MULTILINE
)
692 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
693 OnParentEnable(enable
);
697 gtk_widget_set_sensitive( m_text
, enable
);
703 // wxGTK-specific: called recursively by Enable,
704 // to give widgets an oppprtunity to correct their colours after they
705 // have been changed by Enable
706 void wxTextCtrl::OnParentEnable( bool enable
)
708 // If we have a custom background colour, we use this colour in both
709 // disabled and enabled mode, or we end up with a different colour under the
711 wxColour oldColour
= GetBackgroundColour();
714 // Need to set twice or it'll optimize the useful stuff out
715 if (oldColour
== * wxWHITE
)
716 SetBackgroundColour(*wxBLACK
);
718 SetBackgroundColour(*wxWHITE
);
719 SetBackgroundColour(oldColour
);
723 void wxTextCtrl::DiscardEdits()
728 // ----------------------------------------------------------------------------
729 // max text length support
730 // ----------------------------------------------------------------------------
732 void wxTextCtrl::IgnoreNextTextUpdate()
734 m_ignoreNextUpdate
= TRUE
;
737 bool wxTextCtrl::IgnoreTextUpdate()
739 if ( m_ignoreNextUpdate
)
741 m_ignoreNextUpdate
= FALSE
;
749 void wxTextCtrl::SetMaxLength(unsigned long len
)
751 if ( !HasFlag(wxTE_MULTILINE
) )
753 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
755 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
756 // we had tried to enter more text than allowed by max text length and
757 // the text wasn't really changed
759 // to detect this and generate TEXT_MAXLEN event instead of
760 // TEXT_CHANGED one in this case we also catch "insert_text" signal
762 // when max len is set to 0 we disconnect our handler as it means that
763 // we shouldn't check anything any more
766 gtk_signal_connect( GTK_OBJECT(m_text
),
768 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
773 gtk_signal_disconnect_by_func
776 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
783 void wxTextCtrl::SetSelection( long from
, long to
)
785 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
787 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
788 !GTK_TEXT(m_text
)->line_start_cache
)
790 // tell the programmer that it didn't work
791 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
795 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
798 void wxTextCtrl::ShowPosition( long pos
)
800 if (m_windowStyle
& wxTE_MULTILINE
)
802 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
803 float totalLines
= (float) GetNumberOfLines();
806 PositionToXY(pos
, &posX
, &posY
);
807 float posLine
= (float) posY
;
808 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
809 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
813 long wxTextCtrl::GetInsertionPoint() const
815 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
817 return (long) GET_EDITABLE_POS(m_text
);
820 long wxTextCtrl::GetLastPosition() const
822 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
825 if (m_windowStyle
& wxTE_MULTILINE
)
826 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
828 pos
= GTK_ENTRY(m_text
)->text_length
;
833 void wxTextCtrl::Remove( long from
, long to
)
835 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
837 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
840 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
842 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
844 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
846 if (!value
.IsEmpty())
848 gint pos
= (gint
)from
;
850 wxWX2MBbuf buf
= value
.mbc_str();
851 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
853 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
858 void wxTextCtrl::Cut()
860 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
862 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
865 void wxTextCtrl::Copy()
867 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
869 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
872 void wxTextCtrl::Paste()
874 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
876 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
880 void wxTextCtrl::Undo()
883 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
886 void wxTextCtrl::Redo()
889 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
892 bool wxTextCtrl::CanUndo() const
895 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
899 bool wxTextCtrl::CanRedo() const
902 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
906 // If the return values from and to are the same, there is no
908 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
910 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
914 if ( !gtk_editable_get_selection_bounds(GTK_EDITABLE(m_text
), &from
, &to
) )
916 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
920 to
= GetInsertionPoint();
922 else // got selection
925 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
926 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
931 // exchange them to be compatible with wxMSW
944 bool wxTextCtrl::IsEditable() const
946 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
949 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
951 return GTK_EDITABLE(m_text
)->editable
;
955 bool wxTextCtrl::IsModified() const
960 void wxTextCtrl::Clear()
965 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
967 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
969 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
971 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
972 event
.SetEventObject(this);
973 event
.SetString(GetValue());
974 if (GetEventHandler()->ProcessEvent(event
)) return;
977 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
979 // This will invoke the dialog default action, such
980 // as the clicking the default button.
982 wxWindow
*top_frame
= m_parent
;
983 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
984 top_frame
= top_frame
->GetParent();
986 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
988 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
990 if (window
->default_widget
)
992 gtk_widget_activate (window
->default_widget
);
1001 GtkWidget
* wxTextCtrl::GetConnectWidget()
1003 return GTK_WIDGET(m_text
);
1006 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1008 if (m_windowStyle
& wxTE_MULTILINE
)
1009 return (window
== GTK_TEXT(m_text
)->text_area
);
1011 return (window
== GTK_ENTRY(m_text
)->text_area
);
1014 // the font will change for subsequent text insertiongs
1015 bool wxTextCtrl::SetFont( const wxFont
&font
)
1017 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1019 if ( !wxTextCtrlBase::SetFont(font
) )
1021 // font didn't change, nothing to do
1025 if ( m_windowStyle
& wxTE_MULTILINE
)
1027 m_updateFont
= TRUE
;
1029 m_defaultStyle
.SetFont(font
);
1031 ChangeFontGlobally();
1037 void wxTextCtrl::ChangeFontGlobally()
1039 // this method is very inefficient and hence should be called as rarely as
1041 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1042 _T("shouldn't be called for single line controls") );
1044 wxString value
= GetValue();
1045 if ( !value
.IsEmpty() )
1047 m_updateFont
= FALSE
;
1054 void wxTextCtrl::UpdateFontIfNeeded()
1057 ChangeFontGlobally();
1060 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1062 if ( !wxControl::SetForegroundColour(colour
) )
1065 // update default fg colour too
1066 m_defaultStyle
.SetTextColour(colour
);
1071 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1073 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1075 if ( !wxControl::SetBackgroundColour( colour
) )
1078 if (!m_widget
->window
)
1081 wxColour sysbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1082 if (sysbg
.Red() == colour
.Red() &&
1083 sysbg
.Green() == colour
.Green() &&
1084 sysbg
.Blue() == colour
.Blue())
1086 return FALSE
; // FIXME or TRUE?
1089 if (!m_backgroundColour
.Ok())
1092 if (m_windowStyle
& wxTE_MULTILINE
)
1094 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1097 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1098 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1099 gdk_window_clear( window
);
1102 // change active background color too
1103 m_defaultStyle
.SetBackgroundColour( colour
);
1108 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1110 /* VERY dirty way to do that - removes the required text and re-adds it
1111 with styling (FIXME) */
1112 if ( m_windowStyle
& wxTE_MULTILINE
)
1114 if ( style
.IsDefault() )
1120 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1122 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1123 _T("invalid range in wxTextCtrl::SetStyle") );
1125 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1126 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1127 wxString
tmp(text
,*wxConvCurrent
);
1130 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1131 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1134 wxWX2MBbuf buf
= tmp
.mbc_str();
1135 const char *txt
= buf
;
1136 size_t txtlen
= strlen(buf
);
1138 const char *txt
= tmp
;
1139 size_t txtlen
= tmp
.length();
1142 // use the attributes from style which are set in it and fall back
1143 // first to the default style and then to the text control default
1144 // colours for the others
1145 wxGtkTextInsert(m_text
,
1146 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1150 /* does not seem to help under GTK+ 1.2 !!!
1151 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1152 SetInsertionPoint( old_pos
);
1157 // cannot do this for GTK+'s Entry widget
1162 void wxTextCtrl::ApplyWidgetStyle()
1164 if (m_windowStyle
& wxTE_MULTILINE
)
1171 gtk_widget_set_style( m_text
, m_widgetStyle
);
1175 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1180 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1185 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1190 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1195 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1200 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1202 event
.Enable( CanCut() );
1205 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1207 event
.Enable( CanCopy() );
1210 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1212 event
.Enable( CanPaste() );
1215 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1217 event
.Enable( CanUndo() );
1220 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1222 event
.Enable( CanRedo() );
1225 void wxTextCtrl::OnInternalIdle()
1227 wxCursor cursor
= m_cursor
;
1228 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1232 GdkWindow
*window
= (GdkWindow
*) NULL
;
1233 if (HasFlag(wxTE_MULTILINE
))
1234 window
= GTK_TEXT(m_text
)->text_area
;
1236 window
= GTK_ENTRY(m_text
)->text_area
;
1239 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1241 if (!g_globalCursor
.Ok())
1242 cursor
= *wxSTANDARD_CURSOR
;
1244 window
= m_widget
->window
;
1245 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1246 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1249 if (g_delayedFocus
== this)
1251 if (GTK_WIDGET_REALIZED(m_widget
))
1253 gtk_widget_grab_focus( m_widget
);
1254 g_delayedFocus
= NULL
;
1261 wxSize
wxTextCtrl::DoGetBestSize() const
1263 // FIXME should be different for multi-line controls...
1264 wxSize
ret( wxControl::DoGetBestSize() );
1265 return wxSize(80, ret
.y
);
1268 // ----------------------------------------------------------------------------
1270 // ----------------------------------------------------------------------------
1272 void wxTextCtrl::Freeze()
1274 if ( HasFlag(wxTE_MULTILINE
) )
1276 gtk_text_freeze(GTK_TEXT(m_text
));
1280 void wxTextCtrl::Thaw()
1282 if ( HasFlag(wxTE_MULTILINE
) )
1284 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1286 gtk_text_thaw(GTK_TEXT(m_text
));
1290 // ----------------------------------------------------------------------------
1292 // ----------------------------------------------------------------------------
1294 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1296 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1299 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1301 float value
= adj
->value
+ diff
;
1306 float upper
= adj
->upper
- adj
->page_size
;
1307 if ( value
> upper
)
1310 // did we noticeably change the scroll position?
1311 if ( fabs(adj
->value
- value
) < 0.2 )
1313 // well, this is what Robert does in wxScrollBar, so it must be good...
1319 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1324 bool wxTextCtrl::ScrollLines(int lines
)
1326 GtkAdjustment
*adj
= GetVAdj();
1330 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1331 static const int KEY_SCROLL_PIXELS
= 10;
1333 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1336 bool wxTextCtrl::ScrollPages(int pages
)
1338 GtkAdjustment
*adj
= GetVAdj();
1342 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));