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())
28 #include <sys/types.h>
32 #include "wx/gtk1/private.h"
33 #include <gdk/gdkkeysyms.h>
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern void wxapp_install_idle_handler();
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern wxCursor g_globalCursor
;
47 extern wxWindowGTK
*g_delayedFocus
;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
54 static void wxGtkTextInsert(GtkWidget
*text
,
55 const wxTextAttr
& attr
,
63 tmpFont
= attr
.GetFont();
65 // FIXME: if this crashes because tmpFont goes out of scope and the GdkFont is
66 // deleted, then we need to call gdk_font_ref on font.
67 // This is because attr.GetFont() now returns a temporary font since wxTextAttr
68 // no longer stores a wxFont object, for efficiency.
70 font
= tmpFont
.GetInternalFont();
75 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
78 GdkColor
*colBg
= attr
.HasBackgroundColour()
79 ? attr
.GetBackgroundColour().GetColor()
82 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
86 // ----------------------------------------------------------------------------
87 // "insert_text" for GtkEntry
88 // ----------------------------------------------------------------------------
92 gtk_insert_text_callback(GtkEditable
*editable
,
93 const gchar
*WXUNUSED(new_text
),
94 gint
WXUNUSED(new_text_length
),
95 gint
*WXUNUSED(position
),
99 wxapp_install_idle_handler();
101 // we should only be called if we have a max len limit at all
102 GtkEntry
*entry
= GTK_ENTRY (editable
);
104 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
106 // check that we don't overflow the max length limit
108 // FIXME: this doesn't work when we paste a string which is going to be
110 if ( entry
->text_length
== entry
->text_max_length
)
112 // we don't need to run the base class version at all
113 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
115 // remember that the next changed signal is to be ignored to avoid
116 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
117 win
->IgnoreNextTextUpdate();
119 // and generate the correct one ourselves
120 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
121 event
.SetEventObject(win
);
122 event
.SetString(win
->GetValue());
123 win
->HandleWindowEvent( event
);
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
134 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
136 if ( win
->IgnoreTextUpdate() )
139 if (!win
->m_hasVMT
) return;
142 wxapp_install_idle_handler();
145 win
->UpdateFontIfNeeded();
147 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
148 event
.SetEventObject( win
);
149 win
->HandleWindowEvent( event
);
153 //-----------------------------------------------------------------------------
154 // "changed" from vertical scrollbar
155 //-----------------------------------------------------------------------------
159 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
161 if (!win
->m_hasVMT
) return;
164 wxapp_install_idle_handler();
166 win
->CalculateScrollbar();
170 // ----------------------------------------------------------------------------
171 // redraw callback for multiline text
172 // ----------------------------------------------------------------------------
174 // redrawing a GtkText from inside a wxYield() call results in crashes (the
175 // text sample shows it in its "Add lines" command which shows wxProgressDialog
176 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
177 // don't do anything if we're inside wxYield()
180 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
183 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
186 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
188 if ( !wxTheApp
->IsYielding() )
190 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
191 _T("infinite recursion in wxgtk_text_draw aborted") );
193 gs_gtk_text_draw(widget
, rect
);
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
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::GetValue() 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 // wxGTK-specific: called recursively by Enable,
718 // to give widgets an oppprtunity to correct their colours after they
719 // have been changed by Enable
720 void wxTextCtrl::OnEnabled( bool WXUNUSED(enable
) )
722 if ( IsSingleLine() )
725 // If we have a custom background colour, we use this colour in both
726 // disabled and enabled mode, or we end up with a different colour under the
728 wxColour oldColour
= GetBackgroundColour();
731 // Need to set twice or it'll optimize the useful stuff out
732 if (oldColour
== * wxWHITE
)
733 SetBackgroundColour(*wxBLACK
);
735 SetBackgroundColour(*wxWHITE
);
736 SetBackgroundColour(oldColour
);
740 void wxTextCtrl::MarkDirty()
745 void wxTextCtrl::DiscardEdits()
750 // ----------------------------------------------------------------------------
751 // max text length support
752 // ----------------------------------------------------------------------------
754 void wxTextCtrl::IgnoreNextTextUpdate()
756 m_ignoreNextUpdate
= true;
759 bool wxTextCtrl::IgnoreTextUpdate()
761 if ( m_ignoreNextUpdate
)
763 m_ignoreNextUpdate
= false;
771 void wxTextCtrl::SetMaxLength(unsigned long len
)
773 if ( !HasFlag(wxTE_MULTILINE
) )
775 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
777 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
778 // we had tried to enter more text than allowed by max text length and
779 // the text wasn't really changed
781 // to detect this and generate TEXT_MAXLEN event instead of
782 // TEXT_CHANGED one in this case we also catch "insert_text" signal
784 // when max len is set to 0 we disconnect our handler as it means that
785 // we shouldn't check anything any more
788 gtk_signal_connect( GTK_OBJECT(m_text
),
790 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
795 gtk_signal_disconnect_by_func
798 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
805 void wxTextCtrl::SetSelection( long from
, long to
)
807 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
809 if (from
== -1 && to
== -1)
812 to
= GetValue().length();
815 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
816 !GTK_TEXT(m_text
)->line_start_cache
)
818 // tell the programmer that it didn't work
819 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
823 if (m_windowStyle
& wxTE_MULTILINE
)
825 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
829 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
833 void wxTextCtrl::ShowPosition( long pos
)
835 if (m_windowStyle
& wxTE_MULTILINE
)
837 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
838 float totalLines
= (float) GetNumberOfLines();
841 PositionToXY(pos
, &posX
, &posY
);
842 float posLine
= (float) posY
;
843 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
844 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
848 long wxTextCtrl::GetInsertionPoint() const
850 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
851 return (long) GET_EDITABLE_POS(m_text
);
854 wxTextPos
wxTextCtrl::GetLastPosition() const
856 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
860 if (m_windowStyle
& wxTE_MULTILINE
)
862 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
866 pos
= GTK_ENTRY(m_text
)->text_length
;
872 void wxTextCtrl::Remove( long from
, long to
)
874 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
875 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
878 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
880 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
886 gint pos
= (gint
)from
;
888 wxWX2MBbuf buf
= value
.mbc_str();
889 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
891 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &pos
);
892 #endif // wxUSE_UNICODE
896 void wxTextCtrl::Cut()
898 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") );
905 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
908 void wxTextCtrl::Paste()
910 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
911 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
915 void wxTextCtrl::Undo()
918 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
921 void wxTextCtrl::Redo()
924 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
927 bool wxTextCtrl::CanUndo() const
930 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
934 bool wxTextCtrl::CanRedo() const
937 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
941 // If the return values from and to are the same, there is no
943 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
945 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
949 bool haveSelection
= false;
951 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
953 haveSelection
= true;
954 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
955 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
958 if (! haveSelection
)
959 from
= to
= GetInsertionPoint();
963 // exchange them to be compatible with wxMSW
976 bool wxTextCtrl::IsEditable() const
978 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
979 return GTK_EDITABLE(m_text
)->editable
;
982 bool wxTextCtrl::IsModified() const
987 void wxTextCtrl::Clear()
989 SetValue( wxEmptyString
);
992 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
994 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
996 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
998 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
999 event
.SetEventObject(this);
1000 event
.SetString(GetValue());
1001 if (HandleWindowEvent(event
)) return;
1004 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1006 // This will invoke the dialog default action, such
1007 // as the clicking the default button.
1009 wxWindow
*top_frame
= m_parent
;
1010 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1011 top_frame
= top_frame
->GetParent();
1013 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1015 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1017 if (window
->default_widget
)
1019 gtk_widget_activate (window
->default_widget
);
1028 GtkWidget
* wxTextCtrl::GetConnectWidget()
1030 return GTK_WIDGET(m_text
);
1033 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1035 if (m_windowStyle
& wxTE_MULTILINE
)
1037 return (window
== GTK_TEXT(m_text
)->text_area
);
1041 return (window
== GTK_ENTRY(m_text
)->text_area
);
1045 // the font will change for subsequent text insertiongs
1046 bool wxTextCtrl::SetFont( const wxFont
&font
)
1048 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1050 if ( !wxTextCtrlBase::SetFont(font
) )
1052 // font didn't change, nothing to do
1056 if ( m_windowStyle
& wxTE_MULTILINE
)
1058 SetUpdateFont(true);
1060 m_defaultStyle
.SetFont(font
);
1062 ChangeFontGlobally();
1068 void wxTextCtrl::ChangeFontGlobally()
1070 // this method is very inefficient and hence should be called as rarely as
1072 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1074 _T("shouldn't be called for single line controls") );
1076 wxString value
= GetValue();
1077 if ( !value
.empty() )
1079 SetUpdateFont(false);
1086 void wxTextCtrl::UpdateFontIfNeeded()
1089 ChangeFontGlobally();
1092 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1094 if ( !wxControl::SetForegroundColour(colour
) )
1097 // update default fg colour too
1098 m_defaultStyle
.SetTextColour(colour
);
1103 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1105 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1107 if ( !wxControl::SetBackgroundColour( colour
) )
1110 if (!m_widget
->window
)
1113 if (!m_backgroundColour
.Ok())
1116 if (m_windowStyle
& wxTE_MULTILINE
)
1118 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1121 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1122 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1123 gdk_window_clear( window
);
1126 // change active background color too
1127 m_defaultStyle
.SetBackgroundColour( colour
);
1132 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1134 if ( m_windowStyle
& wxTE_MULTILINE
)
1136 if ( style
.IsDefault() )
1142 // VERY dirty way to do that - removes the required text and re-adds it
1143 // with styling (FIXME)
1145 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1147 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1148 _T("invalid range in wxTextCtrl::SetStyle") );
1150 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1151 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1152 wxString
tmp(text
,*wxConvCurrent
);
1155 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1156 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1159 wxWX2MBbuf buf
= tmp
.mbc_str();
1160 const char *txt
= buf
;
1161 size_t txtlen
= strlen(buf
);
1163 const char *txt
= tmp
;
1164 size_t txtlen
= tmp
.length();
1167 // use the attributes from style which are set in it and fall back
1168 // first to the default style and then to the text control default
1169 // colours for the others
1170 wxGtkTextInsert(m_text
,
1171 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1175 /* does not seem to help under GTK+ 1.2 !!!
1176 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1177 SetInsertionPoint( old_pos
);
1183 // cannot do this for GTK+'s Entry widget
1187 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1189 gtk_widget_modify_style(m_text
, style
);
1192 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1197 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1202 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1207 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1212 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1217 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1219 event
.Enable( CanCut() );
1222 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1224 event
.Enable( CanCopy() );
1227 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1229 event
.Enable( CanPaste() );
1232 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1234 event
.Enable( CanUndo() );
1237 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1239 event
.Enable( CanRedo() );
1242 void wxTextCtrl::OnInternalIdle()
1244 wxCursor cursor
= m_cursor
;
1245 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1249 GdkWindow
*window
= NULL
;
1250 if (HasFlag(wxTE_MULTILINE
))
1251 window
= GTK_TEXT(m_text
)->text_area
;
1253 window
= GTK_ENTRY(m_text
)->text_area
;
1256 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1258 if (!g_globalCursor
.Ok())
1259 cursor
= *wxSTANDARD_CURSOR
;
1261 window
= m_widget
->window
;
1262 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1263 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1266 if (g_delayedFocus
== this)
1268 if (GTK_WIDGET_REALIZED(m_widget
))
1270 gtk_widget_grab_focus( m_widget
);
1271 g_delayedFocus
= NULL
;
1275 if (wxUpdateUIEvent::CanUpdate(this))
1276 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1279 wxSize
wxTextCtrl::DoGetBestSize() const
1281 // FIXME should be different for multi-line controls...
1282 wxSize
ret( wxControl::DoGetBestSize() );
1283 wxSize
best(80, ret
.y
);
1284 CacheBestSize(best
);
1288 // ----------------------------------------------------------------------------
1290 // ----------------------------------------------------------------------------
1292 void wxTextCtrl::DoFreeze()
1294 if ( HasFlag(wxTE_MULTILINE
) )
1296 gtk_text_freeze(GTK_TEXT(m_text
));
1300 void wxTextCtrl::DoThaw()
1302 if ( HasFlag(wxTE_MULTILINE
) )
1304 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1306 gtk_text_thaw(GTK_TEXT(m_text
));
1310 // ----------------------------------------------------------------------------
1312 // ----------------------------------------------------------------------------
1314 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1316 if ( !IsMultiLine() )
1319 return GTK_TEXT(m_text
)->vadj
;
1322 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1324 float value
= adj
->value
+ diff
;
1329 float upper
= adj
->upper
- adj
->page_size
;
1330 if ( value
> upper
)
1333 // did we noticeably change the scroll position?
1334 if ( fabs(adj
->value
- value
) < 0.2 )
1336 // well, this is what Robert does in wxScrollBar, so it must be good...
1341 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1346 bool wxTextCtrl::ScrollLines(int lines
)
1348 GtkAdjustment
*adj
= GetVAdj();
1352 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1353 int diff
= 10*lines
;
1355 return DoScroll(adj
, diff
);
1358 bool wxTextCtrl::ScrollPages(int pages
)
1360 GtkAdjustment
*adj
= GetVAdj();
1364 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
1370 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1372 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);