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
*WXUNUSED(new_text
),
93 gint
WXUNUSED(new_text_length
),
94 gint
*WXUNUSED(position
),
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
->HandleWindowEvent( event
);
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
133 gtk_text_changed_callback( GtkWidget
*WXUNUSED(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
->HandleWindowEvent( 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()
179 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
182 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
185 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
187 if ( !wxTheApp
->IsYielding() )
189 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
190 _T("infinite recursion in wxgtk_text_draw aborted") );
192 gs_gtk_text_draw(widget
, rect
);
197 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
201 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
203 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
204 EVT_CHAR(wxTextCtrl::OnChar
)
206 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
207 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
208 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
209 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
210 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
212 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
213 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
214 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
215 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
216 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
219 void wxTextCtrl::Init()
223 SetUpdateFont(false);
225 m_vScrollbar
= (GtkWidget
*)NULL
;
228 wxTextCtrl::~wxTextCtrl()
232 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
234 const wxString
&value
,
238 const wxValidator
& validator
,
239 const wxString
&name
)
243 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
246 bool wxTextCtrl::Create( wxWindow
*parent
,
248 const wxString
&value
,
252 const wxValidator
& validator
,
253 const wxString
&name
)
256 m_acceptsFocus
= true;
258 if (!PreCreation( parent
, pos
, size
) ||
259 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
261 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
266 m_vScrollbarVisible
= false;
268 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
272 // create our control ...
273 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
275 // ... and put into the upper left hand corner of the table
276 bool bHasHScrollbar
= false;
277 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
278 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
279 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
280 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
281 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
285 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
287 // finally, put the vertical scrollbar in the upper right corner
288 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
289 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
290 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
292 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
297 // a single-line text control: no need for scrollbars
299 m_text
= gtk_entry_new();
302 m_parent
->DoAddChild( this );
304 m_focusWidget
= m_text
;
309 gtk_widget_show(m_text
);
313 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
314 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
316 // only initialize gs_gtk_text_draw once, starting from the next the
317 // klass::draw will already be wxgtk_text_draw
318 if ( !gs_gtk_text_draw
)
321 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
323 gs_gtk_text_draw
= draw
;
325 draw
= wxgtk_text_draw
;
331 #if !GTK_CHECK_VERSION(1, 2, 0)
332 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
333 // gtk_editable_insert_text()
334 gtk_widget_realize(m_text
);
339 wxWX2MBbuf val
= value
.mbc_str();
340 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
342 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &tmp
);
347 // Bring editable's cursor uptodate. Bug in GTK.
348 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
352 if (style
& wxTE_PASSWORD
)
355 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
358 if (style
& wxTE_READONLY
)
361 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
366 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
369 // We want to be notified about text changes.
370 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
371 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
373 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
375 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
376 SetDefaultStyle( attrDef
);
382 void wxTextCtrl::CalculateScrollbar()
384 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
386 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
388 if (adj
->upper
- adj
->page_size
< 0.8)
390 if (m_vScrollbarVisible
)
392 gtk_widget_hide( m_vScrollbar
);
393 m_vScrollbarVisible
= false;
398 if (!m_vScrollbarVisible
)
400 gtk_widget_show( m_vScrollbar
);
401 m_vScrollbarVisible
= true;
406 wxString
wxTextCtrl::GetValue() const
408 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
411 if (m_windowStyle
& wxTE_MULTILINE
)
413 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
414 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
420 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
426 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
428 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
430 if ( !(flags
& SetValue_SendEvent
) )
432 // do not generate events
433 IgnoreNextTextUpdate();
436 if (m_windowStyle
& wxTE_MULTILINE
)
438 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
439 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
441 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.length(), &len
);
445 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
448 // GRG, Jun/2000: Changed this after a lot of discussion in
449 // the lists. wxWidgets 2.2 will have a set of flags to
450 // customize this behaviour.
451 SetInsertionPoint(0);
456 void wxTextCtrl::WriteText( const wxString
&text
)
458 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
463 // gtk_text_changed_callback() will set m_modified to true but m_modified
464 // shouldn't be changed by the program writing to the text control itself,
465 // so save the old value and restore when we're done
466 bool oldModified
= m_modified
;
468 if ( m_windowStyle
& wxTE_MULTILINE
)
470 // After cursor movements, gtk_text_get_point() is wrong by one.
471 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
473 // always use m_defaultStyle, even if it is empty as otherwise
474 // resetting the style and appending some more text wouldn't work: if
475 // we don't specify the style explicitly, the old style would be used
476 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
477 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.length());
479 // we called wxGtkTextInsert with correct font, no need to do anything
480 // in UpdateFontIfNeeded() any longer
483 SetUpdateFont(false);
486 // Bring editable's cursor back uptodate.
487 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
491 // First remove the selection if there is one
492 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
494 // This moves the cursor pos to behind the inserted text.
495 gint len
= GET_EDITABLE_POS(m_text
);
497 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.length(), &len
);
499 // Bring entry's cursor uptodate.
500 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
503 m_modified
= oldModified
;
506 void wxTextCtrl::AppendText( const wxString
&text
)
508 SetInsertionPointEnd();
512 wxString
wxTextCtrl::GetLineText( long lineNo
) const
514 if (m_windowStyle
& wxTE_MULTILINE
)
516 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
517 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
524 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
529 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
537 return wxEmptyString
;
542 if (lineNo
== 0) return GetValue();
543 return wxEmptyString
;
547 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
549 /* If you implement this, don't forget to update the documentation!
550 * (file docs/latex/wx/text.tex) */
551 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
554 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
556 if ( m_windowStyle
& wxTE_MULTILINE
)
558 wxString text
= GetValue();
560 // cast to prevent warning. But pos really should've been unsigned.
561 if( (unsigned long)pos
> text
.length() )
567 const wxChar
* stop
= text
.c_str() + pos
;
568 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
579 else // single line control
581 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
588 // index out of bounds
596 long wxTextCtrl::XYToPosition(long x
, long y
) const
598 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
601 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
607 int wxTextCtrl::GetLineLength(long lineNo
) const
609 wxString str
= GetLineText (lineNo
);
610 return (int) str
.length();
613 int wxTextCtrl::GetNumberOfLines() const
615 if (m_windowStyle
& wxTE_MULTILINE
)
617 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
618 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
623 for (int i
= 0; i
< len
; i
++ )
630 // currentLine is 0 based, add 1 to get number of lines
631 return currentLine
+ 1;
644 void wxTextCtrl::SetInsertionPoint( long pos
)
646 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
650 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
651 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
653 /* we fake a set_point by inserting and deleting. as the user
654 isn't supposed to get to know about this non-sense, we
655 disconnect so that no events are sent to the user program. */
657 gint tmp
= (gint
)pos
;
658 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
659 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
661 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
662 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
664 // bring editable's cursor uptodate. Bug in GTK.
665 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
669 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
671 // Bring editable's cursor uptodate. Bug in GTK.
672 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
676 void wxTextCtrl::SetInsertionPointEnd()
678 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
680 if (m_windowStyle
& wxTE_MULTILINE
)
682 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
686 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
690 void wxTextCtrl::SetEditable( bool editable
)
692 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
694 if (m_windowStyle
& wxTE_MULTILINE
)
696 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
700 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
704 void wxTextCtrl::DoEnable( bool enable
)
706 if (m_windowStyle
& wxTE_MULTILINE
)
708 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
712 gtk_widget_set_sensitive( m_text
, enable
);
716 // wxGTK-specific: called recursively by Enable,
717 // to give widgets an oppprtunity to correct their colours after they
718 // have been changed by Enable
719 void wxTextCtrl::OnEnabled( bool WXUNUSED(enable
) )
721 if ( IsSingleLine() )
724 // If we have a custom background colour, we use this colour in both
725 // disabled and enabled mode, or we end up with a different colour under the
727 wxColour oldColour
= GetBackgroundColour();
730 // Need to set twice or it'll optimize the useful stuff out
731 if (oldColour
== * wxWHITE
)
732 SetBackgroundColour(*wxBLACK
);
734 SetBackgroundColour(*wxWHITE
);
735 SetBackgroundColour(oldColour
);
739 void wxTextCtrl::MarkDirty()
744 void wxTextCtrl::DiscardEdits()
749 // ----------------------------------------------------------------------------
750 // max text length support
751 // ----------------------------------------------------------------------------
753 void wxTextCtrl::IgnoreNextTextUpdate()
755 m_ignoreNextUpdate
= true;
758 bool wxTextCtrl::IgnoreTextUpdate()
760 if ( m_ignoreNextUpdate
)
762 m_ignoreNextUpdate
= false;
770 void wxTextCtrl::SetMaxLength(unsigned long len
)
772 if ( !HasFlag(wxTE_MULTILINE
) )
774 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
776 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
777 // we had tried to enter more text than allowed by max text length and
778 // the text wasn't really changed
780 // to detect this and generate TEXT_MAXLEN event instead of
781 // TEXT_CHANGED one in this case we also catch "insert_text" signal
783 // when max len is set to 0 we disconnect our handler as it means that
784 // we shouldn't check anything any more
787 gtk_signal_connect( GTK_OBJECT(m_text
),
789 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
794 gtk_signal_disconnect_by_func
797 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
804 void wxTextCtrl::SetSelection( long from
, long to
)
806 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
808 if (from
== -1 && to
== -1)
811 to
= GetValue().length();
814 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
815 !GTK_TEXT(m_text
)->line_start_cache
)
817 // tell the programmer that it didn't work
818 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
822 if (m_windowStyle
& wxTE_MULTILINE
)
824 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
828 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
832 void wxTextCtrl::ShowPosition( long pos
)
834 if (m_windowStyle
& wxTE_MULTILINE
)
836 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
837 float totalLines
= (float) GetNumberOfLines();
840 PositionToXY(pos
, &posX
, &posY
);
841 float posLine
= (float) posY
;
842 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
843 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
847 long wxTextCtrl::GetInsertionPoint() const
849 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
850 return (long) GET_EDITABLE_POS(m_text
);
853 wxTextPos
wxTextCtrl::GetLastPosition() const
855 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
859 if (m_windowStyle
& wxTE_MULTILINE
)
861 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
865 pos
= GTK_ENTRY(m_text
)->text_length
;
871 void wxTextCtrl::Remove( long from
, long to
)
873 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
874 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
877 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
879 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
885 gint pos
= (gint
)from
;
887 wxWX2MBbuf buf
= value
.mbc_str();
888 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
890 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &pos
);
891 #endif // wxUSE_UNICODE
895 void wxTextCtrl::Cut()
897 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
898 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
901 void wxTextCtrl::Copy()
903 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
904 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
907 void wxTextCtrl::Paste()
909 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
910 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
914 void wxTextCtrl::Undo()
917 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
920 void wxTextCtrl::Redo()
923 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
926 bool wxTextCtrl::CanUndo() const
929 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
933 bool wxTextCtrl::CanRedo() const
936 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
940 // If the return values from and to are the same, there is no
942 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
944 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
948 bool haveSelection
= false;
950 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
952 haveSelection
= true;
953 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
954 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
957 if (! haveSelection
)
958 from
= to
= GetInsertionPoint();
962 // exchange them to be compatible with wxMSW
975 bool wxTextCtrl::IsEditable() const
977 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
978 return GTK_EDITABLE(m_text
)->editable
;
981 bool wxTextCtrl::IsModified() const
986 void wxTextCtrl::Clear()
988 SetValue( wxEmptyString
);
991 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
993 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
995 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
997 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
998 event
.SetEventObject(this);
999 event
.SetString(GetValue());
1000 if (HandleWindowEvent(event
)) return;
1003 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1005 // This will invoke the dialog default action, such
1006 // as the clicking the default button.
1008 wxWindow
*top_frame
= m_parent
;
1009 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1010 top_frame
= top_frame
->GetParent();
1012 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1014 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1016 if (window
->default_widget
)
1018 gtk_widget_activate (window
->default_widget
);
1027 GtkWidget
* wxTextCtrl::GetConnectWidget()
1029 return GTK_WIDGET(m_text
);
1032 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1034 if (m_windowStyle
& wxTE_MULTILINE
)
1036 return (window
== GTK_TEXT(m_text
)->text_area
);
1040 return (window
== GTK_ENTRY(m_text
)->text_area
);
1044 // the font will change for subsequent text insertiongs
1045 bool wxTextCtrl::SetFont( const wxFont
&font
)
1047 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1049 if ( !wxTextCtrlBase::SetFont(font
) )
1051 // font didn't change, nothing to do
1055 if ( m_windowStyle
& wxTE_MULTILINE
)
1057 SetUpdateFont(true);
1059 m_defaultStyle
.SetFont(font
);
1061 ChangeFontGlobally();
1067 void wxTextCtrl::ChangeFontGlobally()
1069 // this method is very inefficient and hence should be called as rarely as
1071 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1073 _T("shouldn't be called for single line controls") );
1075 wxString value
= GetValue();
1076 if ( !value
.empty() )
1078 SetUpdateFont(false);
1085 void wxTextCtrl::UpdateFontIfNeeded()
1088 ChangeFontGlobally();
1091 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1093 if ( !wxControl::SetForegroundColour(colour
) )
1096 // update default fg colour too
1097 m_defaultStyle
.SetTextColour(colour
);
1102 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1104 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1106 if ( !wxControl::SetBackgroundColour( colour
) )
1109 if (!m_widget
->window
)
1112 if (!m_backgroundColour
.Ok())
1115 if (m_windowStyle
& wxTE_MULTILINE
)
1117 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1120 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1121 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1122 gdk_window_clear( window
);
1125 // change active background color too
1126 m_defaultStyle
.SetBackgroundColour( colour
);
1131 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1133 if ( m_windowStyle
& wxTE_MULTILINE
)
1135 if ( style
.IsDefault() )
1141 // VERY dirty way to do that - removes the required text and re-adds it
1142 // with styling (FIXME)
1144 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1146 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1147 _T("invalid range in wxTextCtrl::SetStyle") );
1149 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1150 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1151 wxString
tmp(text
,*wxConvCurrent
);
1154 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1155 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1158 wxWX2MBbuf buf
= tmp
.mbc_str();
1159 const char *txt
= buf
;
1160 size_t txtlen
= strlen(buf
);
1162 const char *txt
= tmp
;
1163 size_t txtlen
= tmp
.length();
1166 // use the attributes from style which are set in it and fall back
1167 // first to the default style and then to the text control default
1168 // colours for the others
1169 wxGtkTextInsert(m_text
,
1170 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1174 /* does not seem to help under GTK+ 1.2 !!!
1175 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1176 SetInsertionPoint( old_pos
);
1182 // cannot do this for GTK+'s Entry widget
1186 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1188 gtk_widget_modify_style(m_text
, style
);
1191 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1196 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1201 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1206 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1211 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1216 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1218 event
.Enable( CanCut() );
1221 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1223 event
.Enable( CanCopy() );
1226 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1228 event
.Enable( CanPaste() );
1231 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1233 event
.Enable( CanUndo() );
1236 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1238 event
.Enable( CanRedo() );
1241 void wxTextCtrl::OnInternalIdle()
1243 wxCursor cursor
= m_cursor
;
1244 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1248 GdkWindow
*window
= (GdkWindow
*) NULL
;
1249 if (HasFlag(wxTE_MULTILINE
))
1250 window
= GTK_TEXT(m_text
)->text_area
;
1252 window
= GTK_ENTRY(m_text
)->text_area
;
1255 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1257 if (!g_globalCursor
.Ok())
1258 cursor
= *wxSTANDARD_CURSOR
;
1260 window
= m_widget
->window
;
1261 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1262 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1265 if (g_delayedFocus
== this)
1267 if (GTK_WIDGET_REALIZED(m_widget
))
1269 gtk_widget_grab_focus( m_widget
);
1270 g_delayedFocus
= NULL
;
1274 if (wxUpdateUIEvent::CanUpdate(this))
1275 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1278 wxSize
wxTextCtrl::DoGetBestSize() const
1280 // FIXME should be different for multi-line controls...
1281 wxSize
ret( wxControl::DoGetBestSize() );
1282 wxSize
best(80, ret
.y
);
1283 CacheBestSize(best
);
1287 // ----------------------------------------------------------------------------
1289 // ----------------------------------------------------------------------------
1291 void wxTextCtrl::DoFreeze()
1293 if ( HasFlag(wxTE_MULTILINE
) )
1295 gtk_text_freeze(GTK_TEXT(m_text
));
1299 void wxTextCtrl::DoThaw()
1301 if ( HasFlag(wxTE_MULTILINE
) )
1303 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1305 gtk_text_thaw(GTK_TEXT(m_text
));
1309 // ----------------------------------------------------------------------------
1311 // ----------------------------------------------------------------------------
1313 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1315 if ( !IsMultiLine() )
1318 return GTK_TEXT(m_text
)->vadj
;
1321 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1323 float value
= adj
->value
+ diff
;
1328 float upper
= adj
->upper
- adj
->page_size
;
1329 if ( value
> upper
)
1332 // did we noticeably change the scroll position?
1333 if ( fabs(adj
->value
- value
) < 0.2 )
1335 // well, this is what Robert does in wxScrollBar, so it must be good...
1340 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1345 bool wxTextCtrl::ScrollLines(int lines
)
1347 GtkAdjustment
*adj
= GetVAdj();
1351 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1352 int diff
= 10*lines
;
1354 return DoScroll(adj
, diff
);
1357 bool wxTextCtrl::ScrollPages(int pages
)
1359 GtkAdjustment
*adj
= GetVAdj();
1363 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
1369 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1371 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);