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"
20 #include "wx/settings.h"
24 #include "wx/strconv.h"
25 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
27 #include <sys/types.h>
31 #include "wx/gtk1/private.h"
32 #include <gdk/gdkkeysyms.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern void wxapp_install_idle_handler();
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern wxCursor g_globalCursor;
46 extern wxWindowGTK *g_delayedFocus;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
53 static void wxGtkTextInsert(GtkWidget *text,
54 const wxTextAttr& attr,
62 tmpFont = attr.GetFont();
64 // FIXME: if this crashes because tmpFont goes out of scope and the GdkFont is
65 // deleted, then we need to call gdk_font_ref on font.
66 // This is because attr.GetFont() now returns a temporary font since wxTextAttr
67 // no longer stores a wxFont object, for efficiency.
69 font = tmpFont.GetInternalFont();
74 GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor()
77 GdkColor *colBg = attr.HasBackgroundColour()
78 ? attr.GetBackgroundColour().GetColor()
81 gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len );
85 // ----------------------------------------------------------------------------
86 // "insert_text" for GtkEntry
87 // ----------------------------------------------------------------------------
91 gtk_insert_text_callback(GtkEditable *editable,
92 const gchar *new_text,
98 wxapp_install_idle_handler();
100 // we should only be called if we have a max len limit at all
101 GtkEntry *entry = GTK_ENTRY (editable);
103 wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") );
105 // check that we don't overflow the max length limit
107 // FIXME: this doesn't work when we paste a string which is going to be
109 if ( entry->text_length == entry->text_max_length )
111 // we don't need to run the base class version at all
112 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
114 // remember that the next changed signal is to be ignored to avoid
115 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
116 win->IgnoreNextTextUpdate();
118 // and generate the correct one ourselves
119 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId());
120 event.SetEventObject(win);
121 event.SetString(win->GetValue());
122 win->GetEventHandler()->ProcessEvent( event );
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
133 gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win )
135 if ( win->IgnoreTextUpdate() )
138 if (!win->m_hasVMT) return;
141 wxapp_install_idle_handler();
144 win->UpdateFontIfNeeded();
146 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
147 event.SetEventObject( win );
148 win->GetEventHandler()->ProcessEvent( event );
152 //-----------------------------------------------------------------------------
153 // "changed" from vertical scrollbar
154 //-----------------------------------------------------------------------------
158 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
160 if (!win->m_hasVMT) return;
163 wxapp_install_idle_handler();
165 win->CalculateScrollbar();
169 // ----------------------------------------------------------------------------
170 // redraw callback for multiline text
171 // ----------------------------------------------------------------------------
173 // redrawing a GtkText from inside a wxYield() call results in crashes (the
174 // text sample shows it in its "Add lines" command which shows wxProgressDialog
175 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
176 // don't do anything if we're inside wxYield()
178 extern bool wxIsInsideYield;
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 if ( !wxIsInsideYield )
191 wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw,
192 _T("infinite recursion in wxgtk_text_draw aborted") );
194 gs_gtk_text_draw(widget, rect);
199 //-----------------------------------------------------------------------------
201 //-----------------------------------------------------------------------------
203 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
205 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
206 EVT_CHAR(wxTextCtrl::OnChar)
208 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
209 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
210 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
211 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
212 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
214 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
215 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
216 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
217 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
218 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
221 void wxTextCtrl::Init()
225 SetUpdateFont(false);
227 m_vScrollbar = (GtkWidget *)NULL;
230 wxTextCtrl::~wxTextCtrl()
234 wxTextCtrl::wxTextCtrl( wxWindow *parent,
236 const wxString &value,
240 const wxValidator& validator,
241 const wxString &name )
245 Create( parent, id, value, pos, size, style, validator, name );
248 bool wxTextCtrl::Create( wxWindow *parent,
250 const wxString &value,
254 const wxValidator& validator,
255 const wxString &name )
258 m_acceptsFocus = true;
260 if (!PreCreation( parent, pos, size ) ||
261 !CreateBase( parent, id, pos, size, style, validator, name ))
263 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
268 m_vScrollbarVisible = false;
270 bool multi_line = (style & wxTE_MULTILINE) != 0;
274 // create our control ...
275 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
277 // ... and put into the upper left hand corner of the table
278 bool bHasHScrollbar = false;
279 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
280 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
281 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
282 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
283 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
287 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
289 // finally, put the vertical scrollbar in the upper right corner
290 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
291 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
292 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
294 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
299 // a single-line text control: no need for scrollbars
301 m_text = gtk_entry_new();
304 m_parent->DoAddChild( this );
306 m_focusWidget = m_text;
311 gtk_widget_show(m_text);
315 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
316 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
318 // only initialize gs_gtk_text_draw once, starting from the next the
319 // klass::draw will already be wxgtk_text_draw
320 if ( !gs_gtk_text_draw )
323 draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw;
325 gs_gtk_text_draw = draw;
327 draw = wxgtk_text_draw;
333 #if !GTK_CHECK_VERSION(1, 2, 0)
334 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
335 // gtk_editable_insert_text()
336 gtk_widget_realize(m_text);
341 wxWX2MBbuf val = value.mbc_str();
342 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
344 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.length(), &tmp );
349 // Bring editable's cursor uptodate. Bug in GTK.
350 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
354 if (style & wxTE_PASSWORD)
357 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
360 if (style & wxTE_READONLY)
363 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
368 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
371 // We want to be notified about text changes.
372 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
373 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
375 m_cursor = wxCursor( wxCURSOR_IBEAM );
377 wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
378 SetDefaultStyle( attrDef );
384 void wxTextCtrl::CalculateScrollbar()
386 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
388 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
390 if (adj->upper - adj->page_size < 0.8)
392 if (m_vScrollbarVisible)
394 gtk_widget_hide( m_vScrollbar );
395 m_vScrollbarVisible = false;
400 if (!m_vScrollbarVisible)
402 gtk_widget_show( m_vScrollbar );
403 m_vScrollbarVisible = true;
408 wxString wxTextCtrl::GetValue() const
410 wxCHECK_MSG( m_text != NULL, wxEmptyString, wxT("invalid text ctrl") );
413 if (m_windowStyle & wxTE_MULTILINE)
415 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
416 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
422 tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) );
428 void wxTextCtrl::DoSetValue( const wxString &value, int flags )
430 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
432 if ( !(flags & SetValue_SendEvent) )
434 // do not generate events
435 IgnoreNextTextUpdate();
438 if (m_windowStyle & wxTE_MULTILINE)
440 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
441 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
443 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.length(), &len );
447 gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) );
450 // GRG, Jun/2000: Changed this after a lot of discussion in
451 // the lists. wxWidgets 2.2 will have a set of flags to
452 // customize this behaviour.
453 SetInsertionPoint(0);
458 void wxTextCtrl::WriteText( const wxString &text )
460 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
465 // gtk_text_changed_callback() will set m_modified to true but m_modified
466 // shouldn't be changed by the program writing to the text control itself,
467 // so save the old value and restore when we're done
468 bool oldModified = m_modified;
470 if ( m_windowStyle & wxTE_MULTILINE )
472 // After cursor movements, gtk_text_get_point() is wrong by one.
473 gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) );
475 // always use m_defaultStyle, even if it is empty as otherwise
476 // resetting the style and appending some more text wouldn't work: if
477 // we don't specify the style explicitly, the old style would be used
478 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
479 wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.length());
481 // we called wxGtkTextInsert with correct font, no need to do anything
482 // in UpdateFontIfNeeded() any longer
485 SetUpdateFont(false);
488 // Bring editable's cursor back uptodate.
489 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
493 // First remove the selection if there is one
494 gtk_editable_delete_selection( GTK_EDITABLE(m_text) );
496 // This moves the cursor pos to behind the inserted text.
497 gint len = GET_EDITABLE_POS(m_text);
499 gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.length(), &len );
501 // Bring entry's cursor uptodate.
502 gtk_entry_set_position( GTK_ENTRY(m_text), len );
505 m_modified = oldModified;
508 void wxTextCtrl::AppendText( const wxString &text )
510 SetInsertionPointEnd();
514 wxString wxTextCtrl::GetLineText( long lineNo ) const
516 if (m_windowStyle & wxTE_MULTILINE)
518 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
519 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
526 for (i = 0; currentLine != lineNo && text[i]; i++ )
531 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
539 return wxEmptyString;
544 if (lineNo == 0) return GetValue();
545 return wxEmptyString;
549 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
551 /* If you implement this, don't forget to update the documentation!
552 * (file docs/latex/wx/text.tex) */
553 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
556 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
558 if ( m_windowStyle & wxTE_MULTILINE )
560 wxString text = GetValue();
562 // cast to prevent warning. But pos really should've been unsigned.
563 if( (unsigned long)pos > text.length() )
569 const wxChar* stop = text.c_str() + pos;
570 for ( const wxChar *p = text.c_str(); p < stop; p++ )
581 else // single line control
583 if ( pos <= GTK_ENTRY(m_text)->text_length )
590 // index out of bounds
598 long wxTextCtrl::XYToPosition(long x, long y ) const
600 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
603 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
609 int wxTextCtrl::GetLineLength(long lineNo) const
611 wxString str = GetLineText (lineNo);
612 return (int) str.length();
615 int wxTextCtrl::GetNumberOfLines() const
617 if (m_windowStyle & wxTE_MULTILINE)
619 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
620 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
625 for (int i = 0; i < len; i++ )
632 // currentLine is 0 based, add 1 to get number of lines
633 return currentLine + 1;
646 void wxTextCtrl::SetInsertionPoint( long pos )
648 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
652 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
653 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
655 /* we fake a set_point by inserting and deleting. as the user
656 isn't supposed to get to know about this non-sense, we
657 disconnect so that no events are sent to the user program. */
659 gint tmp = (gint)pos;
660 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
661 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
663 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
664 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
666 // bring editable's cursor uptodate. Bug in GTK.
667 SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) ));
671 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
673 // Bring editable's cursor uptodate. Bug in GTK.
674 SET_EDITABLE_POS(m_text, (guint32)pos);
678 void wxTextCtrl::SetInsertionPointEnd()
680 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
682 if (m_windowStyle & wxTE_MULTILINE)
684 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
688 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
692 void wxTextCtrl::SetEditable( bool editable )
694 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
696 if (m_windowStyle & wxTE_MULTILINE)
698 gtk_text_set_editable( GTK_TEXT(m_text), editable );
702 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
706 void wxTextCtrl::DoEnable( bool enable )
708 if (m_windowStyle & wxTE_MULTILINE)
710 gtk_text_set_editable( GTK_TEXT(m_text), enable );
714 gtk_widget_set_sensitive( m_text, enable );
718 // wxGTK-specific: called recursively by Enable,
719 // to give widgets an oppprtunity to correct their colours after they
720 // have been changed by Enable
721 void wxTextCtrl::OnEnabled( bool enable )
723 if ( IsSingleLine() )
726 // If we have a custom background colour, we use this colour in both
727 // disabled and enabled mode, or we end up with a different colour under the
729 wxColour oldColour = GetBackgroundColour();
732 // Need to set twice or it'll optimize the useful stuff out
733 if (oldColour == * wxWHITE)
734 SetBackgroundColour(*wxBLACK);
736 SetBackgroundColour(*wxWHITE);
737 SetBackgroundColour(oldColour);
741 void wxTextCtrl::MarkDirty()
746 void wxTextCtrl::DiscardEdits()
751 // ----------------------------------------------------------------------------
752 // max text length support
753 // ----------------------------------------------------------------------------
755 void wxTextCtrl::IgnoreNextTextUpdate()
757 m_ignoreNextUpdate = true;
760 bool wxTextCtrl::IgnoreTextUpdate()
762 if ( m_ignoreNextUpdate )
764 m_ignoreNextUpdate = false;
772 void wxTextCtrl::SetMaxLength(unsigned long len)
774 if ( !HasFlag(wxTE_MULTILINE) )
776 gtk_entry_set_max_length(GTK_ENTRY(m_text), len);
778 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
779 // we had tried to enter more text than allowed by max text length and
780 // the text wasn't really changed
782 // to detect this and generate TEXT_MAXLEN event instead of
783 // TEXT_CHANGED one in this case we also catch "insert_text" signal
785 // when max len is set to 0 we disconnect our handler as it means that
786 // we shouldn't check anything any more
789 gtk_signal_connect( GTK_OBJECT(m_text),
791 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
796 gtk_signal_disconnect_by_func
799 GTK_SIGNAL_FUNC(gtk_insert_text_callback),
806 void wxTextCtrl::SetSelection( long from, long to )
808 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
810 if (from == -1 && to == -1)
813 to = GetValue().length();
816 if ( (m_windowStyle & wxTE_MULTILINE) &&
817 !GTK_TEXT(m_text)->line_start_cache )
819 // tell the programmer that it didn't work
820 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
824 if (m_windowStyle & wxTE_MULTILINE)
826 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
830 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
834 void wxTextCtrl::ShowPosition( long pos )
836 if (m_windowStyle & wxTE_MULTILINE)
838 GtkAdjustment *vp = GTK_TEXT(m_text)->vadj;
839 float totalLines = (float) GetNumberOfLines();
842 PositionToXY(pos, &posX, &posY);
843 float posLine = (float) posY;
844 float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower;
845 gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p);
849 long wxTextCtrl::GetInsertionPoint() const
851 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
852 return (long) GET_EDITABLE_POS(m_text);
855 wxTextPos wxTextCtrl::GetLastPosition() const
857 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
861 if (m_windowStyle & wxTE_MULTILINE)
863 pos = gtk_text_get_length( GTK_TEXT(m_text) );
867 pos = GTK_ENTRY(m_text)->text_length;
873 void wxTextCtrl::Remove( long from, long to )
875 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
876 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
879 void wxTextCtrl::Replace( long from, long to, const wxString &value )
881 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
887 gint pos = (gint)from;
889 wxWX2MBbuf buf = value.mbc_str();
890 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
892 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.length(), &pos );
893 #endif // wxUSE_UNICODE
897 void wxTextCtrl::Cut()
899 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
900 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
903 void wxTextCtrl::Copy()
905 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") );
912 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG);
916 void wxTextCtrl::Undo()
919 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
922 void wxTextCtrl::Redo()
925 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
928 bool wxTextCtrl::CanUndo() const
931 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
935 bool wxTextCtrl::CanRedo() const
938 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
942 // If the return values from and to are the same, there is no
944 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
946 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
950 bool haveSelection = false;
952 if ( (GTK_EDITABLE(m_text)->has_selection) )
954 haveSelection = true;
955 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
956 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
959 if (! haveSelection )
960 from = to = GetInsertionPoint();
964 // exchange them to be compatible with wxMSW
977 bool wxTextCtrl::IsEditable() const
979 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
980 return GTK_EDITABLE(m_text)->editable;
983 bool wxTextCtrl::IsModified() const
988 void wxTextCtrl::Clear()
990 SetValue( wxEmptyString );
993 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
995 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
997 if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
999 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1000 event.SetEventObject(this);
1001 event.SetString(GetValue());
1002 if (GetEventHandler()->ProcessEvent(event)) return;
1005 if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1007 // This will invoke the dialog default action, such
1008 // as the clicking the default button.
1010 wxWindow *top_frame = m_parent;
1011 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1012 top_frame = top_frame->GetParent();
1014 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
1016 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1018 if (window->default_widget)
1020 gtk_widget_activate (window->default_widget);
1029 GtkWidget* wxTextCtrl::GetConnectWidget()
1031 return GTK_WIDGET(m_text);
1034 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1036 if (m_windowStyle & wxTE_MULTILINE)
1038 return (window == GTK_TEXT(m_text)->text_area);
1042 return (window == GTK_ENTRY(m_text)->text_area);
1046 // the font will change for subsequent text insertiongs
1047 bool wxTextCtrl::SetFont( const wxFont &font )
1049 wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") );
1051 if ( !wxTextCtrlBase::SetFont(font) )
1053 // font didn't change, nothing to do
1057 if ( m_windowStyle & wxTE_MULTILINE )
1059 SetUpdateFont(true);
1061 m_defaultStyle.SetFont(font);
1063 ChangeFontGlobally();
1069 void wxTextCtrl::ChangeFontGlobally()
1071 // this method is very inefficient and hence should be called as rarely as
1073 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.empty() )
1080 SetUpdateFont(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 if (!m_backgroundColour.Ok())
1117 if (m_windowStyle & wxTE_MULTILINE)
1119 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1122 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1123 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1124 gdk_window_clear( window );
1127 // change active background color too
1128 m_defaultStyle.SetBackgroundColour( colour );
1133 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style )
1135 if ( m_windowStyle & wxTE_MULTILINE )
1137 if ( style.IsDefault() )
1143 // VERY dirty way to do that - removes the required text and re-adds it
1144 // with styling (FIXME)
1146 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1148 wxCHECK_MSG( start >= 0 && end <= l, false,
1149 _T("invalid range in wxTextCtrl::SetStyle") );
1151 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1152 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1153 wxString tmp(text,*wxConvCurrent);
1156 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1157 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1160 wxWX2MBbuf buf = tmp.mbc_str();
1161 const char *txt = buf;
1162 size_t txtlen = strlen(buf);
1164 const char *txt = tmp;
1165 size_t txtlen = tmp.length();
1168 // use the attributes from style which are set in it and fall back
1169 // first to the default style and then to the text control default
1170 // colours for the others
1171 wxGtkTextInsert(m_text,
1172 wxTextAttr::Combine(style, m_defaultStyle, this),
1176 /* does not seem to help under GTK+ 1.2 !!!
1177 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1178 SetInsertionPoint( old_pos );
1184 // cannot do this for GTK+'s Entry widget
1188 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style)
1190 gtk_widget_modify_style(m_text, style);
1193 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1198 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1203 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1208 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1213 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1218 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1220 event.Enable( CanCut() );
1223 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1225 event.Enable( CanCopy() );
1228 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1230 event.Enable( CanPaste() );
1233 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1235 event.Enable( CanUndo() );
1238 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1240 event.Enable( CanRedo() );
1243 void wxTextCtrl::OnInternalIdle()
1245 wxCursor cursor = m_cursor;
1246 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1250 GdkWindow *window = (GdkWindow*) NULL;
1251 if (HasFlag(wxTE_MULTILINE))
1252 window = GTK_TEXT(m_text)->text_area;
1254 window = GTK_ENTRY(m_text)->text_area;
1257 gdk_window_set_cursor( window, cursor.GetCursor() );
1259 if (!g_globalCursor.Ok())
1260 cursor = *wxSTANDARD_CURSOR;
1262 window = m_widget->window;
1263 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1264 gdk_window_set_cursor( window, cursor.GetCursor() );
1267 if (g_delayedFocus == this)
1269 if (GTK_WIDGET_REALIZED(m_widget))
1271 gtk_widget_grab_focus( m_widget );
1272 g_delayedFocus = NULL;
1276 if (wxUpdateUIEvent::CanUpdate(this))
1277 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1280 wxSize wxTextCtrl::DoGetBestSize() const
1282 // FIXME should be different for multi-line controls...
1283 wxSize ret( wxControl::DoGetBestSize() );
1284 wxSize best(80, ret.y);
1285 CacheBestSize(best);
1289 // ----------------------------------------------------------------------------
1291 // ----------------------------------------------------------------------------
1293 void wxTextCtrl::Freeze()
1295 if ( HasFlag(wxTE_MULTILINE) )
1297 gtk_text_freeze(GTK_TEXT(m_text));
1301 void wxTextCtrl::Thaw()
1303 if ( HasFlag(wxTE_MULTILINE) )
1305 GTK_TEXT(m_text)->vadj->value = 0.0;
1307 gtk_text_thaw(GTK_TEXT(m_text));
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1315 GtkAdjustment *wxTextCtrl::GetVAdj() const
1317 if ( !IsMultiLine() )
1320 return GTK_TEXT(m_text)->vadj;
1323 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1325 float value = adj->value + diff;
1330 float upper = adj->upper - adj->page_size;
1331 if ( value > upper )
1334 // did we noticeably change the scroll position?
1335 if ( fabs(adj->value - value) < 0.2 )
1337 // well, this is what Robert does in wxScrollBar, so it must be good...
1342 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1347 bool wxTextCtrl::ScrollLines(int lines)
1349 GtkAdjustment *adj = GetVAdj();
1353 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1354 int diff = 10*lines;
1356 return DoScroll(adj, diff);
1359 bool wxTextCtrl::ScrollPages(int pages)
1361 GtkAdjustment *adj = GetVAdj();
1365 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1371 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1373 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);