1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/textctrl.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
21 #include "wx/settings.h"
25 #include "wx/strconv.h"
26 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
27 #include "wx/evtloop.h"
29 #include <sys/types.h>
33 #include "wx/gtk1/private.h"
34 #include <gdk/gdkkeysyms.h>
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern void wxapp_install_idle_handler();
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 extern wxCursor g_globalCursor
;
48 extern wxWindowGTK
*g_delayedFocus
;
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
55 static void wxGtkTextInsert(GtkWidget
*text
,
56 const wxTextAttr
& attr
,
64 tmpFont
= attr
.GetFont();
66 // FIXME: if this crashes because tmpFont goes out of scope and the GdkFont is
67 // deleted, then we need to call gdk_font_ref on font.
68 // This is because attr.GetFont() now returns a temporary font since wxTextAttr
69 // no longer stores a wxFont object, for efficiency.
71 font
= tmpFont
.GetInternalFont();
76 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
79 GdkColor
*colBg
= attr
.HasBackgroundColour()
80 ? attr
.GetBackgroundColour().GetColor()
83 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
87 // ----------------------------------------------------------------------------
88 // "insert_text" for GtkEntry
89 // ----------------------------------------------------------------------------
93 gtk_insert_text_callback(GtkEditable
*editable
,
94 const gchar
*WXUNUSED(new_text
),
95 gint
WXUNUSED(new_text_length
),
96 gint
*WXUNUSED(position
),
100 wxapp_install_idle_handler();
102 // we should only be called if we have a max len limit at all
103 GtkEntry
*entry
= GTK_ENTRY (editable
);
105 wxCHECK_RET( entry
->text_max_length
, wxT("shouldn't be called") );
107 // check that we don't overflow the max length limit
109 // FIXME: this doesn't work when we paste a string which is going to be
111 if ( entry
->text_length
== entry
->text_max_length
)
113 // we don't need to run the base class version at all
114 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
116 // remember that the next changed signal is to be ignored to avoid
117 // generating a dummy wxEVT_TEXT event
118 win
->IgnoreNextTextUpdate();
120 // and generate the correct one ourselves
121 wxCommandEvent
event(wxEVT_TEXT_MAXLEN
, win
->GetId());
122 event
.SetEventObject(win
);
123 event
.SetString(win
->GetValue());
124 win
->HandleWindowEvent( event
);
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
135 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
137 if ( win
->IgnoreTextUpdate() )
140 if (!win
->m_hasVMT
) return;
143 wxapp_install_idle_handler();
146 win
->UpdateFontIfNeeded();
148 wxCommandEvent
event( wxEVT_TEXT
, win
->GetId() );
149 event
.SetEventObject( win
);
150 win
->HandleWindowEvent( event
);
154 //-----------------------------------------------------------------------------
155 // "changed" from vertical scrollbar
156 //-----------------------------------------------------------------------------
160 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
162 if (!win
->m_hasVMT
) return;
165 wxapp_install_idle_handler();
167 win
->CalculateScrollbar();
171 // ----------------------------------------------------------------------------
172 // redraw callback for multiline text
173 // ----------------------------------------------------------------------------
175 // redrawing a GtkText from inside a wxYield() call results in crashes (the
176 // text sample shows it in its "Add lines" command which shows wxProgressDialog
177 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
178 // don't do anything if we're inside wxYield()
181 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
184 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
187 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
189 wxEventLoopBase
* loop
= wxEventLoopBase::GetActive();
190 if ( loop
&& !loop
->IsYielding() )
192 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
193 wxT("infinite recursion in wxgtk_text_draw aborted") );
195 gs_gtk_text_draw(widget
, rect
);
200 //-----------------------------------------------------------------------------
202 //-----------------------------------------------------------------------------
204 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
205 EVT_CHAR(wxTextCtrl::OnChar
)
207 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
208 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
209 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
210 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
211 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
213 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
214 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
215 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
216 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
217 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
220 void wxTextCtrl::Init()
224 SetUpdateFont(false);
229 wxTextCtrl::~wxTextCtrl()
233 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
235 const wxString
&value
,
239 const wxValidator
& validator
,
240 const wxString
&name
)
244 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
247 bool wxTextCtrl::Create( wxWindow
*parent
,
249 const wxString
&value
,
253 const wxValidator
& validator
,
254 const wxString
&name
)
257 m_acceptsFocus
= true;
259 if (!PreCreation( parent
, pos
, size
) ||
260 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
262 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
267 m_vScrollbarVisible
= false;
269 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
273 // create our control ...
274 m_text
= gtk_text_new( NULL
, NULL
);
276 // ... and put into the upper left hand corner of the table
277 bool bHasHScrollbar
= false;
278 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
279 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
280 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
281 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
282 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
286 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
288 // finally, put the vertical scrollbar in the upper right corner
289 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
290 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
291 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
293 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
298 // a single-line text control: no need for scrollbars
300 m_text
= gtk_entry_new();
303 m_parent
->DoAddChild( this );
305 m_focusWidget
= m_text
;
310 gtk_widget_show(m_text
);
314 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
315 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
317 // only initialize gs_gtk_text_draw once, starting from the next the
318 // klass::draw will already be wxgtk_text_draw
319 if ( !gs_gtk_text_draw
)
322 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
324 gs_gtk_text_draw
= draw
;
326 draw
= wxgtk_text_draw
;
332 #if !GTK_CHECK_VERSION(1, 2, 0)
333 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
334 // gtk_editable_insert_text()
335 gtk_widget_realize(m_text
);
340 wxWX2MBbuf val
= value
.mbc_str();
341 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
343 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &tmp
);
348 // Bring editable's cursor uptodate. Bug in GTK.
349 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
353 if (style
& wxTE_PASSWORD
)
356 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
359 if (style
& wxTE_READONLY
)
362 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
367 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
370 // We want to be notified about text changes.
371 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
372 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
374 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
376 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
377 SetDefaultStyle( attrDef
);
383 void wxTextCtrl::CalculateScrollbar()
385 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
387 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
389 if (adj
->upper
- adj
->page_size
< 0.8)
391 if (m_vScrollbarVisible
)
393 gtk_widget_hide( m_vScrollbar
);
394 m_vScrollbarVisible
= false;
399 if (!m_vScrollbarVisible
)
401 gtk_widget_show( m_vScrollbar
);
402 m_vScrollbarVisible
= true;
407 wxString
wxTextCtrl::DoGetValue() const
409 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
412 if (m_windowStyle
& wxTE_MULTILINE
)
414 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
415 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
421 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
427 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
429 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
431 if ( !(flags
& SetValue_SendEvent
) )
433 // do not generate events
434 IgnoreNextTextUpdate();
437 if (m_windowStyle
& wxTE_MULTILINE
)
439 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
440 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
442 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.length(), &len
);
446 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
449 // GRG, Jun/2000: Changed this after a lot of discussion in
450 // the lists. wxWidgets 2.2 will have a set of flags to
451 // customize this behaviour.
452 SetInsertionPoint(0);
457 void wxTextCtrl::WriteText( const wxString
&text
)
459 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
464 // gtk_text_changed_callback() will set m_modified to true but m_modified
465 // shouldn't be changed by the program writing to the text control itself,
466 // so save the old value and restore when we're done
467 bool oldModified
= m_modified
;
469 if ( m_windowStyle
& wxTE_MULTILINE
)
471 // After cursor movements, gtk_text_get_point() is wrong by one.
472 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
474 // always use m_defaultStyle, even if it is empty as otherwise
475 // resetting the style and appending some more text wouldn't work: if
476 // we don't specify the style explicitly, the old style would be used
477 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
478 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.length());
480 // we called wxGtkTextInsert with correct font, no need to do anything
481 // in UpdateFontIfNeeded() any longer
484 SetUpdateFont(false);
487 // Bring editable's cursor back uptodate.
488 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
492 // First remove the selection if there is one
493 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
495 // This moves the cursor pos to behind the inserted text.
496 gint len
= GET_EDITABLE_POS(m_text
);
498 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.length(), &len
);
500 // Bring entry's cursor uptodate.
501 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
504 m_modified
= oldModified
;
507 void wxTextCtrl::AppendText( const wxString
&text
)
509 SetInsertionPointEnd();
513 wxString
wxTextCtrl::GetLineText( long lineNo
) const
515 if (m_windowStyle
& wxTE_MULTILINE
)
517 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
518 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
525 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
530 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
538 return wxEmptyString
;
543 if (lineNo
== 0) return GetValue();
544 return wxEmptyString
;
548 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
550 /* If you implement this, don't forget to update the documentation!
551 * (file docs/latex/wx/text.tex) */
552 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
555 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
557 if ( m_windowStyle
& wxTE_MULTILINE
)
559 wxString text
= GetValue();
561 // cast to prevent warning. But pos really should've been unsigned.
562 if( (unsigned long)pos
> text
.length() )
568 const wxChar
* stop
= text
.c_str() + pos
;
569 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
580 else // single line control
582 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
589 // index out of bounds
597 long wxTextCtrl::XYToPosition(long x
, long y
) const
599 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
602 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
608 int wxTextCtrl::GetLineLength(long lineNo
) const
610 wxString str
= GetLineText (lineNo
);
611 return (int) str
.length();
614 int wxTextCtrl::GetNumberOfLines() const
616 if (m_windowStyle
& wxTE_MULTILINE
)
618 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
619 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
624 for (int i
= 0; i
< len
; i
++ )
631 // currentLine is 0 based, add 1 to get number of lines
632 return currentLine
+ 1;
645 void wxTextCtrl::SetInsertionPoint( long pos
)
647 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
651 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
652 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
654 /* we fake a set_point by inserting and deleting. as the user
655 isn't supposed to get to know about this non-sense, we
656 disconnect so that no events are sent to the user program. */
658 gint tmp
= (gint
)pos
;
659 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
660 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
662 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
663 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
665 // bring editable's cursor uptodate. Bug in GTK.
666 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
670 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
672 // Bring editable's cursor uptodate. Bug in GTK.
673 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
677 void wxTextCtrl::SetInsertionPointEnd()
679 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
681 if (m_windowStyle
& wxTE_MULTILINE
)
683 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
687 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
691 void wxTextCtrl::SetEditable( bool editable
)
693 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
695 if (m_windowStyle
& wxTE_MULTILINE
)
697 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
701 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
705 void wxTextCtrl::DoEnable( bool enable
)
707 if (m_windowStyle
& wxTE_MULTILINE
)
709 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
713 gtk_widget_set_sensitive( m_text
, enable
);
717 void wxTextCtrl::MarkDirty()
722 void wxTextCtrl::DiscardEdits()
727 // ----------------------------------------------------------------------------
728 // max text length support
729 // ----------------------------------------------------------------------------
731 void wxTextCtrl::IgnoreNextTextUpdate()
733 m_ignoreNextUpdate
= true;
736 bool wxTextCtrl::IgnoreTextUpdate()
738 if ( m_ignoreNextUpdate
)
740 m_ignoreNextUpdate
= false;
748 void wxTextCtrl::SetMaxLength(unsigned long len
)
750 if ( !HasFlag(wxTE_MULTILINE
) )
752 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
754 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
755 // we had tried to enter more text than allowed by max text length and
756 // the text wasn't really changed
758 // to detect this and generate TEXT_MAXLEN event instead of
759 // TEXT_CHANGED one in this case we also catch "insert_text" signal
761 // when max len is set to 0 we disconnect our handler as it means that
762 // we shouldn't check anything any more
765 gtk_signal_connect( GTK_OBJECT(m_text
),
767 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
772 gtk_signal_disconnect_by_func
775 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
782 void wxTextCtrl::SetSelection( long from
, long to
)
784 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
786 if (from
== -1 && to
== -1)
789 to
= GetValue().length();
792 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
793 !GTK_TEXT(m_text
)->line_start_cache
)
795 // tell the programmer that it didn't work
796 wxLogDebug(wxT("Can't call SetSelection() before realizing the control"));
800 if (m_windowStyle
& wxTE_MULTILINE
)
802 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
806 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
810 void wxTextCtrl::ShowPosition( long pos
)
812 if (m_windowStyle
& wxTE_MULTILINE
)
814 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
815 float totalLines
= (float) GetNumberOfLines();
818 PositionToXY(pos
, &posX
, &posY
);
819 float posLine
= (float) posY
;
820 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
821 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
825 long wxTextCtrl::GetInsertionPoint() const
827 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
828 return (long) GET_EDITABLE_POS(m_text
);
831 wxTextPos
wxTextCtrl::GetLastPosition() const
833 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
837 if (m_windowStyle
& wxTE_MULTILINE
)
839 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
843 pos
= GTK_ENTRY(m_text
)->text_length
;
849 void wxTextCtrl::Remove( long from
, long to
)
851 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
852 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
855 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
857 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
863 gint pos
= (gint
)from
;
865 wxWX2MBbuf buf
= value
.mbc_str();
866 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
868 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &pos
);
869 #endif // wxUSE_UNICODE
873 void wxTextCtrl::Cut()
875 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
876 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
879 void wxTextCtrl::Copy()
881 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
882 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
885 void wxTextCtrl::Paste()
887 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
888 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
892 void wxTextCtrl::Undo()
895 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
898 void wxTextCtrl::Redo()
901 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
904 bool wxTextCtrl::CanUndo() const
907 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
911 bool wxTextCtrl::CanRedo() const
914 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
918 // If the return values from and to are the same, there is no
920 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
922 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
926 bool haveSelection
= false;
928 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
930 haveSelection
= true;
931 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
932 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
935 if (! haveSelection
)
936 from
= to
= GetInsertionPoint();
940 // exchange them to be compatible with wxMSW
953 bool wxTextCtrl::IsEditable() const
955 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
956 return GTK_EDITABLE(m_text
)->editable
;
959 bool wxTextCtrl::IsModified() const
964 void wxTextCtrl::Clear()
966 SetValue( wxEmptyString
);
969 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
971 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
973 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
975 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
976 event
.SetEventObject(this);
977 event
.SetString(GetValue());
978 if (HandleWindowEvent(event
)) return;
981 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
983 // This will invoke the dialog default action, such
984 // as the clicking the default button.
986 wxWindow
*top_frame
= m_parent
;
987 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
988 top_frame
= top_frame
->GetParent();
990 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
992 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
994 if (window
->default_widget
)
996 gtk_widget_activate (window
->default_widget
);
1005 GtkWidget
* wxTextCtrl::GetConnectWidget()
1007 return GTK_WIDGET(m_text
);
1010 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1012 if (m_windowStyle
& wxTE_MULTILINE
)
1014 return (window
== GTK_TEXT(m_text
)->text_area
);
1018 return (window
== GTK_ENTRY(m_text
)->text_area
);
1022 // the font will change for subsequent text insertiongs
1023 bool wxTextCtrl::SetFont( const wxFont
&font
)
1025 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1027 if ( !wxTextCtrlBase::SetFont(font
) )
1029 // font didn't change, nothing to do
1033 if ( m_windowStyle
& wxTE_MULTILINE
)
1035 SetUpdateFont(true);
1037 m_defaultStyle
.SetFont(font
);
1039 ChangeFontGlobally();
1045 void wxTextCtrl::ChangeFontGlobally()
1047 // this method is very inefficient and hence should be called as rarely as
1049 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1051 wxT("shouldn't be called for single line controls") );
1053 wxString value
= GetValue();
1054 if ( !value
.empty() )
1056 SetUpdateFont(false);
1063 void wxTextCtrl::UpdateFontIfNeeded()
1066 ChangeFontGlobally();
1069 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1071 if ( !wxControl::SetForegroundColour(colour
) )
1074 // update default fg colour too
1075 m_defaultStyle
.SetTextColour(colour
);
1080 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1082 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1084 if ( !wxControl::SetBackgroundColour( colour
) )
1087 if (!m_widget
->window
)
1090 if (!m_backgroundColour
.IsOk())
1093 if (m_windowStyle
& wxTE_MULTILINE
)
1095 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1098 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1099 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1100 gdk_window_clear( window
);
1103 // change active background color too
1104 m_defaultStyle
.SetBackgroundColour( colour
);
1109 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1111 if ( m_windowStyle
& wxTE_MULTILINE
)
1113 if ( style
.IsDefault() )
1119 // VERY dirty way to do that - removes the required text and re-adds it
1120 // with styling (FIXME)
1122 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1124 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1125 wxT("invalid range in wxTextCtrl::SetStyle") );
1127 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1128 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1129 wxString
tmp(text
,*wxConvCurrent
);
1132 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1133 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1136 wxWX2MBbuf buf
= tmp
.mbc_str();
1137 const char *txt
= buf
;
1138 size_t txtlen
= strlen(buf
);
1140 const char *txt
= tmp
;
1141 size_t txtlen
= tmp
.length();
1144 // use the attributes from style which are set in it and fall back
1145 // first to the default style and then to the text control default
1146 // colours for the others
1147 wxGtkTextInsert(m_text
,
1148 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1152 /* does not seem to help under GTK+ 1.2 !!!
1153 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1154 SetInsertionPoint( old_pos
);
1160 // cannot do this for GTK+'s Entry widget
1164 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1166 gtk_widget_modify_style(m_text
, style
);
1169 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1174 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1179 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1184 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1189 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1194 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1196 event
.Enable( CanCut() );
1199 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1201 event
.Enable( CanCopy() );
1204 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1206 event
.Enable( CanPaste() );
1209 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1211 event
.Enable( CanUndo() );
1214 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1216 event
.Enable( CanRedo() );
1219 void wxTextCtrl::OnInternalIdle()
1221 wxCursor cursor
= m_cursor
;
1222 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
1226 GdkWindow
*window
= NULL
;
1227 if (HasFlag(wxTE_MULTILINE
))
1228 window
= GTK_TEXT(m_text
)->text_area
;
1230 window
= GTK_ENTRY(m_text
)->text_area
;
1233 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1235 if (!g_globalCursor
.IsOk())
1236 cursor
= *wxSTANDARD_CURSOR
;
1238 window
= m_widget
->window
;
1239 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1240 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1243 if (g_delayedFocus
== this)
1245 if (GTK_WIDGET_REALIZED(m_widget
))
1247 gtk_widget_grab_focus( m_widget
);
1248 g_delayedFocus
= NULL
;
1252 if (wxUpdateUIEvent::CanUpdate(this))
1253 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1256 wxSize
wxTextCtrl::DoGetBestSize() const
1258 // FIXME should be different for multi-line controls...
1259 wxSize
ret( wxControl::DoGetBestSize() );
1260 wxSize
best(80, ret
.y
);
1261 CacheBestSize(best
);
1265 // ----------------------------------------------------------------------------
1267 // ----------------------------------------------------------------------------
1269 void wxTextCtrl::DoFreeze()
1271 if ( HasFlag(wxTE_MULTILINE
) )
1273 gtk_text_freeze(GTK_TEXT(m_text
));
1277 void wxTextCtrl::DoThaw()
1279 if ( HasFlag(wxTE_MULTILINE
) )
1281 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1283 gtk_text_thaw(GTK_TEXT(m_text
));
1287 // ----------------------------------------------------------------------------
1289 // ----------------------------------------------------------------------------
1291 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1293 if ( !IsMultiLine() )
1296 return GTK_TEXT(m_text
)->vadj
;
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...
1318 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1323 bool wxTextCtrl::ScrollLines(int lines
)
1325 GtkAdjustment
*adj
= GetVAdj();
1329 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1330 int diff
= 10*lines
;
1332 return DoScroll(adj
, diff
);
1335 bool wxTextCtrl::ScrollPages(int pages
)
1337 GtkAdjustment
*adj
= GetVAdj();
1341 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
1347 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1349 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);